2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2008-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com>
6 * Copyright 2013-2014 Intel Mobile Communications GmbH
7 * Copyright 2017 Intel Deutschland GmbH
8 * Copyright (C) 2018 - 2019 Intel Corporation
10 * Permission to use, copy, modify, and/or distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * DOC: Wireless regulatory infrastructure
27 * The usual implementation is for a driver to read a device EEPROM to
28 * determine which regulatory domain it should be operating under, then
29 * looking up the allowable channels in a driver-local table and finally
30 * registering those channels in the wiphy structure.
32 * Another set of compliance enforcement is for drivers to use their
33 * own compliance limits which can be stored on the EEPROM. The host
34 * driver or firmware may ensure these are used.
36 * In addition to all this we provide an extra layer of regulatory
37 * conformance. For drivers which do not have any regulatory
38 * information CRDA provides the complete regulatory solution.
39 * For others it provides a community effort on further restrictions
40 * to enhance compliance.
42 * Note: When number of rules --> infinity we will not be able to
43 * index on alpha2 any more, instead we'll probably have to
44 * rely on some SHA1 checksum of the regdomain for example.
48 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50 #include <linux/kernel.h>
51 #include <linux/export.h>
52 #include <linux/slab.h>
53 #include <linux/list.h>
54 #include <linux/ctype.h>
55 #include <linux/nl80211.h>
56 #include <linux/platform_device.h>
57 #include <linux/verification.h>
58 #include <linux/moduleparam.h>
59 #include <linux/firmware.h>
60 #include <net/cfg80211.h>
67 * Grace period we give before making sure all current interfaces reside on
68 * channels allowed by the current regulatory domain.
70 #define REG_ENFORCE_GRACE_MS 60000
73 * enum reg_request_treatment - regulatory request treatment
75 * @REG_REQ_OK: continue processing the regulatory request
76 * @REG_REQ_IGNORE: ignore the regulatory request
77 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
78 * be intersected with the current one.
79 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
80 * regulatory settings, and no further processing is required.
82 enum reg_request_treatment {
89 static struct regulatory_request core_request_world = {
90 .initiator = NL80211_REGDOM_SET_BY_CORE,
95 .country_ie_env = ENVIRON_ANY,
99 * Receipt of information from last regulatory request,
100 * protected by RTNL (and can be accessed with RCU protection)
102 static struct regulatory_request __rcu *last_request =
103 (void __force __rcu *)&core_request_world;
105 /* To trigger userspace events and load firmware */
106 static struct platform_device *reg_pdev;
109 * Central wireless core regulatory domains, we only need two,
110 * the current one and a world regulatory domain in case we have no
111 * information to give us an alpha2.
112 * (protected by RTNL, can be read under RCU)
114 const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
117 * Number of devices that registered to the core
118 * that support cellular base station regulatory hints
119 * (protected by RTNL)
121 static int reg_num_devs_support_basehint;
124 * State variable indicating if the platform on which the devices
125 * are attached is operating in an indoor environment. The state variable
126 * is relevant for all registered devices.
128 static bool reg_is_indoor;
129 static spinlock_t reg_indoor_lock;
131 /* Used to track the userspace process controlling the indoor setting */
132 static u32 reg_is_indoor_portid;
134 static void restore_regulatory_settings(bool reset_user, bool cached);
135 static void print_regdomain(const struct ieee80211_regdomain *rd);
137 static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
139 return rcu_dereference_rtnl(cfg80211_regdomain);
142 const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
144 return rcu_dereference_rtnl(wiphy->regd);
147 static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
149 switch (dfs_region) {
150 case NL80211_DFS_UNSET:
152 case NL80211_DFS_FCC:
154 case NL80211_DFS_ETSI:
162 enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
164 const struct ieee80211_regdomain *regd = NULL;
165 const struct ieee80211_regdomain *wiphy_regd = NULL;
167 regd = get_cfg80211_regdom();
171 wiphy_regd = get_wiphy_regdom(wiphy);
175 if (wiphy_regd->dfs_region == regd->dfs_region)
178 pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n",
179 dev_name(&wiphy->dev),
180 reg_dfs_region_str(wiphy_regd->dfs_region),
181 reg_dfs_region_str(regd->dfs_region));
184 return regd->dfs_region;
187 static void rcu_free_regdom(const struct ieee80211_regdomain *r)
191 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
194 static struct regulatory_request *get_last_request(void)
196 return rcu_dereference_rtnl(last_request);
199 /* Used to queue up regulatory hints */
200 static LIST_HEAD(reg_requests_list);
201 static spinlock_t reg_requests_lock;
203 /* Used to queue up beacon hints for review */
204 static LIST_HEAD(reg_pending_beacons);
205 static spinlock_t reg_pending_beacons_lock;
207 /* Used to keep track of processed beacon hints */
208 static LIST_HEAD(reg_beacon_list);
211 struct list_head list;
212 struct ieee80211_channel chan;
215 static void reg_check_chans_work(struct work_struct *work);
216 static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);
218 static void reg_todo(struct work_struct *work);
219 static DECLARE_WORK(reg_work, reg_todo);
221 /* We keep a static world regulatory domain in case of the absence of CRDA */
222 static const struct ieee80211_regdomain world_regdom = {
226 /* IEEE 802.11b/g, channels 1..11 */
227 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
228 /* IEEE 802.11b/g, channels 12..13. */
229 REG_RULE(2467-10, 2472+10, 20, 6, 20,
230 NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW),
231 /* IEEE 802.11 channel 14 - Only JP enables
232 * this and for 802.11b only */
233 REG_RULE(2484-10, 2484+10, 20, 6, 20,
235 NL80211_RRF_NO_OFDM),
236 /* IEEE 802.11a, channel 36..48 */
237 REG_RULE(5180-10, 5240+10, 80, 6, 20,
239 NL80211_RRF_AUTO_BW),
241 /* IEEE 802.11a, channel 52..64 - DFS required */
242 REG_RULE(5260-10, 5320+10, 80, 6, 20,
244 NL80211_RRF_AUTO_BW |
247 /* IEEE 802.11a, channel 100..144 - DFS required */
248 REG_RULE(5500-10, 5720+10, 160, 6, 20,
252 /* IEEE 802.11a, channel 149..165 */
253 REG_RULE(5745-10, 5825+10, 80, 6, 20,
256 /* IEEE 802.11ad (60GHz), channels 1..3 */
257 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
261 /* protected by RTNL */
262 static const struct ieee80211_regdomain *cfg80211_world_regdom =
265 static char *ieee80211_regdom = "00";
266 static char user_alpha2[2];
267 static const struct ieee80211_regdomain *cfg80211_user_regdom;
269 module_param(ieee80211_regdom, charp, 0444);
270 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
272 static void reg_free_request(struct regulatory_request *request)
274 if (request == &core_request_world)
277 if (request != get_last_request())
281 static void reg_free_last_request(void)
283 struct regulatory_request *lr = get_last_request();
285 if (lr != &core_request_world && lr)
286 kfree_rcu(lr, rcu_head);
289 static void reg_update_last_request(struct regulatory_request *request)
291 struct regulatory_request *lr;
293 lr = get_last_request();
297 reg_free_last_request();
298 rcu_assign_pointer(last_request, request);
301 static void reset_regdomains(bool full_reset,
302 const struct ieee80211_regdomain *new_regdom)
304 const struct ieee80211_regdomain *r;
308 r = get_cfg80211_regdom();
310 /* avoid freeing static information or freeing something twice */
311 if (r == cfg80211_world_regdom)
313 if (cfg80211_world_regdom == &world_regdom)
314 cfg80211_world_regdom = NULL;
315 if (r == &world_regdom)
319 rcu_free_regdom(cfg80211_world_regdom);
321 cfg80211_world_regdom = &world_regdom;
322 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
327 reg_update_last_request(&core_request_world);
331 * Dynamic world regulatory domain requested by the wireless
332 * core upon initialization
334 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
336 struct regulatory_request *lr;
338 lr = get_last_request();
342 reset_regdomains(false, rd);
344 cfg80211_world_regdom = rd;
347 bool is_world_regdom(const char *alpha2)
351 return alpha2[0] == '0' && alpha2[1] == '0';
354 static bool is_alpha2_set(const char *alpha2)
358 return alpha2[0] && alpha2[1];
361 static bool is_unknown_alpha2(const char *alpha2)
366 * Special case where regulatory domain was built by driver
367 * but a specific alpha2 cannot be determined
369 return alpha2[0] == '9' && alpha2[1] == '9';
372 static bool is_intersected_alpha2(const char *alpha2)
377 * Special case where regulatory domain is the
378 * result of an intersection between two regulatory domain
381 return alpha2[0] == '9' && alpha2[1] == '8';
384 static bool is_an_alpha2(const char *alpha2)
388 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
391 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
393 if (!alpha2_x || !alpha2_y)
395 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
398 static bool regdom_changes(const char *alpha2)
400 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
404 return !alpha2_equal(r->alpha2, alpha2);
408 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
409 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
410 * has ever been issued.
412 static bool is_user_regdom_saved(void)
414 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
417 /* This would indicate a mistake on the design */
418 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
419 "Unexpected user alpha2: %c%c\n",
420 user_alpha2[0], user_alpha2[1]))
426 static const struct ieee80211_regdomain *
427 reg_copy_regd(const struct ieee80211_regdomain *src_regd)
429 struct ieee80211_regdomain *regd;
432 regd = kzalloc(struct_size(regd, reg_rules, src_regd->n_reg_rules),
435 return ERR_PTR(-ENOMEM);
437 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
439 for (i = 0; i < src_regd->n_reg_rules; i++)
440 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i],
441 sizeof(struct ieee80211_reg_rule));
446 static void cfg80211_save_user_regdom(const struct ieee80211_regdomain *rd)
450 if (!IS_ERR(cfg80211_user_regdom))
451 kfree(cfg80211_user_regdom);
452 cfg80211_user_regdom = reg_copy_regd(rd);
455 struct reg_regdb_apply_request {
456 struct list_head list;
457 const struct ieee80211_regdomain *regdom;
460 static LIST_HEAD(reg_regdb_apply_list);
461 static DEFINE_MUTEX(reg_regdb_apply_mutex);
463 static void reg_regdb_apply(struct work_struct *work)
465 struct reg_regdb_apply_request *request;
469 mutex_lock(®_regdb_apply_mutex);
470 while (!list_empty(®_regdb_apply_list)) {
471 request = list_first_entry(®_regdb_apply_list,
472 struct reg_regdb_apply_request,
474 list_del(&request->list);
476 set_regdom(request->regdom, REGD_SOURCE_INTERNAL_DB);
479 mutex_unlock(®_regdb_apply_mutex);
484 static DECLARE_WORK(reg_regdb_work, reg_regdb_apply);
486 static int reg_schedule_apply(const struct ieee80211_regdomain *regdom)
488 struct reg_regdb_apply_request *request;
490 request = kzalloc(sizeof(struct reg_regdb_apply_request), GFP_KERNEL);
496 request->regdom = regdom;
498 mutex_lock(®_regdb_apply_mutex);
499 list_add_tail(&request->list, ®_regdb_apply_list);
500 mutex_unlock(®_regdb_apply_mutex);
502 schedule_work(®_regdb_work);
506 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
507 /* Max number of consecutive attempts to communicate with CRDA */
508 #define REG_MAX_CRDA_TIMEOUTS 10
510 static u32 reg_crda_timeouts;
512 static void crda_timeout_work(struct work_struct *work);
513 static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work);
515 static void crda_timeout_work(struct work_struct *work)
517 pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
520 restore_regulatory_settings(true, false);
524 static void cancel_crda_timeout(void)
526 cancel_delayed_work(&crda_timeout);
529 static void cancel_crda_timeout_sync(void)
531 cancel_delayed_work_sync(&crda_timeout);
534 static void reset_crda_timeouts(void)
536 reg_crda_timeouts = 0;
540 * This lets us keep regulatory code which is updated on a regulatory
541 * basis in userspace.
543 static int call_crda(const char *alpha2)
546 char *env[] = { country, NULL };
549 snprintf(country, sizeof(country), "COUNTRY=%c%c",
550 alpha2[0], alpha2[1]);
552 if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) {
553 pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n");
557 if (!is_world_regdom((char *) alpha2))
558 pr_debug("Calling CRDA for country: %c%c\n",
559 alpha2[0], alpha2[1]);
561 pr_debug("Calling CRDA to update world regulatory domain\n");
563 ret = kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, env);
567 queue_delayed_work(system_power_efficient_wq,
568 &crda_timeout, msecs_to_jiffies(3142));
572 static inline void cancel_crda_timeout(void) {}
573 static inline void cancel_crda_timeout_sync(void) {}
574 static inline void reset_crda_timeouts(void) {}
575 static inline int call_crda(const char *alpha2)
579 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
581 /* code to directly load a firmware database through request_firmware */
582 static const struct fwdb_header *regdb;
584 struct fwdb_country {
587 /* this struct cannot be extended */
588 } __packed __aligned(4);
590 struct fwdb_collection {
594 /* no optional data yet */
595 /* aligned to 2, then followed by __be16 array of rule pointers */
596 } __packed __aligned(4);
599 FWDB_FLAG_NO_OFDM = BIT(0),
600 FWDB_FLAG_NO_OUTDOOR = BIT(1),
601 FWDB_FLAG_DFS = BIT(2),
602 FWDB_FLAG_NO_IR = BIT(3),
603 FWDB_FLAG_AUTO_BW = BIT(4),
612 struct fwdb_wmm_rule {
613 struct fwdb_wmm_ac client[IEEE80211_NUM_ACS];
614 struct fwdb_wmm_ac ap[IEEE80211_NUM_ACS];
621 __be32 start, end, max_bw;
622 /* start of optional data */
625 } __packed __aligned(4);
627 #define FWDB_MAGIC 0x52474442
628 #define FWDB_VERSION 20
633 struct fwdb_country country[];
634 } __packed __aligned(4);
636 static int ecw2cw(int ecw)
638 return (1 << ecw) - 1;
641 static bool valid_wmm(struct fwdb_wmm_rule *rule)
643 struct fwdb_wmm_ac *ac = (struct fwdb_wmm_ac *)rule;
646 for (i = 0; i < IEEE80211_NUM_ACS * 2; i++) {
647 u16 cw_min = ecw2cw((ac[i].ecw & 0xf0) >> 4);
648 u16 cw_max = ecw2cw(ac[i].ecw & 0x0f);
649 u8 aifsn = ac[i].aifsn;
651 if (cw_min >= cw_max)
661 static bool valid_rule(const u8 *data, unsigned int size, u16 rule_ptr)
663 struct fwdb_rule *rule = (void *)(data + (rule_ptr << 2));
665 if ((u8 *)rule + sizeof(rule->len) > data + size)
668 /* mandatory fields */
669 if (rule->len < offsetofend(struct fwdb_rule, max_bw))
671 if (rule->len >= offsetofend(struct fwdb_rule, wmm_ptr)) {
672 u32 wmm_ptr = be16_to_cpu(rule->wmm_ptr) << 2;
673 struct fwdb_wmm_rule *wmm;
675 if (wmm_ptr + sizeof(struct fwdb_wmm_rule) > size)
678 wmm = (void *)(data + wmm_ptr);
686 static bool valid_country(const u8 *data, unsigned int size,
687 const struct fwdb_country *country)
689 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
690 struct fwdb_collection *coll = (void *)(data + ptr);
694 /* make sure we can read len/n_rules */
695 if ((u8 *)coll + offsetofend(typeof(*coll), n_rules) > data + size)
698 /* make sure base struct and all rules fit */
699 if ((u8 *)coll + ALIGN(coll->len, 2) +
700 (coll->n_rules * 2) > data + size)
703 /* mandatory fields must exist */
704 if (coll->len < offsetofend(struct fwdb_collection, dfs_region))
707 rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
709 for (i = 0; i < coll->n_rules; i++) {
710 u16 rule_ptr = be16_to_cpu(rules_ptr[i]);
712 if (!valid_rule(data, size, rule_ptr))
719 #ifdef CONFIG_CFG80211_REQUIRE_SIGNED_REGDB
720 static struct key *builtin_regdb_keys;
722 static void __init load_keys_from_buffer(const u8 *p, unsigned int buflen)
724 const u8 *end = p + buflen;
729 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
730 * than 256 bytes in size.
737 plen = (p[2] << 8) | p[3];
742 key = key_create_or_update(make_key_ref(builtin_regdb_keys, 1),
743 "asymmetric", NULL, p, plen,
744 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
745 KEY_USR_VIEW | KEY_USR_READ),
746 KEY_ALLOC_NOT_IN_QUOTA |
748 KEY_ALLOC_BYPASS_RESTRICTION);
750 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
753 pr_notice("Loaded X.509 cert '%s'\n",
754 key_ref_to_ptr(key)->description);
763 pr_err("Problem parsing in-kernel X.509 certificate list\n");
766 static int __init load_builtin_regdb_keys(void)
769 keyring_alloc(".builtin_regdb_keys",
770 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
771 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
772 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
773 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
774 if (IS_ERR(builtin_regdb_keys))
775 return PTR_ERR(builtin_regdb_keys);
777 pr_notice("Loading compiled-in X.509 certificates for regulatory database\n");
779 #ifdef CONFIG_CFG80211_USE_KERNEL_REGDB_KEYS
780 load_keys_from_buffer(shipped_regdb_certs, shipped_regdb_certs_len);
782 #ifdef CONFIG_CFG80211_EXTRA_REGDB_KEYDIR
783 if (CONFIG_CFG80211_EXTRA_REGDB_KEYDIR[0] != '\0')
784 load_keys_from_buffer(extra_regdb_certs, extra_regdb_certs_len);
790 MODULE_FIRMWARE("regulatory.db.p7s");
792 static bool regdb_has_valid_signature(const u8 *data, unsigned int size)
794 const struct firmware *sig;
797 if (request_firmware(&sig, "regulatory.db.p7s", ®_pdev->dev))
800 result = verify_pkcs7_signature(data, size, sig->data, sig->size,
802 VERIFYING_UNSPECIFIED_SIGNATURE,
805 release_firmware(sig);
810 static void free_regdb_keyring(void)
812 key_put(builtin_regdb_keys);
815 static int load_builtin_regdb_keys(void)
820 static bool regdb_has_valid_signature(const u8 *data, unsigned int size)
825 static void free_regdb_keyring(void)
828 #endif /* CONFIG_CFG80211_REQUIRE_SIGNED_REGDB */
830 static bool valid_regdb(const u8 *data, unsigned int size)
832 const struct fwdb_header *hdr = (void *)data;
833 const struct fwdb_country *country;
835 if (size < sizeof(*hdr))
838 if (hdr->magic != cpu_to_be32(FWDB_MAGIC))
841 if (hdr->version != cpu_to_be32(FWDB_VERSION))
844 if (!regdb_has_valid_signature(data, size))
847 country = &hdr->country[0];
848 while ((u8 *)(country + 1) <= data + size) {
849 if (!country->coll_ptr)
851 if (!valid_country(data, size, country))
859 static void set_wmm_rule(const struct fwdb_header *db,
860 const struct fwdb_country *country,
861 const struct fwdb_rule *rule,
862 struct ieee80211_reg_rule *rrule)
864 struct ieee80211_wmm_rule *wmm_rule = &rrule->wmm_rule;
865 struct fwdb_wmm_rule *wmm;
866 unsigned int i, wmm_ptr;
868 wmm_ptr = be16_to_cpu(rule->wmm_ptr) << 2;
869 wmm = (void *)((u8 *)db + wmm_ptr);
871 if (!valid_wmm(wmm)) {
872 pr_err("Invalid regulatory WMM rule %u-%u in domain %c%c\n",
873 be32_to_cpu(rule->start), be32_to_cpu(rule->end),
874 country->alpha2[0], country->alpha2[1]);
878 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
879 wmm_rule->client[i].cw_min =
880 ecw2cw((wmm->client[i].ecw & 0xf0) >> 4);
881 wmm_rule->client[i].cw_max = ecw2cw(wmm->client[i].ecw & 0x0f);
882 wmm_rule->client[i].aifsn = wmm->client[i].aifsn;
883 wmm_rule->client[i].cot =
884 1000 * be16_to_cpu(wmm->client[i].cot);
885 wmm_rule->ap[i].cw_min = ecw2cw((wmm->ap[i].ecw & 0xf0) >> 4);
886 wmm_rule->ap[i].cw_max = ecw2cw(wmm->ap[i].ecw & 0x0f);
887 wmm_rule->ap[i].aifsn = wmm->ap[i].aifsn;
888 wmm_rule->ap[i].cot = 1000 * be16_to_cpu(wmm->ap[i].cot);
891 rrule->has_wmm = true;
894 static int __regdb_query_wmm(const struct fwdb_header *db,
895 const struct fwdb_country *country, int freq,
896 struct ieee80211_reg_rule *rrule)
898 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
899 struct fwdb_collection *coll = (void *)((u8 *)db + ptr);
902 for (i = 0; i < coll->n_rules; i++) {
903 __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
904 unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2;
905 struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr);
907 if (rule->len < offsetofend(struct fwdb_rule, wmm_ptr))
910 if (freq >= KHZ_TO_MHZ(be32_to_cpu(rule->start)) &&
911 freq <= KHZ_TO_MHZ(be32_to_cpu(rule->end))) {
912 set_wmm_rule(db, country, rule, rrule);
920 int reg_query_regdb_wmm(char *alpha2, int freq, struct ieee80211_reg_rule *rule)
922 const struct fwdb_header *hdr = regdb;
923 const struct fwdb_country *country;
929 return PTR_ERR(regdb);
931 country = &hdr->country[0];
932 while (country->coll_ptr) {
933 if (alpha2_equal(alpha2, country->alpha2))
934 return __regdb_query_wmm(regdb, country, freq, rule);
941 EXPORT_SYMBOL(reg_query_regdb_wmm);
943 static int regdb_query_country(const struct fwdb_header *db,
944 const struct fwdb_country *country)
946 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
947 struct fwdb_collection *coll = (void *)((u8 *)db + ptr);
948 struct ieee80211_regdomain *regdom;
951 regdom = kzalloc(struct_size(regdom, reg_rules, coll->n_rules),
956 regdom->n_reg_rules = coll->n_rules;
957 regdom->alpha2[0] = country->alpha2[0];
958 regdom->alpha2[1] = country->alpha2[1];
959 regdom->dfs_region = coll->dfs_region;
961 for (i = 0; i < regdom->n_reg_rules; i++) {
962 __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
963 unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2;
964 struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr);
965 struct ieee80211_reg_rule *rrule = ®dom->reg_rules[i];
967 rrule->freq_range.start_freq_khz = be32_to_cpu(rule->start);
968 rrule->freq_range.end_freq_khz = be32_to_cpu(rule->end);
969 rrule->freq_range.max_bandwidth_khz = be32_to_cpu(rule->max_bw);
971 rrule->power_rule.max_antenna_gain = 0;
972 rrule->power_rule.max_eirp = be16_to_cpu(rule->max_eirp);
975 if (rule->flags & FWDB_FLAG_NO_OFDM)
976 rrule->flags |= NL80211_RRF_NO_OFDM;
977 if (rule->flags & FWDB_FLAG_NO_OUTDOOR)
978 rrule->flags |= NL80211_RRF_NO_OUTDOOR;
979 if (rule->flags & FWDB_FLAG_DFS)
980 rrule->flags |= NL80211_RRF_DFS;
981 if (rule->flags & FWDB_FLAG_NO_IR)
982 rrule->flags |= NL80211_RRF_NO_IR;
983 if (rule->flags & FWDB_FLAG_AUTO_BW)
984 rrule->flags |= NL80211_RRF_AUTO_BW;
986 rrule->dfs_cac_ms = 0;
988 /* handle optional data */
989 if (rule->len >= offsetofend(struct fwdb_rule, cac_timeout))
991 1000 * be16_to_cpu(rule->cac_timeout);
992 if (rule->len >= offsetofend(struct fwdb_rule, wmm_ptr))
993 set_wmm_rule(db, country, rule, rrule);
996 return reg_schedule_apply(regdom);
999 static int query_regdb(const char *alpha2)
1001 const struct fwdb_header *hdr = regdb;
1002 const struct fwdb_country *country;
1007 return PTR_ERR(regdb);
1009 country = &hdr->country[0];
1010 while (country->coll_ptr) {
1011 if (alpha2_equal(alpha2, country->alpha2))
1012 return regdb_query_country(regdb, country);
1019 static void regdb_fw_cb(const struct firmware *fw, void *context)
1022 bool restore = true;
1026 pr_info("failed to load regulatory.db\n");
1027 set_error = -ENODATA;
1028 } else if (!valid_regdb(fw->data, fw->size)) {
1029 pr_info("loaded regulatory.db is malformed or signature is missing/invalid\n");
1030 set_error = -EINVAL;
1034 if (regdb && !IS_ERR(regdb)) {
1035 /* negative case - a bug
1036 * positive case - can happen due to race in case of multiple cb's in
1037 * queue, due to usage of asynchronous callback
1039 * Either case, just restore and free new db.
1041 } else if (set_error) {
1042 regdb = ERR_PTR(set_error);
1044 db = kmemdup(fw->data, fw->size, GFP_KERNEL);
1047 restore = context && query_regdb(context);
1054 restore_regulatory_settings(true, false);
1060 release_firmware(fw);
1063 MODULE_FIRMWARE("regulatory.db");
1065 static int query_regdb_file(const char *alpha2)
1072 return query_regdb(alpha2);
1074 alpha2 = kmemdup(alpha2, 2, GFP_KERNEL);
1078 err = request_firmware_nowait(THIS_MODULE, true, "regulatory.db",
1079 ®_pdev->dev, GFP_KERNEL,
1080 (void *)alpha2, regdb_fw_cb);
1087 int reg_reload_regdb(void)
1089 const struct firmware *fw;
1093 err = request_firmware(&fw, "regulatory.db", ®_pdev->dev);
1097 if (!valid_regdb(fw->data, fw->size)) {
1102 db = kmemdup(fw->data, fw->size, GFP_KERNEL);
1109 if (!IS_ERR_OR_NULL(regdb))
1115 release_firmware(fw);
1119 static bool reg_query_database(struct regulatory_request *request)
1121 if (query_regdb_file(request->alpha2) == 0)
1124 if (call_crda(request->alpha2) == 0)
1130 bool reg_is_valid_request(const char *alpha2)
1132 struct regulatory_request *lr = get_last_request();
1134 if (!lr || lr->processed)
1137 return alpha2_equal(lr->alpha2, alpha2);
1140 static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
1142 struct regulatory_request *lr = get_last_request();
1145 * Follow the driver's regulatory domain, if present, unless a country
1146 * IE has been processed or a user wants to help complaince further
1148 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1149 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
1151 return get_wiphy_regdom(wiphy);
1153 return get_cfg80211_regdom();
1157 reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
1158 const struct ieee80211_reg_rule *rule)
1160 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
1161 const struct ieee80211_freq_range *freq_range_tmp;
1162 const struct ieee80211_reg_rule *tmp;
1163 u32 start_freq, end_freq, idx, no;
1165 for (idx = 0; idx < rd->n_reg_rules; idx++)
1166 if (rule == &rd->reg_rules[idx])
1169 if (idx == rd->n_reg_rules)
1172 /* get start_freq */
1176 tmp = &rd->reg_rules[--no];
1177 freq_range_tmp = &tmp->freq_range;
1179 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
1182 freq_range = freq_range_tmp;
1185 start_freq = freq_range->start_freq_khz;
1188 freq_range = &rule->freq_range;
1191 while (no < rd->n_reg_rules - 1) {
1192 tmp = &rd->reg_rules[++no];
1193 freq_range_tmp = &tmp->freq_range;
1195 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
1198 freq_range = freq_range_tmp;
1201 end_freq = freq_range->end_freq_khz;
1203 return end_freq - start_freq;
1206 unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
1207 const struct ieee80211_reg_rule *rule)
1209 unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);
1211 if (rule->flags & NL80211_RRF_NO_160MHZ)
1212 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
1213 if (rule->flags & NL80211_RRF_NO_80MHZ)
1214 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));
1217 * HT40+/HT40- limits are handled per-channel. Only limit BW if both
1220 if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
1221 rule->flags & NL80211_RRF_NO_HT40PLUS)
1222 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));
1227 /* Sanity check on a regulatory rule */
1228 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
1230 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
1233 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
1236 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
1239 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1241 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
1242 freq_range->max_bandwidth_khz > freq_diff)
1248 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
1250 const struct ieee80211_reg_rule *reg_rule = NULL;
1253 if (!rd->n_reg_rules)
1256 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
1259 for (i = 0; i < rd->n_reg_rules; i++) {
1260 reg_rule = &rd->reg_rules[i];
1261 if (!is_valid_reg_rule(reg_rule))
1269 * freq_in_rule_band - tells us if a frequency is in a frequency band
1270 * @freq_range: frequency rule we want to query
1271 * @freq_khz: frequency we are inquiring about
1273 * This lets us know if a specific frequency rule is or is not relevant to
1274 * a specific frequency's band. Bands are device specific and artificial
1275 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
1276 * however it is safe for now to assume that a frequency rule should not be
1277 * part of a frequency's band if the start freq or end freq are off by more
1278 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 20 GHz for the
1280 * This resolution can be lowered and should be considered as we add
1281 * regulatory rule support for other "bands".
1283 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
1286 #define ONE_GHZ_IN_KHZ 1000000
1288 * From 802.11ad: directional multi-gigabit (DMG):
1289 * Pertaining to operation in a frequency band containing a channel
1290 * with the Channel starting frequency above 45 GHz.
1292 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
1293 20 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
1294 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
1296 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
1299 #undef ONE_GHZ_IN_KHZ
1303 * Later on we can perhaps use the more restrictive DFS
1304 * region but we don't have information for that yet so
1305 * for now simply disallow conflicts.
1307 static enum nl80211_dfs_regions
1308 reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
1309 const enum nl80211_dfs_regions dfs_region2)
1311 if (dfs_region1 != dfs_region2)
1312 return NL80211_DFS_UNSET;
1316 static void reg_wmm_rules_intersect(const struct ieee80211_wmm_ac *wmm_ac1,
1317 const struct ieee80211_wmm_ac *wmm_ac2,
1318 struct ieee80211_wmm_ac *intersect)
1320 intersect->cw_min = max_t(u16, wmm_ac1->cw_min, wmm_ac2->cw_min);
1321 intersect->cw_max = max_t(u16, wmm_ac1->cw_max, wmm_ac2->cw_max);
1322 intersect->cot = min_t(u16, wmm_ac1->cot, wmm_ac2->cot);
1323 intersect->aifsn = max_t(u8, wmm_ac1->aifsn, wmm_ac2->aifsn);
1327 * Helper for regdom_intersect(), this does the real
1328 * mathematical intersection fun
1330 static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
1331 const struct ieee80211_regdomain *rd2,
1332 const struct ieee80211_reg_rule *rule1,
1333 const struct ieee80211_reg_rule *rule2,
1334 struct ieee80211_reg_rule *intersected_rule)
1336 const struct ieee80211_freq_range *freq_range1, *freq_range2;
1337 struct ieee80211_freq_range *freq_range;
1338 const struct ieee80211_power_rule *power_rule1, *power_rule2;
1339 struct ieee80211_power_rule *power_rule;
1340 const struct ieee80211_wmm_rule *wmm_rule1, *wmm_rule2;
1341 struct ieee80211_wmm_rule *wmm_rule;
1342 u32 freq_diff, max_bandwidth1, max_bandwidth2;
1344 freq_range1 = &rule1->freq_range;
1345 freq_range2 = &rule2->freq_range;
1346 freq_range = &intersected_rule->freq_range;
1348 power_rule1 = &rule1->power_rule;
1349 power_rule2 = &rule2->power_rule;
1350 power_rule = &intersected_rule->power_rule;
1352 wmm_rule1 = &rule1->wmm_rule;
1353 wmm_rule2 = &rule2->wmm_rule;
1354 wmm_rule = &intersected_rule->wmm_rule;
1356 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
1357 freq_range2->start_freq_khz);
1358 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
1359 freq_range2->end_freq_khz);
1361 max_bandwidth1 = freq_range1->max_bandwidth_khz;
1362 max_bandwidth2 = freq_range2->max_bandwidth_khz;
1364 if (rule1->flags & NL80211_RRF_AUTO_BW)
1365 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
1366 if (rule2->flags & NL80211_RRF_AUTO_BW)
1367 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
1369 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
1371 intersected_rule->flags = rule1->flags | rule2->flags;
1374 * In case NL80211_RRF_AUTO_BW requested for both rules
1375 * set AUTO_BW in intersected rule also. Next we will
1376 * calculate BW correctly in handle_channel function.
1377 * In other case remove AUTO_BW flag while we calculate
1378 * maximum bandwidth correctly and auto calculation is
1381 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
1382 (rule2->flags & NL80211_RRF_AUTO_BW))
1383 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
1385 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
1387 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1388 if (freq_range->max_bandwidth_khz > freq_diff)
1389 freq_range->max_bandwidth_khz = freq_diff;
1391 power_rule->max_eirp = min(power_rule1->max_eirp,
1392 power_rule2->max_eirp);
1393 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
1394 power_rule2->max_antenna_gain);
1396 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
1399 if (rule1->has_wmm && rule2->has_wmm) {
1402 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1403 reg_wmm_rules_intersect(&wmm_rule1->client[ac],
1404 &wmm_rule2->client[ac],
1405 &wmm_rule->client[ac]);
1406 reg_wmm_rules_intersect(&wmm_rule1->ap[ac],
1411 intersected_rule->has_wmm = true;
1412 } else if (rule1->has_wmm) {
1413 *wmm_rule = *wmm_rule1;
1414 intersected_rule->has_wmm = true;
1415 } else if (rule2->has_wmm) {
1416 *wmm_rule = *wmm_rule2;
1417 intersected_rule->has_wmm = true;
1419 intersected_rule->has_wmm = false;
1422 if (!is_valid_reg_rule(intersected_rule))
1428 /* check whether old rule contains new rule */
1429 static bool rule_contains(struct ieee80211_reg_rule *r1,
1430 struct ieee80211_reg_rule *r2)
1432 /* for simplicity, currently consider only same flags */
1433 if (r1->flags != r2->flags)
1436 /* verify r1 is more restrictive */
1437 if ((r1->power_rule.max_antenna_gain >
1438 r2->power_rule.max_antenna_gain) ||
1439 r1->power_rule.max_eirp > r2->power_rule.max_eirp)
1442 /* make sure r2's range is contained within r1 */
1443 if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
1444 r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
1447 /* and finally verify that r1.max_bw >= r2.max_bw */
1448 if (r1->freq_range.max_bandwidth_khz <
1449 r2->freq_range.max_bandwidth_khz)
1455 /* add or extend current rules. do nothing if rule is already contained */
1456 static void add_rule(struct ieee80211_reg_rule *rule,
1457 struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
1459 struct ieee80211_reg_rule *tmp_rule;
1462 for (i = 0; i < *n_rules; i++) {
1463 tmp_rule = ®_rules[i];
1464 /* rule is already contained - do nothing */
1465 if (rule_contains(tmp_rule, rule))
1468 /* extend rule if possible */
1469 if (rule_contains(rule, tmp_rule)) {
1470 memcpy(tmp_rule, rule, sizeof(*rule));
1475 memcpy(®_rules[*n_rules], rule, sizeof(*rule));
1480 * regdom_intersect - do the intersection between two regulatory domains
1481 * @rd1: first regulatory domain
1482 * @rd2: second regulatory domain
1484 * Use this function to get the intersection between two regulatory domains.
1485 * Once completed we will mark the alpha2 for the rd as intersected, "98",
1486 * as no one single alpha2 can represent this regulatory domain.
1488 * Returns a pointer to the regulatory domain structure which will hold the
1489 * resulting intersection of rules between rd1 and rd2. We will
1490 * kzalloc() this structure for you.
1492 static struct ieee80211_regdomain *
1493 regdom_intersect(const struct ieee80211_regdomain *rd1,
1494 const struct ieee80211_regdomain *rd2)
1498 unsigned int num_rules = 0;
1499 const struct ieee80211_reg_rule *rule1, *rule2;
1500 struct ieee80211_reg_rule intersected_rule;
1501 struct ieee80211_regdomain *rd;
1507 * First we get a count of the rules we'll need, then we actually
1508 * build them. This is to so we can malloc() and free() a
1509 * regdomain once. The reason we use reg_rules_intersect() here
1510 * is it will return -EINVAL if the rule computed makes no sense.
1511 * All rules that do check out OK are valid.
1514 for (x = 0; x < rd1->n_reg_rules; x++) {
1515 rule1 = &rd1->reg_rules[x];
1516 for (y = 0; y < rd2->n_reg_rules; y++) {
1517 rule2 = &rd2->reg_rules[y];
1518 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
1527 rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
1531 for (x = 0; x < rd1->n_reg_rules; x++) {
1532 rule1 = &rd1->reg_rules[x];
1533 for (y = 0; y < rd2->n_reg_rules; y++) {
1534 rule2 = &rd2->reg_rules[y];
1535 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
1538 * No need to memset here the intersected rule here as
1539 * we're not using the stack anymore
1544 add_rule(&intersected_rule, rd->reg_rules,
1549 rd->alpha2[0] = '9';
1550 rd->alpha2[1] = '8';
1551 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
1558 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1559 * want to just have the channel structure use these
1561 static u32 map_regdom_flags(u32 rd_flags)
1563 u32 channel_flags = 0;
1564 if (rd_flags & NL80211_RRF_NO_IR_ALL)
1565 channel_flags |= IEEE80211_CHAN_NO_IR;
1566 if (rd_flags & NL80211_RRF_DFS)
1567 channel_flags |= IEEE80211_CHAN_RADAR;
1568 if (rd_flags & NL80211_RRF_NO_OFDM)
1569 channel_flags |= IEEE80211_CHAN_NO_OFDM;
1570 if (rd_flags & NL80211_RRF_NO_OUTDOOR)
1571 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
1572 if (rd_flags & NL80211_RRF_IR_CONCURRENT)
1573 channel_flags |= IEEE80211_CHAN_IR_CONCURRENT;
1574 if (rd_flags & NL80211_RRF_NO_HT40MINUS)
1575 channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
1576 if (rd_flags & NL80211_RRF_NO_HT40PLUS)
1577 channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
1578 if (rd_flags & NL80211_RRF_NO_80MHZ)
1579 channel_flags |= IEEE80211_CHAN_NO_80MHZ;
1580 if (rd_flags & NL80211_RRF_NO_160MHZ)
1581 channel_flags |= IEEE80211_CHAN_NO_160MHZ;
1582 if (rd_flags & NL80211_RRF_NO_HE)
1583 channel_flags |= IEEE80211_CHAN_NO_HE;
1584 return channel_flags;
1587 static const struct ieee80211_reg_rule *
1588 freq_reg_info_regd(u32 center_freq,
1589 const struct ieee80211_regdomain *regd, u32 bw)
1592 bool band_rule_found = false;
1593 bool bw_fits = false;
1596 return ERR_PTR(-EINVAL);
1598 for (i = 0; i < regd->n_reg_rules; i++) {
1599 const struct ieee80211_reg_rule *rr;
1600 const struct ieee80211_freq_range *fr = NULL;
1602 rr = ®d->reg_rules[i];
1603 fr = &rr->freq_range;
1606 * We only need to know if one frequency rule was
1607 * in center_freq's band, that's enough, so let's
1608 * not overwrite it once found
1610 if (!band_rule_found)
1611 band_rule_found = freq_in_rule_band(fr, center_freq);
1613 bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw);
1615 if (band_rule_found && bw_fits)
1619 if (!band_rule_found)
1620 return ERR_PTR(-ERANGE);
1622 return ERR_PTR(-EINVAL);
1625 static const struct ieee80211_reg_rule *
1626 __freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw)
1628 const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy);
1629 const u32 bws[] = {0, 1, 2, 4, 5, 8, 10, 16, 20};
1630 const struct ieee80211_reg_rule *reg_rule;
1631 int i = ARRAY_SIZE(bws) - 1;
1634 for (bw = MHZ_TO_KHZ(bws[i]); bw >= min_bw; bw = MHZ_TO_KHZ(bws[i--])) {
1635 reg_rule = freq_reg_info_regd(center_freq, regd, bw);
1636 if (!IS_ERR(reg_rule))
1643 const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
1646 u32 min_bw = center_freq < MHZ_TO_KHZ(1000) ? 1 : 20;
1648 return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(min_bw));
1650 EXPORT_SYMBOL(freq_reg_info);
1652 const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
1654 switch (initiator) {
1655 case NL80211_REGDOM_SET_BY_CORE:
1657 case NL80211_REGDOM_SET_BY_USER:
1659 case NL80211_REGDOM_SET_BY_DRIVER:
1661 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1662 return "country element";
1668 EXPORT_SYMBOL(reg_initiator_name);
1670 static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd,
1671 const struct ieee80211_reg_rule *reg_rule,
1672 const struct ieee80211_channel *chan)
1674 const struct ieee80211_freq_range *freq_range = NULL;
1675 u32 max_bandwidth_khz, center_freq_khz, bw_flags = 0;
1676 bool is_s1g = chan->band == NL80211_BAND_S1GHZ;
1678 freq_range = ®_rule->freq_range;
1680 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1681 center_freq_khz = ieee80211_channel_to_khz(chan);
1682 /* Check if auto calculation requested */
1683 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
1684 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1686 /* If we get a reg_rule we can assume that at least 5Mhz fit */
1687 if (!cfg80211_does_bw_fit_range(freq_range,
1690 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1691 if (!cfg80211_does_bw_fit_range(freq_range,
1694 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1697 /* S1G is strict about non overlapping channels. We can
1698 * calculate which bandwidth is allowed per channel by finding
1699 * the largest bandwidth which cleanly divides the freq_range.
1702 int ch_bw = max_bandwidth_khz;
1705 edge_offset = (center_freq_khz - ch_bw / 2) -
1706 freq_range->start_freq_khz;
1707 if (edge_offset % ch_bw == 0) {
1708 switch (KHZ_TO_MHZ(ch_bw)) {
1710 bw_flags |= IEEE80211_CHAN_1MHZ;
1713 bw_flags |= IEEE80211_CHAN_2MHZ;
1716 bw_flags |= IEEE80211_CHAN_4MHZ;
1719 bw_flags |= IEEE80211_CHAN_8MHZ;
1722 bw_flags |= IEEE80211_CHAN_16MHZ;
1725 /* If we got here, no bandwidths fit on
1726 * this frequency, ie. band edge.
1728 bw_flags |= IEEE80211_CHAN_DISABLED;
1736 if (max_bandwidth_khz < MHZ_TO_KHZ(10))
1737 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1738 if (max_bandwidth_khz < MHZ_TO_KHZ(20))
1739 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1740 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
1741 bw_flags |= IEEE80211_CHAN_NO_HT40;
1742 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
1743 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
1744 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
1745 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
1750 static void handle_channel_single_rule(struct wiphy *wiphy,
1751 enum nl80211_reg_initiator initiator,
1752 struct ieee80211_channel *chan,
1754 struct regulatory_request *lr,
1755 struct wiphy *request_wiphy,
1756 const struct ieee80211_reg_rule *reg_rule)
1759 const struct ieee80211_power_rule *power_rule = NULL;
1760 const struct ieee80211_regdomain *regd;
1762 regd = reg_get_regdomain(wiphy);
1764 power_rule = ®_rule->power_rule;
1765 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
1767 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1768 request_wiphy && request_wiphy == wiphy &&
1769 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
1771 * This guarantees the driver's requested regulatory domain
1772 * will always be used as a base for further regulatory
1775 chan->flags = chan->orig_flags =
1776 map_regdom_flags(reg_rule->flags) | bw_flags;
1777 chan->max_antenna_gain = chan->orig_mag =
1778 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1779 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
1780 (int) MBM_TO_DBM(power_rule->max_eirp);
1782 if (chan->flags & IEEE80211_CHAN_RADAR) {
1783 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1784 if (reg_rule->dfs_cac_ms)
1785 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1791 chan->dfs_state = NL80211_DFS_USABLE;
1792 chan->dfs_state_entered = jiffies;
1794 chan->beacon_found = false;
1795 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1796 chan->max_antenna_gain =
1797 min_t(int, chan->orig_mag,
1798 MBI_TO_DBI(power_rule->max_antenna_gain));
1799 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1801 if (chan->flags & IEEE80211_CHAN_RADAR) {
1802 if (reg_rule->dfs_cac_ms)
1803 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1805 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1808 if (chan->orig_mpwr) {
1810 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1811 * will always follow the passed country IE power settings.
1813 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1814 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
1815 chan->max_power = chan->max_reg_power;
1817 chan->max_power = min(chan->orig_mpwr,
1818 chan->max_reg_power);
1820 chan->max_power = chan->max_reg_power;
1823 static void handle_channel_adjacent_rules(struct wiphy *wiphy,
1824 enum nl80211_reg_initiator initiator,
1825 struct ieee80211_channel *chan,
1827 struct regulatory_request *lr,
1828 struct wiphy *request_wiphy,
1829 const struct ieee80211_reg_rule *rrule1,
1830 const struct ieee80211_reg_rule *rrule2,
1831 struct ieee80211_freq_range *comb_range)
1835 const struct ieee80211_power_rule *power_rule1 = NULL;
1836 const struct ieee80211_power_rule *power_rule2 = NULL;
1837 const struct ieee80211_regdomain *regd;
1839 regd = reg_get_regdomain(wiphy);
1841 power_rule1 = &rrule1->power_rule;
1842 power_rule2 = &rrule2->power_rule;
1843 bw_flags1 = reg_rule_to_chan_bw_flags(regd, rrule1, chan);
1844 bw_flags2 = reg_rule_to_chan_bw_flags(regd, rrule2, chan);
1846 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1847 request_wiphy && request_wiphy == wiphy &&
1848 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
1849 /* This guarantees the driver's requested regulatory domain
1850 * will always be used as a base for further regulatory
1854 map_regdom_flags(rrule1->flags) |
1855 map_regdom_flags(rrule2->flags) |
1858 chan->orig_flags = chan->flags;
1859 chan->max_antenna_gain =
1860 min_t(int, MBI_TO_DBI(power_rule1->max_antenna_gain),
1861 MBI_TO_DBI(power_rule2->max_antenna_gain));
1862 chan->orig_mag = chan->max_antenna_gain;
1863 chan->max_reg_power =
1864 min_t(int, MBM_TO_DBM(power_rule1->max_eirp),
1865 MBM_TO_DBM(power_rule2->max_eirp));
1866 chan->max_power = chan->max_reg_power;
1867 chan->orig_mpwr = chan->max_reg_power;
1869 if (chan->flags & IEEE80211_CHAN_RADAR) {
1870 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1871 if (rrule1->dfs_cac_ms || rrule2->dfs_cac_ms)
1872 chan->dfs_cac_ms = max_t(unsigned int,
1874 rrule2->dfs_cac_ms);
1880 chan->dfs_state = NL80211_DFS_USABLE;
1881 chan->dfs_state_entered = jiffies;
1883 chan->beacon_found = false;
1884 chan->flags = flags | bw_flags1 | bw_flags2 |
1885 map_regdom_flags(rrule1->flags) |
1886 map_regdom_flags(rrule2->flags);
1888 /* reg_rule_to_chan_bw_flags may forbids 10 and forbids 20 MHz
1889 * (otherwise no adj. rule case), recheck therefore
1891 if (cfg80211_does_bw_fit_range(comb_range,
1892 ieee80211_channel_to_khz(chan),
1894 chan->flags &= ~IEEE80211_CHAN_NO_10MHZ;
1895 if (cfg80211_does_bw_fit_range(comb_range,
1896 ieee80211_channel_to_khz(chan),
1898 chan->flags &= ~IEEE80211_CHAN_NO_20MHZ;
1900 chan->max_antenna_gain =
1901 min_t(int, chan->orig_mag,
1903 MBI_TO_DBI(power_rule1->max_antenna_gain),
1904 MBI_TO_DBI(power_rule2->max_antenna_gain)));
1905 chan->max_reg_power = min_t(int,
1906 MBM_TO_DBM(power_rule1->max_eirp),
1907 MBM_TO_DBM(power_rule2->max_eirp));
1909 if (chan->flags & IEEE80211_CHAN_RADAR) {
1910 if (rrule1->dfs_cac_ms || rrule2->dfs_cac_ms)
1911 chan->dfs_cac_ms = max_t(unsigned int,
1913 rrule2->dfs_cac_ms);
1915 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1918 if (chan->orig_mpwr) {
1919 /* Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1920 * will always follow the passed country IE power settings.
1922 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1923 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
1924 chan->max_power = chan->max_reg_power;
1926 chan->max_power = min(chan->orig_mpwr,
1927 chan->max_reg_power);
1929 chan->max_power = chan->max_reg_power;
1933 /* Note that right now we assume the desired channel bandwidth
1934 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1935 * per channel, the primary and the extension channel).
1937 static void handle_channel(struct wiphy *wiphy,
1938 enum nl80211_reg_initiator initiator,
1939 struct ieee80211_channel *chan)
1941 const u32 orig_chan_freq = ieee80211_channel_to_khz(chan);
1942 struct regulatory_request *lr = get_last_request();
1943 struct wiphy *request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
1944 const struct ieee80211_reg_rule *rrule = NULL;
1945 const struct ieee80211_reg_rule *rrule1 = NULL;
1946 const struct ieee80211_reg_rule *rrule2 = NULL;
1948 u32 flags = chan->orig_flags;
1950 rrule = freq_reg_info(wiphy, orig_chan_freq);
1951 if (IS_ERR(rrule)) {
1952 /* check for adjacent match, therefore get rules for
1953 * chan - 20 MHz and chan + 20 MHz and test
1954 * if reg rules are adjacent
1956 rrule1 = freq_reg_info(wiphy,
1957 orig_chan_freq - MHZ_TO_KHZ(20));
1958 rrule2 = freq_reg_info(wiphy,
1959 orig_chan_freq + MHZ_TO_KHZ(20));
1960 if (!IS_ERR(rrule1) && !IS_ERR(rrule2)) {
1961 struct ieee80211_freq_range comb_range;
1963 if (rrule1->freq_range.end_freq_khz !=
1964 rrule2->freq_range.start_freq_khz)
1967 comb_range.start_freq_khz =
1968 rrule1->freq_range.start_freq_khz;
1969 comb_range.end_freq_khz =
1970 rrule2->freq_range.end_freq_khz;
1971 comb_range.max_bandwidth_khz =
1973 rrule1->freq_range.max_bandwidth_khz,
1974 rrule2->freq_range.max_bandwidth_khz);
1976 if (!cfg80211_does_bw_fit_range(&comb_range,
1981 handle_channel_adjacent_rules(wiphy, initiator, chan,
1982 flags, lr, request_wiphy,
1989 /* We will disable all channels that do not match our
1990 * received regulatory rule unless the hint is coming
1991 * from a Country IE and the Country IE had no information
1992 * about a band. The IEEE 802.11 spec allows for an AP
1993 * to send only a subset of the regulatory rules allowed,
1994 * so an AP in the US that only supports 2.4 GHz may only send
1995 * a country IE with information for the 2.4 GHz band
1996 * while 5 GHz is still supported.
1998 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1999 PTR_ERR(rrule) == -ERANGE)
2002 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
2003 request_wiphy && request_wiphy == wiphy &&
2004 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
2005 pr_debug("Disabling freq %d.%03d MHz for good\n",
2006 chan->center_freq, chan->freq_offset);
2007 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
2008 chan->flags = chan->orig_flags;
2010 pr_debug("Disabling freq %d.%03d MHz\n",
2011 chan->center_freq, chan->freq_offset);
2012 chan->flags |= IEEE80211_CHAN_DISABLED;
2017 handle_channel_single_rule(wiphy, initiator, chan, flags, lr,
2018 request_wiphy, rrule);
2021 static void handle_band(struct wiphy *wiphy,
2022 enum nl80211_reg_initiator initiator,
2023 struct ieee80211_supported_band *sband)
2030 for (i = 0; i < sband->n_channels; i++)
2031 handle_channel(wiphy, initiator, &sband->channels[i]);
2034 static bool reg_request_cell_base(struct regulatory_request *request)
2036 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
2038 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
2041 bool reg_last_request_cell_base(void)
2043 return reg_request_cell_base(get_last_request());
2046 #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
2047 /* Core specific check */
2048 static enum reg_request_treatment
2049 reg_ignore_cell_hint(struct regulatory_request *pending_request)
2051 struct regulatory_request *lr = get_last_request();
2053 if (!reg_num_devs_support_basehint)
2054 return REG_REQ_IGNORE;
2056 if (reg_request_cell_base(lr) &&
2057 !regdom_changes(pending_request->alpha2))
2058 return REG_REQ_ALREADY_SET;
2063 /* Device specific check */
2064 static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
2066 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
2069 static enum reg_request_treatment
2070 reg_ignore_cell_hint(struct regulatory_request *pending_request)
2072 return REG_REQ_IGNORE;
2075 static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
2081 static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
2083 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
2084 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
2089 static bool ignore_reg_update(struct wiphy *wiphy,
2090 enum nl80211_reg_initiator initiator)
2092 struct regulatory_request *lr = get_last_request();
2094 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2098 pr_debug("Ignoring regulatory request set by %s since last_request is not set\n",
2099 reg_initiator_name(initiator));
2103 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
2104 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
2105 pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n",
2106 reg_initiator_name(initiator));
2111 * wiphy->regd will be set once the device has its own
2112 * desired regulatory domain set
2114 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
2115 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2116 !is_world_regdom(lr->alpha2)) {
2117 pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n",
2118 reg_initiator_name(initiator));
2122 if (reg_request_cell_base(lr))
2123 return reg_dev_ignore_cell_hint(wiphy);
2128 static bool reg_is_world_roaming(struct wiphy *wiphy)
2130 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
2131 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
2132 struct regulatory_request *lr = get_last_request();
2134 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
2137 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2138 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
2144 static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
2145 struct reg_beacon *reg_beacon)
2147 struct ieee80211_supported_band *sband;
2148 struct ieee80211_channel *chan;
2149 bool channel_changed = false;
2150 struct ieee80211_channel chan_before;
2152 sband = wiphy->bands[reg_beacon->chan.band];
2153 chan = &sband->channels[chan_idx];
2155 if (likely(!ieee80211_channel_equal(chan, ®_beacon->chan)))
2158 if (chan->beacon_found)
2161 chan->beacon_found = true;
2163 if (!reg_is_world_roaming(wiphy))
2166 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
2169 chan_before = *chan;
2171 if (chan->flags & IEEE80211_CHAN_NO_IR) {
2172 chan->flags &= ~IEEE80211_CHAN_NO_IR;
2173 channel_changed = true;
2176 if (channel_changed)
2177 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
2181 * Called when a scan on a wiphy finds a beacon on
2184 static void wiphy_update_new_beacon(struct wiphy *wiphy,
2185 struct reg_beacon *reg_beacon)
2188 struct ieee80211_supported_band *sband;
2190 if (!wiphy->bands[reg_beacon->chan.band])
2193 sband = wiphy->bands[reg_beacon->chan.band];
2195 for (i = 0; i < sband->n_channels; i++)
2196 handle_reg_beacon(wiphy, i, reg_beacon);
2200 * Called upon reg changes or a new wiphy is added
2202 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
2205 struct ieee80211_supported_band *sband;
2206 struct reg_beacon *reg_beacon;
2208 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
2209 if (!wiphy->bands[reg_beacon->chan.band])
2211 sband = wiphy->bands[reg_beacon->chan.band];
2212 for (i = 0; i < sband->n_channels; i++)
2213 handle_reg_beacon(wiphy, i, reg_beacon);
2217 /* Reap the advantages of previously found beacons */
2218 static void reg_process_beacons(struct wiphy *wiphy)
2221 * Means we are just firing up cfg80211, so no beacons would
2222 * have been processed yet.
2226 wiphy_update_beacon_reg(wiphy);
2229 static bool is_ht40_allowed(struct ieee80211_channel *chan)
2233 if (chan->flags & IEEE80211_CHAN_DISABLED)
2235 /* This would happen when regulatory rules disallow HT40 completely */
2236 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
2241 static void reg_process_ht_flags_channel(struct wiphy *wiphy,
2242 struct ieee80211_channel *channel)
2244 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
2245 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
2246 const struct ieee80211_regdomain *regd;
2250 if (!is_ht40_allowed(channel)) {
2251 channel->flags |= IEEE80211_CHAN_NO_HT40;
2256 * We need to ensure the extension channels exist to
2257 * be able to use HT40- or HT40+, this finds them (or not)
2259 for (i = 0; i < sband->n_channels; i++) {
2260 struct ieee80211_channel *c = &sband->channels[i];
2262 if (c->center_freq == (channel->center_freq - 20))
2264 if (c->center_freq == (channel->center_freq + 20))
2269 regd = get_wiphy_regdom(wiphy);
2271 const struct ieee80211_reg_rule *reg_rule =
2272 freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq),
2273 regd, MHZ_TO_KHZ(20));
2275 if (!IS_ERR(reg_rule))
2276 flags = reg_rule->flags;
2280 * Please note that this assumes target bandwidth is 20 MHz,
2281 * if that ever changes we also need to change the below logic
2282 * to include that as well.
2284 if (!is_ht40_allowed(channel_before) ||
2285 flags & NL80211_RRF_NO_HT40MINUS)
2286 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
2288 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
2290 if (!is_ht40_allowed(channel_after) ||
2291 flags & NL80211_RRF_NO_HT40PLUS)
2292 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
2294 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
2297 static void reg_process_ht_flags_band(struct wiphy *wiphy,
2298 struct ieee80211_supported_band *sband)
2305 for (i = 0; i < sband->n_channels; i++)
2306 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
2309 static void reg_process_ht_flags(struct wiphy *wiphy)
2311 enum nl80211_band band;
2316 for (band = 0; band < NUM_NL80211_BANDS; band++)
2317 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
2320 static void reg_call_notifier(struct wiphy *wiphy,
2321 struct regulatory_request *request)
2323 if (wiphy->reg_notifier)
2324 wiphy->reg_notifier(wiphy, request);
2327 static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev)
2329 struct cfg80211_chan_def chandef = {};
2330 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2331 enum nl80211_iftype iftype;
2334 iftype = wdev->iftype;
2336 /* make sure the interface is active */
2337 if (!wdev->netdev || !netif_running(wdev->netdev))
2338 goto wdev_inactive_unlock;
2341 case NL80211_IFTYPE_AP:
2342 case NL80211_IFTYPE_P2P_GO:
2343 if (!wdev->beacon_interval)
2344 goto wdev_inactive_unlock;
2345 chandef = wdev->chandef;
2347 case NL80211_IFTYPE_ADHOC:
2348 if (!wdev->ssid_len)
2349 goto wdev_inactive_unlock;
2350 chandef = wdev->chandef;
2352 case NL80211_IFTYPE_STATION:
2353 case NL80211_IFTYPE_P2P_CLIENT:
2354 if (!wdev->current_bss ||
2355 !wdev->current_bss->pub.channel)
2356 goto wdev_inactive_unlock;
2358 if (!rdev->ops->get_channel ||
2359 rdev_get_channel(rdev, wdev, &chandef))
2360 cfg80211_chandef_create(&chandef,
2361 wdev->current_bss->pub.channel,
2362 NL80211_CHAN_NO_HT);
2364 case NL80211_IFTYPE_MONITOR:
2365 case NL80211_IFTYPE_AP_VLAN:
2366 case NL80211_IFTYPE_P2P_DEVICE:
2367 /* no enforcement required */
2370 /* others not implemented for now */
2378 case NL80211_IFTYPE_AP:
2379 case NL80211_IFTYPE_P2P_GO:
2380 case NL80211_IFTYPE_ADHOC:
2381 return cfg80211_reg_can_beacon_relax(wiphy, &chandef, iftype);
2382 case NL80211_IFTYPE_STATION:
2383 case NL80211_IFTYPE_P2P_CLIENT:
2384 return cfg80211_chandef_usable(wiphy, &chandef,
2385 IEEE80211_CHAN_DISABLED);
2392 wdev_inactive_unlock:
2397 static void reg_leave_invalid_chans(struct wiphy *wiphy)
2399 struct wireless_dev *wdev;
2400 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2404 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
2405 if (!reg_wdev_chan_valid(wiphy, wdev))
2406 cfg80211_leave(rdev, wdev);
2409 static void reg_check_chans_work(struct work_struct *work)
2411 struct cfg80211_registered_device *rdev;
2413 pr_debug("Verifying active interfaces after reg change\n");
2416 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2417 if (!(rdev->wiphy.regulatory_flags &
2418 REGULATORY_IGNORE_STALE_KICKOFF))
2419 reg_leave_invalid_chans(&rdev->wiphy);
2424 static void reg_check_channels(void)
2427 * Give usermode a chance to do something nicer (move to another
2428 * channel, orderly disconnection), before forcing a disconnection.
2430 mod_delayed_work(system_power_efficient_wq,
2432 msecs_to_jiffies(REG_ENFORCE_GRACE_MS));
2435 static void wiphy_update_regulatory(struct wiphy *wiphy,
2436 enum nl80211_reg_initiator initiator)
2438 enum nl80211_band band;
2439 struct regulatory_request *lr = get_last_request();
2441 if (ignore_reg_update(wiphy, initiator)) {
2443 * Regulatory updates set by CORE are ignored for custom
2444 * regulatory cards. Let us notify the changes to the driver,
2445 * as some drivers used this to restore its orig_* reg domain.
2447 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
2448 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG &&
2449 !(wiphy->regulatory_flags &
2450 REGULATORY_WIPHY_SELF_MANAGED))
2451 reg_call_notifier(wiphy, lr);
2455 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
2457 for (band = 0; band < NUM_NL80211_BANDS; band++)
2458 handle_band(wiphy, initiator, wiphy->bands[band]);
2460 reg_process_beacons(wiphy);
2461 reg_process_ht_flags(wiphy);
2462 reg_call_notifier(wiphy, lr);
2465 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
2467 struct cfg80211_registered_device *rdev;
2468 struct wiphy *wiphy;
2472 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2473 wiphy = &rdev->wiphy;
2474 wiphy_update_regulatory(wiphy, initiator);
2477 reg_check_channels();
2480 static void handle_channel_custom(struct wiphy *wiphy,
2481 struct ieee80211_channel *chan,
2482 const struct ieee80211_regdomain *regd,
2486 const struct ieee80211_reg_rule *reg_rule = NULL;
2487 const struct ieee80211_power_rule *power_rule = NULL;
2488 u32 bw, center_freq_khz;
2490 center_freq_khz = ieee80211_channel_to_khz(chan);
2491 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
2492 reg_rule = freq_reg_info_regd(center_freq_khz, regd, bw);
2493 if (!IS_ERR(reg_rule))
2497 if (IS_ERR_OR_NULL(reg_rule)) {
2498 pr_debug("Disabling freq %d.%03d MHz as custom regd has no rule that fits it\n",
2499 chan->center_freq, chan->freq_offset);
2500 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
2501 chan->flags |= IEEE80211_CHAN_DISABLED;
2503 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
2504 chan->flags = chan->orig_flags;
2509 power_rule = ®_rule->power_rule;
2510 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
2512 chan->dfs_state_entered = jiffies;
2513 chan->dfs_state = NL80211_DFS_USABLE;
2515 chan->beacon_found = false;
2517 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2518 chan->flags = chan->orig_flags | bw_flags |
2519 map_regdom_flags(reg_rule->flags);
2521 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
2523 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
2524 chan->max_reg_power = chan->max_power =
2525 (int) MBM_TO_DBM(power_rule->max_eirp);
2527 if (chan->flags & IEEE80211_CHAN_RADAR) {
2528 if (reg_rule->dfs_cac_ms)
2529 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
2531 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
2534 chan->max_power = chan->max_reg_power;
2537 static void handle_band_custom(struct wiphy *wiphy,
2538 struct ieee80211_supported_band *sband,
2539 const struct ieee80211_regdomain *regd)
2547 * We currently assume that you always want at least 20 MHz,
2548 * otherwise channel 12 might get enabled if this rule is
2549 * compatible to US, which permits 2402 - 2472 MHz.
2551 for (i = 0; i < sband->n_channels; i++)
2552 handle_channel_custom(wiphy, &sband->channels[i], regd,
2556 /* Used by drivers prior to wiphy registration */
2557 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
2558 const struct ieee80211_regdomain *regd)
2560 enum nl80211_band band;
2561 unsigned int bands_set = 0;
2563 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
2564 "wiphy should have REGULATORY_CUSTOM_REG\n");
2565 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
2567 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2568 if (!wiphy->bands[band])
2570 handle_band_custom(wiphy, wiphy->bands[band], regd);
2575 * no point in calling this if it won't have any effect
2576 * on your device's supported bands.
2578 WARN_ON(!bands_set);
2580 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
2582 static void reg_set_request_processed(void)
2584 bool need_more_processing = false;
2585 struct regulatory_request *lr = get_last_request();
2587 lr->processed = true;
2589 spin_lock(®_requests_lock);
2590 if (!list_empty(®_requests_list))
2591 need_more_processing = true;
2592 spin_unlock(®_requests_lock);
2594 cancel_crda_timeout();
2596 if (need_more_processing)
2597 schedule_work(®_work);
2601 * reg_process_hint_core - process core regulatory requests
2602 * @core_request: a pending core regulatory request
2604 * The wireless subsystem can use this function to process
2605 * a regulatory request issued by the regulatory core.
2607 static enum reg_request_treatment
2608 reg_process_hint_core(struct regulatory_request *core_request)
2610 if (reg_query_database(core_request)) {
2611 core_request->intersect = false;
2612 core_request->processed = false;
2613 reg_update_last_request(core_request);
2617 return REG_REQ_IGNORE;
2620 static enum reg_request_treatment
2621 __reg_process_hint_user(struct regulatory_request *user_request)
2623 struct regulatory_request *lr = get_last_request();
2625 if (reg_request_cell_base(user_request))
2626 return reg_ignore_cell_hint(user_request);
2628 if (reg_request_cell_base(lr))
2629 return REG_REQ_IGNORE;
2631 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
2632 return REG_REQ_INTERSECT;
2634 * If the user knows better the user should set the regdom
2635 * to their country before the IE is picked up
2637 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
2639 return REG_REQ_IGNORE;
2641 * Process user requests only after previous user/driver/core
2642 * requests have been processed
2644 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
2645 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
2646 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
2647 regdom_changes(lr->alpha2))
2648 return REG_REQ_IGNORE;
2650 if (!regdom_changes(user_request->alpha2))
2651 return REG_REQ_ALREADY_SET;
2657 * reg_process_hint_user - process user regulatory requests
2658 * @user_request: a pending user regulatory request
2660 * The wireless subsystem can use this function to process
2661 * a regulatory request initiated by userspace.
2663 static enum reg_request_treatment
2664 reg_process_hint_user(struct regulatory_request *user_request)
2666 enum reg_request_treatment treatment;
2668 treatment = __reg_process_hint_user(user_request);
2669 if (treatment == REG_REQ_IGNORE ||
2670 treatment == REG_REQ_ALREADY_SET)
2671 return REG_REQ_IGNORE;
2673 user_request->intersect = treatment == REG_REQ_INTERSECT;
2674 user_request->processed = false;
2676 if (reg_query_database(user_request)) {
2677 reg_update_last_request(user_request);
2678 user_alpha2[0] = user_request->alpha2[0];
2679 user_alpha2[1] = user_request->alpha2[1];
2683 return REG_REQ_IGNORE;
2686 static enum reg_request_treatment
2687 __reg_process_hint_driver(struct regulatory_request *driver_request)
2689 struct regulatory_request *lr = get_last_request();
2691 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
2692 if (regdom_changes(driver_request->alpha2))
2694 return REG_REQ_ALREADY_SET;
2698 * This would happen if you unplug and plug your card
2699 * back in or if you add a new device for which the previously
2700 * loaded card also agrees on the regulatory domain.
2702 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
2703 !regdom_changes(driver_request->alpha2))
2704 return REG_REQ_ALREADY_SET;
2706 return REG_REQ_INTERSECT;
2710 * reg_process_hint_driver - process driver regulatory requests
2711 * @wiphy: the wireless device for the regulatory request
2712 * @driver_request: a pending driver regulatory request
2714 * The wireless subsystem can use this function to process
2715 * a regulatory request issued by an 802.11 driver.
2717 * Returns one of the different reg request treatment values.
2719 static enum reg_request_treatment
2720 reg_process_hint_driver(struct wiphy *wiphy,
2721 struct regulatory_request *driver_request)
2723 const struct ieee80211_regdomain *regd, *tmp;
2724 enum reg_request_treatment treatment;
2726 treatment = __reg_process_hint_driver(driver_request);
2728 switch (treatment) {
2731 case REG_REQ_IGNORE:
2732 return REG_REQ_IGNORE;
2733 case REG_REQ_INTERSECT:
2734 case REG_REQ_ALREADY_SET:
2735 regd = reg_copy_regd(get_cfg80211_regdom());
2737 return REG_REQ_IGNORE;
2739 tmp = get_wiphy_regdom(wiphy);
2740 rcu_assign_pointer(wiphy->regd, regd);
2741 rcu_free_regdom(tmp);
2745 driver_request->intersect = treatment == REG_REQ_INTERSECT;
2746 driver_request->processed = false;
2749 * Since CRDA will not be called in this case as we already
2750 * have applied the requested regulatory domain before we just
2751 * inform userspace we have processed the request
2753 if (treatment == REG_REQ_ALREADY_SET) {
2754 nl80211_send_reg_change_event(driver_request);
2755 reg_update_last_request(driver_request);
2756 reg_set_request_processed();
2757 return REG_REQ_ALREADY_SET;
2760 if (reg_query_database(driver_request)) {
2761 reg_update_last_request(driver_request);
2765 return REG_REQ_IGNORE;
2768 static enum reg_request_treatment
2769 __reg_process_hint_country_ie(struct wiphy *wiphy,
2770 struct regulatory_request *country_ie_request)
2772 struct wiphy *last_wiphy = NULL;
2773 struct regulatory_request *lr = get_last_request();
2775 if (reg_request_cell_base(lr)) {
2776 /* Trust a Cell base station over the AP's country IE */
2777 if (regdom_changes(country_ie_request->alpha2))
2778 return REG_REQ_IGNORE;
2779 return REG_REQ_ALREADY_SET;
2781 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
2782 return REG_REQ_IGNORE;
2785 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
2788 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
2791 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
2793 if (last_wiphy != wiphy) {
2795 * Two cards with two APs claiming different
2796 * Country IE alpha2s. We could
2797 * intersect them, but that seems unlikely
2798 * to be correct. Reject second one for now.
2800 if (regdom_changes(country_ie_request->alpha2))
2801 return REG_REQ_IGNORE;
2802 return REG_REQ_ALREADY_SET;
2805 if (regdom_changes(country_ie_request->alpha2))
2807 return REG_REQ_ALREADY_SET;
2811 * reg_process_hint_country_ie - process regulatory requests from country IEs
2812 * @wiphy: the wireless device for the regulatory request
2813 * @country_ie_request: a regulatory request from a country IE
2815 * The wireless subsystem can use this function to process
2816 * a regulatory request issued by a country Information Element.
2818 * Returns one of the different reg request treatment values.
2820 static enum reg_request_treatment
2821 reg_process_hint_country_ie(struct wiphy *wiphy,
2822 struct regulatory_request *country_ie_request)
2824 enum reg_request_treatment treatment;
2826 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
2828 switch (treatment) {
2831 case REG_REQ_IGNORE:
2832 return REG_REQ_IGNORE;
2833 case REG_REQ_ALREADY_SET:
2834 reg_free_request(country_ie_request);
2835 return REG_REQ_ALREADY_SET;
2836 case REG_REQ_INTERSECT:
2838 * This doesn't happen yet, not sure we
2839 * ever want to support it for this case.
2841 WARN_ONCE(1, "Unexpected intersection for country elements");
2842 return REG_REQ_IGNORE;
2845 country_ie_request->intersect = false;
2846 country_ie_request->processed = false;
2848 if (reg_query_database(country_ie_request)) {
2849 reg_update_last_request(country_ie_request);
2853 return REG_REQ_IGNORE;
2856 bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2)
2858 const struct ieee80211_regdomain *wiphy1_regd = NULL;
2859 const struct ieee80211_regdomain *wiphy2_regd = NULL;
2860 const struct ieee80211_regdomain *cfg80211_regd = NULL;
2861 bool dfs_domain_same;
2865 cfg80211_regd = rcu_dereference(cfg80211_regdomain);
2866 wiphy1_regd = rcu_dereference(wiphy1->regd);
2868 wiphy1_regd = cfg80211_regd;
2870 wiphy2_regd = rcu_dereference(wiphy2->regd);
2872 wiphy2_regd = cfg80211_regd;
2874 dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region;
2878 return dfs_domain_same;
2881 static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan,
2882 struct ieee80211_channel *src_chan)
2884 if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) ||
2885 !(src_chan->flags & IEEE80211_CHAN_RADAR))
2888 if (dst_chan->flags & IEEE80211_CHAN_DISABLED ||
2889 src_chan->flags & IEEE80211_CHAN_DISABLED)
2892 if (src_chan->center_freq == dst_chan->center_freq &&
2893 dst_chan->dfs_state == NL80211_DFS_USABLE) {
2894 dst_chan->dfs_state = src_chan->dfs_state;
2895 dst_chan->dfs_state_entered = src_chan->dfs_state_entered;
2899 static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy,
2900 struct wiphy *src_wiphy)
2902 struct ieee80211_supported_band *src_sband, *dst_sband;
2903 struct ieee80211_channel *src_chan, *dst_chan;
2906 if (!reg_dfs_domain_same(dst_wiphy, src_wiphy))
2909 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2910 dst_sband = dst_wiphy->bands[band];
2911 src_sband = src_wiphy->bands[band];
2912 if (!dst_sband || !src_sband)
2915 for (i = 0; i < dst_sband->n_channels; i++) {
2916 dst_chan = &dst_sband->channels[i];
2917 for (j = 0; j < src_sband->n_channels; j++) {
2918 src_chan = &src_sband->channels[j];
2919 reg_copy_dfs_chan_state(dst_chan, src_chan);
2925 static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy)
2927 struct cfg80211_registered_device *rdev;
2931 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2932 if (wiphy == &rdev->wiphy)
2934 wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy);
2938 /* This processes *all* regulatory hints */
2939 static void reg_process_hint(struct regulatory_request *reg_request)
2941 struct wiphy *wiphy = NULL;
2942 enum reg_request_treatment treatment;
2943 enum nl80211_reg_initiator initiator = reg_request->initiator;
2945 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
2946 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
2948 switch (initiator) {
2949 case NL80211_REGDOM_SET_BY_CORE:
2950 treatment = reg_process_hint_core(reg_request);
2952 case NL80211_REGDOM_SET_BY_USER:
2953 treatment = reg_process_hint_user(reg_request);
2955 case NL80211_REGDOM_SET_BY_DRIVER:
2958 treatment = reg_process_hint_driver(wiphy, reg_request);
2960 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2963 treatment = reg_process_hint_country_ie(wiphy, reg_request);
2966 WARN(1, "invalid initiator %d\n", initiator);
2970 if (treatment == REG_REQ_IGNORE)
2973 WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET,
2974 "unexpected treatment value %d\n", treatment);
2976 /* This is required so that the orig_* parameters are saved.
2977 * NOTE: treatment must be set for any case that reaches here!
2979 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
2980 wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
2981 wiphy_update_regulatory(wiphy, initiator);
2982 wiphy_all_share_dfs_chan_state(wiphy);
2983 reg_check_channels();
2989 reg_free_request(reg_request);
2992 static void notify_self_managed_wiphys(struct regulatory_request *request)
2994 struct cfg80211_registered_device *rdev;
2995 struct wiphy *wiphy;
2997 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2998 wiphy = &rdev->wiphy;
2999 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
3000 request->initiator == NL80211_REGDOM_SET_BY_USER)
3001 reg_call_notifier(wiphy, request);
3006 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
3007 * Regulatory hints come on a first come first serve basis and we
3008 * must process each one atomically.
3010 static void reg_process_pending_hints(void)
3012 struct regulatory_request *reg_request, *lr;
3014 lr = get_last_request();
3016 /* When last_request->processed becomes true this will be rescheduled */
3017 if (lr && !lr->processed) {
3018 pr_debug("Pending regulatory request, waiting for it to be processed...\n");
3022 spin_lock(®_requests_lock);
3024 if (list_empty(®_requests_list)) {
3025 spin_unlock(®_requests_lock);
3029 reg_request = list_first_entry(®_requests_list,
3030 struct regulatory_request,
3032 list_del_init(®_request->list);
3034 spin_unlock(®_requests_lock);
3036 notify_self_managed_wiphys(reg_request);
3038 reg_process_hint(reg_request);
3040 lr = get_last_request();
3042 spin_lock(®_requests_lock);
3043 if (!list_empty(®_requests_list) && lr && lr->processed)
3044 schedule_work(®_work);
3045 spin_unlock(®_requests_lock);
3048 /* Processes beacon hints -- this has nothing to do with country IEs */
3049 static void reg_process_pending_beacon_hints(void)
3051 struct cfg80211_registered_device *rdev;
3052 struct reg_beacon *pending_beacon, *tmp;
3054 /* This goes through the _pending_ beacon list */
3055 spin_lock_bh(®_pending_beacons_lock);
3057 list_for_each_entry_safe(pending_beacon, tmp,
3058 ®_pending_beacons, list) {
3059 list_del_init(&pending_beacon->list);
3061 /* Applies the beacon hint to current wiphys */
3062 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
3063 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
3065 /* Remembers the beacon hint for new wiphys or reg changes */
3066 list_add_tail(&pending_beacon->list, ®_beacon_list);
3069 spin_unlock_bh(®_pending_beacons_lock);
3072 static void reg_process_self_managed_hints(void)
3074 struct cfg80211_registered_device *rdev;
3075 struct wiphy *wiphy;
3076 const struct ieee80211_regdomain *tmp;
3077 const struct ieee80211_regdomain *regd;
3078 enum nl80211_band band;
3079 struct regulatory_request request = {};
3081 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3082 wiphy = &rdev->wiphy;
3084 spin_lock(®_requests_lock);
3085 regd = rdev->requested_regd;
3086 rdev->requested_regd = NULL;
3087 spin_unlock(®_requests_lock);
3092 tmp = get_wiphy_regdom(wiphy);
3093 rcu_assign_pointer(wiphy->regd, regd);
3094 rcu_free_regdom(tmp);
3096 for (band = 0; band < NUM_NL80211_BANDS; band++)
3097 handle_band_custom(wiphy, wiphy->bands[band], regd);
3099 reg_process_ht_flags(wiphy);
3101 request.wiphy_idx = get_wiphy_idx(wiphy);
3102 request.alpha2[0] = regd->alpha2[0];
3103 request.alpha2[1] = regd->alpha2[1];
3104 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
3106 nl80211_send_wiphy_reg_change_event(&request);
3109 reg_check_channels();
3112 static void reg_todo(struct work_struct *work)
3115 reg_process_pending_hints();
3116 reg_process_pending_beacon_hints();
3117 reg_process_self_managed_hints();
3121 static void queue_regulatory_request(struct regulatory_request *request)
3123 request->alpha2[0] = toupper(request->alpha2[0]);
3124 request->alpha2[1] = toupper(request->alpha2[1]);
3126 spin_lock(®_requests_lock);
3127 list_add_tail(&request->list, ®_requests_list);
3128 spin_unlock(®_requests_lock);
3130 schedule_work(®_work);
3134 * Core regulatory hint -- happens during cfg80211_init()
3135 * and when we restore regulatory settings.
3137 static int regulatory_hint_core(const char *alpha2)
3139 struct regulatory_request *request;
3141 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
3145 request->alpha2[0] = alpha2[0];
3146 request->alpha2[1] = alpha2[1];
3147 request->initiator = NL80211_REGDOM_SET_BY_CORE;
3148 request->wiphy_idx = WIPHY_IDX_INVALID;
3150 queue_regulatory_request(request);
3156 int regulatory_hint_user(const char *alpha2,
3157 enum nl80211_user_reg_hint_type user_reg_hint_type)
3159 struct regulatory_request *request;
3161 if (WARN_ON(!alpha2))
3164 if (!is_world_regdom(alpha2) && !is_an_alpha2(alpha2))
3167 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
3171 request->wiphy_idx = WIPHY_IDX_INVALID;
3172 request->alpha2[0] = alpha2[0];
3173 request->alpha2[1] = alpha2[1];
3174 request->initiator = NL80211_REGDOM_SET_BY_USER;
3175 request->user_reg_hint_type = user_reg_hint_type;
3177 /* Allow calling CRDA again */
3178 reset_crda_timeouts();
3180 queue_regulatory_request(request);
3185 int regulatory_hint_indoor(bool is_indoor, u32 portid)
3187 spin_lock(®_indoor_lock);
3189 /* It is possible that more than one user space process is trying to
3190 * configure the indoor setting. To handle such cases, clear the indoor
3191 * setting in case that some process does not think that the device
3192 * is operating in an indoor environment. In addition, if a user space
3193 * process indicates that it is controlling the indoor setting, save its
3194 * portid, i.e., make it the owner.
3196 reg_is_indoor = is_indoor;
3197 if (reg_is_indoor) {
3198 if (!reg_is_indoor_portid)
3199 reg_is_indoor_portid = portid;
3201 reg_is_indoor_portid = 0;
3204 spin_unlock(®_indoor_lock);
3207 reg_check_channels();
3212 void regulatory_netlink_notify(u32 portid)
3214 spin_lock(®_indoor_lock);
3216 if (reg_is_indoor_portid != portid) {
3217 spin_unlock(®_indoor_lock);
3221 reg_is_indoor = false;
3222 reg_is_indoor_portid = 0;
3224 spin_unlock(®_indoor_lock);
3226 reg_check_channels();
3230 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
3232 struct regulatory_request *request;
3234 if (WARN_ON(!alpha2 || !wiphy))
3237 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
3239 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
3243 request->wiphy_idx = get_wiphy_idx(wiphy);
3245 request->alpha2[0] = alpha2[0];
3246 request->alpha2[1] = alpha2[1];
3247 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
3249 /* Allow calling CRDA again */
3250 reset_crda_timeouts();
3252 queue_regulatory_request(request);
3256 EXPORT_SYMBOL(regulatory_hint);
3258 void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band,
3259 const u8 *country_ie, u8 country_ie_len)
3262 enum environment_cap env = ENVIRON_ANY;
3263 struct regulatory_request *request = NULL, *lr;
3265 /* IE len must be evenly divisible by 2 */
3266 if (country_ie_len & 0x01)
3269 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
3272 request = kzalloc(sizeof(*request), GFP_KERNEL);
3276 alpha2[0] = country_ie[0];
3277 alpha2[1] = country_ie[1];
3279 if (country_ie[2] == 'I')
3280 env = ENVIRON_INDOOR;
3281 else if (country_ie[2] == 'O')
3282 env = ENVIRON_OUTDOOR;
3285 lr = get_last_request();
3291 * We will run this only upon a successful connection on cfg80211.
3292 * We leave conflict resolution to the workqueue, where can hold
3295 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
3296 lr->wiphy_idx != WIPHY_IDX_INVALID)
3299 request->wiphy_idx = get_wiphy_idx(wiphy);
3300 request->alpha2[0] = alpha2[0];
3301 request->alpha2[1] = alpha2[1];
3302 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
3303 request->country_ie_env = env;
3305 /* Allow calling CRDA again */
3306 reset_crda_timeouts();
3308 queue_regulatory_request(request);
3315 static void restore_alpha2(char *alpha2, bool reset_user)
3317 /* indicates there is no alpha2 to consider for restoration */
3321 /* The user setting has precedence over the module parameter */
3322 if (is_user_regdom_saved()) {
3323 /* Unless we're asked to ignore it and reset it */
3325 pr_debug("Restoring regulatory settings including user preference\n");
3326 user_alpha2[0] = '9';
3327 user_alpha2[1] = '7';
3330 * If we're ignoring user settings, we still need to
3331 * check the module parameter to ensure we put things
3332 * back as they were for a full restore.
3334 if (!is_world_regdom(ieee80211_regdom)) {
3335 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
3336 ieee80211_regdom[0], ieee80211_regdom[1]);
3337 alpha2[0] = ieee80211_regdom[0];
3338 alpha2[1] = ieee80211_regdom[1];
3341 pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n",
3342 user_alpha2[0], user_alpha2[1]);
3343 alpha2[0] = user_alpha2[0];
3344 alpha2[1] = user_alpha2[1];
3346 } else if (!is_world_regdom(ieee80211_regdom)) {
3347 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
3348 ieee80211_regdom[0], ieee80211_regdom[1]);
3349 alpha2[0] = ieee80211_regdom[0];
3350 alpha2[1] = ieee80211_regdom[1];
3352 pr_debug("Restoring regulatory settings\n");
3355 static void restore_custom_reg_settings(struct wiphy *wiphy)
3357 struct ieee80211_supported_band *sband;
3358 enum nl80211_band band;
3359 struct ieee80211_channel *chan;
3362 for (band = 0; band < NUM_NL80211_BANDS; band++) {
3363 sband = wiphy->bands[band];
3366 for (i = 0; i < sband->n_channels; i++) {
3367 chan = &sband->channels[i];
3368 chan->flags = chan->orig_flags;
3369 chan->max_antenna_gain = chan->orig_mag;
3370 chan->max_power = chan->orig_mpwr;
3371 chan->beacon_found = false;
3377 * Restoring regulatory settings involves ingoring any
3378 * possibly stale country IE information and user regulatory
3379 * settings if so desired, this includes any beacon hints
3380 * learned as we could have traveled outside to another country
3381 * after disconnection. To restore regulatory settings we do
3382 * exactly what we did at bootup:
3384 * - send a core regulatory hint
3385 * - send a user regulatory hint if applicable
3387 * Device drivers that send a regulatory hint for a specific country
3388 * keep their own regulatory domain on wiphy->regd so that does
3389 * not need to be remembered.
3391 static void restore_regulatory_settings(bool reset_user, bool cached)
3394 char world_alpha2[2];
3395 struct reg_beacon *reg_beacon, *btmp;
3396 LIST_HEAD(tmp_reg_req_list);
3397 struct cfg80211_registered_device *rdev;
3402 * Clear the indoor setting in case that it is not controlled by user
3403 * space, as otherwise there is no guarantee that the device is still
3404 * operating in an indoor environment.
3406 spin_lock(®_indoor_lock);
3407 if (reg_is_indoor && !reg_is_indoor_portid) {
3408 reg_is_indoor = false;
3409 reg_check_channels();
3411 spin_unlock(®_indoor_lock);
3413 reset_regdomains(true, &world_regdom);
3414 restore_alpha2(alpha2, reset_user);
3417 * If there's any pending requests we simply
3418 * stash them to a temporary pending queue and
3419 * add then after we've restored regulatory
3422 spin_lock(®_requests_lock);
3423 list_splice_tail_init(®_requests_list, &tmp_reg_req_list);
3424 spin_unlock(®_requests_lock);
3426 /* Clear beacon hints */
3427 spin_lock_bh(®_pending_beacons_lock);
3428 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) {
3429 list_del(®_beacon->list);
3432 spin_unlock_bh(®_pending_beacons_lock);
3434 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) {
3435 list_del(®_beacon->list);
3439 /* First restore to the basic regulatory settings */
3440 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
3441 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
3443 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3444 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
3446 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
3447 restore_custom_reg_settings(&rdev->wiphy);
3450 if (cached && (!is_an_alpha2(alpha2) ||
3451 !IS_ERR_OR_NULL(cfg80211_user_regdom))) {
3452 reset_regdomains(false, cfg80211_world_regdom);
3453 update_all_wiphy_regulatory(NL80211_REGDOM_SET_BY_CORE);
3454 print_regdomain(get_cfg80211_regdom());
3455 nl80211_send_reg_change_event(&core_request_world);
3456 reg_set_request_processed();
3458 if (is_an_alpha2(alpha2) &&
3459 !regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER)) {
3460 struct regulatory_request *ureq;
3462 spin_lock(®_requests_lock);
3463 ureq = list_last_entry(®_requests_list,
3464 struct regulatory_request,
3466 list_del(&ureq->list);
3467 spin_unlock(®_requests_lock);
3469 notify_self_managed_wiphys(ureq);
3470 reg_update_last_request(ureq);
3471 set_regdom(reg_copy_regd(cfg80211_user_regdom),
3472 REGD_SOURCE_CACHED);
3475 regulatory_hint_core(world_alpha2);
3478 * This restores the ieee80211_regdom module parameter
3479 * preference or the last user requested regulatory
3480 * settings, user regulatory settings takes precedence.
3482 if (is_an_alpha2(alpha2))
3483 regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER);
3486 spin_lock(®_requests_lock);
3487 list_splice_tail_init(&tmp_reg_req_list, ®_requests_list);
3488 spin_unlock(®_requests_lock);
3490 pr_debug("Kicking the queue\n");
3492 schedule_work(®_work);
3495 static bool is_wiphy_all_set_reg_flag(enum ieee80211_regulatory_flags flag)
3497 struct cfg80211_registered_device *rdev;
3498 struct wireless_dev *wdev;
3500 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3501 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3503 if (!(wdev->wiphy->regulatory_flags & flag)) {
3514 void regulatory_hint_disconnect(void)
3516 /* Restore of regulatory settings is not required when wiphy(s)
3517 * ignore IE from connected access point but clearance of beacon hints
3518 * is required when wiphy(s) supports beacon hints.
3520 if (is_wiphy_all_set_reg_flag(REGULATORY_COUNTRY_IE_IGNORE)) {
3521 struct reg_beacon *reg_beacon, *btmp;
3523 if (is_wiphy_all_set_reg_flag(REGULATORY_DISABLE_BEACON_HINTS))
3526 spin_lock_bh(®_pending_beacons_lock);
3527 list_for_each_entry_safe(reg_beacon, btmp,
3528 ®_pending_beacons, list) {
3529 list_del(®_beacon->list);
3532 spin_unlock_bh(®_pending_beacons_lock);
3534 list_for_each_entry_safe(reg_beacon, btmp,
3535 ®_beacon_list, list) {
3536 list_del(®_beacon->list);
3543 pr_debug("All devices are disconnected, going to restore regulatory settings\n");
3544 restore_regulatory_settings(false, true);
3547 static bool freq_is_chan_12_13_14(u32 freq)
3549 if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) ||
3550 freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) ||
3551 freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ))
3556 static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
3558 struct reg_beacon *pending_beacon;
3560 list_for_each_entry(pending_beacon, ®_pending_beacons, list)
3561 if (ieee80211_channel_equal(beacon_chan,
3562 &pending_beacon->chan))
3567 int regulatory_hint_found_beacon(struct wiphy *wiphy,
3568 struct ieee80211_channel *beacon_chan,
3571 struct reg_beacon *reg_beacon;
3574 if (beacon_chan->beacon_found ||
3575 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
3576 (beacon_chan->band == NL80211_BAND_2GHZ &&
3577 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
3580 spin_lock_bh(®_pending_beacons_lock);
3581 processing = pending_reg_beacon(beacon_chan);
3582 spin_unlock_bh(®_pending_beacons_lock);
3587 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
3591 pr_debug("Found new beacon on frequency: %d.%03d MHz (Ch %d) on %s\n",
3592 beacon_chan->center_freq, beacon_chan->freq_offset,
3593 ieee80211_freq_khz_to_channel(
3594 ieee80211_channel_to_khz(beacon_chan)),
3597 memcpy(®_beacon->chan, beacon_chan,
3598 sizeof(struct ieee80211_channel));
3601 * Since we can be called from BH or and non-BH context
3602 * we must use spin_lock_bh()
3604 spin_lock_bh(®_pending_beacons_lock);
3605 list_add_tail(®_beacon->list, ®_pending_beacons);
3606 spin_unlock_bh(®_pending_beacons_lock);
3608 schedule_work(®_work);
3613 static void print_rd_rules(const struct ieee80211_regdomain *rd)
3616 const struct ieee80211_reg_rule *reg_rule = NULL;
3617 const struct ieee80211_freq_range *freq_range = NULL;
3618 const struct ieee80211_power_rule *power_rule = NULL;
3619 char bw[32], cac_time[32];
3621 pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
3623 for (i = 0; i < rd->n_reg_rules; i++) {
3624 reg_rule = &rd->reg_rules[i];
3625 freq_range = ®_rule->freq_range;
3626 power_rule = ®_rule->power_rule;
3628 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
3629 snprintf(bw, sizeof(bw), "%d KHz, %u KHz AUTO",
3630 freq_range->max_bandwidth_khz,
3631 reg_get_max_bandwidth(rd, reg_rule));
3633 snprintf(bw, sizeof(bw), "%d KHz",
3634 freq_range->max_bandwidth_khz);
3636 if (reg_rule->flags & NL80211_RRF_DFS)
3637 scnprintf(cac_time, sizeof(cac_time), "%u s",
3638 reg_rule->dfs_cac_ms/1000);
3640 scnprintf(cac_time, sizeof(cac_time), "N/A");
3644 * There may not be documentation for max antenna gain
3645 * in certain regions
3647 if (power_rule->max_antenna_gain)
3648 pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
3649 freq_range->start_freq_khz,
3650 freq_range->end_freq_khz,
3652 power_rule->max_antenna_gain,
3653 power_rule->max_eirp,
3656 pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
3657 freq_range->start_freq_khz,
3658 freq_range->end_freq_khz,
3660 power_rule->max_eirp,
3665 bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
3667 switch (dfs_region) {
3668 case NL80211_DFS_UNSET:
3669 case NL80211_DFS_FCC:
3670 case NL80211_DFS_ETSI:
3671 case NL80211_DFS_JP:
3674 pr_debug("Ignoring unknown DFS master region: %d\n", dfs_region);
3679 static void print_regdomain(const struct ieee80211_regdomain *rd)
3681 struct regulatory_request *lr = get_last_request();
3683 if (is_intersected_alpha2(rd->alpha2)) {
3684 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
3685 struct cfg80211_registered_device *rdev;
3686 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
3688 pr_debug("Current regulatory domain updated by AP to: %c%c\n",
3689 rdev->country_ie_alpha2[0],
3690 rdev->country_ie_alpha2[1]);
3692 pr_debug("Current regulatory domain intersected:\n");
3694 pr_debug("Current regulatory domain intersected:\n");
3695 } else if (is_world_regdom(rd->alpha2)) {
3696 pr_debug("World regulatory domain updated:\n");
3698 if (is_unknown_alpha2(rd->alpha2))
3699 pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n");
3701 if (reg_request_cell_base(lr))
3702 pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n",
3703 rd->alpha2[0], rd->alpha2[1]);
3705 pr_debug("Regulatory domain changed to country: %c%c\n",
3706 rd->alpha2[0], rd->alpha2[1]);
3710 pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
3714 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
3716 pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
3720 static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
3722 if (!is_world_regdom(rd->alpha2))
3724 update_world_regdomain(rd);
3728 static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
3729 struct regulatory_request *user_request)
3731 const struct ieee80211_regdomain *intersected_rd = NULL;
3733 if (!regdom_changes(rd->alpha2))
3736 if (!is_valid_rd(rd)) {
3737 pr_err("Invalid regulatory domain detected: %c%c\n",
3738 rd->alpha2[0], rd->alpha2[1]);
3739 print_regdomain_info(rd);
3743 if (!user_request->intersect) {
3744 reset_regdomains(false, rd);
3748 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
3749 if (!intersected_rd)
3754 reset_regdomains(false, intersected_rd);
3759 static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
3760 struct regulatory_request *driver_request)
3762 const struct ieee80211_regdomain *regd;
3763 const struct ieee80211_regdomain *intersected_rd = NULL;
3764 const struct ieee80211_regdomain *tmp;
3765 struct wiphy *request_wiphy;
3767 if (is_world_regdom(rd->alpha2))
3770 if (!regdom_changes(rd->alpha2))
3773 if (!is_valid_rd(rd)) {
3774 pr_err("Invalid regulatory domain detected: %c%c\n",
3775 rd->alpha2[0], rd->alpha2[1]);
3776 print_regdomain_info(rd);
3780 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
3784 if (!driver_request->intersect) {
3785 if (request_wiphy->regd)
3788 regd = reg_copy_regd(rd);
3790 return PTR_ERR(regd);
3792 rcu_assign_pointer(request_wiphy->regd, regd);
3793 reset_regdomains(false, rd);
3797 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
3798 if (!intersected_rd)
3802 * We can trash what CRDA provided now.
3803 * However if a driver requested this specific regulatory
3804 * domain we keep it for its private use
3806 tmp = get_wiphy_regdom(request_wiphy);
3807 rcu_assign_pointer(request_wiphy->regd, rd);
3808 rcu_free_regdom(tmp);
3812 reset_regdomains(false, intersected_rd);
3817 static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
3818 struct regulatory_request *country_ie_request)
3820 struct wiphy *request_wiphy;
3822 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
3823 !is_unknown_alpha2(rd->alpha2))
3827 * Lets only bother proceeding on the same alpha2 if the current
3828 * rd is non static (it means CRDA was present and was used last)
3829 * and the pending request came in from a country IE
3832 if (!is_valid_rd(rd)) {
3833 pr_err("Invalid regulatory domain detected: %c%c\n",
3834 rd->alpha2[0], rd->alpha2[1]);
3835 print_regdomain_info(rd);
3839 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
3843 if (country_ie_request->intersect)
3846 reset_regdomains(false, rd);
3851 * Use this call to set the current regulatory domain. Conflicts with
3852 * multiple drivers can be ironed out later. Caller must've already
3853 * kmalloc'd the rd structure.
3855 int set_regdom(const struct ieee80211_regdomain *rd,
3856 enum ieee80211_regd_source regd_src)
3858 struct regulatory_request *lr;
3859 bool user_reset = false;
3862 if (IS_ERR_OR_NULL(rd))
3865 if (!reg_is_valid_request(rd->alpha2)) {
3870 if (regd_src == REGD_SOURCE_CRDA)
3871 reset_crda_timeouts();
3873 lr = get_last_request();
3875 /* Note that this doesn't update the wiphys, this is done below */
3876 switch (lr->initiator) {
3877 case NL80211_REGDOM_SET_BY_CORE:
3878 r = reg_set_rd_core(rd);
3880 case NL80211_REGDOM_SET_BY_USER:
3881 cfg80211_save_user_regdom(rd);
3882 r = reg_set_rd_user(rd, lr);
3885 case NL80211_REGDOM_SET_BY_DRIVER:
3886 r = reg_set_rd_driver(rd, lr);
3888 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
3889 r = reg_set_rd_country_ie(rd, lr);
3892 WARN(1, "invalid initiator %d\n", lr->initiator);
3900 reg_set_request_processed();
3903 /* Back to world regulatory in case of errors */
3904 restore_regulatory_settings(user_reset, false);
3911 /* This would make this whole thing pointless */
3912 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
3915 /* update all wiphys now with the new established regulatory domain */
3916 update_all_wiphy_regulatory(lr->initiator);
3918 print_regdomain(get_cfg80211_regdom());
3920 nl80211_send_reg_change_event(lr);
3922 reg_set_request_processed();
3927 static int __regulatory_set_wiphy_regd(struct wiphy *wiphy,
3928 struct ieee80211_regdomain *rd)
3930 const struct ieee80211_regdomain *regd;
3931 const struct ieee80211_regdomain *prev_regd;
3932 struct cfg80211_registered_device *rdev;
3934 if (WARN_ON(!wiphy || !rd))
3937 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED),
3938 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n"))
3941 if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) {
3942 print_regdomain_info(rd);
3946 regd = reg_copy_regd(rd);
3948 return PTR_ERR(regd);
3950 rdev = wiphy_to_rdev(wiphy);
3952 spin_lock(®_requests_lock);
3953 prev_regd = rdev->requested_regd;
3954 rdev->requested_regd = regd;
3955 spin_unlock(®_requests_lock);
3961 int regulatory_set_wiphy_regd(struct wiphy *wiphy,
3962 struct ieee80211_regdomain *rd)
3964 int ret = __regulatory_set_wiphy_regd(wiphy, rd);
3969 schedule_work(®_work);
3972 EXPORT_SYMBOL(regulatory_set_wiphy_regd);
3974 int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy,
3975 struct ieee80211_regdomain *rd)
3981 ret = __regulatory_set_wiphy_regd(wiphy, rd);
3985 /* process the request immediately */
3986 reg_process_self_managed_hints();
3989 EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl);
3991 void wiphy_regulatory_register(struct wiphy *wiphy)
3993 struct regulatory_request *lr = get_last_request();
3995 /* self-managed devices ignore beacon hints and country IE */
3996 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
3997 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
3998 REGULATORY_COUNTRY_IE_IGNORE;
4001 * The last request may have been received before this
4002 * registration call. Call the driver notifier if
4003 * initiator is USER.
4005 if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
4006 reg_call_notifier(wiphy, lr);
4009 if (!reg_dev_ignore_cell_hint(wiphy))
4010 reg_num_devs_support_basehint++;
4012 wiphy_update_regulatory(wiphy, lr->initiator);
4013 wiphy_all_share_dfs_chan_state(wiphy);
4014 reg_process_self_managed_hints();
4017 void wiphy_regulatory_deregister(struct wiphy *wiphy)
4019 struct wiphy *request_wiphy = NULL;
4020 struct regulatory_request *lr;
4022 lr = get_last_request();
4024 if (!reg_dev_ignore_cell_hint(wiphy))
4025 reg_num_devs_support_basehint--;
4027 rcu_free_regdom(get_wiphy_regdom(wiphy));
4028 RCU_INIT_POINTER(wiphy->regd, NULL);
4031 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
4033 if (!request_wiphy || request_wiphy != wiphy)
4036 lr->wiphy_idx = WIPHY_IDX_INVALID;
4037 lr->country_ie_env = ENVIRON_ANY;
4041 * See FCC notices for UNII band definitions
4042 * 5GHz: https://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii
4043 * 6GHz: https://www.fcc.gov/document/fcc-proposes-more-spectrum-unlicensed-use-0
4045 int cfg80211_get_unii(int freq)
4048 if (freq >= 5150 && freq <= 5250)
4052 if (freq > 5250 && freq <= 5350)
4056 if (freq > 5350 && freq <= 5470)
4060 if (freq > 5470 && freq <= 5725)
4064 if (freq > 5725 && freq <= 5825)
4068 if (freq > 5925 && freq <= 6425)
4072 if (freq > 6425 && freq <= 6525)
4076 if (freq > 6525 && freq <= 6875)
4080 if (freq > 6875 && freq <= 7125)
4086 bool regulatory_indoor_allowed(void)
4088 return reg_is_indoor;
4091 bool regulatory_pre_cac_allowed(struct wiphy *wiphy)
4093 const struct ieee80211_regdomain *regd = NULL;
4094 const struct ieee80211_regdomain *wiphy_regd = NULL;
4095 bool pre_cac_allowed = false;
4099 regd = rcu_dereference(cfg80211_regdomain);
4100 wiphy_regd = rcu_dereference(wiphy->regd);
4102 if (regd->dfs_region == NL80211_DFS_ETSI)
4103 pre_cac_allowed = true;
4107 return pre_cac_allowed;
4110 if (regd->dfs_region == wiphy_regd->dfs_region &&
4111 wiphy_regd->dfs_region == NL80211_DFS_ETSI)
4112 pre_cac_allowed = true;
4116 return pre_cac_allowed;
4118 EXPORT_SYMBOL(regulatory_pre_cac_allowed);
4120 static void cfg80211_check_and_end_cac(struct cfg80211_registered_device *rdev)
4122 struct wireless_dev *wdev;
4123 /* If we finished CAC or received radar, we should end any
4124 * CAC running on the same channels.
4125 * the check !cfg80211_chandef_dfs_usable contain 2 options:
4126 * either all channels are available - those the CAC_FINISHED
4127 * event has effected another wdev state, or there is a channel
4128 * in unavailable state in wdev chandef - those the RADAR_DETECTED
4129 * event has effected another wdev state.
4130 * In both cases we should end the CAC on the wdev.
4132 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4133 if (wdev->cac_started &&
4134 !cfg80211_chandef_dfs_usable(&rdev->wiphy, &wdev->chandef))
4135 rdev_end_cac(rdev, wdev->netdev);
4139 void regulatory_propagate_dfs_state(struct wiphy *wiphy,
4140 struct cfg80211_chan_def *chandef,
4141 enum nl80211_dfs_state dfs_state,
4142 enum nl80211_radar_event event)
4144 struct cfg80211_registered_device *rdev;
4148 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
4151 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
4152 if (wiphy == &rdev->wiphy)
4155 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
4158 if (!ieee80211_get_channel(&rdev->wiphy,
4159 chandef->chan->center_freq))
4162 cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state);
4164 if (event == NL80211_RADAR_DETECTED ||
4165 event == NL80211_RADAR_CAC_FINISHED) {
4166 cfg80211_sched_dfs_chan_update(rdev);
4167 cfg80211_check_and_end_cac(rdev);
4170 nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL);
4174 static int __init regulatory_init_db(void)
4179 * It's possible that - due to other bugs/issues - cfg80211
4180 * never called regulatory_init() below, or that it failed;
4181 * in that case, don't try to do any further work here as
4182 * it's doomed to lead to crashes.
4184 if (IS_ERR_OR_NULL(reg_pdev))
4187 err = load_builtin_regdb_keys();
4189 platform_device_unregister(reg_pdev);
4193 /* We always try to get an update for the static regdomain */
4194 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
4196 if (err == -ENOMEM) {
4197 platform_device_unregister(reg_pdev);
4201 * N.B. kobject_uevent_env() can fail mainly for when we're out
4202 * memory which is handled and propagated appropriately above
4203 * but it can also fail during a netlink_broadcast() or during
4204 * early boot for call_usermodehelper(). For now treat these
4205 * errors as non-fatal.
4207 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
4211 * Finally, if the user set the module parameter treat it
4214 if (!is_world_regdom(ieee80211_regdom))
4215 regulatory_hint_user(ieee80211_regdom,
4216 NL80211_USER_REG_HINT_USER);
4221 late_initcall(regulatory_init_db);
4224 int __init regulatory_init(void)
4226 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
4227 if (IS_ERR(reg_pdev))
4228 return PTR_ERR(reg_pdev);
4230 spin_lock_init(®_requests_lock);
4231 spin_lock_init(®_pending_beacons_lock);
4232 spin_lock_init(®_indoor_lock);
4234 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
4236 user_alpha2[0] = '9';
4237 user_alpha2[1] = '7';
4240 return regulatory_init_db();
4246 void regulatory_exit(void)
4248 struct regulatory_request *reg_request, *tmp;
4249 struct reg_beacon *reg_beacon, *btmp;
4251 cancel_work_sync(®_work);
4252 cancel_crda_timeout_sync();
4253 cancel_delayed_work_sync(®_check_chans);
4255 /* Lock to suppress warnings */
4257 reset_regdomains(true, NULL);
4260 dev_set_uevent_suppress(®_pdev->dev, true);
4262 platform_device_unregister(reg_pdev);
4264 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) {
4265 list_del(®_beacon->list);
4269 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) {
4270 list_del(®_beacon->list);
4274 list_for_each_entry_safe(reg_request, tmp, ®_requests_list, list) {
4275 list_del(®_request->list);
4279 if (!IS_ERR_OR_NULL(regdb))
4281 if (!IS_ERR_OR_NULL(cfg80211_user_regdom))
4282 kfree(cfg80211_user_regdom);
4284 free_regdb_keyring();