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>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * DOC: Wireless regulatory infrastructure
24 * The usual implementation is for a driver to read a device EEPROM to
25 * determine which regulatory domain it should be operating under, then
26 * looking up the allowable channels in a driver-local table and finally
27 * registering those channels in the wiphy structure.
29 * Another set of compliance enforcement is for drivers to use their
30 * own compliance limits which can be stored on the EEPROM. The host
31 * driver or firmware may ensure these are used.
33 * In addition to all this we provide an extra layer of regulatory
34 * conformance. For drivers which do not have any regulatory
35 * information CRDA provides the complete regulatory solution.
36 * For others it provides a community effort on further restrictions
37 * to enhance compliance.
39 * Note: When number of rules --> infinity we will not be able to
40 * index on alpha2 any more, instead we'll probably have to
41 * rely on some SHA1 checksum of the regdomain for example.
45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47 #include <linux/kernel.h>
48 #include <linux/export.h>
49 #include <linux/slab.h>
50 #include <linux/list.h>
51 #include <linux/ctype.h>
52 #include <linux/nl80211.h>
53 #include <linux/platform_device.h>
54 #include <linux/moduleparam.h>
55 #include <net/cfg80211.h>
61 #ifdef CONFIG_CFG80211_REG_DEBUG
62 #define REG_DBG_PRINT(format, args...) \
63 printk(KERN_DEBUG pr_fmt(format), ##args)
65 #define REG_DBG_PRINT(args...)
68 enum reg_request_treatment
{
75 static struct regulatory_request core_request_world
= {
76 .initiator
= NL80211_REGDOM_SET_BY_CORE
,
81 .country_ie_env
= ENVIRON_ANY
,
85 * Receipt of information from last regulatory request,
86 * protected by RTNL (and can be accessed with RCU protection)
88 static struct regulatory_request __rcu
*last_request
=
89 (void __rcu
*)&core_request_world
;
91 /* To trigger userspace events */
92 static struct platform_device
*reg_pdev
;
95 * Central wireless core regulatory domains, we only need two,
96 * the current one and a world regulatory domain in case we have no
97 * information to give us an alpha2.
98 * (protected by RTNL, can be read under RCU)
100 const struct ieee80211_regdomain __rcu
*cfg80211_regdomain
;
103 * Number of devices that registered to the core
104 * that support cellular base station regulatory hints
105 * (protected by RTNL)
107 static int reg_num_devs_support_basehint
;
109 static const struct ieee80211_regdomain
*get_cfg80211_regdom(void)
111 return rtnl_dereference(cfg80211_regdomain
);
114 static const struct ieee80211_regdomain
*get_wiphy_regdom(struct wiphy
*wiphy
)
116 return rtnl_dereference(wiphy
->regd
);
119 static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region
)
121 switch (dfs_region
) {
122 case NL80211_DFS_UNSET
:
124 case NL80211_DFS_FCC
:
126 case NL80211_DFS_ETSI
:
134 enum nl80211_dfs_regions
reg_get_dfs_region(struct wiphy
*wiphy
)
136 const struct ieee80211_regdomain
*regd
= NULL
;
137 const struct ieee80211_regdomain
*wiphy_regd
= NULL
;
139 regd
= get_cfg80211_regdom();
143 wiphy_regd
= get_wiphy_regdom(wiphy
);
147 if (wiphy_regd
->dfs_region
== regd
->dfs_region
)
150 REG_DBG_PRINT("%s: device specific dfs_region "
151 "(%s) disagrees with cfg80211's "
152 "central dfs_region (%s)\n",
153 dev_name(&wiphy
->dev
),
154 reg_dfs_region_str(wiphy_regd
->dfs_region
),
155 reg_dfs_region_str(regd
->dfs_region
));
158 return regd
->dfs_region
;
161 static void rcu_free_regdom(const struct ieee80211_regdomain
*r
)
165 kfree_rcu((struct ieee80211_regdomain
*)r
, rcu_head
);
168 static struct regulatory_request
*get_last_request(void)
170 return rcu_dereference_rtnl(last_request
);
173 /* Used to queue up regulatory hints */
174 static LIST_HEAD(reg_requests_list
);
175 static spinlock_t reg_requests_lock
;
177 /* Used to queue up beacon hints for review */
178 static LIST_HEAD(reg_pending_beacons
);
179 static spinlock_t reg_pending_beacons_lock
;
181 /* Used to keep track of processed beacon hints */
182 static LIST_HEAD(reg_beacon_list
);
185 struct list_head list
;
186 struct ieee80211_channel chan
;
189 static void reg_todo(struct work_struct
*work
);
190 static DECLARE_WORK(reg_work
, reg_todo
);
192 static void reg_timeout_work(struct work_struct
*work
);
193 static DECLARE_DELAYED_WORK(reg_timeout
, reg_timeout_work
);
195 /* We keep a static world regulatory domain in case of the absence of CRDA */
196 static const struct ieee80211_regdomain world_regdom
= {
200 /* IEEE 802.11b/g, channels 1..11 */
201 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
202 /* IEEE 802.11b/g, channels 12..13. */
203 REG_RULE(2467-10, 2472+10, 40, 6, 20,
205 /* IEEE 802.11 channel 14 - Only JP enables
206 * this and for 802.11b only */
207 REG_RULE(2484-10, 2484+10, 20, 6, 20,
209 NL80211_RRF_NO_OFDM
),
210 /* IEEE 802.11a, channel 36..48 */
211 REG_RULE(5180-10, 5240+10, 160, 6, 20,
214 /* IEEE 802.11a, channel 52..64 - DFS required */
215 REG_RULE(5260-10, 5320+10, 160, 6, 20,
219 /* IEEE 802.11a, channel 100..144 - DFS required */
220 REG_RULE(5500-10, 5720+10, 160, 6, 20,
224 /* IEEE 802.11a, channel 149..165 */
225 REG_RULE(5745-10, 5825+10, 80, 6, 20,
228 /* IEEE 802.11ad (60gHz), channels 1..3 */
229 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
233 /* protected by RTNL */
234 static const struct ieee80211_regdomain
*cfg80211_world_regdom
=
237 static char *ieee80211_regdom
= "00";
238 static char user_alpha2
[2];
240 module_param(ieee80211_regdom
, charp
, 0444);
241 MODULE_PARM_DESC(ieee80211_regdom
, "IEEE 802.11 regulatory domain code");
243 static void reg_kfree_last_request(void)
245 struct regulatory_request
*lr
;
247 lr
= get_last_request();
249 if (lr
!= &core_request_world
&& lr
)
250 kfree_rcu(lr
, rcu_head
);
253 static void reg_update_last_request(struct regulatory_request
*request
)
255 reg_kfree_last_request();
256 rcu_assign_pointer(last_request
, request
);
259 static void reset_regdomains(bool full_reset
,
260 const struct ieee80211_regdomain
*new_regdom
)
262 const struct ieee80211_regdomain
*r
;
266 r
= get_cfg80211_regdom();
268 /* avoid freeing static information or freeing something twice */
269 if (r
== cfg80211_world_regdom
)
271 if (cfg80211_world_regdom
== &world_regdom
)
272 cfg80211_world_regdom
= NULL
;
273 if (r
== &world_regdom
)
277 rcu_free_regdom(cfg80211_world_regdom
);
279 cfg80211_world_regdom
= &world_regdom
;
280 rcu_assign_pointer(cfg80211_regdomain
, new_regdom
);
285 reg_update_last_request(&core_request_world
);
289 * Dynamic world regulatory domain requested by the wireless
290 * core upon initialization
292 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
294 struct regulatory_request
*lr
;
296 lr
= get_last_request();
300 reset_regdomains(false, rd
);
302 cfg80211_world_regdom
= rd
;
305 bool is_world_regdom(const char *alpha2
)
309 return alpha2
[0] == '0' && alpha2
[1] == '0';
312 static bool is_alpha2_set(const char *alpha2
)
316 return alpha2
[0] && alpha2
[1];
319 static bool is_unknown_alpha2(const char *alpha2
)
324 * Special case where regulatory domain was built by driver
325 * but a specific alpha2 cannot be determined
327 return alpha2
[0] == '9' && alpha2
[1] == '9';
330 static bool is_intersected_alpha2(const char *alpha2
)
335 * Special case where regulatory domain is the
336 * result of an intersection between two regulatory domain
339 return alpha2
[0] == '9' && alpha2
[1] == '8';
342 static bool is_an_alpha2(const char *alpha2
)
346 return isalpha(alpha2
[0]) && isalpha(alpha2
[1]);
349 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
351 if (!alpha2_x
|| !alpha2_y
)
353 return alpha2_x
[0] == alpha2_y
[0] && alpha2_x
[1] == alpha2_y
[1];
356 static bool regdom_changes(const char *alpha2
)
358 const struct ieee80211_regdomain
*r
= get_cfg80211_regdom();
362 return !alpha2_equal(r
->alpha2
, alpha2
);
366 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
367 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
368 * has ever been issued.
370 static bool is_user_regdom_saved(void)
372 if (user_alpha2
[0] == '9' && user_alpha2
[1] == '7')
375 /* This would indicate a mistake on the design */
376 if (WARN(!is_world_regdom(user_alpha2
) && !is_an_alpha2(user_alpha2
),
377 "Unexpected user alpha2: %c%c\n",
378 user_alpha2
[0], user_alpha2
[1]))
384 static const struct ieee80211_regdomain
*
385 reg_copy_regd(const struct ieee80211_regdomain
*src_regd
)
387 struct ieee80211_regdomain
*regd
;
392 sizeof(struct ieee80211_regdomain
) +
393 src_regd
->n_reg_rules
* sizeof(struct ieee80211_reg_rule
);
395 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
397 return ERR_PTR(-ENOMEM
);
399 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
401 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
402 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
403 sizeof(struct ieee80211_reg_rule
));
408 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
409 struct reg_regdb_search_request
{
411 struct list_head list
;
414 static LIST_HEAD(reg_regdb_search_list
);
415 static DEFINE_MUTEX(reg_regdb_search_mutex
);
417 static void reg_regdb_search(struct work_struct
*work
)
419 struct reg_regdb_search_request
*request
;
420 const struct ieee80211_regdomain
*curdom
, *regdom
= NULL
;
425 mutex_lock(®_regdb_search_mutex
);
426 while (!list_empty(®_regdb_search_list
)) {
427 request
= list_first_entry(®_regdb_search_list
,
428 struct reg_regdb_search_request
,
430 list_del(&request
->list
);
432 for (i
= 0; i
< reg_regdb_size
; i
++) {
433 curdom
= reg_regdb
[i
];
435 if (alpha2_equal(request
->alpha2
, curdom
->alpha2
)) {
436 regdom
= reg_copy_regd(curdom
);
443 mutex_unlock(®_regdb_search_mutex
);
445 if (!IS_ERR_OR_NULL(regdom
))
451 static DECLARE_WORK(reg_regdb_work
, reg_regdb_search
);
453 static void reg_regdb_query(const char *alpha2
)
455 struct reg_regdb_search_request
*request
;
460 request
= kzalloc(sizeof(struct reg_regdb_search_request
), GFP_KERNEL
);
464 memcpy(request
->alpha2
, alpha2
, 2);
466 mutex_lock(®_regdb_search_mutex
);
467 list_add_tail(&request
->list
, ®_regdb_search_list
);
468 mutex_unlock(®_regdb_search_mutex
);
470 schedule_work(®_regdb_work
);
473 /* Feel free to add any other sanity checks here */
474 static void reg_regdb_size_check(void)
476 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
477 WARN_ONCE(!reg_regdb_size
, "db.txt is empty, you should update it...");
480 static inline void reg_regdb_size_check(void) {}
481 static inline void reg_regdb_query(const char *alpha2
) {}
482 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
485 * This lets us keep regulatory code which is updated on a regulatory
486 * basis in userspace.
488 static int call_crda(const char *alpha2
)
491 char *env
[] = { country
, NULL
};
493 snprintf(country
, sizeof(country
), "COUNTRY=%c%c",
494 alpha2
[0], alpha2
[1]);
496 if (!is_world_regdom((char *) alpha2
))
497 pr_info("Calling CRDA for country: %c%c\n",
498 alpha2
[0], alpha2
[1]);
500 pr_info("Calling CRDA to update world regulatory domain\n");
502 /* query internal regulatory database (if it exists) */
503 reg_regdb_query(alpha2
);
505 return kobject_uevent_env(®_pdev
->dev
.kobj
, KOBJ_CHANGE
, env
);
508 static enum reg_request_treatment
509 reg_call_crda(struct regulatory_request
*request
)
511 if (call_crda(request
->alpha2
))
512 return REG_REQ_IGNORE
;
516 bool reg_is_valid_request(const char *alpha2
)
518 struct regulatory_request
*lr
= get_last_request();
520 if (!lr
|| lr
->processed
)
523 return alpha2_equal(lr
->alpha2
, alpha2
);
526 static const struct ieee80211_regdomain
*reg_get_regdomain(struct wiphy
*wiphy
)
528 struct regulatory_request
*lr
= get_last_request();
531 * Follow the driver's regulatory domain, if present, unless a country
532 * IE has been processed or a user wants to help complaince further
534 if (lr
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
535 lr
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
537 return get_wiphy_regdom(wiphy
);
539 return get_cfg80211_regdom();
542 unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain
*rd
,
543 const struct ieee80211_reg_rule
*rule
)
545 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
546 const struct ieee80211_freq_range
*freq_range_tmp
;
547 const struct ieee80211_reg_rule
*tmp
;
548 u32 start_freq
, end_freq
, idx
, no
;
550 for (idx
= 0; idx
< rd
->n_reg_rules
; idx
++)
551 if (rule
== &rd
->reg_rules
[idx
])
554 if (idx
== rd
->n_reg_rules
)
561 tmp
= &rd
->reg_rules
[--no
];
562 freq_range_tmp
= &tmp
->freq_range
;
564 if (freq_range_tmp
->end_freq_khz
< freq_range
->start_freq_khz
)
567 freq_range
= freq_range_tmp
;
570 start_freq
= freq_range
->start_freq_khz
;
573 freq_range
= &rule
->freq_range
;
576 while (no
< rd
->n_reg_rules
- 1) {
577 tmp
= &rd
->reg_rules
[++no
];
578 freq_range_tmp
= &tmp
->freq_range
;
580 if (freq_range_tmp
->start_freq_khz
> freq_range
->end_freq_khz
)
583 freq_range
= freq_range_tmp
;
586 end_freq
= freq_range
->end_freq_khz
;
588 return end_freq
- start_freq
;
591 /* Sanity check on a regulatory rule */
592 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
594 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
597 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
600 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
603 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
605 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
606 freq_range
->max_bandwidth_khz
> freq_diff
)
612 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
614 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
617 if (!rd
->n_reg_rules
)
620 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
623 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
624 reg_rule
= &rd
->reg_rules
[i
];
625 if (!is_valid_reg_rule(reg_rule
))
632 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
633 u32 center_freq_khz
, u32 bw_khz
)
635 u32 start_freq_khz
, end_freq_khz
;
637 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
638 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
640 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
641 end_freq_khz
<= freq_range
->end_freq_khz
)
648 * freq_in_rule_band - tells us if a frequency is in a frequency band
649 * @freq_range: frequency rule we want to query
650 * @freq_khz: frequency we are inquiring about
652 * This lets us know if a specific frequency rule is or is not relevant to
653 * a specific frequency's band. Bands are device specific and artificial
654 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
655 * however it is safe for now to assume that a frequency rule should not be
656 * part of a frequency's band if the start freq or end freq are off by more
657 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
659 * This resolution can be lowered and should be considered as we add
660 * regulatory rule support for other "bands".
662 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
665 #define ONE_GHZ_IN_KHZ 1000000
667 * From 802.11ad: directional multi-gigabit (DMG):
668 * Pertaining to operation in a frequency band containing a channel
669 * with the Channel starting frequency above 45 GHz.
671 u32 limit
= freq_khz
> 45 * ONE_GHZ_IN_KHZ
?
672 10 * ONE_GHZ_IN_KHZ
: 2 * ONE_GHZ_IN_KHZ
;
673 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= limit
)
675 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= limit
)
678 #undef ONE_GHZ_IN_KHZ
682 * Later on we can perhaps use the more restrictive DFS
683 * region but we don't have information for that yet so
684 * for now simply disallow conflicts.
686 static enum nl80211_dfs_regions
687 reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1
,
688 const enum nl80211_dfs_regions dfs_region2
)
690 if (dfs_region1
!= dfs_region2
)
691 return NL80211_DFS_UNSET
;
696 * Helper for regdom_intersect(), this does the real
697 * mathematical intersection fun
699 static int reg_rules_intersect(const struct ieee80211_regdomain
*rd1
,
700 const struct ieee80211_regdomain
*rd2
,
701 const struct ieee80211_reg_rule
*rule1
,
702 const struct ieee80211_reg_rule
*rule2
,
703 struct ieee80211_reg_rule
*intersected_rule
)
705 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
706 struct ieee80211_freq_range
*freq_range
;
707 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
708 struct ieee80211_power_rule
*power_rule
;
709 u32 freq_diff
, max_bandwidth1
, max_bandwidth2
;
711 freq_range1
= &rule1
->freq_range
;
712 freq_range2
= &rule2
->freq_range
;
713 freq_range
= &intersected_rule
->freq_range
;
715 power_rule1
= &rule1
->power_rule
;
716 power_rule2
= &rule2
->power_rule
;
717 power_rule
= &intersected_rule
->power_rule
;
719 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
720 freq_range2
->start_freq_khz
);
721 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
722 freq_range2
->end_freq_khz
);
724 max_bandwidth1
= freq_range1
->max_bandwidth_khz
;
725 max_bandwidth2
= freq_range2
->max_bandwidth_khz
;
727 if (rule1
->flags
& NL80211_RRF_AUTO_BW
)
728 max_bandwidth1
= reg_get_max_bandwidth(rd1
, rule1
);
729 if (rule2
->flags
& NL80211_RRF_AUTO_BW
)
730 max_bandwidth2
= reg_get_max_bandwidth(rd2
, rule2
);
732 freq_range
->max_bandwidth_khz
= min(max_bandwidth1
, max_bandwidth2
);
734 intersected_rule
->flags
= rule1
->flags
| rule2
->flags
;
737 * In case NL80211_RRF_AUTO_BW requested for both rules
738 * set AUTO_BW in intersected rule also. Next we will
739 * calculate BW correctly in handle_channel function.
740 * In other case remove AUTO_BW flag while we calculate
741 * maximum bandwidth correctly and auto calculation is
744 if ((rule1
->flags
& NL80211_RRF_AUTO_BW
) &&
745 (rule2
->flags
& NL80211_RRF_AUTO_BW
))
746 intersected_rule
->flags
|= NL80211_RRF_AUTO_BW
;
748 intersected_rule
->flags
&= ~NL80211_RRF_AUTO_BW
;
750 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
751 if (freq_range
->max_bandwidth_khz
> freq_diff
)
752 freq_range
->max_bandwidth_khz
= freq_diff
;
754 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
755 power_rule2
->max_eirp
);
756 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
757 power_rule2
->max_antenna_gain
);
759 intersected_rule
->dfs_cac_ms
= max(rule1
->dfs_cac_ms
,
762 if (!is_valid_reg_rule(intersected_rule
))
769 * regdom_intersect - do the intersection between two regulatory domains
770 * @rd1: first regulatory domain
771 * @rd2: second regulatory domain
773 * Use this function to get the intersection between two regulatory domains.
774 * Once completed we will mark the alpha2 for the rd as intersected, "98",
775 * as no one single alpha2 can represent this regulatory domain.
777 * Returns a pointer to the regulatory domain structure which will hold the
778 * resulting intersection of rules between rd1 and rd2. We will
779 * kzalloc() this structure for you.
781 static struct ieee80211_regdomain
*
782 regdom_intersect(const struct ieee80211_regdomain
*rd1
,
783 const struct ieee80211_regdomain
*rd2
)
787 unsigned int num_rules
= 0, rule_idx
= 0;
788 const struct ieee80211_reg_rule
*rule1
, *rule2
;
789 struct ieee80211_reg_rule
*intersected_rule
;
790 struct ieee80211_regdomain
*rd
;
791 /* This is just a dummy holder to help us count */
792 struct ieee80211_reg_rule dummy_rule
;
798 * First we get a count of the rules we'll need, then we actually
799 * build them. This is to so we can malloc() and free() a
800 * regdomain once. The reason we use reg_rules_intersect() here
801 * is it will return -EINVAL if the rule computed makes no sense.
802 * All rules that do check out OK are valid.
805 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
806 rule1
= &rd1
->reg_rules
[x
];
807 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
808 rule2
= &rd2
->reg_rules
[y
];
809 if (!reg_rules_intersect(rd1
, rd2
, rule1
, rule2
,
818 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
819 num_rules
* sizeof(struct ieee80211_reg_rule
);
821 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
825 for (x
= 0; x
< rd1
->n_reg_rules
&& rule_idx
< num_rules
; x
++) {
826 rule1
= &rd1
->reg_rules
[x
];
827 for (y
= 0; y
< rd2
->n_reg_rules
&& rule_idx
< num_rules
; y
++) {
828 rule2
= &rd2
->reg_rules
[y
];
830 * This time around instead of using the stack lets
831 * write to the target rule directly saving ourselves
834 intersected_rule
= &rd
->reg_rules
[rule_idx
];
835 r
= reg_rules_intersect(rd1
, rd2
, rule1
, rule2
,
838 * No need to memset here the intersected rule here as
839 * we're not using the stack anymore
847 if (rule_idx
!= num_rules
) {
852 rd
->n_reg_rules
= num_rules
;
855 rd
->dfs_region
= reg_intersect_dfs_region(rd1
->dfs_region
,
862 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
863 * want to just have the channel structure use these
865 static u32
map_regdom_flags(u32 rd_flags
)
867 u32 channel_flags
= 0;
868 if (rd_flags
& NL80211_RRF_NO_IR_ALL
)
869 channel_flags
|= IEEE80211_CHAN_NO_IR
;
870 if (rd_flags
& NL80211_RRF_DFS
)
871 channel_flags
|= IEEE80211_CHAN_RADAR
;
872 if (rd_flags
& NL80211_RRF_NO_OFDM
)
873 channel_flags
|= IEEE80211_CHAN_NO_OFDM
;
874 return channel_flags
;
877 static const struct ieee80211_reg_rule
*
878 freq_reg_info_regd(struct wiphy
*wiphy
, u32 center_freq
,
879 const struct ieee80211_regdomain
*regd
)
882 bool band_rule_found
= false;
883 bool bw_fits
= false;
886 return ERR_PTR(-EINVAL
);
888 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
889 const struct ieee80211_reg_rule
*rr
;
890 const struct ieee80211_freq_range
*fr
= NULL
;
892 rr
= ®d
->reg_rules
[i
];
893 fr
= &rr
->freq_range
;
896 * We only need to know if one frequency rule was
897 * was in center_freq's band, that's enough, so lets
898 * not overwrite it once found
900 if (!band_rule_found
)
901 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
903 bw_fits
= reg_does_bw_fit(fr
, center_freq
, MHZ_TO_KHZ(20));
905 if (band_rule_found
&& bw_fits
)
909 if (!band_rule_found
)
910 return ERR_PTR(-ERANGE
);
912 return ERR_PTR(-EINVAL
);
915 const struct ieee80211_reg_rule
*freq_reg_info(struct wiphy
*wiphy
,
918 const struct ieee80211_regdomain
*regd
;
920 regd
= reg_get_regdomain(wiphy
);
922 return freq_reg_info_regd(wiphy
, center_freq
, regd
);
924 EXPORT_SYMBOL(freq_reg_info
);
926 const char *reg_initiator_name(enum nl80211_reg_initiator initiator
)
929 case NL80211_REGDOM_SET_BY_CORE
:
931 case NL80211_REGDOM_SET_BY_USER
:
933 case NL80211_REGDOM_SET_BY_DRIVER
:
935 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
942 EXPORT_SYMBOL(reg_initiator_name
);
944 #ifdef CONFIG_CFG80211_REG_DEBUG
945 static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain
*regd
,
946 struct ieee80211_channel
*chan
,
947 const struct ieee80211_reg_rule
*reg_rule
)
949 const struct ieee80211_power_rule
*power_rule
;
950 const struct ieee80211_freq_range
*freq_range
;
951 char max_antenna_gain
[32], bw
[32];
953 power_rule
= ®_rule
->power_rule
;
954 freq_range
= ®_rule
->freq_range
;
956 if (!power_rule
->max_antenna_gain
)
957 snprintf(max_antenna_gain
, sizeof(max_antenna_gain
), "N/A");
959 snprintf(max_antenna_gain
, sizeof(max_antenna_gain
), "%d",
960 power_rule
->max_antenna_gain
);
962 if (reg_rule
->flags
& NL80211_RRF_AUTO_BW
)
963 snprintf(bw
, sizeof(bw
), "%d KHz, %d KHz AUTO",
964 freq_range
->max_bandwidth_khz
,
965 reg_get_max_bandwidth(regd
, reg_rule
));
967 snprintf(bw
, sizeof(bw
), "%d KHz",
968 freq_range
->max_bandwidth_khz
);
970 REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
973 REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)\n",
974 freq_range
->start_freq_khz
, freq_range
->end_freq_khz
,
975 bw
, max_antenna_gain
,
976 power_rule
->max_eirp
);
979 static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain
*regd
,
980 struct ieee80211_channel
*chan
,
981 const struct ieee80211_reg_rule
*reg_rule
)
988 * Note that right now we assume the desired channel bandwidth
989 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
990 * per channel, the primary and the extension channel).
992 static void handle_channel(struct wiphy
*wiphy
,
993 enum nl80211_reg_initiator initiator
,
994 struct ieee80211_channel
*chan
)
996 u32 flags
, bw_flags
= 0;
997 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
998 const struct ieee80211_power_rule
*power_rule
= NULL
;
999 const struct ieee80211_freq_range
*freq_range
= NULL
;
1000 struct wiphy
*request_wiphy
= NULL
;
1001 struct regulatory_request
*lr
= get_last_request();
1002 const struct ieee80211_regdomain
*regd
;
1003 u32 max_bandwidth_khz
;
1005 request_wiphy
= wiphy_idx_to_wiphy(lr
->wiphy_idx
);
1007 flags
= chan
->orig_flags
;
1009 reg_rule
= freq_reg_info(wiphy
, MHZ_TO_KHZ(chan
->center_freq
));
1010 if (IS_ERR(reg_rule
)) {
1012 * We will disable all channels that do not match our
1013 * received regulatory rule unless the hint is coming
1014 * from a Country IE and the Country IE had no information
1015 * about a band. The IEEE 802.11 spec allows for an AP
1016 * to send only a subset of the regulatory rules allowed,
1017 * so an AP in the US that only supports 2.4 GHz may only send
1018 * a country IE with information for the 2.4 GHz band
1019 * while 5 GHz is still supported.
1021 if (initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1022 PTR_ERR(reg_rule
) == -ERANGE
)
1025 if (lr
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1026 request_wiphy
&& request_wiphy
== wiphy
&&
1027 request_wiphy
->regulatory_flags
& REGULATORY_STRICT_REG
) {
1028 REG_DBG_PRINT("Disabling freq %d MHz for good\n",
1030 chan
->orig_flags
|= IEEE80211_CHAN_DISABLED
;
1031 chan
->flags
= chan
->orig_flags
;
1033 REG_DBG_PRINT("Disabling freq %d MHz\n",
1035 chan
->flags
|= IEEE80211_CHAN_DISABLED
;
1040 regd
= reg_get_regdomain(wiphy
);
1041 chan_reg_rule_print_dbg(regd
, chan
, reg_rule
);
1043 power_rule
= ®_rule
->power_rule
;
1044 freq_range
= ®_rule
->freq_range
;
1046 max_bandwidth_khz
= freq_range
->max_bandwidth_khz
;
1047 /* Check if auto calculation requested */
1048 if (reg_rule
->flags
& NL80211_RRF_AUTO_BW
)
1049 max_bandwidth_khz
= reg_get_max_bandwidth(regd
, reg_rule
);
1051 if (max_bandwidth_khz
< MHZ_TO_KHZ(40))
1052 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1053 if (max_bandwidth_khz
< MHZ_TO_KHZ(80))
1054 bw_flags
|= IEEE80211_CHAN_NO_80MHZ
;
1055 if (max_bandwidth_khz
< MHZ_TO_KHZ(160))
1056 bw_flags
|= IEEE80211_CHAN_NO_160MHZ
;
1058 if (lr
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1059 request_wiphy
&& request_wiphy
== wiphy
&&
1060 request_wiphy
->regulatory_flags
& REGULATORY_STRICT_REG
) {
1062 * This guarantees the driver's requested regulatory domain
1063 * will always be used as a base for further regulatory
1066 chan
->flags
= chan
->orig_flags
=
1067 map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1068 chan
->max_antenna_gain
= chan
->orig_mag
=
1069 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1070 chan
->max_reg_power
= chan
->max_power
= chan
->orig_mpwr
=
1071 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1075 chan
->dfs_state
= NL80211_DFS_USABLE
;
1076 chan
->dfs_state_entered
= jiffies
;
1078 chan
->beacon_found
= false;
1079 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
1080 chan
->max_antenna_gain
=
1081 min_t(int, chan
->orig_mag
,
1082 MBI_TO_DBI(power_rule
->max_antenna_gain
));
1083 chan
->max_reg_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1085 if (chan
->flags
& IEEE80211_CHAN_RADAR
) {
1086 if (reg_rule
->dfs_cac_ms
)
1087 chan
->dfs_cac_ms
= reg_rule
->dfs_cac_ms
;
1089 chan
->dfs_cac_ms
= IEEE80211_DFS_MIN_CAC_TIME_MS
;
1092 if (chan
->orig_mpwr
) {
1094 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1095 * will always follow the passed country IE power settings.
1097 if (initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1098 wiphy
->regulatory_flags
& REGULATORY_COUNTRY_IE_FOLLOW_POWER
)
1099 chan
->max_power
= chan
->max_reg_power
;
1101 chan
->max_power
= min(chan
->orig_mpwr
,
1102 chan
->max_reg_power
);
1104 chan
->max_power
= chan
->max_reg_power
;
1107 static void handle_band(struct wiphy
*wiphy
,
1108 enum nl80211_reg_initiator initiator
,
1109 struct ieee80211_supported_band
*sband
)
1116 for (i
= 0; i
< sband
->n_channels
; i
++)
1117 handle_channel(wiphy
, initiator
, &sband
->channels
[i
]);
1120 static bool reg_request_cell_base(struct regulatory_request
*request
)
1122 if (request
->initiator
!= NL80211_REGDOM_SET_BY_USER
)
1124 return request
->user_reg_hint_type
== NL80211_USER_REG_HINT_CELL_BASE
;
1127 bool reg_last_request_cell_base(void)
1129 return reg_request_cell_base(get_last_request());
1132 #ifdef CONFIG_CFG80211_CERTIFICATION_ONUS
1133 /* Core specific check */
1134 static enum reg_request_treatment
1135 reg_ignore_cell_hint(struct regulatory_request
*pending_request
)
1137 struct regulatory_request
*lr
= get_last_request();
1139 if (!reg_num_devs_support_basehint
)
1140 return REG_REQ_IGNORE
;
1142 if (reg_request_cell_base(lr
) &&
1143 !regdom_changes(pending_request
->alpha2
))
1144 return REG_REQ_ALREADY_SET
;
1149 /* Device specific check */
1150 static bool reg_dev_ignore_cell_hint(struct wiphy
*wiphy
)
1152 return !(wiphy
->features
& NL80211_FEATURE_CELL_BASE_REG_HINTS
);
1155 static int reg_ignore_cell_hint(struct regulatory_request
*pending_request
)
1157 return REG_REQ_IGNORE
;
1160 static bool reg_dev_ignore_cell_hint(struct wiphy
*wiphy
)
1166 static bool wiphy_strict_alpha2_regd(struct wiphy
*wiphy
)
1168 if (wiphy
->regulatory_flags
& REGULATORY_STRICT_REG
&&
1169 !(wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
))
1174 static bool ignore_reg_update(struct wiphy
*wiphy
,
1175 enum nl80211_reg_initiator initiator
)
1177 struct regulatory_request
*lr
= get_last_request();
1180 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1181 "since last_request is not set\n",
1182 reg_initiator_name(initiator
));
1186 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
1187 wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
) {
1188 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1189 "since the driver uses its own custom "
1190 "regulatory domain\n",
1191 reg_initiator_name(initiator
));
1196 * wiphy->regd will be set once the device has its own
1197 * desired regulatory domain set
1199 if (wiphy_strict_alpha2_regd(wiphy
) && !wiphy
->regd
&&
1200 initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1201 !is_world_regdom(lr
->alpha2
)) {
1202 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1203 "since the driver requires its own regulatory "
1204 "domain to be set first\n",
1205 reg_initiator_name(initiator
));
1209 if (reg_request_cell_base(lr
))
1210 return reg_dev_ignore_cell_hint(wiphy
);
1215 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1217 const struct ieee80211_regdomain
*cr
= get_cfg80211_regdom();
1218 const struct ieee80211_regdomain
*wr
= get_wiphy_regdom(wiphy
);
1219 struct regulatory_request
*lr
= get_last_request();
1221 if (is_world_regdom(cr
->alpha2
) || (wr
&& is_world_regdom(wr
->alpha2
)))
1224 if (lr
&& lr
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1225 wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
)
1231 static void handle_reg_beacon(struct wiphy
*wiphy
, unsigned int chan_idx
,
1232 struct reg_beacon
*reg_beacon
)
1234 struct ieee80211_supported_band
*sband
;
1235 struct ieee80211_channel
*chan
;
1236 bool channel_changed
= false;
1237 struct ieee80211_channel chan_before
;
1239 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1240 chan
= &sband
->channels
[chan_idx
];
1242 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
1245 if (chan
->beacon_found
)
1248 chan
->beacon_found
= true;
1250 if (!reg_is_world_roaming(wiphy
))
1253 if (wiphy
->regulatory_flags
& REGULATORY_DISABLE_BEACON_HINTS
)
1256 chan_before
.center_freq
= chan
->center_freq
;
1257 chan_before
.flags
= chan
->flags
;
1259 if (chan
->flags
& IEEE80211_CHAN_NO_IR
) {
1260 chan
->flags
&= ~IEEE80211_CHAN_NO_IR
;
1261 channel_changed
= true;
1264 if (channel_changed
)
1265 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
1269 * Called when a scan on a wiphy finds a beacon on
1272 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
1273 struct reg_beacon
*reg_beacon
)
1276 struct ieee80211_supported_band
*sband
;
1278 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1281 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1283 for (i
= 0; i
< sband
->n_channels
; i
++)
1284 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1288 * Called upon reg changes or a new wiphy is added
1290 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1293 struct ieee80211_supported_band
*sband
;
1294 struct reg_beacon
*reg_beacon
;
1296 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1297 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1299 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1300 for (i
= 0; i
< sband
->n_channels
; i
++)
1301 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1305 /* Reap the advantages of previously found beacons */
1306 static void reg_process_beacons(struct wiphy
*wiphy
)
1309 * Means we are just firing up cfg80211, so no beacons would
1310 * have been processed yet.
1314 wiphy_update_beacon_reg(wiphy
);
1317 static bool is_ht40_allowed(struct ieee80211_channel
*chan
)
1321 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1323 /* This would happen when regulatory rules disallow HT40 completely */
1324 if ((chan
->flags
& IEEE80211_CHAN_NO_HT40
) == IEEE80211_CHAN_NO_HT40
)
1329 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1330 struct ieee80211_channel
*channel
)
1332 struct ieee80211_supported_band
*sband
= wiphy
->bands
[channel
->band
];
1333 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1336 if (!is_ht40_allowed(channel
)) {
1337 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1342 * We need to ensure the extension channels exist to
1343 * be able to use HT40- or HT40+, this finds them (or not)
1345 for (i
= 0; i
< sband
->n_channels
; i
++) {
1346 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1348 if (c
->center_freq
== (channel
->center_freq
- 20))
1350 if (c
->center_freq
== (channel
->center_freq
+ 20))
1355 * Please note that this assumes target bandwidth is 20 MHz,
1356 * if that ever changes we also need to change the below logic
1357 * to include that as well.
1359 if (!is_ht40_allowed(channel_before
))
1360 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1362 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1364 if (!is_ht40_allowed(channel_after
))
1365 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1367 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1370 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1371 struct ieee80211_supported_band
*sband
)
1378 for (i
= 0; i
< sband
->n_channels
; i
++)
1379 reg_process_ht_flags_channel(wiphy
, &sband
->channels
[i
]);
1382 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1384 enum ieee80211_band band
;
1389 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
1390 reg_process_ht_flags_band(wiphy
, wiphy
->bands
[band
]);
1393 static void reg_call_notifier(struct wiphy
*wiphy
,
1394 struct regulatory_request
*request
)
1396 if (wiphy
->reg_notifier
)
1397 wiphy
->reg_notifier(wiphy
, request
);
1400 static void wiphy_update_regulatory(struct wiphy
*wiphy
,
1401 enum nl80211_reg_initiator initiator
)
1403 enum ieee80211_band band
;
1404 struct regulatory_request
*lr
= get_last_request();
1406 if (ignore_reg_update(wiphy
, initiator
)) {
1408 * Regulatory updates set by CORE are ignored for custom
1409 * regulatory cards. Let us notify the changes to the driver,
1410 * as some drivers used this to restore its orig_* reg domain.
1412 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
1413 wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
)
1414 reg_call_notifier(wiphy
, lr
);
1418 lr
->dfs_region
= get_cfg80211_regdom()->dfs_region
;
1420 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
1421 handle_band(wiphy
, initiator
, wiphy
->bands
[band
]);
1423 reg_process_beacons(wiphy
);
1424 reg_process_ht_flags(wiphy
);
1425 reg_call_notifier(wiphy
, lr
);
1428 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
1430 struct cfg80211_registered_device
*rdev
;
1431 struct wiphy
*wiphy
;
1435 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
) {
1436 wiphy
= &rdev
->wiphy
;
1437 wiphy_update_regulatory(wiphy
, initiator
);
1441 static void handle_channel_custom(struct wiphy
*wiphy
,
1442 struct ieee80211_channel
*chan
,
1443 const struct ieee80211_regdomain
*regd
)
1446 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1447 const struct ieee80211_power_rule
*power_rule
= NULL
;
1448 const struct ieee80211_freq_range
*freq_range
= NULL
;
1449 u32 max_bandwidth_khz
;
1451 reg_rule
= freq_reg_info_regd(wiphy
, MHZ_TO_KHZ(chan
->center_freq
),
1454 if (IS_ERR(reg_rule
)) {
1455 REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1457 chan
->orig_flags
|= IEEE80211_CHAN_DISABLED
;
1458 chan
->flags
= chan
->orig_flags
;
1462 chan_reg_rule_print_dbg(regd
, chan
, reg_rule
);
1464 power_rule
= ®_rule
->power_rule
;
1465 freq_range
= ®_rule
->freq_range
;
1467 max_bandwidth_khz
= freq_range
->max_bandwidth_khz
;
1468 /* Check if auto calculation requested */
1469 if (reg_rule
->flags
& NL80211_RRF_AUTO_BW
)
1470 max_bandwidth_khz
= reg_get_max_bandwidth(regd
, reg_rule
);
1472 if (max_bandwidth_khz
< MHZ_TO_KHZ(40))
1473 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1474 if (max_bandwidth_khz
< MHZ_TO_KHZ(80))
1475 bw_flags
|= IEEE80211_CHAN_NO_80MHZ
;
1476 if (max_bandwidth_khz
< MHZ_TO_KHZ(160))
1477 bw_flags
|= IEEE80211_CHAN_NO_160MHZ
;
1479 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1480 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1481 chan
->max_reg_power
= chan
->max_power
=
1482 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1485 static void handle_band_custom(struct wiphy
*wiphy
,
1486 struct ieee80211_supported_band
*sband
,
1487 const struct ieee80211_regdomain
*regd
)
1494 for (i
= 0; i
< sband
->n_channels
; i
++)
1495 handle_channel_custom(wiphy
, &sband
->channels
[i
], regd
);
1498 /* Used by drivers prior to wiphy registration */
1499 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1500 const struct ieee80211_regdomain
*regd
)
1502 enum ieee80211_band band
;
1503 unsigned int bands_set
= 0;
1505 WARN(!(wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
),
1506 "wiphy should have REGULATORY_CUSTOM_REG\n");
1507 wiphy
->regulatory_flags
|= REGULATORY_CUSTOM_REG
;
1509 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1510 if (!wiphy
->bands
[band
])
1512 handle_band_custom(wiphy
, wiphy
->bands
[band
], regd
);
1517 * no point in calling this if it won't have any effect
1518 * on your device's supported bands.
1520 WARN_ON(!bands_set
);
1522 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1524 static void reg_set_request_processed(void)
1526 bool need_more_processing
= false;
1527 struct regulatory_request
*lr
= get_last_request();
1529 lr
->processed
= true;
1531 spin_lock(®_requests_lock
);
1532 if (!list_empty(®_requests_list
))
1533 need_more_processing
= true;
1534 spin_unlock(®_requests_lock
);
1536 if (lr
->initiator
== NL80211_REGDOM_SET_BY_USER
)
1537 cancel_delayed_work(®_timeout
);
1539 if (need_more_processing
)
1540 schedule_work(®_work
);
1544 * reg_process_hint_core - process core regulatory requests
1545 * @pending_request: a pending core regulatory request
1547 * The wireless subsystem can use this function to process
1548 * a regulatory request issued by the regulatory core.
1550 * Returns one of the different reg request treatment values.
1552 static enum reg_request_treatment
1553 reg_process_hint_core(struct regulatory_request
*core_request
)
1556 core_request
->intersect
= false;
1557 core_request
->processed
= false;
1559 reg_update_last_request(core_request
);
1561 return reg_call_crda(core_request
);
1564 static enum reg_request_treatment
1565 __reg_process_hint_user(struct regulatory_request
*user_request
)
1567 struct regulatory_request
*lr
= get_last_request();
1569 if (reg_request_cell_base(user_request
))
1570 return reg_ignore_cell_hint(user_request
);
1572 if (reg_request_cell_base(lr
))
1573 return REG_REQ_IGNORE
;
1575 if (lr
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1576 return REG_REQ_INTERSECT
;
1578 * If the user knows better the user should set the regdom
1579 * to their country before the IE is picked up
1581 if (lr
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1583 return REG_REQ_IGNORE
;
1585 * Process user requests only after previous user/driver/core
1586 * requests have been processed
1588 if ((lr
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1589 lr
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1590 lr
->initiator
== NL80211_REGDOM_SET_BY_USER
) &&
1591 regdom_changes(lr
->alpha2
))
1592 return REG_REQ_IGNORE
;
1594 if (!regdom_changes(user_request
->alpha2
))
1595 return REG_REQ_ALREADY_SET
;
1601 * reg_process_hint_user - process user regulatory requests
1602 * @user_request: a pending user regulatory request
1604 * The wireless subsystem can use this function to process
1605 * a regulatory request initiated by userspace.
1607 * Returns one of the different reg request treatment values.
1609 static enum reg_request_treatment
1610 reg_process_hint_user(struct regulatory_request
*user_request
)
1612 enum reg_request_treatment treatment
;
1614 treatment
= __reg_process_hint_user(user_request
);
1615 if (treatment
== REG_REQ_IGNORE
||
1616 treatment
== REG_REQ_ALREADY_SET
) {
1617 kfree(user_request
);
1621 user_request
->intersect
= treatment
== REG_REQ_INTERSECT
;
1622 user_request
->processed
= false;
1624 reg_update_last_request(user_request
);
1626 user_alpha2
[0] = user_request
->alpha2
[0];
1627 user_alpha2
[1] = user_request
->alpha2
[1];
1629 return reg_call_crda(user_request
);
1632 static enum reg_request_treatment
1633 __reg_process_hint_driver(struct regulatory_request
*driver_request
)
1635 struct regulatory_request
*lr
= get_last_request();
1637 if (lr
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1638 if (regdom_changes(driver_request
->alpha2
))
1640 return REG_REQ_ALREADY_SET
;
1644 * This would happen if you unplug and plug your card
1645 * back in or if you add a new device for which the previously
1646 * loaded card also agrees on the regulatory domain.
1648 if (lr
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1649 !regdom_changes(driver_request
->alpha2
))
1650 return REG_REQ_ALREADY_SET
;
1652 return REG_REQ_INTERSECT
;
1656 * reg_process_hint_driver - process driver regulatory requests
1657 * @driver_request: a pending driver regulatory request
1659 * The wireless subsystem can use this function to process
1660 * a regulatory request issued by an 802.11 driver.
1662 * Returns one of the different reg request treatment values.
1664 static enum reg_request_treatment
1665 reg_process_hint_driver(struct wiphy
*wiphy
,
1666 struct regulatory_request
*driver_request
)
1668 const struct ieee80211_regdomain
*regd
;
1669 enum reg_request_treatment treatment
;
1671 treatment
= __reg_process_hint_driver(driver_request
);
1673 switch (treatment
) {
1676 case REG_REQ_IGNORE
:
1677 kfree(driver_request
);
1679 case REG_REQ_INTERSECT
:
1681 case REG_REQ_ALREADY_SET
:
1682 regd
= reg_copy_regd(get_cfg80211_regdom());
1684 kfree(driver_request
);
1685 return REG_REQ_IGNORE
;
1687 rcu_assign_pointer(wiphy
->regd
, regd
);
1691 driver_request
->intersect
= treatment
== REG_REQ_INTERSECT
;
1692 driver_request
->processed
= false;
1694 reg_update_last_request(driver_request
);
1697 * Since CRDA will not be called in this case as we already
1698 * have applied the requested regulatory domain before we just
1699 * inform userspace we have processed the request
1701 if (treatment
== REG_REQ_ALREADY_SET
) {
1702 nl80211_send_reg_change_event(driver_request
);
1703 reg_set_request_processed();
1707 return reg_call_crda(driver_request
);
1710 static enum reg_request_treatment
1711 __reg_process_hint_country_ie(struct wiphy
*wiphy
,
1712 struct regulatory_request
*country_ie_request
)
1714 struct wiphy
*last_wiphy
= NULL
;
1715 struct regulatory_request
*lr
= get_last_request();
1717 if (reg_request_cell_base(lr
)) {
1718 /* Trust a Cell base station over the AP's country IE */
1719 if (regdom_changes(country_ie_request
->alpha2
))
1720 return REG_REQ_IGNORE
;
1721 return REG_REQ_ALREADY_SET
;
1723 if (wiphy
->regulatory_flags
& REGULATORY_COUNTRY_IE_IGNORE
)
1724 return REG_REQ_IGNORE
;
1727 if (unlikely(!is_an_alpha2(country_ie_request
->alpha2
)))
1730 if (lr
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1733 last_wiphy
= wiphy_idx_to_wiphy(lr
->wiphy_idx
);
1735 if (last_wiphy
!= wiphy
) {
1737 * Two cards with two APs claiming different
1738 * Country IE alpha2s. We could
1739 * intersect them, but that seems unlikely
1740 * to be correct. Reject second one for now.
1742 if (regdom_changes(country_ie_request
->alpha2
))
1743 return REG_REQ_IGNORE
;
1744 return REG_REQ_ALREADY_SET
;
1747 * Two consecutive Country IE hints on the same wiphy.
1748 * This should be picked up early by the driver/stack
1750 if (WARN_ON(regdom_changes(country_ie_request
->alpha2
)))
1752 return REG_REQ_ALREADY_SET
;
1756 * reg_process_hint_country_ie - process regulatory requests from country IEs
1757 * @country_ie_request: a regulatory request from a country IE
1759 * The wireless subsystem can use this function to process
1760 * a regulatory request issued by a country Information Element.
1762 * Returns one of the different reg request treatment values.
1764 static enum reg_request_treatment
1765 reg_process_hint_country_ie(struct wiphy
*wiphy
,
1766 struct regulatory_request
*country_ie_request
)
1768 enum reg_request_treatment treatment
;
1770 treatment
= __reg_process_hint_country_ie(wiphy
, country_ie_request
);
1772 switch (treatment
) {
1775 case REG_REQ_IGNORE
:
1777 case REG_REQ_ALREADY_SET
:
1778 kfree(country_ie_request
);
1780 case REG_REQ_INTERSECT
:
1781 kfree(country_ie_request
);
1783 * This doesn't happen yet, not sure we
1784 * ever want to support it for this case.
1786 WARN_ONCE(1, "Unexpected intersection for country IEs");
1787 return REG_REQ_IGNORE
;
1790 country_ie_request
->intersect
= false;
1791 country_ie_request
->processed
= false;
1793 reg_update_last_request(country_ie_request
);
1795 return reg_call_crda(country_ie_request
);
1798 /* This processes *all* regulatory hints */
1799 static void reg_process_hint(struct regulatory_request
*reg_request
)
1801 struct wiphy
*wiphy
= NULL
;
1802 enum reg_request_treatment treatment
;
1804 if (reg_request
->wiphy_idx
!= WIPHY_IDX_INVALID
)
1805 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1807 switch (reg_request
->initiator
) {
1808 case NL80211_REGDOM_SET_BY_CORE
:
1809 reg_process_hint_core(reg_request
);
1811 case NL80211_REGDOM_SET_BY_USER
:
1812 treatment
= reg_process_hint_user(reg_request
);
1813 if (treatment
== REG_REQ_OK
||
1814 treatment
== REG_REQ_ALREADY_SET
)
1816 queue_delayed_work(system_power_efficient_wq
,
1817 ®_timeout
, msecs_to_jiffies(3142));
1819 case NL80211_REGDOM_SET_BY_DRIVER
:
1822 treatment
= reg_process_hint_driver(wiphy
, reg_request
);
1824 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1827 treatment
= reg_process_hint_country_ie(wiphy
, reg_request
);
1830 WARN(1, "invalid initiator %d\n", reg_request
->initiator
);
1834 /* This is required so that the orig_* parameters are saved */
1835 if (treatment
== REG_REQ_ALREADY_SET
&& wiphy
&&
1836 wiphy
->regulatory_flags
& REGULATORY_STRICT_REG
)
1837 wiphy_update_regulatory(wiphy
, reg_request
->initiator
);
1846 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1847 * Regulatory hints come on a first come first serve basis and we
1848 * must process each one atomically.
1850 static void reg_process_pending_hints(void)
1852 struct regulatory_request
*reg_request
, *lr
;
1854 lr
= get_last_request();
1856 /* When last_request->processed becomes true this will be rescheduled */
1857 if (lr
&& !lr
->processed
) {
1858 REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n");
1862 spin_lock(®_requests_lock
);
1864 if (list_empty(®_requests_list
)) {
1865 spin_unlock(®_requests_lock
);
1869 reg_request
= list_first_entry(®_requests_list
,
1870 struct regulatory_request
,
1872 list_del_init(®_request
->list
);
1874 spin_unlock(®_requests_lock
);
1876 reg_process_hint(reg_request
);
1879 /* Processes beacon hints -- this has nothing to do with country IEs */
1880 static void reg_process_pending_beacon_hints(void)
1882 struct cfg80211_registered_device
*rdev
;
1883 struct reg_beacon
*pending_beacon
, *tmp
;
1885 /* This goes through the _pending_ beacon list */
1886 spin_lock_bh(®_pending_beacons_lock
);
1888 list_for_each_entry_safe(pending_beacon
, tmp
,
1889 ®_pending_beacons
, list
) {
1890 list_del_init(&pending_beacon
->list
);
1892 /* Applies the beacon hint to current wiphys */
1893 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1894 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1896 /* Remembers the beacon hint for new wiphys or reg changes */
1897 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1900 spin_unlock_bh(®_pending_beacons_lock
);
1903 static void reg_todo(struct work_struct
*work
)
1906 reg_process_pending_hints();
1907 reg_process_pending_beacon_hints();
1911 static void queue_regulatory_request(struct regulatory_request
*request
)
1913 request
->alpha2
[0] = toupper(request
->alpha2
[0]);
1914 request
->alpha2
[1] = toupper(request
->alpha2
[1]);
1916 spin_lock(®_requests_lock
);
1917 list_add_tail(&request
->list
, ®_requests_list
);
1918 spin_unlock(®_requests_lock
);
1920 schedule_work(®_work
);
1924 * Core regulatory hint -- happens during cfg80211_init()
1925 * and when we restore regulatory settings.
1927 static int regulatory_hint_core(const char *alpha2
)
1929 struct regulatory_request
*request
;
1931 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1935 request
->alpha2
[0] = alpha2
[0];
1936 request
->alpha2
[1] = alpha2
[1];
1937 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1939 queue_regulatory_request(request
);
1945 int regulatory_hint_user(const char *alpha2
,
1946 enum nl80211_user_reg_hint_type user_reg_hint_type
)
1948 struct regulatory_request
*request
;
1950 if (WARN_ON(!alpha2
))
1953 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1957 request
->wiphy_idx
= WIPHY_IDX_INVALID
;
1958 request
->alpha2
[0] = alpha2
[0];
1959 request
->alpha2
[1] = alpha2
[1];
1960 request
->initiator
= NL80211_REGDOM_SET_BY_USER
;
1961 request
->user_reg_hint_type
= user_reg_hint_type
;
1963 queue_regulatory_request(request
);
1969 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1971 struct regulatory_request
*request
;
1973 if (WARN_ON(!alpha2
|| !wiphy
))
1976 wiphy
->regulatory_flags
&= ~REGULATORY_CUSTOM_REG
;
1978 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1982 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1984 request
->alpha2
[0] = alpha2
[0];
1985 request
->alpha2
[1] = alpha2
[1];
1986 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
1988 queue_regulatory_request(request
);
1992 EXPORT_SYMBOL(regulatory_hint
);
1994 void regulatory_hint_country_ie(struct wiphy
*wiphy
, enum ieee80211_band band
,
1995 const u8
*country_ie
, u8 country_ie_len
)
1998 enum environment_cap env
= ENVIRON_ANY
;
1999 struct regulatory_request
*request
= NULL
, *lr
;
2001 /* IE len must be evenly divisible by 2 */
2002 if (country_ie_len
& 0x01)
2005 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
2008 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
2012 alpha2
[0] = country_ie
[0];
2013 alpha2
[1] = country_ie
[1];
2015 if (country_ie
[2] == 'I')
2016 env
= ENVIRON_INDOOR
;
2017 else if (country_ie
[2] == 'O')
2018 env
= ENVIRON_OUTDOOR
;
2021 lr
= get_last_request();
2027 * We will run this only upon a successful connection on cfg80211.
2028 * We leave conflict resolution to the workqueue, where can hold
2031 if (lr
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
2032 lr
->wiphy_idx
!= WIPHY_IDX_INVALID
)
2035 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
2036 request
->alpha2
[0] = alpha2
[0];
2037 request
->alpha2
[1] = alpha2
[1];
2038 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
2039 request
->country_ie_env
= env
;
2041 queue_regulatory_request(request
);
2048 static void restore_alpha2(char *alpha2
, bool reset_user
)
2050 /* indicates there is no alpha2 to consider for restoration */
2054 /* The user setting has precedence over the module parameter */
2055 if (is_user_regdom_saved()) {
2056 /* Unless we're asked to ignore it and reset it */
2058 REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
2059 user_alpha2
[0] = '9';
2060 user_alpha2
[1] = '7';
2063 * If we're ignoring user settings, we still need to
2064 * check the module parameter to ensure we put things
2065 * back as they were for a full restore.
2067 if (!is_world_regdom(ieee80211_regdom
)) {
2068 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2069 ieee80211_regdom
[0], ieee80211_regdom
[1]);
2070 alpha2
[0] = ieee80211_regdom
[0];
2071 alpha2
[1] = ieee80211_regdom
[1];
2074 REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
2075 user_alpha2
[0], user_alpha2
[1]);
2076 alpha2
[0] = user_alpha2
[0];
2077 alpha2
[1] = user_alpha2
[1];
2079 } else if (!is_world_regdom(ieee80211_regdom
)) {
2080 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2081 ieee80211_regdom
[0], ieee80211_regdom
[1]);
2082 alpha2
[0] = ieee80211_regdom
[0];
2083 alpha2
[1] = ieee80211_regdom
[1];
2085 REG_DBG_PRINT("Restoring regulatory settings\n");
2088 static void restore_custom_reg_settings(struct wiphy
*wiphy
)
2090 struct ieee80211_supported_band
*sband
;
2091 enum ieee80211_band band
;
2092 struct ieee80211_channel
*chan
;
2095 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
2096 sband
= wiphy
->bands
[band
];
2099 for (i
= 0; i
< sband
->n_channels
; i
++) {
2100 chan
= &sband
->channels
[i
];
2101 chan
->flags
= chan
->orig_flags
;
2102 chan
->max_antenna_gain
= chan
->orig_mag
;
2103 chan
->max_power
= chan
->orig_mpwr
;
2104 chan
->beacon_found
= false;
2110 * Restoring regulatory settings involves ingoring any
2111 * possibly stale country IE information and user regulatory
2112 * settings if so desired, this includes any beacon hints
2113 * learned as we could have traveled outside to another country
2114 * after disconnection. To restore regulatory settings we do
2115 * exactly what we did at bootup:
2117 * - send a core regulatory hint
2118 * - send a user regulatory hint if applicable
2120 * Device drivers that send a regulatory hint for a specific country
2121 * keep their own regulatory domain on wiphy->regd so that does does
2122 * not need to be remembered.
2124 static void restore_regulatory_settings(bool reset_user
)
2127 char world_alpha2
[2];
2128 struct reg_beacon
*reg_beacon
, *btmp
;
2129 struct regulatory_request
*reg_request
, *tmp
;
2130 LIST_HEAD(tmp_reg_req_list
);
2131 struct cfg80211_registered_device
*rdev
;
2135 reset_regdomains(true, &world_regdom
);
2136 restore_alpha2(alpha2
, reset_user
);
2139 * If there's any pending requests we simply
2140 * stash them to a temporary pending queue and
2141 * add then after we've restored regulatory
2144 spin_lock(®_requests_lock
);
2145 list_for_each_entry_safe(reg_request
, tmp
, ®_requests_list
, list
) {
2146 if (reg_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
)
2148 list_move_tail(®_request
->list
, &tmp_reg_req_list
);
2150 spin_unlock(®_requests_lock
);
2152 /* Clear beacon hints */
2153 spin_lock_bh(®_pending_beacons_lock
);
2154 list_for_each_entry_safe(reg_beacon
, btmp
, ®_pending_beacons
, list
) {
2155 list_del(®_beacon
->list
);
2158 spin_unlock_bh(®_pending_beacons_lock
);
2160 list_for_each_entry_safe(reg_beacon
, btmp
, ®_beacon_list
, list
) {
2161 list_del(®_beacon
->list
);
2165 /* First restore to the basic regulatory settings */
2166 world_alpha2
[0] = cfg80211_world_regdom
->alpha2
[0];
2167 world_alpha2
[1] = cfg80211_world_regdom
->alpha2
[1];
2169 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
) {
2170 if (rdev
->wiphy
.regulatory_flags
& REGULATORY_CUSTOM_REG
)
2171 restore_custom_reg_settings(&rdev
->wiphy
);
2174 regulatory_hint_core(world_alpha2
);
2177 * This restores the ieee80211_regdom module parameter
2178 * preference or the last user requested regulatory
2179 * settings, user regulatory settings takes precedence.
2181 if (is_an_alpha2(alpha2
))
2182 regulatory_hint_user(user_alpha2
, NL80211_USER_REG_HINT_USER
);
2184 spin_lock(®_requests_lock
);
2185 list_splice_tail_init(&tmp_reg_req_list
, ®_requests_list
);
2186 spin_unlock(®_requests_lock
);
2188 REG_DBG_PRINT("Kicking the queue\n");
2190 schedule_work(®_work
);
2193 void regulatory_hint_disconnect(void)
2195 REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
2196 restore_regulatory_settings(false);
2199 static bool freq_is_chan_12_13_14(u16 freq
)
2201 if (freq
== ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ
) ||
2202 freq
== ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ
) ||
2203 freq
== ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ
))
2208 static bool pending_reg_beacon(struct ieee80211_channel
*beacon_chan
)
2210 struct reg_beacon
*pending_beacon
;
2212 list_for_each_entry(pending_beacon
, ®_pending_beacons
, list
)
2213 if (beacon_chan
->center_freq
==
2214 pending_beacon
->chan
.center_freq
)
2219 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
2220 struct ieee80211_channel
*beacon_chan
,
2223 struct reg_beacon
*reg_beacon
;
2226 if (beacon_chan
->beacon_found
||
2227 beacon_chan
->flags
& IEEE80211_CHAN_RADAR
||
2228 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
2229 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))
2232 spin_lock_bh(®_pending_beacons_lock
);
2233 processing
= pending_reg_beacon(beacon_chan
);
2234 spin_unlock_bh(®_pending_beacons_lock
);
2239 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
2243 REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
2244 beacon_chan
->center_freq
,
2245 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
2248 memcpy(®_beacon
->chan
, beacon_chan
,
2249 sizeof(struct ieee80211_channel
));
2252 * Since we can be called from BH or and non-BH context
2253 * we must use spin_lock_bh()
2255 spin_lock_bh(®_pending_beacons_lock
);
2256 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
2257 spin_unlock_bh(®_pending_beacons_lock
);
2259 schedule_work(®_work
);
2264 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
2267 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
2268 const struct ieee80211_freq_range
*freq_range
= NULL
;
2269 const struct ieee80211_power_rule
*power_rule
= NULL
;
2270 char bw
[32], cac_time
[32];
2272 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
2274 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
2275 reg_rule
= &rd
->reg_rules
[i
];
2276 freq_range
= ®_rule
->freq_range
;
2277 power_rule
= ®_rule
->power_rule
;
2279 if (reg_rule
->flags
& NL80211_RRF_AUTO_BW
)
2280 snprintf(bw
, sizeof(bw
), "%d KHz, %d KHz AUTO",
2281 freq_range
->max_bandwidth_khz
,
2282 reg_get_max_bandwidth(rd
, reg_rule
));
2284 snprintf(bw
, sizeof(bw
), "%d KHz",
2285 freq_range
->max_bandwidth_khz
);
2287 if (reg_rule
->flags
& NL80211_RRF_DFS
)
2288 scnprintf(cac_time
, sizeof(cac_time
), "%u s",
2289 reg_rule
->dfs_cac_ms
/1000);
2291 scnprintf(cac_time
, sizeof(cac_time
), "N/A");
2295 * There may not be documentation for max antenna gain
2296 * in certain regions
2298 if (power_rule
->max_antenna_gain
)
2299 pr_info(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
2300 freq_range
->start_freq_khz
,
2301 freq_range
->end_freq_khz
,
2303 power_rule
->max_antenna_gain
,
2304 power_rule
->max_eirp
,
2307 pr_info(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
2308 freq_range
->start_freq_khz
,
2309 freq_range
->end_freq_khz
,
2311 power_rule
->max_eirp
,
2316 bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region
)
2318 switch (dfs_region
) {
2319 case NL80211_DFS_UNSET
:
2320 case NL80211_DFS_FCC
:
2321 case NL80211_DFS_ETSI
:
2322 case NL80211_DFS_JP
:
2325 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
2331 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
2333 struct regulatory_request
*lr
= get_last_request();
2335 if (is_intersected_alpha2(rd
->alpha2
)) {
2336 if (lr
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2337 struct cfg80211_registered_device
*rdev
;
2338 rdev
= cfg80211_rdev_by_wiphy_idx(lr
->wiphy_idx
);
2340 pr_info("Current regulatory domain updated by AP to: %c%c\n",
2341 rdev
->country_ie_alpha2
[0],
2342 rdev
->country_ie_alpha2
[1]);
2344 pr_info("Current regulatory domain intersected:\n");
2346 pr_info("Current regulatory domain intersected:\n");
2347 } else if (is_world_regdom(rd
->alpha2
)) {
2348 pr_info("World regulatory domain updated:\n");
2350 if (is_unknown_alpha2(rd
->alpha2
))
2351 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
2353 if (reg_request_cell_base(lr
))
2354 pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
2355 rd
->alpha2
[0], rd
->alpha2
[1]);
2357 pr_info("Regulatory domain changed to country: %c%c\n",
2358 rd
->alpha2
[0], rd
->alpha2
[1]);
2362 pr_info(" DFS Master region: %s", reg_dfs_region_str(rd
->dfs_region
));
2366 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
2368 pr_info("Regulatory domain: %c%c\n", rd
->alpha2
[0], rd
->alpha2
[1]);
2372 static int reg_set_rd_core(const struct ieee80211_regdomain
*rd
)
2374 if (!is_world_regdom(rd
->alpha2
))
2376 update_world_regdomain(rd
);
2380 static int reg_set_rd_user(const struct ieee80211_regdomain
*rd
,
2381 struct regulatory_request
*user_request
)
2383 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2385 if (!regdom_changes(rd
->alpha2
))
2388 if (!is_valid_rd(rd
)) {
2389 pr_err("Invalid regulatory domain detected:\n");
2390 print_regdomain_info(rd
);
2394 if (!user_request
->intersect
) {
2395 reset_regdomains(false, rd
);
2399 intersected_rd
= regdom_intersect(rd
, get_cfg80211_regdom());
2400 if (!intersected_rd
)
2405 reset_regdomains(false, intersected_rd
);
2410 static int reg_set_rd_driver(const struct ieee80211_regdomain
*rd
,
2411 struct regulatory_request
*driver_request
)
2413 const struct ieee80211_regdomain
*regd
;
2414 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2415 const struct ieee80211_regdomain
*tmp
;
2416 struct wiphy
*request_wiphy
;
2418 if (is_world_regdom(rd
->alpha2
))
2421 if (!regdom_changes(rd
->alpha2
))
2424 if (!is_valid_rd(rd
)) {
2425 pr_err("Invalid regulatory domain detected:\n");
2426 print_regdomain_info(rd
);
2430 request_wiphy
= wiphy_idx_to_wiphy(driver_request
->wiphy_idx
);
2431 if (!request_wiphy
) {
2432 queue_delayed_work(system_power_efficient_wq
,
2437 if (!driver_request
->intersect
) {
2438 if (request_wiphy
->regd
)
2441 regd
= reg_copy_regd(rd
);
2443 return PTR_ERR(regd
);
2445 rcu_assign_pointer(request_wiphy
->regd
, regd
);
2446 reset_regdomains(false, rd
);
2450 intersected_rd
= regdom_intersect(rd
, get_cfg80211_regdom());
2451 if (!intersected_rd
)
2455 * We can trash what CRDA provided now.
2456 * However if a driver requested this specific regulatory
2457 * domain we keep it for its private use
2459 tmp
= get_wiphy_regdom(request_wiphy
);
2460 rcu_assign_pointer(request_wiphy
->regd
, rd
);
2461 rcu_free_regdom(tmp
);
2465 reset_regdomains(false, intersected_rd
);
2470 static int reg_set_rd_country_ie(const struct ieee80211_regdomain
*rd
,
2471 struct regulatory_request
*country_ie_request
)
2473 struct wiphy
*request_wiphy
;
2475 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2476 !is_unknown_alpha2(rd
->alpha2
))
2480 * Lets only bother proceeding on the same alpha2 if the current
2481 * rd is non static (it means CRDA was present and was used last)
2482 * and the pending request came in from a country IE
2485 if (!is_valid_rd(rd
)) {
2486 pr_err("Invalid regulatory domain detected:\n");
2487 print_regdomain_info(rd
);
2491 request_wiphy
= wiphy_idx_to_wiphy(country_ie_request
->wiphy_idx
);
2492 if (!request_wiphy
) {
2493 queue_delayed_work(system_power_efficient_wq
,
2498 if (country_ie_request
->intersect
)
2501 reset_regdomains(false, rd
);
2506 * Use this call to set the current regulatory domain. Conflicts with
2507 * multiple drivers can be ironed out later. Caller must've already
2508 * kmalloc'd the rd structure.
2510 int set_regdom(const struct ieee80211_regdomain
*rd
)
2512 struct regulatory_request
*lr
;
2515 if (!reg_is_valid_request(rd
->alpha2
)) {
2520 lr
= get_last_request();
2522 /* Note that this doesn't update the wiphys, this is done below */
2523 switch (lr
->initiator
) {
2524 case NL80211_REGDOM_SET_BY_CORE
:
2525 r
= reg_set_rd_core(rd
);
2527 case NL80211_REGDOM_SET_BY_USER
:
2528 r
= reg_set_rd_user(rd
, lr
);
2530 case NL80211_REGDOM_SET_BY_DRIVER
:
2531 r
= reg_set_rd_driver(rd
, lr
);
2533 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
2534 r
= reg_set_rd_country_ie(rd
, lr
);
2537 WARN(1, "invalid initiator %d\n", lr
->initiator
);
2543 reg_set_request_processed();
2549 /* This would make this whole thing pointless */
2550 if (WARN_ON(!lr
->intersect
&& rd
!= get_cfg80211_regdom()))
2553 /* update all wiphys now with the new established regulatory domain */
2554 update_all_wiphy_regulatory(lr
->initiator
);
2556 print_regdomain(get_cfg80211_regdom());
2558 nl80211_send_reg_change_event(lr
);
2560 reg_set_request_processed();
2565 void wiphy_regulatory_register(struct wiphy
*wiphy
)
2567 struct regulatory_request
*lr
;
2569 if (!reg_dev_ignore_cell_hint(wiphy
))
2570 reg_num_devs_support_basehint
++;
2572 lr
= get_last_request();
2573 wiphy_update_regulatory(wiphy
, lr
->initiator
);
2576 void wiphy_regulatory_deregister(struct wiphy
*wiphy
)
2578 struct wiphy
*request_wiphy
= NULL
;
2579 struct regulatory_request
*lr
;
2581 lr
= get_last_request();
2583 if (!reg_dev_ignore_cell_hint(wiphy
))
2584 reg_num_devs_support_basehint
--;
2586 rcu_free_regdom(get_wiphy_regdom(wiphy
));
2587 rcu_assign_pointer(wiphy
->regd
, NULL
);
2590 request_wiphy
= wiphy_idx_to_wiphy(lr
->wiphy_idx
);
2592 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2595 lr
->wiphy_idx
= WIPHY_IDX_INVALID
;
2596 lr
->country_ie_env
= ENVIRON_ANY
;
2599 static void reg_timeout_work(struct work_struct
*work
)
2601 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
2603 restore_regulatory_settings(true);
2607 int __init
regulatory_init(void)
2611 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2612 if (IS_ERR(reg_pdev
))
2613 return PTR_ERR(reg_pdev
);
2615 spin_lock_init(®_requests_lock
);
2616 spin_lock_init(®_pending_beacons_lock
);
2618 reg_regdb_size_check();
2620 rcu_assign_pointer(cfg80211_regdomain
, cfg80211_world_regdom
);
2622 user_alpha2
[0] = '9';
2623 user_alpha2
[1] = '7';
2625 /* We always try to get an update for the static regdomain */
2626 err
= regulatory_hint_core(cfg80211_world_regdom
->alpha2
);
2631 * N.B. kobject_uevent_env() can fail mainly for when we're out
2632 * memory which is handled and propagated appropriately above
2633 * but it can also fail during a netlink_broadcast() or during
2634 * early boot for call_usermodehelper(). For now treat these
2635 * errors as non-fatal.
2637 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
2641 * Finally, if the user set the module parameter treat it
2644 if (!is_world_regdom(ieee80211_regdom
))
2645 regulatory_hint_user(ieee80211_regdom
,
2646 NL80211_USER_REG_HINT_USER
);
2651 void regulatory_exit(void)
2653 struct regulatory_request
*reg_request
, *tmp
;
2654 struct reg_beacon
*reg_beacon
, *btmp
;
2656 cancel_work_sync(®_work
);
2657 cancel_delayed_work_sync(®_timeout
);
2659 /* Lock to suppress warnings */
2661 reset_regdomains(true, NULL
);
2664 dev_set_uevent_suppress(®_pdev
->dev
, true);
2666 platform_device_unregister(reg_pdev
);
2668 list_for_each_entry_safe(reg_beacon
, btmp
, ®_pending_beacons
, list
) {
2669 list_del(®_beacon
->list
);
2673 list_for_each_entry_safe(reg_beacon
, btmp
, ®_beacon_list
, list
) {
2674 list_del(®_beacon
->list
);
2678 list_for_each_entry_safe(reg_request
, tmp
, ®_requests_list
, list
) {
2679 list_del(®_request
->list
);