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 static const struct cfg80211_chan_def * 623 check_chandef_primary_compat(const struct cfg80211_chan_def *c1, 624 const struct cfg80211_chan_def *c2, 625 enum nl80211_chan_width primary_chan_width) 626 { 627 u16 punct_c1 = 0, punct_c2 = 0; 628 629 /* check primary is compatible -> error if not */ 630 if (cfg80211_chandef_primary(c1, primary_chan_width, &punct_c1) != 631 cfg80211_chandef_primary(c2, primary_chan_width, &punct_c2)) 632 return ERR_PTR(-EINVAL); 633 634 if (punct_c1 != punct_c2) 635 return ERR_PTR(-EINVAL); 636 637 /* assumes c1 is smaller width, if that was just checked -> done */ 638 if (c1->width == primary_chan_width) 639 return c2; 640 641 /* otherwise continue checking the next width */ 642 return NULL; 643 } 644 645 static const struct cfg80211_chan_def * 646 _cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1, 647 const struct cfg80211_chan_def *c2) 648 { 649 const struct cfg80211_chan_def *ret; 650 651 /* If they are identical, return */ 652 if (cfg80211_chandef_identical(c1, c2)) 653 return c2; 654 655 /* otherwise, must have same control channel */ 656 if (c1->chan != c2->chan) 657 return NULL; 658 659 /* 660 * If they have the same width, but aren't identical, 661 * then they can't be compatible. 662 */ 663 if (c1->width == c2->width) 664 return NULL; 665 666 /* 667 * We need NPCA to be compatible for some scenarios such as 668 * multiple APs, but in this case userspace should configure 669 * identical chandefs including NPCA, even if perhaps one of 670 * the AP interfaces doesn't even advertise it. 671 */ 672 if (c1->npca_chan || c2->npca_chan) 673 return NULL; 674 675 /* 676 * can't be compatible if one of them is 5/10 MHz or S1G 677 * but they don't have the same width. 678 */ 679 #define NARROW_OR_S1G(width) ((width) == NL80211_CHAN_WIDTH_5 || \ 680 (width) == NL80211_CHAN_WIDTH_10 || \ 681 (width) == NL80211_CHAN_WIDTH_1 || \ 682 (width) == NL80211_CHAN_WIDTH_2 || \ 683 (width) == NL80211_CHAN_WIDTH_4 || \ 684 (width) == NL80211_CHAN_WIDTH_8 || \ 685 (width) == NL80211_CHAN_WIDTH_16) 686 687 if (NARROW_OR_S1G(c1->width) || NARROW_OR_S1G(c2->width)) 688 return NULL; 689 690 /* 691 * Make sure that c1 is always the narrower one, so that later 692 * we either return NULL or c2 and don't have to check both 693 * directions. 694 */ 695 if (c1->width > c2->width) 696 swap(c1, c2); 697 698 /* 699 * No further checks needed if the "narrower" one is only 20 MHz. 700 * Here "narrower" includes being a 20 MHz non-HT channel vs. a 701 * 20 MHz HT (or later) one. 702 */ 703 if (c1->width <= NL80211_CHAN_WIDTH_20) 704 return c2; 705 706 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_40); 707 if (ret) 708 return ret; 709 710 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_80); 711 if (ret) 712 return ret; 713 714 /* 715 * If c1 is 80+80, then c2 is 160 or higher, but that cannot 716 * match. If c2 was also 80+80 it was already either accepted 717 * or rejected above (identical or not, respectively.) 718 */ 719 if (c1->width == NL80211_CHAN_WIDTH_80P80) 720 return NULL; 721 722 ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_160); 723 if (ret) 724 return ret; 725 726 /* 727 * Getting here would mean they're both wider than 160, have the 728 * same primary 160, but are not identical - this cannot happen 729 * since they must be 320 (no wider chandefs exist, at least yet.) 730 */ 731 WARN_ON_ONCE(1); 732 733 return NULL; 734 } 735 736 const struct cfg80211_chan_def * 737 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1, 738 const struct cfg80211_chan_def *c2) 739 { 740 const struct cfg80211_chan_def *ret; 741 742 ret = _cfg80211_chandef_compatible(c1, c2); 743 if (IS_ERR(ret)) 744 return NULL; 745 return ret; 746 } 747 EXPORT_SYMBOL(cfg80211_chandef_compatible); 748 749 void cfg80211_set_dfs_state(struct wiphy *wiphy, 750 const struct cfg80211_chan_def *chandef, 751 enum nl80211_dfs_state dfs_state) 752 { 753 struct ieee80211_channel *c; 754 int width; 755 756 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 757 return; 758 759 width = cfg80211_chandef_get_width(chandef); 760 if (width < 0) 761 return; 762 763 for_each_subchan(chandef, freq, cf) { 764 c = ieee80211_get_channel_khz(wiphy, freq); 765 if (!c || !(c->flags & IEEE80211_CHAN_RADAR)) 766 continue; 767 768 c->dfs_state = dfs_state; 769 c->dfs_state_entered = jiffies; 770 } 771 } 772 773 void cfg80211_set_cac_state(struct wiphy *wiphy, 774 const struct cfg80211_chan_def *chandef, 775 bool cac_ongoing) 776 { 777 struct ieee80211_channel *c; 778 int width; 779 u64 cac_time; 780 781 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 782 return; 783 784 width = cfg80211_chandef_get_width(chandef); 785 if (width < 0) 786 return; 787 788 /* Get the same timestamp for all subchannels */ 789 cac_time = cac_ongoing ? ktime_get_boottime_ns() : 0; 790 791 for_each_subchan(chandef, freq, cf) { 792 c = ieee80211_get_channel_khz(wiphy, freq); 793 if (!c) 794 continue; 795 796 c->cac_start_time = cac_time; 797 } 798 } 799 800 static bool 801 cfg80211_dfs_permissive_check_wdev(struct cfg80211_registered_device *rdev, 802 enum nl80211_iftype iftype, 803 struct wireless_dev *wdev, 804 struct ieee80211_channel *chan) 805 { 806 unsigned int link_id; 807 808 for_each_valid_link(wdev, link_id) { 809 struct ieee80211_channel *other_chan = NULL; 810 struct cfg80211_chan_def chandef = {}; 811 int ret; 812 813 /* In order to avoid daisy chaining only allow BSS STA */ 814 if (wdev->iftype != NL80211_IFTYPE_STATION || 815 !wdev->links[link_id].client.current_bss) 816 continue; 817 818 other_chan = 819 wdev->links[link_id].client.current_bss->pub.channel; 820 821 if (!other_chan) 822 continue; 823 824 if (chan == other_chan) 825 return true; 826 827 /* continue if we can't get the channel */ 828 ret = rdev_get_channel(rdev, wdev, link_id, &chandef); 829 if (ret) 830 continue; 831 832 if (cfg80211_is_sub_chan(&chandef, chan, false)) 833 return true; 834 } 835 836 return false; 837 } 838 839 /* 840 * Check if P2P GO is allowed to operate on a DFS channel 841 */ 842 static bool cfg80211_dfs_permissive_chan(struct wiphy *wiphy, 843 enum nl80211_iftype iftype, 844 struct ieee80211_channel *chan) 845 { 846 struct wireless_dev *wdev; 847 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 848 849 lockdep_assert_held(&rdev->wiphy.mtx); 850 851 if (!wiphy_ext_feature_isset(&rdev->wiphy, 852 NL80211_EXT_FEATURE_DFS_CONCURRENT) || 853 !(chan->flags & IEEE80211_CHAN_DFS_CONCURRENT)) 854 return false; 855 856 /* only valid for P2P GO */ 857 if (iftype != NL80211_IFTYPE_P2P_GO) 858 return false; 859 860 /* 861 * Allow only if there's a concurrent BSS 862 */ 863 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 864 bool ret = cfg80211_dfs_permissive_check_wdev(rdev, iftype, 865 wdev, chan); 866 if (ret) 867 return ret; 868 } 869 870 return false; 871 } 872 873 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy, 874 const struct cfg80211_chan_def *chandef, 875 enum nl80211_iftype iftype) 876 { 877 struct ieee80211_channel *c; 878 879 /* DFS is not required for S1G */ 880 if (cfg80211_chandef_is_s1g(chandef)) 881 return 0; 882 883 for_each_subchan(chandef, freq, cf) { 884 c = ieee80211_get_channel_khz(wiphy, freq); 885 if (!c) 886 return -EINVAL; 887 888 if (c->flags & IEEE80211_CHAN_RADAR && 889 !cfg80211_dfs_permissive_chan(wiphy, iftype, c)) 890 return 1; 891 } 892 893 return 0; 894 } 895 896 897 int cfg80211_chandef_dfs_required(struct wiphy *wiphy, 898 const struct cfg80211_chan_def *chandef, 899 enum nl80211_iftype iftype) 900 { 901 int width; 902 int ret; 903 904 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 905 return -EINVAL; 906 907 switch (iftype) { 908 case NL80211_IFTYPE_ADHOC: 909 case NL80211_IFTYPE_AP: 910 case NL80211_IFTYPE_P2P_GO: 911 case NL80211_IFTYPE_MESH_POINT: 912 case NL80211_IFTYPE_NAN: 913 width = cfg80211_chandef_get_width(chandef); 914 if (width < 0) 915 return -EINVAL; 916 917 ret = cfg80211_get_chans_dfs_required(wiphy, chandef, iftype); 918 919 return (ret > 0) ? BIT(chandef->width) : ret; 920 break; 921 case NL80211_IFTYPE_STATION: 922 case NL80211_IFTYPE_OCB: 923 case NL80211_IFTYPE_P2P_CLIENT: 924 case NL80211_IFTYPE_MONITOR: 925 case NL80211_IFTYPE_AP_VLAN: 926 case NL80211_IFTYPE_P2P_DEVICE: 927 case NL80211_IFTYPE_NAN_DATA: 928 case NL80211_IFTYPE_PD: 929 break; 930 case NL80211_IFTYPE_WDS: 931 case NL80211_IFTYPE_UNSPECIFIED: 932 case NUM_NL80211_IFTYPES: 933 WARN_ON(1); 934 } 935 936 return 0; 937 } 938 EXPORT_SYMBOL(cfg80211_chandef_dfs_required); 939 940 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy, 941 const struct cfg80211_chan_def *chandef) 942 { 943 struct ieee80211_channel *c; 944 int width, count = 0; 945 946 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 947 return false; 948 949 width = cfg80211_chandef_get_width(chandef); 950 if (width < 0) 951 return false; 952 953 /* 954 * Check entire range of channels for the bandwidth. 955 * Check all channels are DFS channels (DFS_USABLE or 956 * DFS_AVAILABLE). Return number of usable channels 957 * (require CAC). Allow DFS and non-DFS channel mix. 958 */ 959 for_each_subchan(chandef, freq, cf) { 960 c = ieee80211_get_channel_khz(wiphy, freq); 961 if (!c) 962 return false; 963 964 if (c->flags & IEEE80211_CHAN_DISABLED) 965 return false; 966 967 if (c->flags & IEEE80211_CHAN_RADAR) { 968 if (c->dfs_state == NL80211_DFS_UNAVAILABLE) 969 return false; 970 971 if (c->dfs_state == NL80211_DFS_USABLE) 972 count++; 973 } 974 } 975 976 return count > 0; 977 } 978 EXPORT_SYMBOL(cfg80211_chandef_dfs_usable); 979 980 /* 981 * Checks if center frequency of chan falls with in the bandwidth 982 * range of chandef. 983 */ 984 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef, 985 struct ieee80211_channel *chan, 986 bool primary_only) 987 { 988 int width; 989 u32 freq; 990 991 if (!chandef->chan) 992 return false; 993 994 if (chandef->chan->center_freq == chan->center_freq) 995 return true; 996 997 if (primary_only) 998 return false; 999 1000 width = cfg80211_chandef_get_width(chandef); 1001 if (width <= 20) 1002 return false; 1003 1004 for (freq = chandef->center_freq1 - width / 2 + 10; 1005 freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) { 1006 if (chan->center_freq == freq) 1007 return true; 1008 } 1009 1010 if (!chandef->center_freq2) 1011 return false; 1012 1013 for (freq = chandef->center_freq2 - width / 2 + 10; 1014 freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) { 1015 if (chan->center_freq == freq) 1016 return true; 1017 } 1018 1019 return false; 1020 } 1021 1022 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev) 1023 { 1024 unsigned int link; 1025 1026 lockdep_assert_wiphy(wdev->wiphy); 1027 1028 switch (wdev->iftype) { 1029 case NL80211_IFTYPE_AP: 1030 case NL80211_IFTYPE_P2P_GO: 1031 for_each_valid_link(wdev, link) { 1032 if (wdev->links[link].ap.beacon_interval) 1033 return true; 1034 } 1035 break; 1036 case NL80211_IFTYPE_ADHOC: 1037 if (wdev->u.ibss.ssid_len) 1038 return true; 1039 break; 1040 case NL80211_IFTYPE_MESH_POINT: 1041 if (wdev->u.mesh.id_len) 1042 return true; 1043 break; 1044 case NL80211_IFTYPE_STATION: 1045 case NL80211_IFTYPE_OCB: 1046 case NL80211_IFTYPE_P2P_CLIENT: 1047 case NL80211_IFTYPE_MONITOR: 1048 case NL80211_IFTYPE_AP_VLAN: 1049 case NL80211_IFTYPE_P2P_DEVICE: 1050 /* Can NAN type be considered as beaconing interface? */ 1051 case NL80211_IFTYPE_NAN: 1052 case NL80211_IFTYPE_NAN_DATA: 1053 case NL80211_IFTYPE_PD: 1054 break; 1055 case NL80211_IFTYPE_UNSPECIFIED: 1056 case NL80211_IFTYPE_WDS: 1057 case NUM_NL80211_IFTYPES: 1058 WARN_ON(1); 1059 } 1060 1061 return false; 1062 } 1063 1064 bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev, 1065 struct ieee80211_channel *chan, 1066 bool primary_only) 1067 { 1068 unsigned int link; 1069 1070 switch (wdev->iftype) { 1071 case NL80211_IFTYPE_AP: 1072 case NL80211_IFTYPE_P2P_GO: 1073 for_each_valid_link(wdev, link) { 1074 if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef, 1075 chan, primary_only)) 1076 return true; 1077 } 1078 break; 1079 case NL80211_IFTYPE_ADHOC: 1080 return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan, 1081 primary_only); 1082 case NL80211_IFTYPE_MESH_POINT: 1083 return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan, 1084 primary_only); 1085 default: 1086 break; 1087 } 1088 1089 return false; 1090 } 1091 1092 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy, 1093 struct ieee80211_channel *chan) 1094 { 1095 struct wireless_dev *wdev; 1096 1097 lockdep_assert_wiphy(wiphy); 1098 1099 list_for_each_entry(wdev, &wiphy->wdev_list, list) { 1100 if (!cfg80211_beaconing_iface_active(wdev)) 1101 continue; 1102 1103 if (cfg80211_wdev_on_sub_chan(wdev, chan, false)) 1104 return true; 1105 } 1106 1107 return false; 1108 } 1109 1110 static bool 1111 cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev, 1112 struct ieee80211_channel *channel) 1113 { 1114 if (!rdev->background_radar_wdev) 1115 return false; 1116 1117 if (!cfg80211_chandef_valid(&rdev->background_radar_chandef)) 1118 return false; 1119 1120 return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel, 1121 false); 1122 } 1123 1124 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy, 1125 struct ieee80211_channel *chan) 1126 { 1127 struct cfg80211_registered_device *rdev; 1128 1129 ASSERT_RTNL(); 1130 1131 if (!(chan->flags & IEEE80211_CHAN_RADAR)) 1132 return false; 1133 1134 for_each_rdev(rdev) { 1135 bool found; 1136 1137 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy)) 1138 continue; 1139 1140 guard(wiphy)(&rdev->wiphy); 1141 1142 found = cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan) || 1143 cfg80211_offchan_chain_is_active(rdev, chan); 1144 1145 if (found) 1146 return true; 1147 } 1148 1149 return false; 1150 } 1151 1152 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy, 1153 const struct cfg80211_chan_def *chandef) 1154 { 1155 struct ieee80211_channel *c; 1156 int width; 1157 bool dfs_offload; 1158 1159 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 1160 return false; 1161 1162 width = cfg80211_chandef_get_width(chandef); 1163 if (width < 0) 1164 return false; 1165 1166 dfs_offload = wiphy_ext_feature_isset(wiphy, 1167 NL80211_EXT_FEATURE_DFS_OFFLOAD); 1168 1169 /* 1170 * Check entire range of channels for the bandwidth. 1171 * If any channel in between is disabled or has not 1172 * had gone through CAC return false 1173 */ 1174 for_each_subchan(chandef, freq, cf) { 1175 c = ieee80211_get_channel_khz(wiphy, freq); 1176 if (!c) 1177 return false; 1178 1179 if (c->flags & IEEE80211_CHAN_DISABLED) 1180 return false; 1181 1182 if ((c->flags & IEEE80211_CHAN_RADAR) && 1183 (c->dfs_state != NL80211_DFS_AVAILABLE) && 1184 !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload)) 1185 return false; 1186 } 1187 1188 return true; 1189 } 1190 1191 unsigned int 1192 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy, 1193 const struct cfg80211_chan_def *chandef) 1194 { 1195 struct ieee80211_channel *c; 1196 int width; 1197 unsigned int t1 = 0, t2 = 0; 1198 1199 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 1200 return 0; 1201 1202 width = cfg80211_chandef_get_width(chandef); 1203 if (width < 0) 1204 return 0; 1205 1206 for_each_subchan(chandef, freq, cf) { 1207 c = ieee80211_get_channel_khz(wiphy, freq); 1208 if (!c || (c->flags & IEEE80211_CHAN_DISABLED)) { 1209 if (cf == 1) 1210 t1 = INT_MAX; 1211 else 1212 t2 = INT_MAX; 1213 continue; 1214 } 1215 1216 if (!(c->flags & IEEE80211_CHAN_RADAR)) 1217 continue; 1218 1219 if (cf == 1 && c->dfs_cac_ms > t1) 1220 t1 = c->dfs_cac_ms; 1221 1222 if (cf == 2 && c->dfs_cac_ms > t2) 1223 t2 = c->dfs_cac_ms; 1224 } 1225 1226 if (t1 == INT_MAX && t2 == INT_MAX) 1227 return 0; 1228 1229 if (t1 == INT_MAX) 1230 return t2; 1231 1232 if (t2 == INT_MAX) 1233 return t1; 1234 1235 return max(t1, t2); 1236 } 1237 EXPORT_SYMBOL(cfg80211_chandef_dfs_cac_time); 1238 1239 /* check if the operating channels are valid and supported */ 1240 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels, 1241 enum ieee80211_edmg_bw_config edmg_bw_config, 1242 int primary_channel, 1243 struct ieee80211_edmg *edmg_cap) 1244 { 1245 struct ieee80211_channel *chan; 1246 int i, freq; 1247 int channels_counter = 0; 1248 1249 if (!edmg_channels && !edmg_bw_config) 1250 return true; 1251 1252 if ((!edmg_channels && edmg_bw_config) || 1253 (edmg_channels && !edmg_bw_config)) 1254 return false; 1255 1256 if (!(edmg_channels & BIT(primary_channel - 1))) 1257 return false; 1258 1259 /* 60GHz channels 1..6 */ 1260 for (i = 0; i < 6; i++) { 1261 if (!(edmg_channels & BIT(i))) 1262 continue; 1263 1264 if (!(edmg_cap->channels & BIT(i))) 1265 return false; 1266 1267 channels_counter++; 1268 1269 freq = ieee80211_channel_to_frequency(i + 1, 1270 NL80211_BAND_60GHZ); 1271 chan = ieee80211_get_channel(wiphy, freq); 1272 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) 1273 return false; 1274 } 1275 1276 /* IEEE802.11 allows max 4 channels */ 1277 if (channels_counter > 4) 1278 return false; 1279 1280 /* check bw_config is a subset of what driver supports 1281 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13) 1282 */ 1283 if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4)) 1284 return false; 1285 1286 if (edmg_bw_config > edmg_cap->bw_config) 1287 return false; 1288 1289 return true; 1290 } 1291 1292 static bool cfg80211_s1g_usable(struct wiphy *wiphy, 1293 const struct cfg80211_chan_def *chandef) 1294 { 1295 u32 freq_khz; 1296 const struct ieee80211_channel *chan; 1297 u32 pri_khz = ieee80211_channel_to_khz(chandef->chan); 1298 u32 end_khz = cfg80211_s1g_get_end_freq_khz(chandef); 1299 u32 start_khz = cfg80211_s1g_get_start_freq_khz(chandef); 1300 int width_mhz = cfg80211_chandef_get_width(chandef); 1301 u32 prohibited_flags = IEEE80211_CHAN_DISABLED; 1302 1303 if (width_mhz >= 16) 1304 prohibited_flags |= IEEE80211_CHAN_NO_16MHZ; 1305 if (width_mhz >= 8) 1306 prohibited_flags |= IEEE80211_CHAN_NO_8MHZ; 1307 if (width_mhz >= 4) 1308 prohibited_flags |= IEEE80211_CHAN_NO_4MHZ; 1309 1310 if (chandef->chan->flags & IEEE80211_CHAN_S1G_NO_PRIMARY) 1311 return false; 1312 1313 if (pri_khz < start_khz || pri_khz > end_khz) 1314 return false; 1315 1316 for_each_s1g_subchan(chandef, freq_khz) { 1317 chan = ieee80211_get_channel_khz(wiphy, freq_khz); 1318 if (!chan || (chan->flags & prohibited_flags)) 1319 return false; 1320 } 1321 1322 if (chandef->s1g_primary_2mhz) { 1323 u32 sib_khz; 1324 const struct ieee80211_channel *sibling; 1325 1326 sibling = cfg80211_s1g_get_primary_sibling(wiphy, chandef); 1327 if (!sibling) 1328 return false; 1329 1330 if (sibling->flags & IEEE80211_CHAN_S1G_NO_PRIMARY) 1331 return false; 1332 1333 sib_khz = ieee80211_channel_to_khz(sibling); 1334 if (sib_khz < start_khz || sib_khz > end_khz) 1335 return false; 1336 } 1337 1338 return true; 1339 } 1340 1341 bool _cfg80211_chandef_usable(struct wiphy *wiphy, 1342 const struct cfg80211_chan_def *chandef, 1343 u32 prohibited_flags, 1344 u32 permitting_flags) 1345 { 1346 struct ieee80211_sta_ht_cap *ht_cap; 1347 struct ieee80211_sta_vht_cap *vht_cap; 1348 struct ieee80211_edmg *edmg_cap; 1349 u32 width, control_freq, cap; 1350 bool ext_nss_cap, support_80_80 = false, support_320 = false; 1351 const struct ieee80211_sband_iftype_data *iftd; 1352 struct ieee80211_supported_band *sband; 1353 struct ieee80211_channel *c; 1354 int i; 1355 1356 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 1357 return false; 1358 1359 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap; 1360 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap; 1361 edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap; 1362 ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) & 1363 IEEE80211_VHT_EXT_NSS_BW_CAPABLE; 1364 1365 if (cfg80211_chandef_is_s1g(chandef)) 1366 return cfg80211_s1g_usable(wiphy, chandef); 1367 1368 if (edmg_cap->channels && 1369 !cfg80211_edmg_usable(wiphy, 1370 chandef->edmg.channels, 1371 chandef->edmg.bw_config, 1372 chandef->chan->hw_value, 1373 edmg_cap)) 1374 return false; 1375 1376 control_freq = chandef->chan->center_freq; 1377 1378 switch (chandef->width) { 1379 case NL80211_CHAN_WIDTH_5: 1380 width = 5; 1381 break; 1382 case NL80211_CHAN_WIDTH_10: 1383 prohibited_flags |= IEEE80211_CHAN_NO_10MHZ; 1384 width = 10; 1385 break; 1386 case NL80211_CHAN_WIDTH_20: 1387 if (!ht_cap->ht_supported && 1388 chandef->chan->band != NL80211_BAND_6GHZ) 1389 return false; 1390 fallthrough; 1391 case NL80211_CHAN_WIDTH_20_NOHT: 1392 prohibited_flags |= IEEE80211_CHAN_NO_20MHZ; 1393 width = 20; 1394 break; 1395 case NL80211_CHAN_WIDTH_40: 1396 width = 40; 1397 if (chandef->chan->band == NL80211_BAND_6GHZ) 1398 break; 1399 if (!ht_cap->ht_supported) 1400 return false; 1401 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) || 1402 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT) 1403 return false; 1404 if (chandef->center_freq1 < control_freq && 1405 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS) 1406 return false; 1407 if (chandef->center_freq1 > control_freq && 1408 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS) 1409 return false; 1410 break; 1411 case NL80211_CHAN_WIDTH_80P80: 1412 cap = vht_cap->cap; 1413 support_80_80 = 1414 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) || 1415 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ && 1416 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) || 1417 (ext_nss_cap && 1418 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1); 1419 if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80) 1420 return false; 1421 fallthrough; 1422 case NL80211_CHAN_WIDTH_80: 1423 prohibited_flags |= IEEE80211_CHAN_NO_80MHZ; 1424 width = 80; 1425 if (chandef->chan->band == NL80211_BAND_6GHZ) 1426 break; 1427 if (!vht_cap->vht_supported) 1428 return false; 1429 break; 1430 case NL80211_CHAN_WIDTH_160: 1431 prohibited_flags |= IEEE80211_CHAN_NO_160MHZ; 1432 width = 160; 1433 if (chandef->chan->band == NL80211_BAND_6GHZ) 1434 break; 1435 if (!vht_cap->vht_supported) 1436 return false; 1437 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK; 1438 if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ && 1439 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ && 1440 !(ext_nss_cap && 1441 (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))) 1442 return false; 1443 break; 1444 case NL80211_CHAN_WIDTH_320: 1445 prohibited_flags |= IEEE80211_CHAN_NO_320MHZ; 1446 width = 320; 1447 1448 if (chandef->chan->band != NL80211_BAND_6GHZ) 1449 return false; 1450 1451 sband = wiphy->bands[NL80211_BAND_6GHZ]; 1452 if (!sband) 1453 return false; 1454 1455 for_each_sband_iftype_data(sband, i, iftd) { 1456 if (!iftd->eht_cap.has_eht) 1457 continue; 1458 1459 if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] & 1460 IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) { 1461 support_320 = true; 1462 break; 1463 } 1464 } 1465 1466 if (!support_320) 1467 return false; 1468 break; 1469 default: 1470 WARN_ON_ONCE(1); 1471 return false; 1472 } 1473 1474 /* 1475 * TODO: What if there are only certain 80/160/80+80 MHz channels 1476 * allowed by the driver, or only certain combinations? 1477 * For 40 MHz the driver can set the NO_HT40 flags, but for 1478 * 80/160 MHz and in particular 80+80 MHz this isn't really 1479 * feasible and we only have NO_80MHZ/NO_160MHZ so far but 1480 * no way to cover 80+80 MHz or more complex restrictions. 1481 * Note that such restrictions also need to be advertised to 1482 * userspace, for example for P2P channel selection. 1483 */ 1484 1485 if (width > 20) 1486 prohibited_flags |= IEEE80211_CHAN_NO_OFDM; 1487 1488 /* 5 and 10 MHz are only defined for the OFDM PHY */ 1489 if (width < 20) 1490 prohibited_flags |= IEEE80211_CHAN_NO_OFDM; 1491 1492 for_each_subchan(chandef, freq, cf) { 1493 c = ieee80211_get_channel_khz(wiphy, freq); 1494 if (!c) 1495 return false; 1496 if (c->flags & permitting_flags) 1497 continue; 1498 if (c->flags & prohibited_flags) 1499 return false; 1500 } 1501 1502 return true; 1503 } 1504 1505 bool cfg80211_chandef_usable(struct wiphy *wiphy, 1506 const struct cfg80211_chan_def *chandef, 1507 u32 prohibited_flags) 1508 { 1509 return _cfg80211_chandef_usable(wiphy, chandef, prohibited_flags, 0); 1510 } 1511 EXPORT_SYMBOL(cfg80211_chandef_usable); 1512 1513 static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype, 1514 struct wireless_dev *wdev, 1515 struct ieee80211_channel *chan) 1516 { 1517 struct ieee80211_channel *other_chan = NULL; 1518 unsigned int link_id; 1519 int r1, r2; 1520 1521 for_each_valid_link(wdev, link_id) { 1522 if (wdev->iftype == NL80211_IFTYPE_STATION && 1523 wdev->links[link_id].client.current_bss) 1524 other_chan = wdev->links[link_id].client.current_bss->pub.channel; 1525 1526 /* 1527 * If a GO already operates on the same GO_CONCURRENT channel, 1528 * this one (maybe the same one) can beacon as well. We allow 1529 * the operation even if the station we relied on with 1530 * GO_CONCURRENT is disconnected now. But then we must make sure 1531 * we're not outdoor on an indoor-only channel. 1532 */ 1533 if (iftype == NL80211_IFTYPE_P2P_GO && 1534 wdev->iftype == NL80211_IFTYPE_P2P_GO && 1535 wdev->links[link_id].ap.beacon_interval && 1536 !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY)) 1537 other_chan = wdev->links[link_id].ap.chandef.chan; 1538 1539 if (!other_chan) 1540 continue; 1541 1542 if (chan == other_chan) 1543 return true; 1544 1545 if (chan->band != NL80211_BAND_5GHZ && 1546 chan->band != NL80211_BAND_6GHZ) 1547 continue; 1548 1549 r1 = cfg80211_get_unii(chan->center_freq); 1550 r2 = cfg80211_get_unii(other_chan->center_freq); 1551 1552 if (r1 != -EINVAL && r1 == r2) { 1553 /* 1554 * At some locations channels 149-165 are considered a 1555 * bundle, but at other locations, e.g., Indonesia, 1556 * channels 149-161 are considered a bundle while 1557 * channel 165 is left out and considered to be in a 1558 * different bundle. Thus, in case that there is a 1559 * station interface connected to an AP on channel 165, 1560 * it is assumed that channels 149-161 are allowed for 1561 * GO operations. However, having a station interface 1562 * connected to an AP on channels 149-161, does not 1563 * allow GO operation on channel 165. 1564 */ 1565 if (chan->center_freq == 5825 && 1566 other_chan->center_freq != 5825) 1567 continue; 1568 return true; 1569 } 1570 } 1571 1572 return false; 1573 } 1574 1575 /* 1576 * Check if the channel can be used under permissive conditions mandated by 1577 * some regulatory bodies, i.e., the channel is marked with 1578 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface 1579 * associated to an AP on the same channel or on the same UNII band 1580 * (assuming that the AP is an authorized master). 1581 * In addition allow operation on a channel on which indoor operation is 1582 * allowed, iff we are currently operating in an indoor environment. 1583 */ 1584 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy, 1585 enum nl80211_iftype iftype, 1586 struct ieee80211_channel *chan) 1587 { 1588 struct wireless_dev *wdev; 1589 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1590 1591 lockdep_assert_held(&rdev->wiphy.mtx); 1592 1593 if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) || 1594 !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR)) 1595 return false; 1596 1597 /* only valid for GO and TDLS off-channel (station/p2p-CL) */ 1598 if (iftype != NL80211_IFTYPE_P2P_GO && 1599 iftype != NL80211_IFTYPE_STATION && 1600 iftype != NL80211_IFTYPE_P2P_CLIENT) 1601 return false; 1602 1603 if (regulatory_indoor_allowed() && 1604 (chan->flags & IEEE80211_CHAN_INDOOR_ONLY)) 1605 return true; 1606 1607 if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT)) 1608 return false; 1609 1610 /* 1611 * Generally, it is possible to rely on another device/driver to allow 1612 * the IR concurrent relaxation, however, since the device can further 1613 * enforce the relaxation (by doing a similar verifications as this), 1614 * and thus fail the GO instantiation, consider only the interfaces of 1615 * the current registered device. 1616 */ 1617 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 1618 bool ret; 1619 1620 ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan); 1621 if (ret) 1622 return ret; 1623 } 1624 1625 return false; 1626 } 1627 1628 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy, 1629 struct cfg80211_chan_def *chandef, 1630 enum nl80211_iftype iftype, 1631 u32 prohibited_flags, 1632 u32 permitting_flags) 1633 { 1634 bool res, check_radar; 1635 int dfs_required; 1636 1637 trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, 1638 prohibited_flags, 1639 permitting_flags); 1640 1641 if (!_cfg80211_chandef_usable(wiphy, chandef, 1642 IEEE80211_CHAN_DISABLED, 0)) 1643 return false; 1644 1645 dfs_required = cfg80211_chandef_dfs_required(wiphy, chandef, iftype); 1646 check_radar = dfs_required != 0; 1647 1648 if (dfs_required > 0 && 1649 cfg80211_chandef_dfs_available(wiphy, chandef)) { 1650 /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */ 1651 prohibited_flags &= ~IEEE80211_CHAN_NO_IR; 1652 check_radar = false; 1653 } 1654 1655 if (check_radar && 1656 !_cfg80211_chandef_usable(wiphy, chandef, 1657 IEEE80211_CHAN_RADAR, 0)) 1658 return false; 1659 1660 res = _cfg80211_chandef_usable(wiphy, chandef, 1661 prohibited_flags, 1662 permitting_flags); 1663 1664 trace_cfg80211_return_bool(res); 1665 return res; 1666 } 1667 1668 bool cfg80211_reg_check_beaconing(struct wiphy *wiphy, 1669 struct cfg80211_chan_def *chandef, 1670 struct cfg80211_beaconing_check_config *cfg) 1671 { 1672 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 1673 u32 permitting_flags = 0; 1674 bool check_no_ir = true; 1675 1676 /* 1677 * Under certain conditions suggested by some regulatory bodies a 1678 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag 1679 * only if such relaxations are not enabled and the conditions are not 1680 * met. 1681 */ 1682 if (cfg->relax) { 1683 lockdep_assert_held(&rdev->wiphy.mtx); 1684 check_no_ir = !cfg80211_ir_permissive_chan(wiphy, cfg->iftype, 1685 chandef->chan); 1686 } 1687 1688 if (cfg->reg_power == IEEE80211_REG_VLP_AP) 1689 permitting_flags |= IEEE80211_CHAN_ALLOW_6GHZ_VLP_AP; 1690 1691 if ((cfg->iftype == NL80211_IFTYPE_P2P_GO || 1692 cfg->iftype == NL80211_IFTYPE_AP) && 1693 (chandef->width == NL80211_CHAN_WIDTH_20_NOHT || 1694 chandef->width == NL80211_CHAN_WIDTH_20)) 1695 permitting_flags |= IEEE80211_CHAN_ALLOW_20MHZ_ACTIVITY; 1696 1697 return _cfg80211_reg_can_beacon(wiphy, chandef, cfg->iftype, 1698 check_no_ir ? IEEE80211_CHAN_NO_IR : 0, 1699 permitting_flags); 1700 } 1701 EXPORT_SYMBOL(cfg80211_reg_check_beaconing); 1702 1703 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev, 1704 struct net_device *dev, 1705 struct cfg80211_chan_def *chandef) 1706 { 1707 if (!rdev->ops->set_monitor_channel) 1708 return -EOPNOTSUPP; 1709 if (!cfg80211_has_monitors_only(rdev)) 1710 return -EBUSY; 1711 1712 return rdev_set_monitor_channel(rdev, dev, chandef); 1713 } 1714 1715 bool cfg80211_any_usable_channels(struct wiphy *wiphy, 1716 unsigned long sband_mask, 1717 u32 prohibited_flags) 1718 { 1719 int idx; 1720 1721 prohibited_flags |= IEEE80211_CHAN_DISABLED; 1722 1723 for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) { 1724 struct ieee80211_supported_band *sband = wiphy->bands[idx]; 1725 int chanidx; 1726 1727 if (!sband) 1728 continue; 1729 1730 for (chanidx = 0; chanidx < sband->n_channels; chanidx++) { 1731 struct ieee80211_channel *chan; 1732 1733 chan = &sband->channels[chanidx]; 1734 1735 if (chan->flags & prohibited_flags) 1736 continue; 1737 1738 return true; 1739 } 1740 } 1741 1742 return false; 1743 } 1744 EXPORT_SYMBOL(cfg80211_any_usable_channels); 1745 1746 struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev, 1747 unsigned int link_id) 1748 { 1749 lockdep_assert_wiphy(wdev->wiphy); 1750 1751 WARN_ON(wdev->valid_links && !(wdev->valid_links & BIT(link_id))); 1752 WARN_ON(!wdev->valid_links && link_id > 0); 1753 1754 switch (wdev->iftype) { 1755 case NL80211_IFTYPE_MESH_POINT: 1756 return &wdev->u.mesh.chandef; 1757 case NL80211_IFTYPE_ADHOC: 1758 return &wdev->u.ibss.chandef; 1759 case NL80211_IFTYPE_OCB: 1760 return &wdev->u.ocb.chandef; 1761 case NL80211_IFTYPE_AP: 1762 case NL80211_IFTYPE_P2P_GO: 1763 return &wdev->links[link_id].ap.chandef; 1764 default: 1765 return NULL; 1766 } 1767 } 1768 EXPORT_SYMBOL(wdev_chandef); 1769