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