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 Luis R. Rodriguez <lrodriguz@atheros.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 * DOC: Wireless regulatory infrastructure
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
20 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/list.h>
41 #include <linux/random.h>
42 #include <linux/ctype.h>
43 #include <linux/nl80211.h>
44 #include <linux/platform_device.h>
45 #include <linux/moduleparam.h>
46 #include <net/cfg80211.h>
52 #ifdef CONFIG_CFG80211_REG_DEBUG
53 #define REG_DBG_PRINT(format, args...) \
54 printk(KERN_DEBUG pr_fmt(format), ##args)
56 #define REG_DBG_PRINT(args...)
59 /* Receipt of information from last regulatory request */
60 static struct regulatory_request
*last_request
;
62 /* To trigger userspace events */
63 static struct platform_device
*reg_pdev
;
65 static struct device_type reg_device_type
= {
66 .uevent
= reg_device_uevent
,
70 * Central wireless core regulatory domains, we only need two,
71 * the current one and a world regulatory domain in case we have no
72 * information to give us an alpha2
74 const struct ieee80211_regdomain
*cfg80211_regdomain
;
77 * Protects static reg.c components:
78 * - cfg80211_world_regdom
82 static DEFINE_MUTEX(reg_mutex
);
84 static inline void assert_reg_lock(void)
86 lockdep_assert_held(®_mutex
);
89 /* Used to queue up regulatory hints */
90 static LIST_HEAD(reg_requests_list
);
91 static spinlock_t reg_requests_lock
;
93 /* Used to queue up beacon hints for review */
94 static LIST_HEAD(reg_pending_beacons
);
95 static spinlock_t reg_pending_beacons_lock
;
97 /* Used to keep track of processed beacon hints */
98 static LIST_HEAD(reg_beacon_list
);
101 struct list_head list
;
102 struct ieee80211_channel chan
;
105 static void reg_todo(struct work_struct
*work
);
106 static DECLARE_WORK(reg_work
, reg_todo
);
108 static void reg_timeout_work(struct work_struct
*work
);
109 static DECLARE_DELAYED_WORK(reg_timeout
, reg_timeout_work
);
111 /* We keep a static world regulatory domain in case of the absence of CRDA */
112 static const struct ieee80211_regdomain world_regdom
= {
116 /* IEEE 802.11b/g, channels 1..11 */
117 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
118 /* IEEE 802.11b/g, channels 12..13. No HT40
119 * channel fits here. */
120 REG_RULE(2467-10, 2472+10, 20, 6, 20,
121 NL80211_RRF_PASSIVE_SCAN
|
122 NL80211_RRF_NO_IBSS
),
123 /* IEEE 802.11 channel 14 - Only JP enables
124 * this and for 802.11b only */
125 REG_RULE(2484-10, 2484+10, 20, 6, 20,
126 NL80211_RRF_PASSIVE_SCAN
|
127 NL80211_RRF_NO_IBSS
|
128 NL80211_RRF_NO_OFDM
),
129 /* IEEE 802.11a, channel 36..48 */
130 REG_RULE(5180-10, 5240+10, 40, 6, 20,
131 NL80211_RRF_PASSIVE_SCAN
|
132 NL80211_RRF_NO_IBSS
),
134 /* NB: 5260 MHz - 5700 MHz requies DFS */
136 /* IEEE 802.11a, channel 149..165 */
137 REG_RULE(5745-10, 5825+10, 40, 6, 20,
138 NL80211_RRF_PASSIVE_SCAN
|
139 NL80211_RRF_NO_IBSS
),
143 static const struct ieee80211_regdomain
*cfg80211_world_regdom
=
146 static char *ieee80211_regdom
= "00";
147 static char user_alpha2
[2];
149 module_param(ieee80211_regdom
, charp
, 0444);
150 MODULE_PARM_DESC(ieee80211_regdom
, "IEEE 802.11 regulatory domain code");
152 static void reset_regdomains(void)
154 /* avoid freeing static information or freeing something twice */
155 if (cfg80211_regdomain
== cfg80211_world_regdom
)
156 cfg80211_regdomain
= NULL
;
157 if (cfg80211_world_regdom
== &world_regdom
)
158 cfg80211_world_regdom
= NULL
;
159 if (cfg80211_regdomain
== &world_regdom
)
160 cfg80211_regdomain
= NULL
;
162 kfree(cfg80211_regdomain
);
163 kfree(cfg80211_world_regdom
);
165 cfg80211_world_regdom
= &world_regdom
;
166 cfg80211_regdomain
= NULL
;
170 * Dynamic world regulatory domain requested by the wireless
171 * core upon initialization
173 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
175 BUG_ON(!last_request
);
179 cfg80211_world_regdom
= rd
;
180 cfg80211_regdomain
= rd
;
183 bool is_world_regdom(const char *alpha2
)
187 if (alpha2
[0] == '0' && alpha2
[1] == '0')
192 static bool is_alpha2_set(const char *alpha2
)
196 if (alpha2
[0] != 0 && alpha2
[1] != 0)
201 static bool is_unknown_alpha2(const char *alpha2
)
206 * Special case where regulatory domain was built by driver
207 * but a specific alpha2 cannot be determined
209 if (alpha2
[0] == '9' && alpha2
[1] == '9')
214 static bool is_intersected_alpha2(const char *alpha2
)
219 * Special case where regulatory domain is the
220 * result of an intersection between two regulatory domain
223 if (alpha2
[0] == '9' && alpha2
[1] == '8')
228 static bool is_an_alpha2(const char *alpha2
)
232 if (isalpha(alpha2
[0]) && isalpha(alpha2
[1]))
237 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
239 if (!alpha2_x
|| !alpha2_y
)
241 if (alpha2_x
[0] == alpha2_y
[0] &&
242 alpha2_x
[1] == alpha2_y
[1])
247 static bool regdom_changes(const char *alpha2
)
249 assert_cfg80211_lock();
251 if (!cfg80211_regdomain
)
253 if (alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
259 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
260 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
261 * has ever been issued.
263 static bool is_user_regdom_saved(void)
265 if (user_alpha2
[0] == '9' && user_alpha2
[1] == '7')
268 /* This would indicate a mistake on the design */
269 if (WARN((!is_world_regdom(user_alpha2
) &&
270 !is_an_alpha2(user_alpha2
)),
271 "Unexpected user alpha2: %c%c\n",
279 static int reg_copy_regd(const struct ieee80211_regdomain
**dst_regd
,
280 const struct ieee80211_regdomain
*src_regd
)
282 struct ieee80211_regdomain
*regd
;
283 int size_of_regd
= 0;
286 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
287 ((src_regd
->n_reg_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
289 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
293 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
295 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
296 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
297 sizeof(struct ieee80211_reg_rule
));
303 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
304 struct reg_regdb_search_request
{
306 struct list_head list
;
309 static LIST_HEAD(reg_regdb_search_list
);
310 static DEFINE_MUTEX(reg_regdb_search_mutex
);
312 static void reg_regdb_search(struct work_struct
*work
)
314 struct reg_regdb_search_request
*request
;
315 const struct ieee80211_regdomain
*curdom
, *regdom
;
318 mutex_lock(®_regdb_search_mutex
);
319 while (!list_empty(®_regdb_search_list
)) {
320 request
= list_first_entry(®_regdb_search_list
,
321 struct reg_regdb_search_request
,
323 list_del(&request
->list
);
325 for (i
=0; i
<reg_regdb_size
; i
++) {
326 curdom
= reg_regdb
[i
];
328 if (!memcmp(request
->alpha2
, curdom
->alpha2
, 2)) {
329 r
= reg_copy_regd(®dom
, curdom
);
332 mutex_lock(&cfg80211_mutex
);
334 mutex_unlock(&cfg80211_mutex
);
341 mutex_unlock(®_regdb_search_mutex
);
344 static DECLARE_WORK(reg_regdb_work
, reg_regdb_search
);
346 static void reg_regdb_query(const char *alpha2
)
348 struct reg_regdb_search_request
*request
;
353 request
= kzalloc(sizeof(struct reg_regdb_search_request
), GFP_KERNEL
);
357 memcpy(request
->alpha2
, alpha2
, 2);
359 mutex_lock(®_regdb_search_mutex
);
360 list_add_tail(&request
->list
, ®_regdb_search_list
);
361 mutex_unlock(®_regdb_search_mutex
);
363 schedule_work(®_regdb_work
);
366 static inline void reg_regdb_query(const char *alpha2
) {}
367 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
370 * This lets us keep regulatory code which is updated on a regulatory
371 * basis in userspace. Country information is filled in by
374 static int call_crda(const char *alpha2
)
376 if (!is_world_regdom((char *) alpha2
))
377 pr_info("Calling CRDA for country: %c%c\n",
378 alpha2
[0], alpha2
[1]);
380 pr_info("Calling CRDA to update world regulatory domain\n");
382 /* query internal regulatory database (if it exists) */
383 reg_regdb_query(alpha2
);
385 return kobject_uevent(®_pdev
->dev
.kobj
, KOBJ_CHANGE
);
388 /* Used by nl80211 before kmalloc'ing our regulatory domain */
389 bool reg_is_valid_request(const char *alpha2
)
391 assert_cfg80211_lock();
396 return alpha2_equal(last_request
->alpha2
, alpha2
);
399 /* Sanity check on a regulatory rule */
400 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
402 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
405 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
408 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
411 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
413 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
414 freq_range
->max_bandwidth_khz
> freq_diff
)
420 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
422 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
425 if (!rd
->n_reg_rules
)
428 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
431 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
432 reg_rule
= &rd
->reg_rules
[i
];
433 if (!is_valid_reg_rule(reg_rule
))
440 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
444 u32 start_freq_khz
, end_freq_khz
;
446 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
447 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
449 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
450 end_freq_khz
<= freq_range
->end_freq_khz
)
457 * freq_in_rule_band - tells us if a frequency is in a frequency band
458 * @freq_range: frequency rule we want to query
459 * @freq_khz: frequency we are inquiring about
461 * This lets us know if a specific frequency rule is or is not relevant to
462 * a specific frequency's band. Bands are device specific and artificial
463 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
464 * safe for now to assume that a frequency rule should not be part of a
465 * frequency's band if the start freq or end freq are off by more than 2 GHz.
466 * This resolution can be lowered and should be considered as we add
467 * regulatory rule support for other "bands".
469 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
472 #define ONE_GHZ_IN_KHZ 1000000
473 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
475 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
478 #undef ONE_GHZ_IN_KHZ
482 * Helper for regdom_intersect(), this does the real
483 * mathematical intersection fun
485 static int reg_rules_intersect(
486 const struct ieee80211_reg_rule
*rule1
,
487 const struct ieee80211_reg_rule
*rule2
,
488 struct ieee80211_reg_rule
*intersected_rule
)
490 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
491 struct ieee80211_freq_range
*freq_range
;
492 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
493 struct ieee80211_power_rule
*power_rule
;
496 freq_range1
= &rule1
->freq_range
;
497 freq_range2
= &rule2
->freq_range
;
498 freq_range
= &intersected_rule
->freq_range
;
500 power_rule1
= &rule1
->power_rule
;
501 power_rule2
= &rule2
->power_rule
;
502 power_rule
= &intersected_rule
->power_rule
;
504 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
505 freq_range2
->start_freq_khz
);
506 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
507 freq_range2
->end_freq_khz
);
508 freq_range
->max_bandwidth_khz
= min(freq_range1
->max_bandwidth_khz
,
509 freq_range2
->max_bandwidth_khz
);
511 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
512 if (freq_range
->max_bandwidth_khz
> freq_diff
)
513 freq_range
->max_bandwidth_khz
= freq_diff
;
515 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
516 power_rule2
->max_eirp
);
517 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
518 power_rule2
->max_antenna_gain
);
520 intersected_rule
->flags
= (rule1
->flags
| rule2
->flags
);
522 if (!is_valid_reg_rule(intersected_rule
))
529 * regdom_intersect - do the intersection between two regulatory domains
530 * @rd1: first regulatory domain
531 * @rd2: second regulatory domain
533 * Use this function to get the intersection between two regulatory domains.
534 * Once completed we will mark the alpha2 for the rd as intersected, "98",
535 * as no one single alpha2 can represent this regulatory domain.
537 * Returns a pointer to the regulatory domain structure which will hold the
538 * resulting intersection of rules between rd1 and rd2. We will
539 * kzalloc() this structure for you.
541 static struct ieee80211_regdomain
*regdom_intersect(
542 const struct ieee80211_regdomain
*rd1
,
543 const struct ieee80211_regdomain
*rd2
)
547 unsigned int num_rules
= 0, rule_idx
= 0;
548 const struct ieee80211_reg_rule
*rule1
, *rule2
;
549 struct ieee80211_reg_rule
*intersected_rule
;
550 struct ieee80211_regdomain
*rd
;
551 /* This is just a dummy holder to help us count */
552 struct ieee80211_reg_rule irule
;
554 /* Uses the stack temporarily for counter arithmetic */
555 intersected_rule
= &irule
;
557 memset(intersected_rule
, 0, sizeof(struct ieee80211_reg_rule
));
563 * First we get a count of the rules we'll need, then we actually
564 * build them. This is to so we can malloc() and free() a
565 * regdomain once. The reason we use reg_rules_intersect() here
566 * is it will return -EINVAL if the rule computed makes no sense.
567 * All rules that do check out OK are valid.
570 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
571 rule1
= &rd1
->reg_rules
[x
];
572 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
573 rule2
= &rd2
->reg_rules
[y
];
574 if (!reg_rules_intersect(rule1
, rule2
,
577 memset(intersected_rule
, 0,
578 sizeof(struct ieee80211_reg_rule
));
585 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
586 ((num_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
588 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
592 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
593 rule1
= &rd1
->reg_rules
[x
];
594 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
595 rule2
= &rd2
->reg_rules
[y
];
597 * This time around instead of using the stack lets
598 * write to the target rule directly saving ourselves
601 intersected_rule
= &rd
->reg_rules
[rule_idx
];
602 r
= reg_rules_intersect(rule1
, rule2
,
605 * No need to memset here the intersected rule here as
606 * we're not using the stack anymore
614 if (rule_idx
!= num_rules
) {
619 rd
->n_reg_rules
= num_rules
;
627 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
628 * want to just have the channel structure use these
630 static u32
map_regdom_flags(u32 rd_flags
)
632 u32 channel_flags
= 0;
633 if (rd_flags
& NL80211_RRF_PASSIVE_SCAN
)
634 channel_flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
635 if (rd_flags
& NL80211_RRF_NO_IBSS
)
636 channel_flags
|= IEEE80211_CHAN_NO_IBSS
;
637 if (rd_flags
& NL80211_RRF_DFS
)
638 channel_flags
|= IEEE80211_CHAN_RADAR
;
639 return channel_flags
;
642 static int freq_reg_info_regd(struct wiphy
*wiphy
,
645 const struct ieee80211_reg_rule
**reg_rule
,
646 const struct ieee80211_regdomain
*custom_regd
)
649 bool band_rule_found
= false;
650 const struct ieee80211_regdomain
*regd
;
651 bool bw_fits
= false;
654 desired_bw_khz
= MHZ_TO_KHZ(20);
656 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
659 * Follow the driver's regulatory domain, if present, unless a country
660 * IE has been processed or a user wants to help complaince further
663 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
664 last_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
671 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
672 const struct ieee80211_reg_rule
*rr
;
673 const struct ieee80211_freq_range
*fr
= NULL
;
675 rr
= ®d
->reg_rules
[i
];
676 fr
= &rr
->freq_range
;
679 * We only need to know if one frequency rule was
680 * was in center_freq's band, that's enough, so lets
681 * not overwrite it once found
683 if (!band_rule_found
)
684 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
686 bw_fits
= reg_does_bw_fit(fr
,
690 if (band_rule_found
&& bw_fits
) {
696 if (!band_rule_found
)
702 int freq_reg_info(struct wiphy
*wiphy
,
705 const struct ieee80211_reg_rule
**reg_rule
)
707 assert_cfg80211_lock();
708 return freq_reg_info_regd(wiphy
,
714 EXPORT_SYMBOL(freq_reg_info
);
716 #ifdef CONFIG_CFG80211_REG_DEBUG
717 static const char *reg_initiator_name(enum nl80211_reg_initiator initiator
)
720 case NL80211_REGDOM_SET_BY_CORE
:
721 return "Set by core";
722 case NL80211_REGDOM_SET_BY_USER
:
723 return "Set by user";
724 case NL80211_REGDOM_SET_BY_DRIVER
:
725 return "Set by driver";
726 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
727 return "Set by country IE";
734 static void chan_reg_rule_print_dbg(struct ieee80211_channel
*chan
,
736 const struct ieee80211_reg_rule
*reg_rule
)
738 const struct ieee80211_power_rule
*power_rule
;
739 const struct ieee80211_freq_range
*freq_range
;
740 char max_antenna_gain
[32];
742 power_rule
= ®_rule
->power_rule
;
743 freq_range
= ®_rule
->freq_range
;
745 if (!power_rule
->max_antenna_gain
)
746 snprintf(max_antenna_gain
, 32, "N/A");
748 snprintf(max_antenna_gain
, 32, "%d", power_rule
->max_antenna_gain
);
750 REG_DBG_PRINT("Updating information on frequency %d MHz "
751 "for a %d MHz width channel with regulatory rule:\n",
753 KHZ_TO_MHZ(desired_bw_khz
));
755 REG_DBG_PRINT("%d KHz - %d KHz @ %d KHz), (%s mBi, %d mBm)\n",
756 freq_range
->start_freq_khz
,
757 freq_range
->end_freq_khz
,
758 freq_range
->max_bandwidth_khz
,
760 power_rule
->max_eirp
);
763 static void chan_reg_rule_print_dbg(struct ieee80211_channel
*chan
,
765 const struct ieee80211_reg_rule
*reg_rule
)
772 * Note that right now we assume the desired channel bandwidth
773 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
774 * per channel, the primary and the extension channel). To support
775 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
776 * new ieee80211_channel.target_bw and re run the regulatory check
777 * on the wiphy with the target_bw specified. Then we can simply use
778 * that below for the desired_bw_khz below.
780 static void handle_channel(struct wiphy
*wiphy
,
781 enum nl80211_reg_initiator initiator
,
782 enum ieee80211_band band
,
783 unsigned int chan_idx
)
786 u32 flags
, bw_flags
= 0;
787 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
788 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
789 const struct ieee80211_power_rule
*power_rule
= NULL
;
790 const struct ieee80211_freq_range
*freq_range
= NULL
;
791 struct ieee80211_supported_band
*sband
;
792 struct ieee80211_channel
*chan
;
793 struct wiphy
*request_wiphy
= NULL
;
795 assert_cfg80211_lock();
797 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
799 sband
= wiphy
->bands
[band
];
800 BUG_ON(chan_idx
>= sband
->n_channels
);
801 chan
= &sband
->channels
[chan_idx
];
803 flags
= chan
->orig_flags
;
805 r
= freq_reg_info(wiphy
,
806 MHZ_TO_KHZ(chan
->center_freq
),
812 * We will disable all channels that do not match our
813 * received regulatory rule unless the hint is coming
814 * from a Country IE and the Country IE had no information
815 * about a band. The IEEE 802.11 spec allows for an AP
816 * to send only a subset of the regulatory rules allowed,
817 * so an AP in the US that only supports 2.4 GHz may only send
818 * a country IE with information for the 2.4 GHz band
819 * while 5 GHz is still supported.
821 if (initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
825 REG_DBG_PRINT("Disabling freq %d MHz\n", chan
->center_freq
);
826 chan
->flags
= IEEE80211_CHAN_DISABLED
;
830 chan_reg_rule_print_dbg(chan
, desired_bw_khz
, reg_rule
);
832 power_rule
= ®_rule
->power_rule
;
833 freq_range
= ®_rule
->freq_range
;
835 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
836 bw_flags
= IEEE80211_CHAN_NO_HT40
;
838 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
839 request_wiphy
&& request_wiphy
== wiphy
&&
840 request_wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
) {
842 * This guarantees the driver's requested regulatory domain
843 * will always be used as a base for further regulatory
846 chan
->flags
= chan
->orig_flags
=
847 map_regdom_flags(reg_rule
->flags
) | bw_flags
;
848 chan
->max_antenna_gain
= chan
->orig_mag
=
849 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
850 chan
->max_power
= chan
->orig_mpwr
=
851 (int) MBM_TO_DBM(power_rule
->max_eirp
);
855 chan
->beacon_found
= false;
856 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
857 chan
->max_antenna_gain
= min(chan
->orig_mag
,
858 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
860 chan
->max_power
= min(chan
->orig_mpwr
,
861 (int) MBM_TO_DBM(power_rule
->max_eirp
));
863 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
866 static void handle_band(struct wiphy
*wiphy
,
867 enum ieee80211_band band
,
868 enum nl80211_reg_initiator initiator
)
871 struct ieee80211_supported_band
*sband
;
873 BUG_ON(!wiphy
->bands
[band
]);
874 sband
= wiphy
->bands
[band
];
876 for (i
= 0; i
< sband
->n_channels
; i
++)
877 handle_channel(wiphy
, initiator
, band
, i
);
880 static bool ignore_reg_update(struct wiphy
*wiphy
,
881 enum nl80211_reg_initiator initiator
)
884 REG_DBG_PRINT("Ignoring regulatory request %s since "
885 "last_request is not set\n",
886 reg_initiator_name(initiator
));
890 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
891 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
) {
892 REG_DBG_PRINT("Ignoring regulatory request %s "
893 "since the driver uses its own custom "
894 "regulatory domain\n",
895 reg_initiator_name(initiator
));
900 * wiphy->regd will be set once the device has its own
901 * desired regulatory domain set
903 if (wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
&& !wiphy
->regd
&&
904 initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
905 !is_world_regdom(last_request
->alpha2
)) {
906 REG_DBG_PRINT("Ignoring regulatory request %s "
907 "since the driver requires its own regulatory "
908 "domain to be set first\n",
909 reg_initiator_name(initiator
));
916 static void handle_reg_beacon(struct wiphy
*wiphy
,
917 unsigned int chan_idx
,
918 struct reg_beacon
*reg_beacon
)
920 struct ieee80211_supported_band
*sband
;
921 struct ieee80211_channel
*chan
;
922 bool channel_changed
= false;
923 struct ieee80211_channel chan_before
;
925 assert_cfg80211_lock();
927 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
928 chan
= &sband
->channels
[chan_idx
];
930 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
933 if (chan
->beacon_found
)
936 chan
->beacon_found
= true;
938 if (wiphy
->flags
& WIPHY_FLAG_DISABLE_BEACON_HINTS
)
941 chan_before
.center_freq
= chan
->center_freq
;
942 chan_before
.flags
= chan
->flags
;
944 if (chan
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
945 chan
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
946 channel_changed
= true;
949 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
) {
950 chan
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
951 channel_changed
= true;
955 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
959 * Called when a scan on a wiphy finds a beacon on
962 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
963 struct reg_beacon
*reg_beacon
)
966 struct ieee80211_supported_band
*sband
;
968 assert_cfg80211_lock();
970 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
973 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
975 for (i
= 0; i
< sband
->n_channels
; i
++)
976 handle_reg_beacon(wiphy
, i
, reg_beacon
);
980 * Called upon reg changes or a new wiphy is added
982 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
985 struct ieee80211_supported_band
*sband
;
986 struct reg_beacon
*reg_beacon
;
988 assert_cfg80211_lock();
990 if (list_empty(®_beacon_list
))
993 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
994 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
996 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
997 for (i
= 0; i
< sband
->n_channels
; i
++)
998 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1002 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1004 if (is_world_regdom(cfg80211_regdomain
->alpha2
) ||
1005 (wiphy
->regd
&& is_world_regdom(wiphy
->regd
->alpha2
)))
1008 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1009 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1014 /* Reap the advantages of previously found beacons */
1015 static void reg_process_beacons(struct wiphy
*wiphy
)
1018 * Means we are just firing up cfg80211, so no beacons would
1019 * have been processed yet.
1023 if (!reg_is_world_roaming(wiphy
))
1025 wiphy_update_beacon_reg(wiphy
);
1028 static bool is_ht40_not_allowed(struct ieee80211_channel
*chan
)
1032 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1034 /* This would happen when regulatory rules disallow HT40 completely */
1035 if (IEEE80211_CHAN_NO_HT40
== (chan
->flags
& (IEEE80211_CHAN_NO_HT40
)))
1040 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1041 enum ieee80211_band band
,
1042 unsigned int chan_idx
)
1044 struct ieee80211_supported_band
*sband
;
1045 struct ieee80211_channel
*channel
;
1046 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1049 assert_cfg80211_lock();
1051 sband
= wiphy
->bands
[band
];
1052 BUG_ON(chan_idx
>= sband
->n_channels
);
1053 channel
= &sband
->channels
[chan_idx
];
1055 if (is_ht40_not_allowed(channel
)) {
1056 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1061 * We need to ensure the extension channels exist to
1062 * be able to use HT40- or HT40+, this finds them (or not)
1064 for (i
= 0; i
< sband
->n_channels
; i
++) {
1065 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1066 if (c
->center_freq
== (channel
->center_freq
- 20))
1068 if (c
->center_freq
== (channel
->center_freq
+ 20))
1073 * Please note that this assumes target bandwidth is 20 MHz,
1074 * if that ever changes we also need to change the below logic
1075 * to include that as well.
1077 if (is_ht40_not_allowed(channel_before
))
1078 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1080 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1082 if (is_ht40_not_allowed(channel_after
))
1083 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1085 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1088 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1089 enum ieee80211_band band
)
1092 struct ieee80211_supported_band
*sband
;
1094 BUG_ON(!wiphy
->bands
[band
]);
1095 sband
= wiphy
->bands
[band
];
1097 for (i
= 0; i
< sband
->n_channels
; i
++)
1098 reg_process_ht_flags_channel(wiphy
, band
, i
);
1101 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1103 enum ieee80211_band band
;
1108 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1109 if (wiphy
->bands
[band
])
1110 reg_process_ht_flags_band(wiphy
, band
);
1115 static void wiphy_update_regulatory(struct wiphy
*wiphy
,
1116 enum nl80211_reg_initiator initiator
)
1118 enum ieee80211_band band
;
1122 if (ignore_reg_update(wiphy
, initiator
))
1125 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1126 if (wiphy
->bands
[band
])
1127 handle_band(wiphy
, band
, initiator
);
1130 reg_process_beacons(wiphy
);
1131 reg_process_ht_flags(wiphy
);
1132 if (wiphy
->reg_notifier
)
1133 wiphy
->reg_notifier(wiphy
, last_request
);
1136 void regulatory_update(struct wiphy
*wiphy
,
1137 enum nl80211_reg_initiator setby
)
1139 mutex_lock(®_mutex
);
1140 wiphy_update_regulatory(wiphy
, setby
);
1141 mutex_unlock(®_mutex
);
1144 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
1146 struct cfg80211_registered_device
*rdev
;
1148 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1149 wiphy_update_regulatory(&rdev
->wiphy
, initiator
);
1152 static void handle_channel_custom(struct wiphy
*wiphy
,
1153 enum ieee80211_band band
,
1154 unsigned int chan_idx
,
1155 const struct ieee80211_regdomain
*regd
)
1158 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
1160 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1161 const struct ieee80211_power_rule
*power_rule
= NULL
;
1162 const struct ieee80211_freq_range
*freq_range
= NULL
;
1163 struct ieee80211_supported_band
*sband
;
1164 struct ieee80211_channel
*chan
;
1168 sband
= wiphy
->bands
[band
];
1169 BUG_ON(chan_idx
>= sband
->n_channels
);
1170 chan
= &sband
->channels
[chan_idx
];
1172 r
= freq_reg_info_regd(wiphy
,
1173 MHZ_TO_KHZ(chan
->center_freq
),
1179 REG_DBG_PRINT("Disabling freq %d MHz as custom "
1180 "regd has no rule that fits a %d MHz "
1183 KHZ_TO_MHZ(desired_bw_khz
));
1184 chan
->flags
= IEEE80211_CHAN_DISABLED
;
1188 chan_reg_rule_print_dbg(chan
, desired_bw_khz
, reg_rule
);
1190 power_rule
= ®_rule
->power_rule
;
1191 freq_range
= ®_rule
->freq_range
;
1193 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1194 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1196 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1197 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1198 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1201 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
1202 const struct ieee80211_regdomain
*regd
)
1205 struct ieee80211_supported_band
*sband
;
1207 BUG_ON(!wiphy
->bands
[band
]);
1208 sband
= wiphy
->bands
[band
];
1210 for (i
= 0; i
< sband
->n_channels
; i
++)
1211 handle_channel_custom(wiphy
, band
, i
, regd
);
1214 /* Used by drivers prior to wiphy registration */
1215 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1216 const struct ieee80211_regdomain
*regd
)
1218 enum ieee80211_band band
;
1219 unsigned int bands_set
= 0;
1221 mutex_lock(®_mutex
);
1222 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1223 if (!wiphy
->bands
[band
])
1225 handle_band_custom(wiphy
, band
, regd
);
1228 mutex_unlock(®_mutex
);
1231 * no point in calling this if it won't have any effect
1232 * on your device's supportd bands.
1234 WARN_ON(!bands_set
);
1236 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1239 * Return value which can be used by ignore_request() to indicate
1240 * it has been determined we should intersect two regulatory domains
1242 #define REG_INTERSECT 1
1244 /* This has the logic which determines when a new request
1245 * should be ignored. */
1246 static int ignore_request(struct wiphy
*wiphy
,
1247 struct regulatory_request
*pending_request
)
1249 struct wiphy
*last_wiphy
= NULL
;
1251 assert_cfg80211_lock();
1253 /* All initial requests are respected */
1257 switch (pending_request
->initiator
) {
1258 case NL80211_REGDOM_SET_BY_CORE
:
1260 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1262 last_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1264 if (unlikely(!is_an_alpha2(pending_request
->alpha2
)))
1266 if (last_request
->initiator
==
1267 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1268 if (last_wiphy
!= wiphy
) {
1270 * Two cards with two APs claiming different
1271 * Country IE alpha2s. We could
1272 * intersect them, but that seems unlikely
1273 * to be correct. Reject second one for now.
1275 if (regdom_changes(pending_request
->alpha2
))
1280 * Two consecutive Country IE hints on the same wiphy.
1281 * This should be picked up early by the driver/stack
1283 if (WARN_ON(regdom_changes(pending_request
->alpha2
)))
1288 case NL80211_REGDOM_SET_BY_DRIVER
:
1289 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1290 if (regdom_changes(pending_request
->alpha2
))
1296 * This would happen if you unplug and plug your card
1297 * back in or if you add a new device for which the previously
1298 * loaded card also agrees on the regulatory domain.
1300 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1301 !regdom_changes(pending_request
->alpha2
))
1304 return REG_INTERSECT
;
1305 case NL80211_REGDOM_SET_BY_USER
:
1306 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1307 return REG_INTERSECT
;
1309 * If the user knows better the user should set the regdom
1310 * to their country before the IE is picked up
1312 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1313 last_request
->intersect
)
1316 * Process user requests only after previous user/driver/core
1317 * requests have been processed
1319 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1320 last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1321 last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1322 if (regdom_changes(last_request
->alpha2
))
1326 if (!regdom_changes(pending_request
->alpha2
))
1335 static void reg_set_request_processed(void)
1337 bool need_more_processing
= false;
1339 last_request
->processed
= true;
1341 spin_lock(®_requests_lock
);
1342 if (!list_empty(®_requests_list
))
1343 need_more_processing
= true;
1344 spin_unlock(®_requests_lock
);
1346 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
)
1347 cancel_delayed_work_sync(®_timeout
);
1349 if (need_more_processing
)
1350 schedule_work(®_work
);
1354 * __regulatory_hint - hint to the wireless core a regulatory domain
1355 * @wiphy: if the hint comes from country information from an AP, this
1356 * is required to be set to the wiphy that received the information
1357 * @pending_request: the regulatory request currently being processed
1359 * The Wireless subsystem can use this function to hint to the wireless core
1360 * what it believes should be the current regulatory domain.
1362 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1363 * already been set or other standard error codes.
1365 * Caller must hold &cfg80211_mutex and ®_mutex
1367 static int __regulatory_hint(struct wiphy
*wiphy
,
1368 struct regulatory_request
*pending_request
)
1370 bool intersect
= false;
1373 assert_cfg80211_lock();
1375 r
= ignore_request(wiphy
, pending_request
);
1377 if (r
== REG_INTERSECT
) {
1378 if (pending_request
->initiator
==
1379 NL80211_REGDOM_SET_BY_DRIVER
) {
1380 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1382 kfree(pending_request
);
1389 * If the regulatory domain being requested by the
1390 * driver has already been set just copy it to the
1393 if (r
== -EALREADY
&&
1394 pending_request
->initiator
==
1395 NL80211_REGDOM_SET_BY_DRIVER
) {
1396 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1398 kfree(pending_request
);
1404 kfree(pending_request
);
1409 kfree(last_request
);
1411 last_request
= pending_request
;
1412 last_request
->intersect
= intersect
;
1414 pending_request
= NULL
;
1416 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1417 user_alpha2
[0] = last_request
->alpha2
[0];
1418 user_alpha2
[1] = last_request
->alpha2
[1];
1421 /* When r == REG_INTERSECT we do need to call CRDA */
1424 * Since CRDA will not be called in this case as we already
1425 * have applied the requested regulatory domain before we just
1426 * inform userspace we have processed the request
1428 if (r
== -EALREADY
) {
1429 nl80211_send_reg_change_event(last_request
);
1430 reg_set_request_processed();
1435 return call_crda(last_request
->alpha2
);
1438 /* This processes *all* regulatory hints */
1439 static void reg_process_hint(struct regulatory_request
*reg_request
)
1442 struct wiphy
*wiphy
= NULL
;
1443 enum nl80211_reg_initiator initiator
= reg_request
->initiator
;
1445 BUG_ON(!reg_request
->alpha2
);
1447 if (wiphy_idx_valid(reg_request
->wiphy_idx
))
1448 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1450 if (reg_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1456 r
= __regulatory_hint(wiphy
, reg_request
);
1457 /* This is required so that the orig_* parameters are saved */
1458 if (r
== -EALREADY
&& wiphy
&&
1459 wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
) {
1460 wiphy_update_regulatory(wiphy
, initiator
);
1465 * We only time out user hints, given that they should be the only
1466 * source of bogus requests.
1468 if (r
!= -EALREADY
&&
1469 reg_request
->initiator
== NL80211_REGDOM_SET_BY_USER
)
1470 schedule_delayed_work(®_timeout
, msecs_to_jiffies(3142));
1474 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1475 * Regulatory hints come on a first come first serve basis and we
1476 * must process each one atomically.
1478 static void reg_process_pending_hints(void)
1480 struct regulatory_request
*reg_request
;
1482 mutex_lock(&cfg80211_mutex
);
1483 mutex_lock(®_mutex
);
1485 /* When last_request->processed becomes true this will be rescheduled */
1486 if (last_request
&& !last_request
->processed
) {
1487 REG_DBG_PRINT("Pending regulatory request, waiting "
1488 "for it to be processed...\n");
1492 spin_lock(®_requests_lock
);
1494 if (list_empty(®_requests_list
)) {
1495 spin_unlock(®_requests_lock
);
1499 reg_request
= list_first_entry(®_requests_list
,
1500 struct regulatory_request
,
1502 list_del_init(®_request
->list
);
1504 spin_unlock(®_requests_lock
);
1506 reg_process_hint(reg_request
);
1509 mutex_unlock(®_mutex
);
1510 mutex_unlock(&cfg80211_mutex
);
1513 /* Processes beacon hints -- this has nothing to do with country IEs */
1514 static void reg_process_pending_beacon_hints(void)
1516 struct cfg80211_registered_device
*rdev
;
1517 struct reg_beacon
*pending_beacon
, *tmp
;
1520 * No need to hold the reg_mutex here as we just touch wiphys
1521 * and do not read or access regulatory variables.
1523 mutex_lock(&cfg80211_mutex
);
1525 /* This goes through the _pending_ beacon list */
1526 spin_lock_bh(®_pending_beacons_lock
);
1528 if (list_empty(®_pending_beacons
)) {
1529 spin_unlock_bh(®_pending_beacons_lock
);
1533 list_for_each_entry_safe(pending_beacon
, tmp
,
1534 ®_pending_beacons
, list
) {
1536 list_del_init(&pending_beacon
->list
);
1538 /* Applies the beacon hint to current wiphys */
1539 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1540 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1542 /* Remembers the beacon hint for new wiphys or reg changes */
1543 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1546 spin_unlock_bh(®_pending_beacons_lock
);
1548 mutex_unlock(&cfg80211_mutex
);
1551 static void reg_todo(struct work_struct
*work
)
1553 reg_process_pending_hints();
1554 reg_process_pending_beacon_hints();
1557 static void queue_regulatory_request(struct regulatory_request
*request
)
1559 if (isalpha(request
->alpha2
[0]))
1560 request
->alpha2
[0] = toupper(request
->alpha2
[0]);
1561 if (isalpha(request
->alpha2
[1]))
1562 request
->alpha2
[1] = toupper(request
->alpha2
[1]);
1564 spin_lock(®_requests_lock
);
1565 list_add_tail(&request
->list
, ®_requests_list
);
1566 spin_unlock(®_requests_lock
);
1568 schedule_work(®_work
);
1572 * Core regulatory hint -- happens during cfg80211_init()
1573 * and when we restore regulatory settings.
1575 static int regulatory_hint_core(const char *alpha2
)
1577 struct regulatory_request
*request
;
1579 kfree(last_request
);
1580 last_request
= NULL
;
1582 request
= kzalloc(sizeof(struct regulatory_request
),
1587 request
->alpha2
[0] = alpha2
[0];
1588 request
->alpha2
[1] = alpha2
[1];
1589 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1591 queue_regulatory_request(request
);
1597 int regulatory_hint_user(const char *alpha2
)
1599 struct regulatory_request
*request
;
1603 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1607 request
->wiphy_idx
= WIPHY_IDX_STALE
;
1608 request
->alpha2
[0] = alpha2
[0];
1609 request
->alpha2
[1] = alpha2
[1];
1610 request
->initiator
= NL80211_REGDOM_SET_BY_USER
;
1612 queue_regulatory_request(request
);
1618 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1620 struct regulatory_request
*request
;
1625 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1629 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1631 /* Must have registered wiphy first */
1632 BUG_ON(!wiphy_idx_valid(request
->wiphy_idx
));
1634 request
->alpha2
[0] = alpha2
[0];
1635 request
->alpha2
[1] = alpha2
[1];
1636 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
1638 queue_regulatory_request(request
);
1642 EXPORT_SYMBOL(regulatory_hint
);
1645 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1646 * therefore cannot iterate over the rdev list here.
1648 void regulatory_hint_11d(struct wiphy
*wiphy
,
1649 enum ieee80211_band band
,
1654 enum environment_cap env
= ENVIRON_ANY
;
1655 struct regulatory_request
*request
;
1657 mutex_lock(®_mutex
);
1659 if (unlikely(!last_request
))
1662 /* IE len must be evenly divisible by 2 */
1663 if (country_ie_len
& 0x01)
1666 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
1669 alpha2
[0] = country_ie
[0];
1670 alpha2
[1] = country_ie
[1];
1672 if (country_ie
[2] == 'I')
1673 env
= ENVIRON_INDOOR
;
1674 else if (country_ie
[2] == 'O')
1675 env
= ENVIRON_OUTDOOR
;
1678 * We will run this only upon a successful connection on cfg80211.
1679 * We leave conflict resolution to the workqueue, where can hold
1682 if (likely(last_request
->initiator
==
1683 NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1684 wiphy_idx_valid(last_request
->wiphy_idx
)))
1687 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1691 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1692 request
->alpha2
[0] = alpha2
[0];
1693 request
->alpha2
[1] = alpha2
[1];
1694 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
1695 request
->country_ie_env
= env
;
1697 mutex_unlock(®_mutex
);
1699 queue_regulatory_request(request
);
1704 mutex_unlock(®_mutex
);
1707 static void restore_alpha2(char *alpha2
, bool reset_user
)
1709 /* indicates there is no alpha2 to consider for restoration */
1713 /* The user setting has precedence over the module parameter */
1714 if (is_user_regdom_saved()) {
1715 /* Unless we're asked to ignore it and reset it */
1717 REG_DBG_PRINT("Restoring regulatory settings "
1718 "including user preference\n");
1719 user_alpha2
[0] = '9';
1720 user_alpha2
[1] = '7';
1723 * If we're ignoring user settings, we still need to
1724 * check the module parameter to ensure we put things
1725 * back as they were for a full restore.
1727 if (!is_world_regdom(ieee80211_regdom
)) {
1728 REG_DBG_PRINT("Keeping preference on "
1729 "module parameter ieee80211_regdom: %c%c\n",
1730 ieee80211_regdom
[0],
1731 ieee80211_regdom
[1]);
1732 alpha2
[0] = ieee80211_regdom
[0];
1733 alpha2
[1] = ieee80211_regdom
[1];
1736 REG_DBG_PRINT("Restoring regulatory settings "
1737 "while preserving user preference for: %c%c\n",
1740 alpha2
[0] = user_alpha2
[0];
1741 alpha2
[1] = user_alpha2
[1];
1743 } else if (!is_world_regdom(ieee80211_regdom
)) {
1744 REG_DBG_PRINT("Keeping preference on "
1745 "module parameter ieee80211_regdom: %c%c\n",
1746 ieee80211_regdom
[0],
1747 ieee80211_regdom
[1]);
1748 alpha2
[0] = ieee80211_regdom
[0];
1749 alpha2
[1] = ieee80211_regdom
[1];
1751 REG_DBG_PRINT("Restoring regulatory settings\n");
1755 * Restoring regulatory settings involves ingoring any
1756 * possibly stale country IE information and user regulatory
1757 * settings if so desired, this includes any beacon hints
1758 * learned as we could have traveled outside to another country
1759 * after disconnection. To restore regulatory settings we do
1760 * exactly what we did at bootup:
1762 * - send a core regulatory hint
1763 * - send a user regulatory hint if applicable
1765 * Device drivers that send a regulatory hint for a specific country
1766 * keep their own regulatory domain on wiphy->regd so that does does
1767 * not need to be remembered.
1769 static void restore_regulatory_settings(bool reset_user
)
1772 struct reg_beacon
*reg_beacon
, *btmp
;
1773 struct regulatory_request
*reg_request
, *tmp
;
1774 LIST_HEAD(tmp_reg_req_list
);
1776 mutex_lock(&cfg80211_mutex
);
1777 mutex_lock(®_mutex
);
1780 restore_alpha2(alpha2
, reset_user
);
1783 * If there's any pending requests we simply
1784 * stash them to a temporary pending queue and
1785 * add then after we've restored regulatory
1788 spin_lock(®_requests_lock
);
1789 if (!list_empty(®_requests_list
)) {
1790 list_for_each_entry_safe(reg_request
, tmp
,
1791 ®_requests_list
, list
) {
1792 if (reg_request
->initiator
!=
1793 NL80211_REGDOM_SET_BY_USER
)
1795 list_del(®_request
->list
);
1796 list_add_tail(®_request
->list
, &tmp_reg_req_list
);
1799 spin_unlock(®_requests_lock
);
1801 /* Clear beacon hints */
1802 spin_lock_bh(®_pending_beacons_lock
);
1803 if (!list_empty(®_pending_beacons
)) {
1804 list_for_each_entry_safe(reg_beacon
, btmp
,
1805 ®_pending_beacons
, list
) {
1806 list_del(®_beacon
->list
);
1810 spin_unlock_bh(®_pending_beacons_lock
);
1812 if (!list_empty(®_beacon_list
)) {
1813 list_for_each_entry_safe(reg_beacon
, btmp
,
1814 ®_beacon_list
, list
) {
1815 list_del(®_beacon
->list
);
1820 /* First restore to the basic regulatory settings */
1821 cfg80211_regdomain
= cfg80211_world_regdom
;
1823 mutex_unlock(®_mutex
);
1824 mutex_unlock(&cfg80211_mutex
);
1826 regulatory_hint_core(cfg80211_regdomain
->alpha2
);
1829 * This restores the ieee80211_regdom module parameter
1830 * preference or the last user requested regulatory
1831 * settings, user regulatory settings takes precedence.
1833 if (is_an_alpha2(alpha2
))
1834 regulatory_hint_user(user_alpha2
);
1836 if (list_empty(&tmp_reg_req_list
))
1839 mutex_lock(&cfg80211_mutex
);
1840 mutex_lock(®_mutex
);
1842 spin_lock(®_requests_lock
);
1843 list_for_each_entry_safe(reg_request
, tmp
, &tmp_reg_req_list
, list
) {
1844 REG_DBG_PRINT("Adding request for country %c%c back "
1846 reg_request
->alpha2
[0],
1847 reg_request
->alpha2
[1]);
1848 list_del(®_request
->list
);
1849 list_add_tail(®_request
->list
, ®_requests_list
);
1851 spin_unlock(®_requests_lock
);
1853 mutex_unlock(®_mutex
);
1854 mutex_unlock(&cfg80211_mutex
);
1856 REG_DBG_PRINT("Kicking the queue\n");
1858 schedule_work(®_work
);
1861 void regulatory_hint_disconnect(void)
1863 REG_DBG_PRINT("All devices are disconnected, going to "
1864 "restore regulatory settings\n");
1865 restore_regulatory_settings(false);
1868 static bool freq_is_chan_12_13_14(u16 freq
)
1870 if (freq
== ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ
) ||
1871 freq
== ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ
) ||
1872 freq
== ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ
))
1877 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
1878 struct ieee80211_channel
*beacon_chan
,
1881 struct reg_beacon
*reg_beacon
;
1883 if (likely((beacon_chan
->beacon_found
||
1884 (beacon_chan
->flags
& IEEE80211_CHAN_RADAR
) ||
1885 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
1886 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))))
1889 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
1893 REG_DBG_PRINT("Found new beacon on "
1894 "frequency: %d MHz (Ch %d) on %s\n",
1895 beacon_chan
->center_freq
,
1896 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
1899 memcpy(®_beacon
->chan
, beacon_chan
,
1900 sizeof(struct ieee80211_channel
));
1904 * Since we can be called from BH or and non-BH context
1905 * we must use spin_lock_bh()
1907 spin_lock_bh(®_pending_beacons_lock
);
1908 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
1909 spin_unlock_bh(®_pending_beacons_lock
);
1911 schedule_work(®_work
);
1916 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
1919 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1920 const struct ieee80211_freq_range
*freq_range
= NULL
;
1921 const struct ieee80211_power_rule
*power_rule
= NULL
;
1923 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
1925 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
1926 reg_rule
= &rd
->reg_rules
[i
];
1927 freq_range
= ®_rule
->freq_range
;
1928 power_rule
= ®_rule
->power_rule
;
1931 * There may not be documentation for max antenna gain
1932 * in certain regions
1934 if (power_rule
->max_antenna_gain
)
1935 pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
1936 freq_range
->start_freq_khz
,
1937 freq_range
->end_freq_khz
,
1938 freq_range
->max_bandwidth_khz
,
1939 power_rule
->max_antenna_gain
,
1940 power_rule
->max_eirp
);
1942 pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
1943 freq_range
->start_freq_khz
,
1944 freq_range
->end_freq_khz
,
1945 freq_range
->max_bandwidth_khz
,
1946 power_rule
->max_eirp
);
1950 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
1953 if (is_intersected_alpha2(rd
->alpha2
)) {
1955 if (last_request
->initiator
==
1956 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1957 struct cfg80211_registered_device
*rdev
;
1958 rdev
= cfg80211_rdev_by_wiphy_idx(
1959 last_request
->wiphy_idx
);
1961 pr_info("Current regulatory domain updated by AP to: %c%c\n",
1962 rdev
->country_ie_alpha2
[0],
1963 rdev
->country_ie_alpha2
[1]);
1965 pr_info("Current regulatory domain intersected:\n");
1967 pr_info("Current regulatory domain intersected:\n");
1968 } else if (is_world_regdom(rd
->alpha2
))
1969 pr_info("World regulatory domain updated:\n");
1971 if (is_unknown_alpha2(rd
->alpha2
))
1972 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
1974 pr_info("Regulatory domain changed to country: %c%c\n",
1975 rd
->alpha2
[0], rd
->alpha2
[1]);
1980 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
1982 pr_info("Regulatory domain: %c%c\n", rd
->alpha2
[0], rd
->alpha2
[1]);
1986 /* Takes ownership of rd only if it doesn't fail */
1987 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
1989 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
1990 struct cfg80211_registered_device
*rdev
= NULL
;
1991 struct wiphy
*request_wiphy
;
1992 /* Some basic sanity checks first */
1994 if (is_world_regdom(rd
->alpha2
)) {
1995 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
1997 update_world_regdomain(rd
);
2001 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2002 !is_unknown_alpha2(rd
->alpha2
))
2009 * Lets only bother proceeding on the same alpha2 if the current
2010 * rd is non static (it means CRDA was present and was used last)
2011 * and the pending request came in from a country IE
2013 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2015 * If someone else asked us to change the rd lets only bother
2016 * checking if the alpha2 changes if CRDA was already called
2018 if (!regdom_changes(rd
->alpha2
))
2023 * Now lets set the regulatory domain, update all driver channels
2024 * and finally inform them of what we have done, in case they want
2025 * to review or adjust their own settings based on their own
2026 * internal EEPROM data
2029 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2032 if (!is_valid_rd(rd
)) {
2033 pr_err("Invalid regulatory domain detected:\n");
2034 print_regdomain_info(rd
);
2038 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2040 if (!last_request
->intersect
) {
2043 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_DRIVER
) {
2045 cfg80211_regdomain
= rd
;
2050 * For a driver hint, lets copy the regulatory domain the
2051 * driver wanted to the wiphy to deal with conflicts
2055 * Userspace could have sent two replies with only
2056 * one kernel request.
2058 if (request_wiphy
->regd
)
2061 r
= reg_copy_regd(&request_wiphy
->regd
, rd
);
2066 cfg80211_regdomain
= rd
;
2070 /* Intersection requires a bit more work */
2072 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2074 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
2075 if (!intersected_rd
)
2079 * We can trash what CRDA provided now.
2080 * However if a driver requested this specific regulatory
2081 * domain we keep it for its private use
2083 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
)
2084 request_wiphy
->regd
= rd
;
2091 cfg80211_regdomain
= intersected_rd
;
2096 if (!intersected_rd
)
2099 rdev
= wiphy_to_dev(request_wiphy
);
2101 rdev
->country_ie_alpha2
[0] = rd
->alpha2
[0];
2102 rdev
->country_ie_alpha2
[1] = rd
->alpha2
[1];
2103 rdev
->env
= last_request
->country_ie_env
;
2105 BUG_ON(intersected_rd
== rd
);
2111 cfg80211_regdomain
= intersected_rd
;
2118 * Use this call to set the current regulatory domain. Conflicts with
2119 * multiple drivers can be ironed out later. Caller must've already
2120 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2122 int set_regdom(const struct ieee80211_regdomain
*rd
)
2126 assert_cfg80211_lock();
2128 mutex_lock(®_mutex
);
2130 /* Note that this doesn't update the wiphys, this is done below */
2131 r
= __set_regdom(rd
);
2134 mutex_unlock(®_mutex
);
2138 /* This would make this whole thing pointless */
2139 if (!last_request
->intersect
)
2140 BUG_ON(rd
!= cfg80211_regdomain
);
2142 /* update all wiphys now with the new established regulatory domain */
2143 update_all_wiphy_regulatory(last_request
->initiator
);
2145 print_regdomain(cfg80211_regdomain
);
2147 nl80211_send_reg_change_event(last_request
);
2149 reg_set_request_processed();
2151 mutex_unlock(®_mutex
);
2156 #ifdef CONFIG_HOTPLUG
2157 int reg_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
2159 if (last_request
&& !last_request
->processed
) {
2160 if (add_uevent_var(env
, "COUNTRY=%c%c",
2161 last_request
->alpha2
[0],
2162 last_request
->alpha2
[1]))
2169 int reg_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
2173 #endif /* CONFIG_HOTPLUG */
2175 /* Caller must hold cfg80211_mutex */
2176 void reg_device_remove(struct wiphy
*wiphy
)
2178 struct wiphy
*request_wiphy
= NULL
;
2180 assert_cfg80211_lock();
2182 mutex_lock(®_mutex
);
2187 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2189 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2192 last_request
->wiphy_idx
= WIPHY_IDX_STALE
;
2193 last_request
->country_ie_env
= ENVIRON_ANY
;
2195 mutex_unlock(®_mutex
);
2198 static void reg_timeout_work(struct work_struct
*work
)
2200 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, "
2201 "restoring regulatory settings\n");
2202 restore_regulatory_settings(true);
2205 int __init
regulatory_init(void)
2209 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2210 if (IS_ERR(reg_pdev
))
2211 return PTR_ERR(reg_pdev
);
2213 reg_pdev
->dev
.type
= ®_device_type
;
2215 spin_lock_init(®_requests_lock
);
2216 spin_lock_init(®_pending_beacons_lock
);
2218 cfg80211_regdomain
= cfg80211_world_regdom
;
2220 user_alpha2
[0] = '9';
2221 user_alpha2
[1] = '7';
2223 /* We always try to get an update for the static regdomain */
2224 err
= regulatory_hint_core(cfg80211_regdomain
->alpha2
);
2229 * N.B. kobject_uevent_env() can fail mainly for when we're out
2230 * memory which is handled and propagated appropriately above
2231 * but it can also fail during a netlink_broadcast() or during
2232 * early boot for call_usermodehelper(). For now treat these
2233 * errors as non-fatal.
2235 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
2236 #ifdef CONFIG_CFG80211_REG_DEBUG
2237 /* We want to find out exactly why when debugging */
2243 * Finally, if the user set the module parameter treat it
2246 if (!is_world_regdom(ieee80211_regdom
))
2247 regulatory_hint_user(ieee80211_regdom
);
2252 void /* __init_or_exit */ regulatory_exit(void)
2254 struct regulatory_request
*reg_request
, *tmp
;
2255 struct reg_beacon
*reg_beacon
, *btmp
;
2257 cancel_work_sync(®_work
);
2258 cancel_delayed_work_sync(®_timeout
);
2260 mutex_lock(&cfg80211_mutex
);
2261 mutex_lock(®_mutex
);
2265 kfree(last_request
);
2267 platform_device_unregister(reg_pdev
);
2269 spin_lock_bh(®_pending_beacons_lock
);
2270 if (!list_empty(®_pending_beacons
)) {
2271 list_for_each_entry_safe(reg_beacon
, btmp
,
2272 ®_pending_beacons
, list
) {
2273 list_del(®_beacon
->list
);
2277 spin_unlock_bh(®_pending_beacons_lock
);
2279 if (!list_empty(®_beacon_list
)) {
2280 list_for_each_entry_safe(reg_beacon
, btmp
,
2281 ®_beacon_list
, list
) {
2282 list_del(®_beacon
->list
);
2287 spin_lock(®_requests_lock
);
2288 if (!list_empty(®_requests_list
)) {
2289 list_for_each_entry_safe(reg_request
, tmp
,
2290 ®_requests_list
, list
) {
2291 list_del(®_request
->list
);
2295 spin_unlock(®_requests_lock
);
2297 mutex_unlock(®_mutex
);
2298 mutex_unlock(&cfg80211_mutex
);