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...)
69 * enum reg_request_treatment - regulatory request treatment
71 * @REG_REQ_OK: continue processing the regulatory request
72 * @REG_REQ_IGNORE: ignore the regulatory request
73 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
74 * be intersected with the current one.
75 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
76 * regulatory settings, and no further processing is required.
77 * @REG_REQ_USER_HINT_HANDLED: a non alpha2 user hint was handled and no
78 * further processing is required, i.e., not need to update last_request
79 * etc. This should be used for user hints that do not provide an alpha2
80 * but some other type of regulatory hint, i.e., indoor operation.
82 enum reg_request_treatment
{
87 REG_REQ_USER_HINT_HANDLED
,
90 static struct regulatory_request core_request_world
= {
91 .initiator
= NL80211_REGDOM_SET_BY_CORE
,
96 .country_ie_env
= ENVIRON_ANY
,
100 * Receipt of information from last regulatory request,
101 * protected by RTNL (and can be accessed with RCU protection)
103 static struct regulatory_request __rcu
*last_request
=
104 (void __rcu
*)&core_request_world
;
106 /* To trigger userspace events */
107 static struct platform_device
*reg_pdev
;
110 * Central wireless core regulatory domains, we only need two,
111 * the current one and a world regulatory domain in case we have no
112 * information to give us an alpha2.
113 * (protected by RTNL, can be read under RCU)
115 const struct ieee80211_regdomain __rcu
*cfg80211_regdomain
;
118 * Number of devices that registered to the core
119 * that support cellular base station regulatory hints
120 * (protected by RTNL)
122 static int reg_num_devs_support_basehint
;
125 * State variable indicating if the platform on which the devices
126 * are attached is operating in an indoor environment. The state variable
127 * is relevant for all registered devices.
128 * (protected by RTNL)
130 static bool reg_is_indoor
;
132 static const struct ieee80211_regdomain
*get_cfg80211_regdom(void)
134 return rtnl_dereference(cfg80211_regdomain
);
137 static const struct ieee80211_regdomain
*get_wiphy_regdom(struct wiphy
*wiphy
)
139 return rtnl_dereference(wiphy
->regd
);
142 static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region
)
144 switch (dfs_region
) {
145 case NL80211_DFS_UNSET
:
147 case NL80211_DFS_FCC
:
149 case NL80211_DFS_ETSI
:
157 enum nl80211_dfs_regions
reg_get_dfs_region(struct wiphy
*wiphy
)
159 const struct ieee80211_regdomain
*regd
= NULL
;
160 const struct ieee80211_regdomain
*wiphy_regd
= NULL
;
162 regd
= get_cfg80211_regdom();
166 wiphy_regd
= get_wiphy_regdom(wiphy
);
170 if (wiphy_regd
->dfs_region
== regd
->dfs_region
)
173 REG_DBG_PRINT("%s: device specific dfs_region "
174 "(%s) disagrees with cfg80211's "
175 "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_todo(struct work_struct
*work
);
213 static DECLARE_WORK(reg_work
, reg_todo
);
215 static void reg_timeout_work(struct work_struct
*work
);
216 static DECLARE_DELAYED_WORK(reg_timeout
, reg_timeout_work
);
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, 40, 6, 20,
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, 160, 6, 20,
237 /* IEEE 802.11a, channel 52..64 - DFS required */
238 REG_RULE(5260-10, 5320+10, 160, 6, 20,
242 /* IEEE 802.11a, channel 100..144 - DFS required */
243 REG_RULE(5500-10, 5720+10, 160, 6, 20,
247 /* IEEE 802.11a, channel 149..165 */
248 REG_RULE(5745-10, 5825+10, 80, 6, 20,
251 /* IEEE 802.11ad (60gHz), channels 1..3 */
252 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
256 /* protected by RTNL */
257 static const struct ieee80211_regdomain
*cfg80211_world_regdom
=
260 static char *ieee80211_regdom
= "00";
261 static char user_alpha2
[2];
263 module_param(ieee80211_regdom
, charp
, 0444);
264 MODULE_PARM_DESC(ieee80211_regdom
, "IEEE 802.11 regulatory domain code");
266 static void reg_free_request(struct regulatory_request
*lr
)
268 if (lr
!= &core_request_world
&& lr
)
269 kfree_rcu(lr
, rcu_head
);
272 static void reg_update_last_request(struct regulatory_request
*request
)
274 struct regulatory_request
*lr
;
276 lr
= get_last_request();
280 reg_free_request(lr
);
281 rcu_assign_pointer(last_request
, request
);
284 static void reset_regdomains(bool full_reset
,
285 const struct ieee80211_regdomain
*new_regdom
)
287 const struct ieee80211_regdomain
*r
;
291 r
= get_cfg80211_regdom();
293 /* avoid freeing static information or freeing something twice */
294 if (r
== cfg80211_world_regdom
)
296 if (cfg80211_world_regdom
== &world_regdom
)
297 cfg80211_world_regdom
= NULL
;
298 if (r
== &world_regdom
)
302 rcu_free_regdom(cfg80211_world_regdom
);
304 cfg80211_world_regdom
= &world_regdom
;
305 rcu_assign_pointer(cfg80211_regdomain
, new_regdom
);
310 reg_update_last_request(&core_request_world
);
314 * Dynamic world regulatory domain requested by the wireless
315 * core upon initialization
317 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
319 struct regulatory_request
*lr
;
321 lr
= get_last_request();
325 reset_regdomains(false, rd
);
327 cfg80211_world_regdom
= rd
;
330 bool is_world_regdom(const char *alpha2
)
334 return alpha2
[0] == '0' && alpha2
[1] == '0';
337 static bool is_alpha2_set(const char *alpha2
)
341 return alpha2
[0] && alpha2
[1];
344 static bool is_unknown_alpha2(const char *alpha2
)
349 * Special case where regulatory domain was built by driver
350 * but a specific alpha2 cannot be determined
352 return alpha2
[0] == '9' && alpha2
[1] == '9';
355 static bool is_intersected_alpha2(const char *alpha2
)
360 * Special case where regulatory domain is the
361 * result of an intersection between two regulatory domain
364 return alpha2
[0] == '9' && alpha2
[1] == '8';
367 static bool is_an_alpha2(const char *alpha2
)
371 return isalpha(alpha2
[0]) && isalpha(alpha2
[1]);
374 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
376 if (!alpha2_x
|| !alpha2_y
)
378 return alpha2_x
[0] == alpha2_y
[0] && alpha2_x
[1] == alpha2_y
[1];
381 static bool regdom_changes(const char *alpha2
)
383 const struct ieee80211_regdomain
*r
= get_cfg80211_regdom();
387 return !alpha2_equal(r
->alpha2
, alpha2
);
391 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
392 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
393 * has ever been issued.
395 static bool is_user_regdom_saved(void)
397 if (user_alpha2
[0] == '9' && user_alpha2
[1] == '7')
400 /* This would indicate a mistake on the design */
401 if (WARN(!is_world_regdom(user_alpha2
) && !is_an_alpha2(user_alpha2
),
402 "Unexpected user alpha2: %c%c\n",
403 user_alpha2
[0], user_alpha2
[1]))
409 static const struct ieee80211_regdomain
*
410 reg_copy_regd(const struct ieee80211_regdomain
*src_regd
)
412 struct ieee80211_regdomain
*regd
;
417 sizeof(struct ieee80211_regdomain
) +
418 src_regd
->n_reg_rules
* sizeof(struct ieee80211_reg_rule
);
420 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
422 return ERR_PTR(-ENOMEM
);
424 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
426 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
427 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
428 sizeof(struct ieee80211_reg_rule
));
433 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
434 struct reg_regdb_search_request
{
436 struct list_head list
;
439 static LIST_HEAD(reg_regdb_search_list
);
440 static DEFINE_MUTEX(reg_regdb_search_mutex
);
442 static void reg_regdb_search(struct work_struct
*work
)
444 struct reg_regdb_search_request
*request
;
445 const struct ieee80211_regdomain
*curdom
, *regdom
= NULL
;
450 mutex_lock(®_regdb_search_mutex
);
451 while (!list_empty(®_regdb_search_list
)) {
452 request
= list_first_entry(®_regdb_search_list
,
453 struct reg_regdb_search_request
,
455 list_del(&request
->list
);
457 for (i
= 0; i
< reg_regdb_size
; i
++) {
458 curdom
= reg_regdb
[i
];
460 if (alpha2_equal(request
->alpha2
, curdom
->alpha2
)) {
461 regdom
= reg_copy_regd(curdom
);
468 mutex_unlock(®_regdb_search_mutex
);
470 if (!IS_ERR_OR_NULL(regdom
))
476 static DECLARE_WORK(reg_regdb_work
, reg_regdb_search
);
478 static void reg_regdb_query(const char *alpha2
)
480 struct reg_regdb_search_request
*request
;
485 request
= kzalloc(sizeof(struct reg_regdb_search_request
), GFP_KERNEL
);
489 memcpy(request
->alpha2
, alpha2
, 2);
491 mutex_lock(®_regdb_search_mutex
);
492 list_add_tail(&request
->list
, ®_regdb_search_list
);
493 mutex_unlock(®_regdb_search_mutex
);
495 schedule_work(®_regdb_work
);
498 /* Feel free to add any other sanity checks here */
499 static void reg_regdb_size_check(void)
501 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
502 WARN_ONCE(!reg_regdb_size
, "db.txt is empty, you should update it...");
505 static inline void reg_regdb_size_check(void) {}
506 static inline void reg_regdb_query(const char *alpha2
) {}
507 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
510 * This lets us keep regulatory code which is updated on a regulatory
511 * basis in userspace.
513 static int call_crda(const char *alpha2
)
516 char *env
[] = { country
, NULL
};
518 snprintf(country
, sizeof(country
), "COUNTRY=%c%c",
519 alpha2
[0], alpha2
[1]);
521 if (!is_world_regdom((char *) alpha2
))
522 pr_info("Calling CRDA for country: %c%c\n",
523 alpha2
[0], alpha2
[1]);
525 pr_info("Calling CRDA to update world regulatory domain\n");
527 /* query internal regulatory database (if it exists) */
528 reg_regdb_query(alpha2
);
530 return kobject_uevent_env(®_pdev
->dev
.kobj
, KOBJ_CHANGE
, env
);
533 static enum reg_request_treatment
534 reg_call_crda(struct regulatory_request
*request
)
536 if (call_crda(request
->alpha2
))
537 return REG_REQ_IGNORE
;
541 bool reg_is_valid_request(const char *alpha2
)
543 struct regulatory_request
*lr
= get_last_request();
545 if (!lr
|| lr
->processed
)
548 return alpha2_equal(lr
->alpha2
, alpha2
);
551 static const struct ieee80211_regdomain
*reg_get_regdomain(struct wiphy
*wiphy
)
553 struct regulatory_request
*lr
= get_last_request();
556 * Follow the driver's regulatory domain, if present, unless a country
557 * IE has been processed or a user wants to help complaince further
559 if (lr
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
560 lr
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
562 return get_wiphy_regdom(wiphy
);
564 return get_cfg80211_regdom();
567 unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain
*rd
,
568 const struct ieee80211_reg_rule
*rule
)
570 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
571 const struct ieee80211_freq_range
*freq_range_tmp
;
572 const struct ieee80211_reg_rule
*tmp
;
573 u32 start_freq
, end_freq
, idx
, no
;
575 for (idx
= 0; idx
< rd
->n_reg_rules
; idx
++)
576 if (rule
== &rd
->reg_rules
[idx
])
579 if (idx
== rd
->n_reg_rules
)
586 tmp
= &rd
->reg_rules
[--no
];
587 freq_range_tmp
= &tmp
->freq_range
;
589 if (freq_range_tmp
->end_freq_khz
< freq_range
->start_freq_khz
)
592 freq_range
= freq_range_tmp
;
595 start_freq
= freq_range
->start_freq_khz
;
598 freq_range
= &rule
->freq_range
;
601 while (no
< rd
->n_reg_rules
- 1) {
602 tmp
= &rd
->reg_rules
[++no
];
603 freq_range_tmp
= &tmp
->freq_range
;
605 if (freq_range_tmp
->start_freq_khz
> freq_range
->end_freq_khz
)
608 freq_range
= freq_range_tmp
;
611 end_freq
= freq_range
->end_freq_khz
;
613 return end_freq
- start_freq
;
616 /* Sanity check on a regulatory rule */
617 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
619 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
622 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
625 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
628 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
630 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
631 freq_range
->max_bandwidth_khz
> freq_diff
)
637 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
639 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
642 if (!rd
->n_reg_rules
)
645 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
648 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
649 reg_rule
= &rd
->reg_rules
[i
];
650 if (!is_valid_reg_rule(reg_rule
))
657 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
658 u32 center_freq_khz
, u32 bw_khz
)
660 u32 start_freq_khz
, end_freq_khz
;
662 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
663 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
665 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
666 end_freq_khz
<= freq_range
->end_freq_khz
)
673 * freq_in_rule_band - tells us if a frequency is in a frequency band
674 * @freq_range: frequency rule we want to query
675 * @freq_khz: frequency we are inquiring about
677 * This lets us know if a specific frequency rule is or is not relevant to
678 * a specific frequency's band. Bands are device specific and artificial
679 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
680 * however it is safe for now to assume that a frequency rule should not be
681 * part of a frequency's band if the start freq or end freq are off by more
682 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
684 * This resolution can be lowered and should be considered as we add
685 * regulatory rule support for other "bands".
687 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
690 #define ONE_GHZ_IN_KHZ 1000000
692 * From 802.11ad: directional multi-gigabit (DMG):
693 * Pertaining to operation in a frequency band containing a channel
694 * with the Channel starting frequency above 45 GHz.
696 u32 limit
= freq_khz
> 45 * ONE_GHZ_IN_KHZ
?
697 10 * ONE_GHZ_IN_KHZ
: 2 * ONE_GHZ_IN_KHZ
;
698 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= limit
)
700 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= limit
)
703 #undef ONE_GHZ_IN_KHZ
707 * Later on we can perhaps use the more restrictive DFS
708 * region but we don't have information for that yet so
709 * for now simply disallow conflicts.
711 static enum nl80211_dfs_regions
712 reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1
,
713 const enum nl80211_dfs_regions dfs_region2
)
715 if (dfs_region1
!= dfs_region2
)
716 return NL80211_DFS_UNSET
;
721 * Helper for regdom_intersect(), this does the real
722 * mathematical intersection fun
724 static int reg_rules_intersect(const struct ieee80211_regdomain
*rd1
,
725 const struct ieee80211_regdomain
*rd2
,
726 const struct ieee80211_reg_rule
*rule1
,
727 const struct ieee80211_reg_rule
*rule2
,
728 struct ieee80211_reg_rule
*intersected_rule
)
730 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
731 struct ieee80211_freq_range
*freq_range
;
732 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
733 struct ieee80211_power_rule
*power_rule
;
734 u32 freq_diff
, max_bandwidth1
, max_bandwidth2
;
736 freq_range1
= &rule1
->freq_range
;
737 freq_range2
= &rule2
->freq_range
;
738 freq_range
= &intersected_rule
->freq_range
;
740 power_rule1
= &rule1
->power_rule
;
741 power_rule2
= &rule2
->power_rule
;
742 power_rule
= &intersected_rule
->power_rule
;
744 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
745 freq_range2
->start_freq_khz
);
746 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
747 freq_range2
->end_freq_khz
);
749 max_bandwidth1
= freq_range1
->max_bandwidth_khz
;
750 max_bandwidth2
= freq_range2
->max_bandwidth_khz
;
752 if (rule1
->flags
& NL80211_RRF_AUTO_BW
)
753 max_bandwidth1
= reg_get_max_bandwidth(rd1
, rule1
);
754 if (rule2
->flags
& NL80211_RRF_AUTO_BW
)
755 max_bandwidth2
= reg_get_max_bandwidth(rd2
, rule2
);
757 freq_range
->max_bandwidth_khz
= min(max_bandwidth1
, max_bandwidth2
);
759 intersected_rule
->flags
= rule1
->flags
| rule2
->flags
;
762 * In case NL80211_RRF_AUTO_BW requested for both rules
763 * set AUTO_BW in intersected rule also. Next we will
764 * calculate BW correctly in handle_channel function.
765 * In other case remove AUTO_BW flag while we calculate
766 * maximum bandwidth correctly and auto calculation is
769 if ((rule1
->flags
& NL80211_RRF_AUTO_BW
) &&
770 (rule2
->flags
& NL80211_RRF_AUTO_BW
))
771 intersected_rule
->flags
|= NL80211_RRF_AUTO_BW
;
773 intersected_rule
->flags
&= ~NL80211_RRF_AUTO_BW
;
775 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
776 if (freq_range
->max_bandwidth_khz
> freq_diff
)
777 freq_range
->max_bandwidth_khz
= freq_diff
;
779 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
780 power_rule2
->max_eirp
);
781 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
782 power_rule2
->max_antenna_gain
);
784 intersected_rule
->dfs_cac_ms
= max(rule1
->dfs_cac_ms
,
787 if (!is_valid_reg_rule(intersected_rule
))
794 * regdom_intersect - do the intersection between two regulatory domains
795 * @rd1: first regulatory domain
796 * @rd2: second regulatory domain
798 * Use this function to get the intersection between two regulatory domains.
799 * Once completed we will mark the alpha2 for the rd as intersected, "98",
800 * as no one single alpha2 can represent this regulatory domain.
802 * Returns a pointer to the regulatory domain structure which will hold the
803 * resulting intersection of rules between rd1 and rd2. We will
804 * kzalloc() this structure for you.
806 static struct ieee80211_regdomain
*
807 regdom_intersect(const struct ieee80211_regdomain
*rd1
,
808 const struct ieee80211_regdomain
*rd2
)
812 unsigned int num_rules
= 0, rule_idx
= 0;
813 const struct ieee80211_reg_rule
*rule1
, *rule2
;
814 struct ieee80211_reg_rule
*intersected_rule
;
815 struct ieee80211_regdomain
*rd
;
816 /* This is just a dummy holder to help us count */
817 struct ieee80211_reg_rule dummy_rule
;
823 * First we get a count of the rules we'll need, then we actually
824 * build them. This is to so we can malloc() and free() a
825 * regdomain once. The reason we use reg_rules_intersect() here
826 * is it will return -EINVAL if the rule computed makes no sense.
827 * All rules that do check out OK are valid.
830 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
831 rule1
= &rd1
->reg_rules
[x
];
832 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
833 rule2
= &rd2
->reg_rules
[y
];
834 if (!reg_rules_intersect(rd1
, rd2
, rule1
, rule2
,
843 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
844 num_rules
* sizeof(struct ieee80211_reg_rule
);
846 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
850 for (x
= 0; x
< rd1
->n_reg_rules
&& rule_idx
< num_rules
; x
++) {
851 rule1
= &rd1
->reg_rules
[x
];
852 for (y
= 0; y
< rd2
->n_reg_rules
&& rule_idx
< num_rules
; y
++) {
853 rule2
= &rd2
->reg_rules
[y
];
855 * This time around instead of using the stack lets
856 * write to the target rule directly saving ourselves
859 intersected_rule
= &rd
->reg_rules
[rule_idx
];
860 r
= reg_rules_intersect(rd1
, rd2
, rule1
, rule2
,
863 * No need to memset here the intersected rule here as
864 * we're not using the stack anymore
872 if (rule_idx
!= num_rules
) {
877 rd
->n_reg_rules
= num_rules
;
880 rd
->dfs_region
= reg_intersect_dfs_region(rd1
->dfs_region
,
887 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
888 * want to just have the channel structure use these
890 static u32
map_regdom_flags(u32 rd_flags
)
892 u32 channel_flags
= 0;
893 if (rd_flags
& NL80211_RRF_NO_IR_ALL
)
894 channel_flags
|= IEEE80211_CHAN_NO_IR
;
895 if (rd_flags
& NL80211_RRF_DFS
)
896 channel_flags
|= IEEE80211_CHAN_RADAR
;
897 if (rd_flags
& NL80211_RRF_NO_OFDM
)
898 channel_flags
|= IEEE80211_CHAN_NO_OFDM
;
899 if (rd_flags
& NL80211_RRF_NO_OUTDOOR
)
900 channel_flags
|= IEEE80211_CHAN_INDOOR_ONLY
;
901 return channel_flags
;
904 static const struct ieee80211_reg_rule
*
905 freq_reg_info_regd(struct wiphy
*wiphy
, u32 center_freq
,
906 const struct ieee80211_regdomain
*regd
)
909 bool band_rule_found
= false;
910 bool bw_fits
= false;
913 return ERR_PTR(-EINVAL
);
915 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
916 const struct ieee80211_reg_rule
*rr
;
917 const struct ieee80211_freq_range
*fr
= NULL
;
919 rr
= ®d
->reg_rules
[i
];
920 fr
= &rr
->freq_range
;
923 * We only need to know if one frequency rule was
924 * was in center_freq's band, that's enough, so lets
925 * not overwrite it once found
927 if (!band_rule_found
)
928 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
930 bw_fits
= reg_does_bw_fit(fr
, center_freq
, MHZ_TO_KHZ(20));
932 if (band_rule_found
&& bw_fits
)
936 if (!band_rule_found
)
937 return ERR_PTR(-ERANGE
);
939 return ERR_PTR(-EINVAL
);
942 const struct ieee80211_reg_rule
*freq_reg_info(struct wiphy
*wiphy
,
945 const struct ieee80211_regdomain
*regd
;
947 regd
= reg_get_regdomain(wiphy
);
949 return freq_reg_info_regd(wiphy
, center_freq
, regd
);
951 EXPORT_SYMBOL(freq_reg_info
);
953 const char *reg_initiator_name(enum nl80211_reg_initiator initiator
)
956 case NL80211_REGDOM_SET_BY_CORE
:
958 case NL80211_REGDOM_SET_BY_USER
:
960 case NL80211_REGDOM_SET_BY_DRIVER
:
962 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
969 EXPORT_SYMBOL(reg_initiator_name
);
971 #ifdef CONFIG_CFG80211_REG_DEBUG
972 static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain
*regd
,
973 struct ieee80211_channel
*chan
,
974 const struct ieee80211_reg_rule
*reg_rule
)
976 const struct ieee80211_power_rule
*power_rule
;
977 const struct ieee80211_freq_range
*freq_range
;
978 char max_antenna_gain
[32], bw
[32];
980 power_rule
= ®_rule
->power_rule
;
981 freq_range
= ®_rule
->freq_range
;
983 if (!power_rule
->max_antenna_gain
)
984 snprintf(max_antenna_gain
, sizeof(max_antenna_gain
), "N/A");
986 snprintf(max_antenna_gain
, sizeof(max_antenna_gain
), "%d",
987 power_rule
->max_antenna_gain
);
989 if (reg_rule
->flags
& NL80211_RRF_AUTO_BW
)
990 snprintf(bw
, sizeof(bw
), "%d KHz, %d KHz AUTO",
991 freq_range
->max_bandwidth_khz
,
992 reg_get_max_bandwidth(regd
, reg_rule
));
994 snprintf(bw
, sizeof(bw
), "%d KHz",
995 freq_range
->max_bandwidth_khz
);
997 REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
1000 REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)\n",
1001 freq_range
->start_freq_khz
, freq_range
->end_freq_khz
,
1002 bw
, max_antenna_gain
,
1003 power_rule
->max_eirp
);
1006 static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain
*regd
,
1007 struct ieee80211_channel
*chan
,
1008 const struct ieee80211_reg_rule
*reg_rule
)
1015 * Note that right now we assume the desired channel bandwidth
1016 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1017 * per channel, the primary and the extension channel).
1019 static void handle_channel(struct wiphy
*wiphy
,
1020 enum nl80211_reg_initiator initiator
,
1021 struct ieee80211_channel
*chan
)
1023 u32 flags
, bw_flags
= 0;
1024 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1025 const struct ieee80211_power_rule
*power_rule
= NULL
;
1026 const struct ieee80211_freq_range
*freq_range
= NULL
;
1027 struct wiphy
*request_wiphy
= NULL
;
1028 struct regulatory_request
*lr
= get_last_request();
1029 const struct ieee80211_regdomain
*regd
;
1030 u32 max_bandwidth_khz
;
1032 request_wiphy
= wiphy_idx_to_wiphy(lr
->wiphy_idx
);
1034 flags
= chan
->orig_flags
;
1036 reg_rule
= freq_reg_info(wiphy
, MHZ_TO_KHZ(chan
->center_freq
));
1037 if (IS_ERR(reg_rule
)) {
1039 * We will disable all channels that do not match our
1040 * received regulatory rule unless the hint is coming
1041 * from a Country IE and the Country IE had no information
1042 * about a band. The IEEE 802.11 spec allows for an AP
1043 * to send only a subset of the regulatory rules allowed,
1044 * so an AP in the US that only supports 2.4 GHz may only send
1045 * a country IE with information for the 2.4 GHz band
1046 * while 5 GHz is still supported.
1048 if (initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1049 PTR_ERR(reg_rule
) == -ERANGE
)
1052 if (lr
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1053 request_wiphy
&& request_wiphy
== wiphy
&&
1054 request_wiphy
->regulatory_flags
& REGULATORY_STRICT_REG
) {
1055 REG_DBG_PRINT("Disabling freq %d MHz for good\n",
1057 chan
->orig_flags
|= IEEE80211_CHAN_DISABLED
;
1058 chan
->flags
= chan
->orig_flags
;
1060 REG_DBG_PRINT("Disabling freq %d MHz\n",
1062 chan
->flags
|= IEEE80211_CHAN_DISABLED
;
1067 regd
= reg_get_regdomain(wiphy
);
1068 chan_reg_rule_print_dbg(regd
, chan
, reg_rule
);
1070 power_rule
= ®_rule
->power_rule
;
1071 freq_range
= ®_rule
->freq_range
;
1073 max_bandwidth_khz
= freq_range
->max_bandwidth_khz
;
1074 /* Check if auto calculation requested */
1075 if (reg_rule
->flags
& NL80211_RRF_AUTO_BW
)
1076 max_bandwidth_khz
= reg_get_max_bandwidth(regd
, reg_rule
);
1078 if (max_bandwidth_khz
< MHZ_TO_KHZ(40))
1079 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1080 if (max_bandwidth_khz
< MHZ_TO_KHZ(80))
1081 bw_flags
|= IEEE80211_CHAN_NO_80MHZ
;
1082 if (max_bandwidth_khz
< MHZ_TO_KHZ(160))
1083 bw_flags
|= IEEE80211_CHAN_NO_160MHZ
;
1085 if (lr
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1086 request_wiphy
&& request_wiphy
== wiphy
&&
1087 request_wiphy
->regulatory_flags
& REGULATORY_STRICT_REG
) {
1089 * This guarantees the driver's requested regulatory domain
1090 * will always be used as a base for further regulatory
1093 chan
->flags
= chan
->orig_flags
=
1094 map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1095 chan
->max_antenna_gain
= chan
->orig_mag
=
1096 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1097 chan
->max_reg_power
= chan
->max_power
= chan
->orig_mpwr
=
1098 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1102 chan
->dfs_state
= NL80211_DFS_USABLE
;
1103 chan
->dfs_state_entered
= jiffies
;
1105 chan
->beacon_found
= false;
1106 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
1107 chan
->max_antenna_gain
=
1108 min_t(int, chan
->orig_mag
,
1109 MBI_TO_DBI(power_rule
->max_antenna_gain
));
1110 chan
->max_reg_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1112 if (chan
->flags
& IEEE80211_CHAN_RADAR
) {
1113 if (reg_rule
->dfs_cac_ms
)
1114 chan
->dfs_cac_ms
= reg_rule
->dfs_cac_ms
;
1116 chan
->dfs_cac_ms
= IEEE80211_DFS_MIN_CAC_TIME_MS
;
1119 if (chan
->orig_mpwr
) {
1121 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1122 * will always follow the passed country IE power settings.
1124 if (initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1125 wiphy
->regulatory_flags
& REGULATORY_COUNTRY_IE_FOLLOW_POWER
)
1126 chan
->max_power
= chan
->max_reg_power
;
1128 chan
->max_power
= min(chan
->orig_mpwr
,
1129 chan
->max_reg_power
);
1131 chan
->max_power
= chan
->max_reg_power
;
1134 static void handle_band(struct wiphy
*wiphy
,
1135 enum nl80211_reg_initiator initiator
,
1136 struct ieee80211_supported_band
*sband
)
1143 for (i
= 0; i
< sband
->n_channels
; i
++)
1144 handle_channel(wiphy
, initiator
, &sband
->channels
[i
]);
1147 static bool reg_request_cell_base(struct regulatory_request
*request
)
1149 if (request
->initiator
!= NL80211_REGDOM_SET_BY_USER
)
1151 return request
->user_reg_hint_type
== NL80211_USER_REG_HINT_CELL_BASE
;
1154 static bool reg_request_indoor(struct regulatory_request
*request
)
1156 if (request
->initiator
!= NL80211_REGDOM_SET_BY_USER
)
1158 return request
->user_reg_hint_type
== NL80211_USER_REG_HINT_INDOOR
;
1161 bool reg_last_request_cell_base(void)
1163 return reg_request_cell_base(get_last_request());
1166 #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
1167 /* Core specific check */
1168 static enum reg_request_treatment
1169 reg_ignore_cell_hint(struct regulatory_request
*pending_request
)
1171 struct regulatory_request
*lr
= get_last_request();
1173 if (!reg_num_devs_support_basehint
)
1174 return REG_REQ_IGNORE
;
1176 if (reg_request_cell_base(lr
) &&
1177 !regdom_changes(pending_request
->alpha2
))
1178 return REG_REQ_ALREADY_SET
;
1183 /* Device specific check */
1184 static bool reg_dev_ignore_cell_hint(struct wiphy
*wiphy
)
1186 return !(wiphy
->features
& NL80211_FEATURE_CELL_BASE_REG_HINTS
);
1189 static int reg_ignore_cell_hint(struct regulatory_request
*pending_request
)
1191 return REG_REQ_IGNORE
;
1194 static bool reg_dev_ignore_cell_hint(struct wiphy
*wiphy
)
1200 static bool wiphy_strict_alpha2_regd(struct wiphy
*wiphy
)
1202 if (wiphy
->regulatory_flags
& REGULATORY_STRICT_REG
&&
1203 !(wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
))
1208 static bool ignore_reg_update(struct wiphy
*wiphy
,
1209 enum nl80211_reg_initiator initiator
)
1211 struct regulatory_request
*lr
= get_last_request();
1214 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1215 "since last_request is not set\n",
1216 reg_initiator_name(initiator
));
1220 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
1221 wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
) {
1222 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1223 "since the driver uses its own custom "
1224 "regulatory domain\n",
1225 reg_initiator_name(initiator
));
1230 * wiphy->regd will be set once the device has its own
1231 * desired regulatory domain set
1233 if (wiphy_strict_alpha2_regd(wiphy
) && !wiphy
->regd
&&
1234 initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1235 !is_world_regdom(lr
->alpha2
)) {
1236 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1237 "since the driver requires its own regulatory "
1238 "domain to be set first\n",
1239 reg_initiator_name(initiator
));
1243 if (reg_request_cell_base(lr
))
1244 return reg_dev_ignore_cell_hint(wiphy
);
1249 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1251 const struct ieee80211_regdomain
*cr
= get_cfg80211_regdom();
1252 const struct ieee80211_regdomain
*wr
= get_wiphy_regdom(wiphy
);
1253 struct regulatory_request
*lr
= get_last_request();
1255 if (is_world_regdom(cr
->alpha2
) || (wr
&& is_world_regdom(wr
->alpha2
)))
1258 if (lr
&& lr
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1259 wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
)
1265 static void handle_reg_beacon(struct wiphy
*wiphy
, unsigned int chan_idx
,
1266 struct reg_beacon
*reg_beacon
)
1268 struct ieee80211_supported_band
*sband
;
1269 struct ieee80211_channel
*chan
;
1270 bool channel_changed
= false;
1271 struct ieee80211_channel chan_before
;
1273 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1274 chan
= &sband
->channels
[chan_idx
];
1276 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
1279 if (chan
->beacon_found
)
1282 chan
->beacon_found
= true;
1284 if (!reg_is_world_roaming(wiphy
))
1287 if (wiphy
->regulatory_flags
& REGULATORY_DISABLE_BEACON_HINTS
)
1290 chan_before
.center_freq
= chan
->center_freq
;
1291 chan_before
.flags
= chan
->flags
;
1293 if (chan
->flags
& IEEE80211_CHAN_NO_IR
) {
1294 chan
->flags
&= ~IEEE80211_CHAN_NO_IR
;
1295 channel_changed
= true;
1298 if (channel_changed
)
1299 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
1303 * Called when a scan on a wiphy finds a beacon on
1306 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
1307 struct reg_beacon
*reg_beacon
)
1310 struct ieee80211_supported_band
*sband
;
1312 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1315 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1317 for (i
= 0; i
< sband
->n_channels
; i
++)
1318 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1322 * Called upon reg changes or a new wiphy is added
1324 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1327 struct ieee80211_supported_band
*sband
;
1328 struct reg_beacon
*reg_beacon
;
1330 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1331 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1333 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1334 for (i
= 0; i
< sband
->n_channels
; i
++)
1335 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1339 /* Reap the advantages of previously found beacons */
1340 static void reg_process_beacons(struct wiphy
*wiphy
)
1343 * Means we are just firing up cfg80211, so no beacons would
1344 * have been processed yet.
1348 wiphy_update_beacon_reg(wiphy
);
1351 static bool is_ht40_allowed(struct ieee80211_channel
*chan
)
1355 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1357 /* This would happen when regulatory rules disallow HT40 completely */
1358 if ((chan
->flags
& IEEE80211_CHAN_NO_HT40
) == IEEE80211_CHAN_NO_HT40
)
1363 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1364 struct ieee80211_channel
*channel
)
1366 struct ieee80211_supported_band
*sband
= wiphy
->bands
[channel
->band
];
1367 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1370 if (!is_ht40_allowed(channel
)) {
1371 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1376 * We need to ensure the extension channels exist to
1377 * be able to use HT40- or HT40+, this finds them (or not)
1379 for (i
= 0; i
< sband
->n_channels
; i
++) {
1380 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1382 if (c
->center_freq
== (channel
->center_freq
- 20))
1384 if (c
->center_freq
== (channel
->center_freq
+ 20))
1389 * Please note that this assumes target bandwidth is 20 MHz,
1390 * if that ever changes we also need to change the below logic
1391 * to include that as well.
1393 if (!is_ht40_allowed(channel_before
))
1394 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1396 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1398 if (!is_ht40_allowed(channel_after
))
1399 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1401 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1404 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1405 struct ieee80211_supported_band
*sband
)
1412 for (i
= 0; i
< sband
->n_channels
; i
++)
1413 reg_process_ht_flags_channel(wiphy
, &sband
->channels
[i
]);
1416 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1418 enum ieee80211_band band
;
1423 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
1424 reg_process_ht_flags_band(wiphy
, wiphy
->bands
[band
]);
1427 static void reg_call_notifier(struct wiphy
*wiphy
,
1428 struct regulatory_request
*request
)
1430 if (wiphy
->reg_notifier
)
1431 wiphy
->reg_notifier(wiphy
, request
);
1434 static void wiphy_update_regulatory(struct wiphy
*wiphy
,
1435 enum nl80211_reg_initiator initiator
)
1437 enum ieee80211_band band
;
1438 struct regulatory_request
*lr
= get_last_request();
1440 if (ignore_reg_update(wiphy
, initiator
)) {
1442 * Regulatory updates set by CORE are ignored for custom
1443 * regulatory cards. Let us notify the changes to the driver,
1444 * as some drivers used this to restore its orig_* reg domain.
1446 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
1447 wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
)
1448 reg_call_notifier(wiphy
, lr
);
1452 lr
->dfs_region
= get_cfg80211_regdom()->dfs_region
;
1454 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
1455 handle_band(wiphy
, initiator
, wiphy
->bands
[band
]);
1457 reg_process_beacons(wiphy
);
1458 reg_process_ht_flags(wiphy
);
1459 reg_call_notifier(wiphy
, lr
);
1462 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
1464 struct cfg80211_registered_device
*rdev
;
1465 struct wiphy
*wiphy
;
1469 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
) {
1470 wiphy
= &rdev
->wiphy
;
1471 wiphy_update_regulatory(wiphy
, initiator
);
1475 static void handle_channel_custom(struct wiphy
*wiphy
,
1476 struct ieee80211_channel
*chan
,
1477 const struct ieee80211_regdomain
*regd
)
1480 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1481 const struct ieee80211_power_rule
*power_rule
= NULL
;
1482 const struct ieee80211_freq_range
*freq_range
= NULL
;
1483 u32 max_bandwidth_khz
;
1485 reg_rule
= freq_reg_info_regd(wiphy
, MHZ_TO_KHZ(chan
->center_freq
),
1488 if (IS_ERR(reg_rule
)) {
1489 REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1491 chan
->orig_flags
|= IEEE80211_CHAN_DISABLED
;
1492 chan
->flags
= chan
->orig_flags
;
1496 chan_reg_rule_print_dbg(regd
, chan
, reg_rule
);
1498 power_rule
= ®_rule
->power_rule
;
1499 freq_range
= ®_rule
->freq_range
;
1501 max_bandwidth_khz
= freq_range
->max_bandwidth_khz
;
1502 /* Check if auto calculation requested */
1503 if (reg_rule
->flags
& NL80211_RRF_AUTO_BW
)
1504 max_bandwidth_khz
= reg_get_max_bandwidth(regd
, reg_rule
);
1506 if (max_bandwidth_khz
< MHZ_TO_KHZ(40))
1507 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1508 if (max_bandwidth_khz
< MHZ_TO_KHZ(80))
1509 bw_flags
|= IEEE80211_CHAN_NO_80MHZ
;
1510 if (max_bandwidth_khz
< MHZ_TO_KHZ(160))
1511 bw_flags
|= IEEE80211_CHAN_NO_160MHZ
;
1513 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1514 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1515 chan
->max_reg_power
= chan
->max_power
=
1516 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1519 static void handle_band_custom(struct wiphy
*wiphy
,
1520 struct ieee80211_supported_band
*sband
,
1521 const struct ieee80211_regdomain
*regd
)
1528 for (i
= 0; i
< sband
->n_channels
; i
++)
1529 handle_channel_custom(wiphy
, &sband
->channels
[i
], regd
);
1532 /* Used by drivers prior to wiphy registration */
1533 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1534 const struct ieee80211_regdomain
*regd
)
1536 enum ieee80211_band band
;
1537 unsigned int bands_set
= 0;
1539 WARN(!(wiphy
->regulatory_flags
& REGULATORY_CUSTOM_REG
),
1540 "wiphy should have REGULATORY_CUSTOM_REG\n");
1541 wiphy
->regulatory_flags
|= REGULATORY_CUSTOM_REG
;
1543 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1544 if (!wiphy
->bands
[band
])
1546 handle_band_custom(wiphy
, wiphy
->bands
[band
], regd
);
1551 * no point in calling this if it won't have any effect
1552 * on your device's supported bands.
1554 WARN_ON(!bands_set
);
1556 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1558 static void reg_set_request_processed(void)
1560 bool need_more_processing
= false;
1561 struct regulatory_request
*lr
= get_last_request();
1563 lr
->processed
= true;
1565 spin_lock(®_requests_lock
);
1566 if (!list_empty(®_requests_list
))
1567 need_more_processing
= true;
1568 spin_unlock(®_requests_lock
);
1570 if (lr
->initiator
== NL80211_REGDOM_SET_BY_USER
)
1571 cancel_delayed_work(®_timeout
);
1573 if (need_more_processing
)
1574 schedule_work(®_work
);
1578 * reg_process_hint_core - process core regulatory requests
1579 * @pending_request: a pending core regulatory request
1581 * The wireless subsystem can use this function to process
1582 * a regulatory request issued by the regulatory core.
1584 * Returns one of the different reg request treatment values.
1586 static enum reg_request_treatment
1587 reg_process_hint_core(struct regulatory_request
*core_request
)
1590 core_request
->intersect
= false;
1591 core_request
->processed
= false;
1593 reg_update_last_request(core_request
);
1595 return reg_call_crda(core_request
);
1598 static enum reg_request_treatment
1599 __reg_process_hint_user(struct regulatory_request
*user_request
)
1601 struct regulatory_request
*lr
= get_last_request();
1603 if (reg_request_indoor(user_request
)) {
1604 reg_is_indoor
= true;
1605 return REG_REQ_USER_HINT_HANDLED
;
1608 if (reg_request_cell_base(user_request
))
1609 return reg_ignore_cell_hint(user_request
);
1611 if (reg_request_cell_base(lr
))
1612 return REG_REQ_IGNORE
;
1614 if (lr
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1615 return REG_REQ_INTERSECT
;
1617 * If the user knows better the user should set the regdom
1618 * to their country before the IE is picked up
1620 if (lr
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1622 return REG_REQ_IGNORE
;
1624 * Process user requests only after previous user/driver/core
1625 * requests have been processed
1627 if ((lr
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1628 lr
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1629 lr
->initiator
== NL80211_REGDOM_SET_BY_USER
) &&
1630 regdom_changes(lr
->alpha2
))
1631 return REG_REQ_IGNORE
;
1633 if (!regdom_changes(user_request
->alpha2
))
1634 return REG_REQ_ALREADY_SET
;
1640 * reg_process_hint_user - process user regulatory requests
1641 * @user_request: a pending user regulatory request
1643 * The wireless subsystem can use this function to process
1644 * a regulatory request initiated by userspace.
1646 * Returns one of the different reg request treatment values.
1648 static enum reg_request_treatment
1649 reg_process_hint_user(struct regulatory_request
*user_request
)
1651 enum reg_request_treatment treatment
;
1653 treatment
= __reg_process_hint_user(user_request
);
1654 if (treatment
== REG_REQ_IGNORE
||
1655 treatment
== REG_REQ_ALREADY_SET
||
1656 treatment
== REG_REQ_USER_HINT_HANDLED
) {
1657 kfree(user_request
);
1661 user_request
->intersect
= treatment
== REG_REQ_INTERSECT
;
1662 user_request
->processed
= false;
1664 reg_update_last_request(user_request
);
1666 user_alpha2
[0] = user_request
->alpha2
[0];
1667 user_alpha2
[1] = user_request
->alpha2
[1];
1669 return reg_call_crda(user_request
);
1672 static enum reg_request_treatment
1673 __reg_process_hint_driver(struct regulatory_request
*driver_request
)
1675 struct regulatory_request
*lr
= get_last_request();
1677 if (lr
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1678 if (regdom_changes(driver_request
->alpha2
))
1680 return REG_REQ_ALREADY_SET
;
1684 * This would happen if you unplug and plug your card
1685 * back in or if you add a new device for which the previously
1686 * loaded card also agrees on the regulatory domain.
1688 if (lr
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1689 !regdom_changes(driver_request
->alpha2
))
1690 return REG_REQ_ALREADY_SET
;
1692 return REG_REQ_INTERSECT
;
1696 * reg_process_hint_driver - process driver regulatory requests
1697 * @driver_request: a pending driver regulatory request
1699 * The wireless subsystem can use this function to process
1700 * a regulatory request issued by an 802.11 driver.
1702 * Returns one of the different reg request treatment values.
1704 static enum reg_request_treatment
1705 reg_process_hint_driver(struct wiphy
*wiphy
,
1706 struct regulatory_request
*driver_request
)
1708 const struct ieee80211_regdomain
*regd
;
1709 enum reg_request_treatment treatment
;
1711 treatment
= __reg_process_hint_driver(driver_request
);
1713 switch (treatment
) {
1716 case REG_REQ_IGNORE
:
1717 case REG_REQ_USER_HINT_HANDLED
:
1718 kfree(driver_request
);
1720 case REG_REQ_INTERSECT
:
1722 case REG_REQ_ALREADY_SET
:
1723 regd
= reg_copy_regd(get_cfg80211_regdom());
1725 kfree(driver_request
);
1726 return REG_REQ_IGNORE
;
1728 rcu_assign_pointer(wiphy
->regd
, regd
);
1732 driver_request
->intersect
= treatment
== REG_REQ_INTERSECT
;
1733 driver_request
->processed
= false;
1735 reg_update_last_request(driver_request
);
1738 * Since CRDA will not be called in this case as we already
1739 * have applied the requested regulatory domain before we just
1740 * inform userspace we have processed the request
1742 if (treatment
== REG_REQ_ALREADY_SET
) {
1743 nl80211_send_reg_change_event(driver_request
);
1744 reg_set_request_processed();
1748 return reg_call_crda(driver_request
);
1751 static enum reg_request_treatment
1752 __reg_process_hint_country_ie(struct wiphy
*wiphy
,
1753 struct regulatory_request
*country_ie_request
)
1755 struct wiphy
*last_wiphy
= NULL
;
1756 struct regulatory_request
*lr
= get_last_request();
1758 if (reg_request_cell_base(lr
)) {
1759 /* Trust a Cell base station over the AP's country IE */
1760 if (regdom_changes(country_ie_request
->alpha2
))
1761 return REG_REQ_IGNORE
;
1762 return REG_REQ_ALREADY_SET
;
1764 if (wiphy
->regulatory_flags
& REGULATORY_COUNTRY_IE_IGNORE
)
1765 return REG_REQ_IGNORE
;
1768 if (unlikely(!is_an_alpha2(country_ie_request
->alpha2
)))
1771 if (lr
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1774 last_wiphy
= wiphy_idx_to_wiphy(lr
->wiphy_idx
);
1776 if (last_wiphy
!= wiphy
) {
1778 * Two cards with two APs claiming different
1779 * Country IE alpha2s. We could
1780 * intersect them, but that seems unlikely
1781 * to be correct. Reject second one for now.
1783 if (regdom_changes(country_ie_request
->alpha2
))
1784 return REG_REQ_IGNORE
;
1785 return REG_REQ_ALREADY_SET
;
1788 * Two consecutive Country IE hints on the same wiphy.
1789 * This should be picked up early by the driver/stack
1791 if (WARN_ON(regdom_changes(country_ie_request
->alpha2
)))
1793 return REG_REQ_ALREADY_SET
;
1797 * reg_process_hint_country_ie - process regulatory requests from country IEs
1798 * @country_ie_request: a regulatory request from a country IE
1800 * The wireless subsystem can use this function to process
1801 * a regulatory request issued by a country Information Element.
1803 * Returns one of the different reg request treatment values.
1805 static enum reg_request_treatment
1806 reg_process_hint_country_ie(struct wiphy
*wiphy
,
1807 struct regulatory_request
*country_ie_request
)
1809 enum reg_request_treatment treatment
;
1811 treatment
= __reg_process_hint_country_ie(wiphy
, country_ie_request
);
1813 switch (treatment
) {
1816 case REG_REQ_IGNORE
:
1817 case REG_REQ_USER_HINT_HANDLED
:
1819 case REG_REQ_ALREADY_SET
:
1820 kfree(country_ie_request
);
1822 case REG_REQ_INTERSECT
:
1823 kfree(country_ie_request
);
1825 * This doesn't happen yet, not sure we
1826 * ever want to support it for this case.
1828 WARN_ONCE(1, "Unexpected intersection for country IEs");
1829 return REG_REQ_IGNORE
;
1832 country_ie_request
->intersect
= false;
1833 country_ie_request
->processed
= false;
1835 reg_update_last_request(country_ie_request
);
1837 return reg_call_crda(country_ie_request
);
1840 /* This processes *all* regulatory hints */
1841 static void reg_process_hint(struct regulatory_request
*reg_request
)
1843 struct wiphy
*wiphy
= NULL
;
1844 enum reg_request_treatment treatment
;
1846 if (reg_request
->wiphy_idx
!= WIPHY_IDX_INVALID
)
1847 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1849 switch (reg_request
->initiator
) {
1850 case NL80211_REGDOM_SET_BY_CORE
:
1851 reg_process_hint_core(reg_request
);
1853 case NL80211_REGDOM_SET_BY_USER
:
1854 treatment
= reg_process_hint_user(reg_request
);
1855 if (treatment
== REG_REQ_IGNORE
||
1856 treatment
== REG_REQ_ALREADY_SET
)
1858 queue_delayed_work(system_power_efficient_wq
,
1859 ®_timeout
, msecs_to_jiffies(3142));
1861 case NL80211_REGDOM_SET_BY_DRIVER
:
1864 treatment
= reg_process_hint_driver(wiphy
, reg_request
);
1866 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1869 treatment
= reg_process_hint_country_ie(wiphy
, reg_request
);
1872 WARN(1, "invalid initiator %d\n", reg_request
->initiator
);
1876 /* This is required so that the orig_* parameters are saved */
1877 if (treatment
== REG_REQ_ALREADY_SET
&& wiphy
&&
1878 wiphy
->regulatory_flags
& REGULATORY_STRICT_REG
)
1879 wiphy_update_regulatory(wiphy
, reg_request
->initiator
);
1888 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1889 * Regulatory hints come on a first come first serve basis and we
1890 * must process each one atomically.
1892 static void reg_process_pending_hints(void)
1894 struct regulatory_request
*reg_request
, *lr
;
1896 lr
= get_last_request();
1898 /* When last_request->processed becomes true this will be rescheduled */
1899 if (lr
&& !lr
->processed
) {
1900 REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n");
1904 spin_lock(®_requests_lock
);
1906 if (list_empty(®_requests_list
)) {
1907 spin_unlock(®_requests_lock
);
1911 reg_request
= list_first_entry(®_requests_list
,
1912 struct regulatory_request
,
1914 list_del_init(®_request
->list
);
1916 spin_unlock(®_requests_lock
);
1918 reg_process_hint(reg_request
);
1921 /* Processes beacon hints -- this has nothing to do with country IEs */
1922 static void reg_process_pending_beacon_hints(void)
1924 struct cfg80211_registered_device
*rdev
;
1925 struct reg_beacon
*pending_beacon
, *tmp
;
1927 /* This goes through the _pending_ beacon list */
1928 spin_lock_bh(®_pending_beacons_lock
);
1930 list_for_each_entry_safe(pending_beacon
, tmp
,
1931 ®_pending_beacons
, list
) {
1932 list_del_init(&pending_beacon
->list
);
1934 /* Applies the beacon hint to current wiphys */
1935 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1936 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1938 /* Remembers the beacon hint for new wiphys or reg changes */
1939 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1942 spin_unlock_bh(®_pending_beacons_lock
);
1945 static void reg_todo(struct work_struct
*work
)
1948 reg_process_pending_hints();
1949 reg_process_pending_beacon_hints();
1953 static void queue_regulatory_request(struct regulatory_request
*request
)
1955 request
->alpha2
[0] = toupper(request
->alpha2
[0]);
1956 request
->alpha2
[1] = toupper(request
->alpha2
[1]);
1958 spin_lock(®_requests_lock
);
1959 list_add_tail(&request
->list
, ®_requests_list
);
1960 spin_unlock(®_requests_lock
);
1962 schedule_work(®_work
);
1966 * Core regulatory hint -- happens during cfg80211_init()
1967 * and when we restore regulatory settings.
1969 static int regulatory_hint_core(const char *alpha2
)
1971 struct regulatory_request
*request
;
1973 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1977 request
->alpha2
[0] = alpha2
[0];
1978 request
->alpha2
[1] = alpha2
[1];
1979 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1981 queue_regulatory_request(request
);
1987 int regulatory_hint_user(const char *alpha2
,
1988 enum nl80211_user_reg_hint_type user_reg_hint_type
)
1990 struct regulatory_request
*request
;
1992 if (WARN_ON(!alpha2
))
1995 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1999 request
->wiphy_idx
= WIPHY_IDX_INVALID
;
2000 request
->alpha2
[0] = alpha2
[0];
2001 request
->alpha2
[1] = alpha2
[1];
2002 request
->initiator
= NL80211_REGDOM_SET_BY_USER
;
2003 request
->user_reg_hint_type
= user_reg_hint_type
;
2005 queue_regulatory_request(request
);
2010 int regulatory_hint_indoor_user(void)
2012 struct regulatory_request
*request
;
2014 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
2018 request
->wiphy_idx
= WIPHY_IDX_INVALID
;
2019 request
->initiator
= NL80211_REGDOM_SET_BY_USER
;
2020 request
->user_reg_hint_type
= NL80211_USER_REG_HINT_INDOOR
;
2021 queue_regulatory_request(request
);
2027 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
2029 struct regulatory_request
*request
;
2031 if (WARN_ON(!alpha2
|| !wiphy
))
2034 wiphy
->regulatory_flags
&= ~REGULATORY_CUSTOM_REG
;
2036 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
2040 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
2042 request
->alpha2
[0] = alpha2
[0];
2043 request
->alpha2
[1] = alpha2
[1];
2044 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
2046 queue_regulatory_request(request
);
2050 EXPORT_SYMBOL(regulatory_hint
);
2052 void regulatory_hint_country_ie(struct wiphy
*wiphy
, enum ieee80211_band band
,
2053 const u8
*country_ie
, u8 country_ie_len
)
2056 enum environment_cap env
= ENVIRON_ANY
;
2057 struct regulatory_request
*request
= NULL
, *lr
;
2059 /* IE len must be evenly divisible by 2 */
2060 if (country_ie_len
& 0x01)
2063 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
2066 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
2070 alpha2
[0] = country_ie
[0];
2071 alpha2
[1] = country_ie
[1];
2073 if (country_ie
[2] == 'I')
2074 env
= ENVIRON_INDOOR
;
2075 else if (country_ie
[2] == 'O')
2076 env
= ENVIRON_OUTDOOR
;
2079 lr
= get_last_request();
2085 * We will run this only upon a successful connection on cfg80211.
2086 * We leave conflict resolution to the workqueue, where can hold
2089 if (lr
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
2090 lr
->wiphy_idx
!= WIPHY_IDX_INVALID
)
2093 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
2094 request
->alpha2
[0] = alpha2
[0];
2095 request
->alpha2
[1] = alpha2
[1];
2096 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
2097 request
->country_ie_env
= env
;
2099 queue_regulatory_request(request
);
2106 static void restore_alpha2(char *alpha2
, bool reset_user
)
2108 /* indicates there is no alpha2 to consider for restoration */
2112 /* The user setting has precedence over the module parameter */
2113 if (is_user_regdom_saved()) {
2114 /* Unless we're asked to ignore it and reset it */
2116 REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
2117 user_alpha2
[0] = '9';
2118 user_alpha2
[1] = '7';
2121 * If we're ignoring user settings, we still need to
2122 * check the module parameter to ensure we put things
2123 * back as they were for a full restore.
2125 if (!is_world_regdom(ieee80211_regdom
)) {
2126 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2127 ieee80211_regdom
[0], ieee80211_regdom
[1]);
2128 alpha2
[0] = ieee80211_regdom
[0];
2129 alpha2
[1] = ieee80211_regdom
[1];
2132 REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
2133 user_alpha2
[0], user_alpha2
[1]);
2134 alpha2
[0] = user_alpha2
[0];
2135 alpha2
[1] = user_alpha2
[1];
2137 } else if (!is_world_regdom(ieee80211_regdom
)) {
2138 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2139 ieee80211_regdom
[0], ieee80211_regdom
[1]);
2140 alpha2
[0] = ieee80211_regdom
[0];
2141 alpha2
[1] = ieee80211_regdom
[1];
2143 REG_DBG_PRINT("Restoring regulatory settings\n");
2146 static void restore_custom_reg_settings(struct wiphy
*wiphy
)
2148 struct ieee80211_supported_band
*sband
;
2149 enum ieee80211_band band
;
2150 struct ieee80211_channel
*chan
;
2153 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
2154 sband
= wiphy
->bands
[band
];
2157 for (i
= 0; i
< sband
->n_channels
; i
++) {
2158 chan
= &sband
->channels
[i
];
2159 chan
->flags
= chan
->orig_flags
;
2160 chan
->max_antenna_gain
= chan
->orig_mag
;
2161 chan
->max_power
= chan
->orig_mpwr
;
2162 chan
->beacon_found
= false;
2168 * Restoring regulatory settings involves ingoring any
2169 * possibly stale country IE information and user regulatory
2170 * settings if so desired, this includes any beacon hints
2171 * learned as we could have traveled outside to another country
2172 * after disconnection. To restore regulatory settings we do
2173 * exactly what we did at bootup:
2175 * - send a core regulatory hint
2176 * - send a user regulatory hint if applicable
2178 * Device drivers that send a regulatory hint for a specific country
2179 * keep their own regulatory domain on wiphy->regd so that does does
2180 * not need to be remembered.
2182 static void restore_regulatory_settings(bool reset_user
)
2185 char world_alpha2
[2];
2186 struct reg_beacon
*reg_beacon
, *btmp
;
2187 struct regulatory_request
*reg_request
, *tmp
;
2188 LIST_HEAD(tmp_reg_req_list
);
2189 struct cfg80211_registered_device
*rdev
;
2193 reg_is_indoor
= false;
2195 reset_regdomains(true, &world_regdom
);
2196 restore_alpha2(alpha2
, reset_user
);
2199 * If there's any pending requests we simply
2200 * stash them to a temporary pending queue and
2201 * add then after we've restored regulatory
2204 spin_lock(®_requests_lock
);
2205 list_for_each_entry_safe(reg_request
, tmp
, ®_requests_list
, list
) {
2206 if (reg_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
)
2208 list_move_tail(®_request
->list
, &tmp_reg_req_list
);
2210 spin_unlock(®_requests_lock
);
2212 /* Clear beacon hints */
2213 spin_lock_bh(®_pending_beacons_lock
);
2214 list_for_each_entry_safe(reg_beacon
, btmp
, ®_pending_beacons
, list
) {
2215 list_del(®_beacon
->list
);
2218 spin_unlock_bh(®_pending_beacons_lock
);
2220 list_for_each_entry_safe(reg_beacon
, btmp
, ®_beacon_list
, list
) {
2221 list_del(®_beacon
->list
);
2225 /* First restore to the basic regulatory settings */
2226 world_alpha2
[0] = cfg80211_world_regdom
->alpha2
[0];
2227 world_alpha2
[1] = cfg80211_world_regdom
->alpha2
[1];
2229 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
) {
2230 if (rdev
->wiphy
.regulatory_flags
& REGULATORY_CUSTOM_REG
)
2231 restore_custom_reg_settings(&rdev
->wiphy
);
2234 regulatory_hint_core(world_alpha2
);
2237 * This restores the ieee80211_regdom module parameter
2238 * preference or the last user requested regulatory
2239 * settings, user regulatory settings takes precedence.
2241 if (is_an_alpha2(alpha2
))
2242 regulatory_hint_user(user_alpha2
, NL80211_USER_REG_HINT_USER
);
2244 spin_lock(®_requests_lock
);
2245 list_splice_tail_init(&tmp_reg_req_list
, ®_requests_list
);
2246 spin_unlock(®_requests_lock
);
2248 REG_DBG_PRINT("Kicking the queue\n");
2250 schedule_work(®_work
);
2253 void regulatory_hint_disconnect(void)
2255 REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
2256 restore_regulatory_settings(false);
2259 static bool freq_is_chan_12_13_14(u16 freq
)
2261 if (freq
== ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ
) ||
2262 freq
== ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ
) ||
2263 freq
== ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ
))
2268 static bool pending_reg_beacon(struct ieee80211_channel
*beacon_chan
)
2270 struct reg_beacon
*pending_beacon
;
2272 list_for_each_entry(pending_beacon
, ®_pending_beacons
, list
)
2273 if (beacon_chan
->center_freq
==
2274 pending_beacon
->chan
.center_freq
)
2279 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
2280 struct ieee80211_channel
*beacon_chan
,
2283 struct reg_beacon
*reg_beacon
;
2286 if (beacon_chan
->beacon_found
||
2287 beacon_chan
->flags
& IEEE80211_CHAN_RADAR
||
2288 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
2289 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))
2292 spin_lock_bh(®_pending_beacons_lock
);
2293 processing
= pending_reg_beacon(beacon_chan
);
2294 spin_unlock_bh(®_pending_beacons_lock
);
2299 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
2303 REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
2304 beacon_chan
->center_freq
,
2305 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
2308 memcpy(®_beacon
->chan
, beacon_chan
,
2309 sizeof(struct ieee80211_channel
));
2312 * Since we can be called from BH or and non-BH context
2313 * we must use spin_lock_bh()
2315 spin_lock_bh(®_pending_beacons_lock
);
2316 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
2317 spin_unlock_bh(®_pending_beacons_lock
);
2319 schedule_work(®_work
);
2324 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
2327 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
2328 const struct ieee80211_freq_range
*freq_range
= NULL
;
2329 const struct ieee80211_power_rule
*power_rule
= NULL
;
2330 char bw
[32], cac_time
[32];
2332 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
2334 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
2335 reg_rule
= &rd
->reg_rules
[i
];
2336 freq_range
= ®_rule
->freq_range
;
2337 power_rule
= ®_rule
->power_rule
;
2339 if (reg_rule
->flags
& NL80211_RRF_AUTO_BW
)
2340 snprintf(bw
, sizeof(bw
), "%d KHz, %d KHz AUTO",
2341 freq_range
->max_bandwidth_khz
,
2342 reg_get_max_bandwidth(rd
, reg_rule
));
2344 snprintf(bw
, sizeof(bw
), "%d KHz",
2345 freq_range
->max_bandwidth_khz
);
2347 if (reg_rule
->flags
& NL80211_RRF_DFS
)
2348 scnprintf(cac_time
, sizeof(cac_time
), "%u s",
2349 reg_rule
->dfs_cac_ms
/1000);
2351 scnprintf(cac_time
, sizeof(cac_time
), "N/A");
2355 * There may not be documentation for max antenna gain
2356 * in certain regions
2358 if (power_rule
->max_antenna_gain
)
2359 pr_info(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
2360 freq_range
->start_freq_khz
,
2361 freq_range
->end_freq_khz
,
2363 power_rule
->max_antenna_gain
,
2364 power_rule
->max_eirp
,
2367 pr_info(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
2368 freq_range
->start_freq_khz
,
2369 freq_range
->end_freq_khz
,
2371 power_rule
->max_eirp
,
2376 bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region
)
2378 switch (dfs_region
) {
2379 case NL80211_DFS_UNSET
:
2380 case NL80211_DFS_FCC
:
2381 case NL80211_DFS_ETSI
:
2382 case NL80211_DFS_JP
:
2385 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
2391 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
2393 struct regulatory_request
*lr
= get_last_request();
2395 if (is_intersected_alpha2(rd
->alpha2
)) {
2396 if (lr
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2397 struct cfg80211_registered_device
*rdev
;
2398 rdev
= cfg80211_rdev_by_wiphy_idx(lr
->wiphy_idx
);
2400 pr_info("Current regulatory domain updated by AP to: %c%c\n",
2401 rdev
->country_ie_alpha2
[0],
2402 rdev
->country_ie_alpha2
[1]);
2404 pr_info("Current regulatory domain intersected:\n");
2406 pr_info("Current regulatory domain intersected:\n");
2407 } else if (is_world_regdom(rd
->alpha2
)) {
2408 pr_info("World regulatory domain updated:\n");
2410 if (is_unknown_alpha2(rd
->alpha2
))
2411 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
2413 if (reg_request_cell_base(lr
))
2414 pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
2415 rd
->alpha2
[0], rd
->alpha2
[1]);
2417 pr_info("Regulatory domain changed to country: %c%c\n",
2418 rd
->alpha2
[0], rd
->alpha2
[1]);
2422 pr_info(" DFS Master region: %s", reg_dfs_region_str(rd
->dfs_region
));
2426 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
2428 pr_info("Regulatory domain: %c%c\n", rd
->alpha2
[0], rd
->alpha2
[1]);
2432 static int reg_set_rd_core(const struct ieee80211_regdomain
*rd
)
2434 if (!is_world_regdom(rd
->alpha2
))
2436 update_world_regdomain(rd
);
2440 static int reg_set_rd_user(const struct ieee80211_regdomain
*rd
,
2441 struct regulatory_request
*user_request
)
2443 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2445 if (!regdom_changes(rd
->alpha2
))
2448 if (!is_valid_rd(rd
)) {
2449 pr_err("Invalid regulatory domain detected:\n");
2450 print_regdomain_info(rd
);
2454 if (!user_request
->intersect
) {
2455 reset_regdomains(false, rd
);
2459 intersected_rd
= regdom_intersect(rd
, get_cfg80211_regdom());
2460 if (!intersected_rd
)
2465 reset_regdomains(false, intersected_rd
);
2470 static int reg_set_rd_driver(const struct ieee80211_regdomain
*rd
,
2471 struct regulatory_request
*driver_request
)
2473 const struct ieee80211_regdomain
*regd
;
2474 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2475 const struct ieee80211_regdomain
*tmp
;
2476 struct wiphy
*request_wiphy
;
2478 if (is_world_regdom(rd
->alpha2
))
2481 if (!regdom_changes(rd
->alpha2
))
2484 if (!is_valid_rd(rd
)) {
2485 pr_err("Invalid regulatory domain detected:\n");
2486 print_regdomain_info(rd
);
2490 request_wiphy
= wiphy_idx_to_wiphy(driver_request
->wiphy_idx
);
2491 if (!request_wiphy
) {
2492 queue_delayed_work(system_power_efficient_wq
,
2497 if (!driver_request
->intersect
) {
2498 if (request_wiphy
->regd
)
2501 regd
= reg_copy_regd(rd
);
2503 return PTR_ERR(regd
);
2505 rcu_assign_pointer(request_wiphy
->regd
, regd
);
2506 reset_regdomains(false, rd
);
2510 intersected_rd
= regdom_intersect(rd
, get_cfg80211_regdom());
2511 if (!intersected_rd
)
2515 * We can trash what CRDA provided now.
2516 * However if a driver requested this specific regulatory
2517 * domain we keep it for its private use
2519 tmp
= get_wiphy_regdom(request_wiphy
);
2520 rcu_assign_pointer(request_wiphy
->regd
, rd
);
2521 rcu_free_regdom(tmp
);
2525 reset_regdomains(false, intersected_rd
);
2530 static int reg_set_rd_country_ie(const struct ieee80211_regdomain
*rd
,
2531 struct regulatory_request
*country_ie_request
)
2533 struct wiphy
*request_wiphy
;
2535 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2536 !is_unknown_alpha2(rd
->alpha2
))
2540 * Lets only bother proceeding on the same alpha2 if the current
2541 * rd is non static (it means CRDA was present and was used last)
2542 * and the pending request came in from a country IE
2545 if (!is_valid_rd(rd
)) {
2546 pr_err("Invalid regulatory domain detected:\n");
2547 print_regdomain_info(rd
);
2551 request_wiphy
= wiphy_idx_to_wiphy(country_ie_request
->wiphy_idx
);
2552 if (!request_wiphy
) {
2553 queue_delayed_work(system_power_efficient_wq
,
2558 if (country_ie_request
->intersect
)
2561 reset_regdomains(false, rd
);
2566 * Use this call to set the current regulatory domain. Conflicts with
2567 * multiple drivers can be ironed out later. Caller must've already
2568 * kmalloc'd the rd structure.
2570 int set_regdom(const struct ieee80211_regdomain
*rd
)
2572 struct regulatory_request
*lr
;
2573 bool user_reset
= false;
2576 if (!reg_is_valid_request(rd
->alpha2
)) {
2581 lr
= get_last_request();
2583 /* Note that this doesn't update the wiphys, this is done below */
2584 switch (lr
->initiator
) {
2585 case NL80211_REGDOM_SET_BY_CORE
:
2586 r
= reg_set_rd_core(rd
);
2588 case NL80211_REGDOM_SET_BY_USER
:
2589 r
= reg_set_rd_user(rd
, lr
);
2592 case NL80211_REGDOM_SET_BY_DRIVER
:
2593 r
= reg_set_rd_driver(rd
, lr
);
2595 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
2596 r
= reg_set_rd_country_ie(rd
, lr
);
2599 WARN(1, "invalid initiator %d\n", lr
->initiator
);
2606 reg_set_request_processed();
2609 /* Back to world regulatory in case of errors */
2610 restore_regulatory_settings(user_reset
);
2617 /* This would make this whole thing pointless */
2618 if (WARN_ON(!lr
->intersect
&& rd
!= get_cfg80211_regdom()))
2621 /* update all wiphys now with the new established regulatory domain */
2622 update_all_wiphy_regulatory(lr
->initiator
);
2624 print_regdomain(get_cfg80211_regdom());
2626 nl80211_send_reg_change_event(lr
);
2628 reg_set_request_processed();
2633 void wiphy_regulatory_register(struct wiphy
*wiphy
)
2635 struct regulatory_request
*lr
;
2637 if (!reg_dev_ignore_cell_hint(wiphy
))
2638 reg_num_devs_support_basehint
++;
2640 lr
= get_last_request();
2641 wiphy_update_regulatory(wiphy
, lr
->initiator
);
2644 void wiphy_regulatory_deregister(struct wiphy
*wiphy
)
2646 struct wiphy
*request_wiphy
= NULL
;
2647 struct regulatory_request
*lr
;
2649 lr
= get_last_request();
2651 if (!reg_dev_ignore_cell_hint(wiphy
))
2652 reg_num_devs_support_basehint
--;
2654 rcu_free_regdom(get_wiphy_regdom(wiphy
));
2655 RCU_INIT_POINTER(wiphy
->regd
, NULL
);
2658 request_wiphy
= wiphy_idx_to_wiphy(lr
->wiphy_idx
);
2660 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2663 lr
->wiphy_idx
= WIPHY_IDX_INVALID
;
2664 lr
->country_ie_env
= ENVIRON_ANY
;
2667 static void reg_timeout_work(struct work_struct
*work
)
2669 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
2671 restore_regulatory_settings(true);
2676 * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
2677 * UNII band definitions
2679 int cfg80211_get_unii(int freq
)
2682 if (freq
>= 5150 && freq
<= 5250)
2686 if (freq
> 5250 && freq
<= 5350)
2690 if (freq
> 5350 && freq
<= 5470)
2694 if (freq
> 5470 && freq
<= 5725)
2698 if (freq
> 5725 && freq
<= 5825)
2704 int __init
regulatory_init(void)
2708 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2709 if (IS_ERR(reg_pdev
))
2710 return PTR_ERR(reg_pdev
);
2712 spin_lock_init(®_requests_lock
);
2713 spin_lock_init(®_pending_beacons_lock
);
2715 reg_regdb_size_check();
2717 rcu_assign_pointer(cfg80211_regdomain
, cfg80211_world_regdom
);
2719 user_alpha2
[0] = '9';
2720 user_alpha2
[1] = '7';
2722 /* We always try to get an update for the static regdomain */
2723 err
= regulatory_hint_core(cfg80211_world_regdom
->alpha2
);
2728 * N.B. kobject_uevent_env() can fail mainly for when we're out
2729 * memory which is handled and propagated appropriately above
2730 * but it can also fail during a netlink_broadcast() or during
2731 * early boot for call_usermodehelper(). For now treat these
2732 * errors as non-fatal.
2734 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
2738 * Finally, if the user set the module parameter treat it
2741 if (!is_world_regdom(ieee80211_regdom
))
2742 regulatory_hint_user(ieee80211_regdom
,
2743 NL80211_USER_REG_HINT_USER
);
2748 void regulatory_exit(void)
2750 struct regulatory_request
*reg_request
, *tmp
;
2751 struct reg_beacon
*reg_beacon
, *btmp
;
2753 cancel_work_sync(®_work
);
2754 cancel_delayed_work_sync(®_timeout
);
2756 /* Lock to suppress warnings */
2758 reset_regdomains(true, NULL
);
2761 dev_set_uevent_suppress(®_pdev
->dev
, true);
2763 platform_device_unregister(reg_pdev
);
2765 list_for_each_entry_safe(reg_beacon
, btmp
, ®_pending_beacons
, list
) {
2766 list_del(®_beacon
->list
);
2770 list_for_each_entry_safe(reg_beacon
, btmp
, ®_beacon_list
, list
) {
2771 list_del(®_beacon
->list
);
2775 list_for_each_entry_safe(reg_request
, tmp
, ®_requests_list
, list
) {
2776 list_del(®_request
->list
);