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 default: 2416 /* others not implemented for now */ 2417 WARN_ON_ONCE(1); 2418 break; 2419 } 2420 2421 switch (iftype) { 2422 case NL80211_IFTYPE_AP: 2423 case NL80211_IFTYPE_P2P_GO: 2424 case NL80211_IFTYPE_ADHOC: 2425 case NL80211_IFTYPE_MESH_POINT: 2426 ret = cfg80211_reg_can_beacon_relax(wiphy, &chandef, 2427 iftype); 2428 if (!ret) 2429 return ret; 2430 break; 2431 case NL80211_IFTYPE_STATION: 2432 case NL80211_IFTYPE_P2P_CLIENT: 2433 ret = cfg80211_chandef_usable(wiphy, &chandef, 2434 IEEE80211_CHAN_DISABLED); 2435 if (!ret) 2436 return ret; 2437 break; 2438 default: 2439 break; 2440 } 2441 } 2442 2443 return true; 2444 } 2445 2446 static void reg_leave_invalid_chans(struct wiphy *wiphy) 2447 { 2448 struct wireless_dev *wdev; 2449 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 2450 2451 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 2452 bool valid; 2453 2454 scoped_guard(wiphy, wiphy) 2455 valid = reg_wdev_chan_valid(wiphy, wdev); 2456 if (!valid) 2457 cfg80211_leave(rdev, wdev, -1); 2458 } 2459 } 2460 2461 static void reg_check_chans_work(struct work_struct *work) 2462 { 2463 struct cfg80211_registered_device *rdev; 2464 2465 pr_debug("Verifying active interfaces after reg change\n"); 2466 rtnl_lock(); 2467 2468 for_each_rdev(rdev) 2469 reg_leave_invalid_chans(&rdev->wiphy); 2470 2471 rtnl_unlock(); 2472 } 2473 2474 void reg_check_channels(void) 2475 { 2476 /* 2477 * Give usermode a chance to do something nicer (move to another 2478 * channel, orderly disconnection), before forcing a disconnection. 2479 */ 2480 mod_delayed_work(system_power_efficient_wq, 2481 ®_check_chans, 2482 msecs_to_jiffies(REG_ENFORCE_GRACE_MS)); 2483 } 2484 2485 static void wiphy_update_regulatory(struct wiphy *wiphy, 2486 enum nl80211_reg_initiator initiator) 2487 { 2488 enum nl80211_band band; 2489 struct regulatory_request *lr = get_last_request(); 2490 2491 if (ignore_reg_update(wiphy, initiator)) { 2492 /* 2493 * Regulatory updates set by CORE are ignored for custom 2494 * regulatory cards. Let us notify the changes to the driver, 2495 * as some drivers used this to restore its orig_* reg domain. 2496 */ 2497 if (initiator == NL80211_REGDOM_SET_BY_CORE && 2498 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG && 2499 !(wiphy->regulatory_flags & 2500 REGULATORY_WIPHY_SELF_MANAGED)) 2501 reg_call_notifier(wiphy, lr); 2502 return; 2503 } 2504 2505 lr->dfs_region = get_cfg80211_regdom()->dfs_region; 2506 2507 for (band = 0; band < NUM_NL80211_BANDS; band++) 2508 handle_band(wiphy, initiator, wiphy->bands[band]); 2509 2510 reg_process_beacons(wiphy); 2511 reg_process_ht_flags(wiphy); 2512 reg_call_notifier(wiphy, lr); 2513 } 2514 2515 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator) 2516 { 2517 struct cfg80211_registered_device *rdev; 2518 struct wiphy *wiphy; 2519 2520 ASSERT_RTNL(); 2521 2522 for_each_rdev(rdev) { 2523 wiphy = &rdev->wiphy; 2524 wiphy_update_regulatory(wiphy, initiator); 2525 } 2526 2527 reg_check_channels(); 2528 } 2529 2530 static void handle_channel_custom(struct wiphy *wiphy, 2531 struct ieee80211_channel *chan, 2532 const struct ieee80211_regdomain *regd, 2533 u32 min_bw) 2534 { 2535 u32 bw_flags = 0; 2536 const struct ieee80211_reg_rule *reg_rule = NULL; 2537 const struct ieee80211_power_rule *power_rule = NULL; 2538 u32 bw, center_freq_khz; 2539 2540 center_freq_khz = ieee80211_channel_to_khz(chan); 2541 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) { 2542 reg_rule = freq_reg_info_regd(center_freq_khz, regd, bw); 2543 if (!IS_ERR(reg_rule)) 2544 break; 2545 } 2546 2547 if (IS_ERR_OR_NULL(reg_rule)) { 2548 pr_debug("Disabling freq %d.%03d MHz as custom regd has no rule that fits it\n", 2549 chan->center_freq, chan->freq_offset); 2550 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) { 2551 chan->flags |= IEEE80211_CHAN_DISABLED; 2552 } else { 2553 chan->orig_flags |= IEEE80211_CHAN_DISABLED; 2554 chan->flags = chan->orig_flags; 2555 } 2556 return; 2557 } 2558 2559 power_rule = ®_rule->power_rule; 2560 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan); 2561 2562 chan->dfs_state_entered = jiffies; 2563 chan->dfs_state = NL80211_DFS_USABLE; 2564 2565 chan->beacon_found = false; 2566 2567 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) 2568 chan->flags = chan->orig_flags | bw_flags | 2569 map_regdom_flags(reg_rule->flags); 2570 else 2571 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags; 2572 2573 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain); 2574 chan->max_reg_power = chan->max_power = 2575 (int) MBM_TO_DBM(power_rule->max_eirp); 2576 2577 if (chan->flags & IEEE80211_CHAN_RADAR) { 2578 if (reg_rule->dfs_cac_ms) 2579 chan->dfs_cac_ms = reg_rule->dfs_cac_ms; 2580 else 2581 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS; 2582 } 2583 2584 if (chan->flags & IEEE80211_CHAN_PSD) 2585 chan->psd = reg_rule->psd; 2586 2587 chan->max_power = chan->max_reg_power; 2588 } 2589 2590 static void handle_band_custom(struct wiphy *wiphy, 2591 struct ieee80211_supported_band *sband, 2592 const struct ieee80211_regdomain *regd) 2593 { 2594 unsigned int i; 2595 2596 if (!sband) 2597 return; 2598 2599 /* 2600 * We currently assume that you always want at least 20 MHz, 2601 * otherwise channel 12 might get enabled if this rule is 2602 * compatible to US, which permits 2402 - 2472 MHz. 2603 */ 2604 for (i = 0; i < sband->n_channels; i++) 2605 handle_channel_custom(wiphy, &sband->channels[i], regd, 2606 MHZ_TO_KHZ(20)); 2607 } 2608 2609 /* Used by drivers prior to wiphy registration */ 2610 void wiphy_apply_custom_regulatory(struct wiphy *wiphy, 2611 const struct ieee80211_regdomain *regd) 2612 { 2613 const struct ieee80211_regdomain *new_regd, *tmp; 2614 enum nl80211_band band; 2615 unsigned int bands_set = 0; 2616 2617 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG), 2618 "wiphy should have REGULATORY_CUSTOM_REG\n"); 2619 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG; 2620 2621 for (band = 0; band < NUM_NL80211_BANDS; band++) { 2622 if (!wiphy->bands[band]) 2623 continue; 2624 handle_band_custom(wiphy, wiphy->bands[band], regd); 2625 bands_set++; 2626 } 2627 2628 /* 2629 * no point in calling this if it won't have any effect 2630 * on your device's supported bands. 2631 */ 2632 WARN_ON(!bands_set); 2633 new_regd = reg_copy_regd(regd); 2634 if (IS_ERR(new_regd)) 2635 return; 2636 2637 rtnl_lock(); 2638 scoped_guard(wiphy, wiphy) { 2639 tmp = get_wiphy_regdom(wiphy); 2640 rcu_assign_pointer(wiphy->regd, new_regd); 2641 rcu_free_regdom(tmp); 2642 } 2643 rtnl_unlock(); 2644 } 2645 EXPORT_SYMBOL(wiphy_apply_custom_regulatory); 2646 2647 static void reg_set_request_processed(void) 2648 { 2649 bool need_more_processing = false; 2650 struct regulatory_request *lr = get_last_request(); 2651 2652 lr->processed = true; 2653 2654 spin_lock(®_requests_lock); 2655 if (!list_empty(®_requests_list)) 2656 need_more_processing = true; 2657 spin_unlock(®_requests_lock); 2658 2659 cancel_crda_timeout(); 2660 2661 if (need_more_processing) 2662 schedule_work(®_work); 2663 } 2664 2665 /** 2666 * reg_process_hint_core - process core regulatory requests 2667 * @core_request: a pending core regulatory request 2668 * 2669 * The wireless subsystem can use this function to process 2670 * a regulatory request issued by the regulatory core. 2671 * 2672 * Returns: %REG_REQ_OK or %REG_REQ_IGNORE, indicating if the 2673 * hint was processed or ignored 2674 */ 2675 static enum reg_request_treatment 2676 reg_process_hint_core(struct regulatory_request *core_request) 2677 { 2678 if (reg_query_database(core_request)) { 2679 core_request->intersect = false; 2680 core_request->processed = false; 2681 reg_update_last_request(core_request); 2682 return REG_REQ_OK; 2683 } 2684 2685 return REG_REQ_IGNORE; 2686 } 2687 2688 static enum reg_request_treatment 2689 __reg_process_hint_user(struct regulatory_request *user_request) 2690 { 2691 struct regulatory_request *lr = get_last_request(); 2692 2693 if (reg_request_cell_base(user_request)) 2694 return reg_ignore_cell_hint(user_request); 2695 2696 if (reg_request_cell_base(lr)) 2697 return REG_REQ_IGNORE; 2698 2699 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) 2700 return REG_REQ_INTERSECT; 2701 /* 2702 * If the user knows better the user should set the regdom 2703 * to their country before the IE is picked up 2704 */ 2705 if (lr->initiator == NL80211_REGDOM_SET_BY_USER && 2706 lr->intersect) 2707 return REG_REQ_IGNORE; 2708 /* 2709 * Process user requests only after previous user/driver/core 2710 * requests have been processed 2711 */ 2712 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE || 2713 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER || 2714 lr->initiator == NL80211_REGDOM_SET_BY_USER) && 2715 regdom_changes(lr->alpha2)) 2716 return REG_REQ_IGNORE; 2717 2718 if (!regdom_changes(user_request->alpha2)) 2719 return REG_REQ_ALREADY_SET; 2720 2721 return REG_REQ_OK; 2722 } 2723 2724 /** 2725 * reg_process_hint_user - process user regulatory requests 2726 * @user_request: a pending user regulatory request 2727 * 2728 * The wireless subsystem can use this function to process 2729 * a regulatory request initiated by userspace. 2730 * 2731 * Returns: %REG_REQ_OK or %REG_REQ_IGNORE, indicating if the 2732 * hint was processed or ignored 2733 */ 2734 static enum reg_request_treatment 2735 reg_process_hint_user(struct regulatory_request *user_request) 2736 { 2737 enum reg_request_treatment treatment; 2738 2739 treatment = __reg_process_hint_user(user_request); 2740 if (treatment == REG_REQ_IGNORE || 2741 treatment == REG_REQ_ALREADY_SET) 2742 return REG_REQ_IGNORE; 2743 2744 user_request->intersect = treatment == REG_REQ_INTERSECT; 2745 user_request->processed = false; 2746 2747 if (reg_query_database(user_request)) { 2748 reg_update_last_request(user_request); 2749 user_alpha2[0] = user_request->alpha2[0]; 2750 user_alpha2[1] = user_request->alpha2[1]; 2751 return REG_REQ_OK; 2752 } 2753 2754 return REG_REQ_IGNORE; 2755 } 2756 2757 static enum reg_request_treatment 2758 __reg_process_hint_driver(struct regulatory_request *driver_request) 2759 { 2760 struct regulatory_request *lr = get_last_request(); 2761 2762 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) { 2763 if (regdom_changes(driver_request->alpha2)) 2764 return REG_REQ_OK; 2765 return REG_REQ_ALREADY_SET; 2766 } 2767 2768 /* 2769 * This would happen if you unplug and plug your card 2770 * back in or if you add a new device for which the previously 2771 * loaded card also agrees on the regulatory domain. 2772 */ 2773 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && 2774 !regdom_changes(driver_request->alpha2)) 2775 return REG_REQ_ALREADY_SET; 2776 2777 return REG_REQ_INTERSECT; 2778 } 2779 2780 /** 2781 * reg_process_hint_driver - process driver regulatory requests 2782 * @wiphy: the wireless device for the regulatory request 2783 * @driver_request: a pending driver regulatory request 2784 * 2785 * The wireless subsystem can use this function to process 2786 * a regulatory request issued by an 802.11 driver. 2787 * 2788 * Returns: one of the different reg request treatment values. 2789 */ 2790 static enum reg_request_treatment 2791 reg_process_hint_driver(struct wiphy *wiphy, 2792 struct regulatory_request *driver_request) 2793 { 2794 const struct ieee80211_regdomain *regd, *tmp; 2795 enum reg_request_treatment treatment; 2796 2797 treatment = __reg_process_hint_driver(driver_request); 2798 2799 switch (treatment) { 2800 case REG_REQ_OK: 2801 break; 2802 case REG_REQ_IGNORE: 2803 return REG_REQ_IGNORE; 2804 case REG_REQ_INTERSECT: 2805 case REG_REQ_ALREADY_SET: 2806 regd = reg_copy_regd(get_cfg80211_regdom()); 2807 if (IS_ERR(regd)) 2808 return REG_REQ_IGNORE; 2809 2810 tmp = get_wiphy_regdom(wiphy); 2811 ASSERT_RTNL(); 2812 scoped_guard(wiphy, wiphy) { 2813 rcu_assign_pointer(wiphy->regd, regd); 2814 } 2815 rcu_free_regdom(tmp); 2816 } 2817 2818 2819 driver_request->intersect = treatment == REG_REQ_INTERSECT; 2820 driver_request->processed = false; 2821 2822 /* 2823 * Since CRDA will not be called in this case as we already 2824 * have applied the requested regulatory domain before we just 2825 * inform userspace we have processed the request 2826 */ 2827 if (treatment == REG_REQ_ALREADY_SET) { 2828 nl80211_send_reg_change_event(driver_request); 2829 reg_update_last_request(driver_request); 2830 reg_set_request_processed(); 2831 return REG_REQ_ALREADY_SET; 2832 } 2833 2834 if (reg_query_database(driver_request)) { 2835 reg_update_last_request(driver_request); 2836 return REG_REQ_OK; 2837 } 2838 2839 return REG_REQ_IGNORE; 2840 } 2841 2842 static enum reg_request_treatment 2843 __reg_process_hint_country_ie(struct wiphy *wiphy, 2844 struct regulatory_request *country_ie_request) 2845 { 2846 struct wiphy *last_wiphy = NULL; 2847 struct regulatory_request *lr = get_last_request(); 2848 2849 if (reg_request_cell_base(lr)) { 2850 /* Trust a Cell base station over the AP's country IE */ 2851 if (regdom_changes(country_ie_request->alpha2)) 2852 return REG_REQ_IGNORE; 2853 return REG_REQ_ALREADY_SET; 2854 } else { 2855 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE) 2856 return REG_REQ_IGNORE; 2857 } 2858 2859 if (unlikely(!is_an_alpha2(country_ie_request->alpha2))) 2860 return -EINVAL; 2861 2862 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) 2863 return REG_REQ_OK; 2864 2865 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); 2866 2867 if (last_wiphy != wiphy) { 2868 /* 2869 * Two cards with two APs claiming different 2870 * Country IE alpha2s. We could 2871 * intersect them, but that seems unlikely 2872 * to be correct. Reject second one for now. 2873 */ 2874 if (regdom_changes(country_ie_request->alpha2)) 2875 return REG_REQ_IGNORE; 2876 return REG_REQ_ALREADY_SET; 2877 } 2878 2879 if (regdom_changes(country_ie_request->alpha2)) 2880 return REG_REQ_OK; 2881 return REG_REQ_ALREADY_SET; 2882 } 2883 2884 /** 2885 * reg_process_hint_country_ie - process regulatory requests from country IEs 2886 * @wiphy: the wireless device for the regulatory request 2887 * @country_ie_request: a regulatory request from a country IE 2888 * 2889 * The wireless subsystem can use this function to process 2890 * a regulatory request issued by a country Information Element. 2891 * 2892 * Returns: one of the different reg request treatment values. 2893 */ 2894 static enum reg_request_treatment 2895 reg_process_hint_country_ie(struct wiphy *wiphy, 2896 struct regulatory_request *country_ie_request) 2897 { 2898 enum reg_request_treatment treatment; 2899 2900 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request); 2901 2902 switch (treatment) { 2903 case REG_REQ_OK: 2904 break; 2905 case REG_REQ_IGNORE: 2906 return REG_REQ_IGNORE; 2907 case REG_REQ_ALREADY_SET: 2908 reg_free_request(country_ie_request); 2909 return REG_REQ_ALREADY_SET; 2910 case REG_REQ_INTERSECT: 2911 /* 2912 * This doesn't happen yet, not sure we 2913 * ever want to support it for this case. 2914 */ 2915 WARN_ONCE(1, "Unexpected intersection for country elements"); 2916 return REG_REQ_IGNORE; 2917 } 2918 2919 country_ie_request->intersect = false; 2920 country_ie_request->processed = false; 2921 2922 if (reg_query_database(country_ie_request)) { 2923 reg_update_last_request(country_ie_request); 2924 return REG_REQ_OK; 2925 } 2926 2927 return REG_REQ_IGNORE; 2928 } 2929 2930 bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2) 2931 { 2932 const struct ieee80211_regdomain *wiphy1_regd = NULL; 2933 const struct ieee80211_regdomain *wiphy2_regd = NULL; 2934 const struct ieee80211_regdomain *cfg80211_regd = NULL; 2935 bool dfs_domain_same; 2936 2937 rcu_read_lock(); 2938 2939 cfg80211_regd = rcu_dereference(cfg80211_regdomain); 2940 wiphy1_regd = rcu_dereference(wiphy1->regd); 2941 if (!wiphy1_regd) 2942 wiphy1_regd = cfg80211_regd; 2943 2944 wiphy2_regd = rcu_dereference(wiphy2->regd); 2945 if (!wiphy2_regd) 2946 wiphy2_regd = cfg80211_regd; 2947 2948 dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region; 2949 2950 rcu_read_unlock(); 2951 2952 return dfs_domain_same; 2953 } 2954 2955 static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan, 2956 struct ieee80211_channel *src_chan) 2957 { 2958 if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) || 2959 !(src_chan->flags & IEEE80211_CHAN_RADAR)) 2960 return; 2961 2962 if (dst_chan->flags & IEEE80211_CHAN_DISABLED || 2963 src_chan->flags & IEEE80211_CHAN_DISABLED) 2964 return; 2965 2966 if (src_chan->center_freq == dst_chan->center_freq && 2967 dst_chan->dfs_state == NL80211_DFS_USABLE) { 2968 dst_chan->dfs_state = src_chan->dfs_state; 2969 dst_chan->dfs_state_entered = src_chan->dfs_state_entered; 2970 } 2971 } 2972 2973 static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy, 2974 struct wiphy *src_wiphy) 2975 { 2976 struct ieee80211_supported_band *src_sband, *dst_sband; 2977 struct ieee80211_channel *src_chan, *dst_chan; 2978 int i, j, band; 2979 2980 if (!reg_dfs_domain_same(dst_wiphy, src_wiphy)) 2981 return; 2982 2983 for (band = 0; band < NUM_NL80211_BANDS; band++) { 2984 dst_sband = dst_wiphy->bands[band]; 2985 src_sband = src_wiphy->bands[band]; 2986 if (!dst_sband || !src_sband) 2987 continue; 2988 2989 for (i = 0; i < dst_sband->n_channels; i++) { 2990 dst_chan = &dst_sband->channels[i]; 2991 for (j = 0; j < src_sband->n_channels; j++) { 2992 src_chan = &src_sband->channels[j]; 2993 reg_copy_dfs_chan_state(dst_chan, src_chan); 2994 } 2995 } 2996 } 2997 } 2998 2999 static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy) 3000 { 3001 struct cfg80211_registered_device *rdev; 3002 3003 ASSERT_RTNL(); 3004 3005 for_each_rdev(rdev) { 3006 if (wiphy == &rdev->wiphy) 3007 continue; 3008 wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy); 3009 } 3010 } 3011 3012 /* This processes *all* regulatory hints */ 3013 static void reg_process_hint(struct regulatory_request *reg_request) 3014 { 3015 struct wiphy *wiphy = NULL; 3016 enum reg_request_treatment treatment; 3017 enum nl80211_reg_initiator initiator = reg_request->initiator; 3018 3019 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID) 3020 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx); 3021 3022 switch (initiator) { 3023 case NL80211_REGDOM_SET_BY_CORE: 3024 treatment = reg_process_hint_core(reg_request); 3025 break; 3026 case NL80211_REGDOM_SET_BY_USER: 3027 treatment = reg_process_hint_user(reg_request); 3028 break; 3029 case NL80211_REGDOM_SET_BY_DRIVER: 3030 if (!wiphy) 3031 goto out_free; 3032 treatment = reg_process_hint_driver(wiphy, reg_request); 3033 break; 3034 case NL80211_REGDOM_SET_BY_COUNTRY_IE: 3035 if (!wiphy) 3036 goto out_free; 3037 treatment = reg_process_hint_country_ie(wiphy, reg_request); 3038 break; 3039 default: 3040 WARN(1, "invalid initiator %d\n", initiator); 3041 goto out_free; 3042 } 3043 3044 if (treatment == REG_REQ_IGNORE) 3045 goto out_free; 3046 3047 WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET, 3048 "unexpected treatment value %d\n", treatment); 3049 3050 /* This is required so that the orig_* parameters are saved. 3051 * NOTE: treatment must be set for any case that reaches here! 3052 */ 3053 if (treatment == REG_REQ_ALREADY_SET && wiphy && 3054 wiphy->regulatory_flags & REGULATORY_STRICT_REG) { 3055 wiphy_update_regulatory(wiphy, initiator); 3056 wiphy_all_share_dfs_chan_state(wiphy); 3057 reg_check_channels(); 3058 } 3059 3060 return; 3061 3062 out_free: 3063 reg_free_request(reg_request); 3064 } 3065 3066 static void notify_self_managed_wiphys(struct regulatory_request *request) 3067 { 3068 struct cfg80211_registered_device *rdev; 3069 struct wiphy *wiphy; 3070 3071 for_each_rdev(rdev) { 3072 wiphy = &rdev->wiphy; 3073 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED && 3074 request->initiator == NL80211_REGDOM_SET_BY_USER) 3075 reg_call_notifier(wiphy, request); 3076 } 3077 } 3078 3079 /* 3080 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* 3081 * Regulatory hints come on a first come first serve basis and we 3082 * must process each one atomically. 3083 */ 3084 static void reg_process_pending_hints(void) 3085 { 3086 struct regulatory_request *reg_request, *lr; 3087 3088 lr = get_last_request(); 3089 3090 /* When last_request->processed becomes true this will be rescheduled */ 3091 if (lr && !lr->processed) { 3092 pr_debug("Pending regulatory request, waiting for it to be processed...\n"); 3093 return; 3094 } 3095 3096 spin_lock(®_requests_lock); 3097 3098 if (list_empty(®_requests_list)) { 3099 spin_unlock(®_requests_lock); 3100 return; 3101 } 3102 3103 reg_request = list_first_entry(®_requests_list, 3104 struct regulatory_request, 3105 list); 3106 list_del_init(®_request->list); 3107 3108 spin_unlock(®_requests_lock); 3109 3110 notify_self_managed_wiphys(reg_request); 3111 3112 reg_process_hint(reg_request); 3113 3114 lr = get_last_request(); 3115 3116 spin_lock(®_requests_lock); 3117 if (!list_empty(®_requests_list) && lr && lr->processed) 3118 schedule_work(®_work); 3119 spin_unlock(®_requests_lock); 3120 } 3121 3122 /* Processes beacon hints -- this has nothing to do with country IEs */ 3123 static void reg_process_pending_beacon_hints(void) 3124 { 3125 struct cfg80211_registered_device *rdev; 3126 struct reg_beacon *pending_beacon, *tmp; 3127 3128 /* This goes through the _pending_ beacon list */ 3129 spin_lock_bh(®_pending_beacons_lock); 3130 3131 list_for_each_entry_safe(pending_beacon, tmp, 3132 ®_pending_beacons, list) { 3133 list_del_init(&pending_beacon->list); 3134 3135 /* Applies the beacon hint to current wiphys */ 3136 for_each_rdev(rdev) 3137 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon); 3138 3139 /* Remembers the beacon hint for new wiphys or reg changes */ 3140 list_add_tail(&pending_beacon->list, ®_beacon_list); 3141 } 3142 3143 spin_unlock_bh(®_pending_beacons_lock); 3144 } 3145 3146 static void reg_process_self_managed_hint(struct wiphy *wiphy) 3147 { 3148 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 3149 const struct ieee80211_regdomain *tmp; 3150 const struct ieee80211_regdomain *regd; 3151 enum nl80211_band band; 3152 struct regulatory_request request = {}; 3153 3154 ASSERT_RTNL(); 3155 lockdep_assert_wiphy(wiphy); 3156 3157 spin_lock(®_requests_lock); 3158 regd = rdev->requested_regd; 3159 rdev->requested_regd = NULL; 3160 spin_unlock(®_requests_lock); 3161 3162 if (!regd) 3163 return; 3164 3165 tmp = get_wiphy_regdom(wiphy); 3166 rcu_assign_pointer(wiphy->regd, regd); 3167 rcu_free_regdom(tmp); 3168 3169 for (band = 0; band < NUM_NL80211_BANDS; band++) 3170 handle_band_custom(wiphy, wiphy->bands[band], regd); 3171 3172 reg_process_ht_flags(wiphy); 3173 3174 request.wiphy_idx = get_wiphy_idx(wiphy); 3175 request.alpha2[0] = regd->alpha2[0]; 3176 request.alpha2[1] = regd->alpha2[1]; 3177 request.initiator = NL80211_REGDOM_SET_BY_DRIVER; 3178 3179 if (wiphy->flags & WIPHY_FLAG_NOTIFY_REGDOM_BY_DRIVER) 3180 reg_call_notifier(wiphy, &request); 3181 3182 nl80211_send_wiphy_reg_change_event(&request); 3183 } 3184 3185 static void reg_process_self_managed_hints(void) 3186 { 3187 struct cfg80211_registered_device *rdev; 3188 3189 ASSERT_RTNL(); 3190 3191 for_each_rdev(rdev) { 3192 guard(wiphy)(&rdev->wiphy); 3193 3194 reg_process_self_managed_hint(&rdev->wiphy); 3195 } 3196 3197 reg_check_channels(); 3198 } 3199 3200 static void reg_todo(struct work_struct *work) 3201 { 3202 rtnl_lock(); 3203 reg_process_pending_hints(); 3204 reg_process_pending_beacon_hints(); 3205 reg_process_self_managed_hints(); 3206 rtnl_unlock(); 3207 } 3208 3209 static void queue_regulatory_request(struct regulatory_request *request) 3210 { 3211 request->alpha2[0] = toupper(request->alpha2[0]); 3212 request->alpha2[1] = toupper(request->alpha2[1]); 3213 3214 spin_lock(®_requests_lock); 3215 list_add_tail(&request->list, ®_requests_list); 3216 spin_unlock(®_requests_lock); 3217 3218 schedule_work(®_work); 3219 } 3220 3221 /* 3222 * Core regulatory hint -- happens during cfg80211_init() 3223 * and when we restore regulatory settings. 3224 */ 3225 static int regulatory_hint_core(const char *alpha2) 3226 { 3227 struct regulatory_request *request; 3228 3229 request = kzalloc_obj(struct regulatory_request); 3230 if (!request) 3231 return -ENOMEM; 3232 3233 request->alpha2[0] = alpha2[0]; 3234 request->alpha2[1] = alpha2[1]; 3235 request->initiator = NL80211_REGDOM_SET_BY_CORE; 3236 request->wiphy_idx = WIPHY_IDX_INVALID; 3237 3238 queue_regulatory_request(request); 3239 3240 return 0; 3241 } 3242 3243 /* User hints */ 3244 int regulatory_hint_user(const char *alpha2, 3245 enum nl80211_user_reg_hint_type user_reg_hint_type) 3246 { 3247 struct regulatory_request *request; 3248 3249 if (WARN_ON(!alpha2)) 3250 return -EINVAL; 3251 3252 if (!is_world_regdom(alpha2) && !is_an_alpha2(alpha2)) 3253 return -EINVAL; 3254 3255 request = kzalloc_obj(struct regulatory_request); 3256 if (!request) 3257 return -ENOMEM; 3258 3259 request->wiphy_idx = WIPHY_IDX_INVALID; 3260 request->alpha2[0] = alpha2[0]; 3261 request->alpha2[1] = alpha2[1]; 3262 request->initiator = NL80211_REGDOM_SET_BY_USER; 3263 request->user_reg_hint_type = user_reg_hint_type; 3264 3265 /* Allow calling CRDA again */ 3266 reset_crda_timeouts(); 3267 3268 queue_regulatory_request(request); 3269 3270 return 0; 3271 } 3272 3273 void regulatory_hint_indoor(bool is_indoor, u32 portid) 3274 { 3275 spin_lock(®_indoor_lock); 3276 3277 /* It is possible that more than one user space process is trying to 3278 * configure the indoor setting. To handle such cases, clear the indoor 3279 * setting in case that some process does not think that the device 3280 * is operating in an indoor environment. In addition, if a user space 3281 * process indicates that it is controlling the indoor setting, save its 3282 * portid, i.e., make it the owner. 3283 */ 3284 reg_is_indoor = is_indoor; 3285 if (reg_is_indoor) { 3286 if (!reg_is_indoor_portid) 3287 reg_is_indoor_portid = portid; 3288 } else { 3289 reg_is_indoor_portid = 0; 3290 } 3291 3292 spin_unlock(®_indoor_lock); 3293 3294 if (!is_indoor) 3295 reg_check_channels(); 3296 } 3297 3298 void regulatory_netlink_notify(u32 portid) 3299 { 3300 spin_lock(®_indoor_lock); 3301 3302 if (reg_is_indoor_portid != portid) { 3303 spin_unlock(®_indoor_lock); 3304 return; 3305 } 3306 3307 reg_is_indoor = false; 3308 reg_is_indoor_portid = 0; 3309 3310 spin_unlock(®_indoor_lock); 3311 3312 reg_check_channels(); 3313 } 3314 3315 /* Driver hints */ 3316 int regulatory_hint(struct wiphy *wiphy, const char *alpha2) 3317 { 3318 struct regulatory_request *request; 3319 3320 if (WARN_ON(!alpha2 || !wiphy)) 3321 return -EINVAL; 3322 3323 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG; 3324 3325 request = kzalloc_obj(struct regulatory_request); 3326 if (!request) 3327 return -ENOMEM; 3328 3329 request->wiphy_idx = get_wiphy_idx(wiphy); 3330 3331 request->alpha2[0] = alpha2[0]; 3332 request->alpha2[1] = alpha2[1]; 3333 request->initiator = NL80211_REGDOM_SET_BY_DRIVER; 3334 3335 /* Allow calling CRDA again */ 3336 reset_crda_timeouts(); 3337 3338 queue_regulatory_request(request); 3339 3340 return 0; 3341 } 3342 EXPORT_SYMBOL(regulatory_hint); 3343 3344 void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band, 3345 const u8 *country_ie, u8 country_ie_len) 3346 { 3347 char alpha2[2]; 3348 enum environment_cap env = ENVIRON_ANY; 3349 struct regulatory_request *request = NULL, *lr; 3350 3351 /* IE len must be evenly divisible by 2 */ 3352 if (country_ie_len & 0x01) 3353 return; 3354 3355 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) 3356 return; 3357 3358 request = kzalloc_obj(*request); 3359 if (!request) 3360 return; 3361 3362 alpha2[0] = country_ie[0]; 3363 alpha2[1] = country_ie[1]; 3364 3365 if (country_ie[2] == 'I') 3366 env = ENVIRON_INDOOR; 3367 else if (country_ie[2] == 'O') 3368 env = ENVIRON_OUTDOOR; 3369 3370 rcu_read_lock(); 3371 lr = get_last_request(); 3372 3373 if (unlikely(!lr)) 3374 goto out; 3375 3376 /* 3377 * We will run this only upon a successful connection on cfg80211. 3378 * We leave conflict resolution to the workqueue, where can hold 3379 * the RTNL. 3380 */ 3381 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && 3382 lr->wiphy_idx != WIPHY_IDX_INVALID) 3383 goto out; 3384 3385 request->wiphy_idx = get_wiphy_idx(wiphy); 3386 request->alpha2[0] = alpha2[0]; 3387 request->alpha2[1] = alpha2[1]; 3388 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE; 3389 request->country_ie_env = env; 3390 3391 /* Allow calling CRDA again */ 3392 reset_crda_timeouts(); 3393 3394 queue_regulatory_request(request); 3395 request = NULL; 3396 out: 3397 kfree(request); 3398 rcu_read_unlock(); 3399 } 3400 3401 static void restore_alpha2(char *alpha2, bool reset_user) 3402 { 3403 /* indicates there is no alpha2 to consider for restoration */ 3404 alpha2[0] = '9'; 3405 alpha2[1] = '7'; 3406 3407 /* The user setting has precedence over the module parameter */ 3408 if (is_user_regdom_saved()) { 3409 /* Unless we're asked to ignore it and reset it */ 3410 if (reset_user) { 3411 pr_debug("Restoring regulatory settings including user preference\n"); 3412 user_alpha2[0] = '9'; 3413 user_alpha2[1] = '7'; 3414 3415 /* 3416 * If we're ignoring user settings, we still need to 3417 * check the module parameter to ensure we put things 3418 * back as they were for a full restore. 3419 */ 3420 if (!is_world_regdom(ieee80211_regdom)) { 3421 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n", 3422 ieee80211_regdom[0], ieee80211_regdom[1]); 3423 alpha2[0] = ieee80211_regdom[0]; 3424 alpha2[1] = ieee80211_regdom[1]; 3425 } 3426 } else { 3427 pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n", 3428 user_alpha2[0], user_alpha2[1]); 3429 alpha2[0] = user_alpha2[0]; 3430 alpha2[1] = user_alpha2[1]; 3431 } 3432 } else if (!is_world_regdom(ieee80211_regdom)) { 3433 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n", 3434 ieee80211_regdom[0], ieee80211_regdom[1]); 3435 alpha2[0] = ieee80211_regdom[0]; 3436 alpha2[1] = ieee80211_regdom[1]; 3437 } else 3438 pr_debug("Restoring regulatory settings\n"); 3439 } 3440 3441 static void restore_custom_reg_settings(struct wiphy *wiphy) 3442 { 3443 struct ieee80211_supported_band *sband; 3444 enum nl80211_band band; 3445 struct ieee80211_channel *chan; 3446 int i; 3447 3448 for (band = 0; band < NUM_NL80211_BANDS; band++) { 3449 sband = wiphy->bands[band]; 3450 if (!sband) 3451 continue; 3452 for (i = 0; i < sband->n_channels; i++) { 3453 chan = &sband->channels[i]; 3454 chan->flags = chan->orig_flags; 3455 chan->max_antenna_gain = chan->orig_mag; 3456 chan->max_power = chan->orig_mpwr; 3457 chan->beacon_found = false; 3458 } 3459 } 3460 } 3461 3462 /* 3463 * Restoring regulatory settings involves ignoring any 3464 * possibly stale country IE information and user regulatory 3465 * settings if so desired, this includes any beacon hints 3466 * learned as we could have traveled outside to another country 3467 * after disconnection. To restore regulatory settings we do 3468 * exactly what we did at bootup: 3469 * 3470 * - send a core regulatory hint 3471 * - send a user regulatory hint if applicable 3472 * 3473 * Device drivers that send a regulatory hint for a specific country 3474 * keep their own regulatory domain on wiphy->regd so that does 3475 * not need to be remembered. 3476 */ 3477 static void restore_regulatory_settings(bool reset_user, bool cached) 3478 { 3479 char alpha2[2]; 3480 char world_alpha2[2]; 3481 struct reg_beacon *reg_beacon, *btmp; 3482 LIST_HEAD(tmp_reg_req_list); 3483 struct cfg80211_registered_device *rdev; 3484 3485 ASSERT_RTNL(); 3486 3487 /* 3488 * Clear the indoor setting in case that it is not controlled by user 3489 * space, as otherwise there is no guarantee that the device is still 3490 * operating in an indoor environment. 3491 */ 3492 spin_lock(®_indoor_lock); 3493 if (reg_is_indoor && !reg_is_indoor_portid) { 3494 reg_is_indoor = false; 3495 reg_check_channels(); 3496 } 3497 spin_unlock(®_indoor_lock); 3498 3499 reset_regdomains(true, &world_regdom); 3500 restore_alpha2(alpha2, reset_user); 3501 3502 /* 3503 * If there's any pending requests we simply 3504 * stash them to a temporary pending queue and 3505 * add then after we've restored regulatory 3506 * settings. 3507 */ 3508 spin_lock(®_requests_lock); 3509 list_splice_tail_init(®_requests_list, &tmp_reg_req_list); 3510 spin_unlock(®_requests_lock); 3511 3512 /* Clear beacon hints */ 3513 spin_lock_bh(®_pending_beacons_lock); 3514 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) { 3515 list_del(®_beacon->list); 3516 kfree(reg_beacon); 3517 } 3518 spin_unlock_bh(®_pending_beacons_lock); 3519 3520 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) { 3521 list_del(®_beacon->list); 3522 kfree(reg_beacon); 3523 } 3524 3525 /* First restore to the basic regulatory settings */ 3526 world_alpha2[0] = cfg80211_world_regdom->alpha2[0]; 3527 world_alpha2[1] = cfg80211_world_regdom->alpha2[1]; 3528 3529 for_each_rdev(rdev) { 3530 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) 3531 continue; 3532 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG) 3533 restore_custom_reg_settings(&rdev->wiphy); 3534 } 3535 3536 if (cached && (!is_an_alpha2(alpha2) || 3537 !IS_ERR_OR_NULL(cfg80211_user_regdom))) { 3538 reset_regdomains(false, cfg80211_world_regdom); 3539 update_all_wiphy_regulatory(NL80211_REGDOM_SET_BY_CORE); 3540 print_regdomain(get_cfg80211_regdom()); 3541 nl80211_send_reg_change_event(&core_request_world); 3542 reg_set_request_processed(); 3543 3544 if (is_an_alpha2(alpha2) && 3545 !regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER)) { 3546 struct regulatory_request *ureq; 3547 3548 spin_lock(®_requests_lock); 3549 ureq = list_last_entry(®_requests_list, 3550 struct regulatory_request, 3551 list); 3552 list_del(&ureq->list); 3553 spin_unlock(®_requests_lock); 3554 3555 notify_self_managed_wiphys(ureq); 3556 reg_update_last_request(ureq); 3557 set_regdom(reg_copy_regd(cfg80211_user_regdom), 3558 REGD_SOURCE_CACHED); 3559 } 3560 } else { 3561 regulatory_hint_core(world_alpha2); 3562 3563 /* 3564 * This restores the ieee80211_regdom module parameter 3565 * preference or the last user requested regulatory 3566 * settings, user regulatory settings takes precedence. 3567 */ 3568 if (is_an_alpha2(alpha2)) 3569 regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER); 3570 } 3571 3572 spin_lock(®_requests_lock); 3573 list_splice_tail_init(&tmp_reg_req_list, ®_requests_list); 3574 spin_unlock(®_requests_lock); 3575 3576 pr_debug("Kicking the queue\n"); 3577 3578 schedule_work(®_work); 3579 } 3580 3581 static bool is_wiphy_all_set_reg_flag(enum ieee80211_regulatory_flags flag) 3582 { 3583 struct cfg80211_registered_device *rdev; 3584 struct wireless_dev *wdev; 3585 3586 for_each_rdev(rdev) { 3587 guard(wiphy)(&rdev->wiphy); 3588 3589 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 3590 if (!(wdev->wiphy->regulatory_flags & flag)) 3591 return false; 3592 } 3593 } 3594 3595 return true; 3596 } 3597 3598 void regulatory_hint_disconnect(void) 3599 { 3600 /* Restore of regulatory settings is not required when wiphy(s) 3601 * ignore IE from connected access point but clearance of beacon hints 3602 * is required when wiphy(s) supports beacon hints. 3603 */ 3604 if (is_wiphy_all_set_reg_flag(REGULATORY_COUNTRY_IE_IGNORE)) { 3605 struct reg_beacon *reg_beacon, *btmp; 3606 3607 if (is_wiphy_all_set_reg_flag(REGULATORY_DISABLE_BEACON_HINTS)) 3608 return; 3609 3610 spin_lock_bh(®_pending_beacons_lock); 3611 list_for_each_entry_safe(reg_beacon, btmp, 3612 ®_pending_beacons, list) { 3613 list_del(®_beacon->list); 3614 kfree(reg_beacon); 3615 } 3616 spin_unlock_bh(®_pending_beacons_lock); 3617 3618 list_for_each_entry_safe(reg_beacon, btmp, 3619 ®_beacon_list, list) { 3620 list_del(®_beacon->list); 3621 kfree(reg_beacon); 3622 } 3623 3624 return; 3625 } 3626 3627 pr_debug("All devices are disconnected, going to restore regulatory settings\n"); 3628 restore_regulatory_settings(false, true); 3629 } 3630 3631 static bool freq_is_chan_12_13_14(u32 freq) 3632 { 3633 if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) || 3634 freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) || 3635 freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ)) 3636 return true; 3637 return false; 3638 } 3639 3640 static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan) 3641 { 3642 struct reg_beacon *pending_beacon; 3643 3644 list_for_each_entry(pending_beacon, ®_pending_beacons, list) 3645 if (ieee80211_channel_equal(beacon_chan, 3646 &pending_beacon->chan)) 3647 return true; 3648 return false; 3649 } 3650 3651 void regulatory_hint_found_beacon(struct wiphy *wiphy, 3652 struct ieee80211_channel *beacon_chan, 3653 gfp_t gfp) 3654 { 3655 struct reg_beacon *reg_beacon; 3656 bool processing; 3657 3658 if (beacon_chan->beacon_found || 3659 beacon_chan->flags & IEEE80211_CHAN_RADAR || 3660 (beacon_chan->band == NL80211_BAND_2GHZ && 3661 !freq_is_chan_12_13_14(beacon_chan->center_freq))) 3662 return; 3663 3664 spin_lock_bh(®_pending_beacons_lock); 3665 processing = pending_reg_beacon(beacon_chan); 3666 spin_unlock_bh(®_pending_beacons_lock); 3667 3668 if (processing) 3669 return; 3670 3671 reg_beacon = kzalloc_obj(struct reg_beacon, gfp); 3672 if (!reg_beacon) 3673 return; 3674 3675 pr_debug("Found new beacon on frequency: %d.%03d MHz (Ch %d) on %s\n", 3676 beacon_chan->center_freq, beacon_chan->freq_offset, 3677 ieee80211_freq_khz_to_channel( 3678 ieee80211_channel_to_khz(beacon_chan)), 3679 wiphy_name(wiphy)); 3680 3681 memcpy(®_beacon->chan, beacon_chan, 3682 sizeof(struct ieee80211_channel)); 3683 3684 /* 3685 * Since we can be called from BH or and non-BH context 3686 * we must use spin_lock_bh() 3687 */ 3688 spin_lock_bh(®_pending_beacons_lock); 3689 list_add_tail(®_beacon->list, ®_pending_beacons); 3690 spin_unlock_bh(®_pending_beacons_lock); 3691 3692 schedule_work(®_work); 3693 } 3694 3695 static void print_rd_rules(const struct ieee80211_regdomain *rd) 3696 { 3697 unsigned int i; 3698 const struct ieee80211_reg_rule *reg_rule = NULL; 3699 const struct ieee80211_freq_range *freq_range = NULL; 3700 const struct ieee80211_power_rule *power_rule = NULL; 3701 char bw[32], cac_time[32]; 3702 3703 pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n"); 3704 3705 for (i = 0; i < rd->n_reg_rules; i++) { 3706 reg_rule = &rd->reg_rules[i]; 3707 freq_range = ®_rule->freq_range; 3708 power_rule = ®_rule->power_rule; 3709 3710 if (reg_rule->flags & NL80211_RRF_AUTO_BW) 3711 snprintf(bw, sizeof(bw), "%d KHz, %u KHz AUTO", 3712 freq_range->max_bandwidth_khz, 3713 reg_get_max_bandwidth(rd, reg_rule)); 3714 else 3715 snprintf(bw, sizeof(bw), "%d KHz", 3716 freq_range->max_bandwidth_khz); 3717 3718 if (reg_rule->flags & NL80211_RRF_DFS) 3719 scnprintf(cac_time, sizeof(cac_time), "%u s", 3720 reg_rule->dfs_cac_ms/1000); 3721 else 3722 scnprintf(cac_time, sizeof(cac_time), "N/A"); 3723 3724 3725 /* 3726 * There may not be documentation for max antenna gain 3727 * in certain regions 3728 */ 3729 if (power_rule->max_antenna_gain) 3730 pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n", 3731 freq_range->start_freq_khz, 3732 freq_range->end_freq_khz, 3733 bw, 3734 power_rule->max_antenna_gain, 3735 power_rule->max_eirp, 3736 cac_time); 3737 else 3738 pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n", 3739 freq_range->start_freq_khz, 3740 freq_range->end_freq_khz, 3741 bw, 3742 power_rule->max_eirp, 3743 cac_time); 3744 } 3745 } 3746 3747 bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region) 3748 { 3749 switch (dfs_region) { 3750 case NL80211_DFS_UNSET: 3751 case NL80211_DFS_FCC: 3752 case NL80211_DFS_ETSI: 3753 case NL80211_DFS_JP: 3754 return true; 3755 default: 3756 pr_debug("Ignoring unknown DFS master region: %d\n", dfs_region); 3757 return false; 3758 } 3759 } 3760 3761 static void print_regdomain(const struct ieee80211_regdomain *rd) 3762 { 3763 struct regulatory_request *lr = get_last_request(); 3764 3765 if (is_intersected_alpha2(rd->alpha2)) { 3766 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) { 3767 struct cfg80211_registered_device *rdev; 3768 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx); 3769 if (rdev) { 3770 pr_debug("Current regulatory domain updated by AP to: %c%c\n", 3771 rdev->country_ie_alpha2[0], 3772 rdev->country_ie_alpha2[1]); 3773 } else 3774 pr_debug("Current regulatory domain intersected:\n"); 3775 } else 3776 pr_debug("Current regulatory domain intersected:\n"); 3777 } else if (is_world_regdom(rd->alpha2)) { 3778 pr_debug("World regulatory domain updated:\n"); 3779 } else { 3780 if (is_unknown_alpha2(rd->alpha2)) 3781 pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n"); 3782 else { 3783 if (reg_request_cell_base(lr)) 3784 pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n", 3785 rd->alpha2[0], rd->alpha2[1]); 3786 else 3787 pr_debug("Regulatory domain changed to country: %c%c\n", 3788 rd->alpha2[0], rd->alpha2[1]); 3789 } 3790 } 3791 3792 pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region)); 3793 print_rd_rules(rd); 3794 } 3795 3796 static void print_regdomain_info(const struct ieee80211_regdomain *rd) 3797 { 3798 pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]); 3799 print_rd_rules(rd); 3800 } 3801 3802 static int reg_set_rd_core(const struct ieee80211_regdomain *rd) 3803 { 3804 if (!is_world_regdom(rd->alpha2)) 3805 return -EINVAL; 3806 update_world_regdomain(rd); 3807 return 0; 3808 } 3809 3810 static int reg_set_rd_user(const struct ieee80211_regdomain *rd, 3811 struct regulatory_request *user_request) 3812 { 3813 const struct ieee80211_regdomain *intersected_rd = NULL; 3814 3815 if (!regdom_changes(rd->alpha2)) 3816 return -EALREADY; 3817 3818 if (!is_valid_rd(rd)) { 3819 pr_err("Invalid regulatory domain detected: %c%c\n", 3820 rd->alpha2[0], rd->alpha2[1]); 3821 print_regdomain_info(rd); 3822 return -EINVAL; 3823 } 3824 3825 if (!user_request->intersect) { 3826 reset_regdomains(false, rd); 3827 return 0; 3828 } 3829 3830 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom()); 3831 if (!intersected_rd) 3832 return -EINVAL; 3833 3834 kfree(rd); 3835 rd = NULL; 3836 reset_regdomains(false, intersected_rd); 3837 3838 return 0; 3839 } 3840 3841 static int reg_set_rd_driver(const struct ieee80211_regdomain *rd, 3842 struct regulatory_request *driver_request) 3843 { 3844 const struct ieee80211_regdomain *regd; 3845 const struct ieee80211_regdomain *intersected_rd = NULL; 3846 const struct ieee80211_regdomain *tmp = NULL; 3847 struct wiphy *request_wiphy; 3848 3849 if (is_world_regdom(rd->alpha2)) 3850 return -EINVAL; 3851 3852 if (!regdom_changes(rd->alpha2)) 3853 return -EALREADY; 3854 3855 if (!is_valid_rd(rd)) { 3856 pr_err("Invalid regulatory domain detected: %c%c\n", 3857 rd->alpha2[0], rd->alpha2[1]); 3858 print_regdomain_info(rd); 3859 return -EINVAL; 3860 } 3861 3862 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx); 3863 if (!request_wiphy) 3864 return -ENODEV; 3865 3866 if (!driver_request->intersect) { 3867 ASSERT_RTNL(); 3868 scoped_guard(wiphy, request_wiphy) { 3869 if (request_wiphy->regd) 3870 tmp = get_wiphy_regdom(request_wiphy); 3871 3872 regd = reg_copy_regd(rd); 3873 if (IS_ERR(regd)) 3874 return PTR_ERR(regd); 3875 3876 rcu_assign_pointer(request_wiphy->regd, regd); 3877 rcu_free_regdom(tmp); 3878 } 3879 3880 reset_regdomains(false, rd); 3881 return 0; 3882 } 3883 3884 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom()); 3885 if (!intersected_rd) 3886 return -EINVAL; 3887 3888 /* 3889 * We can trash what CRDA provided now. 3890 * However if a driver requested this specific regulatory 3891 * domain we keep it for its private use 3892 */ 3893 tmp = get_wiphy_regdom(request_wiphy); 3894 rcu_assign_pointer(request_wiphy->regd, rd); 3895 rcu_free_regdom(tmp); 3896 3897 rd = NULL; 3898 3899 reset_regdomains(false, intersected_rd); 3900 3901 return 0; 3902 } 3903 3904 static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd, 3905 struct regulatory_request *country_ie_request) 3906 { 3907 struct wiphy *request_wiphy; 3908 3909 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && 3910 !is_unknown_alpha2(rd->alpha2)) 3911 return -EINVAL; 3912 3913 /* 3914 * Lets only bother proceeding on the same alpha2 if the current 3915 * rd is non static (it means CRDA was present and was used last) 3916 * and the pending request came in from a country IE 3917 */ 3918 3919 if (!is_valid_rd(rd)) { 3920 pr_err("Invalid regulatory domain detected: %c%c\n", 3921 rd->alpha2[0], rd->alpha2[1]); 3922 print_regdomain_info(rd); 3923 return -EINVAL; 3924 } 3925 3926 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx); 3927 if (!request_wiphy) 3928 return -ENODEV; 3929 3930 if (country_ie_request->intersect) 3931 return -EINVAL; 3932 3933 reset_regdomains(false, rd); 3934 return 0; 3935 } 3936 3937 /* 3938 * Use this call to set the current regulatory domain. Conflicts with 3939 * multiple drivers can be ironed out later. Caller must've already 3940 * kmalloc'd the rd structure. 3941 */ 3942 int set_regdom(const struct ieee80211_regdomain *rd, 3943 enum ieee80211_regd_source regd_src) 3944 { 3945 struct regulatory_request *lr; 3946 bool user_reset = false; 3947 int r; 3948 3949 if (IS_ERR_OR_NULL(rd)) 3950 return -ENODATA; 3951 3952 if (!reg_is_valid_request(rd->alpha2)) { 3953 kfree(rd); 3954 return -EINVAL; 3955 } 3956 3957 if (regd_src == REGD_SOURCE_CRDA) 3958 reset_crda_timeouts(); 3959 3960 lr = get_last_request(); 3961 3962 /* Note that this doesn't update the wiphys, this is done below */ 3963 switch (lr->initiator) { 3964 case NL80211_REGDOM_SET_BY_CORE: 3965 r = reg_set_rd_core(rd); 3966 break; 3967 case NL80211_REGDOM_SET_BY_USER: 3968 cfg80211_save_user_regdom(rd); 3969 r = reg_set_rd_user(rd, lr); 3970 user_reset = true; 3971 break; 3972 case NL80211_REGDOM_SET_BY_DRIVER: 3973 r = reg_set_rd_driver(rd, lr); 3974 break; 3975 case NL80211_REGDOM_SET_BY_COUNTRY_IE: 3976 r = reg_set_rd_country_ie(rd, lr); 3977 break; 3978 default: 3979 WARN(1, "invalid initiator %d\n", lr->initiator); 3980 kfree(rd); 3981 return -EINVAL; 3982 } 3983 3984 if (r) { 3985 switch (r) { 3986 case -EALREADY: 3987 reg_set_request_processed(); 3988 break; 3989 default: 3990 /* Back to world regulatory in case of errors */ 3991 restore_regulatory_settings(user_reset, false); 3992 } 3993 3994 kfree(rd); 3995 return r; 3996 } 3997 3998 /* This would make this whole thing pointless */ 3999 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom())) 4000 return -EINVAL; 4001 4002 /* update all wiphys now with the new established regulatory domain */ 4003 update_all_wiphy_regulatory(lr->initiator); 4004 4005 print_regdomain(get_cfg80211_regdom()); 4006 4007 nl80211_send_reg_change_event(lr); 4008 4009 reg_set_request_processed(); 4010 4011 return 0; 4012 } 4013 4014 static int __regulatory_set_wiphy_regd(struct wiphy *wiphy, 4015 struct ieee80211_regdomain *rd) 4016 { 4017 const struct ieee80211_regdomain *regd; 4018 const struct ieee80211_regdomain *prev_regd; 4019 struct cfg80211_registered_device *rdev; 4020 4021 if (WARN_ON(!wiphy || !rd)) 4022 return -EINVAL; 4023 4024 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED), 4025 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n")) 4026 return -EPERM; 4027 4028 if (WARN(!is_valid_rd(rd), 4029 "Invalid regulatory domain detected: %c%c\n", 4030 rd->alpha2[0], rd->alpha2[1])) { 4031 print_regdomain_info(rd); 4032 return -EINVAL; 4033 } 4034 4035 regd = reg_copy_regd(rd); 4036 if (IS_ERR(regd)) 4037 return PTR_ERR(regd); 4038 4039 rdev = wiphy_to_rdev(wiphy); 4040 4041 spin_lock(®_requests_lock); 4042 prev_regd = rdev->requested_regd; 4043 rdev->requested_regd = regd; 4044 spin_unlock(®_requests_lock); 4045 4046 kfree(prev_regd); 4047 return 0; 4048 } 4049 4050 int regulatory_set_wiphy_regd(struct wiphy *wiphy, 4051 struct ieee80211_regdomain *rd) 4052 { 4053 int ret = __regulatory_set_wiphy_regd(wiphy, rd); 4054 4055 if (ret) 4056 return ret; 4057 4058 schedule_work(®_work); 4059 return 0; 4060 } 4061 EXPORT_SYMBOL(regulatory_set_wiphy_regd); 4062 4063 int regulatory_set_wiphy_regd_sync(struct wiphy *wiphy, 4064 struct ieee80211_regdomain *rd) 4065 { 4066 int ret; 4067 4068 ASSERT_RTNL(); 4069 4070 ret = __regulatory_set_wiphy_regd(wiphy, rd); 4071 if (ret) 4072 return ret; 4073 4074 /* process the request immediately */ 4075 reg_process_self_managed_hint(wiphy); 4076 reg_check_channels(); 4077 return 0; 4078 } 4079 EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync); 4080 4081 void wiphy_regulatory_register(struct wiphy *wiphy) 4082 { 4083 struct regulatory_request *lr = get_last_request(); 4084 4085 /* self-managed devices ignore beacon hints and country IE */ 4086 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) { 4087 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS | 4088 REGULATORY_COUNTRY_IE_IGNORE; 4089 4090 /* 4091 * The last request may have been received before this 4092 * registration call. Call the driver notifier if 4093 * initiator is USER. 4094 */ 4095 if (lr->initiator == NL80211_REGDOM_SET_BY_USER) 4096 reg_call_notifier(wiphy, lr); 4097 } 4098 4099 if (!reg_dev_ignore_cell_hint(wiphy)) 4100 reg_num_devs_support_basehint++; 4101 4102 wiphy_update_regulatory(wiphy, lr->initiator); 4103 wiphy_all_share_dfs_chan_state(wiphy); 4104 reg_process_self_managed_hints(); 4105 } 4106 4107 void wiphy_regulatory_deregister(struct wiphy *wiphy) 4108 { 4109 struct wiphy *request_wiphy = NULL; 4110 struct regulatory_request *lr; 4111 4112 lr = get_last_request(); 4113 4114 if (!reg_dev_ignore_cell_hint(wiphy)) 4115 reg_num_devs_support_basehint--; 4116 4117 rcu_free_regdom(get_wiphy_regdom(wiphy)); 4118 RCU_INIT_POINTER(wiphy->regd, NULL); 4119 4120 if (lr) 4121 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); 4122 4123 if (!request_wiphy || request_wiphy != wiphy) 4124 return; 4125 4126 lr->wiphy_idx = WIPHY_IDX_INVALID; 4127 lr->country_ie_env = ENVIRON_ANY; 4128 } 4129 4130 /* 4131 * See FCC notices for UNII band definitions 4132 * 5GHz: https://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii 4133 * 6GHz: https://www.fcc.gov/document/fcc-proposes-more-spectrum-unlicensed-use-0 4134 */ 4135 int cfg80211_get_unii(int freq) 4136 { 4137 /* UNII-1 */ 4138 if (freq >= 5150 && freq <= 5250) 4139 return 0; 4140 4141 /* UNII-2A */ 4142 if (freq > 5250 && freq <= 5350) 4143 return 1; 4144 4145 /* UNII-2B */ 4146 if (freq > 5350 && freq <= 5470) 4147 return 2; 4148 4149 /* UNII-2C */ 4150 if (freq > 5470 && freq <= 5725) 4151 return 3; 4152 4153 /* UNII-3 */ 4154 if (freq > 5725 && freq <= 5825) 4155 return 4; 4156 4157 /* UNII-5 */ 4158 if (freq > 5925 && freq <= 6425) 4159 return 5; 4160 4161 /* UNII-6 */ 4162 if (freq > 6425 && freq <= 6525) 4163 return 6; 4164 4165 /* UNII-7 */ 4166 if (freq > 6525 && freq <= 6875) 4167 return 7; 4168 4169 /* UNII-8 */ 4170 if (freq > 6875 && freq <= 7125) 4171 return 8; 4172 4173 return -EINVAL; 4174 } 4175 4176 bool regulatory_indoor_allowed(void) 4177 { 4178 return reg_is_indoor; 4179 } 4180 4181 bool regulatory_pre_cac_allowed(struct wiphy *wiphy) 4182 { 4183 const struct ieee80211_regdomain *regd = NULL; 4184 const struct ieee80211_regdomain *wiphy_regd = NULL; 4185 bool pre_cac_allowed = false; 4186 4187 rcu_read_lock(); 4188 4189 regd = rcu_dereference(cfg80211_regdomain); 4190 wiphy_regd = rcu_dereference(wiphy->regd); 4191 if (!wiphy_regd) { 4192 if (regd->dfs_region == NL80211_DFS_ETSI) 4193 pre_cac_allowed = true; 4194 4195 rcu_read_unlock(); 4196 4197 return pre_cac_allowed; 4198 } 4199 4200 if (regd->dfs_region == wiphy_regd->dfs_region && 4201 wiphy_regd->dfs_region == NL80211_DFS_ETSI) 4202 pre_cac_allowed = true; 4203 4204 rcu_read_unlock(); 4205 4206 return pre_cac_allowed; 4207 } 4208 EXPORT_SYMBOL(regulatory_pre_cac_allowed); 4209 4210 static void cfg80211_check_and_end_cac(struct cfg80211_registered_device *rdev) 4211 { 4212 struct wireless_dev *wdev; 4213 unsigned int link_id; 4214 4215 guard(wiphy)(&rdev->wiphy); 4216 4217 /* If we finished CAC or received radar, we should end any 4218 * CAC running on the same channels. 4219 * the check !cfg80211_chandef_dfs_usable contain 2 options: 4220 * either all channels are available - those the CAC_FINISHED 4221 * event has effected another wdev state, or there is a channel 4222 * in unavailable state in wdev chandef - those the RADAR_DETECTED 4223 * event has effected another wdev state. 4224 * In both cases we should end the CAC on the wdev. 4225 */ 4226 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 4227 struct cfg80211_chan_def *chandef; 4228 4229 for_each_valid_link(wdev, link_id) { 4230 if (!wdev->links[link_id].cac_started) 4231 continue; 4232 4233 chandef = wdev_chandef(wdev, link_id); 4234 if (!chandef) 4235 continue; 4236 4237 if (!cfg80211_chandef_dfs_usable(&rdev->wiphy, chandef)) 4238 rdev_end_cac(rdev, wdev->netdev, link_id); 4239 } 4240 } 4241 } 4242 4243 void regulatory_propagate_dfs_state(struct wiphy *wiphy, 4244 struct cfg80211_chan_def *chandef, 4245 enum nl80211_dfs_state dfs_state, 4246 enum nl80211_radar_event event) 4247 { 4248 struct cfg80211_registered_device *rdev; 4249 4250 ASSERT_RTNL(); 4251 4252 if (WARN_ON(!cfg80211_chandef_valid(chandef))) 4253 return; 4254 4255 for_each_rdev(rdev) { 4256 if (wiphy == &rdev->wiphy) 4257 continue; 4258 4259 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy)) 4260 continue; 4261 4262 if (!ieee80211_get_channel(&rdev->wiphy, 4263 chandef->chan->center_freq)) 4264 continue; 4265 4266 cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state); 4267 4268 if (event == NL80211_RADAR_DETECTED || 4269 event == NL80211_RADAR_CAC_FINISHED) { 4270 cfg80211_sched_dfs_chan_update(rdev); 4271 cfg80211_check_and_end_cac(rdev); 4272 } 4273 4274 nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL); 4275 } 4276 } 4277 4278 static int __init regulatory_init_db(void) 4279 { 4280 int err; 4281 4282 /* 4283 * It's possible that - due to other bugs/issues - cfg80211 4284 * never called regulatory_init() below, or that it failed; 4285 * in that case, don't try to do any further work here as 4286 * it's doomed to lead to crashes. 4287 */ 4288 if (!reg_fdev) 4289 return -EINVAL; 4290 4291 err = load_builtin_regdb_keys(); 4292 if (err) { 4293 faux_device_destroy(reg_fdev); 4294 return err; 4295 } 4296 4297 /* We always try to get an update for the static regdomain */ 4298 err = regulatory_hint_core(cfg80211_world_regdom->alpha2); 4299 if (err) { 4300 if (err == -ENOMEM) { 4301 faux_device_destroy(reg_fdev); 4302 return err; 4303 } 4304 /* 4305 * N.B. kobject_uevent_env() can fail mainly for when we're out 4306 * memory which is handled and propagated appropriately above 4307 * but it can also fail during a netlink_broadcast() or during 4308 * early boot for call_usermodehelper(). For now treat these 4309 * errors as non-fatal. 4310 */ 4311 pr_err("kobject_uevent_env() was unable to call CRDA during init\n"); 4312 } 4313 4314 /* 4315 * Finally, if the user set the module parameter treat it 4316 * as a user hint. 4317 */ 4318 if (!is_world_regdom(ieee80211_regdom)) 4319 regulatory_hint_user(ieee80211_regdom, 4320 NL80211_USER_REG_HINT_USER); 4321 4322 return 0; 4323 } 4324 #ifndef MODULE 4325 late_initcall(regulatory_init_db); 4326 #endif 4327 4328 int __init regulatory_init(void) 4329 { 4330 reg_fdev = faux_device_create("regulatory", NULL, NULL); 4331 if (!reg_fdev) 4332 return -ENODEV; 4333 4334 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom); 4335 4336 user_alpha2[0] = '9'; 4337 user_alpha2[1] = '7'; 4338 4339 #ifdef MODULE 4340 return regulatory_init_db(); 4341 #else 4342 return 0; 4343 #endif 4344 } 4345 4346 void regulatory_exit(void) 4347 { 4348 struct regulatory_request *reg_request, *tmp; 4349 struct reg_beacon *reg_beacon, *btmp; 4350 4351 cancel_work_sync(®_work); 4352 cancel_crda_timeout_sync(); 4353 cancel_delayed_work_sync(®_check_chans); 4354 4355 /* Lock to suppress warnings */ 4356 rtnl_lock(); 4357 reset_regdomains(true, NULL); 4358 rtnl_unlock(); 4359 4360 dev_set_uevent_suppress(®_fdev->dev, true); 4361 4362 faux_device_destroy(reg_fdev); 4363 4364 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) { 4365 list_del(®_beacon->list); 4366 kfree(reg_beacon); 4367 } 4368 4369 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) { 4370 list_del(®_beacon->list); 4371 kfree(reg_beacon); 4372 } 4373 4374 list_for_each_entry_safe(reg_request, tmp, ®_requests_list, list) { 4375 list_del(®_request->list); 4376 kfree(reg_request); 4377 } 4378 4379 if (!IS_ERR_OR_NULL(regdb)) 4380 kfree(regdb); 4381 if (!IS_ERR_OR_NULL(cfg80211_user_regdom)) 4382 kfree(cfg80211_user_regdom); 4383 4384 free_regdb_keyring(); 4385 } 4386