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