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