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.
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/random.h>
38 #include <linux/nl80211.h>
39 #include <linux/platform_device.h>
40 #include <net/cfg80211.h>
46 #ifdef CONFIG_CFG80211_REG_DEBUG
47 #define REG_DBG_PRINT(format, args...) \
49 printk(KERN_DEBUG format , ## args); \
52 #define REG_DBG_PRINT(args...)
55 /* Receipt of information from last regulatory request */
56 static struct regulatory_request
*last_request
;
58 /* To trigger userspace events */
59 static struct platform_device
*reg_pdev
;
62 * Central wireless core regulatory domains, we only need two,
63 * the current one and a world regulatory domain in case we have no
64 * information to give us an alpha2
66 const struct ieee80211_regdomain
*cfg80211_regdomain
;
69 * We use this as a place for the rd structure built from the
70 * last parsed country IE to rest until CRDA gets back to us with
71 * what it thinks should apply for the same country
73 static const struct ieee80211_regdomain
*country_ie_regdomain
;
76 * Protects static reg.c components:
77 * - cfg80211_world_regdom
79 * - country_ie_regdomain
82 DEFINE_MUTEX(reg_mutex
);
83 #define assert_reg_lock() WARN_ON(!mutex_is_locked(®_mutex))
85 /* Used to queue up regulatory hints */
86 static LIST_HEAD(reg_requests_list
);
87 static spinlock_t reg_requests_lock
;
89 /* Used to queue up beacon hints for review */
90 static LIST_HEAD(reg_pending_beacons
);
91 static spinlock_t reg_pending_beacons_lock
;
93 /* Used to keep track of processed beacon hints */
94 static LIST_HEAD(reg_beacon_list
);
97 struct list_head list
;
98 struct ieee80211_channel chan
;
101 /* We keep a static world regulatory domain in case of the absence of CRDA */
102 static const struct ieee80211_regdomain world_regdom
= {
106 /* IEEE 802.11b/g, channels 1..11 */
107 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
108 /* IEEE 802.11b/g, channels 12..13. No HT40
109 * channel fits here. */
110 REG_RULE(2467-10, 2472+10, 20, 6, 20,
111 NL80211_RRF_PASSIVE_SCAN
|
112 NL80211_RRF_NO_IBSS
),
113 /* IEEE 802.11 channel 14 - Only JP enables
114 * this and for 802.11b only */
115 REG_RULE(2484-10, 2484+10, 20, 6, 20,
116 NL80211_RRF_PASSIVE_SCAN
|
117 NL80211_RRF_NO_IBSS
|
118 NL80211_RRF_NO_OFDM
),
119 /* IEEE 802.11a, channel 36..48 */
120 REG_RULE(5180-10, 5240+10, 40, 6, 20,
121 NL80211_RRF_PASSIVE_SCAN
|
122 NL80211_RRF_NO_IBSS
),
124 /* NB: 5260 MHz - 5700 MHz requies DFS */
126 /* IEEE 802.11a, channel 149..165 */
127 REG_RULE(5745-10, 5825+10, 40, 6, 20,
128 NL80211_RRF_PASSIVE_SCAN
|
129 NL80211_RRF_NO_IBSS
),
133 static const struct ieee80211_regdomain
*cfg80211_world_regdom
=
136 static char *ieee80211_regdom
= "00";
137 static char user_alpha2
[2];
139 module_param(ieee80211_regdom
, charp
, 0444);
140 MODULE_PARM_DESC(ieee80211_regdom
, "IEEE 802.11 regulatory domain code");
142 static void reset_regdomains(void)
144 /* avoid freeing static information or freeing something twice */
145 if (cfg80211_regdomain
== cfg80211_world_regdom
)
146 cfg80211_regdomain
= NULL
;
147 if (cfg80211_world_regdom
== &world_regdom
)
148 cfg80211_world_regdom
= NULL
;
149 if (cfg80211_regdomain
== &world_regdom
)
150 cfg80211_regdomain
= NULL
;
152 kfree(cfg80211_regdomain
);
153 kfree(cfg80211_world_regdom
);
155 cfg80211_world_regdom
= &world_regdom
;
156 cfg80211_regdomain
= NULL
;
160 * Dynamic world regulatory domain requested by the wireless
161 * core upon initialization
163 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
165 BUG_ON(!last_request
);
169 cfg80211_world_regdom
= rd
;
170 cfg80211_regdomain
= rd
;
173 bool is_world_regdom(const char *alpha2
)
177 if (alpha2
[0] == '0' && alpha2
[1] == '0')
182 static bool is_alpha2_set(const char *alpha2
)
186 if (alpha2
[0] != 0 && alpha2
[1] != 0)
191 static bool is_alpha_upper(char letter
)
194 if (letter
>= 65 && letter
<= 90)
199 static bool is_unknown_alpha2(const char *alpha2
)
204 * Special case where regulatory domain was built by driver
205 * but a specific alpha2 cannot be determined
207 if (alpha2
[0] == '9' && alpha2
[1] == '9')
212 static bool is_intersected_alpha2(const char *alpha2
)
217 * Special case where regulatory domain is the
218 * result of an intersection between two regulatory domain
221 if (alpha2
[0] == '9' && alpha2
[1] == '8')
226 static bool is_an_alpha2(const char *alpha2
)
230 if (is_alpha_upper(alpha2
[0]) && is_alpha_upper(alpha2
[1]))
235 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
237 if (!alpha2_x
|| !alpha2_y
)
239 if (alpha2_x
[0] == alpha2_y
[0] &&
240 alpha2_x
[1] == alpha2_y
[1])
245 static bool regdom_changes(const char *alpha2
)
247 assert_cfg80211_lock();
249 if (!cfg80211_regdomain
)
251 if (alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
257 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
258 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
259 * has ever been issued.
261 static bool is_user_regdom_saved(void)
263 if (user_alpha2
[0] == '9' && user_alpha2
[1] == '7')
266 /* This would indicate a mistake on the design */
267 if (WARN((!is_world_regdom(user_alpha2
) &&
268 !is_an_alpha2(user_alpha2
)),
269 "Unexpected user alpha2: %c%c\n",
278 * country_ie_integrity_changes - tells us if the country IE has changed
279 * @checksum: checksum of country IE of fields we are interested in
281 * If the country IE has not changed you can ignore it safely. This is
282 * useful to determine if two devices are seeing two different country IEs
283 * even on the same alpha2. Note that this will return false if no IE has
284 * been set on the wireless core yet.
286 static bool country_ie_integrity_changes(u32 checksum
)
288 /* If no IE has been set then the checksum doesn't change */
289 if (unlikely(!last_request
->country_ie_checksum
))
291 if (unlikely(last_request
->country_ie_checksum
!= checksum
))
296 static int reg_copy_regd(const struct ieee80211_regdomain
**dst_regd
,
297 const struct ieee80211_regdomain
*src_regd
)
299 struct ieee80211_regdomain
*regd
;
300 int size_of_regd
= 0;
303 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
304 ((src_regd
->n_reg_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
306 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
310 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
312 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
313 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
314 sizeof(struct ieee80211_reg_rule
));
320 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
321 struct reg_regdb_search_request
{
323 struct list_head list
;
326 static LIST_HEAD(reg_regdb_search_list
);
327 static DEFINE_SPINLOCK(reg_regdb_search_lock
);
329 static void reg_regdb_search(struct work_struct
*work
)
331 struct reg_regdb_search_request
*request
;
332 const struct ieee80211_regdomain
*curdom
, *regdom
;
335 spin_lock(®_regdb_search_lock
);
336 while (!list_empty(®_regdb_search_list
)) {
337 request
= list_first_entry(®_regdb_search_list
,
338 struct reg_regdb_search_request
,
340 list_del(&request
->list
);
342 for (i
=0; i
<reg_regdb_size
; i
++) {
343 curdom
= reg_regdb
[i
];
345 if (!memcmp(request
->alpha2
, curdom
->alpha2
, 2)) {
346 r
= reg_copy_regd(®dom
, curdom
);
349 spin_unlock(®_regdb_search_lock
);
350 mutex_lock(&cfg80211_mutex
);
352 mutex_unlock(&cfg80211_mutex
);
353 spin_lock(®_regdb_search_lock
);
360 spin_unlock(®_regdb_search_lock
);
363 static DECLARE_WORK(reg_regdb_work
, reg_regdb_search
);
365 static void reg_regdb_query(const char *alpha2
)
367 struct reg_regdb_search_request
*request
;
372 request
= kzalloc(sizeof(struct reg_regdb_search_request
), GFP_KERNEL
);
376 memcpy(request
->alpha2
, alpha2
, 2);
378 spin_lock(®_regdb_search_lock
);
379 list_add_tail(&request
->list
, ®_regdb_search_list
);
380 spin_unlock(®_regdb_search_lock
);
382 schedule_work(®_regdb_work
);
385 static inline void reg_regdb_query(const char *alpha2
) {}
386 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
389 * This lets us keep regulatory code which is updated on a regulatory
390 * basis in userspace.
392 static int call_crda(const char *alpha2
)
394 char country_env
[9 + 2] = "COUNTRY=";
400 if (!is_world_regdom((char *) alpha2
))
401 printk(KERN_INFO
"cfg80211: Calling CRDA for country: %c%c\n",
402 alpha2
[0], alpha2
[1]);
404 printk(KERN_INFO
"cfg80211: Calling CRDA to update world "
405 "regulatory domain\n");
407 /* query internal regulatory database (if it exists) */
408 reg_regdb_query(alpha2
);
410 country_env
[8] = alpha2
[0];
411 country_env
[9] = alpha2
[1];
413 return kobject_uevent_env(®_pdev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
416 /* Used by nl80211 before kmalloc'ing our regulatory domain */
417 bool reg_is_valid_request(const char *alpha2
)
419 assert_cfg80211_lock();
424 return alpha2_equal(last_request
->alpha2
, alpha2
);
427 /* Sanity check on a regulatory rule */
428 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
430 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
433 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
436 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
439 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
441 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
442 freq_range
->max_bandwidth_khz
> freq_diff
)
448 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
450 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
453 if (!rd
->n_reg_rules
)
456 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
459 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
460 reg_rule
= &rd
->reg_rules
[i
];
461 if (!is_valid_reg_rule(reg_rule
))
468 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
472 u32 start_freq_khz
, end_freq_khz
;
474 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
475 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
477 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
478 end_freq_khz
<= freq_range
->end_freq_khz
)
485 * freq_in_rule_band - tells us if a frequency is in a frequency band
486 * @freq_range: frequency rule we want to query
487 * @freq_khz: frequency we are inquiring about
489 * This lets us know if a specific frequency rule is or is not relevant to
490 * a specific frequency's band. Bands are device specific and artificial
491 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
492 * safe for now to assume that a frequency rule should not be part of a
493 * frequency's band if the start freq or end freq are off by more than 2 GHz.
494 * This resolution can be lowered and should be considered as we add
495 * regulatory rule support for other "bands".
497 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
500 #define ONE_GHZ_IN_KHZ 1000000
501 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
503 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
506 #undef ONE_GHZ_IN_KHZ
510 * This is a work around for sanity checking ieee80211_channel_to_frequency()'s
511 * work. ieee80211_channel_to_frequency() can for example currently provide a
512 * 2 GHz channel when in fact a 5 GHz channel was desired. An example would be
513 * an AP providing channel 8 on a country IE triplet when it sent this on the
514 * 5 GHz band, that channel is designed to be channel 8 on 5 GHz, not a 2 GHz
517 * This can be removed once ieee80211_channel_to_frequency() takes in a band.
519 static bool chan_in_band(int chan
, enum ieee80211_band band
)
521 int center_freq
= ieee80211_channel_to_frequency(chan
);
524 case IEEE80211_BAND_2GHZ
:
525 if (center_freq
<= 2484)
528 case IEEE80211_BAND_5GHZ
:
529 if (center_freq
>= 5005)
538 * Some APs may send a country IE triplet for each channel they
539 * support and while this is completely overkill and silly we still
540 * need to support it. We avoid making a single rule for each channel
541 * though and to help us with this we use this helper to find the
542 * actual subband end channel. These type of country IE triplet
543 * scenerios are handled then, all yielding two regulaotry rules from
544 * parsing a country IE:
582 * Returns 0 if the IE has been found to be invalid in the middle
585 static int max_subband_chan(enum ieee80211_band band
,
587 int orig_end_channel
,
592 u8
*triplets_start
= *country_ie
;
593 u8 len_at_triplet
= *country_ie_len
;
594 int end_subband_chan
= orig_end_channel
;
597 * We'll deal with padding for the caller unless
598 * its not immediate and we don't process any channels
600 if (*country_ie_len
== 1) {
602 *country_ie_len
-= 1;
603 return orig_end_channel
;
606 /* Move to the next triplet and then start search */
608 *country_ie_len
-= 3;
610 if (!chan_in_band(orig_cur_chan
, band
))
613 while (*country_ie_len
>= 3) {
615 struct ieee80211_country_ie_triplet
*triplet
=
616 (struct ieee80211_country_ie_triplet
*) *country_ie
;
617 int cur_channel
= 0, next_expected_chan
;
619 /* means last triplet is completely unrelated to this one */
620 if (triplet
->ext
.reg_extension_id
>=
621 IEEE80211_COUNTRY_EXTENSION_ID
) {
623 *country_ie_len
+= 3;
627 if (triplet
->chans
.first_channel
== 0) {
629 *country_ie_len
-= 1;
630 if (*country_ie_len
!= 0)
635 if (triplet
->chans
.num_channels
== 0)
638 /* Monitonically increasing channel order */
639 if (triplet
->chans
.first_channel
<= end_subband_chan
)
642 if (!chan_in_band(triplet
->chans
.first_channel
, band
))
646 if (triplet
->chans
.first_channel
<= 14) {
647 end_channel
= triplet
->chans
.first_channel
+
648 triplet
->chans
.num_channels
- 1;
651 end_channel
= triplet
->chans
.first_channel
+
652 (4 * (triplet
->chans
.num_channels
- 1));
655 if (!chan_in_band(end_channel
, band
))
658 if (orig_max_power
!= triplet
->chans
.max_power
) {
660 *country_ie_len
+= 3;
664 cur_channel
= triplet
->chans
.first_channel
;
666 /* The key is finding the right next expected channel */
667 if (band
== IEEE80211_BAND_2GHZ
)
668 next_expected_chan
= end_subband_chan
+ 1;
670 next_expected_chan
= end_subband_chan
+ 4;
672 if (cur_channel
!= next_expected_chan
) {
674 *country_ie_len
+= 3;
678 end_subband_chan
= end_channel
;
680 /* Move to the next one */
682 *country_ie_len
-= 3;
685 * Padding needs to be dealt with if we processed
688 if (*country_ie_len
== 1) {
690 *country_ie_len
-= 1;
694 /* If seen, the IE is invalid */
695 if (*country_ie_len
== 2)
699 if (end_subband_chan
== orig_end_channel
) {
700 *country_ie
= triplets_start
;
701 *country_ie_len
= len_at_triplet
;
702 return orig_end_channel
;
705 return end_subband_chan
;
709 * Converts a country IE to a regulatory domain. A regulatory domain
710 * structure has a lot of information which the IE doesn't yet have,
711 * so for the other values we use upper max values as we will intersect
712 * with our userspace regulatory agent to get lower bounds.
714 static struct ieee80211_regdomain
*country_ie_2_rd(
715 enum ieee80211_band band
,
720 struct ieee80211_regdomain
*rd
= NULL
;
724 u32 num_rules
= 0, size_of_regd
= 0;
725 u8
*triplets_start
= NULL
;
726 u8 len_at_triplet
= 0;
727 /* the last channel we have registered in a subband (triplet) */
728 int last_sub_max_channel
= 0;
730 *checksum
= 0xDEADBEEF;
732 /* Country IE requirements */
733 BUG_ON(country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
||
734 country_ie_len
& 0x01);
736 alpha2
[0] = country_ie
[0];
737 alpha2
[1] = country_ie
[1];
740 * Third octet can be:
744 * anything else we assume is no restrictions
746 if (country_ie
[2] == 'I')
747 flags
= NL80211_RRF_NO_OUTDOOR
;
748 else if (country_ie
[2] == 'O')
749 flags
= NL80211_RRF_NO_INDOOR
;
754 triplets_start
= country_ie
;
755 len_at_triplet
= country_ie_len
;
757 *checksum
^= ((flags
^ alpha2
[0] ^ alpha2
[1]) << 8);
760 * We need to build a reg rule for each triplet, but first we must
761 * calculate the number of reg rules we will need. We will need one
762 * for each channel subband
764 while (country_ie_len
>= 3) {
766 struct ieee80211_country_ie_triplet
*triplet
=
767 (struct ieee80211_country_ie_triplet
*) country_ie
;
768 int cur_sub_max_channel
= 0, cur_channel
= 0;
770 if (triplet
->ext
.reg_extension_id
>=
771 IEEE80211_COUNTRY_EXTENSION_ID
) {
778 * APs can add padding to make length divisible
779 * by two, required by the spec.
781 if (triplet
->chans
.first_channel
== 0) {
784 /* This is expected to be at the very end only */
785 if (country_ie_len
!= 0)
790 if (triplet
->chans
.num_channels
== 0)
793 if (!chan_in_band(triplet
->chans
.first_channel
, band
))
797 if (band
== IEEE80211_BAND_2GHZ
)
798 end_channel
= triplet
->chans
.first_channel
+
799 triplet
->chans
.num_channels
- 1;
802 * 5 GHz -- For example in country IEs if the first
803 * channel given is 36 and the number of channels is 4
804 * then the individual channel numbers defined for the
805 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
806 * and not 36, 37, 38, 39.
808 * See: http://tinyurl.com/11d-clarification
810 end_channel
= triplet
->chans
.first_channel
+
811 (4 * (triplet
->chans
.num_channels
- 1));
813 cur_channel
= triplet
->chans
.first_channel
;
816 * Enhancement for APs that send a triplet for every channel
817 * or for whatever reason sends triplets with multiple channels
818 * separated when in fact they should be together.
820 end_channel
= max_subband_chan(band
,
823 triplet
->chans
.max_power
,
829 if (!chan_in_band(end_channel
, band
))
832 cur_sub_max_channel
= end_channel
;
834 /* Basic sanity check */
835 if (cur_sub_max_channel
< cur_channel
)
839 * Do not allow overlapping channels. Also channels
840 * passed in each subband must be monotonically
843 if (last_sub_max_channel
) {
844 if (cur_channel
<= last_sub_max_channel
)
846 if (cur_sub_max_channel
<= last_sub_max_channel
)
851 * When dot11RegulatoryClassesRequired is supported
852 * we can throw ext triplets as part of this soup,
853 * for now we don't care when those change as we
856 *checksum
^= ((cur_channel
^ cur_sub_max_channel
) << 8) |
857 ((cur_sub_max_channel
^ cur_sub_max_channel
) << 16) |
858 ((triplet
->chans
.max_power
^ cur_sub_max_channel
) << 24);
860 last_sub_max_channel
= cur_sub_max_channel
;
864 if (country_ie_len
>= 3) {
870 * Note: this is not a IEEE requirement but
871 * simply a memory requirement
873 if (num_rules
> NL80211_MAX_SUPP_REG_RULES
)
877 country_ie
= triplets_start
;
878 country_ie_len
= len_at_triplet
;
880 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
881 (num_rules
* sizeof(struct ieee80211_reg_rule
));
883 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
887 rd
->n_reg_rules
= num_rules
;
888 rd
->alpha2
[0] = alpha2
[0];
889 rd
->alpha2
[1] = alpha2
[1];
891 /* This time around we fill in the rd */
892 while (country_ie_len
>= 3) {
894 struct ieee80211_country_ie_triplet
*triplet
=
895 (struct ieee80211_country_ie_triplet
*) country_ie
;
896 struct ieee80211_reg_rule
*reg_rule
= NULL
;
897 struct ieee80211_freq_range
*freq_range
= NULL
;
898 struct ieee80211_power_rule
*power_rule
= NULL
;
901 * Must parse if dot11RegulatoryClassesRequired is true,
902 * we don't support this yet
904 if (triplet
->ext
.reg_extension_id
>=
905 IEEE80211_COUNTRY_EXTENSION_ID
) {
911 if (triplet
->chans
.first_channel
== 0) {
917 reg_rule
= &rd
->reg_rules
[i
];
918 freq_range
= ®_rule
->freq_range
;
919 power_rule
= ®_rule
->power_rule
;
921 reg_rule
->flags
= flags
;
924 if (band
== IEEE80211_BAND_2GHZ
)
925 end_channel
= triplet
->chans
.first_channel
+
926 triplet
->chans
.num_channels
-1;
928 end_channel
= triplet
->chans
.first_channel
+
929 (4 * (triplet
->chans
.num_channels
- 1));
931 end_channel
= max_subband_chan(band
,
932 triplet
->chans
.first_channel
,
934 triplet
->chans
.max_power
,
939 * The +10 is since the regulatory domain expects
940 * the actual band edge, not the center of freq for
941 * its start and end freqs, assuming 20 MHz bandwidth on
942 * the channels passed
944 freq_range
->start_freq_khz
=
945 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
946 triplet
->chans
.first_channel
) - 10);
947 freq_range
->end_freq_khz
=
948 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
952 * These are large arbitrary values we use to intersect later.
953 * Increment this if we ever support >= 40 MHz channels
956 freq_range
->max_bandwidth_khz
= MHZ_TO_KHZ(40);
957 power_rule
->max_antenna_gain
= DBI_TO_MBI(100);
958 power_rule
->max_eirp
= DBM_TO_MBM(triplet
->chans
.max_power
);
962 if (country_ie_len
>= 3) {
967 BUG_ON(i
> NL80211_MAX_SUPP_REG_RULES
);
975 * Helper for regdom_intersect(), this does the real
976 * mathematical intersection fun
978 static int reg_rules_intersect(
979 const struct ieee80211_reg_rule
*rule1
,
980 const struct ieee80211_reg_rule
*rule2
,
981 struct ieee80211_reg_rule
*intersected_rule
)
983 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
984 struct ieee80211_freq_range
*freq_range
;
985 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
986 struct ieee80211_power_rule
*power_rule
;
989 freq_range1
= &rule1
->freq_range
;
990 freq_range2
= &rule2
->freq_range
;
991 freq_range
= &intersected_rule
->freq_range
;
993 power_rule1
= &rule1
->power_rule
;
994 power_rule2
= &rule2
->power_rule
;
995 power_rule
= &intersected_rule
->power_rule
;
997 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
998 freq_range2
->start_freq_khz
);
999 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
1000 freq_range2
->end_freq_khz
);
1001 freq_range
->max_bandwidth_khz
= min(freq_range1
->max_bandwidth_khz
,
1002 freq_range2
->max_bandwidth_khz
);
1004 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
1005 if (freq_range
->max_bandwidth_khz
> freq_diff
)
1006 freq_range
->max_bandwidth_khz
= freq_diff
;
1008 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
1009 power_rule2
->max_eirp
);
1010 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
1011 power_rule2
->max_antenna_gain
);
1013 intersected_rule
->flags
= (rule1
->flags
| rule2
->flags
);
1015 if (!is_valid_reg_rule(intersected_rule
))
1022 * regdom_intersect - do the intersection between two regulatory domains
1023 * @rd1: first regulatory domain
1024 * @rd2: second regulatory domain
1026 * Use this function to get the intersection between two regulatory domains.
1027 * Once completed we will mark the alpha2 for the rd as intersected, "98",
1028 * as no one single alpha2 can represent this regulatory domain.
1030 * Returns a pointer to the regulatory domain structure which will hold the
1031 * resulting intersection of rules between rd1 and rd2. We will
1032 * kzalloc() this structure for you.
1034 static struct ieee80211_regdomain
*regdom_intersect(
1035 const struct ieee80211_regdomain
*rd1
,
1036 const struct ieee80211_regdomain
*rd2
)
1038 int r
, size_of_regd
;
1040 unsigned int num_rules
= 0, rule_idx
= 0;
1041 const struct ieee80211_reg_rule
*rule1
, *rule2
;
1042 struct ieee80211_reg_rule
*intersected_rule
;
1043 struct ieee80211_regdomain
*rd
;
1044 /* This is just a dummy holder to help us count */
1045 struct ieee80211_reg_rule irule
;
1047 /* Uses the stack temporarily for counter arithmetic */
1048 intersected_rule
= &irule
;
1050 memset(intersected_rule
, 0, sizeof(struct ieee80211_reg_rule
));
1056 * First we get a count of the rules we'll need, then we actually
1057 * build them. This is to so we can malloc() and free() a
1058 * regdomain once. The reason we use reg_rules_intersect() here
1059 * is it will return -EINVAL if the rule computed makes no sense.
1060 * All rules that do check out OK are valid.
1063 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
1064 rule1
= &rd1
->reg_rules
[x
];
1065 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
1066 rule2
= &rd2
->reg_rules
[y
];
1067 if (!reg_rules_intersect(rule1
, rule2
,
1070 memset(intersected_rule
, 0,
1071 sizeof(struct ieee80211_reg_rule
));
1078 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
1079 ((num_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
1081 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
1085 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
1086 rule1
= &rd1
->reg_rules
[x
];
1087 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
1088 rule2
= &rd2
->reg_rules
[y
];
1090 * This time around instead of using the stack lets
1091 * write to the target rule directly saving ourselves
1094 intersected_rule
= &rd
->reg_rules
[rule_idx
];
1095 r
= reg_rules_intersect(rule1
, rule2
,
1098 * No need to memset here the intersected rule here as
1099 * we're not using the stack anymore
1107 if (rule_idx
!= num_rules
) {
1112 rd
->n_reg_rules
= num_rules
;
1113 rd
->alpha2
[0] = '9';
1114 rd
->alpha2
[1] = '8';
1120 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1121 * want to just have the channel structure use these
1123 static u32
map_regdom_flags(u32 rd_flags
)
1125 u32 channel_flags
= 0;
1126 if (rd_flags
& NL80211_RRF_PASSIVE_SCAN
)
1127 channel_flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
1128 if (rd_flags
& NL80211_RRF_NO_IBSS
)
1129 channel_flags
|= IEEE80211_CHAN_NO_IBSS
;
1130 if (rd_flags
& NL80211_RRF_DFS
)
1131 channel_flags
|= IEEE80211_CHAN_RADAR
;
1132 return channel_flags
;
1135 static int freq_reg_info_regd(struct wiphy
*wiphy
,
1138 const struct ieee80211_reg_rule
**reg_rule
,
1139 const struct ieee80211_regdomain
*custom_regd
)
1142 bool band_rule_found
= false;
1143 const struct ieee80211_regdomain
*regd
;
1144 bool bw_fits
= false;
1146 if (!desired_bw_khz
)
1147 desired_bw_khz
= MHZ_TO_KHZ(20);
1149 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
1152 * Follow the driver's regulatory domain, if present, unless a country
1153 * IE has been processed or a user wants to help complaince further
1155 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1156 last_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
1163 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
1164 const struct ieee80211_reg_rule
*rr
;
1165 const struct ieee80211_freq_range
*fr
= NULL
;
1166 const struct ieee80211_power_rule
*pr
= NULL
;
1168 rr
= ®d
->reg_rules
[i
];
1169 fr
= &rr
->freq_range
;
1170 pr
= &rr
->power_rule
;
1173 * We only need to know if one frequency rule was
1174 * was in center_freq's band, that's enough, so lets
1175 * not overwrite it once found
1177 if (!band_rule_found
)
1178 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
1180 bw_fits
= reg_does_bw_fit(fr
,
1184 if (band_rule_found
&& bw_fits
) {
1190 if (!band_rule_found
)
1195 EXPORT_SYMBOL(freq_reg_info
);
1197 int freq_reg_info(struct wiphy
*wiphy
,
1200 const struct ieee80211_reg_rule
**reg_rule
)
1202 assert_cfg80211_lock();
1203 return freq_reg_info_regd(wiphy
,
1211 * Note that right now we assume the desired channel bandwidth
1212 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1213 * per channel, the primary and the extension channel). To support
1214 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1215 * new ieee80211_channel.target_bw and re run the regulatory check
1216 * on the wiphy with the target_bw specified. Then we can simply use
1217 * that below for the desired_bw_khz below.
1219 static void handle_channel(struct wiphy
*wiphy
, enum ieee80211_band band
,
1220 unsigned int chan_idx
)
1223 u32 flags
, bw_flags
= 0;
1224 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
1225 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1226 const struct ieee80211_power_rule
*power_rule
= NULL
;
1227 const struct ieee80211_freq_range
*freq_range
= NULL
;
1228 struct ieee80211_supported_band
*sband
;
1229 struct ieee80211_channel
*chan
;
1230 struct wiphy
*request_wiphy
= NULL
;
1232 assert_cfg80211_lock();
1234 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1236 sband
= wiphy
->bands
[band
];
1237 BUG_ON(chan_idx
>= sband
->n_channels
);
1238 chan
= &sband
->channels
[chan_idx
];
1240 flags
= chan
->orig_flags
;
1242 r
= freq_reg_info(wiphy
,
1243 MHZ_TO_KHZ(chan
->center_freq
),
1249 * This means no regulatory rule was found in the country IE
1250 * with a frequency range on the center_freq's band, since
1251 * IEEE-802.11 allows for a country IE to have a subset of the
1252 * regulatory information provided in a country we ignore
1253 * disabling the channel unless at least one reg rule was
1254 * found on the center_freq's band. For details see this
1257 * http://tinyurl.com/11d-clarification
1260 last_request
->initiator
==
1261 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1262 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
1263 "intact on %s - no rule found in band on "
1265 chan
->center_freq
, wiphy_name(wiphy
));
1268 * In this case we know the country IE has at least one reg rule
1269 * for the band so we respect its band definitions
1271 if (last_request
->initiator
==
1272 NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1273 REG_DBG_PRINT("cfg80211: Disabling "
1274 "channel %d MHz on %s due to "
1276 chan
->center_freq
, wiphy_name(wiphy
));
1277 flags
|= IEEE80211_CHAN_DISABLED
;
1278 chan
->flags
= flags
;
1283 power_rule
= ®_rule
->power_rule
;
1284 freq_range
= ®_rule
->freq_range
;
1286 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1287 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1289 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1290 request_wiphy
&& request_wiphy
== wiphy
&&
1291 request_wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
) {
1293 * This gaurantees the driver's requested regulatory domain
1294 * will always be used as a base for further regulatory
1297 chan
->flags
= chan
->orig_flags
=
1298 map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1299 chan
->max_antenna_gain
= chan
->orig_mag
=
1300 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1301 chan
->max_power
= chan
->orig_mpwr
=
1302 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1306 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
1307 chan
->max_antenna_gain
= min(chan
->orig_mag
,
1308 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
1309 if (chan
->orig_mpwr
)
1310 chan
->max_power
= min(chan
->orig_mpwr
,
1311 (int) MBM_TO_DBM(power_rule
->max_eirp
));
1313 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1316 static void handle_band(struct wiphy
*wiphy
, enum ieee80211_band band
)
1319 struct ieee80211_supported_band
*sband
;
1321 BUG_ON(!wiphy
->bands
[band
]);
1322 sband
= wiphy
->bands
[band
];
1324 for (i
= 0; i
< sband
->n_channels
; i
++)
1325 handle_channel(wiphy
, band
, i
);
1328 static bool ignore_reg_update(struct wiphy
*wiphy
,
1329 enum nl80211_reg_initiator initiator
)
1333 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
1334 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1337 * wiphy->regd will be set once the device has its own
1338 * desired regulatory domain set
1340 if (wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
&& !wiphy
->regd
&&
1341 !is_world_regdom(last_request
->alpha2
))
1346 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
1348 struct cfg80211_registered_device
*rdev
;
1350 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1351 wiphy_update_regulatory(&rdev
->wiphy
, initiator
);
1354 static void handle_reg_beacon(struct wiphy
*wiphy
,
1355 unsigned int chan_idx
,
1356 struct reg_beacon
*reg_beacon
)
1358 struct ieee80211_supported_band
*sband
;
1359 struct ieee80211_channel
*chan
;
1360 bool channel_changed
= false;
1361 struct ieee80211_channel chan_before
;
1363 assert_cfg80211_lock();
1365 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1366 chan
= &sband
->channels
[chan_idx
];
1368 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
1371 if (chan
->beacon_found
)
1374 chan
->beacon_found
= true;
1376 if (wiphy
->flags
& WIPHY_FLAG_DISABLE_BEACON_HINTS
)
1379 chan_before
.center_freq
= chan
->center_freq
;
1380 chan_before
.flags
= chan
->flags
;
1382 if (chan
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
1383 chan
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
1384 channel_changed
= true;
1387 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
) {
1388 chan
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
1389 channel_changed
= true;
1392 if (channel_changed
)
1393 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
1397 * Called when a scan on a wiphy finds a beacon on
1400 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
1401 struct reg_beacon
*reg_beacon
)
1404 struct ieee80211_supported_band
*sband
;
1406 assert_cfg80211_lock();
1408 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1411 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1413 for (i
= 0; i
< sband
->n_channels
; i
++)
1414 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1418 * Called upon reg changes or a new wiphy is added
1420 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1423 struct ieee80211_supported_band
*sband
;
1424 struct reg_beacon
*reg_beacon
;
1426 assert_cfg80211_lock();
1428 if (list_empty(®_beacon_list
))
1431 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1432 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1434 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1435 for (i
= 0; i
< sband
->n_channels
; i
++)
1436 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1440 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1442 if (is_world_regdom(cfg80211_regdomain
->alpha2
) ||
1443 (wiphy
->regd
&& is_world_regdom(wiphy
->regd
->alpha2
)))
1446 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1447 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1452 /* Reap the advantages of previously found beacons */
1453 static void reg_process_beacons(struct wiphy
*wiphy
)
1456 * Means we are just firing up cfg80211, so no beacons would
1457 * have been processed yet.
1461 if (!reg_is_world_roaming(wiphy
))
1463 wiphy_update_beacon_reg(wiphy
);
1466 static bool is_ht40_not_allowed(struct ieee80211_channel
*chan
)
1470 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1472 /* This would happen when regulatory rules disallow HT40 completely */
1473 if (IEEE80211_CHAN_NO_HT40
== (chan
->flags
& (IEEE80211_CHAN_NO_HT40
)))
1478 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1479 enum ieee80211_band band
,
1480 unsigned int chan_idx
)
1482 struct ieee80211_supported_band
*sband
;
1483 struct ieee80211_channel
*channel
;
1484 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1487 assert_cfg80211_lock();
1489 sband
= wiphy
->bands
[band
];
1490 BUG_ON(chan_idx
>= sband
->n_channels
);
1491 channel
= &sband
->channels
[chan_idx
];
1493 if (is_ht40_not_allowed(channel
)) {
1494 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1499 * We need to ensure the extension channels exist to
1500 * be able to use HT40- or HT40+, this finds them (or not)
1502 for (i
= 0; i
< sband
->n_channels
; i
++) {
1503 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1504 if (c
->center_freq
== (channel
->center_freq
- 20))
1506 if (c
->center_freq
== (channel
->center_freq
+ 20))
1511 * Please note that this assumes target bandwidth is 20 MHz,
1512 * if that ever changes we also need to change the below logic
1513 * to include that as well.
1515 if (is_ht40_not_allowed(channel_before
))
1516 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1518 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1520 if (is_ht40_not_allowed(channel_after
))
1521 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1523 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1526 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1527 enum ieee80211_band band
)
1530 struct ieee80211_supported_band
*sband
;
1532 BUG_ON(!wiphy
->bands
[band
]);
1533 sband
= wiphy
->bands
[band
];
1535 for (i
= 0; i
< sband
->n_channels
; i
++)
1536 reg_process_ht_flags_channel(wiphy
, band
, i
);
1539 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1541 enum ieee80211_band band
;
1546 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1547 if (wiphy
->bands
[band
])
1548 reg_process_ht_flags_band(wiphy
, band
);
1553 void wiphy_update_regulatory(struct wiphy
*wiphy
,
1554 enum nl80211_reg_initiator initiator
)
1556 enum ieee80211_band band
;
1558 if (ignore_reg_update(wiphy
, initiator
))
1560 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1561 if (wiphy
->bands
[band
])
1562 handle_band(wiphy
, band
);
1565 reg_process_beacons(wiphy
);
1566 reg_process_ht_flags(wiphy
);
1567 if (wiphy
->reg_notifier
)
1568 wiphy
->reg_notifier(wiphy
, last_request
);
1571 static void handle_channel_custom(struct wiphy
*wiphy
,
1572 enum ieee80211_band band
,
1573 unsigned int chan_idx
,
1574 const struct ieee80211_regdomain
*regd
)
1577 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
1579 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1580 const struct ieee80211_power_rule
*power_rule
= NULL
;
1581 const struct ieee80211_freq_range
*freq_range
= NULL
;
1582 struct ieee80211_supported_band
*sband
;
1583 struct ieee80211_channel
*chan
;
1587 sband
= wiphy
->bands
[band
];
1588 BUG_ON(chan_idx
>= sband
->n_channels
);
1589 chan
= &sband
->channels
[chan_idx
];
1591 r
= freq_reg_info_regd(wiphy
,
1592 MHZ_TO_KHZ(chan
->center_freq
),
1598 chan
->flags
= IEEE80211_CHAN_DISABLED
;
1602 power_rule
= ®_rule
->power_rule
;
1603 freq_range
= ®_rule
->freq_range
;
1605 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1606 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1608 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1609 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1610 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1613 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
1614 const struct ieee80211_regdomain
*regd
)
1617 struct ieee80211_supported_band
*sband
;
1619 BUG_ON(!wiphy
->bands
[band
]);
1620 sband
= wiphy
->bands
[band
];
1622 for (i
= 0; i
< sband
->n_channels
; i
++)
1623 handle_channel_custom(wiphy
, band
, i
, regd
);
1626 /* Used by drivers prior to wiphy registration */
1627 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1628 const struct ieee80211_regdomain
*regd
)
1630 enum ieee80211_band band
;
1631 unsigned int bands_set
= 0;
1633 mutex_lock(®_mutex
);
1634 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1635 if (!wiphy
->bands
[band
])
1637 handle_band_custom(wiphy
, band
, regd
);
1640 mutex_unlock(®_mutex
);
1643 * no point in calling this if it won't have any effect
1644 * on your device's supportd bands.
1646 WARN_ON(!bands_set
);
1648 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1651 * Return value which can be used by ignore_request() to indicate
1652 * it has been determined we should intersect two regulatory domains
1654 #define REG_INTERSECT 1
1656 /* This has the logic which determines when a new request
1657 * should be ignored. */
1658 static int ignore_request(struct wiphy
*wiphy
,
1659 struct regulatory_request
*pending_request
)
1661 struct wiphy
*last_wiphy
= NULL
;
1663 assert_cfg80211_lock();
1665 /* All initial requests are respected */
1669 switch (pending_request
->initiator
) {
1670 case NL80211_REGDOM_SET_BY_CORE
:
1672 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1674 last_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1676 if (unlikely(!is_an_alpha2(pending_request
->alpha2
)))
1678 if (last_request
->initiator
==
1679 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1680 if (last_wiphy
!= wiphy
) {
1682 * Two cards with two APs claiming different
1683 * Country IE alpha2s. We could
1684 * intersect them, but that seems unlikely
1685 * to be correct. Reject second one for now.
1687 if (regdom_changes(pending_request
->alpha2
))
1692 * Two consecutive Country IE hints on the same wiphy.
1693 * This should be picked up early by the driver/stack
1695 if (WARN_ON(regdom_changes(pending_request
->alpha2
)))
1699 return REG_INTERSECT
;
1700 case NL80211_REGDOM_SET_BY_DRIVER
:
1701 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1702 if (regdom_changes(pending_request
->alpha2
))
1708 * This would happen if you unplug and plug your card
1709 * back in or if you add a new device for which the previously
1710 * loaded card also agrees on the regulatory domain.
1712 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1713 !regdom_changes(pending_request
->alpha2
))
1716 return REG_INTERSECT
;
1717 case NL80211_REGDOM_SET_BY_USER
:
1718 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1719 return REG_INTERSECT
;
1721 * If the user knows better the user should set the regdom
1722 * to their country before the IE is picked up
1724 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1725 last_request
->intersect
)
1728 * Process user requests only after previous user/driver/core
1729 * requests have been processed
1731 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1732 last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1733 last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1734 if (regdom_changes(last_request
->alpha2
))
1738 if (!regdom_changes(pending_request
->alpha2
))
1748 * __regulatory_hint - hint to the wireless core a regulatory domain
1749 * @wiphy: if the hint comes from country information from an AP, this
1750 * is required to be set to the wiphy that received the information
1751 * @pending_request: the regulatory request currently being processed
1753 * The Wireless subsystem can use this function to hint to the wireless core
1754 * what it believes should be the current regulatory domain.
1756 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1757 * already been set or other standard error codes.
1759 * Caller must hold &cfg80211_mutex and ®_mutex
1761 static int __regulatory_hint(struct wiphy
*wiphy
,
1762 struct regulatory_request
*pending_request
)
1764 bool intersect
= false;
1767 assert_cfg80211_lock();
1769 r
= ignore_request(wiphy
, pending_request
);
1771 if (r
== REG_INTERSECT
) {
1772 if (pending_request
->initiator
==
1773 NL80211_REGDOM_SET_BY_DRIVER
) {
1774 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1776 kfree(pending_request
);
1783 * If the regulatory domain being requested by the
1784 * driver has already been set just copy it to the
1787 if (r
== -EALREADY
&&
1788 pending_request
->initiator
==
1789 NL80211_REGDOM_SET_BY_DRIVER
) {
1790 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1792 kfree(pending_request
);
1798 kfree(pending_request
);
1803 kfree(last_request
);
1805 last_request
= pending_request
;
1806 last_request
->intersect
= intersect
;
1808 pending_request
= NULL
;
1810 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1811 user_alpha2
[0] = last_request
->alpha2
[0];
1812 user_alpha2
[1] = last_request
->alpha2
[1];
1815 /* When r == REG_INTERSECT we do need to call CRDA */
1818 * Since CRDA will not be called in this case as we already
1819 * have applied the requested regulatory domain before we just
1820 * inform userspace we have processed the request
1823 nl80211_send_reg_change_event(last_request
);
1827 return call_crda(last_request
->alpha2
);
1830 /* This processes *all* regulatory hints */
1831 static void reg_process_hint(struct regulatory_request
*reg_request
)
1834 struct wiphy
*wiphy
= NULL
;
1836 BUG_ON(!reg_request
->alpha2
);
1838 mutex_lock(&cfg80211_mutex
);
1839 mutex_lock(®_mutex
);
1841 if (wiphy_idx_valid(reg_request
->wiphy_idx
))
1842 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1844 if (reg_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1850 r
= __regulatory_hint(wiphy
, reg_request
);
1851 /* This is required so that the orig_* parameters are saved */
1852 if (r
== -EALREADY
&& wiphy
&&
1853 wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
)
1854 wiphy_update_regulatory(wiphy
, reg_request
->initiator
);
1856 mutex_unlock(®_mutex
);
1857 mutex_unlock(&cfg80211_mutex
);
1860 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1861 static void reg_process_pending_hints(void)
1863 struct regulatory_request
*reg_request
;
1865 spin_lock(®_requests_lock
);
1866 while (!list_empty(®_requests_list
)) {
1867 reg_request
= list_first_entry(®_requests_list
,
1868 struct regulatory_request
,
1870 list_del_init(®_request
->list
);
1872 spin_unlock(®_requests_lock
);
1873 reg_process_hint(reg_request
);
1874 spin_lock(®_requests_lock
);
1876 spin_unlock(®_requests_lock
);
1879 /* Processes beacon hints -- this has nothing to do with country IEs */
1880 static void reg_process_pending_beacon_hints(void)
1882 struct cfg80211_registered_device
*rdev
;
1883 struct reg_beacon
*pending_beacon
, *tmp
;
1886 * No need to hold the reg_mutex here as we just touch wiphys
1887 * and do not read or access regulatory variables.
1889 mutex_lock(&cfg80211_mutex
);
1891 /* This goes through the _pending_ beacon list */
1892 spin_lock_bh(®_pending_beacons_lock
);
1894 if (list_empty(®_pending_beacons
)) {
1895 spin_unlock_bh(®_pending_beacons_lock
);
1899 list_for_each_entry_safe(pending_beacon
, tmp
,
1900 ®_pending_beacons
, list
) {
1902 list_del_init(&pending_beacon
->list
);
1904 /* Applies the beacon hint to current wiphys */
1905 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1906 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1908 /* Remembers the beacon hint for new wiphys or reg changes */
1909 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1912 spin_unlock_bh(®_pending_beacons_lock
);
1914 mutex_unlock(&cfg80211_mutex
);
1917 static void reg_todo(struct work_struct
*work
)
1919 reg_process_pending_hints();
1920 reg_process_pending_beacon_hints();
1923 static DECLARE_WORK(reg_work
, reg_todo
);
1925 static void queue_regulatory_request(struct regulatory_request
*request
)
1927 spin_lock(®_requests_lock
);
1928 list_add_tail(&request
->list
, ®_requests_list
);
1929 spin_unlock(®_requests_lock
);
1931 schedule_work(®_work
);
1935 * Core regulatory hint -- happens during cfg80211_init()
1936 * and when we restore regulatory settings.
1938 static int regulatory_hint_core(const char *alpha2
)
1940 struct regulatory_request
*request
;
1942 kfree(last_request
);
1943 last_request
= NULL
;
1945 request
= kzalloc(sizeof(struct regulatory_request
),
1950 request
->alpha2
[0] = alpha2
[0];
1951 request
->alpha2
[1] = alpha2
[1];
1952 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1955 * This ensures last_request is populated once modules
1956 * come swinging in and calling regulatory hints and
1957 * wiphy_apply_custom_regulatory().
1959 reg_process_hint(request
);
1965 int regulatory_hint_user(const char *alpha2
)
1967 struct regulatory_request
*request
;
1971 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1975 request
->wiphy_idx
= WIPHY_IDX_STALE
;
1976 request
->alpha2
[0] = alpha2
[0];
1977 request
->alpha2
[1] = alpha2
[1];
1978 request
->initiator
= NL80211_REGDOM_SET_BY_USER
;
1980 queue_regulatory_request(request
);
1986 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1988 struct regulatory_request
*request
;
1993 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1997 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1999 /* Must have registered wiphy first */
2000 BUG_ON(!wiphy_idx_valid(request
->wiphy_idx
));
2002 request
->alpha2
[0] = alpha2
[0];
2003 request
->alpha2
[1] = alpha2
[1];
2004 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
2006 queue_regulatory_request(request
);
2010 EXPORT_SYMBOL(regulatory_hint
);
2012 /* Caller must hold reg_mutex */
2013 static bool reg_same_country_ie_hint(struct wiphy
*wiphy
,
2014 u32 country_ie_checksum
)
2016 struct wiphy
*request_wiphy
;
2020 if (unlikely(last_request
->initiator
!=
2021 NL80211_REGDOM_SET_BY_COUNTRY_IE
))
2024 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2029 if (likely(request_wiphy
!= wiphy
))
2030 return !country_ie_integrity_changes(country_ie_checksum
);
2032 * We should not have let these through at this point, they
2033 * should have been picked up earlier by the first alpha2 check
2036 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum
)))
2042 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
2043 * therefore cannot iterate over the rdev list here.
2045 void regulatory_hint_11d(struct wiphy
*wiphy
,
2046 enum ieee80211_band band
,
2050 struct ieee80211_regdomain
*rd
= NULL
;
2053 enum environment_cap env
= ENVIRON_ANY
;
2054 struct regulatory_request
*request
;
2056 mutex_lock(®_mutex
);
2058 if (unlikely(!last_request
))
2061 /* IE len must be evenly divisible by 2 */
2062 if (country_ie_len
& 0x01)
2065 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
2069 * Pending country IE processing, this can happen after we
2070 * call CRDA and wait for a response if a beacon was received before
2071 * we were able to process the last regulatory_hint_11d() call
2073 if (country_ie_regdomain
)
2076 alpha2
[0] = country_ie
[0];
2077 alpha2
[1] = country_ie
[1];
2079 if (country_ie
[2] == 'I')
2080 env
= ENVIRON_INDOOR
;
2081 else if (country_ie
[2] == 'O')
2082 env
= ENVIRON_OUTDOOR
;
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 (likely(last_request
->initiator
==
2090 NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
2091 wiphy_idx_valid(last_request
->wiphy_idx
)))
2094 rd
= country_ie_2_rd(band
, country_ie
, country_ie_len
, &checksum
);
2096 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
2101 * This will not happen right now but we leave it here for the
2102 * the future when we want to add suspend/resume support and having
2103 * the user move to another country after doing so, or having the user
2104 * move to another AP. Right now we just trust the first AP.
2106 * If we hit this before we add this support we want to be informed of
2107 * it as it would indicate a mistake in the current design
2109 if (WARN_ON(reg_same_country_ie_hint(wiphy
, checksum
)))
2112 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
2117 * We keep this around for when CRDA comes back with a response so
2118 * we can intersect with that
2120 country_ie_regdomain
= rd
;
2122 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
2123 request
->alpha2
[0] = rd
->alpha2
[0];
2124 request
->alpha2
[1] = rd
->alpha2
[1];
2125 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
2126 request
->country_ie_checksum
= checksum
;
2127 request
->country_ie_env
= env
;
2129 mutex_unlock(®_mutex
);
2131 queue_regulatory_request(request
);
2138 mutex_unlock(®_mutex
);
2141 static void restore_alpha2(char *alpha2
, bool reset_user
)
2143 /* indicates there is no alpha2 to consider for restoration */
2147 /* The user setting has precedence over the module parameter */
2148 if (is_user_regdom_saved()) {
2149 /* Unless we're asked to ignore it and reset it */
2151 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2152 "including user preference\n");
2153 user_alpha2
[0] = '9';
2154 user_alpha2
[1] = '7';
2157 * If we're ignoring user settings, we still need to
2158 * check the module parameter to ensure we put things
2159 * back as they were for a full restore.
2161 if (!is_world_regdom(ieee80211_regdom
)) {
2162 REG_DBG_PRINT("cfg80211: Keeping preference on "
2163 "module parameter ieee80211_regdom: %c%c\n",
2164 ieee80211_regdom
[0],
2165 ieee80211_regdom
[1]);
2166 alpha2
[0] = ieee80211_regdom
[0];
2167 alpha2
[1] = ieee80211_regdom
[1];
2170 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2171 "while preserving user preference for: %c%c\n",
2174 alpha2
[0] = user_alpha2
[0];
2175 alpha2
[1] = user_alpha2
[1];
2177 } else if (!is_world_regdom(ieee80211_regdom
)) {
2178 REG_DBG_PRINT("cfg80211: Keeping preference on "
2179 "module parameter ieee80211_regdom: %c%c\n",
2180 ieee80211_regdom
[0],
2181 ieee80211_regdom
[1]);
2182 alpha2
[0] = ieee80211_regdom
[0];
2183 alpha2
[1] = ieee80211_regdom
[1];
2185 REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
2189 * Restoring regulatory settings involves ingoring any
2190 * possibly stale country IE information and user regulatory
2191 * settings if so desired, this includes any beacon hints
2192 * learned as we could have traveled outside to another country
2193 * after disconnection. To restore regulatory settings we do
2194 * exactly what we did at bootup:
2196 * - send a core regulatory hint
2197 * - send a user regulatory hint if applicable
2199 * Device drivers that send a regulatory hint for a specific country
2200 * keep their own regulatory domain on wiphy->regd so that does does
2201 * not need to be remembered.
2203 static void restore_regulatory_settings(bool reset_user
)
2206 struct reg_beacon
*reg_beacon
, *btmp
;
2208 mutex_lock(&cfg80211_mutex
);
2209 mutex_lock(®_mutex
);
2212 restore_alpha2(alpha2
, reset_user
);
2214 /* Clear beacon hints */
2215 spin_lock_bh(®_pending_beacons_lock
);
2216 if (!list_empty(®_pending_beacons
)) {
2217 list_for_each_entry_safe(reg_beacon
, btmp
,
2218 ®_pending_beacons
, list
) {
2219 list_del(®_beacon
->list
);
2223 spin_unlock_bh(®_pending_beacons_lock
);
2225 if (!list_empty(®_beacon_list
)) {
2226 list_for_each_entry_safe(reg_beacon
, btmp
,
2227 ®_beacon_list
, list
) {
2228 list_del(®_beacon
->list
);
2233 /* First restore to the basic regulatory settings */
2234 cfg80211_regdomain
= cfg80211_world_regdom
;
2236 mutex_unlock(®_mutex
);
2237 mutex_unlock(&cfg80211_mutex
);
2239 regulatory_hint_core(cfg80211_regdomain
->alpha2
);
2242 * This restores the ieee80211_regdom module parameter
2243 * preference or the last user requested regulatory
2244 * settings, user regulatory settings takes precedence.
2246 if (is_an_alpha2(alpha2
))
2247 regulatory_hint_user(user_alpha2
);
2251 void regulatory_hint_disconnect(void)
2253 REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
2254 "restore regulatory settings\n");
2255 restore_regulatory_settings(false);
2258 static bool freq_is_chan_12_13_14(u16 freq
)
2260 if (freq
== ieee80211_channel_to_frequency(12) ||
2261 freq
== ieee80211_channel_to_frequency(13) ||
2262 freq
== ieee80211_channel_to_frequency(14))
2267 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
2268 struct ieee80211_channel
*beacon_chan
,
2271 struct reg_beacon
*reg_beacon
;
2273 if (likely((beacon_chan
->beacon_found
||
2274 (beacon_chan
->flags
& IEEE80211_CHAN_RADAR
) ||
2275 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
2276 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))))
2279 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
2283 REG_DBG_PRINT("cfg80211: Found new beacon on "
2284 "frequency: %d MHz (Ch %d) on %s\n",
2285 beacon_chan
->center_freq
,
2286 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
2289 memcpy(®_beacon
->chan
, beacon_chan
,
2290 sizeof(struct ieee80211_channel
));
2294 * Since we can be called from BH or and non-BH context
2295 * we must use spin_lock_bh()
2297 spin_lock_bh(®_pending_beacons_lock
);
2298 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
2299 spin_unlock_bh(®_pending_beacons_lock
);
2301 schedule_work(®_work
);
2306 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
2309 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
2310 const struct ieee80211_freq_range
*freq_range
= NULL
;
2311 const struct ieee80211_power_rule
*power_rule
= NULL
;
2313 printk(KERN_INFO
" (start_freq - end_freq @ bandwidth), "
2314 "(max_antenna_gain, max_eirp)\n");
2316 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
2317 reg_rule
= &rd
->reg_rules
[i
];
2318 freq_range
= ®_rule
->freq_range
;
2319 power_rule
= ®_rule
->power_rule
;
2322 * There may not be documentation for max antenna gain
2323 * in certain regions
2325 if (power_rule
->max_antenna_gain
)
2326 printk(KERN_INFO
" (%d KHz - %d KHz @ %d KHz), "
2327 "(%d mBi, %d mBm)\n",
2328 freq_range
->start_freq_khz
,
2329 freq_range
->end_freq_khz
,
2330 freq_range
->max_bandwidth_khz
,
2331 power_rule
->max_antenna_gain
,
2332 power_rule
->max_eirp
);
2334 printk(KERN_INFO
" (%d KHz - %d KHz @ %d KHz), "
2336 freq_range
->start_freq_khz
,
2337 freq_range
->end_freq_khz
,
2338 freq_range
->max_bandwidth_khz
,
2339 power_rule
->max_eirp
);
2343 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
2346 if (is_intersected_alpha2(rd
->alpha2
)) {
2348 if (last_request
->initiator
==
2349 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2350 struct cfg80211_registered_device
*rdev
;
2351 rdev
= cfg80211_rdev_by_wiphy_idx(
2352 last_request
->wiphy_idx
);
2354 printk(KERN_INFO
"cfg80211: Current regulatory "
2355 "domain updated by AP to: %c%c\n",
2356 rdev
->country_ie_alpha2
[0],
2357 rdev
->country_ie_alpha2
[1]);
2359 printk(KERN_INFO
"cfg80211: Current regulatory "
2360 "domain intersected: \n");
2362 printk(KERN_INFO
"cfg80211: Current regulatory "
2363 "domain intersected: \n");
2364 } else if (is_world_regdom(rd
->alpha2
))
2365 printk(KERN_INFO
"cfg80211: World regulatory "
2366 "domain updated:\n");
2368 if (is_unknown_alpha2(rd
->alpha2
))
2369 printk(KERN_INFO
"cfg80211: Regulatory domain "
2370 "changed to driver built-in settings "
2371 "(unknown country)\n");
2373 printk(KERN_INFO
"cfg80211: Regulatory domain "
2374 "changed to country: %c%c\n",
2375 rd
->alpha2
[0], rd
->alpha2
[1]);
2380 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
2382 printk(KERN_INFO
"cfg80211: Regulatory domain: %c%c\n",
2383 rd
->alpha2
[0], rd
->alpha2
[1]);
2387 #ifdef CONFIG_CFG80211_REG_DEBUG
2388 static void reg_country_ie_process_debug(
2389 const struct ieee80211_regdomain
*rd
,
2390 const struct ieee80211_regdomain
*country_ie_regdomain
,
2391 const struct ieee80211_regdomain
*intersected_rd
)
2393 printk(KERN_DEBUG
"cfg80211: Received country IE:\n");
2394 print_regdomain_info(country_ie_regdomain
);
2395 printk(KERN_DEBUG
"cfg80211: CRDA thinks this should applied:\n");
2396 print_regdomain_info(rd
);
2397 if (intersected_rd
) {
2398 printk(KERN_DEBUG
"cfg80211: We intersect both of these "
2400 print_regdomain_info(intersected_rd
);
2403 printk(KERN_DEBUG
"cfg80211: Intersection between both failed\n");
2406 static inline void reg_country_ie_process_debug(
2407 const struct ieee80211_regdomain
*rd
,
2408 const struct ieee80211_regdomain
*country_ie_regdomain
,
2409 const struct ieee80211_regdomain
*intersected_rd
)
2414 /* Takes ownership of rd only if it doesn't fail */
2415 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
2417 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2418 struct cfg80211_registered_device
*rdev
= NULL
;
2419 struct wiphy
*request_wiphy
;
2420 /* Some basic sanity checks first */
2422 if (is_world_regdom(rd
->alpha2
)) {
2423 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2425 update_world_regdomain(rd
);
2429 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2430 !is_unknown_alpha2(rd
->alpha2
))
2437 * Lets only bother proceeding on the same alpha2 if the current
2438 * rd is non static (it means CRDA was present and was used last)
2439 * and the pending request came in from a country IE
2441 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2443 * If someone else asked us to change the rd lets only bother
2444 * checking if the alpha2 changes if CRDA was already called
2446 if (!regdom_changes(rd
->alpha2
))
2451 * Now lets set the regulatory domain, update all driver channels
2452 * and finally inform them of what we have done, in case they want
2453 * to review or adjust their own settings based on their own
2454 * internal EEPROM data
2457 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2460 if (!is_valid_rd(rd
)) {
2461 printk(KERN_ERR
"cfg80211: Invalid "
2462 "regulatory domain detected:\n");
2463 print_regdomain_info(rd
);
2467 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2469 if (!last_request
->intersect
) {
2472 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_DRIVER
) {
2474 cfg80211_regdomain
= rd
;
2479 * For a driver hint, lets copy the regulatory domain the
2480 * driver wanted to the wiphy to deal with conflicts
2484 * Userspace could have sent two replies with only
2485 * one kernel request.
2487 if (request_wiphy
->regd
)
2490 r
= reg_copy_regd(&request_wiphy
->regd
, rd
);
2495 cfg80211_regdomain
= rd
;
2499 /* Intersection requires a bit more work */
2501 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2503 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
2504 if (!intersected_rd
)
2508 * We can trash what CRDA provided now.
2509 * However if a driver requested this specific regulatory
2510 * domain we keep it for its private use
2512 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
)
2513 request_wiphy
->regd
= rd
;
2520 cfg80211_regdomain
= intersected_rd
;
2526 * Country IE requests are handled a bit differently, we intersect
2527 * the country IE rd with what CRDA believes that country should have
2531 * Userspace could have sent two replies with only
2532 * one kernel request. By the second reply we would have
2533 * already processed and consumed the country_ie_regdomain.
2535 if (!country_ie_regdomain
)
2537 BUG_ON(rd
== country_ie_regdomain
);
2540 * Intersect what CRDA returned and our what we
2541 * had built from the Country IE received
2544 intersected_rd
= regdom_intersect(rd
, country_ie_regdomain
);
2546 reg_country_ie_process_debug(rd
,
2547 country_ie_regdomain
,
2550 kfree(country_ie_regdomain
);
2551 country_ie_regdomain
= NULL
;
2553 if (!intersected_rd
)
2556 rdev
= wiphy_to_dev(request_wiphy
);
2558 rdev
->country_ie_alpha2
[0] = rd
->alpha2
[0];
2559 rdev
->country_ie_alpha2
[1] = rd
->alpha2
[1];
2560 rdev
->env
= last_request
->country_ie_env
;
2562 BUG_ON(intersected_rd
== rd
);
2568 cfg80211_regdomain
= intersected_rd
;
2575 * Use this call to set the current regulatory domain. Conflicts with
2576 * multiple drivers can be ironed out later. Caller must've already
2577 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2579 int set_regdom(const struct ieee80211_regdomain
*rd
)
2583 assert_cfg80211_lock();
2585 mutex_lock(®_mutex
);
2587 /* Note that this doesn't update the wiphys, this is done below */
2588 r
= __set_regdom(rd
);
2591 mutex_unlock(®_mutex
);
2595 /* This would make this whole thing pointless */
2596 if (!last_request
->intersect
)
2597 BUG_ON(rd
!= cfg80211_regdomain
);
2599 /* update all wiphys now with the new established regulatory domain */
2600 update_all_wiphy_regulatory(last_request
->initiator
);
2602 print_regdomain(cfg80211_regdomain
);
2604 nl80211_send_reg_change_event(last_request
);
2606 mutex_unlock(®_mutex
);
2611 /* Caller must hold cfg80211_mutex */
2612 void reg_device_remove(struct wiphy
*wiphy
)
2614 struct wiphy
*request_wiphy
= NULL
;
2616 assert_cfg80211_lock();
2618 mutex_lock(®_mutex
);
2623 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2625 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2628 last_request
->wiphy_idx
= WIPHY_IDX_STALE
;
2629 last_request
->country_ie_env
= ENVIRON_ANY
;
2631 mutex_unlock(®_mutex
);
2634 int regulatory_init(void)
2638 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2639 if (IS_ERR(reg_pdev
))
2640 return PTR_ERR(reg_pdev
);
2642 spin_lock_init(®_requests_lock
);
2643 spin_lock_init(®_pending_beacons_lock
);
2645 cfg80211_regdomain
= cfg80211_world_regdom
;
2647 user_alpha2
[0] = '9';
2648 user_alpha2
[1] = '7';
2650 /* We always try to get an update for the static regdomain */
2651 err
= regulatory_hint_core(cfg80211_regdomain
->alpha2
);
2656 * N.B. kobject_uevent_env() can fail mainly for when we're out
2657 * memory which is handled and propagated appropriately above
2658 * but it can also fail during a netlink_broadcast() or during
2659 * early boot for call_usermodehelper(). For now treat these
2660 * errors as non-fatal.
2662 printk(KERN_ERR
"cfg80211: kobject_uevent_env() was unable "
2663 "to call CRDA during init");
2664 #ifdef CONFIG_CFG80211_REG_DEBUG
2665 /* We want to find out exactly why when debugging */
2671 * Finally, if the user set the module parameter treat it
2674 if (!is_world_regdom(ieee80211_regdom
))
2675 regulatory_hint_user(ieee80211_regdom
);
2680 void regulatory_exit(void)
2682 struct regulatory_request
*reg_request
, *tmp
;
2683 struct reg_beacon
*reg_beacon
, *btmp
;
2685 cancel_work_sync(®_work
);
2687 mutex_lock(&cfg80211_mutex
);
2688 mutex_lock(®_mutex
);
2692 kfree(country_ie_regdomain
);
2693 country_ie_regdomain
= NULL
;
2695 kfree(last_request
);
2697 platform_device_unregister(reg_pdev
);
2699 spin_lock_bh(®_pending_beacons_lock
);
2700 if (!list_empty(®_pending_beacons
)) {
2701 list_for_each_entry_safe(reg_beacon
, btmp
,
2702 ®_pending_beacons
, list
) {
2703 list_del(®_beacon
->list
);
2707 spin_unlock_bh(®_pending_beacons_lock
);
2709 if (!list_empty(®_beacon_list
)) {
2710 list_for_each_entry_safe(reg_beacon
, btmp
,
2711 ®_beacon_list
, list
) {
2712 list_del(®_beacon
->list
);
2717 spin_lock(®_requests_lock
);
2718 if (!list_empty(®_requests_list
)) {
2719 list_for_each_entry_safe(reg_request
, tmp
,
2720 ®_requests_list
, list
) {
2721 list_del(®_request
->list
);
2725 spin_unlock(®_requests_lock
);
2727 mutex_unlock(®_mutex
);
2728 mutex_unlock(&cfg80211_mutex
);