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/moduleparam.h>
57 #include <net/cfg80211.h>
65 * Grace period we give before making sure all current interfaces reside on
66 * channels allowed by the current regulatory domain.
68 #define REG_ENFORCE_GRACE_MS 60000
71 * enum reg_request_treatment - regulatory request treatment
73 * @REG_REQ_OK: continue processing the regulatory request
74 * @REG_REQ_IGNORE: ignore the regulatory request
75 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
76 * be intersected with the current one.
77 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
78 * regulatory settings, and no further processing is required.
80 enum reg_request_treatment {
87 static struct regulatory_request core_request_world = {
88 .initiator = NL80211_REGDOM_SET_BY_CORE,
93 .country_ie_env = ENVIRON_ANY,
97 * Receipt of information from last regulatory request,
98 * protected by RTNL (and can be accessed with RCU protection)
100 static struct regulatory_request __rcu *last_request =
101 (void __force __rcu *)&core_request_world;
103 /* To trigger userspace events */
104 static struct platform_device *reg_pdev;
107 * Central wireless core regulatory domains, we only need two,
108 * the current one and a world regulatory domain in case we have no
109 * information to give us an alpha2.
110 * (protected by RTNL, can be read under RCU)
112 const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
115 * Number of devices that registered to the core
116 * that support cellular base station regulatory hints
117 * (protected by RTNL)
119 static int reg_num_devs_support_basehint;
122 * State variable indicating if the platform on which the devices
123 * are attached is operating in an indoor environment. The state variable
124 * is relevant for all registered devices.
126 static bool reg_is_indoor;
127 static spinlock_t reg_indoor_lock;
129 /* Used to track the userspace process controlling the indoor setting */
130 static u32 reg_is_indoor_portid;
132 static void restore_regulatory_settings(bool reset_user);
134 static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
136 return rtnl_dereference(cfg80211_regdomain);
139 const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
141 return rtnl_dereference(wiphy->regd);
144 static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
146 switch (dfs_region) {
147 case NL80211_DFS_UNSET:
149 case NL80211_DFS_FCC:
151 case NL80211_DFS_ETSI:
159 enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
161 const struct ieee80211_regdomain *regd = NULL;
162 const struct ieee80211_regdomain *wiphy_regd = NULL;
164 regd = get_cfg80211_regdom();
168 wiphy_regd = get_wiphy_regdom(wiphy);
172 if (wiphy_regd->dfs_region == regd->dfs_region)
175 pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n",
176 dev_name(&wiphy->dev),
177 reg_dfs_region_str(wiphy_regd->dfs_region),
178 reg_dfs_region_str(regd->dfs_region));
181 return regd->dfs_region;
184 static void rcu_free_regdom(const struct ieee80211_regdomain *r)
188 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
191 static struct regulatory_request *get_last_request(void)
193 return rcu_dereference_rtnl(last_request);
196 /* Used to queue up regulatory hints */
197 static LIST_HEAD(reg_requests_list);
198 static spinlock_t reg_requests_lock;
200 /* Used to queue up beacon hints for review */
201 static LIST_HEAD(reg_pending_beacons);
202 static spinlock_t reg_pending_beacons_lock;
204 /* Used to keep track of processed beacon hints */
205 static LIST_HEAD(reg_beacon_list);
208 struct list_head list;
209 struct ieee80211_channel chan;
212 static void reg_check_chans_work(struct work_struct *work);
213 static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);
215 static void reg_todo(struct work_struct *work);
216 static DECLARE_WORK(reg_work, reg_todo);
218 /* We keep a static world regulatory domain in case of the absence of CRDA */
219 static const struct ieee80211_regdomain world_regdom = {
223 /* IEEE 802.11b/g, channels 1..11 */
224 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
225 /* IEEE 802.11b/g, channels 12..13. */
226 REG_RULE(2467-10, 2472+10, 20, 6, 20,
227 NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW),
228 /* IEEE 802.11 channel 14 - Only JP enables
229 * this and for 802.11b only */
230 REG_RULE(2484-10, 2484+10, 20, 6, 20,
232 NL80211_RRF_NO_OFDM),
233 /* IEEE 802.11a, channel 36..48 */
234 REG_RULE(5180-10, 5240+10, 80, 6, 20,
236 NL80211_RRF_AUTO_BW),
238 /* IEEE 802.11a, channel 52..64 - DFS required */
239 REG_RULE(5260-10, 5320+10, 80, 6, 20,
241 NL80211_RRF_AUTO_BW |
244 /* IEEE 802.11a, channel 100..144 - DFS required */
245 REG_RULE(5500-10, 5720+10, 160, 6, 20,
249 /* IEEE 802.11a, channel 149..165 */
250 REG_RULE(5745-10, 5825+10, 80, 6, 20,
253 /* IEEE 802.11ad (60GHz), channels 1..3 */
254 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
258 /* protected by RTNL */
259 static const struct ieee80211_regdomain *cfg80211_world_regdom =
262 static char *ieee80211_regdom = "00";
263 static char user_alpha2[2];
265 module_param(ieee80211_regdom, charp, 0444);
266 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
268 static void reg_free_request(struct regulatory_request *request)
270 if (request == &core_request_world)
273 if (request != get_last_request())
277 static void reg_free_last_request(void)
279 struct regulatory_request *lr = get_last_request();
281 if (lr != &core_request_world && lr)
282 kfree_rcu(lr, rcu_head);
285 static void reg_update_last_request(struct regulatory_request *request)
287 struct regulatory_request *lr;
289 lr = get_last_request();
293 reg_free_last_request();
294 rcu_assign_pointer(last_request, request);
297 static void reset_regdomains(bool full_reset,
298 const struct ieee80211_regdomain *new_regdom)
300 const struct ieee80211_regdomain *r;
304 r = get_cfg80211_regdom();
306 /* avoid freeing static information or freeing something twice */
307 if (r == cfg80211_world_regdom)
309 if (cfg80211_world_regdom == &world_regdom)
310 cfg80211_world_regdom = NULL;
311 if (r == &world_regdom)
315 rcu_free_regdom(cfg80211_world_regdom);
317 cfg80211_world_regdom = &world_regdom;
318 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
323 reg_update_last_request(&core_request_world);
327 * Dynamic world regulatory domain requested by the wireless
328 * core upon initialization
330 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
332 struct regulatory_request *lr;
334 lr = get_last_request();
338 reset_regdomains(false, rd);
340 cfg80211_world_regdom = rd;
343 bool is_world_regdom(const char *alpha2)
347 return alpha2[0] == '0' && alpha2[1] == '0';
350 static bool is_alpha2_set(const char *alpha2)
354 return alpha2[0] && alpha2[1];
357 static bool is_unknown_alpha2(const char *alpha2)
362 * Special case where regulatory domain was built by driver
363 * but a specific alpha2 cannot be determined
365 return alpha2[0] == '9' && alpha2[1] == '9';
368 static bool is_intersected_alpha2(const char *alpha2)
373 * Special case where regulatory domain is the
374 * result of an intersection between two regulatory domain
377 return alpha2[0] == '9' && alpha2[1] == '8';
380 static bool is_an_alpha2(const char *alpha2)
384 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
387 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
389 if (!alpha2_x || !alpha2_y)
391 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
394 static bool regdom_changes(const char *alpha2)
396 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
400 return !alpha2_equal(r->alpha2, alpha2);
404 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
405 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
406 * has ever been issued.
408 static bool is_user_regdom_saved(void)
410 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
413 /* This would indicate a mistake on the design */
414 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
415 "Unexpected user alpha2: %c%c\n",
416 user_alpha2[0], user_alpha2[1]))
422 static const struct ieee80211_regdomain *
423 reg_copy_regd(const struct ieee80211_regdomain *src_regd)
425 struct ieee80211_regdomain *regd;
430 sizeof(struct ieee80211_regdomain) +
431 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
433 regd = kzalloc(size_of_regd, GFP_KERNEL);
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 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
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_query_builtin(const char *alpha2)
480 const struct ieee80211_regdomain *regdom = NULL;
481 struct reg_regdb_apply_request *request;
484 for (i = 0; i < reg_regdb_size; i++) {
485 if (alpha2_equal(alpha2, reg_regdb[i]->alpha2)) {
486 regdom = reg_regdb[i];
494 request = kzalloc(sizeof(struct reg_regdb_apply_request), GFP_KERNEL);
498 request->regdom = reg_copy_regd(regdom);
499 if (IS_ERR_OR_NULL(request->regdom)) {
504 mutex_lock(®_regdb_apply_mutex);
505 list_add_tail(&request->list, ®_regdb_apply_list);
506 mutex_unlock(®_regdb_apply_mutex);
508 schedule_work(®_regdb_work);
513 /* Feel free to add any other sanity checks here */
514 static void reg_regdb_size_check(void)
516 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
517 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
520 static inline void reg_regdb_size_check(void) {}
521 static inline int reg_query_builtin(const char *alpha2)
525 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
527 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
528 /* Max number of consecutive attempts to communicate with CRDA */
529 #define REG_MAX_CRDA_TIMEOUTS 10
531 static u32 reg_crda_timeouts;
533 static void crda_timeout_work(struct work_struct *work);
534 static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work);
536 static void crda_timeout_work(struct work_struct *work)
538 pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
541 restore_regulatory_settings(true);
545 static void cancel_crda_timeout(void)
547 cancel_delayed_work(&crda_timeout);
550 static void cancel_crda_timeout_sync(void)
552 cancel_delayed_work_sync(&crda_timeout);
555 static void reset_crda_timeouts(void)
557 reg_crda_timeouts = 0;
561 * This lets us keep regulatory code which is updated on a regulatory
562 * basis in userspace.
564 static int call_crda(const char *alpha2)
567 char *env[] = { country, NULL };
570 snprintf(country, sizeof(country), "COUNTRY=%c%c",
571 alpha2[0], alpha2[1]);
573 if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) {
574 pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n");
578 if (!is_world_regdom((char *) alpha2))
579 pr_debug("Calling CRDA for country: %c%c\n",
580 alpha2[0], alpha2[1]);
582 pr_debug("Calling CRDA to update world regulatory domain\n");
584 ret = kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, env);
588 queue_delayed_work(system_power_efficient_wq,
589 &crda_timeout, msecs_to_jiffies(3142));
593 static inline void cancel_crda_timeout(void) {}
594 static inline void cancel_crda_timeout_sync(void) {}
595 static inline void reset_crda_timeouts(void) {}
596 static inline int call_crda(const char *alpha2)
600 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
602 static bool reg_query_database(struct regulatory_request *request)
604 /* query internal regulatory database (if it exists) */
605 if (reg_query_builtin(request->alpha2) == 0)
608 if (call_crda(request->alpha2) == 0)
614 bool reg_is_valid_request(const char *alpha2)
616 struct regulatory_request *lr = get_last_request();
618 if (!lr || lr->processed)
621 return alpha2_equal(lr->alpha2, alpha2);
624 static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
626 struct regulatory_request *lr = get_last_request();
629 * Follow the driver's regulatory domain, if present, unless a country
630 * IE has been processed or a user wants to help complaince further
632 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
633 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
635 return get_wiphy_regdom(wiphy);
637 return get_cfg80211_regdom();
641 reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
642 const struct ieee80211_reg_rule *rule)
644 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
645 const struct ieee80211_freq_range *freq_range_tmp;
646 const struct ieee80211_reg_rule *tmp;
647 u32 start_freq, end_freq, idx, no;
649 for (idx = 0; idx < rd->n_reg_rules; idx++)
650 if (rule == &rd->reg_rules[idx])
653 if (idx == rd->n_reg_rules)
660 tmp = &rd->reg_rules[--no];
661 freq_range_tmp = &tmp->freq_range;
663 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
666 freq_range = freq_range_tmp;
669 start_freq = freq_range->start_freq_khz;
672 freq_range = &rule->freq_range;
675 while (no < rd->n_reg_rules - 1) {
676 tmp = &rd->reg_rules[++no];
677 freq_range_tmp = &tmp->freq_range;
679 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
682 freq_range = freq_range_tmp;
685 end_freq = freq_range->end_freq_khz;
687 return end_freq - start_freq;
690 unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
691 const struct ieee80211_reg_rule *rule)
693 unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);
695 if (rule->flags & NL80211_RRF_NO_160MHZ)
696 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
697 if (rule->flags & NL80211_RRF_NO_80MHZ)
698 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));
701 * HT40+/HT40- limits are handled per-channel. Only limit BW if both
704 if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
705 rule->flags & NL80211_RRF_NO_HT40PLUS)
706 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));
711 /* Sanity check on a regulatory rule */
712 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
714 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
717 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
720 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
723 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
725 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
726 freq_range->max_bandwidth_khz > freq_diff)
732 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
734 const struct ieee80211_reg_rule *reg_rule = NULL;
737 if (!rd->n_reg_rules)
740 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
743 for (i = 0; i < rd->n_reg_rules; i++) {
744 reg_rule = &rd->reg_rules[i];
745 if (!is_valid_reg_rule(reg_rule))
753 * freq_in_rule_band - tells us if a frequency is in a frequency band
754 * @freq_range: frequency rule we want to query
755 * @freq_khz: frequency we are inquiring about
757 * This lets us know if a specific frequency rule is or is not relevant to
758 * a specific frequency's band. Bands are device specific and artificial
759 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
760 * however it is safe for now to assume that a frequency rule should not be
761 * part of a frequency's band if the start freq or end freq are off by more
762 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 20 GHz for the
764 * This resolution can be lowered and should be considered as we add
765 * regulatory rule support for other "bands".
767 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
770 #define ONE_GHZ_IN_KHZ 1000000
772 * From 802.11ad: directional multi-gigabit (DMG):
773 * Pertaining to operation in a frequency band containing a channel
774 * with the Channel starting frequency above 45 GHz.
776 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
777 20 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
778 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
780 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
783 #undef ONE_GHZ_IN_KHZ
787 * Later on we can perhaps use the more restrictive DFS
788 * region but we don't have information for that yet so
789 * for now simply disallow conflicts.
791 static enum nl80211_dfs_regions
792 reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
793 const enum nl80211_dfs_regions dfs_region2)
795 if (dfs_region1 != dfs_region2)
796 return NL80211_DFS_UNSET;
801 * Helper for regdom_intersect(), this does the real
802 * mathematical intersection fun
804 static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
805 const struct ieee80211_regdomain *rd2,
806 const struct ieee80211_reg_rule *rule1,
807 const struct ieee80211_reg_rule *rule2,
808 struct ieee80211_reg_rule *intersected_rule)
810 const struct ieee80211_freq_range *freq_range1, *freq_range2;
811 struct ieee80211_freq_range *freq_range;
812 const struct ieee80211_power_rule *power_rule1, *power_rule2;
813 struct ieee80211_power_rule *power_rule;
814 u32 freq_diff, max_bandwidth1, max_bandwidth2;
816 freq_range1 = &rule1->freq_range;
817 freq_range2 = &rule2->freq_range;
818 freq_range = &intersected_rule->freq_range;
820 power_rule1 = &rule1->power_rule;
821 power_rule2 = &rule2->power_rule;
822 power_rule = &intersected_rule->power_rule;
824 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
825 freq_range2->start_freq_khz);
826 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
827 freq_range2->end_freq_khz);
829 max_bandwidth1 = freq_range1->max_bandwidth_khz;
830 max_bandwidth2 = freq_range2->max_bandwidth_khz;
832 if (rule1->flags & NL80211_RRF_AUTO_BW)
833 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
834 if (rule2->flags & NL80211_RRF_AUTO_BW)
835 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
837 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
839 intersected_rule->flags = rule1->flags | rule2->flags;
842 * In case NL80211_RRF_AUTO_BW requested for both rules
843 * set AUTO_BW in intersected rule also. Next we will
844 * calculate BW correctly in handle_channel function.
845 * In other case remove AUTO_BW flag while we calculate
846 * maximum bandwidth correctly and auto calculation is
849 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
850 (rule2->flags & NL80211_RRF_AUTO_BW))
851 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
853 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
855 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
856 if (freq_range->max_bandwidth_khz > freq_diff)
857 freq_range->max_bandwidth_khz = freq_diff;
859 power_rule->max_eirp = min(power_rule1->max_eirp,
860 power_rule2->max_eirp);
861 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
862 power_rule2->max_antenna_gain);
864 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
867 if (!is_valid_reg_rule(intersected_rule))
873 /* check whether old rule contains new rule */
874 static bool rule_contains(struct ieee80211_reg_rule *r1,
875 struct ieee80211_reg_rule *r2)
877 /* for simplicity, currently consider only same flags */
878 if (r1->flags != r2->flags)
881 /* verify r1 is more restrictive */
882 if ((r1->power_rule.max_antenna_gain >
883 r2->power_rule.max_antenna_gain) ||
884 r1->power_rule.max_eirp > r2->power_rule.max_eirp)
887 /* make sure r2's range is contained within r1 */
888 if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
889 r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
892 /* and finally verify that r1.max_bw >= r2.max_bw */
893 if (r1->freq_range.max_bandwidth_khz <
894 r2->freq_range.max_bandwidth_khz)
900 /* add or extend current rules. do nothing if rule is already contained */
901 static void add_rule(struct ieee80211_reg_rule *rule,
902 struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
904 struct ieee80211_reg_rule *tmp_rule;
907 for (i = 0; i < *n_rules; i++) {
908 tmp_rule = ®_rules[i];
909 /* rule is already contained - do nothing */
910 if (rule_contains(tmp_rule, rule))
913 /* extend rule if possible */
914 if (rule_contains(rule, tmp_rule)) {
915 memcpy(tmp_rule, rule, sizeof(*rule));
920 memcpy(®_rules[*n_rules], rule, sizeof(*rule));
925 * regdom_intersect - do the intersection between two regulatory domains
926 * @rd1: first regulatory domain
927 * @rd2: second regulatory domain
929 * Use this function to get the intersection between two regulatory domains.
930 * Once completed we will mark the alpha2 for the rd as intersected, "98",
931 * as no one single alpha2 can represent this regulatory domain.
933 * Returns a pointer to the regulatory domain structure which will hold the
934 * resulting intersection of rules between rd1 and rd2. We will
935 * kzalloc() this structure for you.
937 static struct ieee80211_regdomain *
938 regdom_intersect(const struct ieee80211_regdomain *rd1,
939 const struct ieee80211_regdomain *rd2)
943 unsigned int num_rules = 0;
944 const struct ieee80211_reg_rule *rule1, *rule2;
945 struct ieee80211_reg_rule intersected_rule;
946 struct ieee80211_regdomain *rd;
952 * First we get a count of the rules we'll need, then we actually
953 * build them. This is to so we can malloc() and free() a
954 * regdomain once. The reason we use reg_rules_intersect() here
955 * is it will return -EINVAL if the rule computed makes no sense.
956 * All rules that do check out OK are valid.
959 for (x = 0; x < rd1->n_reg_rules; x++) {
960 rule1 = &rd1->reg_rules[x];
961 for (y = 0; y < rd2->n_reg_rules; y++) {
962 rule2 = &rd2->reg_rules[y];
963 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
972 size_of_regd = sizeof(struct ieee80211_regdomain) +
973 num_rules * sizeof(struct ieee80211_reg_rule);
975 rd = kzalloc(size_of_regd, GFP_KERNEL);
979 for (x = 0; x < rd1->n_reg_rules; x++) {
980 rule1 = &rd1->reg_rules[x];
981 for (y = 0; y < rd2->n_reg_rules; y++) {
982 rule2 = &rd2->reg_rules[y];
983 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
986 * No need to memset here the intersected rule here as
987 * we're not using the stack anymore
992 add_rule(&intersected_rule, rd->reg_rules,
999 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
1006 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1007 * want to just have the channel structure use these
1009 static u32 map_regdom_flags(u32 rd_flags)
1011 u32 channel_flags = 0;
1012 if (rd_flags & NL80211_RRF_NO_IR_ALL)
1013 channel_flags |= IEEE80211_CHAN_NO_IR;
1014 if (rd_flags & NL80211_RRF_DFS)
1015 channel_flags |= IEEE80211_CHAN_RADAR;
1016 if (rd_flags & NL80211_RRF_NO_OFDM)
1017 channel_flags |= IEEE80211_CHAN_NO_OFDM;
1018 if (rd_flags & NL80211_RRF_NO_OUTDOOR)
1019 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
1020 if (rd_flags & NL80211_RRF_IR_CONCURRENT)
1021 channel_flags |= IEEE80211_CHAN_IR_CONCURRENT;
1022 if (rd_flags & NL80211_RRF_NO_HT40MINUS)
1023 channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
1024 if (rd_flags & NL80211_RRF_NO_HT40PLUS)
1025 channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
1026 if (rd_flags & NL80211_RRF_NO_80MHZ)
1027 channel_flags |= IEEE80211_CHAN_NO_80MHZ;
1028 if (rd_flags & NL80211_RRF_NO_160MHZ)
1029 channel_flags |= IEEE80211_CHAN_NO_160MHZ;
1030 return channel_flags;
1033 static const struct ieee80211_reg_rule *
1034 freq_reg_info_regd(u32 center_freq,
1035 const struct ieee80211_regdomain *regd, u32 bw)
1038 bool band_rule_found = false;
1039 bool bw_fits = false;
1042 return ERR_PTR(-EINVAL);
1044 for (i = 0; i < regd->n_reg_rules; i++) {
1045 const struct ieee80211_reg_rule *rr;
1046 const struct ieee80211_freq_range *fr = NULL;
1048 rr = ®d->reg_rules[i];
1049 fr = &rr->freq_range;
1052 * We only need to know if one frequency rule was
1053 * was in center_freq's band, that's enough, so lets
1054 * not overwrite it once found
1056 if (!band_rule_found)
1057 band_rule_found = freq_in_rule_band(fr, center_freq);
1059 bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw);
1061 if (band_rule_found && bw_fits)
1065 if (!band_rule_found)
1066 return ERR_PTR(-ERANGE);
1068 return ERR_PTR(-EINVAL);
1071 static const struct ieee80211_reg_rule *
1072 __freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw)
1074 const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy);
1075 const struct ieee80211_reg_rule *reg_rule = NULL;
1078 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
1079 reg_rule = freq_reg_info_regd(center_freq, regd, bw);
1080 if (!IS_ERR(reg_rule))
1087 const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
1090 return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(20));
1092 EXPORT_SYMBOL(freq_reg_info);
1094 const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
1096 switch (initiator) {
1097 case NL80211_REGDOM_SET_BY_CORE:
1099 case NL80211_REGDOM_SET_BY_USER:
1101 case NL80211_REGDOM_SET_BY_DRIVER:
1103 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1104 return "country IE";
1110 EXPORT_SYMBOL(reg_initiator_name);
1112 static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd,
1113 const struct ieee80211_reg_rule *reg_rule,
1114 const struct ieee80211_channel *chan)
1116 const struct ieee80211_freq_range *freq_range = NULL;
1117 u32 max_bandwidth_khz, bw_flags = 0;
1119 freq_range = ®_rule->freq_range;
1121 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1122 /* Check if auto calculation requested */
1123 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
1124 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1126 /* If we get a reg_rule we can assume that at least 5Mhz fit */
1127 if (!cfg80211_does_bw_fit_range(freq_range,
1128 MHZ_TO_KHZ(chan->center_freq),
1130 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1131 if (!cfg80211_does_bw_fit_range(freq_range,
1132 MHZ_TO_KHZ(chan->center_freq),
1134 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1136 if (max_bandwidth_khz < MHZ_TO_KHZ(10))
1137 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1138 if (max_bandwidth_khz < MHZ_TO_KHZ(20))
1139 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1140 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
1141 bw_flags |= IEEE80211_CHAN_NO_HT40;
1142 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
1143 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
1144 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
1145 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
1150 * Note that right now we assume the desired channel bandwidth
1151 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1152 * per channel, the primary and the extension channel).
1154 static void handle_channel(struct wiphy *wiphy,
1155 enum nl80211_reg_initiator initiator,
1156 struct ieee80211_channel *chan)
1158 u32 flags, bw_flags = 0;
1159 const struct ieee80211_reg_rule *reg_rule = NULL;
1160 const struct ieee80211_power_rule *power_rule = NULL;
1161 struct wiphy *request_wiphy = NULL;
1162 struct regulatory_request *lr = get_last_request();
1163 const struct ieee80211_regdomain *regd;
1165 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
1167 flags = chan->orig_flags;
1169 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1170 if (IS_ERR(reg_rule)) {
1172 * We will disable all channels that do not match our
1173 * received regulatory rule unless the hint is coming
1174 * from a Country IE and the Country IE had no information
1175 * about a band. The IEEE 802.11 spec allows for an AP
1176 * to send only a subset of the regulatory rules allowed,
1177 * so an AP in the US that only supports 2.4 GHz may only send
1178 * a country IE with information for the 2.4 GHz band
1179 * while 5 GHz is still supported.
1181 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1182 PTR_ERR(reg_rule) == -ERANGE)
1185 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1186 request_wiphy && request_wiphy == wiphy &&
1187 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
1188 pr_debug("Disabling freq %d MHz for good\n",
1190 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1191 chan->flags = chan->orig_flags;
1193 pr_debug("Disabling freq %d MHz\n",
1195 chan->flags |= IEEE80211_CHAN_DISABLED;
1200 regd = reg_get_regdomain(wiphy);
1202 power_rule = ®_rule->power_rule;
1203 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
1205 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1206 request_wiphy && request_wiphy == wiphy &&
1207 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
1209 * This guarantees the driver's requested regulatory domain
1210 * will always be used as a base for further regulatory
1213 chan->flags = chan->orig_flags =
1214 map_regdom_flags(reg_rule->flags) | bw_flags;
1215 chan->max_antenna_gain = chan->orig_mag =
1216 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1217 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
1218 (int) MBM_TO_DBM(power_rule->max_eirp);
1220 if (chan->flags & IEEE80211_CHAN_RADAR) {
1221 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1222 if (reg_rule->dfs_cac_ms)
1223 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1229 chan->dfs_state = NL80211_DFS_USABLE;
1230 chan->dfs_state_entered = jiffies;
1232 chan->beacon_found = false;
1233 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1234 chan->max_antenna_gain =
1235 min_t(int, chan->orig_mag,
1236 MBI_TO_DBI(power_rule->max_antenna_gain));
1237 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1239 if (chan->flags & IEEE80211_CHAN_RADAR) {
1240 if (reg_rule->dfs_cac_ms)
1241 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1243 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1246 if (chan->orig_mpwr) {
1248 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1249 * will always follow the passed country IE power settings.
1251 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1252 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
1253 chan->max_power = chan->max_reg_power;
1255 chan->max_power = min(chan->orig_mpwr,
1256 chan->max_reg_power);
1258 chan->max_power = chan->max_reg_power;
1261 static void handle_band(struct wiphy *wiphy,
1262 enum nl80211_reg_initiator initiator,
1263 struct ieee80211_supported_band *sband)
1270 for (i = 0; i < sband->n_channels; i++)
1271 handle_channel(wiphy, initiator, &sband->channels[i]);
1274 static bool reg_request_cell_base(struct regulatory_request *request)
1276 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1278 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
1281 bool reg_last_request_cell_base(void)
1283 return reg_request_cell_base(get_last_request());
1286 #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
1287 /* Core specific check */
1288 static enum reg_request_treatment
1289 reg_ignore_cell_hint(struct regulatory_request *pending_request)
1291 struct regulatory_request *lr = get_last_request();
1293 if (!reg_num_devs_support_basehint)
1294 return REG_REQ_IGNORE;
1296 if (reg_request_cell_base(lr) &&
1297 !regdom_changes(pending_request->alpha2))
1298 return REG_REQ_ALREADY_SET;
1303 /* Device specific check */
1304 static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1306 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
1309 static enum reg_request_treatment
1310 reg_ignore_cell_hint(struct regulatory_request *pending_request)
1312 return REG_REQ_IGNORE;
1315 static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1321 static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1323 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1324 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
1329 static bool ignore_reg_update(struct wiphy *wiphy,
1330 enum nl80211_reg_initiator initiator)
1332 struct regulatory_request *lr = get_last_request();
1334 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1338 pr_debug("Ignoring regulatory request set by %s since last_request is not set\n",
1339 reg_initiator_name(initiator));
1343 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1344 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
1345 pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n",
1346 reg_initiator_name(initiator));
1351 * wiphy->regd will be set once the device has its own
1352 * desired regulatory domain set
1354 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
1355 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1356 !is_world_regdom(lr->alpha2)) {
1357 pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n",
1358 reg_initiator_name(initiator));
1362 if (reg_request_cell_base(lr))
1363 return reg_dev_ignore_cell_hint(wiphy);
1368 static bool reg_is_world_roaming(struct wiphy *wiphy)
1370 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1371 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1372 struct regulatory_request *lr = get_last_request();
1374 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1377 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1378 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
1384 static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
1385 struct reg_beacon *reg_beacon)
1387 struct ieee80211_supported_band *sband;
1388 struct ieee80211_channel *chan;
1389 bool channel_changed = false;
1390 struct ieee80211_channel chan_before;
1392 sband = wiphy->bands[reg_beacon->chan.band];
1393 chan = &sband->channels[chan_idx];
1395 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1398 if (chan->beacon_found)
1401 chan->beacon_found = true;
1403 if (!reg_is_world_roaming(wiphy))
1406 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
1409 chan_before.center_freq = chan->center_freq;
1410 chan_before.flags = chan->flags;
1412 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1413 chan->flags &= ~IEEE80211_CHAN_NO_IR;
1414 channel_changed = true;
1417 if (channel_changed)
1418 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1422 * Called when a scan on a wiphy finds a beacon on
1425 static void wiphy_update_new_beacon(struct wiphy *wiphy,
1426 struct reg_beacon *reg_beacon)
1429 struct ieee80211_supported_band *sband;
1431 if (!wiphy->bands[reg_beacon->chan.band])
1434 sband = wiphy->bands[reg_beacon->chan.band];
1436 for (i = 0; i < sband->n_channels; i++)
1437 handle_reg_beacon(wiphy, i, reg_beacon);
1441 * Called upon reg changes or a new wiphy is added
1443 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1446 struct ieee80211_supported_band *sband;
1447 struct reg_beacon *reg_beacon;
1449 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
1450 if (!wiphy->bands[reg_beacon->chan.band])
1452 sband = wiphy->bands[reg_beacon->chan.band];
1453 for (i = 0; i < sband->n_channels; i++)
1454 handle_reg_beacon(wiphy, i, reg_beacon);
1458 /* Reap the advantages of previously found beacons */
1459 static void reg_process_beacons(struct wiphy *wiphy)
1462 * Means we are just firing up cfg80211, so no beacons would
1463 * have been processed yet.
1467 wiphy_update_beacon_reg(wiphy);
1470 static bool is_ht40_allowed(struct ieee80211_channel *chan)
1474 if (chan->flags & IEEE80211_CHAN_DISABLED)
1476 /* This would happen when regulatory rules disallow HT40 completely */
1477 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1482 static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1483 struct ieee80211_channel *channel)
1485 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
1486 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1487 const struct ieee80211_regdomain *regd;
1491 if (!is_ht40_allowed(channel)) {
1492 channel->flags |= IEEE80211_CHAN_NO_HT40;
1497 * We need to ensure the extension channels exist to
1498 * be able to use HT40- or HT40+, this finds them (or not)
1500 for (i = 0; i < sband->n_channels; i++) {
1501 struct ieee80211_channel *c = &sband->channels[i];
1503 if (c->center_freq == (channel->center_freq - 20))
1505 if (c->center_freq == (channel->center_freq + 20))
1510 regd = get_wiphy_regdom(wiphy);
1512 const struct ieee80211_reg_rule *reg_rule =
1513 freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq),
1514 regd, MHZ_TO_KHZ(20));
1516 if (!IS_ERR(reg_rule))
1517 flags = reg_rule->flags;
1521 * Please note that this assumes target bandwidth is 20 MHz,
1522 * if that ever changes we also need to change the below logic
1523 * to include that as well.
1525 if (!is_ht40_allowed(channel_before) ||
1526 flags & NL80211_RRF_NO_HT40MINUS)
1527 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1529 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1531 if (!is_ht40_allowed(channel_after) ||
1532 flags & NL80211_RRF_NO_HT40PLUS)
1533 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1535 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1538 static void reg_process_ht_flags_band(struct wiphy *wiphy,
1539 struct ieee80211_supported_band *sband)
1546 for (i = 0; i < sband->n_channels; i++)
1547 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
1550 static void reg_process_ht_flags(struct wiphy *wiphy)
1552 enum nl80211_band band;
1557 for (band = 0; band < NUM_NL80211_BANDS; band++)
1558 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
1561 static void reg_call_notifier(struct wiphy *wiphy,
1562 struct regulatory_request *request)
1564 if (wiphy->reg_notifier)
1565 wiphy->reg_notifier(wiphy, request);
1568 static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev)
1570 struct cfg80211_chan_def chandef = {};
1571 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1572 enum nl80211_iftype iftype;
1575 iftype = wdev->iftype;
1577 /* make sure the interface is active */
1578 if (!wdev->netdev || !netif_running(wdev->netdev))
1579 goto wdev_inactive_unlock;
1582 case NL80211_IFTYPE_AP:
1583 case NL80211_IFTYPE_P2P_GO:
1584 if (!wdev->beacon_interval)
1585 goto wdev_inactive_unlock;
1586 chandef = wdev->chandef;
1588 case NL80211_IFTYPE_ADHOC:
1589 if (!wdev->ssid_len)
1590 goto wdev_inactive_unlock;
1591 chandef = wdev->chandef;
1593 case NL80211_IFTYPE_STATION:
1594 case NL80211_IFTYPE_P2P_CLIENT:
1595 if (!wdev->current_bss ||
1596 !wdev->current_bss->pub.channel)
1597 goto wdev_inactive_unlock;
1599 if (!rdev->ops->get_channel ||
1600 rdev_get_channel(rdev, wdev, &chandef))
1601 cfg80211_chandef_create(&chandef,
1602 wdev->current_bss->pub.channel,
1603 NL80211_CHAN_NO_HT);
1605 case NL80211_IFTYPE_MONITOR:
1606 case NL80211_IFTYPE_AP_VLAN:
1607 case NL80211_IFTYPE_P2P_DEVICE:
1608 /* no enforcement required */
1611 /* others not implemented for now */
1619 case NL80211_IFTYPE_AP:
1620 case NL80211_IFTYPE_P2P_GO:
1621 case NL80211_IFTYPE_ADHOC:
1622 return cfg80211_reg_can_beacon_relax(wiphy, &chandef, iftype);
1623 case NL80211_IFTYPE_STATION:
1624 case NL80211_IFTYPE_P2P_CLIENT:
1625 return cfg80211_chandef_usable(wiphy, &chandef,
1626 IEEE80211_CHAN_DISABLED);
1633 wdev_inactive_unlock:
1638 static void reg_leave_invalid_chans(struct wiphy *wiphy)
1640 struct wireless_dev *wdev;
1641 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1645 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
1646 if (!reg_wdev_chan_valid(wiphy, wdev))
1647 cfg80211_leave(rdev, wdev);
1650 static void reg_check_chans_work(struct work_struct *work)
1652 struct cfg80211_registered_device *rdev;
1654 pr_debug("Verifying active interfaces after reg change\n");
1657 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1658 if (!(rdev->wiphy.regulatory_flags &
1659 REGULATORY_IGNORE_STALE_KICKOFF))
1660 reg_leave_invalid_chans(&rdev->wiphy);
1665 static void reg_check_channels(void)
1668 * Give usermode a chance to do something nicer (move to another
1669 * channel, orderly disconnection), before forcing a disconnection.
1671 mod_delayed_work(system_power_efficient_wq,
1673 msecs_to_jiffies(REG_ENFORCE_GRACE_MS));
1676 static void wiphy_update_regulatory(struct wiphy *wiphy,
1677 enum nl80211_reg_initiator initiator)
1679 enum nl80211_band band;
1680 struct regulatory_request *lr = get_last_request();
1682 if (ignore_reg_update(wiphy, initiator)) {
1684 * Regulatory updates set by CORE are ignored for custom
1685 * regulatory cards. Let us notify the changes to the driver,
1686 * as some drivers used this to restore its orig_* reg domain.
1688 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1689 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
1690 reg_call_notifier(wiphy, lr);
1694 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
1696 for (band = 0; band < NUM_NL80211_BANDS; band++)
1697 handle_band(wiphy, initiator, wiphy->bands[band]);
1699 reg_process_beacons(wiphy);
1700 reg_process_ht_flags(wiphy);
1701 reg_call_notifier(wiphy, lr);
1704 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1706 struct cfg80211_registered_device *rdev;
1707 struct wiphy *wiphy;
1711 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1712 wiphy = &rdev->wiphy;
1713 wiphy_update_regulatory(wiphy, initiator);
1716 reg_check_channels();
1719 static void handle_channel_custom(struct wiphy *wiphy,
1720 struct ieee80211_channel *chan,
1721 const struct ieee80211_regdomain *regd,
1725 const struct ieee80211_reg_rule *reg_rule = NULL;
1726 const struct ieee80211_power_rule *power_rule = NULL;
1729 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
1730 reg_rule = freq_reg_info_regd(MHZ_TO_KHZ(chan->center_freq),
1732 if (!IS_ERR(reg_rule))
1736 if (IS_ERR_OR_NULL(reg_rule)) {
1737 pr_debug("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1739 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
1740 chan->flags |= IEEE80211_CHAN_DISABLED;
1742 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1743 chan->flags = chan->orig_flags;
1748 power_rule = ®_rule->power_rule;
1749 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
1751 chan->dfs_state_entered = jiffies;
1752 chan->dfs_state = NL80211_DFS_USABLE;
1754 chan->beacon_found = false;
1756 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1757 chan->flags = chan->orig_flags | bw_flags |
1758 map_regdom_flags(reg_rule->flags);
1760 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1762 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1763 chan->max_reg_power = chan->max_power =
1764 (int) MBM_TO_DBM(power_rule->max_eirp);
1766 if (chan->flags & IEEE80211_CHAN_RADAR) {
1767 if (reg_rule->dfs_cac_ms)
1768 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1770 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1773 chan->max_power = chan->max_reg_power;
1776 static void handle_band_custom(struct wiphy *wiphy,
1777 struct ieee80211_supported_band *sband,
1778 const struct ieee80211_regdomain *regd)
1786 * We currently assume that you always want at least 20 MHz,
1787 * otherwise channel 12 might get enabled if this rule is
1788 * compatible to US, which permits 2402 - 2472 MHz.
1790 for (i = 0; i < sband->n_channels; i++)
1791 handle_channel_custom(wiphy, &sband->channels[i], regd,
1795 /* Used by drivers prior to wiphy registration */
1796 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1797 const struct ieee80211_regdomain *regd)
1799 enum nl80211_band band;
1800 unsigned int bands_set = 0;
1802 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
1803 "wiphy should have REGULATORY_CUSTOM_REG\n");
1804 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
1806 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1807 if (!wiphy->bands[band])
1809 handle_band_custom(wiphy, wiphy->bands[band], regd);
1814 * no point in calling this if it won't have any effect
1815 * on your device's supported bands.
1817 WARN_ON(!bands_set);
1819 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1821 static void reg_set_request_processed(void)
1823 bool need_more_processing = false;
1824 struct regulatory_request *lr = get_last_request();
1826 lr->processed = true;
1828 spin_lock(®_requests_lock);
1829 if (!list_empty(®_requests_list))
1830 need_more_processing = true;
1831 spin_unlock(®_requests_lock);
1833 cancel_crda_timeout();
1835 if (need_more_processing)
1836 schedule_work(®_work);
1840 * reg_process_hint_core - process core regulatory requests
1841 * @pending_request: a pending core regulatory request
1843 * The wireless subsystem can use this function to process
1844 * a regulatory request issued by the regulatory core.
1846 static enum reg_request_treatment
1847 reg_process_hint_core(struct regulatory_request *core_request)
1849 if (reg_query_database(core_request)) {
1850 core_request->intersect = false;
1851 core_request->processed = false;
1852 reg_update_last_request(core_request);
1856 return REG_REQ_IGNORE;
1859 static enum reg_request_treatment
1860 __reg_process_hint_user(struct regulatory_request *user_request)
1862 struct regulatory_request *lr = get_last_request();
1864 if (reg_request_cell_base(user_request))
1865 return reg_ignore_cell_hint(user_request);
1867 if (reg_request_cell_base(lr))
1868 return REG_REQ_IGNORE;
1870 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1871 return REG_REQ_INTERSECT;
1873 * If the user knows better the user should set the regdom
1874 * to their country before the IE is picked up
1876 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
1878 return REG_REQ_IGNORE;
1880 * Process user requests only after previous user/driver/core
1881 * requests have been processed
1883 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
1884 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1885 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
1886 regdom_changes(lr->alpha2))
1887 return REG_REQ_IGNORE;
1889 if (!regdom_changes(user_request->alpha2))
1890 return REG_REQ_ALREADY_SET;
1896 * reg_process_hint_user - process user regulatory requests
1897 * @user_request: a pending user regulatory request
1899 * The wireless subsystem can use this function to process
1900 * a regulatory request initiated by userspace.
1902 static enum reg_request_treatment
1903 reg_process_hint_user(struct regulatory_request *user_request)
1905 enum reg_request_treatment treatment;
1907 treatment = __reg_process_hint_user(user_request);
1908 if (treatment == REG_REQ_IGNORE ||
1909 treatment == REG_REQ_ALREADY_SET)
1910 return REG_REQ_IGNORE;
1912 user_request->intersect = treatment == REG_REQ_INTERSECT;
1913 user_request->processed = false;
1915 if (reg_query_database(user_request)) {
1916 reg_update_last_request(user_request);
1917 user_alpha2[0] = user_request->alpha2[0];
1918 user_alpha2[1] = user_request->alpha2[1];
1922 return REG_REQ_IGNORE;
1925 static enum reg_request_treatment
1926 __reg_process_hint_driver(struct regulatory_request *driver_request)
1928 struct regulatory_request *lr = get_last_request();
1930 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
1931 if (regdom_changes(driver_request->alpha2))
1933 return REG_REQ_ALREADY_SET;
1937 * This would happen if you unplug and plug your card
1938 * back in or if you add a new device for which the previously
1939 * loaded card also agrees on the regulatory domain.
1941 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1942 !regdom_changes(driver_request->alpha2))
1943 return REG_REQ_ALREADY_SET;
1945 return REG_REQ_INTERSECT;
1949 * reg_process_hint_driver - process driver regulatory requests
1950 * @driver_request: a pending driver regulatory request
1952 * The wireless subsystem can use this function to process
1953 * a regulatory request issued by an 802.11 driver.
1955 * Returns one of the different reg request treatment values.
1957 static enum reg_request_treatment
1958 reg_process_hint_driver(struct wiphy *wiphy,
1959 struct regulatory_request *driver_request)
1961 const struct ieee80211_regdomain *regd, *tmp;
1962 enum reg_request_treatment treatment;
1964 treatment = __reg_process_hint_driver(driver_request);
1966 switch (treatment) {
1969 case REG_REQ_IGNORE:
1970 return REG_REQ_IGNORE;
1971 case REG_REQ_INTERSECT:
1972 case REG_REQ_ALREADY_SET:
1973 regd = reg_copy_regd(get_cfg80211_regdom());
1975 return REG_REQ_IGNORE;
1977 tmp = get_wiphy_regdom(wiphy);
1978 rcu_assign_pointer(wiphy->regd, regd);
1979 rcu_free_regdom(tmp);
1983 driver_request->intersect = treatment == REG_REQ_INTERSECT;
1984 driver_request->processed = false;
1987 * Since CRDA will not be called in this case as we already
1988 * have applied the requested regulatory domain before we just
1989 * inform userspace we have processed the request
1991 if (treatment == REG_REQ_ALREADY_SET) {
1992 nl80211_send_reg_change_event(driver_request);
1993 reg_update_last_request(driver_request);
1994 reg_set_request_processed();
1995 return REG_REQ_ALREADY_SET;
1998 if (reg_query_database(driver_request)) {
1999 reg_update_last_request(driver_request);
2003 return REG_REQ_IGNORE;
2006 static enum reg_request_treatment
2007 __reg_process_hint_country_ie(struct wiphy *wiphy,
2008 struct regulatory_request *country_ie_request)
2010 struct wiphy *last_wiphy = NULL;
2011 struct regulatory_request *lr = get_last_request();
2013 if (reg_request_cell_base(lr)) {
2014 /* Trust a Cell base station over the AP's country IE */
2015 if (regdom_changes(country_ie_request->alpha2))
2016 return REG_REQ_IGNORE;
2017 return REG_REQ_ALREADY_SET;
2019 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
2020 return REG_REQ_IGNORE;
2023 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
2026 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
2029 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
2031 if (last_wiphy != wiphy) {
2033 * Two cards with two APs claiming different
2034 * Country IE alpha2s. We could
2035 * intersect them, but that seems unlikely
2036 * to be correct. Reject second one for now.
2038 if (regdom_changes(country_ie_request->alpha2))
2039 return REG_REQ_IGNORE;
2040 return REG_REQ_ALREADY_SET;
2043 if (regdom_changes(country_ie_request->alpha2))
2045 return REG_REQ_ALREADY_SET;
2049 * reg_process_hint_country_ie - process regulatory requests from country IEs
2050 * @country_ie_request: a regulatory request from a country IE
2052 * The wireless subsystem can use this function to process
2053 * a regulatory request issued by a country Information Element.
2055 * Returns one of the different reg request treatment values.
2057 static enum reg_request_treatment
2058 reg_process_hint_country_ie(struct wiphy *wiphy,
2059 struct regulatory_request *country_ie_request)
2061 enum reg_request_treatment treatment;
2063 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
2065 switch (treatment) {
2068 case REG_REQ_IGNORE:
2069 return REG_REQ_IGNORE;
2070 case REG_REQ_ALREADY_SET:
2071 reg_free_request(country_ie_request);
2072 return REG_REQ_ALREADY_SET;
2073 case REG_REQ_INTERSECT:
2075 * This doesn't happen yet, not sure we
2076 * ever want to support it for this case.
2078 WARN_ONCE(1, "Unexpected intersection for country IEs");
2079 return REG_REQ_IGNORE;
2082 country_ie_request->intersect = false;
2083 country_ie_request->processed = false;
2085 if (reg_query_database(country_ie_request)) {
2086 reg_update_last_request(country_ie_request);
2090 return REG_REQ_IGNORE;
2093 bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2)
2095 const struct ieee80211_regdomain *wiphy1_regd = NULL;
2096 const struct ieee80211_regdomain *wiphy2_regd = NULL;
2097 const struct ieee80211_regdomain *cfg80211_regd = NULL;
2098 bool dfs_domain_same;
2102 cfg80211_regd = rcu_dereference(cfg80211_regdomain);
2103 wiphy1_regd = rcu_dereference(wiphy1->regd);
2105 wiphy1_regd = cfg80211_regd;
2107 wiphy2_regd = rcu_dereference(wiphy2->regd);
2109 wiphy2_regd = cfg80211_regd;
2111 dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region;
2115 return dfs_domain_same;
2118 static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan,
2119 struct ieee80211_channel *src_chan)
2121 if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) ||
2122 !(src_chan->flags & IEEE80211_CHAN_RADAR))
2125 if (dst_chan->flags & IEEE80211_CHAN_DISABLED ||
2126 src_chan->flags & IEEE80211_CHAN_DISABLED)
2129 if (src_chan->center_freq == dst_chan->center_freq &&
2130 dst_chan->dfs_state == NL80211_DFS_USABLE) {
2131 dst_chan->dfs_state = src_chan->dfs_state;
2132 dst_chan->dfs_state_entered = src_chan->dfs_state_entered;
2136 static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy,
2137 struct wiphy *src_wiphy)
2139 struct ieee80211_supported_band *src_sband, *dst_sband;
2140 struct ieee80211_channel *src_chan, *dst_chan;
2143 if (!reg_dfs_domain_same(dst_wiphy, src_wiphy))
2146 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2147 dst_sband = dst_wiphy->bands[band];
2148 src_sband = src_wiphy->bands[band];
2149 if (!dst_sband || !src_sband)
2152 for (i = 0; i < dst_sband->n_channels; i++) {
2153 dst_chan = &dst_sband->channels[i];
2154 for (j = 0; j < src_sband->n_channels; j++) {
2155 src_chan = &src_sband->channels[j];
2156 reg_copy_dfs_chan_state(dst_chan, src_chan);
2162 static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy)
2164 struct cfg80211_registered_device *rdev;
2168 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2169 if (wiphy == &rdev->wiphy)
2171 wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy);
2175 /* This processes *all* regulatory hints */
2176 static void reg_process_hint(struct regulatory_request *reg_request)
2178 struct wiphy *wiphy = NULL;
2179 enum reg_request_treatment treatment;
2180 enum nl80211_reg_initiator initiator = reg_request->initiator;
2182 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
2183 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
2185 switch (initiator) {
2186 case NL80211_REGDOM_SET_BY_CORE:
2187 treatment = reg_process_hint_core(reg_request);
2189 case NL80211_REGDOM_SET_BY_USER:
2190 treatment = reg_process_hint_user(reg_request);
2192 case NL80211_REGDOM_SET_BY_DRIVER:
2195 treatment = reg_process_hint_driver(wiphy, reg_request);
2197 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2200 treatment = reg_process_hint_country_ie(wiphy, reg_request);
2203 WARN(1, "invalid initiator %d\n", initiator);
2207 if (treatment == REG_REQ_IGNORE)
2210 WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET,
2211 "unexpected treatment value %d\n", treatment);
2213 /* This is required so that the orig_* parameters are saved.
2214 * NOTE: treatment must be set for any case that reaches here!
2216 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
2217 wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
2218 wiphy_update_regulatory(wiphy, initiator);
2219 wiphy_all_share_dfs_chan_state(wiphy);
2220 reg_check_channels();
2226 reg_free_request(reg_request);
2229 static bool reg_only_self_managed_wiphys(void)
2231 struct cfg80211_registered_device *rdev;
2232 struct wiphy *wiphy;
2233 bool self_managed_found = false;
2237 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2238 wiphy = &rdev->wiphy;
2239 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2240 self_managed_found = true;
2245 /* make sure at least one self-managed wiphy exists */
2246 return self_managed_found;
2250 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
2251 * Regulatory hints come on a first come first serve basis and we
2252 * must process each one atomically.
2254 static void reg_process_pending_hints(void)
2256 struct regulatory_request *reg_request, *lr;
2258 lr = get_last_request();
2260 /* When last_request->processed becomes true this will be rescheduled */
2261 if (lr && !lr->processed) {
2262 pr_debug("Pending regulatory request, waiting for it to be processed...\n");
2266 spin_lock(®_requests_lock);
2268 if (list_empty(®_requests_list)) {
2269 spin_unlock(®_requests_lock);
2273 reg_request = list_first_entry(®_requests_list,
2274 struct regulatory_request,
2276 list_del_init(®_request->list);
2278 spin_unlock(®_requests_lock);
2280 if (reg_only_self_managed_wiphys()) {
2281 reg_free_request(reg_request);
2285 reg_process_hint(reg_request);
2287 lr = get_last_request();
2289 spin_lock(®_requests_lock);
2290 if (!list_empty(®_requests_list) && lr && lr->processed)
2291 schedule_work(®_work);
2292 spin_unlock(®_requests_lock);
2295 /* Processes beacon hints -- this has nothing to do with country IEs */
2296 static void reg_process_pending_beacon_hints(void)
2298 struct cfg80211_registered_device *rdev;
2299 struct reg_beacon *pending_beacon, *tmp;
2301 /* This goes through the _pending_ beacon list */
2302 spin_lock_bh(®_pending_beacons_lock);
2304 list_for_each_entry_safe(pending_beacon, tmp,
2305 ®_pending_beacons, list) {
2306 list_del_init(&pending_beacon->list);
2308 /* Applies the beacon hint to current wiphys */
2309 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2310 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
2312 /* Remembers the beacon hint for new wiphys or reg changes */
2313 list_add_tail(&pending_beacon->list, ®_beacon_list);
2316 spin_unlock_bh(®_pending_beacons_lock);
2319 static void reg_process_self_managed_hints(void)
2321 struct cfg80211_registered_device *rdev;
2322 struct wiphy *wiphy;
2323 const struct ieee80211_regdomain *tmp;
2324 const struct ieee80211_regdomain *regd;
2325 enum nl80211_band band;
2326 struct regulatory_request request = {};
2328 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2329 wiphy = &rdev->wiphy;
2331 spin_lock(®_requests_lock);
2332 regd = rdev->requested_regd;
2333 rdev->requested_regd = NULL;
2334 spin_unlock(®_requests_lock);
2339 tmp = get_wiphy_regdom(wiphy);
2340 rcu_assign_pointer(wiphy->regd, regd);
2341 rcu_free_regdom(tmp);
2343 for (band = 0; band < NUM_NL80211_BANDS; band++)
2344 handle_band_custom(wiphy, wiphy->bands[band], regd);
2346 reg_process_ht_flags(wiphy);
2348 request.wiphy_idx = get_wiphy_idx(wiphy);
2349 request.alpha2[0] = regd->alpha2[0];
2350 request.alpha2[1] = regd->alpha2[1];
2351 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
2353 nl80211_send_wiphy_reg_change_event(&request);
2356 reg_check_channels();
2359 static void reg_todo(struct work_struct *work)
2362 reg_process_pending_hints();
2363 reg_process_pending_beacon_hints();
2364 reg_process_self_managed_hints();
2368 static void queue_regulatory_request(struct regulatory_request *request)
2370 request->alpha2[0] = toupper(request->alpha2[0]);
2371 request->alpha2[1] = toupper(request->alpha2[1]);
2373 spin_lock(®_requests_lock);
2374 list_add_tail(&request->list, ®_requests_list);
2375 spin_unlock(®_requests_lock);
2377 schedule_work(®_work);
2381 * Core regulatory hint -- happens during cfg80211_init()
2382 * and when we restore regulatory settings.
2384 static int regulatory_hint_core(const char *alpha2)
2386 struct regulatory_request *request;
2388 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2392 request->alpha2[0] = alpha2[0];
2393 request->alpha2[1] = alpha2[1];
2394 request->initiator = NL80211_REGDOM_SET_BY_CORE;
2395 request->wiphy_idx = WIPHY_IDX_INVALID;
2397 queue_regulatory_request(request);
2403 int regulatory_hint_user(const char *alpha2,
2404 enum nl80211_user_reg_hint_type user_reg_hint_type)
2406 struct regulatory_request *request;
2408 if (WARN_ON(!alpha2))
2411 if (!is_world_regdom(alpha2) && !is_an_alpha2(alpha2))
2414 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2418 request->wiphy_idx = WIPHY_IDX_INVALID;
2419 request->alpha2[0] = alpha2[0];
2420 request->alpha2[1] = alpha2[1];
2421 request->initiator = NL80211_REGDOM_SET_BY_USER;
2422 request->user_reg_hint_type = user_reg_hint_type;
2424 /* Allow calling CRDA again */
2425 reset_crda_timeouts();
2427 queue_regulatory_request(request);
2432 int regulatory_hint_indoor(bool is_indoor, u32 portid)
2434 spin_lock(®_indoor_lock);
2436 /* It is possible that more than one user space process is trying to
2437 * configure the indoor setting. To handle such cases, clear the indoor
2438 * setting in case that some process does not think that the device
2439 * is operating in an indoor environment. In addition, if a user space
2440 * process indicates that it is controlling the indoor setting, save its
2441 * portid, i.e., make it the owner.
2443 reg_is_indoor = is_indoor;
2444 if (reg_is_indoor) {
2445 if (!reg_is_indoor_portid)
2446 reg_is_indoor_portid = portid;
2448 reg_is_indoor_portid = 0;
2451 spin_unlock(®_indoor_lock);
2454 reg_check_channels();
2459 void regulatory_netlink_notify(u32 portid)
2461 spin_lock(®_indoor_lock);
2463 if (reg_is_indoor_portid != portid) {
2464 spin_unlock(®_indoor_lock);
2468 reg_is_indoor = false;
2469 reg_is_indoor_portid = 0;
2471 spin_unlock(®_indoor_lock);
2473 reg_check_channels();
2477 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2479 struct regulatory_request *request;
2481 if (WARN_ON(!alpha2 || !wiphy))
2484 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
2486 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2490 request->wiphy_idx = get_wiphy_idx(wiphy);
2492 request->alpha2[0] = alpha2[0];
2493 request->alpha2[1] = alpha2[1];
2494 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
2496 /* Allow calling CRDA again */
2497 reset_crda_timeouts();
2499 queue_regulatory_request(request);
2503 EXPORT_SYMBOL(regulatory_hint);
2505 void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band,
2506 const u8 *country_ie, u8 country_ie_len)
2509 enum environment_cap env = ENVIRON_ANY;
2510 struct regulatory_request *request = NULL, *lr;
2512 /* IE len must be evenly divisible by 2 */
2513 if (country_ie_len & 0x01)
2516 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2519 request = kzalloc(sizeof(*request), GFP_KERNEL);
2523 alpha2[0] = country_ie[0];
2524 alpha2[1] = country_ie[1];
2526 if (country_ie[2] == 'I')
2527 env = ENVIRON_INDOOR;
2528 else if (country_ie[2] == 'O')
2529 env = ENVIRON_OUTDOOR;
2532 lr = get_last_request();
2538 * We will run this only upon a successful connection on cfg80211.
2539 * We leave conflict resolution to the workqueue, where can hold
2542 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2543 lr->wiphy_idx != WIPHY_IDX_INVALID)
2546 request->wiphy_idx = get_wiphy_idx(wiphy);
2547 request->alpha2[0] = alpha2[0];
2548 request->alpha2[1] = alpha2[1];
2549 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
2550 request->country_ie_env = env;
2552 /* Allow calling CRDA again */
2553 reset_crda_timeouts();
2555 queue_regulatory_request(request);
2562 static void restore_alpha2(char *alpha2, bool reset_user)
2564 /* indicates there is no alpha2 to consider for restoration */
2568 /* The user setting has precedence over the module parameter */
2569 if (is_user_regdom_saved()) {
2570 /* Unless we're asked to ignore it and reset it */
2572 pr_debug("Restoring regulatory settings including user preference\n");
2573 user_alpha2[0] = '9';
2574 user_alpha2[1] = '7';
2577 * If we're ignoring user settings, we still need to
2578 * check the module parameter to ensure we put things
2579 * back as they were for a full restore.
2581 if (!is_world_regdom(ieee80211_regdom)) {
2582 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2583 ieee80211_regdom[0], ieee80211_regdom[1]);
2584 alpha2[0] = ieee80211_regdom[0];
2585 alpha2[1] = ieee80211_regdom[1];
2588 pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n",
2589 user_alpha2[0], user_alpha2[1]);
2590 alpha2[0] = user_alpha2[0];
2591 alpha2[1] = user_alpha2[1];
2593 } else if (!is_world_regdom(ieee80211_regdom)) {
2594 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2595 ieee80211_regdom[0], ieee80211_regdom[1]);
2596 alpha2[0] = ieee80211_regdom[0];
2597 alpha2[1] = ieee80211_regdom[1];
2599 pr_debug("Restoring regulatory settings\n");
2602 static void restore_custom_reg_settings(struct wiphy *wiphy)
2604 struct ieee80211_supported_band *sband;
2605 enum nl80211_band band;
2606 struct ieee80211_channel *chan;
2609 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2610 sband = wiphy->bands[band];
2613 for (i = 0; i < sband->n_channels; i++) {
2614 chan = &sband->channels[i];
2615 chan->flags = chan->orig_flags;
2616 chan->max_antenna_gain = chan->orig_mag;
2617 chan->max_power = chan->orig_mpwr;
2618 chan->beacon_found = false;
2624 * Restoring regulatory settings involves ingoring any
2625 * possibly stale country IE information and user regulatory
2626 * settings if so desired, this includes any beacon hints
2627 * learned as we could have traveled outside to another country
2628 * after disconnection. To restore regulatory settings we do
2629 * exactly what we did at bootup:
2631 * - send a core regulatory hint
2632 * - send a user regulatory hint if applicable
2634 * Device drivers that send a regulatory hint for a specific country
2635 * keep their own regulatory domain on wiphy->regd so that does does
2636 * not need to be remembered.
2638 static void restore_regulatory_settings(bool reset_user)
2641 char world_alpha2[2];
2642 struct reg_beacon *reg_beacon, *btmp;
2643 LIST_HEAD(tmp_reg_req_list);
2644 struct cfg80211_registered_device *rdev;
2649 * Clear the indoor setting in case that it is not controlled by user
2650 * space, as otherwise there is no guarantee that the device is still
2651 * operating in an indoor environment.
2653 spin_lock(®_indoor_lock);
2654 if (reg_is_indoor && !reg_is_indoor_portid) {
2655 reg_is_indoor = false;
2656 reg_check_channels();
2658 spin_unlock(®_indoor_lock);
2660 reset_regdomains(true, &world_regdom);
2661 restore_alpha2(alpha2, reset_user);
2664 * If there's any pending requests we simply
2665 * stash them to a temporary pending queue and
2666 * add then after we've restored regulatory
2669 spin_lock(®_requests_lock);
2670 list_splice_tail_init(®_requests_list, &tmp_reg_req_list);
2671 spin_unlock(®_requests_lock);
2673 /* Clear beacon hints */
2674 spin_lock_bh(®_pending_beacons_lock);
2675 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) {
2676 list_del(®_beacon->list);
2679 spin_unlock_bh(®_pending_beacons_lock);
2681 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) {
2682 list_del(®_beacon->list);
2686 /* First restore to the basic regulatory settings */
2687 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
2688 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
2690 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2691 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2693 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
2694 restore_custom_reg_settings(&rdev->wiphy);
2697 regulatory_hint_core(world_alpha2);
2700 * This restores the ieee80211_regdom module parameter
2701 * preference or the last user requested regulatory
2702 * settings, user regulatory settings takes precedence.
2704 if (is_an_alpha2(alpha2))
2705 regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER);
2707 spin_lock(®_requests_lock);
2708 list_splice_tail_init(&tmp_reg_req_list, ®_requests_list);
2709 spin_unlock(®_requests_lock);
2711 pr_debug("Kicking the queue\n");
2713 schedule_work(®_work);
2716 static bool is_wiphy_all_set_reg_flag(enum ieee80211_regulatory_flags flag)
2718 struct cfg80211_registered_device *rdev;
2719 struct wireless_dev *wdev;
2721 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2722 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
2724 if (!(wdev->wiphy->regulatory_flags & flag)) {
2735 void regulatory_hint_disconnect(void)
2737 /* Restore of regulatory settings is not required when wiphy(s)
2738 * ignore IE from connected access point but clearance of beacon hints
2739 * is required when wiphy(s) supports beacon hints.
2741 if (is_wiphy_all_set_reg_flag(REGULATORY_COUNTRY_IE_IGNORE)) {
2742 struct reg_beacon *reg_beacon, *btmp;
2744 if (is_wiphy_all_set_reg_flag(REGULATORY_DISABLE_BEACON_HINTS))
2747 spin_lock_bh(®_pending_beacons_lock);
2748 list_for_each_entry_safe(reg_beacon, btmp,
2749 ®_pending_beacons, list) {
2750 list_del(®_beacon->list);
2753 spin_unlock_bh(®_pending_beacons_lock);
2755 list_for_each_entry_safe(reg_beacon, btmp,
2756 ®_beacon_list, list) {
2757 list_del(®_beacon->list);
2764 pr_debug("All devices are disconnected, going to restore regulatory settings\n");
2765 restore_regulatory_settings(false);
2768 static bool freq_is_chan_12_13_14(u16 freq)
2770 if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) ||
2771 freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) ||
2772 freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ))
2777 static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
2779 struct reg_beacon *pending_beacon;
2781 list_for_each_entry(pending_beacon, ®_pending_beacons, list)
2782 if (beacon_chan->center_freq ==
2783 pending_beacon->chan.center_freq)
2788 int regulatory_hint_found_beacon(struct wiphy *wiphy,
2789 struct ieee80211_channel *beacon_chan,
2792 struct reg_beacon *reg_beacon;
2795 if (beacon_chan->beacon_found ||
2796 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
2797 (beacon_chan->band == NL80211_BAND_2GHZ &&
2798 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
2801 spin_lock_bh(®_pending_beacons_lock);
2802 processing = pending_reg_beacon(beacon_chan);
2803 spin_unlock_bh(®_pending_beacons_lock);
2808 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2812 pr_debug("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
2813 beacon_chan->center_freq,
2814 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2817 memcpy(®_beacon->chan, beacon_chan,
2818 sizeof(struct ieee80211_channel));
2821 * Since we can be called from BH or and non-BH context
2822 * we must use spin_lock_bh()
2824 spin_lock_bh(®_pending_beacons_lock);
2825 list_add_tail(®_beacon->list, ®_pending_beacons);
2826 spin_unlock_bh(®_pending_beacons_lock);
2828 schedule_work(®_work);
2833 static void print_rd_rules(const struct ieee80211_regdomain *rd)
2836 const struct ieee80211_reg_rule *reg_rule = NULL;
2837 const struct ieee80211_freq_range *freq_range = NULL;
2838 const struct ieee80211_power_rule *power_rule = NULL;
2839 char bw[32], cac_time[32];
2841 pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
2843 for (i = 0; i < rd->n_reg_rules; i++) {
2844 reg_rule = &rd->reg_rules[i];
2845 freq_range = ®_rule->freq_range;
2846 power_rule = ®_rule->power_rule;
2848 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
2849 snprintf(bw, sizeof(bw), "%d KHz, %u KHz AUTO",
2850 freq_range->max_bandwidth_khz,
2851 reg_get_max_bandwidth(rd, reg_rule));
2853 snprintf(bw, sizeof(bw), "%d KHz",
2854 freq_range->max_bandwidth_khz);
2856 if (reg_rule->flags & NL80211_RRF_DFS)
2857 scnprintf(cac_time, sizeof(cac_time), "%u s",
2858 reg_rule->dfs_cac_ms/1000);
2860 scnprintf(cac_time, sizeof(cac_time), "N/A");
2864 * There may not be documentation for max antenna gain
2865 * in certain regions
2867 if (power_rule->max_antenna_gain)
2868 pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
2869 freq_range->start_freq_khz,
2870 freq_range->end_freq_khz,
2872 power_rule->max_antenna_gain,
2873 power_rule->max_eirp,
2876 pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
2877 freq_range->start_freq_khz,
2878 freq_range->end_freq_khz,
2880 power_rule->max_eirp,
2885 bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
2887 switch (dfs_region) {
2888 case NL80211_DFS_UNSET:
2889 case NL80211_DFS_FCC:
2890 case NL80211_DFS_ETSI:
2891 case NL80211_DFS_JP:
2894 pr_debug("Ignoring uknown DFS master region: %d\n", dfs_region);
2899 static void print_regdomain(const struct ieee80211_regdomain *rd)
2901 struct regulatory_request *lr = get_last_request();
2903 if (is_intersected_alpha2(rd->alpha2)) {
2904 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2905 struct cfg80211_registered_device *rdev;
2906 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
2908 pr_debug("Current regulatory domain updated by AP to: %c%c\n",
2909 rdev->country_ie_alpha2[0],
2910 rdev->country_ie_alpha2[1]);
2912 pr_debug("Current regulatory domain intersected:\n");
2914 pr_debug("Current regulatory domain intersected:\n");
2915 } else if (is_world_regdom(rd->alpha2)) {
2916 pr_debug("World regulatory domain updated:\n");
2918 if (is_unknown_alpha2(rd->alpha2))
2919 pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n");
2921 if (reg_request_cell_base(lr))
2922 pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n",
2923 rd->alpha2[0], rd->alpha2[1]);
2925 pr_debug("Regulatory domain changed to country: %c%c\n",
2926 rd->alpha2[0], rd->alpha2[1]);
2930 pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
2934 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
2936 pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
2940 static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
2942 if (!is_world_regdom(rd->alpha2))
2944 update_world_regdomain(rd);
2948 static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
2949 struct regulatory_request *user_request)
2951 const struct ieee80211_regdomain *intersected_rd = NULL;
2953 if (!regdom_changes(rd->alpha2))
2956 if (!is_valid_rd(rd)) {
2957 pr_err("Invalid regulatory domain detected: %c%c\n",
2958 rd->alpha2[0], rd->alpha2[1]);
2959 print_regdomain_info(rd);
2963 if (!user_request->intersect) {
2964 reset_regdomains(false, rd);
2968 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2969 if (!intersected_rd)
2974 reset_regdomains(false, intersected_rd);
2979 static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
2980 struct regulatory_request *driver_request)
2982 const struct ieee80211_regdomain *regd;
2983 const struct ieee80211_regdomain *intersected_rd = NULL;
2984 const struct ieee80211_regdomain *tmp;
2985 struct wiphy *request_wiphy;
2987 if (is_world_regdom(rd->alpha2))
2990 if (!regdom_changes(rd->alpha2))
2993 if (!is_valid_rd(rd)) {
2994 pr_err("Invalid regulatory domain detected: %c%c\n",
2995 rd->alpha2[0], rd->alpha2[1]);
2996 print_regdomain_info(rd);
3000 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
3004 if (!driver_request->intersect) {
3005 if (request_wiphy->regd)
3008 regd = reg_copy_regd(rd);
3010 return PTR_ERR(regd);
3012 rcu_assign_pointer(request_wiphy->regd, regd);
3013 reset_regdomains(false, rd);
3017 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
3018 if (!intersected_rd)
3022 * We can trash what CRDA provided now.
3023 * However if a driver requested this specific regulatory
3024 * domain we keep it for its private use
3026 tmp = get_wiphy_regdom(request_wiphy);
3027 rcu_assign_pointer(request_wiphy->regd, rd);
3028 rcu_free_regdom(tmp);
3032 reset_regdomains(false, intersected_rd);
3037 static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
3038 struct regulatory_request *country_ie_request)
3040 struct wiphy *request_wiphy;
3042 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
3043 !is_unknown_alpha2(rd->alpha2))
3047 * Lets only bother proceeding on the same alpha2 if the current
3048 * rd is non static (it means CRDA was present and was used last)
3049 * and the pending request came in from a country IE
3052 if (!is_valid_rd(rd)) {
3053 pr_err("Invalid regulatory domain detected: %c%c\n",
3054 rd->alpha2[0], rd->alpha2[1]);
3055 print_regdomain_info(rd);
3059 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
3063 if (country_ie_request->intersect)
3066 reset_regdomains(false, rd);
3071 * Use this call to set the current regulatory domain. Conflicts with
3072 * multiple drivers can be ironed out later. Caller must've already
3073 * kmalloc'd the rd structure.
3075 int set_regdom(const struct ieee80211_regdomain *rd,
3076 enum ieee80211_regd_source regd_src)
3078 struct regulatory_request *lr;
3079 bool user_reset = false;
3082 if (!reg_is_valid_request(rd->alpha2)) {
3087 if (regd_src == REGD_SOURCE_CRDA)
3088 reset_crda_timeouts();
3090 lr = get_last_request();
3092 /* Note that this doesn't update the wiphys, this is done below */
3093 switch (lr->initiator) {
3094 case NL80211_REGDOM_SET_BY_CORE:
3095 r = reg_set_rd_core(rd);
3097 case NL80211_REGDOM_SET_BY_USER:
3098 r = reg_set_rd_user(rd, lr);
3101 case NL80211_REGDOM_SET_BY_DRIVER:
3102 r = reg_set_rd_driver(rd, lr);
3104 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
3105 r = reg_set_rd_country_ie(rd, lr);
3108 WARN(1, "invalid initiator %d\n", lr->initiator);
3116 reg_set_request_processed();
3119 /* Back to world regulatory in case of errors */
3120 restore_regulatory_settings(user_reset);
3127 /* This would make this whole thing pointless */
3128 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
3131 /* update all wiphys now with the new established regulatory domain */
3132 update_all_wiphy_regulatory(lr->initiator);
3134 print_regdomain(get_cfg80211_regdom());
3136 nl80211_send_reg_change_event(lr);
3138 reg_set_request_processed();
3143 static int __regulatory_set_wiphy_regd(struct wiphy *wiphy,
3144 struct ieee80211_regdomain *rd)
3146 const struct ieee80211_regdomain *regd;
3147 const struct ieee80211_regdomain *prev_regd;
3148 struct cfg80211_registered_device *rdev;
3150 if (WARN_ON(!wiphy || !rd))
3153 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED),
3154 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n"))
3157 if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) {
3158 print_regdomain_info(rd);
3162 regd = reg_copy_regd(rd);
3164 return PTR_ERR(regd);
3166 rdev = wiphy_to_rdev(wiphy);
3168 spin_lock(®_requests_lock);
3169 prev_regd = rdev->requested_regd;
3170 rdev->requested_regd = regd;
3171 spin_unlock(®_requests_lock);
3177 int regulatory_set_wiphy_regd(struct wiphy *wiphy,
3178 struct ieee80211_regdomain *rd)
3180 int ret = __regulatory_set_wiphy_regd(wiphy, rd);
3185 schedule_work(®_work);
3188 EXPORT_SYMBOL(regulatory_set_wiphy_regd);
3190 int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy,
3191 struct ieee80211_regdomain *rd)
3197 ret = __regulatory_set_wiphy_regd(wiphy, rd);
3201 /* process the request immediately */
3202 reg_process_self_managed_hints();
3205 EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl);
3207 void wiphy_regulatory_register(struct wiphy *wiphy)
3209 struct regulatory_request *lr;
3211 /* self-managed devices ignore external hints */
3212 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
3213 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
3214 REGULATORY_COUNTRY_IE_IGNORE;
3216 if (!reg_dev_ignore_cell_hint(wiphy))
3217 reg_num_devs_support_basehint++;
3219 lr = get_last_request();
3220 wiphy_update_regulatory(wiphy, lr->initiator);
3221 wiphy_all_share_dfs_chan_state(wiphy);
3224 void wiphy_regulatory_deregister(struct wiphy *wiphy)
3226 struct wiphy *request_wiphy = NULL;
3227 struct regulatory_request *lr;
3229 lr = get_last_request();
3231 if (!reg_dev_ignore_cell_hint(wiphy))
3232 reg_num_devs_support_basehint--;
3234 rcu_free_regdom(get_wiphy_regdom(wiphy));
3235 RCU_INIT_POINTER(wiphy->regd, NULL);
3238 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
3240 if (!request_wiphy || request_wiphy != wiphy)
3243 lr->wiphy_idx = WIPHY_IDX_INVALID;
3244 lr->country_ie_env = ENVIRON_ANY;
3248 * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
3249 * UNII band definitions
3251 int cfg80211_get_unii(int freq)
3254 if (freq >= 5150 && freq <= 5250)
3258 if (freq > 5250 && freq <= 5350)
3262 if (freq > 5350 && freq <= 5470)
3266 if (freq > 5470 && freq <= 5725)
3270 if (freq > 5725 && freq <= 5825)
3276 bool regulatory_indoor_allowed(void)
3278 return reg_is_indoor;
3281 bool regulatory_pre_cac_allowed(struct wiphy *wiphy)
3283 const struct ieee80211_regdomain *regd = NULL;
3284 const struct ieee80211_regdomain *wiphy_regd = NULL;
3285 bool pre_cac_allowed = false;
3289 regd = rcu_dereference(cfg80211_regdomain);
3290 wiphy_regd = rcu_dereference(wiphy->regd);
3292 if (regd->dfs_region == NL80211_DFS_ETSI)
3293 pre_cac_allowed = true;
3297 return pre_cac_allowed;
3300 if (regd->dfs_region == wiphy_regd->dfs_region &&
3301 wiphy_regd->dfs_region == NL80211_DFS_ETSI)
3302 pre_cac_allowed = true;
3306 return pre_cac_allowed;
3309 static void cfg80211_check_and_end_cac(struct cfg80211_registered_device *rdev)
3311 struct wireless_dev *wdev;
3312 /* If we finished CAC or received radar, we should end any
3313 * CAC running on the same channels.
3314 * the check !cfg80211_chandef_dfs_usable contain 2 options:
3315 * either all channels are available - those the CAC_FINISHED
3316 * event has effected another wdev state, or there is a channel
3317 * in unavailable state in wdev chandef - those the RADAR_DETECTED
3318 * event has effected another wdev state.
3319 * In both cases we should end the CAC on the wdev.
3321 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3322 if (wdev->cac_started &&
3323 !cfg80211_chandef_dfs_usable(&rdev->wiphy, &wdev->chandef))
3324 rdev_end_cac(rdev, wdev->netdev);
3328 void regulatory_propagate_dfs_state(struct wiphy *wiphy,
3329 struct cfg80211_chan_def *chandef,
3330 enum nl80211_dfs_state dfs_state,
3331 enum nl80211_radar_event event)
3333 struct cfg80211_registered_device *rdev;
3337 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3340 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3341 if (wiphy == &rdev->wiphy)
3344 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
3347 if (!ieee80211_get_channel(&rdev->wiphy,
3348 chandef->chan->center_freq))
3351 cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state);
3353 if (event == NL80211_RADAR_DETECTED ||
3354 event == NL80211_RADAR_CAC_FINISHED) {
3355 cfg80211_sched_dfs_chan_update(rdev);
3356 cfg80211_check_and_end_cac(rdev);
3359 nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL);
3363 int __init regulatory_init(void)
3367 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
3368 if (IS_ERR(reg_pdev))
3369 return PTR_ERR(reg_pdev);
3371 spin_lock_init(®_requests_lock);
3372 spin_lock_init(®_pending_beacons_lock);
3373 spin_lock_init(®_indoor_lock);
3375 reg_regdb_size_check();
3377 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
3379 user_alpha2[0] = '9';
3380 user_alpha2[1] = '7';
3382 /* We always try to get an update for the static regdomain */
3383 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
3385 if (err == -ENOMEM) {
3386 platform_device_unregister(reg_pdev);
3390 * N.B. kobject_uevent_env() can fail mainly for when we're out
3391 * memory which is handled and propagated appropriately above
3392 * but it can also fail during a netlink_broadcast() or during
3393 * early boot for call_usermodehelper(). For now treat these
3394 * errors as non-fatal.
3396 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
3400 * Finally, if the user set the module parameter treat it
3403 if (!is_world_regdom(ieee80211_regdom))
3404 regulatory_hint_user(ieee80211_regdom,
3405 NL80211_USER_REG_HINT_USER);
3410 void regulatory_exit(void)
3412 struct regulatory_request *reg_request, *tmp;
3413 struct reg_beacon *reg_beacon, *btmp;
3415 cancel_work_sync(®_work);
3416 cancel_crda_timeout_sync();
3417 cancel_delayed_work_sync(®_check_chans);
3419 /* Lock to suppress warnings */
3421 reset_regdomains(true, NULL);
3424 dev_set_uevent_suppress(®_pdev->dev, true);
3426 platform_device_unregister(reg_pdev);
3428 list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) {
3429 list_del(®_beacon->list);
3433 list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) {
3434 list_del(®_beacon->list);
3438 list_for_each_entry_safe(reg_request, tmp, ®_requests_list, list) {
3439 list_del(®_request->list);