1 // SPDX-License-Identifier: GPL-2.0 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> 8 * Copyright 2013-2014 Intel Mobile Communications GmbH 9 * Copyright 2018-2026 Intel Corporation 10 */ 11 12 #include <linux/export.h> 13 #include <linux/bitfield.h> 14 #include <net/cfg80211.h> 15 #include "core.h" 16 #include "rdev-ops.h" 17 18 static bool cfg80211_valid_60g_freq(u32 freq) 19 { 20 return freq >= 58320 && freq <= 70200; 21 } 22 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 = (struct cfg80211_chan_def) { 31 .chan = chan, 32 }; 33 34 WARN_ON(chan->band == NL80211_BAND_60GHZ || 35 chan->band == NL80211_BAND_S1GHZ); 36 37 switch (chan_type) { 38 case NL80211_CHAN_NO_HT: 39 chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 40 chandef->center_freq1 = chan->center_freq; 41 break; 42 case NL80211_CHAN_HT20: 43 chandef->width = NL80211_CHAN_WIDTH_20; 44 chandef->center_freq1 = chan->center_freq; 45 break; 46 case NL80211_CHAN_HT40PLUS: 47 chandef->width = NL80211_CHAN_WIDTH_40; 48 chandef->center_freq1 = chan->center_freq + 10; 49 break; 50 case NL80211_CHAN_HT40MINUS: 51 chandef->width = NL80211_CHAN_WIDTH_40; 52 chandef->center_freq1 = chan->center_freq - 10; 53 break; 54 default: 55 WARN_ON(1); 56 } 57 } 58 EXPORT_SYMBOL(cfg80211_chandef_create); 59 60 static u32 cfg80211_get_start_freq(const struct cfg80211_chan_def *chandef, 61 u32 cf) 62 { 63 u32 start_freq, center_freq, bandwidth; 64 65 center_freq = MHZ_TO_KHZ((cf == 1) ? 66 chandef->center_freq1 : chandef->center_freq2); 67 bandwidth = MHZ_TO_KHZ(cfg80211_chandef_get_width(chandef)); 68 69 if (bandwidth <= MHZ_TO_KHZ(20)) 70 start_freq = center_freq; 71 else 72 start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10); 73 74 return start_freq; 75 } 76 77 static u32 cfg80211_get_end_freq(const struct cfg80211_chan_def *chandef, 78 u32 cf) 79 { 80 u32 end_freq, center_freq, bandwidth; 81 82 center_freq = MHZ_TO_KHZ((cf == 1) ? 83 chandef->center_freq1 : chandef->center_freq2); 84 bandwidth = MHZ_TO_KHZ(cfg80211_chandef_get_width(chandef)); 85 86 if (bandwidth <= MHZ_TO_KHZ(20)) 87 end_freq = center_freq; 88 else 89 end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10); 90 91 return end_freq; 92 } 93 94 #define for_each_subchan(chandef, freq, cf) \ 95 for (u32 punctured = chandef->punctured, \ 96 cf = 1, freq = cfg80211_get_start_freq(chandef, cf); \ 97 freq <= cfg80211_get_end_freq(chandef, cf); \ 98 freq += MHZ_TO_KHZ(20), \ 99 ((cf == 1 && chandef->center_freq2 != 0 && \ 100 freq > cfg80211_get_end_freq(chandef, cf)) ? \ 101 (cf++, freq = cfg80211_get_start_freq(chandef, cf), \ 102 punctured = 0) : (punctured >>= 1))) \ 103 if (!(punctured & 1)) 104 105 #define for_each_s1g_subchan(chandef, freq_khz) \ 106 for (freq_khz = cfg80211_s1g_get_start_freq_khz(chandef); \ 107 freq_khz <= cfg80211_s1g_get_end_freq_khz(chandef); \ 108 freq_khz += MHZ_TO_KHZ(1)) 109 110 struct cfg80211_per_bw_puncturing_values { 111 u8 len; 112 const u16 *valid_values; 113 }; 114 115 static const u16 puncturing_values_80mhz[] = { 116 0x8, 0x4, 0x2, 0x1 117 }; 118 119 static const u16 puncturing_values_160mhz[] = { 120 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3 121 }; 122 123 static const u16 puncturing_values_320mhz[] = { 124 0xc000, 0x3000, 0xc00, 0x300, 0xc0, 0x30, 0xc, 0x3, 0xf000, 0xf00, 125 0xf0, 0xf, 0xfc00, 0xf300, 0xf0c0, 0xf030, 0xf00c, 0xf003, 0xc00f, 126 0x300f, 0xc0f, 0x30f, 0xcf, 0x3f 127 }; 128 129 #define CFG80211_PER_BW_VALID_PUNCTURING_VALUES(_bw) \ 130 { \ 131 .len = ARRAY_SIZE(puncturing_values_ ## _bw ## mhz), \ 132 .valid_values = puncturing_values_ ## _bw ## mhz \ 133 } 134 135 static const struct cfg80211_per_bw_puncturing_values per_bw_puncturing[] = { 136 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(80), 137 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(160), 138 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(320) 139 }; 140 141 static bool valid_puncturing_bitmap(const struct cfg80211_chan_def *chandef, 142 u32 primary_center, u32 punctured) 143 { 144 u32 idx, i, start_freq; 145 146 switch (chandef->width) { 147 case NL80211_CHAN_WIDTH_80: 148 idx = 0; 149 start_freq = chandef->center_freq1 - 40; 150 break; 151 case NL80211_CHAN_WIDTH_160: 152 idx = 1; 153 start_freq = chandef->center_freq1 - 80; 154 break; 155 case NL80211_CHAN_WIDTH_320: 156 idx = 2; 157 start_freq = chandef->center_freq1 - 160; 158 break; 159 default: 160 return punctured == 0; 161 } 162 163 if (!punctured) 164 return true; 165 166 /* check if primary channel is punctured */ 167 if (punctured & (u16)BIT((primary_center - start_freq) / 20)) 168 return false; 169 170 for (i = 0; i < per_bw_puncturing[idx].len; i++) { 171 if (per_bw_puncturing[idx].valid_values[i] == punctured) 172 return true; 173 } 174 175 return false; 176 } 177 178 static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef) 179 { 180 int max_contiguous = 0; 181 int num_of_enabled = 0; 182 int contiguous = 0; 183 int i; 184 185 if (!chandef->edmg.channels || !chandef->edmg.bw_config) 186 return false; 187 188 if (!cfg80211_valid_60g_freq(chandef->chan->center_freq)) 189 return false; 190 191 for (i = 0; i < 6; i++) { 192 if (chandef->edmg.channels & BIT(i)) { 193 contiguous++; 194 num_of_enabled++; 195 } else { 196 contiguous = 0; 197 } 198 199 max_contiguous = max(contiguous, max_contiguous); 200 } 201 /* basic verification of edmg configuration according to 202 * IEEE P802.11ay/D4.0 section 9.4.2.251 203 */ 204 /* check bw_config against contiguous edmg channels */ 205 switch (chandef->edmg.bw_config) { 206 case IEEE80211_EDMG_BW_CONFIG_4: 207 case IEEE80211_EDMG_BW_CONFIG_8: 208 case IEEE80211_EDMG_BW_CONFIG_12: 209 if (max_contiguous < 1) 210 return false; 211 break; 212 case IEEE80211_EDMG_BW_CONFIG_5: 213 case IEEE80211_EDMG_BW_CONFIG_9: 214 case IEEE80211_EDMG_BW_CONFIG_13: 215 if (max_contiguous < 2) 216 return false; 217 break; 218 case IEEE80211_EDMG_BW_CONFIG_6: 219 case IEEE80211_EDMG_BW_CONFIG_10: 220 case IEEE80211_EDMG_BW_CONFIG_14: 221 if (max_contiguous < 3) 222 return false; 223 break; 224 case IEEE80211_EDMG_BW_CONFIG_7: 225 case IEEE80211_EDMG_BW_CONFIG_11: 226 case IEEE80211_EDMG_BW_CONFIG_15: 227 if (max_contiguous < 4) 228 return false; 229 break; 230 231 default: 232 return false; 233 } 234 235 /* check bw_config against aggregated (non contiguous) edmg channels */ 236 switch (chandef->edmg.bw_config) { 237 case IEEE80211_EDMG_BW_CONFIG_4: 238 case IEEE80211_EDMG_BW_CONFIG_5: 239 case IEEE80211_EDMG_BW_CONFIG_6: 240 case IEEE80211_EDMG_BW_CONFIG_7: 241 break; 242 case IEEE80211_EDMG_BW_CONFIG_8: 243 case IEEE80211_EDMG_BW_CONFIG_9: 244 case IEEE80211_EDMG_BW_CONFIG_10: 245 case IEEE80211_EDMG_BW_CONFIG_11: 246 if (num_of_enabled < 2) 247 return false; 248 break; 249 case IEEE80211_EDMG_BW_CONFIG_12: 250 case IEEE80211_EDMG_BW_CONFIG_13: 251 case IEEE80211_EDMG_BW_CONFIG_14: 252 case IEEE80211_EDMG_BW_CONFIG_15: 253 if (num_of_enabled < 4 || max_contiguous < 2) 254 return false; 255 break; 256 default: 257 return false; 258 } 259 260 return true; 261 } 262 263 int nl80211_chan_width_to_mhz(enum nl80211_chan_width chan_width) 264 { 265 int mhz; 266 267 switch (chan_width) { 268 case NL80211_CHAN_WIDTH_1: 269 mhz = 1; 270 break; 271 case NL80211_CHAN_WIDTH_2: 272 mhz = 2; 273 break; 274 case NL80211_CHAN_WIDTH_4: 275 mhz = 4; 276 break; 277 case NL80211_CHAN_WIDTH_8: 278 mhz = 8; 279 break; 280 case NL80211_CHAN_WIDTH_16: 281 mhz = 16; 282 break; 283 case NL80211_CHAN_WIDTH_20: 284 case NL80211_CHAN_WIDTH_20_NOHT: 285 mhz = 20; 286 break; 287 case NL80211_CHAN_WIDTH_40: 288 mhz = 40; 289 break; 290 case NL80211_CHAN_WIDTH_80P80: 291 case NL80211_CHAN_WIDTH_80: 292 mhz = 80; 293 break; 294 case NL80211_CHAN_WIDTH_160: 295 mhz = 160; 296 break; 297 case NL80211_CHAN_WIDTH_320: 298 mhz = 320; 299 break; 300 default: 301 WARN_ON_ONCE(1); 302 return -1; 303 } 304 return mhz; 305 } 306 EXPORT_SYMBOL(nl80211_chan_width_to_mhz); 307 308 static bool cfg80211_valid_center_freq(u32 center, 309 enum nl80211_chan_width width) 310 { 311 int bw; 312 int step; 313 314 /* We only do strict verification on 6 GHz */ 315 if (center < 5955 || center > 7215) 316 return true; 317 318 bw = nl80211_chan_width_to_mhz(width); 319 if (bw < 0) 320 return false; 321 322 /* Validate that the channels bw is entirely within the 6 GHz band */ 323 if (center - bw / 2 < 5945 || center + bw / 2 > 7225) 324 return false; 325 326 /* With 320 MHz the permitted channels overlap */ 327 if (bw == 320) 328 step = 160; 329 else 330 step = bw; 331 332 /* 333 * Valid channels are packed from lowest frequency towards higher ones. 334 * So test that the lower frequency aligns with one of these steps. 335 */ 336 return (center - bw / 2 - 5945) % step == 0; 337 } 338 339 static bool 340 cfg80211_chandef_valid_control_freq(const struct cfg80211_chan_def *chandef, 341 u32 control_freq) 342 { 343 switch (chandef->width) { 344 case NL80211_CHAN_WIDTH_20: 345 case NL80211_CHAN_WIDTH_20_NOHT: 346 case NL80211_CHAN_WIDTH_1: 347 case NL80211_CHAN_WIDTH_2: 348 case NL80211_CHAN_WIDTH_4: 349 case NL80211_CHAN_WIDTH_8: 350 case NL80211_CHAN_WIDTH_16: 351 /* checked separately */ 352 break; 353 case NL80211_CHAN_WIDTH_320: 354 if (chandef->center_freq1 == control_freq + 150 || 355 chandef->center_freq1 == control_freq + 130 || 356 chandef->center_freq1 == control_freq + 110 || 357 chandef->center_freq1 == control_freq + 90 || 358 chandef->center_freq1 == control_freq - 90 || 359 chandef->center_freq1 == control_freq - 110 || 360 chandef->center_freq1 == control_freq - 130 || 361 chandef->center_freq1 == control_freq - 150) 362 break; 363 fallthrough; 364 case NL80211_CHAN_WIDTH_160: 365 if (chandef->center_freq1 == control_freq + 70 || 366 chandef->center_freq1 == control_freq + 50 || 367 chandef->center_freq1 == control_freq - 50 || 368 chandef->center_freq1 == control_freq - 70) 369 break; 370 fallthrough; 371 case NL80211_CHAN_WIDTH_80P80: 372 case NL80211_CHAN_WIDTH_80: 373 if (chandef->center_freq1 == control_freq + 30 || 374 chandef->center_freq1 == control_freq - 30) 375 break; 376 fallthrough; 377 case NL80211_CHAN_WIDTH_40: 378 if (chandef->center_freq1 == control_freq + 10 || 379 chandef->center_freq1 == control_freq - 10) 380 break; 381 fallthrough; 382 default: 383 return false; 384 } 385 386 return true; 387 } 388 389 bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef) 390 { 391 u32 control_freq, control_freq_khz, start_khz, end_khz; 392 393 if (!chandef->chan) 394 return false; 395 396 if (chandef->freq1_offset >= 1000) 397 return false; 398 399 control_freq = chandef->chan->center_freq; 400 401 if (cfg80211_chandef_is_s1g(chandef) && 402 chandef->width != NL80211_CHAN_WIDTH_1 && 403 chandef->width != NL80211_CHAN_WIDTH_2 && 404 chandef->width != NL80211_CHAN_WIDTH_4 && 405 chandef->width != NL80211_CHAN_WIDTH_8 && 406 chandef->width != NL80211_CHAN_WIDTH_16) 407 return false; 408 409 switch (chandef->width) { 410 case NL80211_CHAN_WIDTH_20: 411 case NL80211_CHAN_WIDTH_20_NOHT: 412 if (ieee80211_chandef_to_khz(chandef) != 413 ieee80211_channel_to_khz(chandef->chan)) 414 return false; 415 if (chandef->center_freq2) 416 return false; 417 break; 418 case NL80211_CHAN_WIDTH_1: 419 case NL80211_CHAN_WIDTH_2: 420 case NL80211_CHAN_WIDTH_4: 421 case NL80211_CHAN_WIDTH_8: 422 case NL80211_CHAN_WIDTH_16: 423 if (!cfg80211_chandef_is_s1g(chandef)) 424 return false; 425 if (chandef->center_freq2) 426 return false; 427 428 control_freq_khz = ieee80211_channel_to_khz(chandef->chan); 429 start_khz = cfg80211_s1g_get_start_freq_khz(chandef); 430 end_khz = cfg80211_s1g_get_end_freq_khz(chandef); 431 432 if (control_freq_khz < start_khz || control_freq_khz > end_khz) 433 return false; 434 break; 435 case NL80211_CHAN_WIDTH_80P80: 436 if (!chandef->center_freq2) 437 return false; 438 /* adjacent is not allowed -- that's a 160 MHz channel */ 439 if (chandef->center_freq1 - chandef->center_freq2 == 80 || 440 chandef->center_freq2 - chandef->center_freq1 == 80) 441 return false; 442 break; 443 default: 444 if (chandef->center_freq2) 445 return false; 446 break; 447 } 448 449 if (!cfg80211_chandef_valid_control_freq(chandef, control_freq)) 450 return false; 451 452 if (chandef->npca_chan) { 453 switch (chandef->width) { 454 case NL80211_CHAN_WIDTH_80: 455 case NL80211_CHAN_WIDTH_160: 456 case NL80211_CHAN_WIDTH_320: 457 break; 458 default: 459 return false; 460 } 461 } else if (chandef->npca_punctured) { 462 return false; 463 } 464 465 if (!cfg80211_valid_center_freq(chandef->center_freq1, chandef->width)) 466 return false; 467 468 if (chandef->width == NL80211_CHAN_WIDTH_80P80 && 469 !cfg80211_valid_center_freq(chandef->center_freq2, chandef->width)) 470 return false; 471 472 /* channel 14 is only for IEEE 802.11b */ 473 if (chandef->center_freq1 == 2484 && 474 chandef->width != NL80211_CHAN_WIDTH_20_NOHT) 475 return false; 476 477 if (cfg80211_chandef_is_edmg(chandef) && 478 !cfg80211_edmg_chandef_valid(chandef)) 479 return false; 480 481 if (!cfg80211_chandef_is_s1g(chandef) && chandef->s1g_primary_2mhz) 482 return false; 483 484 return valid_puncturing_bitmap(chandef, control_freq, 485 chandef->punctured); 486 } 487 EXPORT_SYMBOL(cfg80211_chandef_valid); 488 489 int cfg80211_chandef_primary(const struct cfg80211_chan_def *c, 490 enum nl80211_chan_width primary_chan_width, 491 u16 *punctured) 492 { 493 int pri_width = nl80211_chan_width_to_mhz(primary_chan_width); 494 int width = cfg80211_chandef_get_width(c); 495 u32 control = c->chan->center_freq; 496 u32 center = c->center_freq1; 497 u16 _punct = 0; 498 499 if (WARN_ON_ONCE(pri_width < 0 || width < 0)) 500 return -1; 501 502 /* not intended to be called this way, can't determine */ 503 if (WARN_ON_ONCE(pri_width > width)) 504 return -1; 505 506 if (!punctured) 507 punctured = &_punct; 508 509 *punctured = c->punctured; 510 511 while (width > pri_width) { 512 unsigned int bits_to_drop = width / 20 / 2; 513 514 if (control > center) { 515 center += width / 4; 516 *punctured >>= bits_to_drop; 517 } else { 518 center -= width / 4; 519 *punctured &= (1 << bits_to_drop) - 1; 520 } 521 width /= 2; 522 } 523 524 return center; 525 } 526 EXPORT_SYMBOL(cfg80211_chandef_primary); 527 528 bool cfg80211_chandef_npca_valid(struct wiphy *wiphy, 529 const struct cfg80211_chan_def *chandef, 530 const struct ieee80211_uhr_npca_info *npca) 531 { 532 struct cfg80211_chan_def tmp = *chandef; 533 bool pri_upper, npca_upper; 534 u32 cf1; 535 536 if (chandef->npca_chan || chandef->npca_punctured) 537 return false; 538 539 if (!npca) 540 return true; 541 542 if (cfg80211_chandef_add_npca(wiphy, &tmp, npca)) 543 return false; 544 545 if (!cfg80211_chandef_valid_control_freq(&tmp, 546 tmp.npca_chan->center_freq)) 547 return false; 548 549 cf1 = tmp.center_freq1; 550 pri_upper = tmp.chan->center_freq > cf1; 551 npca_upper = tmp.npca_chan->center_freq > cf1; 552 553 if (pri_upper == npca_upper) 554 return false; 555 556 if (!valid_puncturing_bitmap(&tmp, 557 tmp.npca_chan->center_freq, 558 tmp.npca_punctured) || 559 (tmp.punctured & tmp.npca_punctured) != tmp.punctured) 560 return false; 561 562 return true; 563 } 564 EXPORT_SYMBOL(cfg80211_chandef_npca_valid); 565 566 int cfg80211_chandef_add_npca(struct wiphy *wiphy, 567 struct cfg80211_chan_def *chandef, 568 const struct ieee80211_uhr_npca_info *npca) 569 { 570 struct cfg80211_chan_def new_chandef = *chandef; 571 u32 npca_freq; 572 u8 chan; 573 574 if (chandef->npca_chan || chandef->npca_punctured) 575 return -EINVAL; 576 577 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 578 return -EINVAL; 579 580 if (!npca) 581 return 0; 582 583 switch (chandef->width) { 584 case NL80211_CHAN_WIDTH_80: 585 case NL80211_CHAN_WIDTH_160: 586 case NL80211_CHAN_WIDTH_320: 587 break; 588 default: 589 return -EINVAL; 590 } 591 592 chan = le32_get_bits(npca->params, 593 IEEE80211_UHR_NPCA_PARAMS_PRIMARY_CHAN); 594 595 npca_freq = ieee80211_channel_to_frequency(chan, chandef->chan->band); 596 new_chandef.npca_chan = ieee80211_get_channel(wiphy, npca_freq); 597 if (!new_chandef.npca_chan) 598 return -EINVAL; 599 600 if (npca->params & cpu_to_le32(IEEE80211_UHR_NPCA_PARAMS_DIS_SUBCH_BMAP_PRES)) 601 new_chandef.npca_punctured = le16_to_cpu(npca->dis_subch_bmap[0]); 602 603 if (!cfg80211_chandef_valid(&new_chandef)) 604 return -EINVAL; 605 606 *chandef = new_chandef; 607 return 0; 608 } 609 EXPORT_SYMBOL(cfg80211_chandef_add_npca); 610 611 int cfg80211_chandef_add_dbe(struct cfg80211_chan_def *chandef, 612 const struct ieee80211_uhr_dbe_info *dbe) 613 { 614 struct cfg80211_chan_def new_chandef = *chandef; 615 u16 starting_freq, bw_mhz, start_old, start_new; 616 u8 bw, punct_shift; 617 int offset, index; 618 619 if (!dbe) 620 return 0; 621 622 if (!cfg80211_chandef_valid(chandef)) 623 return -EINVAL; 624 625 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT) 626 return -EINVAL; 627 628 bw = u8_get_bits(dbe->params, IEEE80211_UHR_DBE_OPER_BANDWIDTH); 629 630 switch (chandef->chan->band) { 631 case NL80211_BAND_5GHZ: 632 if (bw > IEEE80211_UHR_DBE_OPER_BW_160) 633 return -EINVAL; 634 if (chandef->chan->center_freq < 5745) 635 starting_freq = 5180; /* channel 36 */ 636 else 637 starting_freq = 5745; /* channel 149 */ 638 break; 639 case NL80211_BAND_6GHZ: 640 starting_freq = 5955; /* channel 1 center */ 641 break; 642 default: 643 return -EINVAL; 644 } 645 646 switch (bw) { 647 case IEEE80211_UHR_DBE_OPER_BW_320_2: 648 case IEEE80211_UHR_DBE_OPER_BW_320_1: 649 if (chandef->width == NL80211_CHAN_WIDTH_160) 650 break; 651 fallthrough; 652 case IEEE80211_UHR_DBE_OPER_BW_160: 653 if (chandef->width == NL80211_CHAN_WIDTH_80) 654 break; 655 fallthrough; 656 case IEEE80211_UHR_DBE_OPER_BW_80: 657 if (chandef->width == NL80211_CHAN_WIDTH_40) 658 break; 659 fallthrough; 660 case IEEE80211_UHR_DBE_OPER_BW_40: 661 if (chandef->width == NL80211_CHAN_WIDTH_20) 662 break; 663 fallthrough; 664 default: 665 return -EINVAL; 666 } 667 668 switch (bw) { 669 case IEEE80211_UHR_DBE_OPER_BW_320_2: 670 /* 320-2 starts shifted by 160 */ 671 starting_freq += 160; 672 fallthrough; 673 case IEEE80211_UHR_DBE_OPER_BW_320_1: 674 new_chandef.width = NL80211_CHAN_WIDTH_320; 675 bw_mhz = 320; 676 break; 677 case IEEE80211_UHR_DBE_OPER_BW_160: 678 new_chandef.width = NL80211_CHAN_WIDTH_160; 679 bw_mhz = 160; 680 break; 681 case IEEE80211_UHR_DBE_OPER_BW_80: 682 new_chandef.width = NL80211_CHAN_WIDTH_80; 683 bw_mhz = 80; 684 break; 685 case IEEE80211_UHR_DBE_OPER_BW_40: 686 new_chandef.width = NL80211_CHAN_WIDTH_40; 687 bw_mhz = 40; 688 break; 689 } 690 691 /* this should only happen for 320-2 and misconfigured AP */ 692 if (chandef->chan->center_freq < starting_freq) 693 return -EINVAL; 694 695 offset = chandef->chan->center_freq - starting_freq; 696 index = offset / bw_mhz; 697 start_new = starting_freq - 10 + index * bw_mhz; 698 new_chandef.center_freq1 = start_new + bw_mhz / 2; 699 700 start_old = chandef->center_freq1 - 701 cfg80211_chandef_get_width(chandef) / 2; 702 703 /* 704 * If the DBE channel extends downward below the lower 705 * edge of the BSS channel, we need to shift puncturing 706 * bitmaps up to adjust for that. 707 */ 708 if (start_new < start_old) 709 punct_shift = (start_old - start_new) / 20; 710 else 711 punct_shift = 0; 712 713 new_chandef.punctured <<= punct_shift; 714 new_chandef.npca_punctured <<= punct_shift; 715 716 if (dbe->params & IEEE80211_UHR_DBE_OPER_DIS_SUBCHANNEL_BITMAP_PRES) { 717 u16 punct_mask = ((1 << (bw_mhz / 40)) - 1) << punct_shift; 718 u16 punctured = le16_to_cpu(dbe->dis_subch_bmap[0]); 719 720 if ((punctured & punct_mask) != (new_chandef.punctured & punct_mask)) 721 return -EINVAL; 722 723 new_chandef.punctured = punctured; 724 } 725 726 if (!cfg80211_chandef_valid(&new_chandef)) 727 return -EINVAL; 728 729 /* 730 * If e.g. a 40 MHz BSS channel (erroneously) occupies the center of the 731 * DBE 80 MHz channel, they would be incompatible; check and reject. 732 */ 733 if (!cfg80211_chandef_compatible(&new_chandef, chandef)) 734 return -EINVAL; 735 736 *chandef = new_chandef; 737 return 0; 738 } 739 EXPORT_SYMBOL(cfg80211_chandef_add_dbe); 740 741 static const struct cfg80211_chan_def * 742 check_chandef_primary_compat(const struct cfg80211_chan_def *c1, 743 const struct cfg80211_chan_def *c2, 744 enum nl80211_chan_width primary_chan_width) 745 { 746 u16 punct_c1 = 0, punct_c2 = 0; 747 748 /* check primary is compatible -> error if not */ 749 if (cfg80211_chandef_primary(c1, primary_chan_width, &punct_c1) != 750 cfg80211_chandef_primary(c2, primary_chan_width, &punct_c2)) 751 return ERR_PTR(-EINVAL); 752 753 if (punct_c1 != punct_c2) 754 return ERR_PTR(-EINVAL); 755 756 /* assumes c1 is smaller width, if that was just checked -> done */ 757 if (c1->width == primary_chan_width) 758 return c2; 759 760 /* otherwise continue checking the next width */ 761 return NULL; 762 } 763 764 static const struct cfg80211_chan_def * 765 _cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1, 766 const struct cfg80211_chan_def *c2) 767 { 768 const struct cfg80211_chan_def *ret; 769 770 /* If they are identical, return */ 771 if (cfg80211_chandef_identical(c1, c2)) 772 return c2; 773 774 /* otherwise, must have same control channel */ 775 if (c1->chan != c2->chan) 776 return NULL; 777 778 /* 779 * If they have the same width, but aren't identical, 780 * then they can't be compatible. 781 */ 782 if (c1->width == c2->width) 783 return NULL; 784 785 /* 786 * We need NPCA to be compatible for some scenarios such as 787 * multiple APs, but in this case userspace should configure 788 * identical chandefs including NPCA, even if perhaps one of 789 * the AP interfaces doesn't even advertise it. 790 */ 791 if (c1->npca_chan || c2->npca_chan) 792 return NULL; 793 794 /* 795 * can't be compatible if one of them is S1G 796 * but they don't have the same width. 797 */ 798 #define IS_S1G(width) ((width) == NL80211_CHAN_WIDTH_1 || \ 799 (width) == NL80211_CHAN_WIDTH_2 || \ 800 (width) == NL80211_CHAN_WIDTH_4 || \ 801 (width) == NL80211_CHAN_WIDTH_8 || \ 802 (width) == NL80211_CHAN_WIDTH_16) 803 804 if (IS_S1G(c1->width) || IS_S1G(c2->width)) 805 return NULL; 806 807 /* 808 * Make sure that c1 is always the narrower one, so that later 809 * we either return NULL or c2 and don't have to check both 810 * directions. 811 */ 812 if (c1->width > c2->width) 813 swap(c1, c2); 814 815 /* 816 * No further checks needed if the "narrower" one is only 20 MHz. 817 * Here "narrower" includes being a 20 MHz non-HT channel vs. a 818 * 20 MHz HT (or later) one. 819 */ 820 if (c1->width <= NL80211_CHAN_WIDTH_20) 821 return c2; 822 823 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_40); 824 if (ret) 825 return ret; 826 827 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_80); 828 if (ret) 829 return ret; 830 831 /* 832 * If c1 is 80+80, then c2 is 160 or higher, but that cannot 833 * match. If c2 was also 80+80 it was already either accepted 834 * or rejected above (identical or not, respectively.) 835 */ 836 if (c1->width == NL80211_CHAN_WIDTH_80P80) 837 return NULL; 838 839 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_160); 840 if (ret) 841 return ret; 842 843 /* 844 * Getting here would mean they're both wider than 160, have the 845 * same primary 160, but are not identical - this cannot happen 846 * since they must be 320 (no wider chandefs exist, at least yet.) 847 */ 848 WARN_ON_ONCE(1); 849 850 return NULL; 851 } 852 853 const struct cfg80211_chan_def * 854 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1, 855 const struct cfg80211_chan_def *c2) 856 { 857 const struct cfg80211_chan_def *ret; 858 859 ret = _cfg80211_chandef_compatible(c1, c2); 860 if (IS_ERR(ret)) 861 return NULL; 862 return ret; 863 } 864 EXPORT_SYMBOL(cfg80211_chandef_compatible); 865 866 void cfg80211_set_dfs_state(struct wiphy *wiphy, 867 const struct cfg80211_chan_def *chandef, 868 enum nl80211_dfs_state dfs_state) 869 { 870 struct ieee80211_channel *c; 871 int width; 872 873 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 874 return; 875 876 width = cfg80211_chandef_get_width(chandef); 877 if (width < 0) 878 return; 879 880 for_each_subchan(chandef, freq, cf) { 881 c = ieee80211_get_channel_khz(wiphy, freq); 882 if (!c || !(c->flags & IEEE80211_CHAN_RADAR)) 883 continue; 884 885 c->dfs_state = dfs_state; 886 c->dfs_state_entered = jiffies; 887 } 888 } 889 890 void cfg80211_set_cac_state(struct wiphy *wiphy, 891 const struct cfg80211_chan_def *chandef, 892 bool cac_ongoing) 893 { 894 struct ieee80211_channel *c; 895 int width; 896 u64 cac_time; 897 898 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 899 return; 900 901 width = cfg80211_chandef_get_width(chandef); 902 if (width < 0) 903 return; 904 905 /* Get the same timestamp for all subchannels */ 906 cac_time = cac_ongoing ? ktime_get_boottime_ns() : 0; 907 908 for_each_subchan(chandef, freq, cf) { 909 c = ieee80211_get_channel_khz(wiphy, freq); 910 if (!c) 911 continue; 912 913 c->cac_start_time = cac_time; 914 } 915 } 916 917 static bool 918 cfg80211_dfs_permissive_check_wdev(struct cfg80211_registered_device *rdev, 919 enum nl80211_iftype iftype, 920 struct wireless_dev *wdev, 921 struct ieee80211_channel *chan) 922 { 923 unsigned int link_id; 924 925 for_each_valid_link(wdev, link_id) { 926 struct ieee80211_channel *other_chan = NULL; 927 struct cfg80211_chan_def chandef = {}; 928 int ret; 929 930 /* In order to avoid daisy chaining only allow BSS STA */ 931 if (wdev->iftype != NL80211_IFTYPE_STATION || 932 !wdev->links[link_id].client.current_bss) 933 continue; 934 935 other_chan = 936 wdev->links[link_id].client.current_bss->pub.channel; 937 938 if (!other_chan) 939 continue; 940 941 if (chan == other_chan) 942 return true; 943 944 /* continue if we can't get the channel */ 945 ret = rdev_get_channel(rdev, wdev, link_id, &chandef); 946 if (ret) 947 continue; 948 949 if (cfg80211_is_sub_chan(&chandef, chan, false)) 950 return true; 951 } 952 953 return false; 954 } 955 956 /* 957 * Check if P2P GO is allowed to operate on a DFS channel 958 */ 959 static bool cfg80211_dfs_permissive_chan(struct wiphy *wiphy, 960 enum nl80211_iftype iftype, 961 struct ieee80211_channel *chan) 962 { 963 struct wireless_dev *wdev; 964 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 965 966 lockdep_assert_held(&rdev->wiphy.mtx); 967 968 if (!wiphy_ext_feature_isset(&rdev->wiphy, 969 NL80211_EXT_FEATURE_DFS_CONCURRENT) || 970 !(chan->flags & IEEE80211_CHAN_DFS_CONCURRENT)) 971 return false; 972 973 /* only valid for P2P GO */ 974 if (iftype != NL80211_IFTYPE_P2P_GO) 975 return false; 976 977 /* 978 * Allow only if there's a concurrent BSS 979 */ 980 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 981 bool ret = cfg80211_dfs_permissive_check_wdev(rdev, iftype, 982 wdev, chan); 983 if (ret) 984 return ret; 985 } 986 987 return false; 988 } 989 990 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy, 991 const struct cfg80211_chan_def *chandef, 992 enum nl80211_iftype iftype) 993 { 994 struct ieee80211_channel *c; 995 996 /* DFS is not required for S1G */ 997 if (cfg80211_chandef_is_s1g(chandef)) 998 return 0; 999 1000 for_each_subchan(chandef, freq, cf) { 1001 c = ieee80211_get_channel_khz(wiphy, freq); 1002 if (!c) 1003 return -EINVAL; 1004 1005 if (c->flags & IEEE80211_CHAN_RADAR && 1006 !cfg80211_dfs_permissive_chan(wiphy, iftype, c)) 1007 return 1; 1008 } 1009 1010 return 0; 1011 } 1012 1013 1014 int cfg80211_chandef_dfs_required(struct wiphy *wiphy, 1015 const struct cfg80211_chan_def *chandef, 1016 enum nl80211_iftype iftype) 1017 { 1018 int width; 1019 int ret; 1020 1021 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 1022 return -EINVAL; 1023 1024 switch (iftype) { 1025 case NL80211_IFTYPE_ADHOC: 1026 case NL80211_IFTYPE_AP: 1027 case NL80211_IFTYPE_P2P_GO: 1028 case NL80211_IFTYPE_MESH_POINT: 1029 case NL80211_IFTYPE_NAN: 1030 width = cfg80211_chandef_get_width(chandef); 1031 if (width < 0) 1032 return -EINVAL; 1033 1034 ret = cfg80211_get_chans_dfs_required(wiphy, chandef, iftype); 1035 1036 return (ret > 0) ? BIT(chandef->width) : ret; 1037 break; 1038 case NL80211_IFTYPE_STATION: 1039 case NL80211_IFTYPE_OCB: 1040 case NL80211_IFTYPE_P2P_CLIENT: 1041 case NL80211_IFTYPE_MONITOR: 1042 case NL80211_IFTYPE_AP_VLAN: 1043 case NL80211_IFTYPE_P2P_DEVICE: 1044 case NL80211_IFTYPE_NAN_DATA: 1045 case NL80211_IFTYPE_PD: 1046 break; 1047 case NL80211_IFTYPE_WDS: 1048 case NL80211_IFTYPE_UNSPECIFIED: 1049 case NUM_NL80211_IFTYPES: 1050 WARN_ON(1); 1051 } 1052 1053 return 0; 1054 } 1055 EXPORT_SYMBOL(cfg80211_chandef_dfs_required); 1056 1057 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy, 1058 const struct cfg80211_chan_def *chandef) 1059 { 1060 struct ieee80211_channel *c; 1061 int width, count = 0; 1062 1063 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 1064 return false; 1065 1066 width = cfg80211_chandef_get_width(chandef); 1067 if (width < 0) 1068 return false; 1069 1070 /* 1071 * Check entire range of channels for the bandwidth. 1072 * Check all channels are DFS channels (DFS_USABLE or 1073 * DFS_AVAILABLE). Return number of usable channels 1074 * (require CAC). Allow DFS and non-DFS channel mix. 1075 */ 1076 for_each_subchan(chandef, freq, cf) { 1077 c = ieee80211_get_channel_khz(wiphy, freq); 1078 if (!c) 1079 return false; 1080 1081 if (c->flags & IEEE80211_CHAN_DISABLED) 1082 return false; 1083 1084 if (c->flags & IEEE80211_CHAN_RADAR) { 1085 if (c->dfs_state == NL80211_DFS_UNAVAILABLE) 1086 return false; 1087 1088 if (c->dfs_state == NL80211_DFS_USABLE) 1089 count++; 1090 } 1091 } 1092 1093 return count > 0; 1094 } 1095 EXPORT_SYMBOL(cfg80211_chandef_dfs_usable); 1096 1097 /* 1098 * Checks if center frequency of chan falls with in the bandwidth 1099 * range of chandef. 1100 */ 1101 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef, 1102 struct ieee80211_channel *chan, 1103 bool primary_only) 1104 { 1105 int width; 1106 u32 freq; 1107 1108 if (!chandef->chan) 1109 return false; 1110 1111 if (chandef->chan->center_freq == chan->center_freq) 1112 return true; 1113 1114 if (primary_only) 1115 return false; 1116 1117 width = cfg80211_chandef_get_width(chandef); 1118 if (width <= 20) 1119 return false; 1120 1121 for (freq = chandef->center_freq1 - width / 2 + 10; 1122 freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) { 1123 if (chan->center_freq == freq) 1124 return true; 1125 } 1126 1127 if (!chandef->center_freq2) 1128 return false; 1129 1130 for (freq = chandef->center_freq2 - width / 2 + 10; 1131 freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) { 1132 if (chan->center_freq == freq) 1133 return true; 1134 } 1135 1136 return false; 1137 } 1138 1139 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev) 1140 { 1141 unsigned int link; 1142 1143 lockdep_assert_wiphy(wdev->wiphy); 1144 1145 switch (wdev->iftype) { 1146 case NL80211_IFTYPE_AP: 1147 case NL80211_IFTYPE_P2P_GO: 1148 for_each_valid_link(wdev, link) { 1149 if (wdev->links[link].ap.beacon_interval) 1150 return true; 1151 } 1152 break; 1153 case NL80211_IFTYPE_ADHOC: 1154 if (wdev->u.ibss.ssid_len) 1155 return true; 1156 break; 1157 case NL80211_IFTYPE_MESH_POINT: 1158 if (wdev->u.mesh.id_len) 1159 return true; 1160 break; 1161 case NL80211_IFTYPE_STATION: 1162 case NL80211_IFTYPE_OCB: 1163 case NL80211_IFTYPE_P2P_CLIENT: 1164 case NL80211_IFTYPE_MONITOR: 1165 case NL80211_IFTYPE_AP_VLAN: 1166 case NL80211_IFTYPE_P2P_DEVICE: 1167 /* Can NAN type be considered as beaconing interface? */ 1168 case NL80211_IFTYPE_NAN: 1169 case NL80211_IFTYPE_NAN_DATA: 1170 case NL80211_IFTYPE_PD: 1171 break; 1172 case NL80211_IFTYPE_UNSPECIFIED: 1173 case NL80211_IFTYPE_WDS: 1174 case NUM_NL80211_IFTYPES: 1175 WARN_ON(1); 1176 } 1177 1178 return false; 1179 } 1180 1181 bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev, 1182 struct ieee80211_channel *chan, 1183 bool primary_only) 1184 { 1185 unsigned int link; 1186 1187 switch (wdev->iftype) { 1188 case NL80211_IFTYPE_AP: 1189 case NL80211_IFTYPE_P2P_GO: 1190 for_each_valid_link(wdev, link) { 1191 if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef, 1192 chan, primary_only)) 1193 return true; 1194 } 1195 break; 1196 case NL80211_IFTYPE_ADHOC: 1197 return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan, 1198 primary_only); 1199 case NL80211_IFTYPE_MESH_POINT: 1200 return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan, 1201 primary_only); 1202 default: 1203 break; 1204 } 1205 1206 return false; 1207 } 1208 1209 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy, 1210 struct ieee80211_channel *chan) 1211 { 1212 struct wireless_dev *wdev; 1213 1214 lockdep_assert_wiphy(wiphy); 1215 1216 list_for_each_entry(wdev, &wiphy->wdev_list, list) { 1217 if (!cfg80211_beaconing_iface_active(wdev)) 1218 continue; 1219 1220 if (cfg80211_wdev_on_sub_chan(wdev, chan, false)) 1221 return true; 1222 } 1223 1224 return false; 1225 } 1226 1227 static bool 1228 cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev, 1229 struct ieee80211_channel *channel) 1230 { 1231 if (!rdev->background_radar_wdev) 1232 return false; 1233 1234 if (!cfg80211_chandef_valid(&rdev->background_radar_chandef)) 1235 return false; 1236 1237 return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel, 1238 false); 1239 } 1240 1241 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy, 1242 struct ieee80211_channel *chan) 1243 { 1244 struct cfg80211_registered_device *rdev; 1245 1246 ASSERT_RTNL(); 1247 1248 if (!(chan->flags & IEEE80211_CHAN_RADAR)) 1249 return false; 1250 1251 for_each_rdev(rdev) { 1252 bool found; 1253 1254 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy)) 1255 continue; 1256 1257 guard(wiphy)(&rdev->wiphy); 1258 1259 found = cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan) || 1260 cfg80211_offchan_chain_is_active(rdev, chan); 1261 1262 if (found) 1263 return true; 1264 } 1265 1266 return false; 1267 } 1268 1269 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy, 1270 const struct cfg80211_chan_def *chandef) 1271 { 1272 struct ieee80211_channel *c; 1273 int width; 1274 bool dfs_offload; 1275 1276 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 1277 return false; 1278 1279 width = cfg80211_chandef_get_width(chandef); 1280 if (width < 0) 1281 return false; 1282 1283 dfs_offload = wiphy_ext_feature_isset(wiphy, 1284 NL80211_EXT_FEATURE_DFS_OFFLOAD); 1285 1286 /* 1287 * Check entire range of channels for the bandwidth. 1288 * If any channel in between is disabled or has not 1289 * had gone through CAC return false 1290 */ 1291 for_each_subchan(chandef, freq, cf) { 1292 c = ieee80211_get_channel_khz(wiphy, freq); 1293 if (!c) 1294 return false; 1295 1296 if (c->flags & IEEE80211_CHAN_DISABLED) 1297 return false; 1298 1299 if ((c->flags & IEEE80211_CHAN_RADAR) && 1300 (c->dfs_state != NL80211_DFS_AVAILABLE) && 1301 !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload)) 1302 return false; 1303 } 1304 1305 return true; 1306 } 1307 1308 unsigned int 1309 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy, 1310 const struct cfg80211_chan_def *chandef) 1311 { 1312 struct ieee80211_channel *c; 1313 int width; 1314 unsigned int t1 = 0, t2 = 0; 1315 1316 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 1317 return 0; 1318 1319 width = cfg80211_chandef_get_width(chandef); 1320 if (width < 0) 1321 return 0; 1322 1323 for_each_subchan(chandef, freq, cf) { 1324 c = ieee80211_get_channel_khz(wiphy, freq); 1325 if (!c || (c->flags & IEEE80211_CHAN_DISABLED)) { 1326 if (cf == 1) 1327 t1 = INT_MAX; 1328 else 1329 t2 = INT_MAX; 1330 continue; 1331 } 1332 1333 if (!(c->flags & IEEE80211_CHAN_RADAR)) 1334 continue; 1335 1336 if (cf == 1 && c->dfs_cac_ms > t1) 1337 t1 = c->dfs_cac_ms; 1338 1339 if (cf == 2 && c->dfs_cac_ms > t2) 1340 t2 = c->dfs_cac_ms; 1341 } 1342 1343 if (t1 == INT_MAX && t2 == INT_MAX) 1344 return 0; 1345 1346 if (t1 == INT_MAX) 1347 return t2; 1348 1349 if (t2 == INT_MAX) 1350 return t1; 1351 1352 return max(t1, t2); 1353 } 1354 EXPORT_SYMBOL(cfg80211_chandef_dfs_cac_time); 1355 1356 /* check if the operating channels are valid and supported */ 1357 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels, 1358 enum ieee80211_edmg_bw_config edmg_bw_config, 1359 int primary_channel, 1360 struct ieee80211_edmg *edmg_cap) 1361 { 1362 struct ieee80211_channel *chan; 1363 int i, freq; 1364 int channels_counter = 0; 1365 1366 if (!edmg_channels && !edmg_bw_config) 1367 return true; 1368 1369 if ((!edmg_channels && edmg_bw_config) || 1370 (edmg_channels && !edmg_bw_config)) 1371 return false; 1372 1373 if (!(edmg_channels & BIT(primary_channel - 1))) 1374 return false; 1375 1376 /* 60GHz channels 1..6 */ 1377 for (i = 0; i < 6; i++) { 1378 if (!(edmg_channels & BIT(i))) 1379 continue; 1380 1381 if (!(edmg_cap->channels & BIT(i))) 1382 return false; 1383 1384 channels_counter++; 1385 1386 freq = ieee80211_channel_to_frequency(i + 1, 1387 NL80211_BAND_60GHZ); 1388 chan = ieee80211_get_channel(wiphy, freq); 1389 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) 1390 return false; 1391 } 1392 1393 /* IEEE802.11 allows max 4 channels */ 1394 if (channels_counter > 4) 1395 return false; 1396 1397 /* check bw_config is a subset of what driver supports 1398 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13) 1399 */ 1400 if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4)) 1401 return false; 1402 1403 if (edmg_bw_config > edmg_cap->bw_config) 1404 return false; 1405 1406 return true; 1407 } 1408 1409 static bool cfg80211_s1g_usable(struct wiphy *wiphy, 1410 const struct cfg80211_chan_def *chandef) 1411 { 1412 u32 freq_khz; 1413 const struct ieee80211_channel *chan; 1414 u32 pri_khz = ieee80211_channel_to_khz(chandef->chan); 1415 u32 end_khz = cfg80211_s1g_get_end_freq_khz(chandef); 1416 u32 start_khz = cfg80211_s1g_get_start_freq_khz(chandef); 1417 int width_mhz = cfg80211_chandef_get_width(chandef); 1418 u32 prohibited_flags = IEEE80211_CHAN_DISABLED; 1419 1420 if (width_mhz >= 16) 1421 prohibited_flags |= IEEE80211_CHAN_NO_16MHZ; 1422 if (width_mhz >= 8) 1423 prohibited_flags |= IEEE80211_CHAN_NO_8MHZ; 1424 if (width_mhz >= 4) 1425 prohibited_flags |= IEEE80211_CHAN_NO_4MHZ; 1426 1427 if (chandef->chan->flags & IEEE80211_CHAN_S1G_NO_PRIMARY) 1428 return false; 1429 1430 if (pri_khz < start_khz || pri_khz > end_khz) 1431 return false; 1432 1433 for_each_s1g_subchan(chandef, freq_khz) { 1434 chan = ieee80211_get_channel_khz(wiphy, freq_khz); 1435 if (!chan || (chan->flags & prohibited_flags)) 1436 return false; 1437 } 1438 1439 if (chandef->s1g_primary_2mhz) { 1440 u32 sib_khz; 1441 const struct ieee80211_channel *sibling; 1442 1443 sibling = cfg80211_s1g_get_primary_sibling(wiphy, chandef); 1444 if (!sibling) 1445 return false; 1446 1447 if (sibling->flags & IEEE80211_CHAN_S1G_NO_PRIMARY) 1448 return false; 1449 1450 sib_khz = ieee80211_channel_to_khz(sibling); 1451 if (sib_khz < start_khz || sib_khz > end_khz) 1452 return false; 1453 } 1454 1455 return true; 1456 } 1457 1458 bool _cfg80211_chandef_usable(struct wiphy *wiphy, 1459 const struct cfg80211_chan_def *chandef, 1460 u32 prohibited_flags, 1461 u32 permitting_flags) 1462 { 1463 struct ieee80211_sta_ht_cap *ht_cap; 1464 struct ieee80211_sta_vht_cap *vht_cap; 1465 struct ieee80211_edmg *edmg_cap; 1466 u32 width, control_freq, cap; 1467 bool ext_nss_cap, support_80_80 = false, support_320 = false; 1468 const struct ieee80211_sband_iftype_data *iftd; 1469 struct ieee80211_supported_band *sband; 1470 struct ieee80211_channel *c; 1471 int i; 1472 1473 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 1474 return false; 1475 1476 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap; 1477 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap; 1478 edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap; 1479 ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) & 1480 IEEE80211_VHT_EXT_NSS_BW_CAPABLE; 1481 1482 if (cfg80211_chandef_is_s1g(chandef)) 1483 return cfg80211_s1g_usable(wiphy, chandef); 1484 1485 if (edmg_cap->channels && 1486 !cfg80211_edmg_usable(wiphy, 1487 chandef->edmg.channels, 1488 chandef->edmg.bw_config, 1489 chandef->chan->hw_value, 1490 edmg_cap)) 1491 return false; 1492 1493 control_freq = chandef->chan->center_freq; 1494 1495 switch (chandef->width) { 1496 case NL80211_CHAN_WIDTH_20: 1497 if (!ht_cap->ht_supported && 1498 chandef->chan->band != NL80211_BAND_6GHZ) 1499 return false; 1500 fallthrough; 1501 case NL80211_CHAN_WIDTH_20_NOHT: 1502 prohibited_flags |= IEEE80211_CHAN_NO_20MHZ; 1503 width = 20; 1504 break; 1505 case NL80211_CHAN_WIDTH_40: 1506 width = 40; 1507 if (chandef->chan->band == NL80211_BAND_6GHZ) 1508 break; 1509 if (!ht_cap->ht_supported) 1510 return false; 1511 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) || 1512 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT) 1513 return false; 1514 if (chandef->center_freq1 < control_freq && 1515 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS) 1516 return false; 1517 if (chandef->center_freq1 > control_freq && 1518 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS) 1519 return false; 1520 break; 1521 case NL80211_CHAN_WIDTH_80P80: 1522 cap = vht_cap->cap; 1523 support_80_80 = 1524 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) || 1525 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ && 1526 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) || 1527 (ext_nss_cap && 1528 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1); 1529 if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80) 1530 return false; 1531 fallthrough; 1532 case NL80211_CHAN_WIDTH_80: 1533 prohibited_flags |= IEEE80211_CHAN_NO_80MHZ; 1534 width = 80; 1535 if (chandef->chan->band == NL80211_BAND_6GHZ) 1536 break; 1537 if (!vht_cap->vht_supported) 1538 return false; 1539 break; 1540 case NL80211_CHAN_WIDTH_160: 1541 prohibited_flags |= IEEE80211_CHAN_NO_160MHZ; 1542 width = 160; 1543 if (chandef->chan->band == NL80211_BAND_6GHZ) 1544 break; 1545 if (!vht_cap->vht_supported) 1546 return false; 1547 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK; 1548 if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ && 1549 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ && 1550 !(ext_nss_cap && 1551 (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))) 1552 return false; 1553 break; 1554 case NL80211_CHAN_WIDTH_320: 1555 prohibited_flags |= IEEE80211_CHAN_NO_320MHZ; 1556 width = 320; 1557 1558 if (chandef->chan->band != NL80211_BAND_6GHZ) 1559 return false; 1560 1561 sband = wiphy->bands[NL80211_BAND_6GHZ]; 1562 if (!sband) 1563 return false; 1564 1565 for_each_sband_iftype_data(sband, i, iftd) { 1566 if (!iftd->eht_cap.has_eht) 1567 continue; 1568 1569 if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] & 1570 IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) { 1571 support_320 = true; 1572 break; 1573 } 1574 } 1575 1576 if (!support_320) 1577 return false; 1578 break; 1579 default: 1580 WARN_ON_ONCE(1); 1581 return false; 1582 } 1583 1584 /* 1585 * TODO: What if there are only certain 80/160/80+80 MHz channels 1586 * allowed by the driver, or only certain combinations? 1587 * For 40 MHz the driver can set the NO_HT40 flags, but for 1588 * 80/160 MHz and in particular 80+80 MHz this isn't really 1589 * feasible and we only have NO_80MHZ/NO_160MHZ so far but 1590 * no way to cover 80+80 MHz or more complex restrictions. 1591 * Note that such restrictions also need to be advertised to 1592 * userspace, for example for P2P channel selection. 1593 */ 1594 1595 if (width > 20) 1596 prohibited_flags |= IEEE80211_CHAN_NO_OFDM; 1597 1598 /* 5 and 10 MHz are only defined for the OFDM PHY */ 1599 if (width < 20) 1600 prohibited_flags |= IEEE80211_CHAN_NO_OFDM; 1601 1602 for_each_subchan(chandef, freq, cf) { 1603 c = ieee80211_get_channel_khz(wiphy, freq); 1604 if (!c) 1605 return false; 1606 if (c->flags & permitting_flags) 1607 continue; 1608 if (c->flags & prohibited_flags) 1609 return false; 1610 } 1611 1612 return true; 1613 } 1614 1615 bool cfg80211_chandef_usable(struct wiphy *wiphy, 1616 const struct cfg80211_chan_def *chandef, 1617 u32 prohibited_flags) 1618 { 1619 return _cfg80211_chandef_usable(wiphy, chandef, prohibited_flags, 0); 1620 } 1621 EXPORT_SYMBOL(cfg80211_chandef_usable); 1622 1623 static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype, 1624 struct wireless_dev *wdev, 1625 struct ieee80211_channel *chan) 1626 { 1627 struct ieee80211_channel *other_chan = NULL; 1628 unsigned int link_id; 1629 int r1, r2; 1630 1631 for_each_valid_link(wdev, link_id) { 1632 if (wdev->iftype == NL80211_IFTYPE_STATION && 1633 wdev->links[link_id].client.current_bss) 1634 other_chan = wdev->links[link_id].client.current_bss->pub.channel; 1635 1636 /* 1637 * If a GO already operates on the same GO_CONCURRENT channel, 1638 * this one (maybe the same one) can beacon as well. We allow 1639 * the operation even if the station we relied on with 1640 * GO_CONCURRENT is disconnected now. But then we must make sure 1641 * we're not outdoor on an indoor-only channel. 1642 */ 1643 if (iftype == NL80211_IFTYPE_P2P_GO && 1644 wdev->iftype == NL80211_IFTYPE_P2P_GO && 1645 wdev->links[link_id].ap.beacon_interval && 1646 !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY)) 1647 other_chan = wdev->links[link_id].ap.chandef.chan; 1648 1649 if (!other_chan) 1650 continue; 1651 1652 if (chan == other_chan) 1653 return true; 1654 1655 if (chan->band != NL80211_BAND_5GHZ && 1656 chan->band != NL80211_BAND_6GHZ) 1657 continue; 1658 1659 r1 = cfg80211_get_unii(chan->center_freq); 1660 r2 = cfg80211_get_unii(other_chan->center_freq); 1661 1662 if (r1 != -EINVAL && r1 == r2) { 1663 /* 1664 * At some locations channels 149-165 are considered a 1665 * bundle, but at other locations, e.g., Indonesia, 1666 * channels 149-161 are considered a bundle while 1667 * channel 165 is left out and considered to be in a 1668 * different bundle. Thus, in case that there is a 1669 * station interface connected to an AP on channel 165, 1670 * it is assumed that channels 149-161 are allowed for 1671 * GO operations. However, having a station interface 1672 * connected to an AP on channels 149-161, does not 1673 * allow GO operation on channel 165. 1674 */ 1675 if (chan->center_freq == 5825 && 1676 other_chan->center_freq != 5825) 1677 continue; 1678 return true; 1679 } 1680 } 1681 1682 return false; 1683 } 1684 1685 /* 1686 * Check if the channel can be used under permissive conditions mandated by 1687 * some regulatory bodies, i.e., the channel is marked with 1688 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface 1689 * associated to an AP on the same channel or on the same UNII band 1690 * (assuming that the AP is an authorized master). 1691 * In addition allow operation on a channel on which indoor operation is 1692 * allowed, iff we are currently operating in an indoor environment. 1693 */ 1694 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy, 1695 enum nl80211_iftype iftype, 1696 struct ieee80211_channel *chan) 1697 { 1698 struct wireless_dev *wdev; 1699 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1700 1701 lockdep_assert_held(&rdev->wiphy.mtx); 1702 1703 if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) || 1704 !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR)) 1705 return false; 1706 1707 /* only valid for GO and TDLS off-channel (station/p2p-CL) */ 1708 if (iftype != NL80211_IFTYPE_P2P_GO && 1709 iftype != NL80211_IFTYPE_STATION && 1710 iftype != NL80211_IFTYPE_P2P_CLIENT) 1711 return false; 1712 1713 if (regulatory_indoor_allowed() && 1714 (chan->flags & IEEE80211_CHAN_INDOOR_ONLY)) 1715 return true; 1716 1717 if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT)) 1718 return false; 1719 1720 /* 1721 * Generally, it is possible to rely on another device/driver to allow 1722 * the IR concurrent relaxation, however, since the device can further 1723 * enforce the relaxation (by doing a similar verifications as this), 1724 * and thus fail the GO instantiation, consider only the interfaces of 1725 * the current registered device. 1726 */ 1727 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 1728 bool ret; 1729 1730 ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan); 1731 if (ret) 1732 return ret; 1733 } 1734 1735 return false; 1736 } 1737 1738 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy, 1739 struct cfg80211_chan_def *chandef, 1740 enum nl80211_iftype iftype, 1741 u32 prohibited_flags, 1742 u32 permitting_flags) 1743 { 1744 bool res, check_radar; 1745 int dfs_required; 1746 1747 trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, 1748 prohibited_flags, 1749 permitting_flags); 1750 1751 if (!_cfg80211_chandef_usable(wiphy, chandef, 1752 IEEE80211_CHAN_DISABLED, 0)) 1753 return false; 1754 1755 dfs_required = cfg80211_chandef_dfs_required(wiphy, chandef, iftype); 1756 check_radar = dfs_required != 0; 1757 1758 if (dfs_required > 0 && 1759 cfg80211_chandef_dfs_available(wiphy, chandef)) { 1760 /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */ 1761 prohibited_flags &= ~IEEE80211_CHAN_NO_IR; 1762 check_radar = false; 1763 } 1764 1765 if (check_radar && 1766 !_cfg80211_chandef_usable(wiphy, chandef, 1767 IEEE80211_CHAN_RADAR, 0)) 1768 return false; 1769 1770 res = _cfg80211_chandef_usable(wiphy, chandef, 1771 prohibited_flags, 1772 permitting_flags); 1773 1774 trace_cfg80211_return_bool(res); 1775 return res; 1776 } 1777 1778 bool cfg80211_reg_check_beaconing(struct wiphy *wiphy, 1779 struct cfg80211_chan_def *chandef, 1780 struct cfg80211_beaconing_check_config *cfg) 1781 { 1782 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1783 u32 permitting_flags = 0; 1784 bool check_no_ir = true; 1785 1786 /* 1787 * Under certain conditions suggested by some regulatory bodies a 1788 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag 1789 * only if such relaxations are not enabled and the conditions are not 1790 * met. 1791 */ 1792 if (cfg->relax) { 1793 lockdep_assert_held(&rdev->wiphy.mtx); 1794 check_no_ir = !cfg80211_ir_permissive_chan(wiphy, cfg->iftype, 1795 chandef->chan); 1796 } 1797 1798 if (cfg->reg_power == IEEE80211_REG_VLP_AP) 1799 permitting_flags |= IEEE80211_CHAN_ALLOW_6GHZ_VLP_AP; 1800 1801 if ((cfg->iftype == NL80211_IFTYPE_P2P_GO || 1802 cfg->iftype == NL80211_IFTYPE_AP) && 1803 (chandef->width == NL80211_CHAN_WIDTH_20_NOHT || 1804 chandef->width == NL80211_CHAN_WIDTH_20)) 1805 permitting_flags |= IEEE80211_CHAN_ALLOW_20MHZ_ACTIVITY; 1806 1807 return _cfg80211_reg_can_beacon(wiphy, chandef, cfg->iftype, 1808 check_no_ir ? IEEE80211_CHAN_NO_IR : 0, 1809 permitting_flags); 1810 } 1811 EXPORT_SYMBOL(cfg80211_reg_check_beaconing); 1812 1813 static bool cfg80211_can_set_monitor_channel(struct cfg80211_registered_device *rdev, 1814 struct cfg80211_chan_def *chandef) 1815 { 1816 struct wireless_dev *wdev; 1817 int radio_idx; 1818 1819 lockdep_assert_held(&rdev->wiphy.mtx); 1820 1821 if (rdev->num_running_monitor_ifaces < 1) 1822 return false; 1823 1824 if (cfg80211_has_monitors_only(rdev)) 1825 return true; 1826 1827 radio_idx = cfg80211_get_radio_idx_by_chan(&rdev->wiphy, chandef->chan); 1828 /* No defined radio covers this channel. Fall back on global behavior */ 1829 if (radio_idx < 0) 1830 return false; 1831 1832 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 1833 if (wdev->iftype == NL80211_IFTYPE_MONITOR) 1834 continue; 1835 if (!wdev->netdev) 1836 continue; 1837 if (rdev_get_radio_mask(rdev, wdev->netdev) & BIT(radio_idx)) 1838 return false; 1839 } 1840 1841 return true; 1842 } 1843 1844 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev, 1845 struct net_device *dev, 1846 struct cfg80211_chan_def *chandef) 1847 { 1848 if (!rdev->ops->set_monitor_channel) 1849 return -EOPNOTSUPP; 1850 if (!cfg80211_can_set_monitor_channel(rdev, chandef)) 1851 return -EBUSY; 1852 1853 return rdev_set_monitor_channel(rdev, dev, chandef); 1854 } 1855 1856 bool cfg80211_any_usable_channels(struct wiphy *wiphy, 1857 unsigned long sband_mask, 1858 u32 prohibited_flags) 1859 { 1860 int idx; 1861 1862 prohibited_flags |= IEEE80211_CHAN_DISABLED; 1863 1864 for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) { 1865 struct ieee80211_supported_band *sband = wiphy->bands[idx]; 1866 int chanidx; 1867 1868 if (!sband) 1869 continue; 1870 1871 for (chanidx = 0; chanidx < sband->n_channels; chanidx++) { 1872 struct ieee80211_channel *chan; 1873 1874 chan = &sband->channels[chanidx]; 1875 1876 if (chan->flags & prohibited_flags) 1877 continue; 1878 1879 return true; 1880 } 1881 } 1882 1883 return false; 1884 } 1885 EXPORT_SYMBOL(cfg80211_any_usable_channels); 1886 1887 struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev, 1888 unsigned int link_id) 1889 { 1890 lockdep_assert_wiphy(wdev->wiphy); 1891 1892 WARN_ON(wdev->valid_links && !(wdev->valid_links & BIT(link_id))); 1893 WARN_ON(!wdev->valid_links && link_id > 0); 1894 1895 switch (wdev->iftype) { 1896 case NL80211_IFTYPE_MESH_POINT: 1897 return &wdev->u.mesh.chandef; 1898 case NL80211_IFTYPE_ADHOC: 1899 return &wdev->u.ibss.chandef; 1900 case NL80211_IFTYPE_OCB: 1901 return &wdev->u.ocb.chandef; 1902 case NL80211_IFTYPE_AP: 1903 case NL80211_IFTYPE_P2P_GO: 1904 return &wdev->links[link_id].ap.chandef; 1905 default: 1906 return NULL; 1907 } 1908 } 1909 EXPORT_SYMBOL(wdev_chandef); 1910