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 | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
Alexander Simon | 54858ee5b | 2011-11-30 16:56:32 +0100 | [diff] [blame] | 11 | #include <linux/export.h> |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 12 | #include <net/cfg80211.h> |
| 13 | #include "core.h" |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 14 | #include "rdev-ops.h" |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 15 | |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 16 | void cfg80211_chandef_create(struct cfg80211_chan_def *chandef, |
| 17 | struct ieee80211_channel *chan, |
| 18 | enum nl80211_channel_type chan_type) |
| 19 | { |
| 20 | if (WARN_ON(!chan)) |
| 21 | return; |
| 22 | |
| 23 | chandef->chan = chan; |
| 24 | chandef->center_freq2 = 0; |
| 25 | |
| 26 | switch (chan_type) { |
| 27 | case NL80211_CHAN_NO_HT: |
| 28 | chandef->width = NL80211_CHAN_WIDTH_20_NOHT; |
| 29 | chandef->center_freq1 = chan->center_freq; |
| 30 | break; |
| 31 | case NL80211_CHAN_HT20: |
| 32 | chandef->width = NL80211_CHAN_WIDTH_20; |
| 33 | chandef->center_freq1 = chan->center_freq; |
| 34 | break; |
| 35 | case NL80211_CHAN_HT40PLUS: |
| 36 | chandef->width = NL80211_CHAN_WIDTH_40; |
| 37 | chandef->center_freq1 = chan->center_freq + 10; |
| 38 | break; |
| 39 | case NL80211_CHAN_HT40MINUS: |
| 40 | chandef->width = NL80211_CHAN_WIDTH_40; |
| 41 | chandef->center_freq1 = chan->center_freq - 10; |
| 42 | break; |
| 43 | default: |
| 44 | WARN_ON(1); |
| 45 | } |
| 46 | } |
| 47 | EXPORT_SYMBOL(cfg80211_chandef_create); |
| 48 | |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 49 | bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef) |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 50 | { |
| 51 | u32 control_freq; |
| 52 | |
| 53 | if (!chandef->chan) |
| 54 | return false; |
| 55 | |
| 56 | control_freq = chandef->chan->center_freq; |
| 57 | |
| 58 | switch (chandef->width) { |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 59 | case NL80211_CHAN_WIDTH_5: |
| 60 | case NL80211_CHAN_WIDTH_10: |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 61 | case NL80211_CHAN_WIDTH_20: |
| 62 | case NL80211_CHAN_WIDTH_20_NOHT: |
| 63 | if (chandef->center_freq1 != control_freq) |
| 64 | return false; |
| 65 | if (chandef->center_freq2) |
| 66 | return false; |
| 67 | break; |
| 68 | case NL80211_CHAN_WIDTH_40: |
| 69 | if (chandef->center_freq1 != control_freq + 10 && |
| 70 | chandef->center_freq1 != control_freq - 10) |
| 71 | return false; |
| 72 | if (chandef->center_freq2) |
| 73 | return false; |
| 74 | break; |
| 75 | case NL80211_CHAN_WIDTH_80P80: |
| 76 | if (chandef->center_freq1 != control_freq + 30 && |
| 77 | chandef->center_freq1 != control_freq + 10 && |
| 78 | chandef->center_freq1 != control_freq - 10 && |
| 79 | chandef->center_freq1 != control_freq - 30) |
| 80 | return false; |
| 81 | if (!chandef->center_freq2) |
| 82 | return false; |
Johannes Berg | 9cab315 | 2012-12-14 00:19:08 +0100 | [diff] [blame] | 83 | /* adjacent is not allowed -- that's a 160 MHz channel */ |
| 84 | if (chandef->center_freq1 - chandef->center_freq2 == 80 || |
| 85 | chandef->center_freq2 - chandef->center_freq1 == 80) |
| 86 | return false; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 87 | break; |
| 88 | case NL80211_CHAN_WIDTH_80: |
| 89 | if (chandef->center_freq1 != control_freq + 30 && |
| 90 | chandef->center_freq1 != control_freq + 10 && |
| 91 | chandef->center_freq1 != control_freq - 10 && |
| 92 | chandef->center_freq1 != control_freq - 30) |
| 93 | return false; |
| 94 | if (chandef->center_freq2) |
| 95 | return false; |
| 96 | break; |
| 97 | case NL80211_CHAN_WIDTH_160: |
| 98 | if (chandef->center_freq1 != control_freq + 70 && |
| 99 | chandef->center_freq1 != control_freq + 50 && |
| 100 | chandef->center_freq1 != control_freq + 30 && |
| 101 | chandef->center_freq1 != control_freq + 10 && |
| 102 | chandef->center_freq1 != control_freq - 10 && |
| 103 | chandef->center_freq1 != control_freq - 30 && |
| 104 | chandef->center_freq1 != control_freq - 50 && |
| 105 | chandef->center_freq1 != control_freq - 70) |
| 106 | return false; |
| 107 | if (chandef->center_freq2) |
| 108 | return false; |
| 109 | break; |
| 110 | default: |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 116 | EXPORT_SYMBOL(cfg80211_chandef_valid); |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 117 | |
| 118 | static void chandef_primary_freqs(const struct cfg80211_chan_def *c, |
Johannes Berg | fc1f48f | 2014-10-29 17:05:39 +0100 | [diff] [blame] | 119 | u32 *pri40, u32 *pri80) |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 120 | { |
| 121 | int tmp; |
| 122 | |
| 123 | switch (c->width) { |
| 124 | case NL80211_CHAN_WIDTH_40: |
| 125 | *pri40 = c->center_freq1; |
| 126 | *pri80 = 0; |
| 127 | break; |
| 128 | case NL80211_CHAN_WIDTH_80: |
| 129 | case NL80211_CHAN_WIDTH_80P80: |
| 130 | *pri80 = c->center_freq1; |
| 131 | /* n_P20 */ |
| 132 | tmp = (30 + c->chan->center_freq - c->center_freq1)/20; |
| 133 | /* n_P40 */ |
| 134 | tmp /= 2; |
| 135 | /* freq_P40 */ |
| 136 | *pri40 = c->center_freq1 - 20 + 40 * tmp; |
| 137 | break; |
| 138 | case NL80211_CHAN_WIDTH_160: |
| 139 | /* n_P20 */ |
| 140 | tmp = (70 + c->chan->center_freq - c->center_freq1)/20; |
| 141 | /* n_P40 */ |
| 142 | tmp /= 2; |
| 143 | /* freq_P40 */ |
| 144 | *pri40 = c->center_freq1 - 60 + 40 * tmp; |
| 145 | /* n_P80 */ |
| 146 | tmp /= 2; |
| 147 | *pri80 = c->center_freq1 - 40 + 80 * tmp; |
| 148 | break; |
| 149 | default: |
| 150 | WARN_ON_ONCE(1); |
| 151 | } |
| 152 | } |
| 153 | |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 154 | static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c) |
| 155 | { |
| 156 | int width; |
| 157 | |
| 158 | switch (c->width) { |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 159 | case NL80211_CHAN_WIDTH_5: |
| 160 | width = 5; |
| 161 | break; |
| 162 | case NL80211_CHAN_WIDTH_10: |
| 163 | width = 10; |
| 164 | break; |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 165 | case NL80211_CHAN_WIDTH_20: |
| 166 | case NL80211_CHAN_WIDTH_20_NOHT: |
| 167 | width = 20; |
| 168 | break; |
| 169 | case NL80211_CHAN_WIDTH_40: |
| 170 | width = 40; |
| 171 | break; |
| 172 | case NL80211_CHAN_WIDTH_80P80: |
| 173 | case NL80211_CHAN_WIDTH_80: |
| 174 | width = 80; |
| 175 | break; |
| 176 | case NL80211_CHAN_WIDTH_160: |
| 177 | width = 160; |
| 178 | break; |
| 179 | default: |
| 180 | WARN_ON_ONCE(1); |
| 181 | return -1; |
| 182 | } |
| 183 | return width; |
| 184 | } |
| 185 | |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 186 | const struct cfg80211_chan_def * |
| 187 | cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1, |
| 188 | const struct cfg80211_chan_def *c2) |
| 189 | { |
| 190 | u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80; |
| 191 | |
| 192 | /* If they are identical, return */ |
| 193 | if (cfg80211_chandef_identical(c1, c2)) |
| 194 | return c1; |
| 195 | |
| 196 | /* otherwise, must have same control channel */ |
| 197 | if (c1->chan != c2->chan) |
| 198 | return NULL; |
| 199 | |
| 200 | /* |
| 201 | * If they have the same width, but aren't identical, |
| 202 | * then they can't be compatible. |
| 203 | */ |
| 204 | if (c1->width == c2->width) |
| 205 | return NULL; |
| 206 | |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 207 | /* |
| 208 | * can't be compatible if one of them is 5 or 10 MHz, |
| 209 | * but they don't have the same width. |
| 210 | */ |
| 211 | if (c1->width == NL80211_CHAN_WIDTH_5 || |
| 212 | c1->width == NL80211_CHAN_WIDTH_10 || |
| 213 | c2->width == NL80211_CHAN_WIDTH_5 || |
| 214 | c2->width == NL80211_CHAN_WIDTH_10) |
| 215 | return NULL; |
| 216 | |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 217 | if (c1->width == NL80211_CHAN_WIDTH_20_NOHT || |
| 218 | c1->width == NL80211_CHAN_WIDTH_20) |
| 219 | return c2; |
| 220 | |
| 221 | if (c2->width == NL80211_CHAN_WIDTH_20_NOHT || |
| 222 | c2->width == NL80211_CHAN_WIDTH_20) |
| 223 | return c1; |
| 224 | |
| 225 | chandef_primary_freqs(c1, &c1_pri40, &c1_pri80); |
| 226 | chandef_primary_freqs(c2, &c2_pri40, &c2_pri80); |
| 227 | |
| 228 | if (c1_pri40 != c2_pri40) |
| 229 | return NULL; |
| 230 | |
| 231 | WARN_ON(!c1_pri80 && !c2_pri80); |
| 232 | if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80) |
| 233 | return NULL; |
| 234 | |
| 235 | if (c1->width > c2->width) |
| 236 | return c1; |
| 237 | return c2; |
| 238 | } |
| 239 | EXPORT_SYMBOL(cfg80211_chandef_compatible); |
| 240 | |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 241 | static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq, |
| 242 | u32 bandwidth, |
| 243 | enum nl80211_dfs_state dfs_state) |
| 244 | { |
| 245 | struct ieee80211_channel *c; |
| 246 | u32 freq; |
| 247 | |
| 248 | for (freq = center_freq - bandwidth/2 + 10; |
| 249 | freq <= center_freq + bandwidth/2 - 10; |
| 250 | freq += 20) { |
| 251 | c = ieee80211_get_channel(wiphy, freq); |
| 252 | if (!c || !(c->flags & IEEE80211_CHAN_RADAR)) |
| 253 | continue; |
| 254 | |
| 255 | c->dfs_state = dfs_state; |
| 256 | c->dfs_state_entered = jiffies; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void cfg80211_set_dfs_state(struct wiphy *wiphy, |
| 261 | const struct cfg80211_chan_def *chandef, |
| 262 | enum nl80211_dfs_state dfs_state) |
| 263 | { |
| 264 | int width; |
| 265 | |
| 266 | if (WARN_ON(!cfg80211_chandef_valid(chandef))) |
| 267 | return; |
| 268 | |
| 269 | width = cfg80211_chandef_get_width(chandef); |
| 270 | if (width < 0) |
| 271 | return; |
| 272 | |
| 273 | cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1, |
| 274 | width, dfs_state); |
| 275 | |
| 276 | if (!chandef->center_freq2) |
| 277 | return; |
| 278 | cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2, |
| 279 | width, dfs_state); |
| 280 | } |
| 281 | |
Janusz Dziedzic | 40d1ba6 | 2013-11-05 14:48:47 +0100 | [diff] [blame] | 282 | static u32 cfg80211_get_start_freq(u32 center_freq, |
| 283 | u32 bandwidth) |
| 284 | { |
| 285 | u32 start_freq; |
| 286 | |
| 287 | if (bandwidth <= 20) |
| 288 | start_freq = center_freq; |
| 289 | else |
| 290 | start_freq = center_freq - bandwidth/2 + 10; |
| 291 | |
| 292 | return start_freq; |
| 293 | } |
| 294 | |
| 295 | static u32 cfg80211_get_end_freq(u32 center_freq, |
| 296 | u32 bandwidth) |
| 297 | { |
| 298 | u32 end_freq; |
| 299 | |
| 300 | if (bandwidth <= 20) |
| 301 | end_freq = center_freq; |
| 302 | else |
| 303 | end_freq = center_freq + bandwidth/2 - 10; |
| 304 | |
| 305 | return end_freq; |
| 306 | } |
| 307 | |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 308 | static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy, |
| 309 | u32 center_freq, |
| 310 | u32 bandwidth) |
| 311 | { |
| 312 | struct ieee80211_channel *c; |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 313 | u32 freq, start_freq, end_freq; |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 314 | |
Janusz Dziedzic | 40d1ba6 | 2013-11-05 14:48:47 +0100 | [diff] [blame] | 315 | start_freq = cfg80211_get_start_freq(center_freq, bandwidth); |
| 316 | end_freq = cfg80211_get_end_freq(center_freq, bandwidth); |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 317 | |
| 318 | for (freq = start_freq; freq <= end_freq; freq += 20) { |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 319 | c = ieee80211_get_channel(wiphy, freq); |
| 320 | if (!c) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | if (c->flags & IEEE80211_CHAN_RADAR) |
| 324 | return 1; |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | int cfg80211_chandef_dfs_required(struct wiphy *wiphy, |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 331 | const struct cfg80211_chan_def *chandef, |
| 332 | enum nl80211_iftype iftype) |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 333 | { |
| 334 | int width; |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 335 | int ret; |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 336 | |
| 337 | if (WARN_ON(!cfg80211_chandef_valid(chandef))) |
| 338 | return -EINVAL; |
| 339 | |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 340 | switch (iftype) { |
| 341 | case NL80211_IFTYPE_ADHOC: |
| 342 | case NL80211_IFTYPE_AP: |
| 343 | case NL80211_IFTYPE_P2P_GO: |
| 344 | case NL80211_IFTYPE_MESH_POINT: |
| 345 | width = cfg80211_chandef_get_width(chandef); |
| 346 | if (width < 0) |
| 347 | return -EINVAL; |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 348 | |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 349 | ret = cfg80211_get_chans_dfs_required(wiphy, |
| 350 | chandef->center_freq1, |
| 351 | width); |
| 352 | if (ret < 0) |
| 353 | return ret; |
| 354 | else if (ret > 0) |
| 355 | return BIT(chandef->width); |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 356 | |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 357 | if (!chandef->center_freq2) |
| 358 | return 0; |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 359 | |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 360 | ret = cfg80211_get_chans_dfs_required(wiphy, |
| 361 | chandef->center_freq2, |
| 362 | width); |
| 363 | if (ret < 0) |
| 364 | return ret; |
| 365 | else if (ret > 0) |
| 366 | return BIT(chandef->width); |
| 367 | |
| 368 | break; |
| 369 | case NL80211_IFTYPE_STATION: |
Rostislav Lisovy | 6e0bd6c | 2014-11-03 10:33:18 +0100 | [diff] [blame] | 370 | case NL80211_IFTYPE_OCB: |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 371 | case NL80211_IFTYPE_P2P_CLIENT: |
| 372 | case NL80211_IFTYPE_MONITOR: |
| 373 | case NL80211_IFTYPE_AP_VLAN: |
| 374 | case NL80211_IFTYPE_WDS: |
| 375 | case NL80211_IFTYPE_P2P_DEVICE: |
Ayala Beker | cb3b7d8 | 2016-09-20 17:31:13 +0300 | [diff] [blame] | 376 | case NL80211_IFTYPE_NAN: |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 377 | break; |
Luciano Coelho | 00ec75f | 2014-05-15 13:05:39 +0300 | [diff] [blame] | 378 | case NL80211_IFTYPE_UNSPECIFIED: |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 379 | case NUM_NL80211_IFTYPES: |
| 380 | WARN_ON(1); |
| 381 | } |
| 382 | |
| 383 | return 0; |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 384 | } |
Simon Wunderlich | 774f073 | 2013-08-28 13:41:28 +0200 | [diff] [blame] | 385 | EXPORT_SYMBOL(cfg80211_chandef_dfs_required); |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 386 | |
Janusz Dziedzic | fe7c3a1 | 2013-11-05 14:48:48 +0100 | [diff] [blame] | 387 | static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy, |
| 388 | u32 center_freq, |
| 389 | u32 bandwidth) |
| 390 | { |
| 391 | struct ieee80211_channel *c; |
| 392 | u32 freq, start_freq, end_freq; |
| 393 | int count = 0; |
| 394 | |
| 395 | start_freq = cfg80211_get_start_freq(center_freq, bandwidth); |
| 396 | end_freq = cfg80211_get_end_freq(center_freq, bandwidth); |
| 397 | |
| 398 | /* |
| 399 | * Check entire range of channels for the bandwidth. |
| 400 | * Check all channels are DFS channels (DFS_USABLE or |
| 401 | * DFS_AVAILABLE). Return number of usable channels |
| 402 | * (require CAC). Allow DFS and non-DFS channel mix. |
| 403 | */ |
| 404 | for (freq = start_freq; freq <= end_freq; freq += 20) { |
| 405 | c = ieee80211_get_channel(wiphy, freq); |
| 406 | if (!c) |
| 407 | return -EINVAL; |
| 408 | |
| 409 | if (c->flags & IEEE80211_CHAN_DISABLED) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | if (c->flags & IEEE80211_CHAN_RADAR) { |
| 413 | if (c->dfs_state == NL80211_DFS_UNAVAILABLE) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | if (c->dfs_state == NL80211_DFS_USABLE) |
| 417 | count++; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return count; |
| 422 | } |
| 423 | |
| 424 | bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy, |
| 425 | const struct cfg80211_chan_def *chandef) |
| 426 | { |
| 427 | int width; |
| 428 | int r1, r2 = 0; |
| 429 | |
| 430 | if (WARN_ON(!cfg80211_chandef_valid(chandef))) |
| 431 | return false; |
| 432 | |
| 433 | width = cfg80211_chandef_get_width(chandef); |
| 434 | if (width < 0) |
| 435 | return false; |
| 436 | |
| 437 | r1 = cfg80211_get_chans_dfs_usable(wiphy, chandef->center_freq1, |
| 438 | width); |
| 439 | |
| 440 | if (r1 < 0) |
| 441 | return false; |
| 442 | |
| 443 | switch (chandef->width) { |
| 444 | case NL80211_CHAN_WIDTH_80P80: |
| 445 | WARN_ON(!chandef->center_freq2); |
| 446 | r2 = cfg80211_get_chans_dfs_usable(wiphy, |
| 447 | chandef->center_freq2, |
| 448 | width); |
| 449 | if (r2 < 0) |
| 450 | return false; |
| 451 | break; |
| 452 | default: |
| 453 | WARN_ON(chandef->center_freq2); |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | return (r1 + r2 > 0); |
| 458 | } |
| 459 | |
Vasanthakumar Thiagarajan | b35a51c | 2017-02-27 17:04:33 +0530 | [diff] [blame] | 460 | /* |
| 461 | * Checks if center frequency of chan falls with in the bandwidth |
| 462 | * range of chandef. |
| 463 | */ |
| 464 | bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef, |
| 465 | struct ieee80211_channel *chan) |
| 466 | { |
| 467 | int width; |
Johannes Berg | a67a489 | 2017-10-12 11:23:04 +0200 | [diff] [blame] | 468 | u32 freq; |
Vasanthakumar Thiagarajan | b35a51c | 2017-02-27 17:04:33 +0530 | [diff] [blame] | 469 | |
| 470 | if (chandef->chan->center_freq == chan->center_freq) |
| 471 | return true; |
| 472 | |
| 473 | width = cfg80211_chandef_get_width(chandef); |
| 474 | if (width <= 20) |
| 475 | return false; |
| 476 | |
Vasanthakumar Thiagarajan | b35a51c | 2017-02-27 17:04:33 +0530 | [diff] [blame] | 477 | for (freq = chandef->center_freq1 - width / 2 + 10; |
| 478 | freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) { |
| 479 | if (chan->center_freq == freq) |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | if (!chandef->center_freq2) |
| 484 | return false; |
| 485 | |
| 486 | for (freq = chandef->center_freq2 - width / 2 + 10; |
| 487 | freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) { |
| 488 | if (chan->center_freq == freq) |
| 489 | return true; |
| 490 | } |
| 491 | |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev) |
| 496 | { |
| 497 | bool active = false; |
| 498 | |
| 499 | ASSERT_WDEV_LOCK(wdev); |
| 500 | |
| 501 | if (!wdev->chandef.chan) |
| 502 | return false; |
| 503 | |
| 504 | switch (wdev->iftype) { |
| 505 | case NL80211_IFTYPE_AP: |
| 506 | case NL80211_IFTYPE_P2P_GO: |
| 507 | active = wdev->beacon_interval != 0; |
| 508 | break; |
| 509 | case NL80211_IFTYPE_ADHOC: |
| 510 | active = wdev->ssid_len != 0; |
| 511 | break; |
| 512 | case NL80211_IFTYPE_MESH_POINT: |
| 513 | active = wdev->mesh_id_len != 0; |
| 514 | break; |
| 515 | case NL80211_IFTYPE_STATION: |
| 516 | case NL80211_IFTYPE_OCB: |
| 517 | case NL80211_IFTYPE_P2P_CLIENT: |
| 518 | case NL80211_IFTYPE_MONITOR: |
| 519 | case NL80211_IFTYPE_AP_VLAN: |
| 520 | case NL80211_IFTYPE_WDS: |
| 521 | case NL80211_IFTYPE_P2P_DEVICE: |
| 522 | /* Can NAN type be considered as beaconing interface? */ |
| 523 | case NL80211_IFTYPE_NAN: |
| 524 | break; |
| 525 | case NL80211_IFTYPE_UNSPECIFIED: |
| 526 | case NUM_NL80211_IFTYPES: |
| 527 | WARN_ON(1); |
| 528 | } |
| 529 | |
| 530 | return active; |
| 531 | } |
| 532 | |
Vasanthakumar Thiagarajan | 8976672 | 2017-02-27 17:04:35 +0530 | [diff] [blame] | 533 | static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy, |
| 534 | struct ieee80211_channel *chan) |
Vasanthakumar Thiagarajan | b35a51c | 2017-02-27 17:04:33 +0530 | [diff] [blame] | 535 | { |
| 536 | struct wireless_dev *wdev; |
| 537 | |
Vasanthakumar Thiagarajan | b35a51c | 2017-02-27 17:04:33 +0530 | [diff] [blame] | 538 | list_for_each_entry(wdev, &wiphy->wdev_list, list) { |
| 539 | wdev_lock(wdev); |
| 540 | if (!cfg80211_beaconing_iface_active(wdev)) { |
| 541 | wdev_unlock(wdev); |
| 542 | continue; |
| 543 | } |
| 544 | |
| 545 | if (cfg80211_is_sub_chan(&wdev->chandef, chan)) { |
| 546 | wdev_unlock(wdev); |
| 547 | return true; |
| 548 | } |
| 549 | wdev_unlock(wdev); |
| 550 | } |
| 551 | |
| 552 | return false; |
| 553 | } |
Janusz Dziedzic | fe7c3a1 | 2013-11-05 14:48:48 +0100 | [diff] [blame] | 554 | |
Vasanthakumar Thiagarajan | 8976672 | 2017-02-27 17:04:35 +0530 | [diff] [blame] | 555 | bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy, |
| 556 | struct ieee80211_channel *chan) |
| 557 | { |
| 558 | struct cfg80211_registered_device *rdev; |
| 559 | |
| 560 | ASSERT_RTNL(); |
| 561 | |
| 562 | if (!(chan->flags & IEEE80211_CHAN_RADAR)) |
| 563 | return false; |
| 564 | |
| 565 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
| 566 | if (!reg_dfs_domain_same(wiphy, &rdev->wiphy)) |
| 567 | continue; |
| 568 | |
| 569 | if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan)) |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | return false; |
| 574 | } |
| 575 | |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 576 | static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy, |
| 577 | u32 center_freq, |
| 578 | u32 bandwidth) |
| 579 | { |
| 580 | struct ieee80211_channel *c; |
| 581 | u32 freq, start_freq, end_freq; |
Dmitry Lebed | 2c390e4 | 2018-03-26 16:36:32 +0300 | [diff] [blame] | 582 | bool dfs_offload; |
| 583 | |
| 584 | dfs_offload = wiphy_ext_feature_isset(wiphy, |
| 585 | NL80211_EXT_FEATURE_DFS_OFFLOAD); |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 586 | |
| 587 | start_freq = cfg80211_get_start_freq(center_freq, bandwidth); |
| 588 | end_freq = cfg80211_get_end_freq(center_freq, bandwidth); |
| 589 | |
| 590 | /* |
| 591 | * Check entire range of channels for the bandwidth. |
| 592 | * If any channel in between is disabled or has not |
| 593 | * had gone through CAC return false |
| 594 | */ |
| 595 | for (freq = start_freq; freq <= end_freq; freq += 20) { |
| 596 | c = ieee80211_get_channel(wiphy, freq); |
| 597 | if (!c) |
| 598 | return false; |
| 599 | |
| 600 | if (c->flags & IEEE80211_CHAN_DISABLED) |
| 601 | return false; |
| 602 | |
Dmitry Lebed | 2c390e4 | 2018-03-26 16:36:32 +0300 | [diff] [blame] | 603 | if ((c->flags & IEEE80211_CHAN_RADAR) && |
| 604 | (c->dfs_state != NL80211_DFS_AVAILABLE) && |
| 605 | !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload)) |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 606 | return false; |
| 607 | } |
| 608 | |
| 609 | return true; |
| 610 | } |
| 611 | |
| 612 | static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy, |
| 613 | const struct cfg80211_chan_def *chandef) |
| 614 | { |
| 615 | int width; |
| 616 | int r; |
| 617 | |
| 618 | if (WARN_ON(!cfg80211_chandef_valid(chandef))) |
| 619 | return false; |
| 620 | |
| 621 | width = cfg80211_chandef_get_width(chandef); |
| 622 | if (width < 0) |
| 623 | return false; |
| 624 | |
| 625 | r = cfg80211_get_chans_dfs_available(wiphy, chandef->center_freq1, |
| 626 | width); |
| 627 | |
| 628 | /* If any of channels unavailable for cf1 just return */ |
| 629 | if (!r) |
| 630 | return r; |
| 631 | |
| 632 | switch (chandef->width) { |
| 633 | case NL80211_CHAN_WIDTH_80P80: |
| 634 | WARN_ON(!chandef->center_freq2); |
| 635 | r = cfg80211_get_chans_dfs_available(wiphy, |
| 636 | chandef->center_freq2, |
| 637 | width); |
Colin Ian King | 680682d | 2016-07-17 19:55:27 +0100 | [diff] [blame] | 638 | break; |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 639 | default: |
| 640 | WARN_ON(chandef->center_freq2); |
| 641 | break; |
| 642 | } |
| 643 | |
| 644 | return r; |
| 645 | } |
| 646 | |
Janusz Dziedzic | 31559f3 | 2014-02-21 19:46:13 +0100 | [diff] [blame] | 647 | static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy, |
| 648 | u32 center_freq, |
| 649 | u32 bandwidth) |
| 650 | { |
| 651 | struct ieee80211_channel *c; |
| 652 | u32 start_freq, end_freq, freq; |
| 653 | unsigned int dfs_cac_ms = 0; |
| 654 | |
| 655 | start_freq = cfg80211_get_start_freq(center_freq, bandwidth); |
| 656 | end_freq = cfg80211_get_end_freq(center_freq, bandwidth); |
| 657 | |
| 658 | for (freq = start_freq; freq <= end_freq; freq += 20) { |
| 659 | c = ieee80211_get_channel(wiphy, freq); |
| 660 | if (!c) |
| 661 | return 0; |
| 662 | |
| 663 | if (c->flags & IEEE80211_CHAN_DISABLED) |
| 664 | return 0; |
| 665 | |
| 666 | if (!(c->flags & IEEE80211_CHAN_RADAR)) |
| 667 | continue; |
| 668 | |
| 669 | if (c->dfs_cac_ms > dfs_cac_ms) |
| 670 | dfs_cac_ms = c->dfs_cac_ms; |
| 671 | } |
| 672 | |
| 673 | return dfs_cac_ms; |
| 674 | } |
| 675 | |
| 676 | unsigned int |
| 677 | cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy, |
| 678 | const struct cfg80211_chan_def *chandef) |
| 679 | { |
| 680 | int width; |
| 681 | unsigned int t1 = 0, t2 = 0; |
| 682 | |
| 683 | if (WARN_ON(!cfg80211_chandef_valid(chandef))) |
| 684 | return 0; |
| 685 | |
| 686 | width = cfg80211_chandef_get_width(chandef); |
| 687 | if (width < 0) |
| 688 | return 0; |
| 689 | |
| 690 | t1 = cfg80211_get_chans_dfs_cac_time(wiphy, |
| 691 | chandef->center_freq1, |
| 692 | width); |
| 693 | |
| 694 | if (!chandef->center_freq2) |
| 695 | return t1; |
| 696 | |
| 697 | t2 = cfg80211_get_chans_dfs_cac_time(wiphy, |
| 698 | chandef->center_freq2, |
| 699 | width); |
| 700 | |
| 701 | return max(t1, t2); |
| 702 | } |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 703 | |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 704 | static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy, |
| 705 | u32 center_freq, u32 bandwidth, |
| 706 | u32 prohibited_flags) |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 707 | { |
| 708 | struct ieee80211_channel *c; |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 709 | u32 freq, start_freq, end_freq; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 710 | |
Janusz Dziedzic | 40d1ba6 | 2013-11-05 14:48:47 +0100 | [diff] [blame] | 711 | start_freq = cfg80211_get_start_freq(center_freq, bandwidth); |
| 712 | end_freq = cfg80211_get_end_freq(center_freq, bandwidth); |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 713 | |
| 714 | for (freq = start_freq; freq <= end_freq; freq += 20) { |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 715 | c = ieee80211_get_channel(wiphy, freq); |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 716 | if (!c || c->flags & prohibited_flags) |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 717 | return false; |
| 718 | } |
| 719 | |
| 720 | return true; |
| 721 | } |
| 722 | |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 723 | bool cfg80211_chandef_usable(struct wiphy *wiphy, |
| 724 | const struct cfg80211_chan_def *chandef, |
| 725 | u32 prohibited_flags) |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 726 | { |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 727 | struct ieee80211_sta_ht_cap *ht_cap; |
| 728 | struct ieee80211_sta_vht_cap *vht_cap; |
Jouni Malinen | 08f6f14 | 2014-12-11 23:48:55 +0200 | [diff] [blame] | 729 | u32 width, control_freq, cap; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 730 | |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 731 | if (WARN_ON(!cfg80211_chandef_valid(chandef))) |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 732 | return false; |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 733 | |
| 734 | ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap; |
| 735 | vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap; |
| 736 | |
| 737 | control_freq = chandef->chan->center_freq; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 738 | |
| 739 | switch (chandef->width) { |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 740 | case NL80211_CHAN_WIDTH_5: |
| 741 | width = 5; |
| 742 | break; |
| 743 | case NL80211_CHAN_WIDTH_10: |
Rostislav Lisovy | ea077c1 | 2014-04-15 14:37:55 +0200 | [diff] [blame] | 744 | prohibited_flags |= IEEE80211_CHAN_NO_10MHZ; |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 745 | width = 10; |
| 746 | break; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 747 | case NL80211_CHAN_WIDTH_20: |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 748 | if (!ht_cap->ht_supported) |
| 749 | return false; |
| 750 | case NL80211_CHAN_WIDTH_20_NOHT: |
Rostislav Lisovy | ea077c1 | 2014-04-15 14:37:55 +0200 | [diff] [blame] | 751 | prohibited_flags |= IEEE80211_CHAN_NO_20MHZ; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 752 | width = 20; |
Mark Mentovai | 09a02fd | 2010-11-17 16:34:37 -0500 | [diff] [blame] | 753 | break; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 754 | case NL80211_CHAN_WIDTH_40: |
| 755 | width = 40; |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 756 | if (!ht_cap->ht_supported) |
| 757 | return false; |
| 758 | if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) || |
| 759 | ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT) |
| 760 | return false; |
| 761 | if (chandef->center_freq1 < control_freq && |
| 762 | chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS) |
| 763 | return false; |
| 764 | if (chandef->center_freq1 > control_freq && |
| 765 | chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS) |
| 766 | return false; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 767 | break; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 768 | case NL80211_CHAN_WIDTH_80P80: |
Jouni Malinen | 08f6f14 | 2014-12-11 23:48:55 +0200 | [diff] [blame] | 769 | cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK; |
| 770 | if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 771 | return false; |
| 772 | case NL80211_CHAN_WIDTH_80: |
| 773 | if (!vht_cap->vht_supported) |
| 774 | return false; |
Johannes Berg | c7a6ee2 | 2012-12-12 17:50:39 +0100 | [diff] [blame] | 775 | prohibited_flags |= IEEE80211_CHAN_NO_80MHZ; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 776 | width = 80; |
| 777 | break; |
| 778 | case NL80211_CHAN_WIDTH_160: |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 779 | if (!vht_cap->vht_supported) |
| 780 | return false; |
Jouni Malinen | 08f6f14 | 2014-12-11 23:48:55 +0200 | [diff] [blame] | 781 | cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK; |
| 782 | if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ && |
| 783 | cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 784 | return false; |
Johannes Berg | c7a6ee2 | 2012-12-12 17:50:39 +0100 | [diff] [blame] | 785 | prohibited_flags |= IEEE80211_CHAN_NO_160MHZ; |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 786 | width = 160; |
Mark Mentovai | 09a02fd | 2010-11-17 16:34:37 -0500 | [diff] [blame] | 787 | break; |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 788 | default: |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 789 | WARN_ON_ONCE(1); |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 790 | return false; |
Beni Lev | 4ee3e06 | 2012-08-27 12:49:39 +0300 | [diff] [blame] | 791 | } |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 792 | |
Johannes Berg | c7a6ee2 | 2012-12-12 17:50:39 +0100 | [diff] [blame] | 793 | /* |
| 794 | * TODO: What if there are only certain 80/160/80+80 MHz channels |
| 795 | * allowed by the driver, or only certain combinations? |
| 796 | * For 40 MHz the driver can set the NO_HT40 flags, but for |
| 797 | * 80/160 MHz and in particular 80+80 MHz this isn't really |
| 798 | * feasible and we only have NO_80MHZ/NO_160MHZ so far but |
| 799 | * no way to cover 80+80 MHz or more complex restrictions. |
| 800 | * Note that such restrictions also need to be advertised to |
| 801 | * userspace, for example for P2P channel selection. |
| 802 | */ |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 803 | |
Johannes Berg | a6662db | 2012-12-04 20:49:42 +0100 | [diff] [blame] | 804 | if (width > 20) |
| 805 | prohibited_flags |= IEEE80211_CHAN_NO_OFDM; |
| 806 | |
Simon Wunderlich | 2f301ab | 2013-05-16 13:00:28 +0200 | [diff] [blame] | 807 | /* 5 and 10 MHz are only defined for the OFDM PHY */ |
| 808 | if (width < 20) |
| 809 | prohibited_flags |= IEEE80211_CHAN_NO_OFDM; |
| 810 | |
| 811 | |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 812 | if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1, |
| 813 | width, prohibited_flags)) |
| 814 | return false; |
| 815 | |
| 816 | if (!chandef->center_freq2) |
| 817 | return true; |
| 818 | return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2, |
| 819 | width, prohibited_flags); |
| 820 | } |
| 821 | EXPORT_SYMBOL(cfg80211_chandef_usable); |
| 822 | |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 823 | /* |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 824 | * Check if the channel can be used under permissive conditions mandated by |
| 825 | * some regulatory bodies, i.e., the channel is marked with |
| 826 | * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 827 | * associated to an AP on the same channel or on the same UNII band |
| 828 | * (assuming that the AP is an authorized master). |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 829 | * In addition allow operation on a channel on which indoor operation is |
Ilan Peer | c8866e5 | 2014-02-23 09:13:03 +0200 | [diff] [blame] | 830 | * allowed, iff we are currently operating in an indoor environment. |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 831 | */ |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 832 | static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy, |
| 833 | enum nl80211_iftype iftype, |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 834 | struct ieee80211_channel *chan) |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 835 | { |
Avraham Stern | be69c24 | 2015-04-27 16:52:16 +0300 | [diff] [blame] | 836 | struct wireless_dev *wdev; |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 837 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 838 | |
| 839 | ASSERT_RTNL(); |
| 840 | |
Masahiro Yamada | 97f2645 | 2016-08-03 13:45:50 -0700 | [diff] [blame] | 841 | if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) || |
Ilan Peer | c8866e5 | 2014-02-23 09:13:03 +0200 | [diff] [blame] | 842 | !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR)) |
| 843 | return false; |
| 844 | |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 845 | /* only valid for GO and TDLS off-channel (station/p2p-CL) */ |
| 846 | if (iftype != NL80211_IFTYPE_P2P_GO && |
| 847 | iftype != NL80211_IFTYPE_STATION && |
| 848 | iftype != NL80211_IFTYPE_P2P_CLIENT) |
| 849 | return false; |
| 850 | |
Ilan Peer | c8866e5 | 2014-02-23 09:13:03 +0200 | [diff] [blame] | 851 | if (regulatory_indoor_allowed() && |
| 852 | (chan->flags & IEEE80211_CHAN_INDOOR_ONLY)) |
| 853 | return true; |
| 854 | |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 855 | if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT)) |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 856 | return false; |
| 857 | |
| 858 | /* |
| 859 | * Generally, it is possible to rely on another device/driver to allow |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 860 | * the IR concurrent relaxation, however, since the device can further |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 861 | * enforce the relaxation (by doing a similar verifications as this), |
| 862 | * and thus fail the GO instantiation, consider only the interfaces of |
| 863 | * the current registered device. |
| 864 | */ |
Johannes Berg | 53873f1 | 2016-05-03 16:52:04 +0300 | [diff] [blame] | 865 | list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 866 | struct ieee80211_channel *other_chan = NULL; |
| 867 | int r1, r2; |
| 868 | |
Avraham Stern | be69c24 | 2015-04-27 16:52:16 +0300 | [diff] [blame] | 869 | wdev_lock(wdev); |
| 870 | if (wdev->iftype == NL80211_IFTYPE_STATION && |
| 871 | wdev->current_bss) |
| 872 | other_chan = wdev->current_bss->pub.channel; |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 873 | |
Avraham Stern | be69c24 | 2015-04-27 16:52:16 +0300 | [diff] [blame] | 874 | /* |
| 875 | * If a GO already operates on the same GO_CONCURRENT channel, |
| 876 | * this one (maybe the same one) can beacon as well. We allow |
| 877 | * the operation even if the station we relied on with |
| 878 | * GO_CONCURRENT is disconnected now. But then we must make sure |
| 879 | * we're not outdoor on an indoor-only channel. |
| 880 | */ |
Arik Nemtsov | 06f207f | 2015-05-06 16:28:31 +0300 | [diff] [blame] | 881 | if (iftype == NL80211_IFTYPE_P2P_GO && |
| 882 | wdev->iftype == NL80211_IFTYPE_P2P_GO && |
Avraham Stern | be69c24 | 2015-04-27 16:52:16 +0300 | [diff] [blame] | 883 | wdev->beacon_interval && |
| 884 | !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY)) |
| 885 | other_chan = wdev->chandef.chan; |
| 886 | wdev_unlock(wdev); |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 887 | |
| 888 | if (!other_chan) |
| 889 | continue; |
| 890 | |
| 891 | if (chan == other_chan) |
| 892 | return true; |
| 893 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 894 | if (chan->band != NL80211_BAND_5GHZ) |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 895 | continue; |
| 896 | |
| 897 | r1 = cfg80211_get_unii(chan->center_freq); |
| 898 | r2 = cfg80211_get_unii(other_chan->center_freq); |
| 899 | |
Ilan Peer | 46d5372 | 2014-04-23 09:22:58 +0300 | [diff] [blame] | 900 | if (r1 != -EINVAL && r1 == r2) { |
| 901 | /* |
| 902 | * At some locations channels 149-165 are considered a |
| 903 | * bundle, but at other locations, e.g., Indonesia, |
| 904 | * channels 149-161 are considered a bundle while |
| 905 | * channel 165 is left out and considered to be in a |
| 906 | * different bundle. Thus, in case that there is a |
| 907 | * station interface connected to an AP on channel 165, |
| 908 | * it is assumed that channels 149-161 are allowed for |
| 909 | * GO operations. However, having a station interface |
| 910 | * connected to an AP on channels 149-161, does not |
| 911 | * allow GO operation on channel 165. |
| 912 | */ |
| 913 | if (chan->center_freq == 5825 && |
| 914 | other_chan->center_freq != 5825) |
| 915 | continue; |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 916 | return true; |
Ilan Peer | 46d5372 | 2014-04-23 09:22:58 +0300 | [diff] [blame] | 917 | } |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | return false; |
| 921 | } |
| 922 | |
Arik Nemtsov | 923b352 | 2015-07-08 15:41:44 +0300 | [diff] [blame] | 923 | static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy, |
| 924 | struct cfg80211_chan_def *chandef, |
| 925 | enum nl80211_iftype iftype, |
| 926 | bool check_no_ir) |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 927 | { |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 928 | bool res; |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 929 | u32 prohibited_flags = IEEE80211_CHAN_DISABLED | |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 930 | IEEE80211_CHAN_RADAR; |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 931 | |
Arik Nemtsov | 923b352 | 2015-07-08 15:41:44 +0300 | [diff] [blame] | 932 | trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir); |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 933 | |
Arik Nemtsov | 923b352 | 2015-07-08 15:41:44 +0300 | [diff] [blame] | 934 | if (check_no_ir) |
Ilan Peer | 174e0cd | 2014-02-23 09:13:01 +0200 | [diff] [blame] | 935 | prohibited_flags |= IEEE80211_CHAN_NO_IR; |
Johannes Berg | 9f5e8f6 | 2012-11-22 16:59:45 +0100 | [diff] [blame] | 936 | |
Luciano Coelho | 00ec75f | 2014-05-15 13:05:39 +0300 | [diff] [blame] | 937 | if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 && |
Janusz Dziedzic | 6bc54fb | 2013-11-06 13:55:53 +0100 | [diff] [blame] | 938 | cfg80211_chandef_dfs_available(wiphy, chandef)) { |
| 939 | /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */ |
| 940 | prohibited_flags = IEEE80211_CHAN_DISABLED; |
| 941 | } |
| 942 | |
| 943 | res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags); |
Johannes Berg | 3d9d1d6 | 2012-11-08 23:14:50 +0100 | [diff] [blame] | 944 | |
| 945 | trace_cfg80211_return_bool(res); |
| 946 | return res; |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 947 | } |
Arik Nemtsov | 923b352 | 2015-07-08 15:41:44 +0300 | [diff] [blame] | 948 | |
| 949 | bool cfg80211_reg_can_beacon(struct wiphy *wiphy, |
| 950 | struct cfg80211_chan_def *chandef, |
| 951 | enum nl80211_iftype iftype) |
| 952 | { |
| 953 | return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true); |
| 954 | } |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 955 | EXPORT_SYMBOL(cfg80211_reg_can_beacon); |
Luis R. Rodriguez | 9236d83 | 2010-11-12 16:31:23 -0800 | [diff] [blame] | 956 | |
Arik Nemtsov | 923b352 | 2015-07-08 15:41:44 +0300 | [diff] [blame] | 957 | bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy, |
| 958 | struct cfg80211_chan_def *chandef, |
| 959 | enum nl80211_iftype iftype) |
| 960 | { |
| 961 | bool check_no_ir; |
| 962 | |
| 963 | ASSERT_RTNL(); |
| 964 | |
| 965 | /* |
| 966 | * Under certain conditions suggested by some regulatory bodies a |
| 967 | * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag |
| 968 | * only if such relaxations are not enabled and the conditions are not |
| 969 | * met. |
| 970 | */ |
| 971 | check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype, |
| 972 | chandef->chan); |
| 973 | |
| 974 | return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir); |
| 975 | } |
| 976 | EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax); |
| 977 | |
Johannes Berg | e8c9bd5 | 2012-06-06 08:18:22 +0200 | [diff] [blame] | 978 | int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev, |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 979 | struct cfg80211_chan_def *chandef) |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 980 | { |
Johannes Berg | e8c9bd5 | 2012-06-06 08:18:22 +0200 | [diff] [blame] | 981 | if (!rdev->ops->set_monitor_channel) |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 982 | return -EOPNOTSUPP; |
Michal Kazior | 4f03c1e | 2012-06-29 12:47:03 +0200 | [diff] [blame] | 983 | if (!cfg80211_has_monitors_only(rdev)) |
| 984 | return -EBUSY; |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 985 | |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 986 | return rdev_set_monitor_channel(rdev, chandef); |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 987 | } |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 988 | |
| 989 | void |
Johannes Berg | 8e95ea4 | 2012-07-10 19:39:02 +0200 | [diff] [blame] | 990 | cfg80211_get_chan_state(struct wireless_dev *wdev, |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 991 | struct ieee80211_channel **chan, |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 992 | enum cfg80211_chan_mode *chanmode, |
| 993 | u8 *radar_detect) |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 994 | { |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 995 | int ret; |
| 996 | |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 997 | *chan = NULL; |
| 998 | *chanmode = CHAN_MODE_UNDEFINED; |
| 999 | |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1000 | ASSERT_WDEV_LOCK(wdev); |
| 1001 | |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 1002 | if (wdev->netdev && !netif_running(wdev->netdev)) |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1003 | return; |
| 1004 | |
| 1005 | switch (wdev->iftype) { |
| 1006 | case NL80211_IFTYPE_ADHOC: |
| 1007 | if (wdev->current_bss) { |
| 1008 | *chan = wdev->current_bss->pub.channel; |
Simon Wunderlich | 5336fa8 | 2013-10-07 18:41:05 +0200 | [diff] [blame] | 1009 | *chanmode = (wdev->ibss_fixed && |
| 1010 | !wdev->ibss_dfs_possible) |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1011 | ? CHAN_MODE_SHARED |
| 1012 | : CHAN_MODE_EXCLUSIVE; |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1013 | |
| 1014 | /* consider worst-case - IBSS can try to return to the |
| 1015 | * original user-specified channel as creator */ |
| 1016 | if (wdev->ibss_dfs_possible) |
| 1017 | *radar_detect |= BIT(wdev->chandef.width); |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1018 | return; |
| 1019 | } |
Johannes Berg | 0f0094b | 2013-10-25 12:46:44 +0200 | [diff] [blame] | 1020 | break; |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1021 | case NL80211_IFTYPE_STATION: |
| 1022 | case NL80211_IFTYPE_P2P_CLIENT: |
| 1023 | if (wdev->current_bss) { |
| 1024 | *chan = wdev->current_bss->pub.channel; |
| 1025 | *chanmode = CHAN_MODE_SHARED; |
| 1026 | return; |
| 1027 | } |
| 1028 | break; |
| 1029 | case NL80211_IFTYPE_AP: |
| 1030 | case NL80211_IFTYPE_P2P_GO: |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 1031 | if (wdev->cac_started) { |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1032 | *chan = wdev->chandef.chan; |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 1033 | *chanmode = CHAN_MODE_SHARED; |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1034 | *radar_detect |= BIT(wdev->chandef.width); |
Simon Wunderlich | 04f3904 | 2013-02-08 18:16:19 +0100 | [diff] [blame] | 1035 | } else if (wdev->beacon_interval) { |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1036 | *chan = wdev->chandef.chan; |
Felix Fietkau | f53594a | 2012-07-12 16:10:02 +0200 | [diff] [blame] | 1037 | *chanmode = CHAN_MODE_SHARED; |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1038 | |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 1039 | ret = cfg80211_chandef_dfs_required(wdev->wiphy, |
| 1040 | &wdev->chandef, |
| 1041 | wdev->iftype); |
| 1042 | WARN_ON(ret < 0); |
| 1043 | if (ret > 0) |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1044 | *radar_detect |= BIT(wdev->chandef.width); |
Felix Fietkau | f53594a | 2012-07-12 16:10:02 +0200 | [diff] [blame] | 1045 | } |
| 1046 | return; |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1047 | case NL80211_IFTYPE_MESH_POINT: |
Felix Fietkau | f53594a | 2012-07-12 16:10:02 +0200 | [diff] [blame] | 1048 | if (wdev->mesh_id_len) { |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1049 | *chan = wdev->chandef.chan; |
Felix Fietkau | f53594a | 2012-07-12 16:10:02 +0200 | [diff] [blame] | 1050 | *chanmode = CHAN_MODE_SHARED; |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1051 | |
Luciano Coelho | 2beb6dab | 2014-02-18 11:40:36 +0200 | [diff] [blame] | 1052 | ret = cfg80211_chandef_dfs_required(wdev->wiphy, |
| 1053 | &wdev->chandef, |
| 1054 | wdev->iftype); |
| 1055 | WARN_ON(ret < 0); |
| 1056 | if (ret > 0) |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 1057 | *radar_detect |= BIT(wdev->chandef.width); |
Felix Fietkau | f53594a | 2012-07-12 16:10:02 +0200 | [diff] [blame] | 1058 | } |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1059 | return; |
Rostislav Lisovy | 6e0bd6c | 2014-11-03 10:33:18 +0100 | [diff] [blame] | 1060 | case NL80211_IFTYPE_OCB: |
| 1061 | if (wdev->chandef.chan) { |
| 1062 | *chan = wdev->chandef.chan; |
| 1063 | *chanmode = CHAN_MODE_SHARED; |
| 1064 | return; |
| 1065 | } |
| 1066 | break; |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1067 | case NL80211_IFTYPE_MONITOR: |
| 1068 | case NL80211_IFTYPE_AP_VLAN: |
| 1069 | case NL80211_IFTYPE_WDS: |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 1070 | case NL80211_IFTYPE_P2P_DEVICE: |
Ayala Beker | cb3b7d8 | 2016-09-20 17:31:13 +0300 | [diff] [blame] | 1071 | case NL80211_IFTYPE_NAN: |
Johannes Berg | e7aceef | 2014-02-12 14:21:15 +0100 | [diff] [blame] | 1072 | /* these interface types don't really have a channel */ |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 1073 | return; |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1074 | case NL80211_IFTYPE_UNSPECIFIED: |
| 1075 | case NUM_NL80211_IFTYPES: |
| 1076 | WARN_ON(1); |
| 1077 | } |
Michal Kazior | 26ab9a0 | 2012-06-29 12:47:00 +0200 | [diff] [blame] | 1078 | } |