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-2020 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:
191 static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
193 return nl80211_chan_width_to_mhz(c->width);
196 bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
198 u32 control_freq, oper_freq;
199 int oper_width, control_width;
204 if (chandef->freq1_offset >= 1000)
207 control_freq = chandef->chan->center_freq;
209 switch (chandef->width) {
210 case NL80211_CHAN_WIDTH_5:
211 case NL80211_CHAN_WIDTH_10:
212 case NL80211_CHAN_WIDTH_20:
213 case NL80211_CHAN_WIDTH_20_NOHT:
214 if (ieee80211_chandef_to_khz(chandef) !=
215 ieee80211_channel_to_khz(chandef->chan))
217 if (chandef->center_freq2)
220 case NL80211_CHAN_WIDTH_1:
221 case NL80211_CHAN_WIDTH_2:
222 case NL80211_CHAN_WIDTH_4:
223 case NL80211_CHAN_WIDTH_8:
224 case NL80211_CHAN_WIDTH_16:
225 if (chandef->chan->band != NL80211_BAND_S1GHZ)
228 control_freq = ieee80211_channel_to_khz(chandef->chan);
229 oper_freq = ieee80211_chandef_to_khz(chandef);
230 control_width = nl80211_chan_width_to_mhz(
231 ieee80211_s1g_channel_width(
233 oper_width = cfg80211_chandef_get_width(chandef);
235 if (oper_width < 0 || control_width < 0)
237 if (chandef->center_freq2)
240 if (control_freq + MHZ_TO_KHZ(control_width) / 2 >
241 oper_freq + MHZ_TO_KHZ(oper_width) / 2)
244 if (control_freq - MHZ_TO_KHZ(control_width) / 2 <
245 oper_freq - MHZ_TO_KHZ(oper_width) / 2)
248 case NL80211_CHAN_WIDTH_40:
249 if (chandef->center_freq1 != control_freq + 10 &&
250 chandef->center_freq1 != control_freq - 10)
252 if (chandef->center_freq2)
255 case NL80211_CHAN_WIDTH_80P80:
256 if (chandef->center_freq1 != control_freq + 30 &&
257 chandef->center_freq1 != control_freq + 10 &&
258 chandef->center_freq1 != control_freq - 10 &&
259 chandef->center_freq1 != control_freq - 30)
261 if (!chandef->center_freq2)
263 /* adjacent is not allowed -- that's a 160 MHz channel */
264 if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
265 chandef->center_freq2 - chandef->center_freq1 == 80)
268 case NL80211_CHAN_WIDTH_80:
269 if (chandef->center_freq1 != control_freq + 30 &&
270 chandef->center_freq1 != control_freq + 10 &&
271 chandef->center_freq1 != control_freq - 10 &&
272 chandef->center_freq1 != control_freq - 30)
274 if (chandef->center_freq2)
277 case NL80211_CHAN_WIDTH_160:
278 if (chandef->center_freq1 != control_freq + 70 &&
279 chandef->center_freq1 != control_freq + 50 &&
280 chandef->center_freq1 != control_freq + 30 &&
281 chandef->center_freq1 != control_freq + 10 &&
282 chandef->center_freq1 != control_freq - 10 &&
283 chandef->center_freq1 != control_freq - 30 &&
284 chandef->center_freq1 != control_freq - 50 &&
285 chandef->center_freq1 != control_freq - 70)
287 if (chandef->center_freq2)
294 /* channel 14 is only for IEEE 802.11b */
295 if (chandef->center_freq1 == 2484 &&
296 chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
299 if (cfg80211_chandef_is_edmg(chandef) &&
300 !cfg80211_edmg_chandef_valid(chandef))
305 EXPORT_SYMBOL(cfg80211_chandef_valid);
307 static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
308 u32 *pri40, u32 *pri80)
313 case NL80211_CHAN_WIDTH_40:
314 *pri40 = c->center_freq1;
317 case NL80211_CHAN_WIDTH_80:
318 case NL80211_CHAN_WIDTH_80P80:
319 *pri80 = c->center_freq1;
321 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
325 *pri40 = c->center_freq1 - 20 + 40 * tmp;
327 case NL80211_CHAN_WIDTH_160:
329 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
333 *pri40 = c->center_freq1 - 60 + 40 * tmp;
336 *pri80 = c->center_freq1 - 40 + 80 * tmp;
343 const struct cfg80211_chan_def *
344 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
345 const struct cfg80211_chan_def *c2)
347 u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
349 /* If they are identical, return */
350 if (cfg80211_chandef_identical(c1, c2))
353 /* otherwise, must have same control channel */
354 if (c1->chan != c2->chan)
358 * If they have the same width, but aren't identical,
359 * then they can't be compatible.
361 if (c1->width == c2->width)
365 * can't be compatible if one of them is 5 or 10 MHz,
366 * but they don't have the same width.
368 if (c1->width == NL80211_CHAN_WIDTH_5 ||
369 c1->width == NL80211_CHAN_WIDTH_10 ||
370 c2->width == NL80211_CHAN_WIDTH_5 ||
371 c2->width == NL80211_CHAN_WIDTH_10)
374 if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
375 c1->width == NL80211_CHAN_WIDTH_20)
378 if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
379 c2->width == NL80211_CHAN_WIDTH_20)
382 chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
383 chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
385 if (c1_pri40 != c2_pri40)
388 WARN_ON(!c1_pri80 && !c2_pri80);
389 if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
392 if (c1->width > c2->width)
396 EXPORT_SYMBOL(cfg80211_chandef_compatible);
398 static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
400 enum nl80211_dfs_state dfs_state)
402 struct ieee80211_channel *c;
405 for (freq = center_freq - bandwidth/2 + 10;
406 freq <= center_freq + bandwidth/2 - 10;
408 c = ieee80211_get_channel(wiphy, freq);
409 if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
412 c->dfs_state = dfs_state;
413 c->dfs_state_entered = jiffies;
417 void cfg80211_set_dfs_state(struct wiphy *wiphy,
418 const struct cfg80211_chan_def *chandef,
419 enum nl80211_dfs_state dfs_state)
423 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
426 width = cfg80211_chandef_get_width(chandef);
430 cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
433 if (!chandef->center_freq2)
435 cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
439 static u32 cfg80211_get_start_freq(u32 center_freq,
444 bandwidth = MHZ_TO_KHZ(bandwidth);
445 if (bandwidth <= MHZ_TO_KHZ(20))
446 start_freq = center_freq;
448 start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10);
453 static u32 cfg80211_get_end_freq(u32 center_freq,
458 bandwidth = MHZ_TO_KHZ(bandwidth);
459 if (bandwidth <= MHZ_TO_KHZ(20))
460 end_freq = center_freq;
462 end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10);
467 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
471 struct ieee80211_channel *c;
472 u32 freq, start_freq, end_freq;
474 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
475 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
477 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
478 c = ieee80211_get_channel_khz(wiphy, freq);
482 if (c->flags & IEEE80211_CHAN_RADAR)
489 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
490 const struct cfg80211_chan_def *chandef,
491 enum nl80211_iftype iftype)
496 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
500 case NL80211_IFTYPE_ADHOC:
501 case NL80211_IFTYPE_AP:
502 case NL80211_IFTYPE_P2P_GO:
503 case NL80211_IFTYPE_MESH_POINT:
504 width = cfg80211_chandef_get_width(chandef);
508 ret = cfg80211_get_chans_dfs_required(wiphy,
509 ieee80211_chandef_to_khz(chandef),
514 return BIT(chandef->width);
516 if (!chandef->center_freq2)
519 ret = cfg80211_get_chans_dfs_required(wiphy,
520 MHZ_TO_KHZ(chandef->center_freq2),
525 return BIT(chandef->width);
528 case NL80211_IFTYPE_STATION:
529 case NL80211_IFTYPE_OCB:
530 case NL80211_IFTYPE_P2P_CLIENT:
531 case NL80211_IFTYPE_MONITOR:
532 case NL80211_IFTYPE_AP_VLAN:
533 case NL80211_IFTYPE_P2P_DEVICE:
534 case NL80211_IFTYPE_NAN:
536 case NL80211_IFTYPE_WDS:
537 case NL80211_IFTYPE_UNSPECIFIED:
538 case NUM_NL80211_IFTYPES:
544 EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
546 static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
550 struct ieee80211_channel *c;
551 u32 freq, start_freq, end_freq;
554 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
555 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
558 * Check entire range of channels for the bandwidth.
559 * Check all channels are DFS channels (DFS_USABLE or
560 * DFS_AVAILABLE). Return number of usable channels
561 * (require CAC). Allow DFS and non-DFS channel mix.
563 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
564 c = ieee80211_get_channel_khz(wiphy, freq);
568 if (c->flags & IEEE80211_CHAN_DISABLED)
571 if (c->flags & IEEE80211_CHAN_RADAR) {
572 if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
575 if (c->dfs_state == NL80211_DFS_USABLE)
583 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
584 const struct cfg80211_chan_def *chandef)
589 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
592 width = cfg80211_chandef_get_width(chandef);
596 r1 = cfg80211_get_chans_dfs_usable(wiphy,
597 MHZ_TO_KHZ(chandef->center_freq1),
603 switch (chandef->width) {
604 case NL80211_CHAN_WIDTH_80P80:
605 WARN_ON(!chandef->center_freq2);
606 r2 = cfg80211_get_chans_dfs_usable(wiphy,
607 MHZ_TO_KHZ(chandef->center_freq2),
613 WARN_ON(chandef->center_freq2);
617 return (r1 + r2 > 0);
621 * Checks if center frequency of chan falls with in the bandwidth
624 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
625 struct ieee80211_channel *chan)
630 if (chandef->chan->center_freq == chan->center_freq)
633 width = cfg80211_chandef_get_width(chandef);
637 for (freq = chandef->center_freq1 - width / 2 + 10;
638 freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
639 if (chan->center_freq == freq)
643 if (!chandef->center_freq2)
646 for (freq = chandef->center_freq2 - width / 2 + 10;
647 freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
648 if (chan->center_freq == freq)
655 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
659 ASSERT_WDEV_LOCK(wdev);
661 if (!wdev->chandef.chan)
664 switch (wdev->iftype) {
665 case NL80211_IFTYPE_AP:
666 case NL80211_IFTYPE_P2P_GO:
667 active = wdev->beacon_interval != 0;
669 case NL80211_IFTYPE_ADHOC:
670 active = wdev->ssid_len != 0;
672 case NL80211_IFTYPE_MESH_POINT:
673 active = wdev->mesh_id_len != 0;
675 case NL80211_IFTYPE_STATION:
676 case NL80211_IFTYPE_OCB:
677 case NL80211_IFTYPE_P2P_CLIENT:
678 case NL80211_IFTYPE_MONITOR:
679 case NL80211_IFTYPE_AP_VLAN:
680 case NL80211_IFTYPE_P2P_DEVICE:
681 /* Can NAN type be considered as beaconing interface? */
682 case NL80211_IFTYPE_NAN:
684 case NL80211_IFTYPE_UNSPECIFIED:
685 case NL80211_IFTYPE_WDS:
686 case NUM_NL80211_IFTYPES:
693 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
694 struct ieee80211_channel *chan)
696 struct wireless_dev *wdev;
698 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
700 if (!cfg80211_beaconing_iface_active(wdev)) {
705 if (cfg80211_is_sub_chan(&wdev->chandef, chan)) {
715 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
716 struct ieee80211_channel *chan)
718 struct cfg80211_registered_device *rdev;
722 if (!(chan->flags & IEEE80211_CHAN_RADAR))
725 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
726 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
729 if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan))
736 static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
740 struct ieee80211_channel *c;
741 u32 freq, start_freq, end_freq;
744 dfs_offload = wiphy_ext_feature_isset(wiphy,
745 NL80211_EXT_FEATURE_DFS_OFFLOAD);
747 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
748 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
751 * Check entire range of channels for the bandwidth.
752 * If any channel in between is disabled or has not
753 * had gone through CAC return false
755 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
756 c = ieee80211_get_channel_khz(wiphy, freq);
760 if (c->flags & IEEE80211_CHAN_DISABLED)
763 if ((c->flags & IEEE80211_CHAN_RADAR) &&
764 (c->dfs_state != NL80211_DFS_AVAILABLE) &&
765 !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
772 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
773 const struct cfg80211_chan_def *chandef)
778 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
781 width = cfg80211_chandef_get_width(chandef);
785 r = cfg80211_get_chans_dfs_available(wiphy,
786 MHZ_TO_KHZ(chandef->center_freq1),
789 /* If any of channels unavailable for cf1 just return */
793 switch (chandef->width) {
794 case NL80211_CHAN_WIDTH_80P80:
795 WARN_ON(!chandef->center_freq2);
796 r = cfg80211_get_chans_dfs_available(wiphy,
797 MHZ_TO_KHZ(chandef->center_freq2),
801 WARN_ON(chandef->center_freq2);
808 static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
812 struct ieee80211_channel *c;
813 u32 start_freq, end_freq, freq;
814 unsigned int dfs_cac_ms = 0;
816 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
817 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
819 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
820 c = ieee80211_get_channel_khz(wiphy, freq);
824 if (c->flags & IEEE80211_CHAN_DISABLED)
827 if (!(c->flags & IEEE80211_CHAN_RADAR))
830 if (c->dfs_cac_ms > dfs_cac_ms)
831 dfs_cac_ms = c->dfs_cac_ms;
838 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
839 const struct cfg80211_chan_def *chandef)
842 unsigned int t1 = 0, t2 = 0;
844 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
847 width = cfg80211_chandef_get_width(chandef);
851 t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
852 MHZ_TO_KHZ(chandef->center_freq1),
855 if (!chandef->center_freq2)
858 t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
859 MHZ_TO_KHZ(chandef->center_freq2),
865 static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
866 u32 center_freq, u32 bandwidth,
867 u32 prohibited_flags)
869 struct ieee80211_channel *c;
870 u32 freq, start_freq, end_freq;
872 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
873 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
875 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
876 c = ieee80211_get_channel_khz(wiphy, freq);
877 if (!c || c->flags & prohibited_flags)
884 /* check if the operating channels are valid and supported */
885 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
886 enum ieee80211_edmg_bw_config edmg_bw_config,
888 struct ieee80211_edmg *edmg_cap)
890 struct ieee80211_channel *chan;
892 int channels_counter = 0;
894 if (!edmg_channels && !edmg_bw_config)
897 if ((!edmg_channels && edmg_bw_config) ||
898 (edmg_channels && !edmg_bw_config))
901 if (!(edmg_channels & BIT(primary_channel - 1)))
904 /* 60GHz channels 1..6 */
905 for (i = 0; i < 6; i++) {
906 if (!(edmg_channels & BIT(i)))
909 if (!(edmg_cap->channels & BIT(i)))
914 freq = ieee80211_channel_to_frequency(i + 1,
916 chan = ieee80211_get_channel(wiphy, freq);
917 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
921 /* IEEE802.11 allows max 4 channels */
922 if (channels_counter > 4)
925 /* check bw_config is a subset of what driver supports
926 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
928 if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
931 if (edmg_bw_config > edmg_cap->bw_config)
937 bool cfg80211_chandef_usable(struct wiphy *wiphy,
938 const struct cfg80211_chan_def *chandef,
939 u32 prohibited_flags)
941 struct ieee80211_sta_ht_cap *ht_cap;
942 struct ieee80211_sta_vht_cap *vht_cap;
943 struct ieee80211_edmg *edmg_cap;
944 u32 width, control_freq, cap;
945 bool support_80_80 = false;
947 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
950 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
951 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
952 edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
954 if (edmg_cap->channels &&
955 !cfg80211_edmg_usable(wiphy,
956 chandef->edmg.channels,
957 chandef->edmg.bw_config,
958 chandef->chan->hw_value,
962 control_freq = chandef->chan->center_freq;
964 switch (chandef->width) {
965 case NL80211_CHAN_WIDTH_1:
968 case NL80211_CHAN_WIDTH_2:
971 case NL80211_CHAN_WIDTH_4:
974 case NL80211_CHAN_WIDTH_8:
977 case NL80211_CHAN_WIDTH_16:
980 case NL80211_CHAN_WIDTH_5:
983 case NL80211_CHAN_WIDTH_10:
984 prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
987 case NL80211_CHAN_WIDTH_20:
988 if (!ht_cap->ht_supported &&
989 chandef->chan->band != NL80211_BAND_6GHZ)
992 case NL80211_CHAN_WIDTH_20_NOHT:
993 prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
996 case NL80211_CHAN_WIDTH_40:
998 if (chandef->chan->band == NL80211_BAND_6GHZ)
1000 if (!ht_cap->ht_supported)
1002 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1003 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1005 if (chandef->center_freq1 < control_freq &&
1006 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1008 if (chandef->center_freq1 > control_freq &&
1009 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1012 case NL80211_CHAN_WIDTH_80P80:
1015 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
1016 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1017 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
1018 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1;
1019 if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
1022 case NL80211_CHAN_WIDTH_80:
1023 prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
1025 if (chandef->chan->band == NL80211_BAND_6GHZ)
1027 if (!vht_cap->vht_supported)
1030 case NL80211_CHAN_WIDTH_160:
1031 prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
1033 if (chandef->chan->band == NL80211_BAND_6GHZ)
1035 if (!vht_cap->vht_supported)
1037 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1038 if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1039 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
1040 !(vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
1049 * TODO: What if there are only certain 80/160/80+80 MHz channels
1050 * allowed by the driver, or only certain combinations?
1051 * For 40 MHz the driver can set the NO_HT40 flags, but for
1052 * 80/160 MHz and in particular 80+80 MHz this isn't really
1053 * feasible and we only have NO_80MHZ/NO_160MHZ so far but
1054 * no way to cover 80+80 MHz or more complex restrictions.
1055 * Note that such restrictions also need to be advertised to
1056 * userspace, for example for P2P channel selection.
1060 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1062 /* 5 and 10 MHz are only defined for the OFDM PHY */
1064 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1067 if (!cfg80211_secondary_chans_ok(wiphy,
1068 ieee80211_chandef_to_khz(chandef),
1069 width, prohibited_flags))
1072 if (!chandef->center_freq2)
1074 return cfg80211_secondary_chans_ok(wiphy,
1075 MHZ_TO_KHZ(chandef->center_freq2),
1076 width, prohibited_flags);
1078 EXPORT_SYMBOL(cfg80211_chandef_usable);
1081 * Check if the channel can be used under permissive conditions mandated by
1082 * some regulatory bodies, i.e., the channel is marked with
1083 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
1084 * associated to an AP on the same channel or on the same UNII band
1085 * (assuming that the AP is an authorized master).
1086 * In addition allow operation on a channel on which indoor operation is
1087 * allowed, iff we are currently operating in an indoor environment.
1089 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1090 enum nl80211_iftype iftype,
1091 struct ieee80211_channel *chan)
1093 struct wireless_dev *wdev;
1094 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1096 lockdep_assert_held(&rdev->wiphy.mtx);
1098 if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1099 !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1102 /* only valid for GO and TDLS off-channel (station/p2p-CL) */
1103 if (iftype != NL80211_IFTYPE_P2P_GO &&
1104 iftype != NL80211_IFTYPE_STATION &&
1105 iftype != NL80211_IFTYPE_P2P_CLIENT)
1108 if (regulatory_indoor_allowed() &&
1109 (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1112 if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1116 * Generally, it is possible to rely on another device/driver to allow
1117 * the IR concurrent relaxation, however, since the device can further
1118 * enforce the relaxation (by doing a similar verifications as this),
1119 * and thus fail the GO instantiation, consider only the interfaces of
1120 * the current registered device.
1122 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1123 struct ieee80211_channel *other_chan = NULL;
1127 if (wdev->iftype == NL80211_IFTYPE_STATION &&
1129 other_chan = wdev->current_bss->pub.channel;
1132 * If a GO already operates on the same GO_CONCURRENT channel,
1133 * this one (maybe the same one) can beacon as well. We allow
1134 * the operation even if the station we relied on with
1135 * GO_CONCURRENT is disconnected now. But then we must make sure
1136 * we're not outdoor on an indoor-only channel.
1138 if (iftype == NL80211_IFTYPE_P2P_GO &&
1139 wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1140 wdev->beacon_interval &&
1141 !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1142 other_chan = wdev->chandef.chan;
1148 if (chan == other_chan)
1151 if (chan->band != NL80211_BAND_5GHZ &&
1152 chan->band != NL80211_BAND_6GHZ)
1155 r1 = cfg80211_get_unii(chan->center_freq);
1156 r2 = cfg80211_get_unii(other_chan->center_freq);
1158 if (r1 != -EINVAL && r1 == r2) {
1160 * At some locations channels 149-165 are considered a
1161 * bundle, but at other locations, e.g., Indonesia,
1162 * channels 149-161 are considered a bundle while
1163 * channel 165 is left out and considered to be in a
1164 * different bundle. Thus, in case that there is a
1165 * station interface connected to an AP on channel 165,
1166 * it is assumed that channels 149-161 are allowed for
1167 * GO operations. However, having a station interface
1168 * connected to an AP on channels 149-161, does not
1169 * allow GO operation on channel 165.
1171 if (chan->center_freq == 5825 &&
1172 other_chan->center_freq != 5825)
1181 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1182 struct cfg80211_chan_def *chandef,
1183 enum nl80211_iftype iftype,
1187 u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
1188 IEEE80211_CHAN_RADAR;
1190 trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1193 prohibited_flags |= IEEE80211_CHAN_NO_IR;
1195 if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
1196 cfg80211_chandef_dfs_available(wiphy, chandef)) {
1197 /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1198 prohibited_flags = IEEE80211_CHAN_DISABLED;
1201 res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
1203 trace_cfg80211_return_bool(res);
1207 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
1208 struct cfg80211_chan_def *chandef,
1209 enum nl80211_iftype iftype)
1211 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
1213 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
1215 bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
1216 struct cfg80211_chan_def *chandef,
1217 enum nl80211_iftype iftype)
1219 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1222 lockdep_assert_held(&rdev->wiphy.mtx);
1225 * Under certain conditions suggested by some regulatory bodies a
1226 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1227 * only if such relaxations are not enabled and the conditions are not
1230 check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
1233 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1235 EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
1237 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1238 struct cfg80211_chan_def *chandef)
1240 if (!rdev->ops->set_monitor_channel)
1242 if (!cfg80211_has_monitors_only(rdev))
1245 return rdev_set_monitor_channel(rdev, chandef);
1249 cfg80211_get_chan_state(struct wireless_dev *wdev,
1250 struct ieee80211_channel **chan,
1251 enum cfg80211_chan_mode *chanmode,
1257 *chanmode = CHAN_MODE_UNDEFINED;
1259 ASSERT_WDEV_LOCK(wdev);
1261 if (wdev->netdev && !netif_running(wdev->netdev))
1264 switch (wdev->iftype) {
1265 case NL80211_IFTYPE_ADHOC:
1266 if (wdev->current_bss) {
1267 *chan = wdev->current_bss->pub.channel;
1268 *chanmode = (wdev->ibss_fixed &&
1269 !wdev->ibss_dfs_possible)
1271 : CHAN_MODE_EXCLUSIVE;
1273 /* consider worst-case - IBSS can try to return to the
1274 * original user-specified channel as creator */
1275 if (wdev->ibss_dfs_possible)
1276 *radar_detect |= BIT(wdev->chandef.width);
1280 case NL80211_IFTYPE_STATION:
1281 case NL80211_IFTYPE_P2P_CLIENT:
1282 if (wdev->current_bss) {
1283 *chan = wdev->current_bss->pub.channel;
1284 *chanmode = CHAN_MODE_SHARED;
1288 case NL80211_IFTYPE_AP:
1289 case NL80211_IFTYPE_P2P_GO:
1290 if (wdev->cac_started) {
1291 *chan = wdev->chandef.chan;
1292 *chanmode = CHAN_MODE_SHARED;
1293 *radar_detect |= BIT(wdev->chandef.width);
1294 } else if (wdev->beacon_interval) {
1295 *chan = wdev->chandef.chan;
1296 *chanmode = CHAN_MODE_SHARED;
1298 ret = cfg80211_chandef_dfs_required(wdev->wiphy,
1303 *radar_detect |= BIT(wdev->chandef.width);
1306 case NL80211_IFTYPE_MESH_POINT:
1307 if (wdev->mesh_id_len) {
1308 *chan = wdev->chandef.chan;
1309 *chanmode = CHAN_MODE_SHARED;
1311 ret = cfg80211_chandef_dfs_required(wdev->wiphy,
1316 *radar_detect |= BIT(wdev->chandef.width);
1319 case NL80211_IFTYPE_OCB:
1320 if (wdev->chandef.chan) {
1321 *chan = wdev->chandef.chan;
1322 *chanmode = CHAN_MODE_SHARED;
1326 case NL80211_IFTYPE_MONITOR:
1327 case NL80211_IFTYPE_AP_VLAN:
1328 case NL80211_IFTYPE_P2P_DEVICE:
1329 case NL80211_IFTYPE_NAN:
1330 /* these interface types don't really have a channel */
1332 case NL80211_IFTYPE_UNSPECIFIED:
1333 case NL80211_IFTYPE_WDS:
1334 case NUM_NL80211_IFTYPES: