1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright 2002-2005, Instant802 Networks, Inc. 4 * Copyright 2005-2006, Devicescape Software, Inc. 5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net> 6 * Copyright 2008-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com> 7 * Copyright 2013-2014 Intel Mobile Communications GmbH 8 * Copyright 2017 Intel Deutschland GmbH 9 * Copyright (C) 2018 - 2026 Intel Corporation 10 */ 11 12 13 /** 14 * DOC: Wireless regulatory infrastructure 15 * 16 * The usual implementation is for a driver to read a device EEPROM to 17 * determine which regulatory domain it should be operating under, then 18 * looking up the allowable channels in a driver-local table and finally 19 * registering those channels in the wiphy structure. 20 * 21 * Another set of compliance enforcement is for drivers to use their 22 * own compliance limits which can be stored on the EEPROM. The host 23 * driver or firmware may ensure these are used. 24 * 25 * In addition to all this we provide an extra layer of regulatory 26 * conformance. For drivers which do not have any regulatory 27 * information CRDA provides the complete regulatory solution. 28 * For others it provides a community effort on further restrictions 29 * to enhance compliance. 30 * 31 * Note: When number of rules --> infinity we will not be able to 32 * index on alpha2 any more, instead we'll probably have to 33 * rely on some SHA1 checksum of the regdomain for example. 34 * 35 */ 36 37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 38 39 #include <linux/kernel.h> 40 #include <linux/export.h> 41 #include <linux/slab.h> 42 #include <linux/list.h> 43 #include <linux/ctype.h> 44 #include <linux/nl80211.h> 45 #include <linux/device/faux.h> 46 #include <linux/verification.h> 47 #include <linux/moduleparam.h> 48 #include <linux/firmware.h> 49 #include <linux/units.h> 50 51 #include <net/cfg80211.h> 52 #include "core.h" 53 #include "reg.h" 54 #include "rdev-ops.h" 55 #include "nl80211.h" 56 57 /* 58 * Grace period we give before making sure all current interfaces reside on 59 * channels allowed by the current regulatory domain. 60 */ 61 #define REG_ENFORCE_GRACE_MS 60000 62 63 /** 64 * enum reg_request_treatment - regulatory request treatment 65 * 66 * @REG_REQ_OK: continue processing the regulatory request 67 * @REG_REQ_IGNORE: ignore the regulatory request 68 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should 69 * be intersected with the current one. 70 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current 71 * regulatory settings, and no further processing is required. 72 */ 73 enum reg_request_treatment { 74 REG_REQ_OK, 75 REG_REQ_IGNORE, 76 REG_REQ_INTERSECT, 77 REG_REQ_ALREADY_SET, 78 }; 79 80 static struct regulatory_request core_request_world = { 81 .initiator = NL80211_REGDOM_SET_BY_CORE, 82 .alpha2[0] = '0', 83 .alpha2[1] = '0', 84 .intersect = false, 85 .processed = true, 86 .country_ie_env = ENVIRON_ANY, 87 }; 88 89 /* 90 * Receipt of information from last regulatory request, 91 * protected by RTNL (and can be accessed with RCU protection) 92 */ 93 static struct regulatory_request __rcu *last_request = 94 (void __force __rcu *)&core_request_world; 95 96 /* To trigger userspace events and load firmware */ 97 static struct faux_device *reg_fdev; 98 99 /* 100 * Central wireless core regulatory domains, we only need two, 101 * the current one and a world regulatory domain in case we have no 102 * information to give us an alpha2. 103 * (protected by RTNL, can be read under RCU) 104 */ 105 const struct ieee80211_regdomain __rcu *cfg80211_regdomain; 106 107 /* 108 * Number of devices that registered to the core 109 * that support cellular base station regulatory hints 110 * (protected by RTNL) 111 */ 112 static int reg_num_devs_support_basehint; 113 114 /* 115 * State variable indicating if the platform on which the devices 116 * are attached is operating in an indoor environment. The state variable 117 * is relevant for all registered devices. 118 */ 119 static bool reg_is_indoor; 120 static DEFINE_SPINLOCK(reg_indoor_lock); 121 122 /* Used to track the userspace process controlling the indoor setting */ 123 static u32 reg_is_indoor_portid; 124 125 static void restore_regulatory_settings(bool reset_user, bool cached); 126 static void print_regdomain(const struct ieee80211_regdomain *rd); 127 static void reg_process_hint(struct regulatory_request *reg_request); 128 129 static const struct ieee80211_regdomain *get_cfg80211_regdom(void) 130 { 131 return rcu_dereference_rtnl(cfg80211_regdomain); 132 } 133 134 /* 135 * Returns the regulatory domain associated with the wiphy. 136 * 137 * Requires any of RTNL, wiphy mutex or RCU protection. 138 */ 139 const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy) 140 { 141 return rcu_dereference_check(wiphy->regd, 142 lockdep_is_held(&wiphy->mtx) || 143 lockdep_rtnl_is_held()); 144 } 145 EXPORT_SYMBOL(get_wiphy_regdom); 146 147 static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region) 148 { 149 switch (dfs_region) { 150 case NL80211_DFS_UNSET: 151 return "unset"; 152 case NL80211_DFS_FCC: 153 return "FCC"; 154 case NL80211_DFS_ETSI: 155 return "ETSI"; 156 case NL80211_DFS_JP: 157 return "JP"; 158 } 159 return "Unknown"; 160 } 161 162 enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy) 163 { 164 const struct ieee80211_regdomain *regd = NULL; 165 const struct ieee80211_regdomain *wiphy_regd = NULL; 166 enum nl80211_dfs_regions dfs_region; 167 168 rcu_read_lock(); 169 regd = get_cfg80211_regdom(); 170 dfs_region = regd->dfs_region; 171 172 if (!wiphy) 173 goto out; 174 175 wiphy_regd = get_wiphy_regdom(wiphy); 176 if (!wiphy_regd) 177 goto out; 178 179 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) { 180 dfs_region = wiphy_regd->dfs_region; 181 goto out; 182 } 183 184 if (wiphy_regd->dfs_region == regd->dfs_region) 185 goto out; 186 187 pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n", 188 dev_name(&wiphy->dev), 189 reg_dfs_region_str(wiphy_regd->dfs_region), 190 reg_dfs_region_str(regd->dfs_region)); 191 192 out: 193 rcu_read_unlock(); 194 195 return dfs_region; 196 } 197 198 static void rcu_free_regdom(const struct ieee80211_regdomain *r) 199 { 200 if (!r) 201 return; 202 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head); 203 } 204 205 static struct regulatory_request *get_last_request(void) 206 { 207 return rcu_dereference_rtnl(last_request); 208 } 209 210 /* Used to queue up regulatory hints */ 211 static LIST_HEAD(reg_requests_list); 212 static DEFINE_SPINLOCK(reg_requests_lock); 213 214 /* Used to queue up beacon hints for review */ 215 static LIST_HEAD(reg_pending_beacons); 216 static DEFINE_SPINLOCK(reg_pending_beacons_lock); 217 218 /* Used to keep track of processed beacon hints */ 219 static LIST_HEAD(reg_beacon_list); 220 221 struct reg_beacon { 222 struct list_head list; 223 struct ieee80211_channel chan; 224 }; 225 226 static void reg_check_chans_work(struct work_struct *work); 227 static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work); 228 229 static void reg_todo(struct work_struct *work); 230 static DECLARE_WORK(reg_work, reg_todo); 231 232 /* We keep a static world regulatory domain in case of the absence of CRDA */ 233 static const struct ieee80211_regdomain world_regdom = { 234 .n_reg_rules = 8, 235 .alpha2 = "00", 236 .reg_rules = { 237 /* IEEE 802.11b/g, channels 1..11 */ 238 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0), 239 /* IEEE 802.11b/g, channels 12..13. */ 240 REG_RULE(2467-10, 2472+10, 20, 6, 20, 241 NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW), 242 /* IEEE 802.11 channel 14 - Only JP enables 243 * this and for 802.11b only */ 244 REG_RULE(2484-10, 2484+10, 20, 6, 20, 245 NL80211_RRF_NO_IR | 246 NL80211_RRF_NO_OFDM), 247 /* IEEE 802.11a, channel 36..48 */ 248 REG_RULE(5180-10, 5240+10, 80, 6, 20, 249 NL80211_RRF_NO_IR | 250 NL80211_RRF_AUTO_BW), 251 252 /* IEEE 802.11a, channel 52..64 - DFS required */ 253 REG_RULE(5260-10, 5320+10, 80, 6, 20, 254 NL80211_RRF_NO_IR | 255 NL80211_RRF_AUTO_BW | 256 NL80211_RRF_DFS), 257 258 /* IEEE 802.11a, channel 100..144 - DFS required */ 259 REG_RULE(5500-10, 5720+10, 160, 6, 20, 260 NL80211_RRF_NO_IR | 261 NL80211_RRF_DFS), 262 263 /* IEEE 802.11a, channel 149..165 */ 264 REG_RULE(5745-10, 5825+10, 80, 6, 20, 265 NL80211_RRF_NO_IR), 266 267 /* IEEE 802.11ad (60GHz), channels 1..3 */ 268 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0), 269 } 270 }; 271 272 /* protected by RTNL */ 273 static const struct ieee80211_regdomain *cfg80211_world_regdom = 274 &world_regdom; 275 276 static char *ieee80211_regdom = "00"; 277 static char user_alpha2[2]; 278 static const struct ieee80211_regdomain *cfg80211_user_regdom; 279 280 module_param(ieee80211_regdom, charp, 0444); 281 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); 282 283 static void reg_free_request(struct regulatory_request *request) 284 { 285 if (request == &core_request_world) 286 return; 287 288 if (request != get_last_request()) 289 kfree(request); 290 } 291 292 static void reg_free_last_request(void) 293 { 294 struct regulatory_request *lr = get_last_request(); 295 296 if (lr != &core_request_world && lr) 297 kfree_rcu(lr, rcu_head); 298 } 299 300 static void reg_update_last_request(struct regulatory_request *request) 301 { 302 struct regulatory_request *lr; 303 304 lr = get_last_request(); 305 if (lr == request) 306 return; 307 308 reg_free_last_request(); 309 rcu_assign_pointer(last_request, request); 310 } 311 312 static void reset_regdomains(bool full_reset, 313 const struct ieee80211_regdomain *new_regdom) 314 { 315 const struct ieee80211_regdomain *r; 316 317 ASSERT_RTNL(); 318 319 r = get_cfg80211_regdom(); 320 321 /* avoid freeing static information or freeing something twice */ 322 if (r == cfg80211_world_regdom) 323 r = NULL; 324 if (cfg80211_world_regdom == &world_regdom) 325 cfg80211_world_regdom = NULL; 326 if (r == &world_regdom) 327 r = NULL; 328 329 rcu_free_regdom(r); 330 rcu_free_regdom(cfg80211_world_regdom); 331 332 cfg80211_world_regdom = &world_regdom; 333 rcu_assign_pointer(cfg80211_regdomain, new_regdom); 334 335 if (!full_reset) 336 return; 337 338 reg_update_last_request(&core_request_world); 339 } 340 341 /* 342 * Dynamic world regulatory domain requested by the wireless 343 * core upon initialization 344 */ 345 static void update_world_regdomain(const struct ieee80211_regdomain *rd) 346 { 347 struct regulatory_request *lr; 348 349 lr = get_last_request(); 350 351 WARN_ON(!lr); 352 353 reset_regdomains(false, rd); 354 355 cfg80211_world_regdom = rd; 356 } 357 358 bool is_world_regdom(const char *alpha2) 359 { 360 if (!alpha2) 361 return false; 362 return alpha2[0] == '0' && alpha2[1] == '0'; 363 } 364 365 static bool is_alpha2_set(const char *alpha2) 366 { 367 if (!alpha2) 368 return false; 369 return alpha2[0] && alpha2[1]; 370 } 371 372 static bool is_unknown_alpha2(const char *alpha2) 373 { 374 if (!alpha2) 375 return false; 376 /* 377 * Special case where regulatory domain was built by driver 378 * but a specific alpha2 cannot be determined 379 */ 380 return alpha2[0] == '9' && alpha2[1] == '9'; 381 } 382 383 static bool is_intersected_alpha2(const char *alpha2) 384 { 385 if (!alpha2) 386 return false; 387 /* 388 * Special case where regulatory domain is the 389 * result of an intersection between two regulatory domain 390 * structures 391 */ 392 return alpha2[0] == '9' && alpha2[1] == '8'; 393 } 394 395 static bool is_an_alpha2(const char *alpha2) 396 { 397 if (!alpha2) 398 return false; 399 return isascii(alpha2[0]) && isalpha(alpha2[0]) && 400 isascii(alpha2[1]) && isalpha(alpha2[1]); 401 } 402 403 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y) 404 { 405 if (!alpha2_x || !alpha2_y) 406 return false; 407 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1]; 408 } 409 410 static bool regdom_changes(const char *alpha2) 411 { 412 const struct ieee80211_regdomain *r = get_cfg80211_regdom(); 413 414 if (!r) 415 return true; 416 return !alpha2_equal(r->alpha2, alpha2); 417 } 418 419 /* 420 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets 421 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER 422 * has ever been issued. 423 */ 424 static bool is_user_regdom_saved(void) 425 { 426 if (user_alpha2[0] == '9' && user_alpha2[1] == '7') 427 return false; 428 429 /* This would indicate a mistake on the design */ 430 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2), 431 "Unexpected user alpha2: %c%c\n", 432 user_alpha2[0], user_alpha2[1])) 433 return false; 434 435 return true; 436 } 437 438 static const struct ieee80211_regdomain * 439 reg_copy_regd(const struct ieee80211_regdomain *src_regd) 440 { 441 struct ieee80211_regdomain *regd; 442 unsigned int i; 443 444 regd = kzalloc_flex(*regd, reg_rules, src_regd->n_reg_rules); 445 if (!regd) 446 return ERR_PTR(-ENOMEM); 447 448 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain)); 449 450 for (i = 0; i < src_regd->n_reg_rules; i++) 451 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i], 452 sizeof(struct ieee80211_reg_rule)); 453 454 return regd; 455 } 456 457 static void cfg80211_save_user_regdom(const struct ieee80211_regdomain *rd) 458 { 459 ASSERT_RTNL(); 460 461 if (!IS_ERR(cfg80211_user_regdom)) 462 kfree(cfg80211_user_regdom); 463 cfg80211_user_regdom = reg_copy_regd(rd); 464 } 465 466 struct reg_regdb_apply_request { 467 struct list_head list; 468 const struct ieee80211_regdomain *regdom; 469 }; 470 471 static LIST_HEAD(reg_regdb_apply_list); 472 static DEFINE_MUTEX(reg_regdb_apply_mutex); 473 474 static void reg_regdb_apply(struct work_struct *work) 475 { 476 struct reg_regdb_apply_request *request; 477 478 rtnl_lock(); 479 480 mutex_lock(®_regdb_apply_mutex); 481 while (!list_empty(®_regdb_apply_list)) { 482 request = list_first_entry(®_regdb_apply_list, 483 struct reg_regdb_apply_request, 484 list); 485 list_del(&request->list); 486 487 set_regdom(request->regdom, REGD_SOURCE_INTERNAL_DB); 488 kfree(request); 489 } 490 mutex_unlock(®_regdb_apply_mutex); 491 492 rtnl_unlock(); 493 } 494 495 static DECLARE_WORK(reg_regdb_work, reg_regdb_apply); 496 497 static int reg_schedule_apply(const struct ieee80211_regdomain *regdom) 498 { 499 struct reg_regdb_apply_request *request; 500 501 request = kzalloc_obj(struct reg_regdb_apply_request); 502 if (!request) { 503 kfree(regdom); 504 return -ENOMEM; 505 } 506 507 request->regdom = regdom; 508 509 mutex_lock(®_regdb_apply_mutex); 510 list_add_tail(&request->list, ®_regdb_apply_list); 511 mutex_unlock(®_regdb_apply_mutex); 512 513 schedule_work(®_regdb_work); 514 return 0; 515 } 516 517 #ifdef CONFIG_CFG80211_CRDA_SUPPORT 518 /* Max number of consecutive attempts to communicate with CRDA */ 519 #define REG_MAX_CRDA_TIMEOUTS 10 520 521 static u32 reg_crda_timeouts; 522 523 static void crda_timeout_work(struct work_struct *work); 524 static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work); 525 526 static void crda_timeout_work(struct work_struct *work) 527 { 528 pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n"); 529 rtnl_lock(); 530 reg_crda_timeouts++; 531 restore_regulatory_settings(true, false); 532 rtnl_unlock(); 533 } 534 535 static void cancel_crda_timeout(void) 536 { 537 cancel_delayed_work(&crda_timeout); 538 } 539 540 static void cancel_crda_timeout_sync(void) 541 { 542 cancel_delayed_work_sync(&crda_timeout); 543 } 544 545 static void reset_crda_timeouts(void) 546 { 547 reg_crda_timeouts = 0; 548 } 549 550 /* 551 * This lets us keep regulatory code which is updated on a regulatory 552 * basis in userspace. 553 */ 554 static int call_crda(const char *alpha2) 555 { 556 char country[12]; 557 char *env[] = { country, NULL }; 558 int ret; 559 560 snprintf(country, sizeof(country), "COUNTRY=%c%c", 561 alpha2[0], alpha2[1]); 562 563 if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) { 564 pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n"); 565 return -EINVAL; 566 } 567 568 if (!is_world_regdom((char *) alpha2)) 569 pr_debug("Calling CRDA for country: %c%c\n", 570 alpha2[0], alpha2[1]); 571 else 572 pr_debug("Calling CRDA to update world regulatory domain\n"); 573 574 ret = kobject_uevent_env(®_fdev->dev.kobj, KOBJ_CHANGE, env); 575 if (ret) 576 return ret; 577 578 queue_delayed_work(system_power_efficient_wq, 579 &crda_timeout, msecs_to_jiffies(3142)); 580 return 0; 581 } 582 #else 583 static inline void cancel_crda_timeout(void) {} 584 static inline void cancel_crda_timeout_sync(void) {} 585 static inline void reset_crda_timeouts(void) {} 586 static inline int call_crda(const char *alpha2) 587 { 588 return -ENODATA; 589 } 590 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */ 591 592 /* code to directly load a firmware database through request_firmware */ 593 static const struct fwdb_header *regdb; 594 595 struct fwdb_country { 596 u8 alpha2[2]; 597 __be16 coll_ptr; 598 /* this struct cannot be extended */ 599 } __packed __aligned(4); 600 601 struct fwdb_collection { 602 u8 len; 603 u8 n_rules; 604 u8 dfs_region; 605 /* no optional data yet */ 606 /* aligned to 2, then followed by __be16 array of rule pointers */ 607 } __packed __aligned(4); 608 609 enum fwdb_flags { 610 FWDB_FLAG_NO_OFDM = BIT(0), 611 FWDB_FLAG_NO_OUTDOOR = BIT(1), 612 FWDB_FLAG_DFS = BIT(2), 613 FWDB_FLAG_NO_IR = BIT(3), 614 FWDB_FLAG_AUTO_BW = BIT(4), 615 }; 616 617 struct fwdb_wmm_ac { 618 u8 ecw; 619 u8 aifsn; 620 __be16 cot; 621 } __packed; 622 623 struct fwdb_wmm_rule { 624 struct fwdb_wmm_ac client[IEEE80211_NUM_ACS]; 625 struct fwdb_wmm_ac ap[IEEE80211_NUM_ACS]; 626 } __packed; 627 628 struct fwdb_rule { 629 u8 len; 630 u8 flags; 631 __be16 max_eirp; 632 __be32 start, end, max_bw; 633 /* start of optional data */ 634 __be16 cac_timeout; 635 __be16 wmm_ptr; 636 } __packed __aligned(4); 637 638 #define FWDB_MAGIC 0x52474442 639 #define FWDB_VERSION 20 640 641 struct fwdb_header { 642 __be32 magic; 643 __be32 version; 644 struct fwdb_country country[]; 645 } __packed __aligned(4); 646 647 static int ecw2cw(int ecw) 648 { 649 return (1 << ecw) - 1; 650 } 651 652 static bool valid_wmm(struct fwdb_wmm_rule *rule) 653 { 654 struct fwdb_wmm_ac *ac = (struct fwdb_wmm_ac *)rule; 655 int i; 656 657 for (i = 0; i < IEEE80211_NUM_ACS * 2; i++) { 658 u16 cw_min = ecw2cw((ac[i].ecw & 0xf0) >> 4); 659 u16 cw_max = ecw2cw(ac[i].ecw & 0x0f); 660 u8 aifsn = ac[i].aifsn; 661 662 if (cw_min >= cw_max) 663 return false; 664 665 if (aifsn < 1) 666 return false; 667 } 668 669 return true; 670 } 671 672 static bool valid_rule(const u8 *data, unsigned int size, u16 rule_ptr) 673 { 674 struct fwdb_rule *rule = (void *)(data + (rule_ptr << 2)); 675 676 if ((u8 *)rule + sizeof(rule->len) > data + size) 677 return false; 678 679 /* mandatory fields */ 680 if (rule->len < offsetofend(struct fwdb_rule, max_bw)) 681 return false; 682 if (rule->len >= offsetofend(struct fwdb_rule, wmm_ptr)) { 683 u32 wmm_ptr = be16_to_cpu(rule->wmm_ptr) << 2; 684 struct fwdb_wmm_rule *wmm; 685 686 if (wmm_ptr + sizeof(struct fwdb_wmm_rule) > size) 687 return false; 688 689 wmm = (void *)(data + wmm_ptr); 690 691 if (!valid_wmm(wmm)) 692 return false; 693 } 694 return true; 695 } 696 697 static bool valid_country(const u8 *data, unsigned int size, 698 const struct fwdb_country *country) 699 { 700 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2; 701 struct fwdb_collection *coll = (void *)(data + ptr); 702 __be16 *rules_ptr; 703 unsigned int i; 704 705 /* make sure we can read len/n_rules */ 706 if ((u8 *)coll + offsetofend(typeof(*coll), n_rules) > data + size) 707 return false; 708 709 /* make sure base struct and all rules fit */ 710 if ((u8 *)coll + ALIGN(coll->len, 2) + 711 (coll->n_rules * 2) > data + size) 712 return false; 713 714 /* mandatory fields must exist */ 715 if (coll->len < offsetofend(struct fwdb_collection, dfs_region)) 716 return false; 717 718 rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2)); 719 720 for (i = 0; i < coll->n_rules; i++) { 721 u16 rule_ptr = be16_to_cpu(rules_ptr[i]); 722 723 if (!valid_rule(data, size, rule_ptr)) 724 return false; 725 } 726 727 return true; 728 } 729 730 #ifdef CONFIG_CFG80211_REQUIRE_SIGNED_REGDB 731 #include <keys/asymmetric-type.h> 732 733 static struct key *builtin_regdb_keys; 734 735 static int __init load_builtin_regdb_keys(void) 736 { 737 builtin_regdb_keys = 738 keyring_alloc(".builtin_regdb_keys", 739 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(), 740 ((KEY_POS_ALL & ~KEY_POS_SETATTR) | 741 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH), 742 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); 743 if (IS_ERR(builtin_regdb_keys)) 744 return PTR_ERR(builtin_regdb_keys); 745 746 pr_notice("Loading compiled-in X.509 certificates for regulatory database\n"); 747 748 #ifdef CONFIG_CFG80211_USE_KERNEL_REGDB_KEYS 749 x509_load_certificate_list(shipped_regdb_certs, 750 shipped_regdb_certs_len, 751 builtin_regdb_keys); 752 #endif 753 #ifdef CONFIG_CFG80211_EXTRA_REGDB_KEYDIR 754 if (CONFIG_CFG80211_EXTRA_REGDB_KEYDIR[0] != '\0') 755 x509_load_certificate_list(extra_regdb_certs, 756 extra_regdb_certs_len, 757 builtin_regdb_keys); 758 #endif 759 760 return 0; 761 } 762 763 MODULE_FIRMWARE("regulatory.db.p7s"); 764 765 static bool regdb_has_valid_signature(const u8 *data, unsigned int size) 766 { 767 const struct firmware *sig; 768 bool result; 769 770 if (request_firmware(&sig, "regulatory.db.p7s", ®_fdev->dev)) 771 return false; 772 773 result = verify_pkcs7_signature(data, size, sig->data, sig->size, 774 builtin_regdb_keys, 775 VERIFYING_UNSPECIFIED_SIGNATURE, 776 NULL, NULL) == 0; 777 778 release_firmware(sig); 779 780 return result; 781 } 782 783 static void free_regdb_keyring(void) 784 { 785 key_put(builtin_regdb_keys); 786 } 787 #else 788 static int load_builtin_regdb_keys(void) 789 { 790 return 0; 791 } 792 793 static bool regdb_has_valid_signature(const u8 *data, unsigned int size) 794 { 795 return true; 796 } 797 798 static void free_regdb_keyring(void) 799 { 800 } 801 #endif /* CONFIG_CFG80211_REQUIRE_SIGNED_REGDB */ 802 803 static bool valid_regdb(const u8 *data, unsigned int size) 804 { 805 const struct fwdb_header *hdr = (void *)data; 806 const struct fwdb_country *country; 807 808 if (size < sizeof(*hdr)) 809 return false; 810 811 if (hdr->magic != cpu_to_be32(FWDB_MAGIC)) 812 return false; 813 814 if (hdr->version != cpu_to_be32(FWDB_VERSION)) 815 return false; 816 817 if (!regdb_has_valid_signature(data, size)) 818 return false; 819 820 country = &hdr->country[0]; 821 while ((u8 *)(country + 1) <= data + size) { 822 if (!country->coll_ptr) 823 break; 824 if (!valid_country(data, size, country)) 825 return false; 826 country++; 827 } 828 829 return true; 830 } 831 832 static void set_wmm_rule(const struct fwdb_header *db, 833 const struct fwdb_country *country, 834 const struct fwdb_rule *rule, 835 struct ieee80211_reg_rule *rrule) 836 { 837 struct ieee80211_wmm_rule *wmm_rule = &rrule->wmm_rule; 838 struct fwdb_wmm_rule *wmm; 839 unsigned int i, wmm_ptr; 840 841 wmm_ptr = be16_to_cpu(rule->wmm_ptr) << 2; 842 wmm = (void *)((u8 *)db + wmm_ptr); 843 844 if (!valid_wmm(wmm)) { 845 pr_err("Invalid regulatory WMM rule %u-%u in domain %c%c\n", 846 be32_to_cpu(rule->start), be32_to_cpu(rule->end), 847 country->alpha2[0], country->alpha2[1]); 848 return; 849 } 850 851 for (i = 0; i < IEEE80211_NUM_ACS; i++) { 852 wmm_rule->client[i].cw_min = 853 ecw2cw((wmm->client[i].ecw & 0xf0) >> 4); 854 wmm_rule->client[i].cw_max = ecw2cw(wmm->client[i].ecw & 0x0f); 855 wmm_rule->client[i].aifsn = wmm->client[i].aifsn; 856 wmm_rule->client[i].cot = 857 1000 * be16_to_cpu(wmm->client[i].cot); 858 wmm_rule->ap[i].cw_min = ecw2cw((wmm->ap[i].ecw & 0xf0) >> 4); 859 wmm_rule->ap[i].cw_max = ecw2cw(wmm->ap[i].ecw & 0x0f); 860 wmm_rule->ap[i].aifsn = wmm->ap[i].aifsn; 861 wmm_rule->ap[i].cot = 1000 * be16_to_cpu(wmm->ap[i].cot); 862 } 863 864 rrule->has_wmm = true; 865 } 866 867 static int __regdb_query_wmm(const struct fwdb_header *db, 868 const struct fwdb_country *country, int freq, 869 struct ieee80211_reg_rule *rrule) 870 { 871 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2; 872 struct fwdb_collection *coll = (void *)((u8 *)db + ptr); 873 int i; 874 875 for (i = 0; i < coll->n_rules; i++) { 876 __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2)); 877 unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2; 878 struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr); 879 880 if (rule->len < offsetofend(struct fwdb_rule, wmm_ptr)) 881 continue; 882 883 if (freq >= KHZ_TO_MHZ(be32_to_cpu(rule->start)) && 884 freq <= KHZ_TO_MHZ(be32_to_cpu(rule->end))) { 885 set_wmm_rule(db, country, rule, rrule); 886 return 0; 887 } 888 } 889 890 return -ENODATA; 891 } 892 893 int reg_query_regdb_wmm(char *alpha2, int freq, struct ieee80211_reg_rule *rule) 894 { 895 const struct fwdb_header *hdr = regdb; 896 const struct fwdb_country *country; 897 898 if (!regdb) 899 return -ENODATA; 900 901 if (IS_ERR(regdb)) 902 return PTR_ERR(regdb); 903 904 country = &hdr->country[0]; 905 while (country->coll_ptr) { 906 if (alpha2_equal(alpha2, country->alpha2)) 907 return __regdb_query_wmm(regdb, country, freq, rule); 908 909 country++; 910 } 911 912 return -ENODATA; 913 } 914 EXPORT_SYMBOL(reg_query_regdb_wmm); 915 916 static int regdb_query_country(const struct fwdb_header *db, 917 const struct fwdb_country *country) 918 { 919 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2; 920 struct fwdb_collection *coll = (void *)((u8 *)db + ptr); 921 struct ieee80211_regdomain *regdom; 922 unsigned int i; 923 924 regdom = kzalloc_flex(*regdom, reg_rules, coll->n_rules); 925 if (!regdom) 926 return -ENOMEM; 927 928 regdom->n_reg_rules = coll->n_rules; 929 regdom->alpha2[0] = country->alpha2[0]; 930 regdom->alpha2[1] = country->alpha2[1]; 931 regdom->dfs_region = coll->dfs_region; 932 933 for (i = 0; i < regdom->n_reg_rules; i++) { 934 __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2)); 935 unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2; 936 struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr); 937 struct ieee80211_reg_rule *rrule = ®dom->reg_rules[i]; 938 939 rrule->freq_range.start_freq_khz = be32_to_cpu(rule->start); 940 rrule->freq_range.end_freq_khz = be32_to_cpu(rule->end); 941 rrule->freq_range.max_bandwidth_khz = be32_to_cpu(rule->max_bw); 942 943 rrule->power_rule.max_antenna_gain = 0; 944 rrule->power_rule.max_eirp = be16_to_cpu(rule->max_eirp); 945 946 rrule->flags = 0; 947 if (rule->flags & FWDB_FLAG_NO_OFDM) 948 rrule->flags |= NL80211_RRF_NO_OFDM; 949 if (rule->flags & FWDB_FLAG_NO_OUTDOOR) 950 rrule->flags |= NL80211_RRF_NO_OUTDOOR; 951 if (rule->flags & FWDB_FLAG_DFS) 952 rrule->flags |= NL80211_RRF_DFS; 953 if (rule->flags & FWDB_FLAG_NO_IR) 954 rrule->flags |= NL80211_RRF_NO_IR; 955 if (rule->flags & FWDB_FLAG_AUTO_BW) 956 rrule->flags |= NL80211_RRF_AUTO_BW; 957 958 rrule->dfs_cac_ms = 0; 959 960 /* handle optional data */ 961 if (rule->len >= offsetofend(struct fwdb_rule, cac_timeout)) 962 rrule->dfs_cac_ms = 963 1000 * be16_to_cpu(rule->cac_timeout); 964 if (rule->len >= offsetofend(struct fwdb_rule, wmm_ptr)) 965 set_wmm_rule(db, country, rule, rrule); 966 } 967 968 return reg_schedule_apply(regdom); 969 } 970 971 static int query_regdb(const char *alpha2) 972 { 973 const struct fwdb_header *hdr = regdb; 974 const struct fwdb_country *country; 975 976 ASSERT_RTNL(); 977 978 if (IS_ERR(regdb)) 979 return PTR_ERR(regdb); 980 981 country = &hdr->country[0]; 982 while (country->coll_ptr) { 983 if (alpha2_equal(alpha2, country->alpha2)) 984 return regdb_query_country(regdb, country); 985 country++; 986 } 987 988 return -ENODATA; 989 } 990 991 static void regdb_fw_cb(const struct firmware *fw, void *context) 992 { 993 int set_error = 0; 994 bool restore = true; 995 void *db; 996 997 if (!fw) { 998 pr_info("failed to load regulatory.db\n"); 999 set_error = -ENODATA; 1000 } else if (!valid_regdb(fw->data, fw->size)) { 1001 pr_info("loaded regulatory.db is malformed or signature is missing/invalid\n"); 1002 set_error = -EINVAL; 1003 } 1004 1005 rtnl_lock(); 1006 if (regdb && !IS_ERR(regdb)) { 1007 /* negative case - a bug 1008 * positive case - can happen due to race in case of multiple cb's in 1009 * queue, due to usage of asynchronous callback 1010 * 1011 * Either case, just restore and free new db. 1012 */ 1013 } else if (set_error) { 1014 regdb = ERR_PTR(set_error); 1015 } else if (fw) { 1016 db = kmemdup(fw->data, fw->size, GFP_KERNEL); 1017 if (db) { 1018 regdb = db; 1019 restore = context && query_regdb(context); 1020 } else { 1021 restore = true; 1022 } 1023 } 1024 1025 if (restore) 1026 restore_regulatory_settings(true, false); 1027 1028 rtnl_unlock(); 1029 1030 kfree(context); 1031 1032 release_firmware(fw); 1033 } 1034 1035 MODULE_FIRMWARE("regulatory.db"); 1036 1037 static int query_regdb_file(const char *alpha2) 1038 { 1039 int err; 1040 1041 ASSERT_RTNL(); 1042 1043 if (regdb) 1044 return query_regdb(alpha2); 1045 1046 alpha2 = kmemdup(alpha2, 2, GFP_KERNEL); 1047 if (!alpha2) 1048 return -ENOMEM; 1049 1050 err = request_firmware_nowait(THIS_MODULE, true, "regulatory.db", 1051 ®_fdev->dev, GFP_KERNEL, 1052 (void *)alpha2, regdb_fw_cb); 1053 if (err) 1054 kfree(alpha2); 1055 1056 return err; 1057 } 1058 1059 int reg_reload_regdb(void) 1060 { 1061 const struct firmware *fw; 1062 void *db; 1063 int err; 1064 const struct ieee80211_regdomain *current_regdomain; 1065 struct regulatory_request *request; 1066 1067 err = request_firmware(&fw, "regulatory.db", ®_fdev->dev); 1068 if (err) 1069 return err; 1070 1071 if (!valid_regdb(fw->data, fw->size)) { 1072 err = -ENODATA; 1073 goto out; 1074 } 1075 1076 db = kmemdup(fw->data, fw->size, GFP_KERNEL); 1077 if (!db) { 1078 err = -ENOMEM; 1079 goto out; 1080 } 1081 1082 rtnl_lock(); 1083 if (!IS_ERR_OR_NULL(regdb)) 1084 kfree(regdb); 1085 regdb = db; 1086 1087 /* reset regulatory domain */ 1088 current_regdomain = get_cfg80211_regdom(); 1089 1090 request = kzalloc_obj(*request); 1091 if (!request) { 1092 err = -ENOMEM; 1093 goto out_unlock; 1094 } 1095 1096 request->wiphy_idx = WIPHY_IDX_INVALID; 1097 request->alpha2[0] = current_regdomain->alpha2[0]; 1098 request->alpha2[1] = current_regdomain->alpha2[1]; 1099 request->initiator = NL80211_REGDOM_SET_BY_CORE; 1100 request->user_reg_hint_type = NL80211_USER_REG_HINT_USER; 1101 1102 reg_process_hint(request); 1103 1104 out_unlock: 1105 rtnl_unlock(); 1106 out: 1107 release_firmware(fw); 1108 return err; 1109 } 1110 1111 static bool reg_query_database(struct regulatory_request *request) 1112 { 1113 if (query_regdb_file(request->alpha2) == 0) 1114 return true; 1115 1116 if (call_crda(request->alpha2) == 0) 1117 return true; 1118 1119 return false; 1120 } 1121 1122 bool reg_is_valid_request(const char *alpha2) 1123 { 1124 struct regulatory_request *lr = get_last_request(); 1125 1126 if (!lr || lr->processed) 1127 return false; 1128 1129 return alpha2_equal(lr->alpha2, alpha2); 1130 } 1131 1132 static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy) 1133 { 1134 struct regulatory_request *lr = get_last_request(); 1135 1136 /* 1137 * Follow the driver's regulatory domain, if present, unless a country 1138 * IE has been processed or a user wants to help compliance further 1139 */ 1140 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && 1141 lr->initiator != NL80211_REGDOM_SET_BY_USER && 1142 wiphy->regd) 1143 return get_wiphy_regdom(wiphy); 1144 1145 return get_cfg80211_regdom(); 1146 } 1147 1148 static unsigned int 1149 reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd, 1150 const struct ieee80211_reg_rule *rule) 1151 { 1152 const struct ieee80211_freq_range *freq_range = &rule->freq_range; 1153 const struct ieee80211_freq_range *freq_range_tmp; 1154 const struct ieee80211_reg_rule *tmp; 1155 u32 start_freq, end_freq, idx, no; 1156 1157 for (idx = 0; idx < rd->n_reg_rules; idx++) 1158 if (rule == &rd->reg_rules[idx]) 1159 break; 1160 1161 if (idx == rd->n_reg_rules) 1162 return 0; 1163 1164 /* get start_freq */ 1165 no = idx; 1166 1167 while (no) { 1168 tmp = &rd->reg_rules[--no]; 1169 freq_range_tmp = &tmp->freq_range; 1170 1171 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz) 1172 break; 1173 1174 freq_range = freq_range_tmp; 1175 } 1176 1177 start_freq = freq_range->start_freq_khz; 1178 1179 /* get end_freq */ 1180 freq_range = &rule->freq_range; 1181 no = idx; 1182 1183 while (no < rd->n_reg_rules - 1) { 1184 tmp = &rd->reg_rules[++no]; 1185 freq_range_tmp = &tmp->freq_range; 1186 1187 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz) 1188 break; 1189 1190 freq_range = freq_range_tmp; 1191 } 1192 1193 end_freq = freq_range->end_freq_khz; 1194 1195 return end_freq - start_freq; 1196 } 1197 1198 unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd, 1199 const struct ieee80211_reg_rule *rule) 1200 { 1201 unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule); 1202 1203 if (rule->flags & NL80211_RRF_NO_320MHZ) 1204 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(160)); 1205 if (rule->flags & NL80211_RRF_NO_160MHZ) 1206 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80)); 1207 if (rule->flags & NL80211_RRF_NO_80MHZ) 1208 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40)); 1209 1210 /* 1211 * HT40+/HT40- limits are handled per-channel. Only limit BW if both 1212 * are not allowed. 1213 */ 1214 if (rule->flags & NL80211_RRF_NO_HT40MINUS && 1215 rule->flags & NL80211_RRF_NO_HT40PLUS) 1216 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20)); 1217 1218 return bw; 1219 } 1220 1221 /* Sanity check on a regulatory rule */ 1222 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule) 1223 { 1224 const struct ieee80211_freq_range *freq_range = &rule->freq_range; 1225 u32 freq_diff; 1226 1227 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0) 1228 return false; 1229 1230 if (freq_range->start_freq_khz > freq_range->end_freq_khz) 1231 return false; 1232 1233 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; 1234 1235 if (freq_range->end_freq_khz <= freq_range->start_freq_khz || 1236 freq_range->max_bandwidth_khz > freq_diff) 1237 return false; 1238 1239 return true; 1240 } 1241 1242 static bool is_valid_rd(const struct ieee80211_regdomain *rd) 1243 { 1244 const struct ieee80211_reg_rule *reg_rule = NULL; 1245 unsigned int i; 1246 1247 if (!rd->n_reg_rules) 1248 return false; 1249 1250 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES)) 1251 return false; 1252 1253 for (i = 0; i < rd->n_reg_rules; i++) { 1254 reg_rule = &rd->reg_rules[i]; 1255 if (!is_valid_reg_rule(reg_rule)) 1256 return false; 1257 } 1258 1259 return true; 1260 } 1261 1262 /** 1263 * freq_in_rule_band - tells us if a frequency is in a frequency band 1264 * @freq_range: frequency rule we want to query 1265 * @freq_khz: frequency we are inquiring about 1266 * 1267 * This lets us know if a specific frequency rule is or is not relevant to 1268 * a specific frequency's band. Bands are device specific and artificial 1269 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"), 1270 * however it is safe for now to assume that a frequency rule should not be 1271 * part of a frequency's band if the start freq or end freq are off by more 1272 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 20 GHz for the 1273 * 60 GHz band. 1274 * This resolution can be lowered and should be considered as we add 1275 * regulatory rule support for other "bands". 1276 * 1277 * Returns: whether or not the frequency is in the range 1278 */ 1279 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range, 1280 u32 freq_khz) 1281 { 1282 /* 1283 * From 802.11ad: directional multi-gigabit (DMG): 1284 * Pertaining to operation in a frequency band containing a channel 1285 * with the Channel starting frequency above 45 GHz. 1286 */ 1287 u32 limit = freq_khz > 45 * KHZ_PER_GHZ ? 20 * KHZ_PER_GHZ : 2 * KHZ_PER_GHZ; 1288 if (abs(freq_khz - freq_range->start_freq_khz) <= limit) 1289 return true; 1290 if (abs(freq_khz - freq_range->end_freq_khz) <= limit) 1291 return true; 1292 return false; 1293 } 1294 1295 /* 1296 * Later on we can perhaps use the more restrictive DFS 1297 * region but we don't have information for that yet so 1298 * for now simply disallow conflicts. 1299 */ 1300 static enum nl80211_dfs_regions 1301 reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1, 1302 const enum nl80211_dfs_regions dfs_region2) 1303 { 1304 if (dfs_region1 != dfs_region2) 1305 return NL80211_DFS_UNSET; 1306 return dfs_region1; 1307 } 1308 1309 static void reg_wmm_rules_intersect(const struct ieee80211_wmm_ac *wmm_ac1, 1310 const struct ieee80211_wmm_ac *wmm_ac2, 1311 struct ieee80211_wmm_ac *intersect) 1312 { 1313 intersect->cw_min = max_t(u16, wmm_ac1->cw_min, wmm_ac2->cw_min); 1314 intersect->cw_max = max_t(u16, wmm_ac1->cw_max, wmm_ac2->cw_max); 1315 intersect->cot = min_t(u16, wmm_ac1->cot, wmm_ac2->cot); 1316 intersect->aifsn = max_t(u8, wmm_ac1->aifsn, wmm_ac2->aifsn); 1317 } 1318 1319 /* 1320 * Helper for regdom_intersect(), this does the real 1321 * mathematical intersection fun 1322 */ 1323 static int reg_rules_intersect(const struct ieee80211_regdomain *rd1, 1324 const struct ieee80211_regdomain *rd2, 1325 const struct ieee80211_reg_rule *rule1, 1326 const struct ieee80211_reg_rule *rule2, 1327 struct ieee80211_reg_rule *intersected_rule) 1328 { 1329 const struct ieee80211_freq_range *freq_range1, *freq_range2; 1330 struct ieee80211_freq_range *freq_range; 1331 const struct ieee80211_power_rule *power_rule1, *power_rule2; 1332 struct ieee80211_power_rule *power_rule; 1333 const struct ieee80211_wmm_rule *wmm_rule1, *wmm_rule2; 1334 struct ieee80211_wmm_rule *wmm_rule; 1335 u32 freq_diff, max_bandwidth1, max_bandwidth2; 1336 1337 freq_range1 = &rule1->freq_range; 1338 freq_range2 = &rule2->freq_range; 1339 freq_range = &intersected_rule->freq_range; 1340 1341 power_rule1 = &rule1->power_rule; 1342 power_rule2 = &rule2->power_rule; 1343 power_rule = &intersected_rule->power_rule; 1344 1345 wmm_rule1 = &rule1->wmm_rule; 1346 wmm_rule2 = &rule2->wmm_rule; 1347 wmm_rule = &intersected_rule->wmm_rule; 1348 1349 freq_range->start_freq_khz = max(freq_range1->start_freq_khz, 1350 freq_range2->start_freq_khz); 1351 freq_range->end_freq_khz = min(freq_range1->end_freq_khz, 1352 freq_range2->end_freq_khz); 1353 1354 max_bandwidth1 = freq_range1->max_bandwidth_khz; 1355 max_bandwidth2 = freq_range2->max_bandwidth_khz; 1356 1357 if (rule1->flags & NL80211_RRF_AUTO_BW) 1358 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1); 1359 if (rule2->flags & NL80211_RRF_AUTO_BW) 1360 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2); 1361 1362 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2); 1363 1364 intersected_rule->flags = rule1->flags | rule2->flags; 1365 1366 /* 1367 * In case NL80211_RRF_AUTO_BW requested for both rules 1368 * set AUTO_BW in intersected rule also. Next we will 1369 * calculate BW correctly in handle_channel function. 1370 * In other case remove AUTO_BW flag while we calculate 1371 * maximum bandwidth correctly and auto calculation is 1372 * not required. 1373 */ 1374 if ((rule1->flags & NL80211_RRF_AUTO_BW) && 1375 (rule2->flags & NL80211_RRF_AUTO_BW)) 1376 intersected_rule->flags |= NL80211_RRF_AUTO_BW; 1377 else 1378 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW; 1379 1380 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; 1381 if (freq_range->max_bandwidth_khz > freq_diff) 1382 freq_range->max_bandwidth_khz = freq_diff; 1383 1384 power_rule->max_eirp = min(power_rule1->max_eirp, 1385 power_rule2->max_eirp); 1386 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain, 1387 power_rule2->max_antenna_gain); 1388 1389 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms, 1390 rule2->dfs_cac_ms); 1391 1392 if (rule1->has_wmm && rule2->has_wmm) { 1393 u8 ac; 1394 1395 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1396 reg_wmm_rules_intersect(&wmm_rule1->client[ac], 1397 &wmm_rule2->client[ac], 1398 &wmm_rule->client[ac]); 1399 reg_wmm_rules_intersect(&wmm_rule1->ap[ac], 1400 &wmm_rule2->ap[ac], 1401 &wmm_rule->ap[ac]); 1402 } 1403 1404 intersected_rule->has_wmm = true; 1405 } else if (rule1->has_wmm) { 1406 *wmm_rule = *wmm_rule1; 1407 intersected_rule->has_wmm = true; 1408 } else if (rule2->has_wmm) { 1409 *wmm_rule = *wmm_rule2; 1410 intersected_rule->has_wmm = true; 1411 } else { 1412 intersected_rule->has_wmm = false; 1413 } 1414 1415 if (!is_valid_reg_rule(intersected_rule)) 1416 return -EINVAL; 1417 1418 return 0; 1419 } 1420 1421 /* check whether old rule contains new rule */ 1422 static bool rule_contains(struct ieee80211_reg_rule *r1, 1423 struct ieee80211_reg_rule *r2) 1424 { 1425 /* for simplicity, currently consider only same flags */ 1426 if (r1->flags != r2->flags) 1427 return false; 1428 1429 /* verify r1 is more restrictive */ 1430 if ((r1->power_rule.max_antenna_gain > 1431 r2->power_rule.max_antenna_gain) || 1432 r1->power_rule.max_eirp > r2->power_rule.max_eirp) 1433 return false; 1434 1435 /* make sure r2's range is contained within r1 */ 1436 if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz || 1437 r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz) 1438 return false; 1439 1440 /* and finally verify that r1.max_bw >= r2.max_bw */ 1441 if (r1->freq_range.max_bandwidth_khz < 1442 r2->freq_range.max_bandwidth_khz) 1443 return false; 1444 1445 return true; 1446 } 1447 1448 /* add or extend current rules. do nothing if rule is already contained */ 1449 static void add_rule(struct ieee80211_reg_rule *rule, 1450 struct ieee80211_reg_rule *reg_rules, u32 *n_rules) 1451 { 1452 struct ieee80211_reg_rule *tmp_rule; 1453 int i; 1454 1455 for (i = 0; i < *n_rules; i++) { 1456 tmp_rule = ®_rules[i]; 1457 /* rule is already contained - do nothing */ 1458 if (rule_contains(tmp_rule, rule)) 1459 return; 1460 1461 /* extend rule if possible */ 1462 if (rule_contains(rule, tmp_rule)) { 1463 memcpy(tmp_rule, rule, sizeof(*rule)); 1464 return; 1465 } 1466 } 1467 1468 memcpy(®_rules[*n_rules], rule, sizeof(*rule)); 1469 (*n_rules)++; 1470 } 1471 1472 /** 1473 * regdom_intersect - do the intersection between two regulatory domains 1474 * @rd1: first regulatory domain 1475 * @rd2: second regulatory domain 1476 * 1477 * Use this function to get the intersection between two regulatory domains. 1478 * Once completed we will mark the alpha2 for the rd as intersected, "98", 1479 * as no one single alpha2 can represent this regulatory domain. 1480 * 1481 * Returns a pointer to the regulatory domain structure which will hold the 1482 * resulting intersection of rules between rd1 and rd2. We will 1483 * kzalloc() this structure for you. 1484 * 1485 * Returns: the intersected regdomain 1486 */ 1487 static struct ieee80211_regdomain * 1488 regdom_intersect(const struct ieee80211_regdomain *rd1, 1489 const struct ieee80211_regdomain *rd2) 1490 { 1491 int r; 1492 unsigned int x, y; 1493 unsigned int num_rules = 0; 1494 const struct ieee80211_reg_rule *rule1, *rule2; 1495 struct ieee80211_reg_rule intersected_rule; 1496 struct ieee80211_regdomain *rd; 1497 1498 if (!rd1 || !rd2) 1499 return NULL; 1500 1501 /* 1502 * First we get a count of the rules we'll need, then we actually 1503 * build them. This is to so we can malloc() and free() a 1504 * regdomain once. The reason we use reg_rules_intersect() here 1505 * is it will return -EINVAL if the rule computed makes no sense. 1506 * All rules that do check out OK are valid. 1507 */ 1508 1509 for (x = 0; x < rd1->n_reg_rules; x++) { 1510 rule1 = &rd1->reg_rules[x]; 1511 for (y = 0; y < rd2->n_reg_rules; y++) { 1512 rule2 = &rd2->reg_rules[y]; 1513 if (!reg_rules_intersect(rd1, rd2, rule1, rule2, 1514 &intersected_rule)) 1515 num_rules++; 1516 } 1517 } 1518 1519 if (!num_rules) 1520 return NULL; 1521 1522 rd = kzalloc_flex(*rd, reg_rules, num_rules); 1523 if (!rd) 1524 return NULL; 1525 1526 for (x = 0; x < rd1->n_reg_rules; x++) { 1527 rule1 = &rd1->reg_rules[x]; 1528 for (y = 0; y < rd2->n_reg_rules; y++) { 1529 rule2 = &rd2->reg_rules[y]; 1530 r = reg_rules_intersect(rd1, rd2, rule1, rule2, 1531 &intersected_rule); 1532 /* 1533 * No need to memset here the intersected rule here as 1534 * we're not using the stack anymore 1535 */ 1536 if (r) 1537 continue; 1538 1539 add_rule(&intersected_rule, rd->reg_rules, 1540 &rd->n_reg_rules); 1541 } 1542 } 1543 1544 rd->alpha2[0] = '9'; 1545 rd->alpha2[1] = '8'; 1546 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region, 1547 rd2->dfs_region); 1548 1549 return rd; 1550 } 1551 1552 /* 1553 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may 1554 * want to just have the channel structure use these 1555 */ 1556 static u32 map_regdom_flags(u32 rd_flags) 1557 { 1558 u32 channel_flags = 0; 1559 if (rd_flags & NL80211_RRF_NO_IR_ALL) 1560 channel_flags |= IEEE80211_CHAN_NO_IR; 1561 if (rd_flags & NL80211_RRF_DFS) 1562 channel_flags |= IEEE80211_CHAN_RADAR; 1563 if (rd_flags & NL80211_RRF_NO_OFDM) 1564 channel_flags |= IEEE80211_CHAN_NO_OFDM; 1565 if (rd_flags & NL80211_RRF_NO_OUTDOOR) 1566 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY; 1567 if (rd_flags & NL80211_RRF_IR_CONCURRENT) 1568 channel_flags |= IEEE80211_CHAN_IR_CONCURRENT; 1569 if (rd_flags & NL80211_RRF_NO_HT40MINUS) 1570 channel_flags |= IEEE80211_CHAN_NO_HT40MINUS; 1571 if (rd_flags & NL80211_RRF_NO_HT40PLUS) 1572 channel_flags |= IEEE80211_CHAN_NO_HT40PLUS; 1573 if (rd_flags & NL80211_RRF_NO_80MHZ) 1574 channel_flags |= IEEE80211_CHAN_NO_80MHZ; 1575 if (rd_flags & NL80211_RRF_NO_160MHZ) 1576 channel_flags |= IEEE80211_CHAN_NO_160MHZ; 1577 if (rd_flags & NL80211_RRF_NO_HE) 1578 channel_flags |= IEEE80211_CHAN_NO_HE; 1579 if (rd_flags & NL80211_RRF_NO_320MHZ) 1580 channel_flags |= IEEE80211_CHAN_NO_320MHZ; 1581 if (rd_flags & NL80211_RRF_NO_EHT) 1582 channel_flags |= IEEE80211_CHAN_NO_EHT; 1583 if (rd_flags & NL80211_RRF_DFS_CONCURRENT) 1584 channel_flags |= IEEE80211_CHAN_DFS_CONCURRENT; 1585 if (rd_flags & NL80211_RRF_NO_6GHZ_VLP_CLIENT) 1586 channel_flags |= IEEE80211_CHAN_NO_6GHZ_VLP_CLIENT; 1587 if (rd_flags & NL80211_RRF_NO_6GHZ_AFC_CLIENT) 1588 channel_flags |= IEEE80211_CHAN_NO_6GHZ_AFC_CLIENT; 1589 if (rd_flags & NL80211_RRF_PSD) 1590 channel_flags |= IEEE80211_CHAN_PSD; 1591 if (rd_flags & NL80211_RRF_ALLOW_6GHZ_VLP_AP) 1592 channel_flags |= IEEE80211_CHAN_ALLOW_6GHZ_VLP_AP; 1593 if (rd_flags & NL80211_RRF_ALLOW_20MHZ_ACTIVITY) 1594 channel_flags |= IEEE80211_CHAN_ALLOW_20MHZ_ACTIVITY; 1595 if (rd_flags & NL80211_RRF_NO_UHR) 1596 channel_flags |= IEEE80211_CHAN_NO_UHR; 1597 return channel_flags; 1598 } 1599 1600 static const struct ieee80211_reg_rule * 1601 freq_reg_info_regd(u32 center_freq, 1602 const struct ieee80211_regdomain *regd, u32 bw) 1603 { 1604 int i; 1605 bool band_rule_found = false; 1606 bool bw_fits = false; 1607 1608 if (!regd) 1609 return ERR_PTR(-EINVAL); 1610 1611 for (i = 0; i < regd->n_reg_rules; i++) { 1612 const struct ieee80211_reg_rule *rr; 1613 const struct ieee80211_freq_range *fr = NULL; 1614 1615 rr = ®d->reg_rules[i]; 1616 fr = &rr->freq_range; 1617 1618 /* 1619 * We only need to know if one frequency rule was 1620 * in center_freq's band, that's enough, so let's 1621 * not overwrite it once found 1622 */ 1623 if (!band_rule_found) 1624 band_rule_found = freq_in_rule_band(fr, center_freq); 1625 1626 bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw); 1627 1628 if (band_rule_found && bw_fits) 1629 return rr; 1630 } 1631 1632 if (!band_rule_found) 1633 return ERR_PTR(-ERANGE); 1634 1635 return ERR_PTR(-EINVAL); 1636 } 1637 1638 static const struct ieee80211_reg_rule * 1639 __freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw) 1640 { 1641 const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy); 1642 static const u32 bws[] = {0, 1, 2, 4, 5, 8, 10, 16, 20}; 1643 const struct ieee80211_reg_rule *reg_rule = ERR_PTR(-ERANGE); 1644 int i = ARRAY_SIZE(bws) - 1; 1645 u32 bw; 1646 1647 for (bw = MHZ_TO_KHZ(bws[i]); bw >= min_bw; bw = MHZ_TO_KHZ(bws[i--])) { 1648 reg_rule = freq_reg_info_regd(center_freq, regd, bw); 1649 if (!IS_ERR(reg_rule)) 1650 return reg_rule; 1651 } 1652 1653 return reg_rule; 1654 } 1655 1656 const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy, 1657 u32 center_freq) 1658 { 1659 u32 min_bw = center_freq < MHZ_TO_KHZ(1000) ? 1 : 20; 1660 1661 return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(min_bw)); 1662 } 1663 EXPORT_SYMBOL(freq_reg_info); 1664 1665 const char *reg_initiator_name(enum nl80211_reg_initiator initiator) 1666 { 1667 switch (initiator) { 1668 case NL80211_REGDOM_SET_BY_CORE: 1669 return "core"; 1670 case NL80211_REGDOM_SET_BY_USER: 1671 return "user"; 1672 case NL80211_REGDOM_SET_BY_DRIVER: 1673 return "driver"; 1674 case NL80211_REGDOM_SET_BY_COUNTRY_IE: 1675 return "country element"; 1676 default: 1677 WARN_ON(1); 1678 return "bug"; 1679 } 1680 } 1681 EXPORT_SYMBOL(reg_initiator_name); 1682 1683 static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd, 1684 const struct ieee80211_reg_rule *reg_rule, 1685 const struct ieee80211_channel *chan) 1686 { 1687 const struct ieee80211_freq_range *freq_range = NULL; 1688 u32 max_bandwidth_khz, center_freq_khz, bw_flags = 0; 1689 bool is_s1g = chan->band == NL80211_BAND_S1GHZ; 1690 1691 freq_range = ®_rule->freq_range; 1692 1693 max_bandwidth_khz = freq_range->max_bandwidth_khz; 1694 center_freq_khz = ieee80211_channel_to_khz(chan); 1695 /* Check if auto calculation requested */ 1696 if (reg_rule->flags & NL80211_RRF_AUTO_BW) 1697 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule); 1698 1699 if (is_s1g) { 1700 if (max_bandwidth_khz < MHZ_TO_KHZ(16)) 1701 bw_flags |= IEEE80211_CHAN_NO_16MHZ; 1702 if (max_bandwidth_khz < MHZ_TO_KHZ(8)) 1703 bw_flags |= IEEE80211_CHAN_NO_8MHZ; 1704 if (max_bandwidth_khz < MHZ_TO_KHZ(4)) 1705 bw_flags |= IEEE80211_CHAN_NO_4MHZ; 1706 return bw_flags; 1707 } 1708 1709 /* If we get a reg_rule we can assume that at least 5Mhz fit */ 1710 if (!cfg80211_does_bw_fit_range(freq_range, 1711 center_freq_khz, 1712 MHZ_TO_KHZ(10))) 1713 bw_flags |= IEEE80211_CHAN_NO_10MHZ; 1714 if (!cfg80211_does_bw_fit_range(freq_range, 1715 center_freq_khz, 1716 MHZ_TO_KHZ(20))) 1717 bw_flags |= IEEE80211_CHAN_NO_20MHZ; 1718 1719 if (max_bandwidth_khz < MHZ_TO_KHZ(10)) 1720 bw_flags |= IEEE80211_CHAN_NO_10MHZ; 1721 if (max_bandwidth_khz < MHZ_TO_KHZ(20)) 1722 bw_flags |= IEEE80211_CHAN_NO_20MHZ; 1723 if (max_bandwidth_khz < MHZ_TO_KHZ(40)) 1724 bw_flags |= IEEE80211_CHAN_NO_HT40; 1725 if (max_bandwidth_khz < MHZ_TO_KHZ(80)) 1726 bw_flags |= IEEE80211_CHAN_NO_80MHZ; 1727 if (max_bandwidth_khz < MHZ_TO_KHZ(160)) 1728 bw_flags |= IEEE80211_CHAN_NO_160MHZ; 1729 if (max_bandwidth_khz < MHZ_TO_KHZ(320)) 1730 bw_flags |= IEEE80211_CHAN_NO_320MHZ; 1731 1732 return bw_flags; 1733 } 1734 1735 static void handle_channel_single_rule(struct wiphy *wiphy, 1736 enum nl80211_reg_initiator initiator, 1737 struct ieee80211_channel *chan, 1738 u32 flags, 1739 struct regulatory_request *lr, 1740 struct wiphy *request_wiphy, 1741 const struct ieee80211_reg_rule *reg_rule) 1742 { 1743 u32 bw_flags = 0; 1744 const struct ieee80211_power_rule *power_rule = NULL; 1745 const struct ieee80211_regdomain *regd; 1746 1747 regd = reg_get_regdomain(wiphy); 1748 1749 power_rule = ®_rule->power_rule; 1750 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan); 1751 1752 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && 1753 request_wiphy && request_wiphy == wiphy && 1754 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) { 1755 /* 1756 * This guarantees the driver's requested regulatory domain 1757 * will always be used as a base for further regulatory 1758 * settings 1759 */ 1760 chan->flags = chan->orig_flags = 1761 map_regdom_flags(reg_rule->flags) | bw_flags; 1762 chan->max_antenna_gain = chan->orig_mag = 1763 (int) MBI_TO_DBI(power_rule->max_antenna_gain); 1764 chan->max_reg_power = chan->max_power = chan->orig_mpwr = 1765 (int) MBM_TO_DBM(power_rule->max_eirp); 1766 1767 if (chan->flags & IEEE80211_CHAN_RADAR) { 1768 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; 1769 if (reg_rule->dfs_cac_ms) 1770 chan->dfs_cac_ms = reg_rule->dfs_cac_ms; 1771 } 1772 1773 if (chan->flags & IEEE80211_CHAN_PSD) 1774 chan->psd = reg_rule->psd; 1775 1776 return; 1777 } 1778 1779 chan->dfs_state = NL80211_DFS_USABLE; 1780 chan->dfs_state_entered = jiffies; 1781 1782 chan->beacon_found = false; 1783 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags); 1784 chan->max_antenna_gain = 1785 min_t(int, chan->orig_mag, 1786 MBI_TO_DBI(power_rule->max_antenna_gain)); 1787 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp); 1788 1789 if (chan->flags & IEEE80211_CHAN_RADAR) { 1790 if (reg_rule->dfs_cac_ms) 1791 chan->dfs_cac_ms = reg_rule->dfs_cac_ms; 1792 else 1793 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; 1794 } 1795 1796 if (chan->flags & IEEE80211_CHAN_PSD) 1797 chan->psd = reg_rule->psd; 1798 1799 if (chan->orig_mpwr) { 1800 /* 1801 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER 1802 * will always follow the passed country IE power settings. 1803 */ 1804 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && 1805 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER) 1806 chan->max_power = chan->max_reg_power; 1807 else 1808 chan->max_power = min(chan->orig_mpwr, 1809 chan->max_reg_power); 1810 } else 1811 chan->max_power = chan->max_reg_power; 1812 } 1813 1814 static void handle_channel_adjacent_rules(struct wiphy *wiphy, 1815 enum nl80211_reg_initiator initiator, 1816 struct ieee80211_channel *chan, 1817 u32 flags, 1818 struct regulatory_request *lr, 1819 struct wiphy *request_wiphy, 1820 const struct ieee80211_reg_rule *rrule1, 1821 const struct ieee80211_reg_rule *rrule2, 1822 struct ieee80211_freq_range *comb_range) 1823 { 1824 u32 bw_flags1 = 0; 1825 u32 bw_flags2 = 0; 1826 const struct ieee80211_power_rule *power_rule1 = NULL; 1827 const struct ieee80211_power_rule *power_rule2 = NULL; 1828 const struct ieee80211_regdomain *regd; 1829 1830 regd = reg_get_regdomain(wiphy); 1831 1832 power_rule1 = &rrule1->power_rule; 1833 power_rule2 = &rrule2->power_rule; 1834 bw_flags1 = reg_rule_to_chan_bw_flags(regd, rrule1, chan); 1835 bw_flags2 = reg_rule_to_chan_bw_flags(regd, rrule2, chan); 1836 1837 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && 1838 request_wiphy && request_wiphy == wiphy && 1839 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) { 1840 /* This guarantees the driver's requested regulatory domain 1841 * will always be used as a base for further regulatory 1842 * settings 1843 */ 1844 chan->flags = 1845 map_regdom_flags(rrule1->flags) | 1846 map_regdom_flags(rrule2->flags) | 1847 bw_flags1 | 1848 bw_flags2; 1849 chan->orig_flags = chan->flags; 1850 chan->max_antenna_gain = 1851 min_t(int, MBI_TO_DBI(power_rule1->max_antenna_gain), 1852 MBI_TO_DBI(power_rule2->max_antenna_gain)); 1853 chan->orig_mag = chan->max_antenna_gain; 1854 chan->max_reg_power = 1855 min_t(int, MBM_TO_DBM(power_rule1->max_eirp), 1856 MBM_TO_DBM(power_rule2->max_eirp)); 1857 chan->max_power = chan->max_reg_power; 1858 chan->orig_mpwr = chan->max_reg_power; 1859 1860 if (chan->flags & IEEE80211_CHAN_RADAR) { 1861 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; 1862 if (rrule1->dfs_cac_ms || rrule2->dfs_cac_ms) 1863 chan->dfs_cac_ms = max_t(unsigned int, 1864 rrule1->dfs_cac_ms, 1865 rrule2->dfs_cac_ms); 1866 } 1867 1868 if ((rrule1->flags & NL80211_RRF_PSD) && 1869 (rrule2->flags & NL80211_RRF_PSD)) 1870 chan->psd = min_t(s8, rrule1->psd, rrule2->psd); 1871 else 1872 chan->flags &= ~NL80211_RRF_PSD; 1873 1874 return; 1875 } 1876 1877 chan->dfs_state = NL80211_DFS_USABLE; 1878 chan->dfs_state_entered = jiffies; 1879 1880 chan->beacon_found = false; 1881 chan->flags = flags | bw_flags1 | bw_flags2 | 1882 map_regdom_flags(rrule1->flags) | 1883 map_regdom_flags(rrule2->flags); 1884 1885 /* reg_rule_to_chan_bw_flags may forbids 10 and forbids 20 MHz 1886 * (otherwise no adj. rule case), recheck therefore 1887 */ 1888 if (cfg80211_does_bw_fit_range(comb_range, 1889 ieee80211_channel_to_khz(chan), 1890 MHZ_TO_KHZ(10))) 1891 chan->flags &= ~IEEE80211_CHAN_NO_10MHZ; 1892 if (cfg80211_does_bw_fit_range(comb_range, 1893 ieee80211_channel_to_khz(chan), 1894 MHZ_TO_KHZ(20))) 1895 chan->flags &= ~IEEE80211_CHAN_NO_20MHZ; 1896 1897 chan->max_antenna_gain = 1898 min_t(int, chan->orig_mag, 1899 min_t(int, 1900 MBI_TO_DBI(power_rule1->max_antenna_gain), 1901 MBI_TO_DBI(power_rule2->max_antenna_gain))); 1902 chan->max_reg_power = min_t(int, 1903 MBM_TO_DBM(power_rule1->max_eirp), 1904 MBM_TO_DBM(power_rule2->max_eirp)); 1905 1906 if (chan->flags & IEEE80211_CHAN_RADAR) { 1907 if (rrule1->dfs_cac_ms || rrule2->dfs_cac_ms) 1908 chan->dfs_cac_ms = max_t(unsigned int, 1909 rrule1->dfs_cac_ms, 1910 rrule2->dfs_cac_ms); 1911 else 1912 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; 1913 } 1914 1915 if (chan->orig_mpwr) { 1916 /* Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER 1917 * will always follow the passed country IE power settings. 1918 */ 1919 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && 1920 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER) 1921 chan->max_power = chan->max_reg_power; 1922 else 1923 chan->max_power = min(chan->orig_mpwr, 1924 chan->max_reg_power); 1925 } else { 1926 chan->max_power = chan->max_reg_power; 1927 } 1928 } 1929 1930 /* Note that right now we assume the desired channel bandwidth 1931 * is always 20 MHz for each individual channel (HT40 uses 20 MHz 1932 * per channel, the primary and the extension channel). 1933 */ 1934 static void handle_channel(struct wiphy *wiphy, 1935 enum nl80211_reg_initiator initiator, 1936 struct ieee80211_channel *chan) 1937 { 1938 const u32 orig_chan_freq = ieee80211_channel_to_khz(chan); 1939 struct regulatory_request *lr = get_last_request(); 1940 struct wiphy *request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); 1941 const struct ieee80211_reg_rule *rrule = NULL; 1942 const struct ieee80211_reg_rule *rrule1 = NULL; 1943 const struct ieee80211_reg_rule *rrule2 = NULL; 1944 1945 u32 flags = chan->orig_flags; 1946 1947 rrule = freq_reg_info(wiphy, orig_chan_freq); 1948 if (IS_ERR(rrule)) { 1949 /* check for adjacent match, therefore get rules for 1950 * chan - 20 MHz and chan + 20 MHz and test 1951 * if reg rules are adjacent 1952 */ 1953 rrule1 = freq_reg_info(wiphy, 1954 orig_chan_freq - MHZ_TO_KHZ(20)); 1955 rrule2 = freq_reg_info(wiphy, 1956 orig_chan_freq + MHZ_TO_KHZ(20)); 1957 if (!IS_ERR(rrule1) && !IS_ERR(rrule2)) { 1958 struct ieee80211_freq_range comb_range; 1959 1960 if (rrule1->freq_range.end_freq_khz != 1961 rrule2->freq_range.start_freq_khz) 1962 goto disable_chan; 1963 1964 comb_range.start_freq_khz = 1965 rrule1->freq_range.start_freq_khz; 1966 comb_range.end_freq_khz = 1967 rrule2->freq_range.end_freq_khz; 1968 comb_range.max_bandwidth_khz = 1969 min_t(u32, 1970 rrule1->freq_range.max_bandwidth_khz, 1971 rrule2->freq_range.max_bandwidth_khz); 1972 1973 if (!cfg80211_does_bw_fit_range(&comb_range, 1974 orig_chan_freq, 1975 MHZ_TO_KHZ(20))) 1976 goto disable_chan; 1977 1978 handle_channel_adjacent_rules(wiphy, initiator, chan, 1979 flags, lr, request_wiphy, 1980 rrule1, rrule2, 1981 &comb_range); 1982 return; 1983 } 1984 1985 disable_chan: 1986 /* We will disable all channels that do not match our 1987 * received regulatory rule unless the hint is coming 1988 * from a Country IE and the Country IE had no information 1989 * about a band. The IEEE 802.11 spec allows for an AP 1990 * to send only a subset of the regulatory rules allowed, 1991 * so an AP in the US that only supports 2.4 GHz may only send 1992 * a country IE with information for the 2.4 GHz band 1993 * while 5 GHz is still supported. 1994 */ 1995 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && 1996 PTR_ERR(rrule) == -ERANGE) 1997 return; 1998 1999 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && 2000 request_wiphy && request_wiphy == wiphy && 2001 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) { 2002 pr_debug("Disabling freq %d.%03d MHz for good\n", 2003 chan->center_freq, chan->freq_offset); 2004 chan->orig_flags |= IEEE80211_CHAN_DISABLED; 2005 chan->flags = chan->orig_flags; 2006 } else { 2007 pr_debug("Disabling freq %d.%03d MHz\n", 2008 chan->center_freq, chan->freq_offset); 2009 chan->flags |= IEEE80211_CHAN_DISABLED; 2010 } 2011 return; 2012 } 2013 2014 handle_channel_single_rule(wiphy, initiator, chan, flags, lr, 2015 request_wiphy, rrule); 2016 } 2017 2018 static void handle_band(struct wiphy *wiphy, 2019 enum nl80211_reg_initiator initiator, 2020 struct ieee80211_supported_band *sband) 2021 { 2022 unsigned int i; 2023 2024 if (!sband) 2025 return; 2026 2027 for (i = 0; i < sband->n_channels; i++) 2028 handle_channel(wiphy, initiator, &sband->channels[i]); 2029 } 2030 2031 static bool reg_request_cell_base(struct regulatory_request *request) 2032 { 2033 if (request->initiator != NL80211_REGDOM_SET_BY_USER) 2034 return false; 2035 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE; 2036 } 2037 2038 bool reg_last_request_cell_base(void) 2039 { 2040 return reg_request_cell_base(get_last_request()); 2041 } 2042 2043 #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS 2044 /* Core specific check */ 2045 static enum reg_request_treatment 2046 reg_ignore_cell_hint(struct regulatory_request *pending_request) 2047 { 2048 struct regulatory_request *lr = get_last_request(); 2049 2050 if (!reg_num_devs_support_basehint) 2051 return REG_REQ_IGNORE; 2052 2053 if (reg_request_cell_base(lr) && 2054 !regdom_changes(pending_request->alpha2)) 2055 return REG_REQ_ALREADY_SET; 2056 2057 return REG_REQ_OK; 2058 } 2059 2060 /* Device specific check */ 2061 static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy) 2062 { 2063 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS); 2064 } 2065 #else 2066 static enum reg_request_treatment 2067 reg_ignore_cell_hint(struct regulatory_request *pending_request) 2068 { 2069 return REG_REQ_IGNORE; 2070 } 2071 2072 static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy) 2073 { 2074 return true; 2075 } 2076 #endif 2077 2078 static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy) 2079 { 2080 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG && 2081 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)) 2082 return true; 2083 return false; 2084 } 2085 2086 static bool ignore_reg_update(struct wiphy *wiphy, 2087 enum nl80211_reg_initiator initiator) 2088 { 2089 struct regulatory_request *lr = get_last_request(); 2090 2091 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) 2092 return true; 2093 2094 if (!lr) { 2095 pr_debug("Ignoring regulatory request set by %s since last_request is not set\n", 2096 reg_initiator_name(initiator)); 2097 return true; 2098 } 2099 2100 if (initiator == NL80211_REGDOM_SET_BY_CORE && 2101 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) { 2102 pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n", 2103 reg_initiator_name(initiator)); 2104 return true; 2105 } 2106 2107 /* 2108 * wiphy->regd will be set once the device has its own 2109 * desired regulatory domain set 2110 */ 2111 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd && 2112 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && 2113 !is_world_regdom(lr->alpha2)) { 2114 pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n", 2115 reg_initiator_name(initiator)); 2116 return true; 2117 } 2118 2119 if (reg_request_cell_base(lr)) 2120 return reg_dev_ignore_cell_hint(wiphy); 2121 2122 return false; 2123 } 2124 2125 static bool reg_is_world_roaming(struct wiphy *wiphy) 2126 { 2127 const struct ieee80211_regdomain *cr = get_cfg80211_regdom(); 2128 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy); 2129 struct regulatory_request *lr = get_last_request(); 2130 2131 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2))) 2132 return true; 2133 2134 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && 2135 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) 2136 return true; 2137 2138 return false; 2139 } 2140 2141 static void reg_call_notifier(struct wiphy *wiphy, 2142 struct regulatory_request *request) 2143 { 2144 if (wiphy->reg_notifier) 2145 wiphy->reg_notifier(wiphy, request); 2146 } 2147 2148 static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx, 2149 struct reg_beacon *reg_beacon) 2150 { 2151 struct ieee80211_supported_band *sband; 2152 struct ieee80211_channel *chan; 2153 bool channel_changed = false; 2154 struct ieee80211_channel chan_before; 2155 struct regulatory_request *lr = get_last_request(); 2156 2157 sband = wiphy->bands[reg_beacon->chan.band]; 2158 chan = &sband->channels[chan_idx]; 2159 2160 if (likely(!ieee80211_channel_equal(chan, ®_beacon->chan))) 2161 return; 2162 2163 if (chan->beacon_found) 2164 return; 2165 2166 chan->beacon_found = true; 2167 2168 if (!reg_is_world_roaming(wiphy)) 2169 return; 2170 2171 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS) 2172 return; 2173 2174 chan_before = *chan; 2175 2176 if (chan->flags & IEEE80211_CHAN_NO_IR) { 2177 chan->flags &= ~IEEE80211_CHAN_NO_IR; 2178 channel_changed = true; 2179 } 2180 2181 if (channel_changed) { 2182 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan); 2183 if (wiphy->flags & WIPHY_FLAG_CHANNEL_CHANGE_ON_BEACON) 2184 reg_call_notifier(wiphy, lr); 2185 } 2186 } 2187 2188 /* 2189 * Called when a scan on a wiphy finds a beacon on 2190 * new channel 2191 */ 2192 static void wiphy_update_new_beacon(struct wiphy *wiphy, 2193 struct reg_beacon *reg_beacon) 2194 { 2195 unsigned int i; 2196 struct ieee80211_supported_band *sband; 2197 2198 if (!wiphy->bands[reg_beacon->chan.band]) 2199 return; 2200 2201 sband = wiphy->bands[reg_beacon->chan.band]; 2202 2203 for (i = 0; i < sband->n_channels; i++) 2204 handle_reg_beacon(wiphy, i, reg_beacon); 2205 } 2206 2207 /* 2208 * Called upon reg changes or a new wiphy is added 2209 */ 2210 static void wiphy_update_beacon_reg(struct wiphy *wiphy) 2211 { 2212 unsigned int i; 2213 struct ieee80211_supported_band *sband; 2214 struct reg_beacon *reg_beacon; 2215 2216 list_for_each_entry(reg_beacon, ®_beacon_list, list) { 2217 if (!wiphy->bands[reg_beacon->chan.band]) 2218 continue; 2219 sband = wiphy->bands[reg_beacon->chan.band]; 2220 for (i = 0; i < sband->n_channels; i++) 2221 handle_reg_beacon(wiphy, i, reg_beacon); 2222 } 2223 } 2224 2225 /* Reap the advantages of previously found beacons */ 2226 static void reg_process_beacons(struct wiphy *wiphy) 2227 { 2228 /* 2229 * Means we are just firing up cfg80211, so no beacons would 2230 * have been processed yet. 2231 */ 2232 if (!last_request) 2233 return; 2234 wiphy_update_beacon_reg(wiphy); 2235 } 2236 2237 static bool is_ht40_allowed(struct ieee80211_channel *chan) 2238 { 2239 if (!chan) 2240 return false; 2241 if (chan->flags & IEEE80211_CHAN_DISABLED) 2242 return false; 2243 /* This would happen when regulatory rules disallow HT40 completely */ 2244 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40) 2245 return false; 2246 return true; 2247 } 2248 2249 static void reg_process_ht_flags_channel(struct wiphy *wiphy, 2250 struct ieee80211_channel *channel) 2251 { 2252 struct ieee80211_supported_band *sband = wiphy->bands[channel->band]; 2253 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL; 2254 const struct ieee80211_regdomain *regd; 2255 unsigned int i; 2256 u32 flags; 2257 2258 if (!is_ht40_allowed(channel)) { 2259 channel->flags |= IEEE80211_CHAN_NO_HT40; 2260 return; 2261 } 2262 2263 /* 2264 * We need to ensure the extension channels exist to 2265 * be able to use HT40- or HT40+, this finds them (or not) 2266 */ 2267 for (i = 0; i < sband->n_channels; i++) { 2268 struct ieee80211_channel *c = &sband->channels[i]; 2269 2270 if (c->center_freq == (channel->center_freq - 20)) 2271 channel_before = c; 2272 if (c->center_freq == (channel->center_freq + 20)) 2273 channel_after = c; 2274 } 2275 2276 flags = 0; 2277 regd = get_wiphy_regdom(wiphy); 2278 if (regd) { 2279 const struct ieee80211_reg_rule *reg_rule = 2280 freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq), 2281 regd, MHZ_TO_KHZ(20)); 2282 2283 if (!IS_ERR(reg_rule)) 2284 flags = reg_rule->flags; 2285 } 2286 2287 /* 2288 * Please note that this assumes target bandwidth is 20 MHz, 2289 * if that ever changes we also need to change the below logic 2290 * to include that as well. 2291 */ 2292 if (!is_ht40_allowed(channel_before) || 2293 flags & NL80211_RRF_NO_HT40MINUS) 2294 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS; 2295 else 2296 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS; 2297 2298 if (!is_ht40_allowed(channel_after) || 2299 flags & NL80211_RRF_NO_HT40PLUS) 2300 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS; 2301 else 2302 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS; 2303 } 2304 2305 static void reg_process_ht_flags_band(struct wiphy *wiphy, 2306 struct ieee80211_supported_band *sband) 2307 { 2308 unsigned int i; 2309 2310 if (!sband) 2311 return; 2312 2313 for (i = 0; i < sband->n_channels; i++) 2314 reg_process_ht_flags_channel(wiphy, &sband->channels[i]); 2315 } 2316 2317 static void reg_process_ht_flags(struct wiphy *wiphy) 2318 { 2319 enum nl80211_band band; 2320 2321 if (!wiphy) 2322 return; 2323 2324 for (band = 0; band < NUM_NL80211_BANDS; band++) { 2325 /* 2326 * Don't apply HT flags to channels within the S1G band. 2327 * Each bonded channel will instead be validated individually 2328 * within cfg80211_s1g_usable(). 2329 */ 2330 if (band == NL80211_BAND_S1GHZ) 2331 continue; 2332 2333 reg_process_ht_flags_band(wiphy, wiphy->bands[band]); 2334 } 2335 } 2336 2337 static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev) 2338 { 2339 struct cfg80211_chan_def chandef = {}; 2340 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 2341 enum nl80211_iftype iftype; 2342 bool ret; 2343 int link; 2344 2345 iftype = wdev->iftype; 2346 2347 /* make sure the interface is active */ 2348 if (!wdev->netdev || !netif_running(wdev->netdev)) 2349 return true; 2350 2351 /* NAN doesn't have links, handle it separately */ 2352 if (iftype == NL80211_IFTYPE_NAN) { 2353 for (int i = 0; i < wdev->u.nan.n_channels; i++) { 2354 ret = cfg80211_reg_can_beacon(wiphy, 2355 &wdev->u.nan.chandefs[i], 2356 NL80211_IFTYPE_NAN); 2357 if (!ret) 2358 return false; 2359 } 2360 return true; 2361 } 2362 2363 for (link = 0; link < ARRAY_SIZE(wdev->links); link++) { 2364 struct ieee80211_channel *chan; 2365 2366 if (!wdev->valid_links && link > 0) 2367 break; 2368 if (wdev->valid_links && !(wdev->valid_links & BIT(link))) 2369 continue; 2370 switch (iftype) { 2371 case NL80211_IFTYPE_AP: 2372 case NL80211_IFTYPE_P2P_GO: 2373 if (!wdev->links[link].ap.beacon_interval) 2374 continue; 2375 chandef = wdev->links[link].ap.chandef; 2376 break; 2377 case NL80211_IFTYPE_MESH_POINT: 2378 if (!wdev->u.mesh.beacon_interval) 2379 continue; 2380 chandef = wdev->u.mesh.chandef; 2381 break; 2382 case NL80211_IFTYPE_ADHOC: 2383 if (!wdev->u.ibss.ssid_len) 2384 continue; 2385 chandef = wdev->u.ibss.chandef; 2386 break; 2387 case NL80211_IFTYPE_STATION: 2388 case NL80211_IFTYPE_P2P_CLIENT: 2389 /* Maybe we could consider disabling that link only? */ 2390 if (!wdev->links[link].client.current_bss) 2391 continue; 2392 2393 chan = wdev->links[link].client.current_bss->pub.channel; 2394 if (!chan) 2395 continue; 2396 2397 if (!rdev->ops->get_channel || 2398 rdev_get_channel(rdev, wdev, link, &chandef)) 2399 cfg80211_chandef_create(&chandef, chan, 2400 NL80211_CHAN_NO_HT); 2401 break; 2402 case NL80211_IFTYPE_MONITOR: 2403 case NL80211_IFTYPE_AP_VLAN: 2404 case NL80211_IFTYPE_P2P_DEVICE: 2405 /* no enforcement required */ 2406 break; 2407 case NL80211_IFTYPE_OCB: 2408 if (!wdev->u.ocb.chandef.chan) 2409 continue; 2410 chandef = wdev->u.ocb.chandef; 2411 break; 2412 case NL80211_IFTYPE_NAN_DATA: 2413 /* NAN channels are checked in NL80211_IFTYPE_NAN interface */ 2414 break; 2415 case NL80211_IFTYPE_PD: 2416 /* we have no info, but PD is also pretty universal */ 2417 continue; 2418 default: 2419 /* others not implemented for now */ 2420 WARN_ON_ONCE(1); 2421 break; 2422 } 2423 2424 switch (iftype) { 2425 case NL80211_IFTYPE_AP: 2426 case NL80211_IFTYPE_P2P_GO: 2427 case NL80211_IFTYPE_ADHOC: 2428 case NL80211_IFTYPE_MESH_POINT: 2429 ret = cfg80211_reg_can_beacon_relax(wiphy, &chandef, 2430 iftype); 2431 if (!ret) 2432 return ret; 2433 break; 2434 case NL80211_IFTYPE_STATION: 2435 case NL80211_IFTYPE_P2P_CLIENT: 2436 ret = cfg80211_chandef_usable(wiphy, &chandef, 2437 IEEE80211_CHAN_DISABLED); 2438 if (!ret) 2439 return ret; 2440 break; 2441 default: 2442 break; 2443 } 2444 } 2445 2446 return true; 2447 } 2448 2449 static void reg_leave_invalid_chans(struct wiphy *wiphy) 2450 { 2451 struct wireless_dev *wdev; 2452 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 2453 2454 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 2455 bool valid; 2456 2457 scoped_guard(wiphy, wiphy) 2458 valid = reg_wdev_chan_valid(wiphy, wdev); 2459 if (!valid) 2460 cfg80211_leave(rdev, wdev, -1); 2461 } 2462 } 2463 2464 static void reg_check_chans_work(struct work_struct *work) 2465 { 2466 struct cfg80211_registered_device *rdev; 2467 2468 pr_debug("Verifying active interfaces after reg change\n"); 2469 rtnl_lock(); 2470 2471 for_each_rdev(rdev) 2472 reg_leave_invalid_chans(&rdev->wiphy); 2473 2474 rtnl_unlock(); 2475 } 2476 2477 void reg_check_channels(void) 2478 { 2479 /* 2480 * Give usermode a chance to do something nicer (move to another 2481 * channel, orderly disconnection), before forcing a disconnection. 2482 */ 2483 mod_delayed_work(system_power_efficient_wq, 2484 ®_check_chans, 2485 msecs_to_jiffies(REG_ENFORCE_GRACE_MS)); 2486 } 2487 2488 static void wiphy_update_regulatory(struct wiphy *wiphy, 2489 enum nl80211_reg_initiator initiator) 2490 { 2491 enum nl80211_band band; 2492 struct regulatory_request *lr = get_last_request(); 2493 2494 if (ignore_reg_update(wiphy, initiator)) { 2495 /* 2496 * Regulatory updates set by CORE are ignored for custom 2497 * regulatory cards. Let us notify the changes to the driver, 2498 * as some drivers used this to restore its orig_* reg domain. 2499 */ 2500 if (initiator == NL80211_REGDOM_SET_BY_CORE && 2501 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG && 2502 !(wiphy->regulatory_flags & 2503 REGULATORY_WIPHY_SELF_MANAGED)) 2504 reg_call_notifier(wiphy, lr); 2505 return; 2506 } 2507 2508 lr->dfs_region = get_cfg80211_regdom()->dfs_region; 2509 2510 for (band = 0; band < NUM_NL80211_BANDS; band++) 2511 handle_band(wiphy, initiator, wiphy->bands[band]); 2512 2513 reg_process_beacons(wiphy); 2514 reg_process_ht_flags(wiphy); 2515 reg_call_notifier(wiphy, lr); 2516 } 2517 2518 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator) 2519 { 2520 struct cfg80211_registered_device *rdev; 2521 struct wiphy *wiphy; 2522 2523 ASSERT_RTNL(); 2524 2525 for_each_rdev(rdev) { 2526 wiphy = &rdev->wiphy; 2527 wiphy_update_regulatory(wiphy, initiator); 2528 } 2529 2530 reg_check_channels(); 2531 } 2532 2533 static void handle_channel_custom(struct wiphy *wiphy, 2534 struct ieee80211_channel *chan, 2535 const struct ieee80211_regdomain *regd, 2536 u32 min_bw) 2537 { 2538 u32 bw_flags = 0; 2539 const struct ieee80211_reg_rule *reg_rule = NULL; 2540 const struct ieee80211_power_rule *power_rule = NULL; 2541 u32 bw, center_freq_khz; 2542 2543 center_freq_khz = ieee80211_channel_to_khz(chan); 2544 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) { 2545 reg_rule = freq_reg_info_regd(center_freq_khz, regd, bw); 2546 if (!IS_ERR(reg_rule)) 2547 break; 2548 } 2549 2550 if (IS_ERR_OR_NULL(reg_rule)) { 2551 pr_debug("Disabling freq %d.%03d MHz as custom regd has no rule that fits it\n", 2552 chan->center_freq, chan->freq_offset); 2553 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) { 2554 chan->flags |= IEEE80211_CHAN_DISABLED; 2555 } else { 2556 chan->orig_flags |= IEEE80211_CHAN_DISABLED; 2557 chan->flags = chan->orig_flags; 2558 } 2559 return; 2560 } 2561 2562 power_rule = ®_rule->power_rule; 2563 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan); 2564 2565 chan->dfs_state_entered = jiffies; 2566 chan->dfs_state = NL80211_DFS_USABLE; 2567 2568 chan->beacon_found = false; 2569 2570 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) 2571 chan->flags = chan->orig_flags | bw_flags | 2572 map_regdom_flags(reg_rule->flags); 2573 else 2574 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags; 2575 2576 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain); 2577 chan->max_reg_power = chan->max_power = 2578 (int) MBM_TO_DBM(power_rule->max_eirp); 2579 2580 if (chan->flags & IEEE80211_CHAN_RADAR) { 2581 if (reg_rule->dfs_cac_ms) 2582 chan->dfs_cac_ms = reg_rule->dfs_cac_ms; 2583 else 2584 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; 2585 } 2586 2587 if (chan->flags & IEEE80211_CHAN_PSD) 2588 chan->psd = reg_rule->psd; 2589 2590 chan->max_power = chan->max_reg_power; 2591 } 2592 2593 static void handle_band_custom(struct wiphy *wiphy, 2594 struct ieee80211_supported_band *sband, 2595 const struct ieee80211_regdomain *regd) 2596 { 2597 unsigned int i; 2598 2599 if (!sband) 2600 return; 2601 2602 /* 2603 * We currently assume that you always want at least 20 MHz, 2604 * otherwise channel 12 might get enabled if this rule is 2605 * compatible to US, which permits 2402 - 2472 MHz. 2606 */ 2607 for (i = 0; i < sband->n_channels; i++) 2608 handle_channel_custom(wiphy, &sband->channels[i], regd, 2609 MHZ_TO_KHZ(20)); 2610 } 2611 2612 /* Used by drivers prior to wiphy registration */ 2613 void wiphy_apply_custom_regulatory(struct wiphy *wiphy, 2614 const struct ieee80211_regdomain *regd) 2615 { 2616 const struct ieee80211_regdomain *new_regd, *tmp; 2617 enum nl80211_band band; 2618 unsigned int bands_set = 0; 2619 2620 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG), 2621 "wiphy should have REGULATORY_CUSTOM_REG\n"); 2622 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG; 2623 2624 for (band = 0; band < NUM_NL80211_BANDS; band++) { 2625 if (!wiphy->bands[band]) 2626 continue; 2627 handle_band_custom(wiphy, wiphy->bands[band], regd); 2628 bands_set++; 2629 } 2630 2631 /* 2632 * no point in calling this if it won't have any effect 2633 * on your device's supported bands. 2634 */ 2635 WARN_ON(!bands_set); 2636 new_regd = reg_copy_regd(regd); 2637 if (IS_ERR(new_regd)) 2638 return; 2639 2640 rtnl_lock(); 2641 scoped_guard(wiphy, wiphy) { 2642 tmp = get_wiphy_regdom(wiphy); 2643 rcu_assign_pointer(wiphy->regd, new_regd); 2644 rcu_free_regdom(tmp); 2645 } 2646 rtnl_unlock(); 2647 } 2648 EXPORT_SYMBOL(wiphy_apply_custom_regulatory); 2649 2650 static void reg_set_request_processed(void) 2651 { 2652 bool need_more_processing = false; 2653 struct regulatory_request *lr = get_last_request(); 2654 2655 lr->processed = true; 2656 2657 spin_lock(®_requests_lock); 2658 if (!list_empty(®_requests_list)) 2659 need_more_processing = true; 2660 spin_unlock(®_requests_lock); 2661 2662 cancel_crda_timeout(); 2663 2664 if (need_more_processing) 2665 schedule_work(®_work); 2666 } 2667 2668 /** 2669 * reg_process_hint_core - process core regulatory requests 2670 * @core_request: a pending core regulatory request 2671 * 2672 * The wireless subsystem can use this function to process 2673 * a regulatory request issued by the regulatory core. 2674 * 2675 * Returns: %REG_REQ_OK or %REG_REQ_IGNORE, indicating if the 2676 * hint was processed or ignored 2677 */ 2678 static enum reg_request_treatment 2679 reg_process_hint_core(struct regulatory_request *core_request) 2680 { 2681 if (reg_query_database(core_request)) { 2682 core_request->intersect = false; 2683 core_request->processed = false; 2684 reg_update_last_request(core_request); 2685 return REG_REQ_OK; 2686 } 2687 2688 return REG_REQ_IGNORE; 2689 } 2690 2691 static enum reg_request_treatment 2692 __reg_process_hint_user(struct regulatory_request *user_request) 2693 { 2694 struct regulatory_request *lr = get_last_request(); 2695 2696 if (reg_request_cell_base(user_request)) 2697 return reg_ignore_cell_hint(user_request); 2698 2699 if (reg_request_cell_base(lr)) 2700 return REG_REQ_IGNORE; 2701 2702 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) 2703 return REG_REQ_INTERSECT; 2704 /* 2705 * If the user knows better the user should set the regdom 2706 * to their country before the IE is picked up 2707 */ 2708 if (lr->initiator == NL80211_REGDOM_SET_BY_USER && 2709 lr->intersect) 2710 return REG_REQ_IGNORE; 2711 /* 2712 * Process user requests only after previous user/driver/core 2713 * requests have been processed 2714 */ 2715 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE || 2716 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER || 2717 lr->initiator == NL80211_REGDOM_SET_BY_USER) && 2718 regdom_changes(lr->alpha2)) 2719 return REG_REQ_IGNORE; 2720 2721 if (!regdom_changes(user_request->alpha2)) 2722 return REG_REQ_ALREADY_SET; 2723 2724 return REG_REQ_OK; 2725 } 2726 2727 /** 2728 * reg_process_hint_user - process user regulatory requests 2729 * @user_request: a pending user regulatory request 2730 * 2731 * The wireless subsystem can use this function to process 2732 * a regulatory request initiated by userspace. 2733 * 2734 * Returns: %REG_REQ_OK or %REG_REQ_IGNORE, indicating if the 2735 * hint was processed or ignored 2736 */ 2737 static enum reg_request_treatment 2738 reg_process_hint_user(struct regulatory_request *user_request) 2739 { 2740 enum reg_request_treatment treatment; 2741 2742 treatment = __reg_process_hint_user(user_request); 2743 if (treatment == REG_REQ_IGNORE || 2744 treatment == REG_REQ_ALREADY_SET) 2745 return REG_REQ_IGNORE; 2746 2747 user_request->intersect = treatment == REG_REQ_INTERSECT; 2748 user_request->processed = false; 2749 2750 if (reg_query_database(user_request)) { 2751 reg_update_last_request(user_request); 2752 user_alpha2[0] = user_request->alpha2[0]; 2753 user_alpha2[1] = user_request->alpha2[1]; 2754 return REG_REQ_OK; 2755 } 2756 2757 return REG_REQ_IGNORE; 2758 } 2759 2760 static enum reg_request_treatment 2761 __reg_process_hint_driver(struct regulatory_request *driver_request) 2762 { 2763 struct regulatory_request *lr = get_last_request(); 2764 2765 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) { 2766 if (regdom_changes(driver_request->alpha2)) 2767 return REG_REQ_OK; 2768 return REG_REQ_ALREADY_SET; 2769 } 2770 2771 /* 2772 * This would happen if you unplug and plug your card 2773 * back in or if you add a new device for which the previously 2774 * loaded card also agrees on the regulatory domain. 2775 */ 2776 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && 2777 !regdom_changes(driver_request->alpha2)) 2778 return REG_REQ_ALREADY_SET; 2779 2780 return REG_REQ_INTERSECT; 2781 } 2782 2783 /** 2784 * reg_process_hint_driver - process driver regulatory requests 2785 * @wiphy: the wireless device for the regulatory request 2786 * @driver_request: a pending driver regulatory request 2787 * 2788 * The wireless subsystem can use this function to process 2789 * a regulatory request issued by an 802.11 driver. 2790 * 2791 * Returns: one of the different reg request treatment values. 2792 */ 2793 static enum reg_request_treatment 2794 reg_process_hint_driver(struct wiphy *wiphy, 2795 struct regulatory_request *driver_request) 2796 { 2797 const struct ieee80211_regdomain *regd, *tmp; 2798 enum reg_request_treatment treatment; 2799 2800 treatment = __reg_process_hint_driver(driver_request); 2801 2802 switch (treatment) { 2803 case REG_REQ_OK: 2804 break; 2805 case REG_REQ_IGNORE: 2806 return REG_REQ_IGNORE; 2807 case REG_REQ_INTERSECT: 2808 case REG_REQ_ALREADY_SET: 2809 regd = reg_copy_regd(get_cfg80211_regdom()); 2810 if (IS_ERR(regd)) 2811 return REG_REQ_IGNORE; 2812 2813 tmp = get_wiphy_regdom(wiphy); 2814 ASSERT_RTNL(); 2815 scoped_guard(wiphy, wiphy) { 2816 rcu_assign_pointer(wiphy->regd, regd); 2817 } 2818 rcu_free_regdom(tmp); 2819 } 2820 2821 2822 driver_request->intersect = treatment == REG_REQ_INTERSECT; 2823 driver_request->processed = false; 2824 2825 /* 2826 * Since CRDA will not be called in this case as we already 2827 * have applied the requested regulatory domain before we just 2828 * inform userspace we have processed the request 2829 */ 2830 if (treatment == REG_REQ_ALREADY_SET) { 2831 nl80211_send_reg_change_event(driver_request); 2832 reg_update_last_request(driver_request); 2833 reg_set_request_processed(); 2834 return REG_REQ_ALREADY_SET; 2835 } 2836 2837 if (reg_query_database(driver_request)) { 2838 reg_update_last_request(driver_request); 2839 return REG_REQ_OK; 2840 } 2841 2842 return REG_REQ_IGNORE; 2843 } 2844 2845 static enum reg_request_treatment 2846 __reg_process_hint_country_ie(struct wiphy *wiphy, 2847 struct regulatory_request *country_ie_request) 2848 { 2849 struct wiphy *last_wiphy = NULL; 2850 struct regulatory_request *lr = get_last_request(); 2851 2852 if (reg_request_cell_base(lr)) { 2853 /* Trust a Cell base station over the AP's country IE */ 2854 if (regdom_changes(country_ie_request->alpha2)) 2855 return REG_REQ_IGNORE; 2856 return REG_REQ_ALREADY_SET; 2857 } else { 2858 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE) 2859 return REG_REQ_IGNORE; 2860 } 2861 2862 if (unlikely(!is_an_alpha2(country_ie_request->alpha2))) 2863 return -EINVAL; 2864 2865 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) 2866 return REG_REQ_OK; 2867 2868 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); 2869 2870 if (last_wiphy != wiphy) { 2871 /* 2872 * Two cards with two APs claiming different 2873 * Country IE alpha2s. We could 2874 * intersect them, but that seems unlikely 2875 * to be correct. Reject second one for now. 2876 */ 2877 if (regdom_changes(country_ie_request->alpha2)) 2878 return REG_REQ_IGNORE; 2879 return REG_REQ_ALREADY_SET; 2880 } 2881 2882 if (regdom_changes(country_ie_request->alpha2)) 2883 return REG_REQ_OK; 2884 return REG_REQ_ALREADY_SET; 2885 } 2886 2887 /** 2888 * reg_process_hint_country_ie - process regulatory requests from country IEs 2889 * @wiphy: the wireless device for the regulatory request 2890 * @country_ie_request: a regulatory request from a country IE 2891 * 2892 * The wireless subsystem can use this function to process 2893 * a regulatory request issued by a country Information Element. 2894 * 2895 * Returns: one of the different reg request treatment values. 2896 */ 2897 static enum reg_request_treatment 2898 reg_process_hint_country_ie(struct wiphy *wiphy, 2899 struct regulatory_request *country_ie_request) 2900 { 2901 enum reg_request_treatment treatment; 2902 2903 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request); 2904 2905 switch (treatment) { 2906 case REG_REQ_OK: 2907 break; 2908 case REG_REQ_IGNORE: 2909 return REG_REQ_IGNORE; 2910 case REG_REQ_ALREADY_SET: 2911 reg_free_request(country_ie_request); 2912 return REG_REQ_ALREADY_SET; 2913 case REG_REQ_INTERSECT: 2914 /* 2915 * This doesn't happen yet, not sure we 2916 * ever want to support it for this case. 2917 */ 2918 WARN_ONCE(1, "Unexpected intersection for country elements"); 2919 return REG_REQ_IGNORE; 2920 } 2921 2922 country_ie_request->intersect = false; 2923 country_ie_request->processed = false; 2924 2925 if (reg_query_database(country_ie_request)) { 2926 reg_update_last_request(country_ie_request); 2927 return REG_REQ_OK; 2928 } 2929 2930 return REG_REQ_IGNORE; 2931 } 2932 2933 bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2) 2934 { 2935 const struct ieee80211_regdomain *wiphy1_regd = NULL; 2936 const struct ieee80211_regdomain *wiphy2_regd = NULL; 2937 const struct ieee80211_regdomain *cfg80211_regd = NULL; 2938 bool dfs_domain_same; 2939 2940 rcu_read_lock(); 2941 2942 cfg80211_regd = rcu_dereference(cfg80211_regdomain); 2943 wiphy1_regd = rcu_dereference(wiphy1->regd); 2944 if (!wiphy1_regd) 2945 wiphy1_regd = cfg80211_regd; 2946 2947 wiphy2_regd = rcu_dereference(wiphy2->regd); 2948 if (!wiphy2_regd) 2949 wiphy2_regd = cfg80211_regd; 2950 2951 dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region; 2952 2953 rcu_read_unlock(); 2954 2955 return dfs_domain_same; 2956 } 2957 2958 static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan, 2959 struct ieee80211_channel *src_chan) 2960 { 2961 if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) || 2962 !(src_chan->flags & IEEE80211_CHAN_RADAR)) 2963 return; 2964 2965 if (dst_chan->flags & IEEE80211_CHAN_DISABLED || 2966 src_chan->flags & IEEE80211_CHAN_DISABLED) 2967 return; 2968 2969 if (src_chan->center_freq == dst_chan->center_freq && 2970 dst_chan->dfs_state == NL80211_DFS_USABLE) { 2971 dst_chan->dfs_state = src_chan->dfs_state; 2972 dst_chan->dfs_state_entered = src_chan->dfs_state_entered; 2973 } 2974 } 2975 2976 static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy, 2977 struct wiphy *src_wiphy) 2978 { 2979 struct ieee80211_supported_band *src_sband, *dst_sband; 2980 struct ieee80211_channel *src_chan, *dst_chan; 2981 int i, j, band; 2982 2983 if (!reg_dfs_domain_same(dst_wiphy, src_wiphy)) 2984 return; 2985 2986 for (band = 0; band < NUM_NL80211_BANDS; band++) { 2987 dst_sband = dst_wiphy->bands[band]; 2988 src_sband = src_wiphy->bands[band]; 2989 if (!dst_sband || !src_sband) 2990 continue; 2991 2992 for (i = 0; i < dst_sband->n_channels; i++) { 2993 dst_chan = &dst_sband->channels[i]; 2994 for (j = 0; j < src_sband->n_channels; j++) { 2995 src_chan = &src_sband->channels[j]; 2996 reg_copy_dfs_chan_state(dst_chan, src_chan); 2997 } 2998 } 2999 } 3000 } 3001 3002 static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy) 3003 { 3004 struct cfg80211_registered_device *rdev; 3005 3006 ASSERT_RTNL(); 3007 3008 for_each_rdev(rdev) { 3009 if (wiphy == &rdev->wiphy) 3010 continue; 3011 wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy); 3012 } 3013 } 3014 3015 /* This processes *all* regulatory hints */ 3016 static void reg_process_hint(struct regulatory_request *reg_request) 3017 { 3018 struct wiphy *wiphy = NULL; 3019 enum reg_request_treatment treatment; 3020 enum nl80211_reg_initiator initiator = reg_request->initiator; 3021 3022 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID) 3023 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx); 3024 3025 switch (initiator) { 3026 case NL80211_REGDOM_SET_BY_CORE: 3027 treatment = reg_process_hint_core(reg_request); 3028 break; 3029 case NL80211_REGDOM_SET_BY_USER: 3030 treatment = reg_process_hint_user(reg_request); 3031 break; 3032 case NL80211_REGDOM_SET_BY_DRIVER: 3033 if (!wiphy) 3034 goto out_free; 3035 treatment = reg_process_hint_driver(wiphy, reg_request); 3036 break; 3037 case NL80211_REGDOM_SET_BY_COUNTRY_IE: 3038 if (!wiphy) 3039 goto out_free; 3040 treatment = reg_process_hint_country_ie(wiphy, reg_request); 3041 break; 3042 default: 3043 WARN(1, "invalid initiator %d\n", initiator); 3044 goto out_free; 3045 } 3046 3047 if (treatment == REG_REQ_IGNORE) 3048 goto out_free; 3049 3050 WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET, 3051 "unexpected treatment value %d\n", treatment); 3052 3053 /* This is required so that the orig_* parameters are saved. 3054 * NOTE: treatment must be set for any case that reaches here! 3055 */ 3056 if (treatment == REG_REQ_ALREADY_SET && wiphy && 3057 wiphy->regulatory_flags & REGULATORY_STRICT_REG) { 3058 wiphy_update_regulatory(wiphy, initiator); 3059 wiphy_all_share_dfs_chan_state(wiphy); 3060 reg_check_channels(); 3061 } 3062 3063 return; 3064 3065 out_free: 3066 reg_free_request(reg_request); 3067 } 3068 3069 static void notify_self_managed_wiphys(struct regulatory_request *request) 3070 { 3071 struct cfg80211_registered_device *rdev; 3072 struct wiphy *wiphy; 3073 3074 for_each_rdev(rdev) { 3075 wiphy = &rdev->wiphy; 3076 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED && 3077 request->initiator == NL80211_REGDOM_SET_BY_USER) 3078 reg_call_notifier(wiphy, request); 3079 } 3080 } 3081 3082 /* 3083 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* 3084 * Regulatory hints come on a first come first serve basis and we 3085 * must process each one atomically. 3086 */ 3087 static void reg_process_pending_hints(void) 3088 { 3089 struct regulatory_request *reg_request, *lr; 3090 3091 lr = get_last_request(); 3092 3093 /* When last_request->processed becomes true this will be rescheduled */ 3094 if (lr && !lr->processed) { 3095 pr_debug("Pending regulatory request, waiting for it to be processed...\n"); 3096 return; 3097 } 3098 3099 spin_lock(®_requests_lock); 3100 3101 if (list_empty(®_requests_list)) { 3102 spin_unlock(®_requests_lock); 3103 return; 3104 } 3105 3106 reg_request = list_first_entry(®_requests_list, 3107 struct regulatory_request, 3108 list); 3109 list_del_init(®_request->list); 3110 3111 spin_unlock(®_requests_lock); 3112 3113 notify_self_managed_wiphys(reg_request); 3114 3115 reg_process_hint(reg_request); 3116 3117 lr = get_last_request(); 3118 3119 spin_lock(®_requests_lock); 3120 if (!list_empty(®_requests_list) && lr && lr->processed) 3121 schedule_work(®_work); 3122 spin_unlock(®_requests_lock); 3123 } 3124 3125 /* Processes beacon hints -- this has nothing to do with country IEs */ 3126 static void reg_process_pending_beacon_hints(void) 3127 { 3128 struct cfg80211_registered_device *rdev; 3129 struct reg_beacon *pending_beacon, *tmp; 3130 3131 /* This goes through the _pending_ beacon list */ 3132 spin_lock_bh(®_pending_beacons_lock); 3133 3134 list_for_each_entry_safe(pending_beacon, tmp, 3135 ®_pending_beacons, list) { 3136 list_del_init(&pending_beacon->list); 3137 3138 /* Applies the beacon hint to current wiphys */ 3139 for_each_rdev(rdev) 3140 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon); 3141 3142 /* Remembers the beacon hint for new wiphys or reg changes */ 3143 list_add_tail(&pending_beacon->list, ®_beacon_list); 3144 } 3145 3146 spin_unlock_bh(®_pending_beacons_lock); 3147 } 3148 3149 static void reg_process_self_managed_hint(struct wiphy *wiphy) 3150 { 3151 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 3152 const struct ieee80211_regdomain *tmp; 3153 const struct ieee80211_regdomain *regd; 3154 enum nl80211_band band; 3155 struct regulatory_request request = {}; 3156 3157 ASSERT_RTNL(); 3158 lockdep_assert_wiphy(wiphy); 3159 3160 spin_lock(®_requests_lock); 3161 regd = rdev->requested_regd; 3162 rdev->requested_regd = NULL; 3163 spin_unlock(®_requests_lock); 3164 3165 if (!regd) 3166 return; 3167 3168 tmp = get_wiphy_regdom(wiphy); 3169 rcu_assign_pointer(wiphy->regd, regd); 3170 rcu_free_regdom(tmp); 3171 3172 for (band = 0; band < NUM_NL80211_BANDS; band++) 3173 handle_band_custom(wiphy, wiphy->bands[band], regd); 3174 3175 reg_process_ht_flags(wiphy); 3176 3177 request.wiphy_idx = get_wiphy_idx(wiphy); 3178 request.alpha2[0] = regd->alpha2[0]; 3179 request.alpha2[1] = regd->alpha2[1]; 3180 request.initiator = NL80211_REGDOM_SET_BY_DRIVER; 3181 3182 if (wiphy->flags & WIPHY_FLAG_NOTIFY_REGDOM_BY_DRIVER) 3183 reg_call_notifier(wiphy, &request); 3184 3185 nl80211_send_wiphy_reg_change_event(&request); 3186 } 3187 3188 static void reg_process_self_managed_hints(void) 3189 { 3190 struct cfg80211_registered_device *rdev; 3191 3192 ASSERT_RTNL(); 3193 3194 for_each_rdev(rdev) { 3195 guard(wiphy)(&rdev->wiphy); 3196 3197 reg_process_self_managed_hint(&rdev->wiphy); 3198 } 3199 3200 reg_check_channels(); 3201 } 3202 3203 static void reg_todo(struct work_struct *work) 3204 { 3205 rtnl_lock(); 3206 reg_process_pending_hints(); 3207 reg_process_pending_beacon_hints(); 3208 reg_process_self_managed_hints(); 3209 rtnl_unlock(); 3210 } 3211 3212 static void queue_regulatory_request(struct regulatory_request *request) 3213 { 3214 request->alpha2[0] = toupper(request->alpha2[0]); 3215 request->alpha2[1] = toupper(request->alpha2[1]); 3216 3217 spin_lock(®_requests_lock); 3218 list_add_tail(&request->list, ®_requests_list); 3219 spin_unlock(®_requests_lock); 3220 3221 schedule_work(®_work); 3222 } 3223 3224 /* 3225 * Core regulatory hint -- happens during cfg80211_init() 3226 * and when we restore regulatory settings. 3227 */ 3228 static int regulatory_hint_core(const char *alpha2) 3229 { 3230 struct regulatory_request *request; 3231 3232 request = kzalloc_obj(struct regulatory_request); 3233 if (!request) 3234 return -ENOMEM; 3235 3236 request->alpha2[0] = alpha2[0]; 3237 request->alpha2[1] = alpha2[1]; 3238 request->initiator = NL80211_REGDOM_SET_BY_CORE; 3239 request->wiphy_idx = WIPHY_IDX_INVALID; 3240 3241 queue_regulatory_request(request); 3242 3243 return 0; 3244 } 3245 3246 /* User hints */ 3247 int regulatory_hint_user(const char *alpha2, 3248 enum nl80211_user_reg_hint_type user_reg_hint_type) 3249 { 3250 struct regulatory_request *request; 3251 3252 if (WARN_ON(!alpha2)) 3253 return -EINVAL; 3254 3255 if (!is_world_regdom(alpha2) && !is_an_alpha2(alpha2)) 3256 return -EINVAL; 3257 3258 request = kzalloc_obj(struct regulatory_request); 3259 if (!request) 3260 return -ENOMEM; 3261 3262 request->wiphy_idx = WIPHY_IDX_INVALID; 3263 request->alpha2[0] = alpha2[0]; 3264 request->alpha2[1] = alpha2[1]; 3265 request->initiator = NL80211_REGDOM_SET_BY_USER; 3266 request->user_reg_hint_type = user_reg_hint_type; 3267 3268 /* Allow calling CRDA again */ 3269 reset_crda_timeouts(); 3270 3271 queue_regulatory_request(request); 3272 3273 return 0; 3274 } 3275 3276 void regulatory_hint_indoor(bool is_indoor, u32 portid) 3277 { 3278 spin_lock(®_indoor_lock); 3279 3280 /* It is possible that more than one user space process is trying to 3281 * configure the indoor setting. To handle such cases, clear the indoor 3282 * setting in case that some process does not think that the device 3283 * is operating in an indoor environment. In addition, if a user space 3284 * process indicates that it is controlling the indoor setting, save its 3285 * portid, i.e., make it the owner. 3286 */ 3287 reg_is_indoor = is_indoor; 3288 if (reg_is_indoor) { 3289 if (!reg_is_indoor_portid) 3290 reg_is_indoor_portid = portid; 3291 } else { 3292 reg_is_indoor_portid = 0; 3293 } 3294 3295 spin_unlock(®_indoor_lock); 3296 3297 if (!is_indoor) 3298 reg_check_channels(); 3299 } 3300 3301 void regulatory_netlink_notify(u32 portid) 3302 { 3303 spin_lock(®_indoor_lock); 3304 3305 if (reg_is_indoor_portid != portid) { 3306 spin_unlock(®_indoor_lock); 3307 return; 3308 } 3309 3310 reg_is_indoor = false; 3311 reg_is_indoor_portid = 0; 3312 3313 spin_unlock(®_indoor_lock); 3314 3315 reg_check_channels(); 3316 } 3317 3318 /* Driver hints */ 3319 int regulatory_hint(struct wiphy *wiphy, const char *alpha2) 3320 { 3321 struct regulatory_request *request; 3322 3323 if (WARN_ON(!alpha2 || !wiphy)) 3324 return -EINVAL; 3325 3326 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG; 3327 3328 request = kzalloc_obj(struct regulatory_request); 3329 if (!request) 3330 return -ENOMEM; 3331 3332 request->wiphy_idx = get_wiphy_idx(wiphy); 3333 3334 request->alpha2[0] = alpha2[0]; 3335 request->alpha2[1] = alpha2[1]; 3336 request->initiator = NL80211_REGDOM_SET_BY_DRIVER; 3337 3338 /* Allow calling CRDA again */ 3339 reset_crda_timeouts(); 3340 3341 queue_regulatory_request(request); 3342 3343 return 0; 3344 } 3345 EXPORT_SYMBOL(regulatory_hint); 3346 3347 void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band, 3348 const u8 *country_ie, u8 country_ie_len) 3349 { 3350 char alpha2[2]; 3351 enum environment_cap env = ENVIRON_ANY; 3352 struct regulatory_request *request = NULL, *lr; 3353 3354 /* IE len must be evenly divisible by 2 */ 3355 if (country_ie_len & 0x01) 3356 return; 3357 3358 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) 3359 return; 3360 3361 request = kzalloc_obj(*request); 3362 if (!request) 3363 return; 3364 3365 alpha2[0] = country_ie[0]; 3366 alpha2[1] = country_ie[1]; 3367 3368 if (country_ie[2] == 'I') 3369 env = ENVIRON_INDOOR; 3370 else if (country_ie[2] == 'O') 3371 env = ENVIRON_OUTDOOR; 3372 3373 rcu_read_lock(); 3374 lr = get_last_request(); 3375 3376 if (unlikely(!lr)) 3377 goto out; 3378 3379 /* 3380 * We will run this only upon a successful connection on cfg80211. 3381 * We leave conflict resolution to the workqueue, where can hold 3382 * the RTNL. 3383 */ 3384 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && 3385 lr->wiphy_idx != WIPHY_IDX_INVALID) 3386 goto out; 3387 3388 request->wiphy_idx = get_wiphy_idx(wiphy); 3389 request->alpha2[0] = alpha2[0]; 3390 request->alpha2[1] = alpha2[1]; 3391 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE; 3392 request->country_ie_env = env; 3393 3394 /* Allow calling CRDA again */ 3395 reset_crda_timeouts(); 3396 3397 queue_regulatory_request(request); 3398 request = NULL; 3399 out: 3400 kfree(request); 3401 rcu_read_unlock(); 3402 } 3403 3404 static void restore_alpha2(char *alpha2, bool reset_user) 3405 { 3406 /* indicates there is no alpha2 to consider for restoration */ 3407 alpha2[0] = '9'; 3408 alpha2[1] = '7'; 3409 3410 /* The user setting has precedence over the module parameter */ 3411 if (is_user_regdom_saved()) { 3412 /* Unless we're asked to ignore it and reset it */ 3413 if (reset_user) { 3414 pr_debug("Restoring regulatory settings including user preference\n"); 3415 user_alpha2[0] = '9'; 3416 user_alpha2[1] = '7'; 3417 3418 /* 3419 * If we're ignoring user settings, we still need to 3420 * check the module parameter to ensure we put things 3421 * back as they were for a full restore. 3422 */ 3423 if (!is_world_regdom(ieee80211_regdom)) { 3424 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n", 3425 ieee80211_regdom[0], ieee80211_regdom[1]); 3426 alpha2[0] = ieee80211_regdom[0]; 3427 alpha2[1] = ieee80211_regdom[1]; 3428 } 3429 } else { 3430 pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n", 3431 user_alpha2[0], user_alpha2[1]); 3432 alpha2[0] = user_alpha2[0]; 3433 alpha2[1] = user_alpha2[1]; 3434 } 3435 } else if (!is_world_regdom(ieee80211_regdom)) { 3436 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n", 3437 ieee80211_regdom[0], ieee80211_regdom[1]); 3438 alpha2[0] = ieee80211_regdom[0]; 3439 alpha2[1] = ieee80211_regdom[1]; 3440 } else 3441 pr_debug("Restoring regulatory settings\n"); 3442 } 3443 3444 static void restore_custom_reg_settings(struct wiphy *wiphy) 3445 { 3446 struct ieee80211_supported_band *sband; 3447 enum nl80211_band band; 3448 struct ieee80211_channel *chan; 3449 int i; 3450 3451 for (band = 0; band < NUM_NL80211_BANDS; band++) { 3452 sband = wiphy->bands[band]; 3453 if (!sband) 3454 continue; 3455 for (i = 0; i < sband->n_channels; i++) { 3456 chan = &sband->channels[i]; 3457 chan->flags = chan->orig_flags; 3458 chan->max_antenna_gain = chan->orig_mag; 3459 chan->max_power = chan->orig_mpwr; 3460 chan->beacon_found = false; 3461 } 3462 } 3463 } 3464 3465 /* 3466 * Restoring regulatory settings involves ignoring any 3467 * possibly stale country IE information and user regulatory 3468 * settings if so desired, this includes any beacon hints 3469 * learned as we could have traveled outside to another country 3470 * after disconnection. To restore regulatory settings we do 3471 * exactly what we did at bootup: 3472 * 3473 * - send a core regulatory hint 3474 * - send a user regulatory hint if applicable 3475 * 3476 * Device drivers that send a regulatory hint for a specific country 3477 * keep their own regulatory domain on wiphy->regd so that does 3478 * not need to be remembered. 3479 */ 3480 static void restore_regulatory_settings(bool reset_user, bool cached) 3481 { 3482 char alpha2[2]; 3483 char world_alpha2[2]; 3484 struct reg_beacon *reg_beacon, *btmp; 3485 LIST_HEAD(tmp_reg_req_list); 3486 struct cfg80211_registered_device *rdev; 3487 3488 ASSERT_RTNL(); 3489 3490 /* 3491 * Clear the indoor setting in case that it is not controlled by user 3492 * space, as otherwise there is no guarantee that the device is still 3493 * operating in an indoor environment. 3494 */ 3495 spin_lock(®_indoor_lock); 3496 if (reg_is_indoor && !reg_is_indoor_portid) { 3497 reg_is_indoor = false; 3498 reg_check_channels(); 3499 } 3500 spin_unlock(®_indoor_lock); 3501 3502 reset_regdomains(true, &world_regdom); 3503 restore_alpha2(alpha2, reset_user); 3504 3505 /* 3506 * If there's any pending requests we simply 3507 * stash them to a temporary pending queue and 3508 * add then after we've restored regulatory 3509 * settings. 3510 */ 3511 spin_lock(®_requests_lock); 3512 list_splice_tail_init(®_requests_list, &tmp_reg_req_list); 3513 spin_unlock(®_requests_lock); 3514 3515 /* Clear beacon hints */ 3516 spin_lock_bh(®_pending_beacons_lock); 3517 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) { 3518 list_del(®_beacon->list); 3519 kfree(reg_beacon); 3520 } 3521 spin_unlock_bh(®_pending_beacons_lock); 3522 3523 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) { 3524 list_del(®_beacon->list); 3525 kfree(reg_beacon); 3526 } 3527 3528 /* First restore to the basic regulatory settings */ 3529 world_alpha2[0] = cfg80211_world_regdom->alpha2[0]; 3530 world_alpha2[1] = cfg80211_world_regdom->alpha2[1]; 3531 3532 for_each_rdev(rdev) { 3533 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) 3534 continue; 3535 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG) 3536 restore_custom_reg_settings(&rdev->wiphy); 3537 } 3538 3539 if (cached && (!is_an_alpha2(alpha2) || 3540 !IS_ERR_OR_NULL(cfg80211_user_regdom))) { 3541 reset_regdomains(false, cfg80211_world_regdom); 3542 update_all_wiphy_regulatory(NL80211_REGDOM_SET_BY_CORE); 3543 print_regdomain(get_cfg80211_regdom()); 3544 nl80211_send_reg_change_event(&core_request_world); 3545 reg_set_request_processed(); 3546 3547 if (is_an_alpha2(alpha2) && 3548 !regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER)) { 3549 struct regulatory_request *ureq; 3550 3551 spin_lock(®_requests_lock); 3552 ureq = list_last_entry(®_requests_list, 3553 struct regulatory_request, 3554 list); 3555 list_del(&ureq->list); 3556 spin_unlock(®_requests_lock); 3557 3558 notify_self_managed_wiphys(ureq); 3559 reg_update_last_request(ureq); 3560 set_regdom(reg_copy_regd(cfg80211_user_regdom), 3561 REGD_SOURCE_CACHED); 3562 } 3563 } else { 3564 regulatory_hint_core(world_alpha2); 3565 3566 /* 3567 * This restores the ieee80211_regdom module parameter 3568 * preference or the last user requested regulatory 3569 * settings, user regulatory settings takes precedence. 3570 */ 3571 if (is_an_alpha2(alpha2)) 3572 regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER); 3573 } 3574 3575 spin_lock(®_requests_lock); 3576 list_splice_tail_init(&tmp_reg_req_list, ®_requests_list); 3577 spin_unlock(®_requests_lock); 3578 3579 pr_debug("Kicking the queue\n"); 3580 3581 schedule_work(®_work); 3582 } 3583 3584 static bool is_wiphy_all_set_reg_flag(enum ieee80211_regulatory_flags flag) 3585 { 3586 struct cfg80211_registered_device *rdev; 3587 struct wireless_dev *wdev; 3588 3589 for_each_rdev(rdev) { 3590 guard(wiphy)(&rdev->wiphy); 3591 3592 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 3593 if (!(wdev->wiphy->regulatory_flags & flag)) 3594 return false; 3595 } 3596 } 3597 3598 return true; 3599 } 3600 3601 void regulatory_hint_disconnect(void) 3602 { 3603 /* Restore of regulatory settings is not required when wiphy(s) 3604 * ignore IE from connected access point but clearance of beacon hints 3605 * is required when wiphy(s) supports beacon hints. 3606 */ 3607 if (is_wiphy_all_set_reg_flag(REGULATORY_COUNTRY_IE_IGNORE)) { 3608 struct reg_beacon *reg_beacon, *btmp; 3609 3610 if (is_wiphy_all_set_reg_flag(REGULATORY_DISABLE_BEACON_HINTS)) 3611 return; 3612 3613 spin_lock_bh(®_pending_beacons_lock); 3614 list_for_each_entry_safe(reg_beacon, btmp, 3615 ®_pending_beacons, list) { 3616 list_del(®_beacon->list); 3617 kfree(reg_beacon); 3618 } 3619 spin_unlock_bh(®_pending_beacons_lock); 3620 3621 list_for_each_entry_safe(reg_beacon, btmp, 3622 ®_beacon_list, list) { 3623 list_del(®_beacon->list); 3624 kfree(reg_beacon); 3625 } 3626 3627 return; 3628 } 3629 3630 pr_debug("All devices are disconnected, going to restore regulatory settings\n"); 3631 restore_regulatory_settings(false, true); 3632 } 3633 3634 static bool freq_is_chan_12_13_14(u32 freq) 3635 { 3636 if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) || 3637 freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) || 3638 freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ)) 3639 return true; 3640 return false; 3641 } 3642 3643 static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan) 3644 { 3645 struct reg_beacon *pending_beacon; 3646 3647 list_for_each_entry(pending_beacon, ®_pending_beacons, list) 3648 if (ieee80211_channel_equal(beacon_chan, 3649 &pending_beacon->chan)) 3650 return true; 3651 return false; 3652 } 3653 3654 void regulatory_hint_found_beacon(struct wiphy *wiphy, 3655 struct ieee80211_channel *beacon_chan, 3656 gfp_t gfp) 3657 { 3658 struct reg_beacon *reg_beacon; 3659 bool processing; 3660 3661 if (beacon_chan->beacon_found || 3662 beacon_chan->flags & IEEE80211_CHAN_RADAR || 3663 (beacon_chan->band == NL80211_BAND_2GHZ && 3664 !freq_is_chan_12_13_14(beacon_chan->center_freq))) 3665 return; 3666 3667 spin_lock_bh(®_pending_beacons_lock); 3668 processing = pending_reg_beacon(beacon_chan); 3669 spin_unlock_bh(®_pending_beacons_lock); 3670 3671 if (processing) 3672 return; 3673 3674 reg_beacon = kzalloc_obj(struct reg_beacon, gfp); 3675 if (!reg_beacon) 3676 return; 3677 3678 pr_debug("Found new beacon on frequency: %d.%03d MHz (Ch %d) on %s\n", 3679 beacon_chan->center_freq, beacon_chan->freq_offset, 3680 ieee80211_freq_khz_to_channel( 3681 ieee80211_channel_to_khz(beacon_chan)), 3682 wiphy_name(wiphy)); 3683 3684 memcpy(®_beacon->chan, beacon_chan, 3685 sizeof(struct ieee80211_channel)); 3686 3687 /* 3688 * Since we can be called from BH or and non-BH context 3689 * we must use spin_lock_bh() 3690 */ 3691 spin_lock_bh(®_pending_beacons_lock); 3692 list_add_tail(®_beacon->list, ®_pending_beacons); 3693 spin_unlock_bh(®_pending_beacons_lock); 3694 3695 schedule_work(®_work); 3696 } 3697 3698 static void print_rd_rules(const struct ieee80211_regdomain *rd) 3699 { 3700 unsigned int i; 3701 const struct ieee80211_reg_rule *reg_rule = NULL; 3702 const struct ieee80211_freq_range *freq_range = NULL; 3703 const struct ieee80211_power_rule *power_rule = NULL; 3704 char bw[32], cac_time[32]; 3705 3706 pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n"); 3707 3708 for (i = 0; i < rd->n_reg_rules; i++) { 3709 reg_rule = &rd->reg_rules[i]; 3710 freq_range = ®_rule->freq_range; 3711 power_rule = ®_rule->power_rule; 3712 3713 if (reg_rule->flags & NL80211_RRF_AUTO_BW) 3714 snprintf(bw, sizeof(bw), "%d KHz, %u KHz AUTO", 3715 freq_range->max_bandwidth_khz, 3716 reg_get_max_bandwidth(rd, reg_rule)); 3717 else 3718 snprintf(bw, sizeof(bw), "%d KHz", 3719 freq_range->max_bandwidth_khz); 3720 3721 if (reg_rule->flags & NL80211_RRF_DFS) 3722 scnprintf(cac_time, sizeof(cac_time), "%u s", 3723 reg_rule->dfs_cac_ms/1000); 3724 else 3725 scnprintf(cac_time, sizeof(cac_time), "N/A"); 3726 3727 3728 /* 3729 * There may not be documentation for max antenna gain 3730 * in certain regions 3731 */ 3732 if (power_rule->max_antenna_gain) 3733 pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n", 3734 freq_range->start_freq_khz, 3735 freq_range->end_freq_khz, 3736 bw, 3737 power_rule->max_antenna_gain, 3738 power_rule->max_eirp, 3739 cac_time); 3740 else 3741 pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n", 3742 freq_range->start_freq_khz, 3743 freq_range->end_freq_khz, 3744 bw, 3745 power_rule->max_eirp, 3746 cac_time); 3747 } 3748 } 3749 3750 bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region) 3751 { 3752 switch (dfs_region) { 3753 case NL80211_DFS_UNSET: 3754 case NL80211_DFS_FCC: 3755 case NL80211_DFS_ETSI: 3756 case NL80211_DFS_JP: 3757 return true; 3758 default: 3759 pr_debug("Ignoring unknown DFS master region: %d\n", dfs_region); 3760 return false; 3761 } 3762 } 3763 3764 static void print_regdomain(const struct ieee80211_regdomain *rd) 3765 { 3766 struct regulatory_request *lr = get_last_request(); 3767 3768 if (is_intersected_alpha2(rd->alpha2)) { 3769 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) { 3770 struct cfg80211_registered_device *rdev; 3771 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx); 3772 if (rdev) { 3773 pr_debug("Current regulatory domain updated by AP to: %c%c\n", 3774 rdev->country_ie_alpha2[0], 3775 rdev->country_ie_alpha2[1]); 3776 } else 3777 pr_debug("Current regulatory domain intersected:\n"); 3778 } else 3779 pr_debug("Current regulatory domain intersected:\n"); 3780 } else if (is_world_regdom(rd->alpha2)) { 3781 pr_debug("World regulatory domain updated:\n"); 3782 } else { 3783 if (is_unknown_alpha2(rd->alpha2)) 3784 pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n"); 3785 else { 3786 if (reg_request_cell_base(lr)) 3787 pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n", 3788 rd->alpha2[0], rd->alpha2[1]); 3789 else 3790 pr_debug("Regulatory domain changed to country: %c%c\n", 3791 rd->alpha2[0], rd->alpha2[1]); 3792 } 3793 } 3794 3795 pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region)); 3796 print_rd_rules(rd); 3797 } 3798 3799 static void print_regdomain_info(const struct ieee80211_regdomain *rd) 3800 { 3801 pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]); 3802 print_rd_rules(rd); 3803 } 3804 3805 static int reg_set_rd_core(const struct ieee80211_regdomain *rd) 3806 { 3807 if (!is_world_regdom(rd->alpha2)) 3808 return -EINVAL; 3809 update_world_regdomain(rd); 3810 return 0; 3811 } 3812 3813 static int reg_set_rd_user(const struct ieee80211_regdomain *rd, 3814 struct regulatory_request *user_request) 3815 { 3816 const struct ieee80211_regdomain *intersected_rd = NULL; 3817 3818 if (!regdom_changes(rd->alpha2)) 3819 return -EALREADY; 3820 3821 if (!is_valid_rd(rd)) { 3822 pr_err("Invalid regulatory domain detected: %c%c\n", 3823 rd->alpha2[0], rd->alpha2[1]); 3824 print_regdomain_info(rd); 3825 return -EINVAL; 3826 } 3827 3828 if (!user_request->intersect) { 3829 reset_regdomains(false, rd); 3830 return 0; 3831 } 3832 3833 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom()); 3834 if (!intersected_rd) 3835 return -EINVAL; 3836 3837 kfree(rd); 3838 rd = NULL; 3839 reset_regdomains(false, intersected_rd); 3840 3841 return 0; 3842 } 3843 3844 static int reg_set_rd_driver(const struct ieee80211_regdomain *rd, 3845 struct regulatory_request *driver_request) 3846 { 3847 const struct ieee80211_regdomain *regd; 3848 const struct ieee80211_regdomain *intersected_rd = NULL; 3849 const struct ieee80211_regdomain *tmp = NULL; 3850 struct wiphy *request_wiphy; 3851 3852 if (is_world_regdom(rd->alpha2)) 3853 return -EINVAL; 3854 3855 if (!regdom_changes(rd->alpha2)) 3856 return -EALREADY; 3857 3858 if (!is_valid_rd(rd)) { 3859 pr_err("Invalid regulatory domain detected: %c%c\n", 3860 rd->alpha2[0], rd->alpha2[1]); 3861 print_regdomain_info(rd); 3862 return -EINVAL; 3863 } 3864 3865 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx); 3866 if (!request_wiphy) 3867 return -ENODEV; 3868 3869 if (!driver_request->intersect) { 3870 ASSERT_RTNL(); 3871 scoped_guard(wiphy, request_wiphy) { 3872 if (request_wiphy->regd) 3873 tmp = get_wiphy_regdom(request_wiphy); 3874 3875 regd = reg_copy_regd(rd); 3876 if (IS_ERR(regd)) 3877 return PTR_ERR(regd); 3878 3879 rcu_assign_pointer(request_wiphy->regd, regd); 3880 rcu_free_regdom(tmp); 3881 } 3882 3883 reset_regdomains(false, rd); 3884 return 0; 3885 } 3886 3887 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom()); 3888 if (!intersected_rd) 3889 return -EINVAL; 3890 3891 /* 3892 * We can trash what CRDA provided now. 3893 * However if a driver requested this specific regulatory 3894 * domain we keep it for its private use 3895 */ 3896 tmp = get_wiphy_regdom(request_wiphy); 3897 rcu_assign_pointer(request_wiphy->regd, rd); 3898 rcu_free_regdom(tmp); 3899 3900 rd = NULL; 3901 3902 reset_regdomains(false, intersected_rd); 3903 3904 return 0; 3905 } 3906 3907 static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd, 3908 struct regulatory_request *country_ie_request) 3909 { 3910 struct wiphy *request_wiphy; 3911 3912 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && 3913 !is_unknown_alpha2(rd->alpha2)) 3914 return -EINVAL; 3915 3916 /* 3917 * Lets only bother proceeding on the same alpha2 if the current 3918 * rd is non static (it means CRDA was present and was used last) 3919 * and the pending request came in from a country IE 3920 */ 3921 3922 if (!is_valid_rd(rd)) { 3923 pr_err("Invalid regulatory domain detected: %c%c\n", 3924 rd->alpha2[0], rd->alpha2[1]); 3925 print_regdomain_info(rd); 3926 return -EINVAL; 3927 } 3928 3929 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx); 3930 if (!request_wiphy) 3931 return -ENODEV; 3932 3933 if (country_ie_request->intersect) 3934 return -EINVAL; 3935 3936 reset_regdomains(false, rd); 3937 return 0; 3938 } 3939 3940 /* 3941 * Use this call to set the current regulatory domain. Conflicts with 3942 * multiple drivers can be ironed out later. Caller must've already 3943 * kmalloc'd the rd structure. 3944 */ 3945 int set_regdom(const struct ieee80211_regdomain *rd, 3946 enum ieee80211_regd_source regd_src) 3947 { 3948 struct regulatory_request *lr; 3949 bool user_reset = false; 3950 int r; 3951 3952 if (IS_ERR_OR_NULL(rd)) 3953 return -ENODATA; 3954 3955 if (!reg_is_valid_request(rd->alpha2)) { 3956 kfree(rd); 3957 return -EINVAL; 3958 } 3959 3960 if (regd_src == REGD_SOURCE_CRDA) 3961 reset_crda_timeouts(); 3962 3963 lr = get_last_request(); 3964 3965 /* Note that this doesn't update the wiphys, this is done below */ 3966 switch (lr->initiator) { 3967 case NL80211_REGDOM_SET_BY_CORE: 3968 r = reg_set_rd_core(rd); 3969 break; 3970 case NL80211_REGDOM_SET_BY_USER: 3971 cfg80211_save_user_regdom(rd); 3972 r = reg_set_rd_user(rd, lr); 3973 user_reset = true; 3974 break; 3975 case NL80211_REGDOM_SET_BY_DRIVER: 3976 r = reg_set_rd_driver(rd, lr); 3977 break; 3978 case NL80211_REGDOM_SET_BY_COUNTRY_IE: 3979 r = reg_set_rd_country_ie(rd, lr); 3980 break; 3981 default: 3982 WARN(1, "invalid initiator %d\n", lr->initiator); 3983 kfree(rd); 3984 return -EINVAL; 3985 } 3986 3987 if (r) { 3988 switch (r) { 3989 case -EALREADY: 3990 reg_set_request_processed(); 3991 break; 3992 default: 3993 /* Back to world regulatory in case of errors */ 3994 restore_regulatory_settings(user_reset, false); 3995 } 3996 3997 kfree(rd); 3998 return r; 3999 } 4000 4001 /* This would make this whole thing pointless */ 4002 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom())) 4003 return -EINVAL; 4004 4005 /* update all wiphys now with the new established regulatory domain */ 4006 update_all_wiphy_regulatory(lr->initiator); 4007 4008 print_regdomain(get_cfg80211_regdom()); 4009 4010 nl80211_send_reg_change_event(lr); 4011 4012 reg_set_request_processed(); 4013 4014 return 0; 4015 } 4016 4017 static int __regulatory_set_wiphy_regd(struct wiphy *wiphy, 4018 struct ieee80211_regdomain *rd) 4019 { 4020 const struct ieee80211_regdomain *regd; 4021 const struct ieee80211_regdomain *prev_regd; 4022 struct cfg80211_registered_device *rdev; 4023 4024 if (WARN_ON(!wiphy || !rd)) 4025 return -EINVAL; 4026 4027 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED), 4028 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n")) 4029 return -EPERM; 4030 4031 if (WARN(!is_valid_rd(rd), 4032 "Invalid regulatory domain detected: %c%c\n", 4033 rd->alpha2[0], rd->alpha2[1])) { 4034 print_regdomain_info(rd); 4035 return -EINVAL; 4036 } 4037 4038 regd = reg_copy_regd(rd); 4039 if (IS_ERR(regd)) 4040 return PTR_ERR(regd); 4041 4042 rdev = wiphy_to_rdev(wiphy); 4043 4044 spin_lock(®_requests_lock); 4045 prev_regd = rdev->requested_regd; 4046 rdev->requested_regd = regd; 4047 spin_unlock(®_requests_lock); 4048 4049 kfree(prev_regd); 4050 return 0; 4051 } 4052 4053 int regulatory_set_wiphy_regd(struct wiphy *wiphy, 4054 struct ieee80211_regdomain *rd) 4055 { 4056 int ret = __regulatory_set_wiphy_regd(wiphy, rd); 4057 4058 if (ret) 4059 return ret; 4060 4061 schedule_work(®_work); 4062 return 0; 4063 } 4064 EXPORT_SYMBOL(regulatory_set_wiphy_regd); 4065 4066 int regulatory_set_wiphy_regd_sync(struct wiphy *wiphy, 4067 struct ieee80211_regdomain *rd) 4068 { 4069 int ret; 4070 4071 ASSERT_RTNL(); 4072 4073 ret = __regulatory_set_wiphy_regd(wiphy, rd); 4074 if (ret) 4075 return ret; 4076 4077 /* process the request immediately */ 4078 reg_process_self_managed_hint(wiphy); 4079 reg_check_channels(); 4080 return 0; 4081 } 4082 EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync); 4083 4084 void wiphy_regulatory_register(struct wiphy *wiphy) 4085 { 4086 struct regulatory_request *lr = get_last_request(); 4087 4088 /* self-managed devices ignore beacon hints and country IE */ 4089 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) { 4090 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS | 4091 REGULATORY_COUNTRY_IE_IGNORE; 4092 4093 /* 4094 * The last request may have been received before this 4095 * registration call. Call the driver notifier if 4096 * initiator is USER. 4097 */ 4098 if (lr->initiator == NL80211_REGDOM_SET_BY_USER) 4099 reg_call_notifier(wiphy, lr); 4100 } 4101 4102 if (!reg_dev_ignore_cell_hint(wiphy)) 4103 reg_num_devs_support_basehint++; 4104 4105 wiphy_update_regulatory(wiphy, lr->initiator); 4106 wiphy_all_share_dfs_chan_state(wiphy); 4107 reg_process_self_managed_hints(); 4108 } 4109 4110 void wiphy_regulatory_deregister(struct wiphy *wiphy) 4111 { 4112 struct wiphy *request_wiphy = NULL; 4113 struct regulatory_request *lr; 4114 4115 lr = get_last_request(); 4116 4117 if (!reg_dev_ignore_cell_hint(wiphy)) 4118 reg_num_devs_support_basehint--; 4119 4120 rcu_free_regdom(get_wiphy_regdom(wiphy)); 4121 RCU_INIT_POINTER(wiphy->regd, NULL); 4122 4123 if (lr) 4124 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); 4125 4126 if (!request_wiphy || request_wiphy != wiphy) 4127 return; 4128 4129 lr->wiphy_idx = WIPHY_IDX_INVALID; 4130 lr->country_ie_env = ENVIRON_ANY; 4131 } 4132 4133 /* 4134 * See FCC notices for UNII band definitions 4135 * 5GHz: https://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii 4136 * 6GHz: https://www.fcc.gov/document/fcc-proposes-more-spectrum-unlicensed-use-0 4137 */ 4138 int cfg80211_get_unii(int freq) 4139 { 4140 /* UNII-1 */ 4141 if (freq >= 5150 && freq <= 5250) 4142 return 0; 4143 4144 /* UNII-2A */ 4145 if (freq > 5250 && freq <= 5350) 4146 return 1; 4147 4148 /* UNII-2B */ 4149 if (freq > 5350 && freq <= 5470) 4150 return 2; 4151 4152 /* UNII-2C */ 4153 if (freq > 5470 && freq <= 5725) 4154 return 3; 4155 4156 /* UNII-3 */ 4157 if (freq > 5725 && freq <= 5825) 4158 return 4; 4159 4160 /* UNII-5 */ 4161 if (freq > 5925 && freq <= 6425) 4162 return 5; 4163 4164 /* UNII-6 */ 4165 if (freq > 6425 && freq <= 6525) 4166 return 6; 4167 4168 /* UNII-7 */ 4169 if (freq > 6525 && freq <= 6875) 4170 return 7; 4171 4172 /* UNII-8 */ 4173 if (freq > 6875 && freq <= 7125) 4174 return 8; 4175 4176 return -EINVAL; 4177 } 4178 4179 bool regulatory_indoor_allowed(void) 4180 { 4181 return reg_is_indoor; 4182 } 4183 4184 bool regulatory_pre_cac_allowed(struct wiphy *wiphy) 4185 { 4186 const struct ieee80211_regdomain *regd = NULL; 4187 const struct ieee80211_regdomain *wiphy_regd = NULL; 4188 bool pre_cac_allowed = false; 4189 4190 rcu_read_lock(); 4191 4192 regd = rcu_dereference(cfg80211_regdomain); 4193 wiphy_regd = rcu_dereference(wiphy->regd); 4194 if (!wiphy_regd) { 4195 if (regd->dfs_region == NL80211_DFS_ETSI) 4196 pre_cac_allowed = true; 4197 4198 rcu_read_unlock(); 4199 4200 return pre_cac_allowed; 4201 } 4202 4203 if (regd->dfs_region == wiphy_regd->dfs_region && 4204 wiphy_regd->dfs_region == NL80211_DFS_ETSI) 4205 pre_cac_allowed = true; 4206 4207 rcu_read_unlock(); 4208 4209 return pre_cac_allowed; 4210 } 4211 EXPORT_SYMBOL(regulatory_pre_cac_allowed); 4212 4213 static void cfg80211_check_and_end_cac(struct cfg80211_registered_device *rdev) 4214 { 4215 struct wireless_dev *wdev; 4216 unsigned int link_id; 4217 4218 guard(wiphy)(&rdev->wiphy); 4219 4220 /* If we finished CAC or received radar, we should end any 4221 * CAC running on the same channels. 4222 * the check !cfg80211_chandef_dfs_usable contain 2 options: 4223 * either all channels are available - those the CAC_FINISHED 4224 * event has effected another wdev state, or there is a channel 4225 * in unavailable state in wdev chandef - those the RADAR_DETECTED 4226 * event has effected another wdev state. 4227 * In both cases we should end the CAC on the wdev. 4228 */ 4229 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 4230 struct cfg80211_chan_def *chandef; 4231 4232 for_each_valid_link(wdev, link_id) { 4233 if (!wdev->links[link_id].cac_started) 4234 continue; 4235 4236 chandef = wdev_chandef(wdev, link_id); 4237 if (!chandef) 4238 continue; 4239 4240 if (!cfg80211_chandef_dfs_usable(&rdev->wiphy, chandef)) 4241 rdev_end_cac(rdev, wdev->netdev, link_id); 4242 } 4243 } 4244 } 4245 4246 void regulatory_propagate_dfs_state(struct wiphy *wiphy, 4247 struct cfg80211_chan_def *chandef, 4248 enum nl80211_dfs_state dfs_state, 4249 enum nl80211_radar_event event) 4250 { 4251 struct cfg80211_registered_device *rdev; 4252 4253 ASSERT_RTNL(); 4254 4255 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 4256 return; 4257 4258 for_each_rdev(rdev) { 4259 if (wiphy == &rdev->wiphy) 4260 continue; 4261 4262 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy)) 4263 continue; 4264 4265 if (!ieee80211_get_channel(&rdev->wiphy, 4266 chandef->chan->center_freq)) 4267 continue; 4268 4269 cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state); 4270 4271 if (event == NL80211_RADAR_DETECTED || 4272 event == NL80211_RADAR_CAC_FINISHED) { 4273 cfg80211_sched_dfs_chan_update(rdev); 4274 cfg80211_check_and_end_cac(rdev); 4275 } 4276 4277 nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL); 4278 } 4279 } 4280 4281 static int __init regulatory_init_db(void) 4282 { 4283 int err; 4284 4285 /* 4286 * It's possible that - due to other bugs/issues - cfg80211 4287 * never called regulatory_init() below, or that it failed; 4288 * in that case, don't try to do any further work here as 4289 * it's doomed to lead to crashes. 4290 */ 4291 if (!reg_fdev) 4292 return -EINVAL; 4293 4294 err = load_builtin_regdb_keys(); 4295 if (err) { 4296 faux_device_destroy(reg_fdev); 4297 return err; 4298 } 4299 4300 /* We always try to get an update for the static regdomain */ 4301 err = regulatory_hint_core(cfg80211_world_regdom->alpha2); 4302 if (err) { 4303 if (err == -ENOMEM) { 4304 faux_device_destroy(reg_fdev); 4305 return err; 4306 } 4307 /* 4308 * N.B. kobject_uevent_env() can fail mainly for when we're out 4309 * memory which is handled and propagated appropriately above 4310 * but it can also fail during a netlink_broadcast() or during 4311 * early boot for call_usermodehelper(). For now treat these 4312 * errors as non-fatal. 4313 */ 4314 pr_err("kobject_uevent_env() was unable to call CRDA during init\n"); 4315 } 4316 4317 /* 4318 * Finally, if the user set the module parameter treat it 4319 * as a user hint. 4320 */ 4321 if (!is_world_regdom(ieee80211_regdom)) 4322 regulatory_hint_user(ieee80211_regdom, 4323 NL80211_USER_REG_HINT_USER); 4324 4325 return 0; 4326 } 4327 #ifndef MODULE 4328 late_initcall(regulatory_init_db); 4329 #endif 4330 4331 int __init regulatory_init(void) 4332 { 4333 reg_fdev = faux_device_create("regulatory", NULL, NULL); 4334 if (!reg_fdev) 4335 return -ENODEV; 4336 4337 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom); 4338 4339 user_alpha2[0] = '9'; 4340 user_alpha2[1] = '7'; 4341 4342 #ifdef MODULE 4343 return regulatory_init_db(); 4344 #else 4345 return 0; 4346 #endif 4347 } 4348 4349 void regulatory_exit(void) 4350 { 4351 struct regulatory_request *reg_request, *tmp; 4352 struct reg_beacon *reg_beacon, *btmp; 4353 4354 cancel_work_sync(®_work); 4355 cancel_crda_timeout_sync(); 4356 cancel_delayed_work_sync(®_check_chans); 4357 4358 /* Lock to suppress warnings */ 4359 rtnl_lock(); 4360 reset_regdomains(true, NULL); 4361 rtnl_unlock(); 4362 4363 dev_set_uevent_suppress(®_fdev->dev, true); 4364 4365 faux_device_destroy(reg_fdev); 4366 4367 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) { 4368 list_del(®_beacon->list); 4369 kfree(reg_beacon); 4370 } 4371 4372 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) { 4373 list_del(®_beacon->list); 4374 kfree(reg_beacon); 4375 } 4376 4377 list_for_each_entry_safe(reg_request, tmp, ®_requests_list, list) { 4378 list_del(®_request->list); 4379 kfree(reg_request); 4380 } 4381 4382 if (!IS_ERR_OR_NULL(regdb)) 4383 kfree(regdb); 4384 if (!IS_ERR_OR_NULL(cfg80211_user_regdom)) 4385 kfree(cfg80211_user_regdom); 4386 4387 free_regdb_keyring(); 4388 } 4389