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-2024 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)
30 *chandef = (struct cfg80211_chan_def) {
32 .freq1_offset = chan->freq_offset,
36 case NL80211_CHAN_NO_HT:
37 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
38 chandef->center_freq1 = chan->center_freq;
40 case NL80211_CHAN_HT20:
41 chandef->width = NL80211_CHAN_WIDTH_20;
42 chandef->center_freq1 = chan->center_freq;
44 case NL80211_CHAN_HT40PLUS:
45 chandef->width = NL80211_CHAN_WIDTH_40;
46 chandef->center_freq1 = chan->center_freq + 10;
48 case NL80211_CHAN_HT40MINUS:
49 chandef->width = NL80211_CHAN_WIDTH_40;
50 chandef->center_freq1 = chan->center_freq - 10;
56 EXPORT_SYMBOL(cfg80211_chandef_create);
58 struct cfg80211_per_bw_puncturing_values {
60 const u16 *valid_values;
63 static const u16 puncturing_values_80mhz[] = {
67 static const u16 puncturing_values_160mhz[] = {
68 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3
71 static const u16 puncturing_values_320mhz[] = {
72 0xc000, 0x3000, 0xc00, 0x300, 0xc0, 0x30, 0xc, 0x3, 0xf000, 0xf00,
73 0xf0, 0xf, 0xfc00, 0xf300, 0xf0c0, 0xf030, 0xf00c, 0xf003, 0xc00f,
74 0x300f, 0xc0f, 0x30f, 0xcf, 0x3f
77 #define CFG80211_PER_BW_VALID_PUNCTURING_VALUES(_bw) \
79 .len = ARRAY_SIZE(puncturing_values_ ## _bw ## mhz), \
80 .valid_values = puncturing_values_ ## _bw ## mhz \
83 static const struct cfg80211_per_bw_puncturing_values per_bw_puncturing[] = {
84 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(80),
85 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(160),
86 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(320)
89 static bool valid_puncturing_bitmap(const struct cfg80211_chan_def *chandef)
91 u32 idx, i, start_freq, primary_center = chandef->chan->center_freq;
93 switch (chandef->width) {
94 case NL80211_CHAN_WIDTH_80:
96 start_freq = chandef->center_freq1 - 40;
98 case NL80211_CHAN_WIDTH_160:
100 start_freq = chandef->center_freq1 - 80;
102 case NL80211_CHAN_WIDTH_320:
104 start_freq = chandef->center_freq1 - 160;
107 return chandef->punctured == 0;
110 if (!chandef->punctured)
113 /* check if primary channel is punctured */
114 if (chandef->punctured & (u16)BIT((primary_center - start_freq) / 20))
117 for (i = 0; i < per_bw_puncturing[idx].len; i++) {
118 if (per_bw_puncturing[idx].valid_values[i] == chandef->punctured)
125 static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef)
127 int max_contiguous = 0;
128 int num_of_enabled = 0;
132 if (!chandef->edmg.channels || !chandef->edmg.bw_config)
135 if (!cfg80211_valid_60g_freq(chandef->chan->center_freq))
138 for (i = 0; i < 6; i++) {
139 if (chandef->edmg.channels & BIT(i)) {
146 max_contiguous = max(contiguous, max_contiguous);
148 /* basic verification of edmg configuration according to
149 * IEEE P802.11ay/D4.0 section 9.4.2.251
151 /* check bw_config against contiguous edmg channels */
152 switch (chandef->edmg.bw_config) {
153 case IEEE80211_EDMG_BW_CONFIG_4:
154 case IEEE80211_EDMG_BW_CONFIG_8:
155 case IEEE80211_EDMG_BW_CONFIG_12:
156 if (max_contiguous < 1)
159 case IEEE80211_EDMG_BW_CONFIG_5:
160 case IEEE80211_EDMG_BW_CONFIG_9:
161 case IEEE80211_EDMG_BW_CONFIG_13:
162 if (max_contiguous < 2)
165 case IEEE80211_EDMG_BW_CONFIG_6:
166 case IEEE80211_EDMG_BW_CONFIG_10:
167 case IEEE80211_EDMG_BW_CONFIG_14:
168 if (max_contiguous < 3)
171 case IEEE80211_EDMG_BW_CONFIG_7:
172 case IEEE80211_EDMG_BW_CONFIG_11:
173 case IEEE80211_EDMG_BW_CONFIG_15:
174 if (max_contiguous < 4)
182 /* check bw_config against aggregated (non contiguous) edmg channels */
183 switch (chandef->edmg.bw_config) {
184 case IEEE80211_EDMG_BW_CONFIG_4:
185 case IEEE80211_EDMG_BW_CONFIG_5:
186 case IEEE80211_EDMG_BW_CONFIG_6:
187 case IEEE80211_EDMG_BW_CONFIG_7:
189 case IEEE80211_EDMG_BW_CONFIG_8:
190 case IEEE80211_EDMG_BW_CONFIG_9:
191 case IEEE80211_EDMG_BW_CONFIG_10:
192 case IEEE80211_EDMG_BW_CONFIG_11:
193 if (num_of_enabled < 2)
196 case IEEE80211_EDMG_BW_CONFIG_12:
197 case IEEE80211_EDMG_BW_CONFIG_13:
198 case IEEE80211_EDMG_BW_CONFIG_14:
199 case IEEE80211_EDMG_BW_CONFIG_15:
200 if (num_of_enabled < 4 || max_contiguous < 2)
210 int nl80211_chan_width_to_mhz(enum nl80211_chan_width chan_width)
214 switch (chan_width) {
215 case NL80211_CHAN_WIDTH_1:
218 case NL80211_CHAN_WIDTH_2:
221 case NL80211_CHAN_WIDTH_4:
224 case NL80211_CHAN_WIDTH_8:
227 case NL80211_CHAN_WIDTH_16:
230 case NL80211_CHAN_WIDTH_5:
233 case NL80211_CHAN_WIDTH_10:
236 case NL80211_CHAN_WIDTH_20:
237 case NL80211_CHAN_WIDTH_20_NOHT:
240 case NL80211_CHAN_WIDTH_40:
243 case NL80211_CHAN_WIDTH_80P80:
244 case NL80211_CHAN_WIDTH_80:
247 case NL80211_CHAN_WIDTH_160:
250 case NL80211_CHAN_WIDTH_320:
259 EXPORT_SYMBOL(nl80211_chan_width_to_mhz);
261 static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
263 return nl80211_chan_width_to_mhz(c->width);
266 bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
268 u32 control_freq, oper_freq;
269 int oper_width, control_width;
274 if (chandef->freq1_offset >= 1000)
277 control_freq = chandef->chan->center_freq;
279 switch (chandef->width) {
280 case NL80211_CHAN_WIDTH_5:
281 case NL80211_CHAN_WIDTH_10:
282 case NL80211_CHAN_WIDTH_20:
283 case NL80211_CHAN_WIDTH_20_NOHT:
284 if (ieee80211_chandef_to_khz(chandef) !=
285 ieee80211_channel_to_khz(chandef->chan))
287 if (chandef->center_freq2)
290 case NL80211_CHAN_WIDTH_1:
291 case NL80211_CHAN_WIDTH_2:
292 case NL80211_CHAN_WIDTH_4:
293 case NL80211_CHAN_WIDTH_8:
294 case NL80211_CHAN_WIDTH_16:
295 if (chandef->chan->band != NL80211_BAND_S1GHZ)
298 control_freq = ieee80211_channel_to_khz(chandef->chan);
299 oper_freq = ieee80211_chandef_to_khz(chandef);
300 control_width = nl80211_chan_width_to_mhz(
301 ieee80211_s1g_channel_width(
303 oper_width = cfg80211_chandef_get_width(chandef);
305 if (oper_width < 0 || control_width < 0)
307 if (chandef->center_freq2)
310 if (control_freq + MHZ_TO_KHZ(control_width) / 2 >
311 oper_freq + MHZ_TO_KHZ(oper_width) / 2)
314 if (control_freq - MHZ_TO_KHZ(control_width) / 2 <
315 oper_freq - MHZ_TO_KHZ(oper_width) / 2)
318 case NL80211_CHAN_WIDTH_80P80:
319 if (!chandef->center_freq2)
321 /* adjacent is not allowed -- that's a 160 MHz channel */
322 if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
323 chandef->center_freq2 - chandef->center_freq1 == 80)
327 if (chandef->center_freq2)
332 switch (chandef->width) {
333 case NL80211_CHAN_WIDTH_5:
334 case NL80211_CHAN_WIDTH_10:
335 case NL80211_CHAN_WIDTH_20:
336 case NL80211_CHAN_WIDTH_20_NOHT:
337 case NL80211_CHAN_WIDTH_1:
338 case NL80211_CHAN_WIDTH_2:
339 case NL80211_CHAN_WIDTH_4:
340 case NL80211_CHAN_WIDTH_8:
341 case NL80211_CHAN_WIDTH_16:
342 /* all checked above */
344 case NL80211_CHAN_WIDTH_320:
345 if (chandef->center_freq1 == control_freq + 150 ||
346 chandef->center_freq1 == control_freq + 130 ||
347 chandef->center_freq1 == control_freq + 110 ||
348 chandef->center_freq1 == control_freq + 90 ||
349 chandef->center_freq1 == control_freq - 90 ||
350 chandef->center_freq1 == control_freq - 110 ||
351 chandef->center_freq1 == control_freq - 130 ||
352 chandef->center_freq1 == control_freq - 150)
355 case NL80211_CHAN_WIDTH_160:
356 if (chandef->center_freq1 == control_freq + 70 ||
357 chandef->center_freq1 == control_freq + 50 ||
358 chandef->center_freq1 == control_freq - 50 ||
359 chandef->center_freq1 == control_freq - 70)
362 case NL80211_CHAN_WIDTH_80P80:
363 case NL80211_CHAN_WIDTH_80:
364 if (chandef->center_freq1 == control_freq + 30 ||
365 chandef->center_freq1 == control_freq - 30)
368 case NL80211_CHAN_WIDTH_40:
369 if (chandef->center_freq1 == control_freq + 10 ||
370 chandef->center_freq1 == control_freq - 10)
377 /* channel 14 is only for IEEE 802.11b */
378 if (chandef->center_freq1 == 2484 &&
379 chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
382 if (cfg80211_chandef_is_edmg(chandef) &&
383 !cfg80211_edmg_chandef_valid(chandef))
386 return valid_puncturing_bitmap(chandef);
388 EXPORT_SYMBOL(cfg80211_chandef_valid);
390 int cfg80211_chandef_primary(const struct cfg80211_chan_def *c,
391 enum nl80211_chan_width primary_chan_width,
394 int pri_width = nl80211_chan_width_to_mhz(primary_chan_width);
395 int width = cfg80211_chandef_get_width(c);
396 u32 control = c->chan->center_freq;
397 u32 center = c->center_freq1;
400 if (WARN_ON_ONCE(pri_width < 0 || width < 0))
403 /* not intended to be called this way, can't determine */
404 if (WARN_ON_ONCE(pri_width > width))
410 *punctured = c->punctured;
412 while (width > pri_width) {
413 unsigned int bits_to_drop = width / 20 / 2;
415 if (control > center) {
417 *punctured >>= bits_to_drop;
420 *punctured &= (1 << bits_to_drop) - 1;
427 EXPORT_SYMBOL(cfg80211_chandef_primary);
429 static const struct cfg80211_chan_def *
430 check_chandef_primary_compat(const struct cfg80211_chan_def *c1,
431 const struct cfg80211_chan_def *c2,
432 enum nl80211_chan_width primary_chan_width)
434 u16 punct_c1 = 0, punct_c2 = 0;
436 /* check primary is compatible -> error if not */
437 if (cfg80211_chandef_primary(c1, primary_chan_width, &punct_c1) !=
438 cfg80211_chandef_primary(c2, primary_chan_width, &punct_c2))
439 return ERR_PTR(-EINVAL);
441 if (punct_c1 != punct_c2)
442 return ERR_PTR(-EINVAL);
444 /* assumes c1 is smaller width, if that was just checked -> done */
445 if (c1->width == primary_chan_width)
448 /* otherwise continue checking the next width */
452 static const struct cfg80211_chan_def *
453 _cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
454 const struct cfg80211_chan_def *c2)
456 const struct cfg80211_chan_def *ret;
458 /* If they are identical, return */
459 if (cfg80211_chandef_identical(c1, c2))
462 /* otherwise, must have same control channel */
463 if (c1->chan != c2->chan)
467 * If they have the same width, but aren't identical,
468 * then they can't be compatible.
470 if (c1->width == c2->width)
474 * can't be compatible if one of them is 5/10 MHz or S1G
475 * but they don't have the same width.
477 #define NARROW_OR_S1G(width) ((width) == NL80211_CHAN_WIDTH_5 || \
478 (width) == NL80211_CHAN_WIDTH_10 || \
479 (width) == NL80211_CHAN_WIDTH_1 || \
480 (width) == NL80211_CHAN_WIDTH_2 || \
481 (width) == NL80211_CHAN_WIDTH_4 || \
482 (width) == NL80211_CHAN_WIDTH_8 || \
483 (width) == NL80211_CHAN_WIDTH_16)
485 if (NARROW_OR_S1G(c1->width) || NARROW_OR_S1G(c2->width))
489 * Make sure that c1 is always the narrower one, so that later
490 * we either return NULL or c2 and don't have to check both
493 if (c1->width > c2->width)
497 * No further checks needed if the "narrower" one is only 20 MHz.
498 * Here "narrower" includes being a 20 MHz non-HT channel vs. a
499 * 20 MHz HT (or later) one.
501 if (c1->width <= NL80211_CHAN_WIDTH_20)
504 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_40);
508 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_80);
513 * If c1 is 80+80, then c2 is 160 or higher, but that cannot
514 * match. If c2 was also 80+80 it was already either accepted
515 * or rejected above (identical or not, respectively.)
517 if (c1->width == NL80211_CHAN_WIDTH_80P80)
520 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_160);
525 * Getting here would mean they're both wider than 160, have the
526 * same primary 160, but are not identical - this cannot happen
527 * since they must be 320 (no wider chandefs exist, at least yet.)
534 const struct cfg80211_chan_def *
535 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
536 const struct cfg80211_chan_def *c2)
538 const struct cfg80211_chan_def *ret;
540 ret = _cfg80211_chandef_compatible(c1, c2);
545 EXPORT_SYMBOL(cfg80211_chandef_compatible);
547 static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
549 enum nl80211_dfs_state dfs_state)
551 struct ieee80211_channel *c;
554 for (freq = center_freq - bandwidth/2 + 10;
555 freq <= center_freq + bandwidth/2 - 10;
557 c = ieee80211_get_channel(wiphy, freq);
558 if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
561 c->dfs_state = dfs_state;
562 c->dfs_state_entered = jiffies;
566 void cfg80211_set_dfs_state(struct wiphy *wiphy,
567 const struct cfg80211_chan_def *chandef,
568 enum nl80211_dfs_state dfs_state)
572 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
575 width = cfg80211_chandef_get_width(chandef);
579 cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
582 if (!chandef->center_freq2)
584 cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
588 static u32 cfg80211_get_start_freq(u32 center_freq,
593 bandwidth = MHZ_TO_KHZ(bandwidth);
594 if (bandwidth <= MHZ_TO_KHZ(20))
595 start_freq = center_freq;
597 start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10);
602 static u32 cfg80211_get_end_freq(u32 center_freq,
607 bandwidth = MHZ_TO_KHZ(bandwidth);
608 if (bandwidth <= MHZ_TO_KHZ(20))
609 end_freq = center_freq;
611 end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10);
617 cfg80211_dfs_permissive_check_wdev(struct cfg80211_registered_device *rdev,
618 enum nl80211_iftype iftype,
619 struct wireless_dev *wdev,
620 struct ieee80211_channel *chan)
622 unsigned int link_id;
624 for_each_valid_link(wdev, link_id) {
625 struct ieee80211_channel *other_chan = NULL;
626 struct cfg80211_chan_def chandef = {};
629 /* In order to avoid daisy chaining only allow BSS STA */
630 if (wdev->iftype != NL80211_IFTYPE_STATION ||
631 !wdev->links[link_id].client.current_bss)
635 wdev->links[link_id].client.current_bss->pub.channel;
640 if (chan == other_chan)
643 /* continue if we can't get the channel */
644 ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
648 if (cfg80211_is_sub_chan(&chandef, chan, false))
656 * Check if P2P GO is allowed to operate on a DFS channel
658 static bool cfg80211_dfs_permissive_chan(struct wiphy *wiphy,
659 enum nl80211_iftype iftype,
660 struct ieee80211_channel *chan)
662 struct wireless_dev *wdev;
663 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
665 lockdep_assert_held(&rdev->wiphy.mtx);
667 if (!wiphy_ext_feature_isset(&rdev->wiphy,
668 NL80211_EXT_FEATURE_DFS_CONCURRENT) ||
669 !(chan->flags & IEEE80211_CHAN_DFS_CONCURRENT))
672 /* only valid for P2P GO */
673 if (iftype != NL80211_IFTYPE_P2P_GO)
677 * Allow only if there's a concurrent BSS
679 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
680 bool ret = cfg80211_dfs_permissive_check_wdev(rdev, iftype,
689 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
692 enum nl80211_iftype iftype)
694 struct ieee80211_channel *c;
695 u32 freq, start_freq, end_freq;
697 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
698 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
700 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
701 c = ieee80211_get_channel_khz(wiphy, freq);
705 if (c->flags & IEEE80211_CHAN_RADAR &&
706 !cfg80211_dfs_permissive_chan(wiphy, iftype, c))
714 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
715 const struct cfg80211_chan_def *chandef,
716 enum nl80211_iftype iftype)
721 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
725 case NL80211_IFTYPE_ADHOC:
726 case NL80211_IFTYPE_AP:
727 case NL80211_IFTYPE_P2P_GO:
728 case NL80211_IFTYPE_MESH_POINT:
729 width = cfg80211_chandef_get_width(chandef);
733 ret = cfg80211_get_chans_dfs_required(wiphy,
734 ieee80211_chandef_to_khz(chandef),
739 return BIT(chandef->width);
741 if (!chandef->center_freq2)
744 ret = cfg80211_get_chans_dfs_required(wiphy,
745 MHZ_TO_KHZ(chandef->center_freq2),
750 return BIT(chandef->width);
753 case NL80211_IFTYPE_STATION:
754 case NL80211_IFTYPE_OCB:
755 case NL80211_IFTYPE_P2P_CLIENT:
756 case NL80211_IFTYPE_MONITOR:
757 case NL80211_IFTYPE_AP_VLAN:
758 case NL80211_IFTYPE_P2P_DEVICE:
759 case NL80211_IFTYPE_NAN:
761 case NL80211_IFTYPE_WDS:
762 case NL80211_IFTYPE_UNSPECIFIED:
763 case NUM_NL80211_IFTYPES:
769 EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
771 static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
775 struct ieee80211_channel *c;
776 u32 freq, start_freq, end_freq;
779 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
780 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
783 * Check entire range of channels for the bandwidth.
784 * Check all channels are DFS channels (DFS_USABLE or
785 * DFS_AVAILABLE). Return number of usable channels
786 * (require CAC). Allow DFS and non-DFS channel mix.
788 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
789 c = ieee80211_get_channel_khz(wiphy, freq);
793 if (c->flags & IEEE80211_CHAN_DISABLED)
796 if (c->flags & IEEE80211_CHAN_RADAR) {
797 if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
800 if (c->dfs_state == NL80211_DFS_USABLE)
808 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
809 const struct cfg80211_chan_def *chandef)
814 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
817 width = cfg80211_chandef_get_width(chandef);
821 r1 = cfg80211_get_chans_dfs_usable(wiphy,
822 MHZ_TO_KHZ(chandef->center_freq1),
828 switch (chandef->width) {
829 case NL80211_CHAN_WIDTH_80P80:
830 WARN_ON(!chandef->center_freq2);
831 r2 = cfg80211_get_chans_dfs_usable(wiphy,
832 MHZ_TO_KHZ(chandef->center_freq2),
838 WARN_ON(chandef->center_freq2);
842 return (r1 + r2 > 0);
844 EXPORT_SYMBOL(cfg80211_chandef_dfs_usable);
847 * Checks if center frequency of chan falls with in the bandwidth
850 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
851 struct ieee80211_channel *chan,
860 if (chandef->chan->center_freq == chan->center_freq)
866 width = cfg80211_chandef_get_width(chandef);
870 for (freq = chandef->center_freq1 - width / 2 + 10;
871 freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
872 if (chan->center_freq == freq)
876 if (!chandef->center_freq2)
879 for (freq = chandef->center_freq2 - width / 2 + 10;
880 freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
881 if (chan->center_freq == freq)
888 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
892 lockdep_assert_wiphy(wdev->wiphy);
894 switch (wdev->iftype) {
895 case NL80211_IFTYPE_AP:
896 case NL80211_IFTYPE_P2P_GO:
897 for_each_valid_link(wdev, link) {
898 if (wdev->links[link].ap.beacon_interval)
902 case NL80211_IFTYPE_ADHOC:
903 if (wdev->u.ibss.ssid_len)
906 case NL80211_IFTYPE_MESH_POINT:
907 if (wdev->u.mesh.id_len)
910 case NL80211_IFTYPE_STATION:
911 case NL80211_IFTYPE_OCB:
912 case NL80211_IFTYPE_P2P_CLIENT:
913 case NL80211_IFTYPE_MONITOR:
914 case NL80211_IFTYPE_AP_VLAN:
915 case NL80211_IFTYPE_P2P_DEVICE:
916 /* Can NAN type be considered as beaconing interface? */
917 case NL80211_IFTYPE_NAN:
919 case NL80211_IFTYPE_UNSPECIFIED:
920 case NL80211_IFTYPE_WDS:
921 case NUM_NL80211_IFTYPES:
928 bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev,
929 struct ieee80211_channel *chan,
934 switch (wdev->iftype) {
935 case NL80211_IFTYPE_AP:
936 case NL80211_IFTYPE_P2P_GO:
937 for_each_valid_link(wdev, link) {
938 if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef,
943 case NL80211_IFTYPE_ADHOC:
944 return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan,
946 case NL80211_IFTYPE_MESH_POINT:
947 return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan,
956 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
957 struct ieee80211_channel *chan)
959 struct wireless_dev *wdev;
961 lockdep_assert_wiphy(wiphy);
963 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
964 if (!cfg80211_beaconing_iface_active(wdev))
967 if (cfg80211_wdev_on_sub_chan(wdev, chan, false))
975 cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev,
976 struct ieee80211_channel *channel)
978 if (!rdev->background_radar_wdev)
981 if (!cfg80211_chandef_valid(&rdev->background_radar_chandef))
984 return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel,
988 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
989 struct ieee80211_channel *chan)
991 struct cfg80211_registered_device *rdev;
995 if (!(chan->flags & IEEE80211_CHAN_RADAR))
998 for_each_rdev(rdev) {
1001 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
1004 wiphy_lock(&rdev->wiphy);
1005 found = cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan) ||
1006 cfg80211_offchan_chain_is_active(rdev, chan);
1007 wiphy_unlock(&rdev->wiphy);
1016 static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
1020 struct ieee80211_channel *c;
1021 u32 freq, start_freq, end_freq;
1024 dfs_offload = wiphy_ext_feature_isset(wiphy,
1025 NL80211_EXT_FEATURE_DFS_OFFLOAD);
1027 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1028 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1031 * Check entire range of channels for the bandwidth.
1032 * If any channel in between is disabled or has not
1033 * had gone through CAC return false
1035 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1036 c = ieee80211_get_channel_khz(wiphy, freq);
1040 if (c->flags & IEEE80211_CHAN_DISABLED)
1043 if ((c->flags & IEEE80211_CHAN_RADAR) &&
1044 (c->dfs_state != NL80211_DFS_AVAILABLE) &&
1045 !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
1052 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
1053 const struct cfg80211_chan_def *chandef)
1058 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1061 width = cfg80211_chandef_get_width(chandef);
1065 r = cfg80211_get_chans_dfs_available(wiphy,
1066 MHZ_TO_KHZ(chandef->center_freq1),
1069 /* If any of channels unavailable for cf1 just return */
1073 switch (chandef->width) {
1074 case NL80211_CHAN_WIDTH_80P80:
1075 WARN_ON(!chandef->center_freq2);
1076 r = cfg80211_get_chans_dfs_available(wiphy,
1077 MHZ_TO_KHZ(chandef->center_freq2),
1081 WARN_ON(chandef->center_freq2);
1088 static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
1092 struct ieee80211_channel *c;
1093 u32 start_freq, end_freq, freq;
1094 unsigned int dfs_cac_ms = 0;
1096 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1097 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1099 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1100 c = ieee80211_get_channel_khz(wiphy, freq);
1104 if (c->flags & IEEE80211_CHAN_DISABLED)
1107 if (!(c->flags & IEEE80211_CHAN_RADAR))
1110 if (c->dfs_cac_ms > dfs_cac_ms)
1111 dfs_cac_ms = c->dfs_cac_ms;
1118 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
1119 const struct cfg80211_chan_def *chandef)
1122 unsigned int t1 = 0, t2 = 0;
1124 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1127 width = cfg80211_chandef_get_width(chandef);
1131 t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
1132 MHZ_TO_KHZ(chandef->center_freq1),
1135 if (!chandef->center_freq2)
1138 t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
1139 MHZ_TO_KHZ(chandef->center_freq2),
1144 EXPORT_SYMBOL(cfg80211_chandef_dfs_cac_time);
1146 static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
1147 u32 center_freq, u32 bandwidth,
1148 u32 prohibited_flags, bool monitor)
1150 struct ieee80211_channel *c;
1151 u32 freq, start_freq, end_freq;
1153 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1154 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1156 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1157 c = ieee80211_get_channel_khz(wiphy, freq);
1160 if (monitor && c->flags & IEEE80211_CHAN_CAN_MONITOR)
1162 if (c->flags & prohibited_flags)
1169 /* check if the operating channels are valid and supported */
1170 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
1171 enum ieee80211_edmg_bw_config edmg_bw_config,
1172 int primary_channel,
1173 struct ieee80211_edmg *edmg_cap)
1175 struct ieee80211_channel *chan;
1177 int channels_counter = 0;
1179 if (!edmg_channels && !edmg_bw_config)
1182 if ((!edmg_channels && edmg_bw_config) ||
1183 (edmg_channels && !edmg_bw_config))
1186 if (!(edmg_channels & BIT(primary_channel - 1)))
1189 /* 60GHz channels 1..6 */
1190 for (i = 0; i < 6; i++) {
1191 if (!(edmg_channels & BIT(i)))
1194 if (!(edmg_cap->channels & BIT(i)))
1199 freq = ieee80211_channel_to_frequency(i + 1,
1200 NL80211_BAND_60GHZ);
1201 chan = ieee80211_get_channel(wiphy, freq);
1202 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1206 /* IEEE802.11 allows max 4 channels */
1207 if (channels_counter > 4)
1210 /* check bw_config is a subset of what driver supports
1211 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
1213 if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
1216 if (edmg_bw_config > edmg_cap->bw_config)
1222 bool _cfg80211_chandef_usable(struct wiphy *wiphy,
1223 const struct cfg80211_chan_def *chandef,
1224 u32 prohibited_flags, bool monitor)
1226 struct ieee80211_sta_ht_cap *ht_cap;
1227 struct ieee80211_sta_vht_cap *vht_cap;
1228 struct ieee80211_edmg *edmg_cap;
1229 u32 width, control_freq, cap;
1230 bool ext_nss_cap, support_80_80 = false, support_320 = false;
1231 const struct ieee80211_sband_iftype_data *iftd;
1232 struct ieee80211_supported_band *sband;
1235 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1238 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
1239 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
1240 edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
1241 ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) &
1242 IEEE80211_VHT_EXT_NSS_BW_CAPABLE;
1244 if (edmg_cap->channels &&
1245 !cfg80211_edmg_usable(wiphy,
1246 chandef->edmg.channels,
1247 chandef->edmg.bw_config,
1248 chandef->chan->hw_value,
1252 control_freq = chandef->chan->center_freq;
1254 switch (chandef->width) {
1255 case NL80211_CHAN_WIDTH_1:
1258 case NL80211_CHAN_WIDTH_2:
1261 case NL80211_CHAN_WIDTH_4:
1264 case NL80211_CHAN_WIDTH_8:
1267 case NL80211_CHAN_WIDTH_16:
1270 case NL80211_CHAN_WIDTH_5:
1273 case NL80211_CHAN_WIDTH_10:
1274 prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
1277 case NL80211_CHAN_WIDTH_20:
1278 if (!ht_cap->ht_supported &&
1279 chandef->chan->band != NL80211_BAND_6GHZ)
1282 case NL80211_CHAN_WIDTH_20_NOHT:
1283 prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
1286 case NL80211_CHAN_WIDTH_40:
1288 if (chandef->chan->band == NL80211_BAND_6GHZ)
1290 if (!ht_cap->ht_supported)
1292 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1293 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1295 if (chandef->center_freq1 < control_freq &&
1296 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1298 if (chandef->center_freq1 > control_freq &&
1299 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1302 case NL80211_CHAN_WIDTH_80P80:
1305 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
1306 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1307 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
1309 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1);
1310 if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
1313 case NL80211_CHAN_WIDTH_80:
1314 prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
1316 if (chandef->chan->band == NL80211_BAND_6GHZ)
1318 if (!vht_cap->vht_supported)
1321 case NL80211_CHAN_WIDTH_160:
1322 prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
1324 if (chandef->chan->band == NL80211_BAND_6GHZ)
1326 if (!vht_cap->vht_supported)
1328 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1329 if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1330 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
1332 (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)))
1335 case NL80211_CHAN_WIDTH_320:
1336 prohibited_flags |= IEEE80211_CHAN_NO_320MHZ;
1339 if (chandef->chan->band != NL80211_BAND_6GHZ)
1342 sband = wiphy->bands[NL80211_BAND_6GHZ];
1346 for_each_sband_iftype_data(sband, i, iftd) {
1347 if (!iftd->eht_cap.has_eht)
1350 if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] &
1351 IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) {
1366 * TODO: What if there are only certain 80/160/80+80 MHz channels
1367 * allowed by the driver, or only certain combinations?
1368 * For 40 MHz the driver can set the NO_HT40 flags, but for
1369 * 80/160 MHz and in particular 80+80 MHz this isn't really
1370 * feasible and we only have NO_80MHZ/NO_160MHZ so far but
1371 * no way to cover 80+80 MHz or more complex restrictions.
1372 * Note that such restrictions also need to be advertised to
1373 * userspace, for example for P2P channel selection.
1377 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1379 /* 5 and 10 MHz are only defined for the OFDM PHY */
1381 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1384 if (!cfg80211_secondary_chans_ok(wiphy,
1385 ieee80211_chandef_to_khz(chandef),
1386 width, prohibited_flags, monitor))
1389 if (!chandef->center_freq2)
1391 return cfg80211_secondary_chans_ok(wiphy,
1392 MHZ_TO_KHZ(chandef->center_freq2),
1393 width, prohibited_flags, monitor);
1396 bool cfg80211_chandef_usable(struct wiphy *wiphy,
1397 const struct cfg80211_chan_def *chandef,
1398 u32 prohibited_flags)
1400 return _cfg80211_chandef_usable(wiphy, chandef, prohibited_flags,
1403 EXPORT_SYMBOL(cfg80211_chandef_usable);
1405 static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype,
1406 struct wireless_dev *wdev,
1407 struct ieee80211_channel *chan)
1409 struct ieee80211_channel *other_chan = NULL;
1410 unsigned int link_id;
1413 for_each_valid_link(wdev, link_id) {
1414 if (wdev->iftype == NL80211_IFTYPE_STATION &&
1415 wdev->links[link_id].client.current_bss)
1416 other_chan = wdev->links[link_id].client.current_bss->pub.channel;
1419 * If a GO already operates on the same GO_CONCURRENT channel,
1420 * this one (maybe the same one) can beacon as well. We allow
1421 * the operation even if the station we relied on with
1422 * GO_CONCURRENT is disconnected now. But then we must make sure
1423 * we're not outdoor on an indoor-only channel.
1425 if (iftype == NL80211_IFTYPE_P2P_GO &&
1426 wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1427 wdev->links[link_id].ap.beacon_interval &&
1428 !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1429 other_chan = wdev->links[link_id].ap.chandef.chan;
1434 if (chan == other_chan)
1437 if (chan->band != NL80211_BAND_5GHZ &&
1438 chan->band != NL80211_BAND_6GHZ)
1441 r1 = cfg80211_get_unii(chan->center_freq);
1442 r2 = cfg80211_get_unii(other_chan->center_freq);
1444 if (r1 != -EINVAL && r1 == r2) {
1446 * At some locations channels 149-165 are considered a
1447 * bundle, but at other locations, e.g., Indonesia,
1448 * channels 149-161 are considered a bundle while
1449 * channel 165 is left out and considered to be in a
1450 * different bundle. Thus, in case that there is a
1451 * station interface connected to an AP on channel 165,
1452 * it is assumed that channels 149-161 are allowed for
1453 * GO operations. However, having a station interface
1454 * connected to an AP on channels 149-161, does not
1455 * allow GO operation on channel 165.
1457 if (chan->center_freq == 5825 &&
1458 other_chan->center_freq != 5825)
1468 * Check if the channel can be used under permissive conditions mandated by
1469 * some regulatory bodies, i.e., the channel is marked with
1470 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
1471 * associated to an AP on the same channel or on the same UNII band
1472 * (assuming that the AP is an authorized master).
1473 * In addition allow operation on a channel on which indoor operation is
1474 * allowed, iff we are currently operating in an indoor environment.
1476 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1477 enum nl80211_iftype iftype,
1478 struct ieee80211_channel *chan)
1480 struct wireless_dev *wdev;
1481 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1483 lockdep_assert_held(&rdev->wiphy.mtx);
1485 if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1486 !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1489 /* only valid for GO and TDLS off-channel (station/p2p-CL) */
1490 if (iftype != NL80211_IFTYPE_P2P_GO &&
1491 iftype != NL80211_IFTYPE_STATION &&
1492 iftype != NL80211_IFTYPE_P2P_CLIENT)
1495 if (regulatory_indoor_allowed() &&
1496 (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1499 if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1503 * Generally, it is possible to rely on another device/driver to allow
1504 * the IR concurrent relaxation, however, since the device can further
1505 * enforce the relaxation (by doing a similar verifications as this),
1506 * and thus fail the GO instantiation, consider only the interfaces of
1507 * the current registered device.
1509 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1512 ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan);
1520 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1521 struct cfg80211_chan_def *chandef,
1522 enum nl80211_iftype iftype,
1526 u32 prohibited_flags = IEEE80211_CHAN_DISABLED;
1529 trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1532 prohibited_flags |= IEEE80211_CHAN_NO_IR;
1534 dfs_required = cfg80211_chandef_dfs_required(wiphy, chandef, iftype);
1535 if (dfs_required != 0)
1536 prohibited_flags |= IEEE80211_CHAN_RADAR;
1538 if (dfs_required > 0 &&
1539 cfg80211_chandef_dfs_available(wiphy, chandef)) {
1540 /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1541 prohibited_flags = IEEE80211_CHAN_DISABLED;
1544 res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
1546 trace_cfg80211_return_bool(res);
1550 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
1551 struct cfg80211_chan_def *chandef,
1552 enum nl80211_iftype iftype)
1554 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
1556 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
1558 bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
1559 struct cfg80211_chan_def *chandef,
1560 enum nl80211_iftype iftype)
1562 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1565 lockdep_assert_held(&rdev->wiphy.mtx);
1568 * Under certain conditions suggested by some regulatory bodies a
1569 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1570 * only if such relaxations are not enabled and the conditions are not
1573 check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
1576 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1578 EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
1580 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1581 struct cfg80211_chan_def *chandef)
1583 if (!rdev->ops->set_monitor_channel)
1585 if (!cfg80211_has_monitors_only(rdev))
1588 return rdev_set_monitor_channel(rdev, chandef);
1591 bool cfg80211_any_usable_channels(struct wiphy *wiphy,
1592 unsigned long sband_mask,
1593 u32 prohibited_flags)
1597 prohibited_flags |= IEEE80211_CHAN_DISABLED;
1599 for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) {
1600 struct ieee80211_supported_band *sband = wiphy->bands[idx];
1606 for (chanidx = 0; chanidx < sband->n_channels; chanidx++) {
1607 struct ieee80211_channel *chan;
1609 chan = &sband->channels[chanidx];
1611 if (chan->flags & prohibited_flags)
1620 EXPORT_SYMBOL(cfg80211_any_usable_channels);
1622 struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev,
1623 unsigned int link_id)
1625 lockdep_assert_wiphy(wdev->wiphy);
1627 WARN_ON(wdev->valid_links && !(wdev->valid_links & BIT(link_id)));
1628 WARN_ON(!wdev->valid_links && link_id > 0);
1630 switch (wdev->iftype) {
1631 case NL80211_IFTYPE_MESH_POINT:
1632 return &wdev->u.mesh.chandef;
1633 case NL80211_IFTYPE_ADHOC:
1634 return &wdev->u.ibss.chandef;
1635 case NL80211_IFTYPE_OCB:
1636 return &wdev->u.ocb.chandef;
1637 case NL80211_IFTYPE_AP:
1638 case NL80211_IFTYPE_P2P_GO:
1639 return &wdev->links[link_id].ap.chandef;
1644 EXPORT_SYMBOL(wdev_chandef);