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
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * DOC: Wireless regulatory infrastructure
26 * The usual implementation is for a driver to read a device EEPROM to
27 * determine which regulatory domain it should be operating under, then
28 * looking up the allowable channels in a driver-local table and finally
29 * registering those channels in the wiphy structure.
31 * Another set of compliance enforcement is for drivers to use their
32 * own compliance limits which can be stored on the EEPROM. The host
33 * driver or firmware may ensure these are used.
35 * In addition to all this we provide an extra layer of regulatory
36 * conformance. For drivers which do not have any regulatory
37 * information CRDA provides the complete regulatory solution.
38 * For others it provides a community effort on further restrictions
39 * to enhance compliance.
41 * Note: When number of rules --> infinity we will not be able to
42 * index on alpha2 any more, instead we'll probably have to
43 * rely on some SHA1 checksum of the regdomain for example.
47 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49 #include <linux/kernel.h>
50 #include <linux/export.h>
51 #include <linux/slab.h>
52 #include <linux/list.h>
53 #include <linux/ctype.h>
54 #include <linux/nl80211.h>
55 #include <linux/platform_device.h>
56 #include <linux/verification.h>
57 #include <linux/moduleparam.h>
58 #include <linux/firmware.h>
59 #include <net/cfg80211.h>
66 * Grace period we give before making sure all current interfaces reside on
67 * channels allowed by the current regulatory domain.
69 #define REG_ENFORCE_GRACE_MS 60000
72 * enum reg_request_treatment - regulatory request treatment
74 * @REG_REQ_OK: continue processing the regulatory request
75 * @REG_REQ_IGNORE: ignore the regulatory request
76 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
77 * be intersected with the current one.
78 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
79 * regulatory settings, and no further processing is required.
81 enum reg_request_treatment {
88 static struct regulatory_request core_request_world = {
89 .initiator = NL80211_REGDOM_SET_BY_CORE,
94 .country_ie_env = ENVIRON_ANY,
98 * Receipt of information from last regulatory request,
99 * protected by RTNL (and can be accessed with RCU protection)
101 static struct regulatory_request __rcu *last_request =
102 (void __force __rcu *)&core_request_world;
104 /* To trigger userspace events and load firmware */
105 static struct platform_device *reg_pdev;
108 * Central wireless core regulatory domains, we only need two,
109 * the current one and a world regulatory domain in case we have no
110 * information to give us an alpha2.
111 * (protected by RTNL, can be read under RCU)
113 const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
116 * Number of devices that registered to the core
117 * that support cellular base station regulatory hints
118 * (protected by RTNL)
120 static int reg_num_devs_support_basehint;
123 * State variable indicating if the platform on which the devices
124 * are attached is operating in an indoor environment. The state variable
125 * is relevant for all registered devices.
127 static bool reg_is_indoor;
128 static spinlock_t reg_indoor_lock;
130 /* Used to track the userspace process controlling the indoor setting */
131 static u32 reg_is_indoor_portid;
133 static void restore_regulatory_settings(bool reset_user);
135 static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
137 return rtnl_dereference(cfg80211_regdomain);
140 const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
142 return rtnl_dereference(wiphy->regd);
145 static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
147 switch (dfs_region) {
148 case NL80211_DFS_UNSET:
150 case NL80211_DFS_FCC:
152 case NL80211_DFS_ETSI:
160 enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
162 const struct ieee80211_regdomain *regd = NULL;
163 const struct ieee80211_regdomain *wiphy_regd = NULL;
165 regd = get_cfg80211_regdom();
169 wiphy_regd = get_wiphy_regdom(wiphy);
173 if (wiphy_regd->dfs_region == regd->dfs_region)
176 pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n",
177 dev_name(&wiphy->dev),
178 reg_dfs_region_str(wiphy_regd->dfs_region),
179 reg_dfs_region_str(regd->dfs_region));
182 return regd->dfs_region;
185 static void rcu_free_regdom(const struct ieee80211_regdomain *r)
189 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
192 static struct regulatory_request *get_last_request(void)
194 return rcu_dereference_rtnl(last_request);
197 /* Used to queue up regulatory hints */
198 static LIST_HEAD(reg_requests_list);
199 static spinlock_t reg_requests_lock;
201 /* Used to queue up beacon hints for review */
202 static LIST_HEAD(reg_pending_beacons);
203 static spinlock_t reg_pending_beacons_lock;
205 /* Used to keep track of processed beacon hints */
206 static LIST_HEAD(reg_beacon_list);
209 struct list_head list;
210 struct ieee80211_channel chan;
213 static void reg_check_chans_work(struct work_struct *work);
214 static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);
216 static void reg_todo(struct work_struct *work);
217 static DECLARE_WORK(reg_work, reg_todo);
219 /* We keep a static world regulatory domain in case of the absence of CRDA */
220 static const struct ieee80211_regdomain world_regdom = {
224 /* IEEE 802.11b/g, channels 1..11 */
225 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
226 /* IEEE 802.11b/g, channels 12..13. */
227 REG_RULE(2467-10, 2472+10, 20, 6, 20,
228 NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW),
229 /* IEEE 802.11 channel 14 - Only JP enables
230 * this and for 802.11b only */
231 REG_RULE(2484-10, 2484+10, 20, 6, 20,
233 NL80211_RRF_NO_OFDM),
234 /* IEEE 802.11a, channel 36..48 */
235 REG_RULE(5180-10, 5240+10, 80, 6, 20,
237 NL80211_RRF_AUTO_BW),
239 /* IEEE 802.11a, channel 52..64 - DFS required */
240 REG_RULE(5260-10, 5320+10, 80, 6, 20,
242 NL80211_RRF_AUTO_BW |
245 /* IEEE 802.11a, channel 100..144 - DFS required */
246 REG_RULE(5500-10, 5720+10, 160, 6, 20,
250 /* IEEE 802.11a, channel 149..165 */
251 REG_RULE(5745-10, 5825+10, 80, 6, 20,
254 /* IEEE 802.11ad (60GHz), channels 1..3 */
255 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
259 /* protected by RTNL */
260 static const struct ieee80211_regdomain *cfg80211_world_regdom =
263 static char *ieee80211_regdom = "00";
264 static char user_alpha2[2];
266 module_param(ieee80211_regdom, charp, 0444);
267 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
269 static void reg_free_request(struct regulatory_request *request)
271 if (request == &core_request_world)
274 if (request != get_last_request())
278 static void reg_free_last_request(void)
280 struct regulatory_request *lr = get_last_request();
282 if (lr != &core_request_world && lr)
283 kfree_rcu(lr, rcu_head);
286 static void reg_update_last_request(struct regulatory_request *request)
288 struct regulatory_request *lr;
290 lr = get_last_request();
294 reg_free_last_request();
295 rcu_assign_pointer(last_request, request);
298 static void reset_regdomains(bool full_reset,
299 const struct ieee80211_regdomain *new_regdom)
301 const struct ieee80211_regdomain *r;
305 r = get_cfg80211_regdom();
307 /* avoid freeing static information or freeing something twice */
308 if (r == cfg80211_world_regdom)
310 if (cfg80211_world_regdom == &world_regdom)
311 cfg80211_world_regdom = NULL;
312 if (r == &world_regdom)
316 rcu_free_regdom(cfg80211_world_regdom);
318 cfg80211_world_regdom = &world_regdom;
319 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
324 reg_update_last_request(&core_request_world);
328 * Dynamic world regulatory domain requested by the wireless
329 * core upon initialization
331 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
333 struct regulatory_request *lr;
335 lr = get_last_request();
339 reset_regdomains(false, rd);
341 cfg80211_world_regdom = rd;
344 bool is_world_regdom(const char *alpha2)
348 return alpha2[0] == '0' && alpha2[1] == '0';
351 static bool is_alpha2_set(const char *alpha2)
355 return alpha2[0] && alpha2[1];
358 static bool is_unknown_alpha2(const char *alpha2)
363 * Special case where regulatory domain was built by driver
364 * but a specific alpha2 cannot be determined
366 return alpha2[0] == '9' && alpha2[1] == '9';
369 static bool is_intersected_alpha2(const char *alpha2)
374 * Special case where regulatory domain is the
375 * result of an intersection between two regulatory domain
378 return alpha2[0] == '9' && alpha2[1] == '8';
381 static bool is_an_alpha2(const char *alpha2)
385 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
388 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
390 if (!alpha2_x || !alpha2_y)
392 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
395 static bool regdom_changes(const char *alpha2)
397 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
401 return !alpha2_equal(r->alpha2, alpha2);
405 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
406 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
407 * has ever been issued.
409 static bool is_user_regdom_saved(void)
411 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
414 /* This would indicate a mistake on the design */
415 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
416 "Unexpected user alpha2: %c%c\n",
417 user_alpha2[0], user_alpha2[1]))
423 static const struct ieee80211_regdomain *
424 reg_copy_regd(const struct ieee80211_regdomain *src_regd)
426 struct ieee80211_regdomain *regd;
431 sizeof(struct ieee80211_regdomain) +
432 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
434 regd = kzalloc(size_of_regd, GFP_KERNEL);
436 return ERR_PTR(-ENOMEM);
438 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
440 for (i = 0; i < src_regd->n_reg_rules; i++)
441 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i],
442 sizeof(struct ieee80211_reg_rule));
447 struct reg_regdb_apply_request {
448 struct list_head list;
449 const struct ieee80211_regdomain *regdom;
452 static LIST_HEAD(reg_regdb_apply_list);
453 static DEFINE_MUTEX(reg_regdb_apply_mutex);
455 static void reg_regdb_apply(struct work_struct *work)
457 struct reg_regdb_apply_request *request;
461 mutex_lock(®_regdb_apply_mutex);
462 while (!list_empty(®_regdb_apply_list)) {
463 request = list_first_entry(®_regdb_apply_list,
464 struct reg_regdb_apply_request,
466 list_del(&request->list);
468 set_regdom(request->regdom, REGD_SOURCE_INTERNAL_DB);
471 mutex_unlock(®_regdb_apply_mutex);
476 static DECLARE_WORK(reg_regdb_work, reg_regdb_apply);
478 static int reg_schedule_apply(const struct ieee80211_regdomain *regdom)
480 struct reg_regdb_apply_request *request;
482 request = kzalloc(sizeof(struct reg_regdb_apply_request), GFP_KERNEL);
488 request->regdom = regdom;
490 mutex_lock(®_regdb_apply_mutex);
491 list_add_tail(&request->list, ®_regdb_apply_list);
492 mutex_unlock(®_regdb_apply_mutex);
494 schedule_work(®_regdb_work);
498 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
499 /* Max number of consecutive attempts to communicate with CRDA */
500 #define REG_MAX_CRDA_TIMEOUTS 10
502 static u32 reg_crda_timeouts;
504 static void crda_timeout_work(struct work_struct *work);
505 static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work);
507 static void crda_timeout_work(struct work_struct *work)
509 pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
512 restore_regulatory_settings(true);
516 static void cancel_crda_timeout(void)
518 cancel_delayed_work(&crda_timeout);
521 static void cancel_crda_timeout_sync(void)
523 cancel_delayed_work_sync(&crda_timeout);
526 static void reset_crda_timeouts(void)
528 reg_crda_timeouts = 0;
532 * This lets us keep regulatory code which is updated on a regulatory
533 * basis in userspace.
535 static int call_crda(const char *alpha2)
538 char *env[] = { country, NULL };
541 snprintf(country, sizeof(country), "COUNTRY=%c%c",
542 alpha2[0], alpha2[1]);
544 if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) {
545 pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n");
549 if (!is_world_regdom((char *) alpha2))
550 pr_debug("Calling CRDA for country: %c%c\n",
551 alpha2[0], alpha2[1]);
553 pr_debug("Calling CRDA to update world regulatory domain\n");
555 ret = kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, env);
559 queue_delayed_work(system_power_efficient_wq,
560 &crda_timeout, msecs_to_jiffies(3142));
564 static inline void cancel_crda_timeout(void) {}
565 static inline void cancel_crda_timeout_sync(void) {}
566 static inline void reset_crda_timeouts(void) {}
567 static inline int call_crda(const char *alpha2)
571 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
573 /* code to directly load a firmware database through request_firmware */
574 static const struct fwdb_header *regdb;
576 struct fwdb_country {
579 /* this struct cannot be extended */
580 } __packed __aligned(4);
582 struct fwdb_collection {
586 /* no optional data yet */
587 /* aligned to 2, then followed by __be16 array of rule pointers */
588 } __packed __aligned(4);
591 FWDB_FLAG_NO_OFDM = BIT(0),
592 FWDB_FLAG_NO_OUTDOOR = BIT(1),
593 FWDB_FLAG_DFS = BIT(2),
594 FWDB_FLAG_NO_IR = BIT(3),
595 FWDB_FLAG_AUTO_BW = BIT(4),
602 __be32 start, end, max_bw;
603 /* start of optional data */
605 } __packed __aligned(4);
607 #define FWDB_MAGIC 0x52474442
608 #define FWDB_VERSION 20
613 struct fwdb_country country[];
614 } __packed __aligned(4);
616 static bool valid_rule(const u8 *data, unsigned int size, u16 rule_ptr)
618 struct fwdb_rule *rule = (void *)(data + (rule_ptr << 2));
620 if ((u8 *)rule + sizeof(rule->len) > data + size)
623 /* mandatory fields */
624 if (rule->len < offsetofend(struct fwdb_rule, max_bw))
630 static bool valid_country(const u8 *data, unsigned int size,
631 const struct fwdb_country *country)
633 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
634 struct fwdb_collection *coll = (void *)(data + ptr);
638 /* make sure we can read len/n_rules */
639 if ((u8 *)coll + offsetofend(typeof(*coll), n_rules) > data + size)
642 /* make sure base struct and all rules fit */
643 if ((u8 *)coll + ALIGN(coll->len, 2) +
644 (coll->n_rules * 2) > data + size)
647 /* mandatory fields must exist */
648 if (coll->len < offsetofend(struct fwdb_collection, dfs_region))
651 rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
653 for (i = 0; i < coll->n_rules; i++) {
654 u16 rule_ptr = be16_to_cpu(rules_ptr[i]);
656 if (!valid_rule(data, size, rule_ptr))
663 #ifdef CONFIG_CFG80211_REQUIRE_SIGNED_REGDB
664 static struct key *builtin_regdb_keys;
666 static void __init load_keys_from_buffer(const u8 *p, unsigned int buflen)
668 const u8 *end = p + buflen;
673 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
674 * than 256 bytes in size.
681 plen = (p[2] << 8) | p[3];
686 key = key_create_or_update(make_key_ref(builtin_regdb_keys, 1),
687 "asymmetric", NULL, p, plen,
688 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
689 KEY_USR_VIEW | KEY_USR_READ),
690 KEY_ALLOC_NOT_IN_QUOTA |
692 KEY_ALLOC_BYPASS_RESTRICTION);
694 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
697 pr_notice("Loaded X.509 cert '%s'\n",
698 key_ref_to_ptr(key)->description);
707 pr_err("Problem parsing in-kernel X.509 certificate list\n");
710 static int __init load_builtin_regdb_keys(void)
713 keyring_alloc(".builtin_regdb_keys",
714 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
715 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
716 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
717 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
718 if (IS_ERR(builtin_regdb_keys))
719 return PTR_ERR(builtin_regdb_keys);
721 pr_notice("Loading compiled-in X.509 certificates for regulatory database\n");
723 #ifdef CONFIG_CFG80211_USE_KERNEL_REGDB_KEYS
724 load_keys_from_buffer(shipped_regdb_certs, shipped_regdb_certs_len);
726 #ifdef CONFIG_CFG80211_EXTRA_REGDB_KEYDIR
727 if (CONFIG_CFG80211_EXTRA_REGDB_KEYDIR[0] != '\0')
728 load_keys_from_buffer(extra_regdb_certs, extra_regdb_certs_len);
734 static bool regdb_has_valid_signature(const u8 *data, unsigned int size)
736 const struct firmware *sig;
739 if (request_firmware(&sig, "regulatory.db.p7s", ®_pdev->dev))
742 result = verify_pkcs7_signature(data, size, sig->data, sig->size,
744 VERIFYING_UNSPECIFIED_SIGNATURE,
747 release_firmware(sig);
752 static void free_regdb_keyring(void)
754 key_put(builtin_regdb_keys);
757 static int load_builtin_regdb_keys(void)
762 static bool regdb_has_valid_signature(const u8 *data, unsigned int size)
767 static void free_regdb_keyring(void)
770 #endif /* CONFIG_CFG80211_REQUIRE_SIGNED_REGDB */
772 static bool valid_regdb(const u8 *data, unsigned int size)
774 const struct fwdb_header *hdr = (void *)data;
775 const struct fwdb_country *country;
777 if (size < sizeof(*hdr))
780 if (hdr->magic != cpu_to_be32(FWDB_MAGIC))
783 if (hdr->version != cpu_to_be32(FWDB_VERSION))
786 if (!regdb_has_valid_signature(data, size))
789 country = &hdr->country[0];
790 while ((u8 *)(country + 1) <= data + size) {
791 if (!country->coll_ptr)
793 if (!valid_country(data, size, country))
801 static int regdb_query_country(const struct fwdb_header *db,
802 const struct fwdb_country *country)
804 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
805 struct fwdb_collection *coll = (void *)((u8 *)db + ptr);
806 struct ieee80211_regdomain *regdom;
807 unsigned int size_of_regd;
811 sizeof(struct ieee80211_regdomain) +
812 coll->n_rules * sizeof(struct ieee80211_reg_rule);
814 regdom = kzalloc(size_of_regd, GFP_KERNEL);
818 regdom->n_reg_rules = coll->n_rules;
819 regdom->alpha2[0] = country->alpha2[0];
820 regdom->alpha2[1] = country->alpha2[1];
821 regdom->dfs_region = coll->dfs_region;
823 for (i = 0; i < regdom->n_reg_rules; i++) {
824 __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
825 unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2;
826 struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr);
827 struct ieee80211_reg_rule *rrule = ®dom->reg_rules[i];
829 rrule->freq_range.start_freq_khz = be32_to_cpu(rule->start);
830 rrule->freq_range.end_freq_khz = be32_to_cpu(rule->end);
831 rrule->freq_range.max_bandwidth_khz = be32_to_cpu(rule->max_bw);
833 rrule->power_rule.max_antenna_gain = 0;
834 rrule->power_rule.max_eirp = be16_to_cpu(rule->max_eirp);
837 if (rule->flags & FWDB_FLAG_NO_OFDM)
838 rrule->flags |= NL80211_RRF_NO_OFDM;
839 if (rule->flags & FWDB_FLAG_NO_OUTDOOR)
840 rrule->flags |= NL80211_RRF_NO_OUTDOOR;
841 if (rule->flags & FWDB_FLAG_DFS)
842 rrule->flags |= NL80211_RRF_DFS;
843 if (rule->flags & FWDB_FLAG_NO_IR)
844 rrule->flags |= NL80211_RRF_NO_IR;
845 if (rule->flags & FWDB_FLAG_AUTO_BW)
846 rrule->flags |= NL80211_RRF_AUTO_BW;
848 rrule->dfs_cac_ms = 0;
850 /* handle optional data */
851 if (rule->len >= offsetofend(struct fwdb_rule, cac_timeout))
853 1000 * be16_to_cpu(rule->cac_timeout);
856 return reg_schedule_apply(regdom);
859 static int query_regdb(const char *alpha2)
861 const struct fwdb_header *hdr = regdb;
862 const struct fwdb_country *country;
867 return PTR_ERR(regdb);
869 country = &hdr->country[0];
870 while (country->coll_ptr) {
871 if (alpha2_equal(alpha2, country->alpha2))
872 return regdb_query_country(regdb, country);
879 static void regdb_fw_cb(const struct firmware *fw, void *context)
886 pr_info("failed to load regulatory.db\n");
887 set_error = -ENODATA;
888 } else if (!valid_regdb(fw->data, fw->size)) {
889 pr_info("loaded regulatory.db is malformed or signature is missing/invalid\n");
894 if (WARN_ON(regdb && !IS_ERR(regdb))) {
895 /* just restore and free new db */
896 } else if (set_error) {
897 regdb = ERR_PTR(set_error);
899 db = kmemdup(fw->data, fw->size, GFP_KERNEL);
902 restore = context && query_regdb(context);
909 restore_regulatory_settings(true);
915 release_firmware(fw);
918 static int query_regdb_file(const char *alpha2)
923 return query_regdb(alpha2);
925 alpha2 = kmemdup(alpha2, 2, GFP_KERNEL);
929 return request_firmware_nowait(THIS_MODULE, true, "regulatory.db",
930 ®_pdev->dev, GFP_KERNEL,
931 (void *)alpha2, regdb_fw_cb);
934 int reg_reload_regdb(void)
936 const struct firmware *fw;
940 err = request_firmware(&fw, "regulatory.db", ®_pdev->dev);
944 if (!valid_regdb(fw->data, fw->size)) {
949 db = kmemdup(fw->data, fw->size, GFP_KERNEL);
956 if (!IS_ERR_OR_NULL(regdb))
962 release_firmware(fw);
966 static bool reg_query_database(struct regulatory_request *request)
968 if (query_regdb_file(request->alpha2) == 0)
971 if (call_crda(request->alpha2) == 0)
977 bool reg_is_valid_request(const char *alpha2)
979 struct regulatory_request *lr = get_last_request();
981 if (!lr || lr->processed)
984 return alpha2_equal(lr->alpha2, alpha2);
987 static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
989 struct regulatory_request *lr = get_last_request();
992 * Follow the driver's regulatory domain, if present, unless a country
993 * IE has been processed or a user wants to help complaince further
995 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
996 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
998 return get_wiphy_regdom(wiphy);
1000 return get_cfg80211_regdom();
1004 reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
1005 const struct ieee80211_reg_rule *rule)
1007 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
1008 const struct ieee80211_freq_range *freq_range_tmp;
1009 const struct ieee80211_reg_rule *tmp;
1010 u32 start_freq, end_freq, idx, no;
1012 for (idx = 0; idx < rd->n_reg_rules; idx++)
1013 if (rule == &rd->reg_rules[idx])
1016 if (idx == rd->n_reg_rules)
1019 /* get start_freq */
1023 tmp = &rd->reg_rules[--no];
1024 freq_range_tmp = &tmp->freq_range;
1026 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
1029 freq_range = freq_range_tmp;
1032 start_freq = freq_range->start_freq_khz;
1035 freq_range = &rule->freq_range;
1038 while (no < rd->n_reg_rules - 1) {
1039 tmp = &rd->reg_rules[++no];
1040 freq_range_tmp = &tmp->freq_range;
1042 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
1045 freq_range = freq_range_tmp;
1048 end_freq = freq_range->end_freq_khz;
1050 return end_freq - start_freq;
1053 unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
1054 const struct ieee80211_reg_rule *rule)
1056 unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);
1058 if (rule->flags & NL80211_RRF_NO_160MHZ)
1059 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
1060 if (rule->flags & NL80211_RRF_NO_80MHZ)
1061 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));
1064 * HT40+/HT40- limits are handled per-channel. Only limit BW if both
1067 if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
1068 rule->flags & NL80211_RRF_NO_HT40PLUS)
1069 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));
1074 /* Sanity check on a regulatory rule */
1075 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
1077 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
1080 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
1083 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
1086 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1088 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
1089 freq_range->max_bandwidth_khz > freq_diff)
1095 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
1097 const struct ieee80211_reg_rule *reg_rule = NULL;
1100 if (!rd->n_reg_rules)
1103 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
1106 for (i = 0; i < rd->n_reg_rules; i++) {
1107 reg_rule = &rd->reg_rules[i];
1108 if (!is_valid_reg_rule(reg_rule))
1116 * freq_in_rule_band - tells us if a frequency is in a frequency band
1117 * @freq_range: frequency rule we want to query
1118 * @freq_khz: frequency we are inquiring about
1120 * This lets us know if a specific frequency rule is or is not relevant to
1121 * a specific frequency's band. Bands are device specific and artificial
1122 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
1123 * however it is safe for now to assume that a frequency rule should not be
1124 * part of a frequency's band if the start freq or end freq are off by more
1125 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
1127 * This resolution can be lowered and should be considered as we add
1128 * regulatory rule support for other "bands".
1130 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
1133 #define ONE_GHZ_IN_KHZ 1000000
1135 * From 802.11ad: directional multi-gigabit (DMG):
1136 * Pertaining to operation in a frequency band containing a channel
1137 * with the Channel starting frequency above 45 GHz.
1139 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
1140 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
1141 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
1143 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
1146 #undef ONE_GHZ_IN_KHZ
1150 * Later on we can perhaps use the more restrictive DFS
1151 * region but we don't have information for that yet so
1152 * for now simply disallow conflicts.
1154 static enum nl80211_dfs_regions
1155 reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
1156 const enum nl80211_dfs_regions dfs_region2)
1158 if (dfs_region1 != dfs_region2)
1159 return NL80211_DFS_UNSET;
1164 * Helper for regdom_intersect(), this does the real
1165 * mathematical intersection fun
1167 static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
1168 const struct ieee80211_regdomain *rd2,
1169 const struct ieee80211_reg_rule *rule1,
1170 const struct ieee80211_reg_rule *rule2,
1171 struct ieee80211_reg_rule *intersected_rule)
1173 const struct ieee80211_freq_range *freq_range1, *freq_range2;
1174 struct ieee80211_freq_range *freq_range;
1175 const struct ieee80211_power_rule *power_rule1, *power_rule2;
1176 struct ieee80211_power_rule *power_rule;
1177 u32 freq_diff, max_bandwidth1, max_bandwidth2;
1179 freq_range1 = &rule1->freq_range;
1180 freq_range2 = &rule2->freq_range;
1181 freq_range = &intersected_rule->freq_range;
1183 power_rule1 = &rule1->power_rule;
1184 power_rule2 = &rule2->power_rule;
1185 power_rule = &intersected_rule->power_rule;
1187 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
1188 freq_range2->start_freq_khz);
1189 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
1190 freq_range2->end_freq_khz);
1192 max_bandwidth1 = freq_range1->max_bandwidth_khz;
1193 max_bandwidth2 = freq_range2->max_bandwidth_khz;
1195 if (rule1->flags & NL80211_RRF_AUTO_BW)
1196 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
1197 if (rule2->flags & NL80211_RRF_AUTO_BW)
1198 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
1200 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
1202 intersected_rule->flags = rule1->flags | rule2->flags;
1205 * In case NL80211_RRF_AUTO_BW requested for both rules
1206 * set AUTO_BW in intersected rule also. Next we will
1207 * calculate BW correctly in handle_channel function.
1208 * In other case remove AUTO_BW flag while we calculate
1209 * maximum bandwidth correctly and auto calculation is
1212 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
1213 (rule2->flags & NL80211_RRF_AUTO_BW))
1214 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
1216 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
1218 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1219 if (freq_range->max_bandwidth_khz > freq_diff)
1220 freq_range->max_bandwidth_khz = freq_diff;
1222 power_rule->max_eirp = min(power_rule1->max_eirp,
1223 power_rule2->max_eirp);
1224 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
1225 power_rule2->max_antenna_gain);
1227 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
1230 if (!is_valid_reg_rule(intersected_rule))
1236 /* check whether old rule contains new rule */
1237 static bool rule_contains(struct ieee80211_reg_rule *r1,
1238 struct ieee80211_reg_rule *r2)
1240 /* for simplicity, currently consider only same flags */
1241 if (r1->flags != r2->flags)
1244 /* verify r1 is more restrictive */
1245 if ((r1->power_rule.max_antenna_gain >
1246 r2->power_rule.max_antenna_gain) ||
1247 r1->power_rule.max_eirp > r2->power_rule.max_eirp)
1250 /* make sure r2's range is contained within r1 */
1251 if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
1252 r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
1255 /* and finally verify that r1.max_bw >= r2.max_bw */
1256 if (r1->freq_range.max_bandwidth_khz <
1257 r2->freq_range.max_bandwidth_khz)
1263 /* add or extend current rules. do nothing if rule is already contained */
1264 static void add_rule(struct ieee80211_reg_rule *rule,
1265 struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
1267 struct ieee80211_reg_rule *tmp_rule;
1270 for (i = 0; i < *n_rules; i++) {
1271 tmp_rule = ®_rules[i];
1272 /* rule is already contained - do nothing */
1273 if (rule_contains(tmp_rule, rule))
1276 /* extend rule if possible */
1277 if (rule_contains(rule, tmp_rule)) {
1278 memcpy(tmp_rule, rule, sizeof(*rule));
1283 memcpy(®_rules[*n_rules], rule, sizeof(*rule));
1288 * regdom_intersect - do the intersection between two regulatory domains
1289 * @rd1: first regulatory domain
1290 * @rd2: second regulatory domain
1292 * Use this function to get the intersection between two regulatory domains.
1293 * Once completed we will mark the alpha2 for the rd as intersected, "98",
1294 * as no one single alpha2 can represent this regulatory domain.
1296 * Returns a pointer to the regulatory domain structure which will hold the
1297 * resulting intersection of rules between rd1 and rd2. We will
1298 * kzalloc() this structure for you.
1300 static struct ieee80211_regdomain *
1301 regdom_intersect(const struct ieee80211_regdomain *rd1,
1302 const struct ieee80211_regdomain *rd2)
1304 int r, size_of_regd;
1306 unsigned int num_rules = 0;
1307 const struct ieee80211_reg_rule *rule1, *rule2;
1308 struct ieee80211_reg_rule intersected_rule;
1309 struct ieee80211_regdomain *rd;
1315 * First we get a count of the rules we'll need, then we actually
1316 * build them. This is to so we can malloc() and free() a
1317 * regdomain once. The reason we use reg_rules_intersect() here
1318 * is it will return -EINVAL if the rule computed makes no sense.
1319 * All rules that do check out OK are valid.
1322 for (x = 0; x < rd1->n_reg_rules; x++) {
1323 rule1 = &rd1->reg_rules[x];
1324 for (y = 0; y < rd2->n_reg_rules; y++) {
1325 rule2 = &rd2->reg_rules[y];
1326 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
1335 size_of_regd = sizeof(struct ieee80211_regdomain) +
1336 num_rules * sizeof(struct ieee80211_reg_rule);
1338 rd = kzalloc(size_of_regd, GFP_KERNEL);
1342 for (x = 0; x < rd1->n_reg_rules; x++) {
1343 rule1 = &rd1->reg_rules[x];
1344 for (y = 0; y < rd2->n_reg_rules; y++) {
1345 rule2 = &rd2->reg_rules[y];
1346 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
1349 * No need to memset here the intersected rule here as
1350 * we're not using the stack anymore
1355 add_rule(&intersected_rule, rd->reg_rules,
1360 rd->alpha2[0] = '9';
1361 rd->alpha2[1] = '8';
1362 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
1369 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1370 * want to just have the channel structure use these
1372 static u32 map_regdom_flags(u32 rd_flags)
1374 u32 channel_flags = 0;
1375 if (rd_flags & NL80211_RRF_NO_IR_ALL)
1376 channel_flags |= IEEE80211_CHAN_NO_IR;
1377 if (rd_flags & NL80211_RRF_DFS)
1378 channel_flags |= IEEE80211_CHAN_RADAR;
1379 if (rd_flags & NL80211_RRF_NO_OFDM)
1380 channel_flags |= IEEE80211_CHAN_NO_OFDM;
1381 if (rd_flags & NL80211_RRF_NO_OUTDOOR)
1382 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
1383 if (rd_flags & NL80211_RRF_IR_CONCURRENT)
1384 channel_flags |= IEEE80211_CHAN_IR_CONCURRENT;
1385 if (rd_flags & NL80211_RRF_NO_HT40MINUS)
1386 channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
1387 if (rd_flags & NL80211_RRF_NO_HT40PLUS)
1388 channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
1389 if (rd_flags & NL80211_RRF_NO_80MHZ)
1390 channel_flags |= IEEE80211_CHAN_NO_80MHZ;
1391 if (rd_flags & NL80211_RRF_NO_160MHZ)
1392 channel_flags |= IEEE80211_CHAN_NO_160MHZ;
1393 return channel_flags;
1396 static const struct ieee80211_reg_rule *
1397 freq_reg_info_regd(u32 center_freq,
1398 const struct ieee80211_regdomain *regd, u32 bw)
1401 bool band_rule_found = false;
1402 bool bw_fits = false;
1405 return ERR_PTR(-EINVAL);
1407 for (i = 0; i < regd->n_reg_rules; i++) {
1408 const struct ieee80211_reg_rule *rr;
1409 const struct ieee80211_freq_range *fr = NULL;
1411 rr = ®d->reg_rules[i];
1412 fr = &rr->freq_range;
1415 * We only need to know if one frequency rule was
1416 * was in center_freq's band, that's enough, so lets
1417 * not overwrite it once found
1419 if (!band_rule_found)
1420 band_rule_found = freq_in_rule_band(fr, center_freq);
1422 bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw);
1424 if (band_rule_found && bw_fits)
1428 if (!band_rule_found)
1429 return ERR_PTR(-ERANGE);
1431 return ERR_PTR(-EINVAL);
1434 static const struct ieee80211_reg_rule *
1435 __freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw)
1437 const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy);
1438 const struct ieee80211_reg_rule *reg_rule = NULL;
1441 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
1442 reg_rule = freq_reg_info_regd(center_freq, regd, bw);
1443 if (!IS_ERR(reg_rule))
1450 const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
1453 return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(20));
1455 EXPORT_SYMBOL(freq_reg_info);
1457 const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
1459 switch (initiator) {
1460 case NL80211_REGDOM_SET_BY_CORE:
1462 case NL80211_REGDOM_SET_BY_USER:
1464 case NL80211_REGDOM_SET_BY_DRIVER:
1466 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1467 return "country IE";
1473 EXPORT_SYMBOL(reg_initiator_name);
1475 static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd,
1476 const struct ieee80211_reg_rule *reg_rule,
1477 const struct ieee80211_channel *chan)
1479 const struct ieee80211_freq_range *freq_range = NULL;
1480 u32 max_bandwidth_khz, bw_flags = 0;
1482 freq_range = ®_rule->freq_range;
1484 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1485 /* Check if auto calculation requested */
1486 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
1487 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1489 /* If we get a reg_rule we can assume that at least 5Mhz fit */
1490 if (!cfg80211_does_bw_fit_range(freq_range,
1491 MHZ_TO_KHZ(chan->center_freq),
1493 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1494 if (!cfg80211_does_bw_fit_range(freq_range,
1495 MHZ_TO_KHZ(chan->center_freq),
1497 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1499 if (max_bandwidth_khz < MHZ_TO_KHZ(10))
1500 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1501 if (max_bandwidth_khz < MHZ_TO_KHZ(20))
1502 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1503 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
1504 bw_flags |= IEEE80211_CHAN_NO_HT40;
1505 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
1506 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
1507 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
1508 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
1513 * Note that right now we assume the desired channel bandwidth
1514 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1515 * per channel, the primary and the extension channel).
1517 static void handle_channel(struct wiphy *wiphy,
1518 enum nl80211_reg_initiator initiator,
1519 struct ieee80211_channel *chan)
1521 u32 flags, bw_flags = 0;
1522 const struct ieee80211_reg_rule *reg_rule = NULL;
1523 const struct ieee80211_power_rule *power_rule = NULL;
1524 struct wiphy *request_wiphy = NULL;
1525 struct regulatory_request *lr = get_last_request();
1526 const struct ieee80211_regdomain *regd;
1528 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
1530 flags = chan->orig_flags;
1532 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1533 if (IS_ERR(reg_rule)) {
1535 * We will disable all channels that do not match our
1536 * received regulatory rule unless the hint is coming
1537 * from a Country IE and the Country IE had no information
1538 * about a band. The IEEE 802.11 spec allows for an AP
1539 * to send only a subset of the regulatory rules allowed,
1540 * so an AP in the US that only supports 2.4 GHz may only send
1541 * a country IE with information for the 2.4 GHz band
1542 * while 5 GHz is still supported.
1544 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1545 PTR_ERR(reg_rule) == -ERANGE)
1548 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1549 request_wiphy && request_wiphy == wiphy &&
1550 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
1551 pr_debug("Disabling freq %d MHz for good\n",
1553 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1554 chan->flags = chan->orig_flags;
1556 pr_debug("Disabling freq %d MHz\n",
1558 chan->flags |= IEEE80211_CHAN_DISABLED;
1563 regd = reg_get_regdomain(wiphy);
1565 power_rule = ®_rule->power_rule;
1566 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
1568 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1569 request_wiphy && request_wiphy == wiphy &&
1570 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
1572 * This guarantees the driver's requested regulatory domain
1573 * will always be used as a base for further regulatory
1576 chan->flags = chan->orig_flags =
1577 map_regdom_flags(reg_rule->flags) | bw_flags;
1578 chan->max_antenna_gain = chan->orig_mag =
1579 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1580 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
1581 (int) MBM_TO_DBM(power_rule->max_eirp);
1583 if (chan->flags & IEEE80211_CHAN_RADAR) {
1584 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1585 if (reg_rule->dfs_cac_ms)
1586 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1592 chan->dfs_state = NL80211_DFS_USABLE;
1593 chan->dfs_state_entered = jiffies;
1595 chan->beacon_found = false;
1596 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1597 chan->max_antenna_gain =
1598 min_t(int, chan->orig_mag,
1599 MBI_TO_DBI(power_rule->max_antenna_gain));
1600 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1602 if (chan->flags & IEEE80211_CHAN_RADAR) {
1603 if (reg_rule->dfs_cac_ms)
1604 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1606 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1609 if (chan->orig_mpwr) {
1611 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1612 * will always follow the passed country IE power settings.
1614 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1615 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
1616 chan->max_power = chan->max_reg_power;
1618 chan->max_power = min(chan->orig_mpwr,
1619 chan->max_reg_power);
1621 chan->max_power = chan->max_reg_power;
1624 static void handle_band(struct wiphy *wiphy,
1625 enum nl80211_reg_initiator initiator,
1626 struct ieee80211_supported_band *sband)
1633 for (i = 0; i < sband->n_channels; i++)
1634 handle_channel(wiphy, initiator, &sband->channels[i]);
1637 static bool reg_request_cell_base(struct regulatory_request *request)
1639 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1641 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
1644 bool reg_last_request_cell_base(void)
1646 return reg_request_cell_base(get_last_request());
1649 #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
1650 /* Core specific check */
1651 static enum reg_request_treatment
1652 reg_ignore_cell_hint(struct regulatory_request *pending_request)
1654 struct regulatory_request *lr = get_last_request();
1656 if (!reg_num_devs_support_basehint)
1657 return REG_REQ_IGNORE;
1659 if (reg_request_cell_base(lr) &&
1660 !regdom_changes(pending_request->alpha2))
1661 return REG_REQ_ALREADY_SET;
1666 /* Device specific check */
1667 static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1669 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
1672 static enum reg_request_treatment
1673 reg_ignore_cell_hint(struct regulatory_request *pending_request)
1675 return REG_REQ_IGNORE;
1678 static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1684 static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1686 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1687 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
1692 static bool ignore_reg_update(struct wiphy *wiphy,
1693 enum nl80211_reg_initiator initiator)
1695 struct regulatory_request *lr = get_last_request();
1697 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1701 pr_debug("Ignoring regulatory request set by %s since last_request is not set\n",
1702 reg_initiator_name(initiator));
1706 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1707 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
1708 pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n",
1709 reg_initiator_name(initiator));
1714 * wiphy->regd will be set once the device has its own
1715 * desired regulatory domain set
1717 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
1718 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1719 !is_world_regdom(lr->alpha2)) {
1720 pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n",
1721 reg_initiator_name(initiator));
1725 if (reg_request_cell_base(lr))
1726 return reg_dev_ignore_cell_hint(wiphy);
1731 static bool reg_is_world_roaming(struct wiphy *wiphy)
1733 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1734 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1735 struct regulatory_request *lr = get_last_request();
1737 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1740 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1741 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
1747 static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
1748 struct reg_beacon *reg_beacon)
1750 struct ieee80211_supported_band *sband;
1751 struct ieee80211_channel *chan;
1752 bool channel_changed = false;
1753 struct ieee80211_channel chan_before;
1755 sband = wiphy->bands[reg_beacon->chan.band];
1756 chan = &sband->channels[chan_idx];
1758 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1761 if (chan->beacon_found)
1764 chan->beacon_found = true;
1766 if (!reg_is_world_roaming(wiphy))
1769 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
1772 chan_before.center_freq = chan->center_freq;
1773 chan_before.flags = chan->flags;
1775 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1776 chan->flags &= ~IEEE80211_CHAN_NO_IR;
1777 channel_changed = true;
1780 if (channel_changed)
1781 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1785 * Called when a scan on a wiphy finds a beacon on
1788 static void wiphy_update_new_beacon(struct wiphy *wiphy,
1789 struct reg_beacon *reg_beacon)
1792 struct ieee80211_supported_band *sband;
1794 if (!wiphy->bands[reg_beacon->chan.band])
1797 sband = wiphy->bands[reg_beacon->chan.band];
1799 for (i = 0; i < sband->n_channels; i++)
1800 handle_reg_beacon(wiphy, i, reg_beacon);
1804 * Called upon reg changes or a new wiphy is added
1806 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1809 struct ieee80211_supported_band *sband;
1810 struct reg_beacon *reg_beacon;
1812 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
1813 if (!wiphy->bands[reg_beacon->chan.band])
1815 sband = wiphy->bands[reg_beacon->chan.band];
1816 for (i = 0; i < sband->n_channels; i++)
1817 handle_reg_beacon(wiphy, i, reg_beacon);
1821 /* Reap the advantages of previously found beacons */
1822 static void reg_process_beacons(struct wiphy *wiphy)
1825 * Means we are just firing up cfg80211, so no beacons would
1826 * have been processed yet.
1830 wiphy_update_beacon_reg(wiphy);
1833 static bool is_ht40_allowed(struct ieee80211_channel *chan)
1837 if (chan->flags & IEEE80211_CHAN_DISABLED)
1839 /* This would happen when regulatory rules disallow HT40 completely */
1840 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1845 static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1846 struct ieee80211_channel *channel)
1848 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
1849 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1850 const struct ieee80211_regdomain *regd;
1854 if (!is_ht40_allowed(channel)) {
1855 channel->flags |= IEEE80211_CHAN_NO_HT40;
1860 * We need to ensure the extension channels exist to
1861 * be able to use HT40- or HT40+, this finds them (or not)
1863 for (i = 0; i < sband->n_channels; i++) {
1864 struct ieee80211_channel *c = &sband->channels[i];
1866 if (c->center_freq == (channel->center_freq - 20))
1868 if (c->center_freq == (channel->center_freq + 20))
1873 regd = get_wiphy_regdom(wiphy);
1875 const struct ieee80211_reg_rule *reg_rule =
1876 freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq),
1877 regd, MHZ_TO_KHZ(20));
1879 if (!IS_ERR(reg_rule))
1880 flags = reg_rule->flags;
1884 * Please note that this assumes target bandwidth is 20 MHz,
1885 * if that ever changes we also need to change the below logic
1886 * to include that as well.
1888 if (!is_ht40_allowed(channel_before) ||
1889 flags & NL80211_RRF_NO_HT40MINUS)
1890 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1892 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1894 if (!is_ht40_allowed(channel_after) ||
1895 flags & NL80211_RRF_NO_HT40PLUS)
1896 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1898 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1901 static void reg_process_ht_flags_band(struct wiphy *wiphy,
1902 struct ieee80211_supported_band *sband)
1909 for (i = 0; i < sband->n_channels; i++)
1910 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
1913 static void reg_process_ht_flags(struct wiphy *wiphy)
1915 enum nl80211_band band;
1920 for (band = 0; band < NUM_NL80211_BANDS; band++)
1921 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
1924 static void reg_call_notifier(struct wiphy *wiphy,
1925 struct regulatory_request *request)
1927 if (wiphy->reg_notifier)
1928 wiphy->reg_notifier(wiphy, request);
1931 static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev)
1933 struct cfg80211_chan_def chandef;
1934 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1935 enum nl80211_iftype iftype;
1938 iftype = wdev->iftype;
1940 /* make sure the interface is active */
1941 if (!wdev->netdev || !netif_running(wdev->netdev))
1942 goto wdev_inactive_unlock;
1945 case NL80211_IFTYPE_AP:
1946 case NL80211_IFTYPE_P2P_GO:
1947 if (!wdev->beacon_interval)
1948 goto wdev_inactive_unlock;
1949 chandef = wdev->chandef;
1951 case NL80211_IFTYPE_ADHOC:
1952 if (!wdev->ssid_len)
1953 goto wdev_inactive_unlock;
1954 chandef = wdev->chandef;
1956 case NL80211_IFTYPE_STATION:
1957 case NL80211_IFTYPE_P2P_CLIENT:
1958 if (!wdev->current_bss ||
1959 !wdev->current_bss->pub.channel)
1960 goto wdev_inactive_unlock;
1962 if (!rdev->ops->get_channel ||
1963 rdev_get_channel(rdev, wdev, &chandef))
1964 cfg80211_chandef_create(&chandef,
1965 wdev->current_bss->pub.channel,
1966 NL80211_CHAN_NO_HT);
1968 case NL80211_IFTYPE_MONITOR:
1969 case NL80211_IFTYPE_AP_VLAN:
1970 case NL80211_IFTYPE_P2P_DEVICE:
1971 /* no enforcement required */
1974 /* others not implemented for now */
1982 case NL80211_IFTYPE_AP:
1983 case NL80211_IFTYPE_P2P_GO:
1984 case NL80211_IFTYPE_ADHOC:
1985 return cfg80211_reg_can_beacon_relax(wiphy, &chandef, iftype);
1986 case NL80211_IFTYPE_STATION:
1987 case NL80211_IFTYPE_P2P_CLIENT:
1988 return cfg80211_chandef_usable(wiphy, &chandef,
1989 IEEE80211_CHAN_DISABLED);
1996 wdev_inactive_unlock:
2001 static void reg_leave_invalid_chans(struct wiphy *wiphy)
2003 struct wireless_dev *wdev;
2004 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2008 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
2009 if (!reg_wdev_chan_valid(wiphy, wdev))
2010 cfg80211_leave(rdev, wdev);
2013 static void reg_check_chans_work(struct work_struct *work)
2015 struct cfg80211_registered_device *rdev;
2017 pr_debug("Verifying active interfaces after reg change\n");
2020 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2021 if (!(rdev->wiphy.regulatory_flags &
2022 REGULATORY_IGNORE_STALE_KICKOFF))
2023 reg_leave_invalid_chans(&rdev->wiphy);
2028 static void reg_check_channels(void)
2031 * Give usermode a chance to do something nicer (move to another
2032 * channel, orderly disconnection), before forcing a disconnection.
2034 mod_delayed_work(system_power_efficient_wq,
2036 msecs_to_jiffies(REG_ENFORCE_GRACE_MS));
2039 static void wiphy_update_regulatory(struct wiphy *wiphy,
2040 enum nl80211_reg_initiator initiator)
2042 enum nl80211_band band;
2043 struct regulatory_request *lr = get_last_request();
2045 if (ignore_reg_update(wiphy, initiator)) {
2047 * Regulatory updates set by CORE are ignored for custom
2048 * regulatory cards. Let us notify the changes to the driver,
2049 * as some drivers used this to restore its orig_* reg domain.
2051 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
2052 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
2053 reg_call_notifier(wiphy, lr);
2057 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
2059 for (band = 0; band < NUM_NL80211_BANDS; band++)
2060 handle_band(wiphy, initiator, wiphy->bands[band]);
2062 reg_process_beacons(wiphy);
2063 reg_process_ht_flags(wiphy);
2064 reg_call_notifier(wiphy, lr);
2067 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
2069 struct cfg80211_registered_device *rdev;
2070 struct wiphy *wiphy;
2074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2075 wiphy = &rdev->wiphy;
2076 wiphy_update_regulatory(wiphy, initiator);
2079 reg_check_channels();
2082 static void handle_channel_custom(struct wiphy *wiphy,
2083 struct ieee80211_channel *chan,
2084 const struct ieee80211_regdomain *regd)
2087 const struct ieee80211_reg_rule *reg_rule = NULL;
2088 const struct ieee80211_power_rule *power_rule = NULL;
2091 for (bw = MHZ_TO_KHZ(20); bw >= MHZ_TO_KHZ(5); bw = bw / 2) {
2092 reg_rule = freq_reg_info_regd(MHZ_TO_KHZ(chan->center_freq),
2094 if (!IS_ERR(reg_rule))
2098 if (IS_ERR(reg_rule)) {
2099 pr_debug("Disabling freq %d MHz as custom regd has no rule that fits it\n",
2101 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
2102 chan->flags |= IEEE80211_CHAN_DISABLED;
2104 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
2105 chan->flags = chan->orig_flags;
2110 power_rule = ®_rule->power_rule;
2111 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
2113 chan->dfs_state_entered = jiffies;
2114 chan->dfs_state = NL80211_DFS_USABLE;
2116 chan->beacon_found = false;
2118 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2119 chan->flags = chan->orig_flags | bw_flags |
2120 map_regdom_flags(reg_rule->flags);
2122 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
2124 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
2125 chan->max_reg_power = chan->max_power =
2126 (int) MBM_TO_DBM(power_rule->max_eirp);
2128 if (chan->flags & IEEE80211_CHAN_RADAR) {
2129 if (reg_rule->dfs_cac_ms)
2130 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
2132 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
2135 chan->max_power = chan->max_reg_power;
2138 static void handle_band_custom(struct wiphy *wiphy,
2139 struct ieee80211_supported_band *sband,
2140 const struct ieee80211_regdomain *regd)
2147 for (i = 0; i < sband->n_channels; i++)
2148 handle_channel_custom(wiphy, &sband->channels[i], regd);
2151 /* Used by drivers prior to wiphy registration */
2152 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
2153 const struct ieee80211_regdomain *regd)
2155 enum nl80211_band band;
2156 unsigned int bands_set = 0;
2158 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
2159 "wiphy should have REGULATORY_CUSTOM_REG\n");
2160 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
2162 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2163 if (!wiphy->bands[band])
2165 handle_band_custom(wiphy, wiphy->bands[band], regd);
2170 * no point in calling this if it won't have any effect
2171 * on your device's supported bands.
2173 WARN_ON(!bands_set);
2175 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
2177 static void reg_set_request_processed(void)
2179 bool need_more_processing = false;
2180 struct regulatory_request *lr = get_last_request();
2182 lr->processed = true;
2184 spin_lock(®_requests_lock);
2185 if (!list_empty(®_requests_list))
2186 need_more_processing = true;
2187 spin_unlock(®_requests_lock);
2189 cancel_crda_timeout();
2191 if (need_more_processing)
2192 schedule_work(®_work);
2196 * reg_process_hint_core - process core regulatory requests
2197 * @pending_request: a pending core regulatory request
2199 * The wireless subsystem can use this function to process
2200 * a regulatory request issued by the regulatory core.
2202 static enum reg_request_treatment
2203 reg_process_hint_core(struct regulatory_request *core_request)
2205 if (reg_query_database(core_request)) {
2206 core_request->intersect = false;
2207 core_request->processed = false;
2208 reg_update_last_request(core_request);
2212 return REG_REQ_IGNORE;
2215 static enum reg_request_treatment
2216 __reg_process_hint_user(struct regulatory_request *user_request)
2218 struct regulatory_request *lr = get_last_request();
2220 if (reg_request_cell_base(user_request))
2221 return reg_ignore_cell_hint(user_request);
2223 if (reg_request_cell_base(lr))
2224 return REG_REQ_IGNORE;
2226 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
2227 return REG_REQ_INTERSECT;
2229 * If the user knows better the user should set the regdom
2230 * to their country before the IE is picked up
2232 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
2234 return REG_REQ_IGNORE;
2236 * Process user requests only after previous user/driver/core
2237 * requests have been processed
2239 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
2240 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
2241 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
2242 regdom_changes(lr->alpha2))
2243 return REG_REQ_IGNORE;
2245 if (!regdom_changes(user_request->alpha2))
2246 return REG_REQ_ALREADY_SET;
2252 * reg_process_hint_user - process user regulatory requests
2253 * @user_request: a pending user regulatory request
2255 * The wireless subsystem can use this function to process
2256 * a regulatory request initiated by userspace.
2258 static enum reg_request_treatment
2259 reg_process_hint_user(struct regulatory_request *user_request)
2261 enum reg_request_treatment treatment;
2263 treatment = __reg_process_hint_user(user_request);
2264 if (treatment == REG_REQ_IGNORE ||
2265 treatment == REG_REQ_ALREADY_SET)
2266 return REG_REQ_IGNORE;
2268 user_request->intersect = treatment == REG_REQ_INTERSECT;
2269 user_request->processed = false;
2271 if (reg_query_database(user_request)) {
2272 reg_update_last_request(user_request);
2273 user_alpha2[0] = user_request->alpha2[0];
2274 user_alpha2[1] = user_request->alpha2[1];
2278 return REG_REQ_IGNORE;
2281 static enum reg_request_treatment
2282 __reg_process_hint_driver(struct regulatory_request *driver_request)
2284 struct regulatory_request *lr = get_last_request();
2286 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
2287 if (regdom_changes(driver_request->alpha2))
2289 return REG_REQ_ALREADY_SET;
2293 * This would happen if you unplug and plug your card
2294 * back in or if you add a new device for which the previously
2295 * loaded card also agrees on the regulatory domain.
2297 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
2298 !regdom_changes(driver_request->alpha2))
2299 return REG_REQ_ALREADY_SET;
2301 return REG_REQ_INTERSECT;
2305 * reg_process_hint_driver - process driver regulatory requests
2306 * @driver_request: a pending driver regulatory request
2308 * The wireless subsystem can use this function to process
2309 * a regulatory request issued by an 802.11 driver.
2311 * Returns one of the different reg request treatment values.
2313 static enum reg_request_treatment
2314 reg_process_hint_driver(struct wiphy *wiphy,
2315 struct regulatory_request *driver_request)
2317 const struct ieee80211_regdomain *regd, *tmp;
2318 enum reg_request_treatment treatment;
2320 treatment = __reg_process_hint_driver(driver_request);
2322 switch (treatment) {
2325 case REG_REQ_IGNORE:
2326 return REG_REQ_IGNORE;
2327 case REG_REQ_INTERSECT:
2328 case REG_REQ_ALREADY_SET:
2329 regd = reg_copy_regd(get_cfg80211_regdom());
2331 return REG_REQ_IGNORE;
2333 tmp = get_wiphy_regdom(wiphy);
2334 rcu_assign_pointer(wiphy->regd, regd);
2335 rcu_free_regdom(tmp);
2339 driver_request->intersect = treatment == REG_REQ_INTERSECT;
2340 driver_request->processed = false;
2343 * Since CRDA will not be called in this case as we already
2344 * have applied the requested regulatory domain before we just
2345 * inform userspace we have processed the request
2347 if (treatment == REG_REQ_ALREADY_SET) {
2348 nl80211_send_reg_change_event(driver_request);
2349 reg_update_last_request(driver_request);
2350 reg_set_request_processed();
2351 return REG_REQ_ALREADY_SET;
2354 if (reg_query_database(driver_request)) {
2355 reg_update_last_request(driver_request);
2359 return REG_REQ_IGNORE;
2362 static enum reg_request_treatment
2363 __reg_process_hint_country_ie(struct wiphy *wiphy,
2364 struct regulatory_request *country_ie_request)
2366 struct wiphy *last_wiphy = NULL;
2367 struct regulatory_request *lr = get_last_request();
2369 if (reg_request_cell_base(lr)) {
2370 /* Trust a Cell base station over the AP's country IE */
2371 if (regdom_changes(country_ie_request->alpha2))
2372 return REG_REQ_IGNORE;
2373 return REG_REQ_ALREADY_SET;
2375 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
2376 return REG_REQ_IGNORE;
2379 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
2382 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
2385 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
2387 if (last_wiphy != wiphy) {
2389 * Two cards with two APs claiming different
2390 * Country IE alpha2s. We could
2391 * intersect them, but that seems unlikely
2392 * to be correct. Reject second one for now.
2394 if (regdom_changes(country_ie_request->alpha2))
2395 return REG_REQ_IGNORE;
2396 return REG_REQ_ALREADY_SET;
2399 if (regdom_changes(country_ie_request->alpha2))
2401 return REG_REQ_ALREADY_SET;
2405 * reg_process_hint_country_ie - process regulatory requests from country IEs
2406 * @country_ie_request: a regulatory request from a country IE
2408 * The wireless subsystem can use this function to process
2409 * a regulatory request issued by a country Information Element.
2411 * Returns one of the different reg request treatment values.
2413 static enum reg_request_treatment
2414 reg_process_hint_country_ie(struct wiphy *wiphy,
2415 struct regulatory_request *country_ie_request)
2417 enum reg_request_treatment treatment;
2419 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
2421 switch (treatment) {
2424 case REG_REQ_IGNORE:
2425 return REG_REQ_IGNORE;
2426 case REG_REQ_ALREADY_SET:
2427 reg_free_request(country_ie_request);
2428 return REG_REQ_ALREADY_SET;
2429 case REG_REQ_INTERSECT:
2431 * This doesn't happen yet, not sure we
2432 * ever want to support it for this case.
2434 WARN_ONCE(1, "Unexpected intersection for country IEs");
2435 return REG_REQ_IGNORE;
2438 country_ie_request->intersect = false;
2439 country_ie_request->processed = false;
2441 if (reg_query_database(country_ie_request)) {
2442 reg_update_last_request(country_ie_request);
2446 return REG_REQ_IGNORE;
2449 bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2)
2451 const struct ieee80211_regdomain *wiphy1_regd = NULL;
2452 const struct ieee80211_regdomain *wiphy2_regd = NULL;
2453 const struct ieee80211_regdomain *cfg80211_regd = NULL;
2454 bool dfs_domain_same;
2458 cfg80211_regd = rcu_dereference(cfg80211_regdomain);
2459 wiphy1_regd = rcu_dereference(wiphy1->regd);
2461 wiphy1_regd = cfg80211_regd;
2463 wiphy2_regd = rcu_dereference(wiphy2->regd);
2465 wiphy2_regd = cfg80211_regd;
2467 dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region;
2471 return dfs_domain_same;
2474 static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan,
2475 struct ieee80211_channel *src_chan)
2477 if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) ||
2478 !(src_chan->flags & IEEE80211_CHAN_RADAR))
2481 if (dst_chan->flags & IEEE80211_CHAN_DISABLED ||
2482 src_chan->flags & IEEE80211_CHAN_DISABLED)
2485 if (src_chan->center_freq == dst_chan->center_freq &&
2486 dst_chan->dfs_state == NL80211_DFS_USABLE) {
2487 dst_chan->dfs_state = src_chan->dfs_state;
2488 dst_chan->dfs_state_entered = src_chan->dfs_state_entered;
2492 static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy,
2493 struct wiphy *src_wiphy)
2495 struct ieee80211_supported_band *src_sband, *dst_sband;
2496 struct ieee80211_channel *src_chan, *dst_chan;
2499 if (!reg_dfs_domain_same(dst_wiphy, src_wiphy))
2502 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2503 dst_sband = dst_wiphy->bands[band];
2504 src_sband = src_wiphy->bands[band];
2505 if (!dst_sband || !src_sband)
2508 for (i = 0; i < dst_sband->n_channels; i++) {
2509 dst_chan = &dst_sband->channels[i];
2510 for (j = 0; j < src_sband->n_channels; j++) {
2511 src_chan = &src_sband->channels[j];
2512 reg_copy_dfs_chan_state(dst_chan, src_chan);
2518 static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy)
2520 struct cfg80211_registered_device *rdev;
2524 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2525 if (wiphy == &rdev->wiphy)
2527 wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy);
2531 /* This processes *all* regulatory hints */
2532 static void reg_process_hint(struct regulatory_request *reg_request)
2534 struct wiphy *wiphy = NULL;
2535 enum reg_request_treatment treatment;
2537 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
2538 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
2540 switch (reg_request->initiator) {
2541 case NL80211_REGDOM_SET_BY_CORE:
2542 treatment = reg_process_hint_core(reg_request);
2544 case NL80211_REGDOM_SET_BY_USER:
2545 treatment = reg_process_hint_user(reg_request);
2547 case NL80211_REGDOM_SET_BY_DRIVER:
2550 treatment = reg_process_hint_driver(wiphy, reg_request);
2552 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2555 treatment = reg_process_hint_country_ie(wiphy, reg_request);
2558 WARN(1, "invalid initiator %d\n", reg_request->initiator);
2562 if (treatment == REG_REQ_IGNORE)
2565 WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET,
2566 "unexpected treatment value %d\n", treatment);
2568 /* This is required so that the orig_* parameters are saved.
2569 * NOTE: treatment must be set for any case that reaches here!
2571 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
2572 wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
2573 wiphy_update_regulatory(wiphy, reg_request->initiator);
2574 wiphy_all_share_dfs_chan_state(wiphy);
2575 reg_check_channels();
2581 reg_free_request(reg_request);
2584 static bool reg_only_self_managed_wiphys(void)
2586 struct cfg80211_registered_device *rdev;
2587 struct wiphy *wiphy;
2588 bool self_managed_found = false;
2592 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2593 wiphy = &rdev->wiphy;
2594 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2595 self_managed_found = true;
2600 /* make sure at least one self-managed wiphy exists */
2601 return self_managed_found;
2605 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
2606 * Regulatory hints come on a first come first serve basis and we
2607 * must process each one atomically.
2609 static void reg_process_pending_hints(void)
2611 struct regulatory_request *reg_request, *lr;
2613 lr = get_last_request();
2615 /* When last_request->processed becomes true this will be rescheduled */
2616 if (lr && !lr->processed) {
2617 reg_process_hint(lr);
2621 spin_lock(®_requests_lock);
2623 if (list_empty(®_requests_list)) {
2624 spin_unlock(®_requests_lock);
2628 reg_request = list_first_entry(®_requests_list,
2629 struct regulatory_request,
2631 list_del_init(®_request->list);
2633 spin_unlock(®_requests_lock);
2635 if (reg_only_self_managed_wiphys()) {
2636 reg_free_request(reg_request);
2640 reg_process_hint(reg_request);
2642 lr = get_last_request();
2644 spin_lock(®_requests_lock);
2645 if (!list_empty(®_requests_list) && lr && lr->processed)
2646 schedule_work(®_work);
2647 spin_unlock(®_requests_lock);
2650 /* Processes beacon hints -- this has nothing to do with country IEs */
2651 static void reg_process_pending_beacon_hints(void)
2653 struct cfg80211_registered_device *rdev;
2654 struct reg_beacon *pending_beacon, *tmp;
2656 /* This goes through the _pending_ beacon list */
2657 spin_lock_bh(®_pending_beacons_lock);
2659 list_for_each_entry_safe(pending_beacon, tmp,
2660 ®_pending_beacons, list) {
2661 list_del_init(&pending_beacon->list);
2663 /* Applies the beacon hint to current wiphys */
2664 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2665 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
2667 /* Remembers the beacon hint for new wiphys or reg changes */
2668 list_add_tail(&pending_beacon->list, ®_beacon_list);
2671 spin_unlock_bh(®_pending_beacons_lock);
2674 static void reg_process_self_managed_hints(void)
2676 struct cfg80211_registered_device *rdev;
2677 struct wiphy *wiphy;
2678 const struct ieee80211_regdomain *tmp;
2679 const struct ieee80211_regdomain *regd;
2680 enum nl80211_band band;
2681 struct regulatory_request request = {};
2683 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2684 wiphy = &rdev->wiphy;
2686 spin_lock(®_requests_lock);
2687 regd = rdev->requested_regd;
2688 rdev->requested_regd = NULL;
2689 spin_unlock(®_requests_lock);
2694 tmp = get_wiphy_regdom(wiphy);
2695 rcu_assign_pointer(wiphy->regd, regd);
2696 rcu_free_regdom(tmp);
2698 for (band = 0; band < NUM_NL80211_BANDS; band++)
2699 handle_band_custom(wiphy, wiphy->bands[band], regd);
2701 reg_process_ht_flags(wiphy);
2703 request.wiphy_idx = get_wiphy_idx(wiphy);
2704 request.alpha2[0] = regd->alpha2[0];
2705 request.alpha2[1] = regd->alpha2[1];
2706 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
2708 nl80211_send_wiphy_reg_change_event(&request);
2711 reg_check_channels();
2714 static void reg_todo(struct work_struct *work)
2717 reg_process_pending_hints();
2718 reg_process_pending_beacon_hints();
2719 reg_process_self_managed_hints();
2723 static void queue_regulatory_request(struct regulatory_request *request)
2725 request->alpha2[0] = toupper(request->alpha2[0]);
2726 request->alpha2[1] = toupper(request->alpha2[1]);
2728 spin_lock(®_requests_lock);
2729 list_add_tail(&request->list, ®_requests_list);
2730 spin_unlock(®_requests_lock);
2732 schedule_work(®_work);
2736 * Core regulatory hint -- happens during cfg80211_init()
2737 * and when we restore regulatory settings.
2739 static int regulatory_hint_core(const char *alpha2)
2741 struct regulatory_request *request;
2743 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2747 request->alpha2[0] = alpha2[0];
2748 request->alpha2[1] = alpha2[1];
2749 request->initiator = NL80211_REGDOM_SET_BY_CORE;
2751 queue_regulatory_request(request);
2757 int regulatory_hint_user(const char *alpha2,
2758 enum nl80211_user_reg_hint_type user_reg_hint_type)
2760 struct regulatory_request *request;
2762 if (WARN_ON(!alpha2))
2765 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2769 request->wiphy_idx = WIPHY_IDX_INVALID;
2770 request->alpha2[0] = alpha2[0];
2771 request->alpha2[1] = alpha2[1];
2772 request->initiator = NL80211_REGDOM_SET_BY_USER;
2773 request->user_reg_hint_type = user_reg_hint_type;
2775 /* Allow calling CRDA again */
2776 reset_crda_timeouts();
2778 queue_regulatory_request(request);
2783 int regulatory_hint_indoor(bool is_indoor, u32 portid)
2785 spin_lock(®_indoor_lock);
2787 /* It is possible that more than one user space process is trying to
2788 * configure the indoor setting. To handle such cases, clear the indoor
2789 * setting in case that some process does not think that the device
2790 * is operating in an indoor environment. In addition, if a user space
2791 * process indicates that it is controlling the indoor setting, save its
2792 * portid, i.e., make it the owner.
2794 reg_is_indoor = is_indoor;
2795 if (reg_is_indoor) {
2796 if (!reg_is_indoor_portid)
2797 reg_is_indoor_portid = portid;
2799 reg_is_indoor_portid = 0;
2802 spin_unlock(®_indoor_lock);
2805 reg_check_channels();
2810 void regulatory_netlink_notify(u32 portid)
2812 spin_lock(®_indoor_lock);
2814 if (reg_is_indoor_portid != portid) {
2815 spin_unlock(®_indoor_lock);
2819 reg_is_indoor = false;
2820 reg_is_indoor_portid = 0;
2822 spin_unlock(®_indoor_lock);
2824 reg_check_channels();
2828 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2830 struct regulatory_request *request;
2832 if (WARN_ON(!alpha2 || !wiphy))
2835 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
2837 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2841 request->wiphy_idx = get_wiphy_idx(wiphy);
2843 request->alpha2[0] = alpha2[0];
2844 request->alpha2[1] = alpha2[1];
2845 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
2847 /* Allow calling CRDA again */
2848 reset_crda_timeouts();
2850 queue_regulatory_request(request);
2854 EXPORT_SYMBOL(regulatory_hint);
2856 void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band,
2857 const u8 *country_ie, u8 country_ie_len)
2860 enum environment_cap env = ENVIRON_ANY;
2861 struct regulatory_request *request = NULL, *lr;
2863 /* IE len must be evenly divisible by 2 */
2864 if (country_ie_len & 0x01)
2867 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2870 request = kzalloc(sizeof(*request), GFP_KERNEL);
2874 alpha2[0] = country_ie[0];
2875 alpha2[1] = country_ie[1];
2877 if (country_ie[2] == 'I')
2878 env = ENVIRON_INDOOR;
2879 else if (country_ie[2] == 'O')
2880 env = ENVIRON_OUTDOOR;
2883 lr = get_last_request();
2889 * We will run this only upon a successful connection on cfg80211.
2890 * We leave conflict resolution to the workqueue, where can hold
2893 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2894 lr->wiphy_idx != WIPHY_IDX_INVALID)
2897 request->wiphy_idx = get_wiphy_idx(wiphy);
2898 request->alpha2[0] = alpha2[0];
2899 request->alpha2[1] = alpha2[1];
2900 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
2901 request->country_ie_env = env;
2903 /* Allow calling CRDA again */
2904 reset_crda_timeouts();
2906 queue_regulatory_request(request);
2913 static void restore_alpha2(char *alpha2, bool reset_user)
2915 /* indicates there is no alpha2 to consider for restoration */
2919 /* The user setting has precedence over the module parameter */
2920 if (is_user_regdom_saved()) {
2921 /* Unless we're asked to ignore it and reset it */
2923 pr_debug("Restoring regulatory settings including user preference\n");
2924 user_alpha2[0] = '9';
2925 user_alpha2[1] = '7';
2928 * If we're ignoring user settings, we still need to
2929 * check the module parameter to ensure we put things
2930 * back as they were for a full restore.
2932 if (!is_world_regdom(ieee80211_regdom)) {
2933 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2934 ieee80211_regdom[0], ieee80211_regdom[1]);
2935 alpha2[0] = ieee80211_regdom[0];
2936 alpha2[1] = ieee80211_regdom[1];
2939 pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n",
2940 user_alpha2[0], user_alpha2[1]);
2941 alpha2[0] = user_alpha2[0];
2942 alpha2[1] = user_alpha2[1];
2944 } else if (!is_world_regdom(ieee80211_regdom)) {
2945 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2946 ieee80211_regdom[0], ieee80211_regdom[1]);
2947 alpha2[0] = ieee80211_regdom[0];
2948 alpha2[1] = ieee80211_regdom[1];
2950 pr_debug("Restoring regulatory settings\n");
2953 static void restore_custom_reg_settings(struct wiphy *wiphy)
2955 struct ieee80211_supported_band *sband;
2956 enum nl80211_band band;
2957 struct ieee80211_channel *chan;
2960 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2961 sband = wiphy->bands[band];
2964 for (i = 0; i < sband->n_channels; i++) {
2965 chan = &sband->channels[i];
2966 chan->flags = chan->orig_flags;
2967 chan->max_antenna_gain = chan->orig_mag;
2968 chan->max_power = chan->orig_mpwr;
2969 chan->beacon_found = false;
2975 * Restoring regulatory settings involves ingoring any
2976 * possibly stale country IE information and user regulatory
2977 * settings if so desired, this includes any beacon hints
2978 * learned as we could have traveled outside to another country
2979 * after disconnection. To restore regulatory settings we do
2980 * exactly what we did at bootup:
2982 * - send a core regulatory hint
2983 * - send a user regulatory hint if applicable
2985 * Device drivers that send a regulatory hint for a specific country
2986 * keep their own regulatory domain on wiphy->regd so that does does
2987 * not need to be remembered.
2989 static void restore_regulatory_settings(bool reset_user)
2992 char world_alpha2[2];
2993 struct reg_beacon *reg_beacon, *btmp;
2994 LIST_HEAD(tmp_reg_req_list);
2995 struct cfg80211_registered_device *rdev;
3000 * Clear the indoor setting in case that it is not controlled by user
3001 * space, as otherwise there is no guarantee that the device is still
3002 * operating in an indoor environment.
3004 spin_lock(®_indoor_lock);
3005 if (reg_is_indoor && !reg_is_indoor_portid) {
3006 reg_is_indoor = false;
3007 reg_check_channels();
3009 spin_unlock(®_indoor_lock);
3011 reset_regdomains(true, &world_regdom);
3012 restore_alpha2(alpha2, reset_user);
3015 * If there's any pending requests we simply
3016 * stash them to a temporary pending queue and
3017 * add then after we've restored regulatory
3020 spin_lock(®_requests_lock);
3021 list_splice_tail_init(®_requests_list, &tmp_reg_req_list);
3022 spin_unlock(®_requests_lock);
3024 /* Clear beacon hints */
3025 spin_lock_bh(®_pending_beacons_lock);
3026 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) {
3027 list_del(®_beacon->list);
3030 spin_unlock_bh(®_pending_beacons_lock);
3032 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) {
3033 list_del(®_beacon->list);
3037 /* First restore to the basic regulatory settings */
3038 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
3039 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
3041 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3042 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
3044 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
3045 restore_custom_reg_settings(&rdev->wiphy);
3048 regulatory_hint_core(world_alpha2);
3051 * This restores the ieee80211_regdom module parameter
3052 * preference or the last user requested regulatory
3053 * settings, user regulatory settings takes precedence.
3055 if (is_an_alpha2(alpha2))
3056 regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER);
3058 spin_lock(®_requests_lock);
3059 list_splice_tail_init(&tmp_reg_req_list, ®_requests_list);
3060 spin_unlock(®_requests_lock);
3062 pr_debug("Kicking the queue\n");
3064 schedule_work(®_work);
3067 void regulatory_hint_disconnect(void)
3069 pr_debug("All devices are disconnected, going to restore regulatory settings\n");
3070 restore_regulatory_settings(false);
3073 static bool freq_is_chan_12_13_14(u16 freq)
3075 if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) ||
3076 freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) ||
3077 freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ))
3082 static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
3084 struct reg_beacon *pending_beacon;
3086 list_for_each_entry(pending_beacon, ®_pending_beacons, list)
3087 if (beacon_chan->center_freq ==
3088 pending_beacon->chan.center_freq)
3093 int regulatory_hint_found_beacon(struct wiphy *wiphy,
3094 struct ieee80211_channel *beacon_chan,
3097 struct reg_beacon *reg_beacon;
3100 if (beacon_chan->beacon_found ||
3101 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
3102 (beacon_chan->band == NL80211_BAND_2GHZ &&
3103 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
3106 spin_lock_bh(®_pending_beacons_lock);
3107 processing = pending_reg_beacon(beacon_chan);
3108 spin_unlock_bh(®_pending_beacons_lock);
3113 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
3117 pr_debug("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
3118 beacon_chan->center_freq,
3119 ieee80211_frequency_to_channel(beacon_chan->center_freq),
3122 memcpy(®_beacon->chan, beacon_chan,
3123 sizeof(struct ieee80211_channel));
3126 * Since we can be called from BH or and non-BH context
3127 * we must use spin_lock_bh()
3129 spin_lock_bh(®_pending_beacons_lock);
3130 list_add_tail(®_beacon->list, ®_pending_beacons);
3131 spin_unlock_bh(®_pending_beacons_lock);
3133 schedule_work(®_work);
3138 static void print_rd_rules(const struct ieee80211_regdomain *rd)
3141 const struct ieee80211_reg_rule *reg_rule = NULL;
3142 const struct ieee80211_freq_range *freq_range = NULL;
3143 const struct ieee80211_power_rule *power_rule = NULL;
3144 char bw[32], cac_time[32];
3146 pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
3148 for (i = 0; i < rd->n_reg_rules; i++) {
3149 reg_rule = &rd->reg_rules[i];
3150 freq_range = ®_rule->freq_range;
3151 power_rule = ®_rule->power_rule;
3153 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
3154 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
3155 freq_range->max_bandwidth_khz,
3156 reg_get_max_bandwidth(rd, reg_rule));
3158 snprintf(bw, sizeof(bw), "%d KHz",
3159 freq_range->max_bandwidth_khz);
3161 if (reg_rule->flags & NL80211_RRF_DFS)
3162 scnprintf(cac_time, sizeof(cac_time), "%u s",
3163 reg_rule->dfs_cac_ms/1000);
3165 scnprintf(cac_time, sizeof(cac_time), "N/A");
3169 * There may not be documentation for max antenna gain
3170 * in certain regions
3172 if (power_rule->max_antenna_gain)
3173 pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
3174 freq_range->start_freq_khz,
3175 freq_range->end_freq_khz,
3177 power_rule->max_antenna_gain,
3178 power_rule->max_eirp,
3181 pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
3182 freq_range->start_freq_khz,
3183 freq_range->end_freq_khz,
3185 power_rule->max_eirp,
3190 bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
3192 switch (dfs_region) {
3193 case NL80211_DFS_UNSET:
3194 case NL80211_DFS_FCC:
3195 case NL80211_DFS_ETSI:
3196 case NL80211_DFS_JP:
3199 pr_debug("Ignoring uknown DFS master region: %d\n", dfs_region);
3204 static void print_regdomain(const struct ieee80211_regdomain *rd)
3206 struct regulatory_request *lr = get_last_request();
3208 if (is_intersected_alpha2(rd->alpha2)) {
3209 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
3210 struct cfg80211_registered_device *rdev;
3211 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
3213 pr_debug("Current regulatory domain updated by AP to: %c%c\n",
3214 rdev->country_ie_alpha2[0],
3215 rdev->country_ie_alpha2[1]);
3217 pr_debug("Current regulatory domain intersected:\n");
3219 pr_debug("Current regulatory domain intersected:\n");
3220 } else if (is_world_regdom(rd->alpha2)) {
3221 pr_debug("World regulatory domain updated:\n");
3223 if (is_unknown_alpha2(rd->alpha2))
3224 pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n");
3226 if (reg_request_cell_base(lr))
3227 pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n",
3228 rd->alpha2[0], rd->alpha2[1]);
3230 pr_debug("Regulatory domain changed to country: %c%c\n",
3231 rd->alpha2[0], rd->alpha2[1]);
3235 pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
3239 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
3241 pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
3245 static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
3247 if (!is_world_regdom(rd->alpha2))
3249 update_world_regdomain(rd);
3253 static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
3254 struct regulatory_request *user_request)
3256 const struct ieee80211_regdomain *intersected_rd = NULL;
3258 if (!regdom_changes(rd->alpha2))
3261 if (!is_valid_rd(rd)) {
3262 pr_err("Invalid regulatory domain detected: %c%c\n",
3263 rd->alpha2[0], rd->alpha2[1]);
3264 print_regdomain_info(rd);
3268 if (!user_request->intersect) {
3269 reset_regdomains(false, rd);
3273 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
3274 if (!intersected_rd)
3279 reset_regdomains(false, intersected_rd);
3284 static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
3285 struct regulatory_request *driver_request)
3287 const struct ieee80211_regdomain *regd;
3288 const struct ieee80211_regdomain *intersected_rd = NULL;
3289 const struct ieee80211_regdomain *tmp;
3290 struct wiphy *request_wiphy;
3292 if (is_world_regdom(rd->alpha2))
3295 if (!regdom_changes(rd->alpha2))
3298 if (!is_valid_rd(rd)) {
3299 pr_err("Invalid regulatory domain detected: %c%c\n",
3300 rd->alpha2[0], rd->alpha2[1]);
3301 print_regdomain_info(rd);
3305 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
3309 if (!driver_request->intersect) {
3310 if (request_wiphy->regd)
3313 regd = reg_copy_regd(rd);
3315 return PTR_ERR(regd);
3317 rcu_assign_pointer(request_wiphy->regd, regd);
3318 reset_regdomains(false, rd);
3322 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
3323 if (!intersected_rd)
3327 * We can trash what CRDA provided now.
3328 * However if a driver requested this specific regulatory
3329 * domain we keep it for its private use
3331 tmp = get_wiphy_regdom(request_wiphy);
3332 rcu_assign_pointer(request_wiphy->regd, rd);
3333 rcu_free_regdom(tmp);
3337 reset_regdomains(false, intersected_rd);
3342 static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
3343 struct regulatory_request *country_ie_request)
3345 struct wiphy *request_wiphy;
3347 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
3348 !is_unknown_alpha2(rd->alpha2))
3352 * Lets only bother proceeding on the same alpha2 if the current
3353 * rd is non static (it means CRDA was present and was used last)
3354 * and the pending request came in from a country IE
3357 if (!is_valid_rd(rd)) {
3358 pr_err("Invalid regulatory domain detected: %c%c\n",
3359 rd->alpha2[0], rd->alpha2[1]);
3360 print_regdomain_info(rd);
3364 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
3368 if (country_ie_request->intersect)
3371 reset_regdomains(false, rd);
3376 * Use this call to set the current regulatory domain. Conflicts with
3377 * multiple drivers can be ironed out later. Caller must've already
3378 * kmalloc'd the rd structure.
3380 int set_regdom(const struct ieee80211_regdomain *rd,
3381 enum ieee80211_regd_source regd_src)
3383 struct regulatory_request *lr;
3384 bool user_reset = false;
3387 if (!reg_is_valid_request(rd->alpha2)) {
3392 if (regd_src == REGD_SOURCE_CRDA)
3393 reset_crda_timeouts();
3395 lr = get_last_request();
3397 /* Note that this doesn't update the wiphys, this is done below */
3398 switch (lr->initiator) {
3399 case NL80211_REGDOM_SET_BY_CORE:
3400 r = reg_set_rd_core(rd);
3402 case NL80211_REGDOM_SET_BY_USER:
3403 r = reg_set_rd_user(rd, lr);
3406 case NL80211_REGDOM_SET_BY_DRIVER:
3407 r = reg_set_rd_driver(rd, lr);
3409 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
3410 r = reg_set_rd_country_ie(rd, lr);
3413 WARN(1, "invalid initiator %d\n", lr->initiator);
3421 reg_set_request_processed();
3424 /* Back to world regulatory in case of errors */
3425 restore_regulatory_settings(user_reset);
3432 /* This would make this whole thing pointless */
3433 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
3436 /* update all wiphys now with the new established regulatory domain */
3437 update_all_wiphy_regulatory(lr->initiator);
3439 print_regdomain(get_cfg80211_regdom());
3441 nl80211_send_reg_change_event(lr);
3443 reg_set_request_processed();
3448 static int __regulatory_set_wiphy_regd(struct wiphy *wiphy,
3449 struct ieee80211_regdomain *rd)
3451 const struct ieee80211_regdomain *regd;
3452 const struct ieee80211_regdomain *prev_regd;
3453 struct cfg80211_registered_device *rdev;
3455 if (WARN_ON(!wiphy || !rd))
3458 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED),
3459 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n"))
3462 if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) {
3463 print_regdomain_info(rd);
3467 regd = reg_copy_regd(rd);
3469 return PTR_ERR(regd);
3471 rdev = wiphy_to_rdev(wiphy);
3473 spin_lock(®_requests_lock);
3474 prev_regd = rdev->requested_regd;
3475 rdev->requested_regd = regd;
3476 spin_unlock(®_requests_lock);
3482 int regulatory_set_wiphy_regd(struct wiphy *wiphy,
3483 struct ieee80211_regdomain *rd)
3485 int ret = __regulatory_set_wiphy_regd(wiphy, rd);
3490 schedule_work(®_work);
3493 EXPORT_SYMBOL(regulatory_set_wiphy_regd);
3495 int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy,
3496 struct ieee80211_regdomain *rd)
3502 ret = __regulatory_set_wiphy_regd(wiphy, rd);
3506 /* process the request immediately */
3507 reg_process_self_managed_hints();
3510 EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl);
3512 void wiphy_regulatory_register(struct wiphy *wiphy)
3514 struct regulatory_request *lr;
3516 /* self-managed devices ignore external hints */
3517 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
3518 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
3519 REGULATORY_COUNTRY_IE_IGNORE;
3521 if (!reg_dev_ignore_cell_hint(wiphy))
3522 reg_num_devs_support_basehint++;
3524 lr = get_last_request();
3525 wiphy_update_regulatory(wiphy, lr->initiator);
3526 wiphy_all_share_dfs_chan_state(wiphy);
3529 void wiphy_regulatory_deregister(struct wiphy *wiphy)
3531 struct wiphy *request_wiphy = NULL;
3532 struct regulatory_request *lr;
3534 lr = get_last_request();
3536 if (!reg_dev_ignore_cell_hint(wiphy))
3537 reg_num_devs_support_basehint--;
3539 rcu_free_regdom(get_wiphy_regdom(wiphy));
3540 RCU_INIT_POINTER(wiphy->regd, NULL);
3543 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
3545 if (!request_wiphy || request_wiphy != wiphy)
3548 lr->wiphy_idx = WIPHY_IDX_INVALID;
3549 lr->country_ie_env = ENVIRON_ANY;
3553 * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
3554 * UNII band definitions
3556 int cfg80211_get_unii(int freq)
3559 if (freq >= 5150 && freq <= 5250)
3563 if (freq > 5250 && freq <= 5350)
3567 if (freq > 5350 && freq <= 5470)
3571 if (freq > 5470 && freq <= 5725)
3575 if (freq > 5725 && freq <= 5825)
3581 bool regulatory_indoor_allowed(void)
3583 return reg_is_indoor;
3586 bool regulatory_pre_cac_allowed(struct wiphy *wiphy)
3588 const struct ieee80211_regdomain *regd = NULL;
3589 const struct ieee80211_regdomain *wiphy_regd = NULL;
3590 bool pre_cac_allowed = false;
3594 regd = rcu_dereference(cfg80211_regdomain);
3595 wiphy_regd = rcu_dereference(wiphy->regd);
3597 if (regd->dfs_region == NL80211_DFS_ETSI)
3598 pre_cac_allowed = true;
3602 return pre_cac_allowed;
3605 if (regd->dfs_region == wiphy_regd->dfs_region &&
3606 wiphy_regd->dfs_region == NL80211_DFS_ETSI)
3607 pre_cac_allowed = true;
3611 return pre_cac_allowed;
3614 void regulatory_propagate_dfs_state(struct wiphy *wiphy,
3615 struct cfg80211_chan_def *chandef,
3616 enum nl80211_dfs_state dfs_state,
3617 enum nl80211_radar_event event)
3619 struct cfg80211_registered_device *rdev;
3623 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3626 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3627 if (wiphy == &rdev->wiphy)
3630 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
3633 if (!ieee80211_get_channel(&rdev->wiphy,
3634 chandef->chan->center_freq))
3637 cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state);
3639 if (event == NL80211_RADAR_DETECTED ||
3640 event == NL80211_RADAR_CAC_FINISHED)
3641 cfg80211_sched_dfs_chan_update(rdev);
3643 nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL);
3647 int __init regulatory_init(void)
3651 err = load_builtin_regdb_keys();
3655 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
3656 if (IS_ERR(reg_pdev))
3657 return PTR_ERR(reg_pdev);
3659 spin_lock_init(®_requests_lock);
3660 spin_lock_init(®_pending_beacons_lock);
3661 spin_lock_init(®_indoor_lock);
3663 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
3665 user_alpha2[0] = '9';
3666 user_alpha2[1] = '7';
3668 /* We always try to get an update for the static regdomain */
3669 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
3671 if (err == -ENOMEM) {
3672 platform_device_unregister(reg_pdev);
3676 * N.B. kobject_uevent_env() can fail mainly for when we're out
3677 * memory which is handled and propagated appropriately above
3678 * but it can also fail during a netlink_broadcast() or during
3679 * early boot for call_usermodehelper(). For now treat these
3680 * errors as non-fatal.
3682 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
3686 * Finally, if the user set the module parameter treat it
3689 if (!is_world_regdom(ieee80211_regdom))
3690 regulatory_hint_user(ieee80211_regdom,
3691 NL80211_USER_REG_HINT_USER);
3696 void regulatory_exit(void)
3698 struct regulatory_request *reg_request, *tmp;
3699 struct reg_beacon *reg_beacon, *btmp;
3701 cancel_work_sync(®_work);
3702 cancel_crda_timeout_sync();
3703 cancel_delayed_work_sync(®_check_chans);
3705 /* Lock to suppress warnings */
3707 reset_regdomains(true, NULL);
3710 dev_set_uevent_suppress(®_pdev->dev, true);
3712 platform_device_unregister(reg_pdev);
3714 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) {
3715 list_del(®_beacon->list);
3719 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) {
3720 list_del(®_beacon->list);
3724 list_for_each_entry_safe(reg_request, tmp, ®_requests_list, list) {
3725 list_del(®_request->list);
3729 if (!IS_ERR_OR_NULL(regdb))
3732 free_regdb_keyring();