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