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";
138 module_param(ieee80211_regdom, charp, 0444);
139 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
141 static void reset_regdomains(void)
143 /* avoid freeing static information or freeing something twice */
144 if (cfg80211_regdomain == cfg80211_world_regdom)
145 cfg80211_regdomain = NULL;
146 if (cfg80211_world_regdom == &world_regdom)
147 cfg80211_world_regdom = NULL;
148 if (cfg80211_regdomain == &world_regdom)
149 cfg80211_regdomain = NULL;
151 kfree(cfg80211_regdomain);
152 kfree(cfg80211_world_regdom);
154 cfg80211_world_regdom = &world_regdom;
155 cfg80211_regdomain = NULL;
159 * Dynamic world regulatory domain requested by the wireless
160 * core upon initialization
162 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
164 BUG_ON(!last_request);
168 cfg80211_world_regdom = rd;
169 cfg80211_regdomain = rd;
172 bool is_world_regdom(const char *alpha2)
176 if (alpha2[0] == '0' && alpha2[1] == '0')
181 static bool is_alpha2_set(const char *alpha2)
185 if (alpha2[0] != 0 && alpha2[1] != 0)
190 static bool is_alpha_upper(char letter)
193 if (letter >= 65 && letter <= 90)
198 static bool is_unknown_alpha2(const char *alpha2)
203 * Special case where regulatory domain was built by driver
204 * but a specific alpha2 cannot be determined
206 if (alpha2[0] == '9' && alpha2[1] == '9')
211 static bool is_intersected_alpha2(const char *alpha2)
216 * Special case where regulatory domain is the
217 * result of an intersection between two regulatory domain
220 if (alpha2[0] == '9' && alpha2[1] == '8')
225 static bool is_an_alpha2(const char *alpha2)
229 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
234 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
236 if (!alpha2_x || !alpha2_y)
238 if (alpha2_x[0] == alpha2_y[0] &&
239 alpha2_x[1] == alpha2_y[1])
244 static bool regdom_changes(const char *alpha2)
246 assert_cfg80211_lock();
248 if (!cfg80211_regdomain)
250 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
256 * country_ie_integrity_changes - tells us if the country IE has changed
257 * @checksum: checksum of country IE of fields we are interested in
259 * If the country IE has not changed you can ignore it safely. This is
260 * useful to determine if two devices are seeing two different country IEs
261 * even on the same alpha2. Note that this will return false if no IE has
262 * been set on the wireless core yet.
264 static bool country_ie_integrity_changes(u32 checksum)
266 /* If no IE has been set then the checksum doesn't change */
267 if (unlikely(!last_request->country_ie_checksum))
269 if (unlikely(last_request->country_ie_checksum != checksum))
274 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
275 const struct ieee80211_regdomain *src_regd)
277 struct ieee80211_regdomain *regd;
278 int size_of_regd = 0;
281 size_of_regd = sizeof(struct ieee80211_regdomain) +
282 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
284 regd = kzalloc(size_of_regd, GFP_KERNEL);
288 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
290 for (i = 0; i < src_regd->n_reg_rules; i++)
291 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i],
292 sizeof(struct ieee80211_reg_rule));
298 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
299 struct reg_regdb_search_request {
301 struct list_head list;
304 static LIST_HEAD(reg_regdb_search_list);
305 static DEFINE_SPINLOCK(reg_regdb_search_lock);
307 static void reg_regdb_search(struct work_struct *work)
309 struct reg_regdb_search_request *request;
310 const struct ieee80211_regdomain *curdom, *regdom;
313 spin_lock(®_regdb_search_lock);
314 while (!list_empty(®_regdb_search_list)) {
315 request = list_first_entry(®_regdb_search_list,
316 struct reg_regdb_search_request,
318 list_del(&request->list);
320 for (i=0; i<reg_regdb_size; i++) {
321 curdom = reg_regdb[i];
323 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
324 r = reg_copy_regd(®dom, curdom);
327 spin_unlock(®_regdb_search_lock);
328 mutex_lock(&cfg80211_mutex);
330 mutex_unlock(&cfg80211_mutex);
331 spin_lock(®_regdb_search_lock);
338 spin_unlock(®_regdb_search_lock);
341 static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
343 static void reg_regdb_query(const char *alpha2)
345 struct reg_regdb_search_request *request;
350 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
354 memcpy(request->alpha2, alpha2, 2);
356 spin_lock(®_regdb_search_lock);
357 list_add_tail(&request->list, ®_regdb_search_list);
358 spin_unlock(®_regdb_search_lock);
360 schedule_work(®_regdb_work);
363 static inline void reg_regdb_query(const char *alpha2) {}
364 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
367 * This lets us keep regulatory code which is updated on a regulatory
368 * basis in userspace.
370 static int call_crda(const char *alpha2)
372 char country_env[9 + 2] = "COUNTRY=";
378 if (!is_world_regdom((char *) alpha2))
379 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
380 alpha2[0], alpha2[1]);
382 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
383 "regulatory domain\n");
385 /* query internal regulatory database (if it exists) */
386 reg_regdb_query(alpha2);
388 country_env[8] = alpha2[0];
389 country_env[9] = alpha2[1];
391 return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp);
394 /* Used by nl80211 before kmalloc'ing our regulatory domain */
395 bool reg_is_valid_request(const char *alpha2)
397 assert_cfg80211_lock();
402 return alpha2_equal(last_request->alpha2, alpha2);
405 /* Sanity check on a regulatory rule */
406 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
408 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
411 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
414 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
417 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
419 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
420 freq_range->max_bandwidth_khz > freq_diff)
426 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
428 const struct ieee80211_reg_rule *reg_rule = NULL;
431 if (!rd->n_reg_rules)
434 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
437 for (i = 0; i < rd->n_reg_rules; i++) {
438 reg_rule = &rd->reg_rules[i];
439 if (!is_valid_reg_rule(reg_rule))
446 static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
450 u32 start_freq_khz, end_freq_khz;
452 start_freq_khz = center_freq_khz - (bw_khz/2);
453 end_freq_khz = center_freq_khz + (bw_khz/2);
455 if (start_freq_khz >= freq_range->start_freq_khz &&
456 end_freq_khz <= freq_range->end_freq_khz)
463 * freq_in_rule_band - tells us if a frequency is in a frequency band
464 * @freq_range: frequency rule we want to query
465 * @freq_khz: frequency we are inquiring about
467 * This lets us know if a specific frequency rule is or is not relevant to
468 * a specific frequency's band. Bands are device specific and artificial
469 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
470 * safe for now to assume that a frequency rule should not be part of a
471 * frequency's band if the start freq or end freq are off by more than 2 GHz.
472 * This resolution can be lowered and should be considered as we add
473 * regulatory rule support for other "bands".
475 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
478 #define ONE_GHZ_IN_KHZ 1000000
479 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
481 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
484 #undef ONE_GHZ_IN_KHZ
488 * Some APs may send a country IE triplet for each channel they
489 * support and while this is completely overkill and silly we still
490 * need to support it. We avoid making a single rule for each channel
491 * though and to help us with this we use this helper to find the
492 * actual subband end channel. These type of country IE triplet
493 * scenerios are handled then, all yielding two regulaotry rules from
494 * parsing a country IE:
532 * Returns 0 if the IE has been found to be invalid in the middle
535 static int max_subband_chan(int orig_cur_chan,
536 int orig_end_channel,
541 u8 *triplets_start = *country_ie;
542 u8 len_at_triplet = *country_ie_len;
543 int end_subband_chan = orig_end_channel;
544 enum ieee80211_band band;
547 * We'll deal with padding for the caller unless
548 * its not immediate and we don't process any channels
550 if (*country_ie_len == 1) {
552 *country_ie_len -= 1;
553 return orig_end_channel;
556 /* Move to the next triplet and then start search */
558 *country_ie_len -= 3;
560 if (orig_cur_chan <= 14)
561 band = IEEE80211_BAND_2GHZ;
563 band = IEEE80211_BAND_5GHZ;
565 while (*country_ie_len >= 3) {
567 struct ieee80211_country_ie_triplet *triplet =
568 (struct ieee80211_country_ie_triplet *) *country_ie;
569 int cur_channel = 0, next_expected_chan;
570 enum ieee80211_band next_band = IEEE80211_BAND_2GHZ;
572 /* means last triplet is completely unrelated to this one */
573 if (triplet->ext.reg_extension_id >=
574 IEEE80211_COUNTRY_EXTENSION_ID) {
576 *country_ie_len += 3;
580 if (triplet->chans.first_channel == 0) {
582 *country_ie_len -= 1;
583 if (*country_ie_len != 0)
588 /* Monitonically increasing channel order */
589 if (triplet->chans.first_channel <= end_subband_chan)
593 if (triplet->chans.first_channel <= 14) {
594 end_channel = triplet->chans.first_channel +
595 triplet->chans.num_channels - 1;
598 end_channel = triplet->chans.first_channel +
599 (4 * (triplet->chans.num_channels - 1));
600 next_band = IEEE80211_BAND_5GHZ;
603 if (band != next_band) {
605 *country_ie_len += 3;
609 if (orig_max_power != triplet->chans.max_power) {
611 *country_ie_len += 3;
615 cur_channel = triplet->chans.first_channel;
617 /* The key is finding the right next expected channel */
618 if (band == IEEE80211_BAND_2GHZ)
619 next_expected_chan = end_subband_chan + 1;
621 next_expected_chan = end_subband_chan + 4;
623 if (cur_channel != next_expected_chan) {
625 *country_ie_len += 3;
629 end_subband_chan = end_channel;
631 /* Move to the next one */
633 *country_ie_len -= 3;
636 * Padding needs to be dealt with if we processed
639 if (*country_ie_len == 1) {
641 *country_ie_len -= 1;
645 /* If seen, the IE is invalid */
646 if (*country_ie_len == 2)
650 if (end_subband_chan == orig_end_channel) {
651 *country_ie = triplets_start;
652 *country_ie_len = len_at_triplet;
653 return orig_end_channel;
656 return end_subband_chan;
660 * Converts a country IE to a regulatory domain. A regulatory domain
661 * structure has a lot of information which the IE doesn't yet have,
662 * so for the other values we use upper max values as we will intersect
663 * with our userspace regulatory agent to get lower bounds.
665 static struct ieee80211_regdomain *country_ie_2_rd(
670 struct ieee80211_regdomain *rd = NULL;
674 u32 num_rules = 0, size_of_regd = 0;
675 u8 *triplets_start = NULL;
676 u8 len_at_triplet = 0;
677 /* the last channel we have registered in a subband (triplet) */
678 int last_sub_max_channel = 0;
680 *checksum = 0xDEADBEEF;
682 /* Country IE requirements */
683 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
684 country_ie_len & 0x01);
686 alpha2[0] = country_ie[0];
687 alpha2[1] = country_ie[1];
690 * Third octet can be:
694 * anything else we assume is no restrictions
696 if (country_ie[2] == 'I')
697 flags = NL80211_RRF_NO_OUTDOOR;
698 else if (country_ie[2] == 'O')
699 flags = NL80211_RRF_NO_INDOOR;
704 triplets_start = country_ie;
705 len_at_triplet = country_ie_len;
707 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
710 * We need to build a reg rule for each triplet, but first we must
711 * calculate the number of reg rules we will need. We will need one
712 * for each channel subband
714 while (country_ie_len >= 3) {
716 struct ieee80211_country_ie_triplet *triplet =
717 (struct ieee80211_country_ie_triplet *) country_ie;
718 int cur_sub_max_channel = 0, cur_channel = 0;
720 if (triplet->ext.reg_extension_id >=
721 IEEE80211_COUNTRY_EXTENSION_ID) {
728 * APs can add padding to make length divisible
729 * by two, required by the spec.
731 if (triplet->chans.first_channel == 0) {
734 /* This is expected to be at the very end only */
735 if (country_ie_len != 0)
741 if (triplet->chans.first_channel <= 14)
742 end_channel = triplet->chans.first_channel +
743 triplet->chans.num_channels;
746 * 5 GHz -- For example in country IEs if the first
747 * channel given is 36 and the number of channels is 4
748 * then the individual channel numbers defined for the
749 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
750 * and not 36, 37, 38, 39.
752 * See: http://tinyurl.com/11d-clarification
754 end_channel = triplet->chans.first_channel +
755 (4 * (triplet->chans.num_channels - 1));
757 cur_channel = triplet->chans.first_channel;
760 * Enhancement for APs that send a triplet for every channel
761 * or for whatever reason sends triplets with multiple channels
762 * separated when in fact they should be together.
764 end_channel = max_subband_chan(cur_channel,
766 triplet->chans.max_power,
772 cur_sub_max_channel = end_channel;
774 /* Basic sanity check */
775 if (cur_sub_max_channel < cur_channel)
779 * Do not allow overlapping channels. Also channels
780 * passed in each subband must be monotonically
783 if (last_sub_max_channel) {
784 if (cur_channel <= last_sub_max_channel)
786 if (cur_sub_max_channel <= last_sub_max_channel)
791 * When dot11RegulatoryClassesRequired is supported
792 * we can throw ext triplets as part of this soup,
793 * for now we don't care when those change as we
796 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
797 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
798 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
800 last_sub_max_channel = cur_sub_max_channel;
804 if (country_ie_len >= 3) {
810 * Note: this is not a IEEE requirement but
811 * simply a memory requirement
813 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
817 country_ie = triplets_start;
818 country_ie_len = len_at_triplet;
820 size_of_regd = sizeof(struct ieee80211_regdomain) +
821 (num_rules * sizeof(struct ieee80211_reg_rule));
823 rd = kzalloc(size_of_regd, GFP_KERNEL);
827 rd->n_reg_rules = num_rules;
828 rd->alpha2[0] = alpha2[0];
829 rd->alpha2[1] = alpha2[1];
831 /* This time around we fill in the rd */
832 while (country_ie_len >= 3) {
834 struct ieee80211_country_ie_triplet *triplet =
835 (struct ieee80211_country_ie_triplet *) country_ie;
836 struct ieee80211_reg_rule *reg_rule = NULL;
837 struct ieee80211_freq_range *freq_range = NULL;
838 struct ieee80211_power_rule *power_rule = NULL;
841 * Must parse if dot11RegulatoryClassesRequired is true,
842 * we don't support this yet
844 if (triplet->ext.reg_extension_id >=
845 IEEE80211_COUNTRY_EXTENSION_ID) {
851 if (triplet->chans.first_channel == 0) {
857 reg_rule = &rd->reg_rules[i];
858 freq_range = ®_rule->freq_range;
859 power_rule = ®_rule->power_rule;
861 reg_rule->flags = flags;
864 if (triplet->chans.first_channel <= 14)
865 end_channel = triplet->chans.first_channel +
866 triplet->chans.num_channels;
868 end_channel = triplet->chans.first_channel +
869 (4 * (triplet->chans.num_channels - 1));
871 end_channel = max_subband_chan(triplet->chans.first_channel,
873 triplet->chans.max_power,
878 * The +10 is since the regulatory domain expects
879 * the actual band edge, not the center of freq for
880 * its start and end freqs, assuming 20 MHz bandwidth on
881 * the channels passed
883 freq_range->start_freq_khz =
884 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
885 triplet->chans.first_channel) - 10);
886 freq_range->end_freq_khz =
887 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
891 * These are large arbitrary values we use to intersect later.
892 * Increment this if we ever support >= 40 MHz channels
895 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
896 power_rule->max_antenna_gain = DBI_TO_MBI(100);
897 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
901 if (country_ie_len >= 3) {
906 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
914 * Helper for regdom_intersect(), this does the real
915 * mathematical intersection fun
917 static int reg_rules_intersect(
918 const struct ieee80211_reg_rule *rule1,
919 const struct ieee80211_reg_rule *rule2,
920 struct ieee80211_reg_rule *intersected_rule)
922 const struct ieee80211_freq_range *freq_range1, *freq_range2;
923 struct ieee80211_freq_range *freq_range;
924 const struct ieee80211_power_rule *power_rule1, *power_rule2;
925 struct ieee80211_power_rule *power_rule;
928 freq_range1 = &rule1->freq_range;
929 freq_range2 = &rule2->freq_range;
930 freq_range = &intersected_rule->freq_range;
932 power_rule1 = &rule1->power_rule;
933 power_rule2 = &rule2->power_rule;
934 power_rule = &intersected_rule->power_rule;
936 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
937 freq_range2->start_freq_khz);
938 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
939 freq_range2->end_freq_khz);
940 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
941 freq_range2->max_bandwidth_khz);
943 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
944 if (freq_range->max_bandwidth_khz > freq_diff)
945 freq_range->max_bandwidth_khz = freq_diff;
947 power_rule->max_eirp = min(power_rule1->max_eirp,
948 power_rule2->max_eirp);
949 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
950 power_rule2->max_antenna_gain);
952 intersected_rule->flags = (rule1->flags | rule2->flags);
954 if (!is_valid_reg_rule(intersected_rule))
961 * regdom_intersect - do the intersection between two regulatory domains
962 * @rd1: first regulatory domain
963 * @rd2: second regulatory domain
965 * Use this function to get the intersection between two regulatory domains.
966 * Once completed we will mark the alpha2 for the rd as intersected, "98",
967 * as no one single alpha2 can represent this regulatory domain.
969 * Returns a pointer to the regulatory domain structure which will hold the
970 * resulting intersection of rules between rd1 and rd2. We will
971 * kzalloc() this structure for you.
973 static struct ieee80211_regdomain *regdom_intersect(
974 const struct ieee80211_regdomain *rd1,
975 const struct ieee80211_regdomain *rd2)
979 unsigned int num_rules = 0, rule_idx = 0;
980 const struct ieee80211_reg_rule *rule1, *rule2;
981 struct ieee80211_reg_rule *intersected_rule;
982 struct ieee80211_regdomain *rd;
983 /* This is just a dummy holder to help us count */
984 struct ieee80211_reg_rule irule;
986 /* Uses the stack temporarily for counter arithmetic */
987 intersected_rule = &irule;
989 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
995 * First we get a count of the rules we'll need, then we actually
996 * build them. This is to so we can malloc() and free() a
997 * regdomain once. The reason we use reg_rules_intersect() here
998 * is it will return -EINVAL if the rule computed makes no sense.
999 * All rules that do check out OK are valid.
1002 for (x = 0; x < rd1->n_reg_rules; x++) {
1003 rule1 = &rd1->reg_rules[x];
1004 for (y = 0; y < rd2->n_reg_rules; y++) {
1005 rule2 = &rd2->reg_rules[y];
1006 if (!reg_rules_intersect(rule1, rule2,
1009 memset(intersected_rule, 0,
1010 sizeof(struct ieee80211_reg_rule));
1017 size_of_regd = sizeof(struct ieee80211_regdomain) +
1018 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
1020 rd = kzalloc(size_of_regd, GFP_KERNEL);
1024 for (x = 0; x < rd1->n_reg_rules; x++) {
1025 rule1 = &rd1->reg_rules[x];
1026 for (y = 0; y < rd2->n_reg_rules; y++) {
1027 rule2 = &rd2->reg_rules[y];
1029 * This time around instead of using the stack lets
1030 * write to the target rule directly saving ourselves
1033 intersected_rule = &rd->reg_rules[rule_idx];
1034 r = reg_rules_intersect(rule1, rule2,
1037 * No need to memset here the intersected rule here as
1038 * we're not using the stack anymore
1046 if (rule_idx != num_rules) {
1051 rd->n_reg_rules = num_rules;
1052 rd->alpha2[0] = '9';
1053 rd->alpha2[1] = '8';
1059 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1060 * want to just have the channel structure use these
1062 static u32 map_regdom_flags(u32 rd_flags)
1064 u32 channel_flags = 0;
1065 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
1066 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1067 if (rd_flags & NL80211_RRF_NO_IBSS)
1068 channel_flags |= IEEE80211_CHAN_NO_IBSS;
1069 if (rd_flags & NL80211_RRF_DFS)
1070 channel_flags |= IEEE80211_CHAN_RADAR;
1071 return channel_flags;
1074 static int freq_reg_info_regd(struct wiphy *wiphy,
1077 const struct ieee80211_reg_rule **reg_rule,
1078 const struct ieee80211_regdomain *custom_regd)
1081 bool band_rule_found = false;
1082 const struct ieee80211_regdomain *regd;
1083 bool bw_fits = false;
1085 if (!desired_bw_khz)
1086 desired_bw_khz = MHZ_TO_KHZ(20);
1088 regd = custom_regd ? custom_regd : cfg80211_regdomain;
1091 * Follow the driver's regulatory domain, if present, unless a country
1092 * IE has been processed or a user wants to help complaince further
1094 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1095 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
1102 for (i = 0; i < regd->n_reg_rules; i++) {
1103 const struct ieee80211_reg_rule *rr;
1104 const struct ieee80211_freq_range *fr = NULL;
1105 const struct ieee80211_power_rule *pr = NULL;
1107 rr = ®d->reg_rules[i];
1108 fr = &rr->freq_range;
1109 pr = &rr->power_rule;
1112 * We only need to know if one frequency rule was
1113 * was in center_freq's band, that's enough, so lets
1114 * not overwrite it once found
1116 if (!band_rule_found)
1117 band_rule_found = freq_in_rule_band(fr, center_freq);
1119 bw_fits = reg_does_bw_fit(fr,
1123 if (band_rule_found && bw_fits) {
1129 if (!band_rule_found)
1134 EXPORT_SYMBOL(freq_reg_info);
1136 int freq_reg_info(struct wiphy *wiphy,
1139 const struct ieee80211_reg_rule **reg_rule)
1141 assert_cfg80211_lock();
1142 return freq_reg_info_regd(wiphy,
1150 * Note that right now we assume the desired channel bandwidth
1151 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1152 * per channel, the primary and the extension channel). To support
1153 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1154 * new ieee80211_channel.target_bw and re run the regulatory check
1155 * on the wiphy with the target_bw specified. Then we can simply use
1156 * that below for the desired_bw_khz below.
1158 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1159 unsigned int chan_idx)
1162 u32 flags, bw_flags = 0;
1163 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1164 const struct ieee80211_reg_rule *reg_rule = NULL;
1165 const struct ieee80211_power_rule *power_rule = NULL;
1166 const struct ieee80211_freq_range *freq_range = NULL;
1167 struct ieee80211_supported_band *sband;
1168 struct ieee80211_channel *chan;
1169 struct wiphy *request_wiphy = NULL;
1171 assert_cfg80211_lock();
1173 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1175 sband = wiphy->bands[band];
1176 BUG_ON(chan_idx >= sband->n_channels);
1177 chan = &sband->channels[chan_idx];
1179 flags = chan->orig_flags;
1181 r = freq_reg_info(wiphy,
1182 MHZ_TO_KHZ(chan->center_freq),
1188 * This means no regulatory rule was found in the country IE
1189 * with a frequency range on the center_freq's band, since
1190 * IEEE-802.11 allows for a country IE to have a subset of the
1191 * regulatory information provided in a country we ignore
1192 * disabling the channel unless at least one reg rule was
1193 * found on the center_freq's band. For details see this
1196 * http://tinyurl.com/11d-clarification
1199 last_request->initiator ==
1200 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1201 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
1202 "intact on %s - no rule found in band on "
1204 chan->center_freq, wiphy_name(wiphy));
1207 * In this case we know the country IE has at least one reg rule
1208 * for the band so we respect its band definitions
1210 if (last_request->initiator ==
1211 NL80211_REGDOM_SET_BY_COUNTRY_IE)
1212 REG_DBG_PRINT("cfg80211: Disabling "
1213 "channel %d MHz on %s due to "
1215 chan->center_freq, wiphy_name(wiphy));
1216 flags |= IEEE80211_CHAN_DISABLED;
1217 chan->flags = flags;
1222 power_rule = ®_rule->power_rule;
1223 freq_range = ®_rule->freq_range;
1225 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1226 bw_flags = IEEE80211_CHAN_NO_HT40;
1228 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1229 request_wiphy && request_wiphy == wiphy &&
1230 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
1232 * This gaurantees the driver's requested regulatory domain
1233 * will always be used as a base for further regulatory
1236 chan->flags = chan->orig_flags =
1237 map_regdom_flags(reg_rule->flags) | bw_flags;
1238 chan->max_antenna_gain = chan->orig_mag =
1239 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1240 chan->max_power = chan->orig_mpwr =
1241 (int) MBM_TO_DBM(power_rule->max_eirp);
1245 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1246 chan->max_antenna_gain = min(chan->orig_mag,
1247 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
1248 if (chan->orig_mpwr)
1249 chan->max_power = min(chan->orig_mpwr,
1250 (int) MBM_TO_DBM(power_rule->max_eirp));
1252 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1255 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
1258 struct ieee80211_supported_band *sband;
1260 BUG_ON(!wiphy->bands[band]);
1261 sband = wiphy->bands[band];
1263 for (i = 0; i < sband->n_channels; i++)
1264 handle_channel(wiphy, band, i);
1267 static bool ignore_reg_update(struct wiphy *wiphy,
1268 enum nl80211_reg_initiator initiator)
1272 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1273 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1276 * wiphy->regd will be set once the device has its own
1277 * desired regulatory domain set
1279 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
1280 !is_world_regdom(last_request->alpha2))
1285 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1287 struct cfg80211_registered_device *rdev;
1289 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1290 wiphy_update_regulatory(&rdev->wiphy, initiator);
1293 static void handle_reg_beacon(struct wiphy *wiphy,
1294 unsigned int chan_idx,
1295 struct reg_beacon *reg_beacon)
1297 struct ieee80211_supported_band *sband;
1298 struct ieee80211_channel *chan;
1299 bool channel_changed = false;
1300 struct ieee80211_channel chan_before;
1302 assert_cfg80211_lock();
1304 sband = wiphy->bands[reg_beacon->chan.band];
1305 chan = &sband->channels[chan_idx];
1307 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1310 if (chan->beacon_found)
1313 chan->beacon_found = true;
1315 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
1318 chan_before.center_freq = chan->center_freq;
1319 chan_before.flags = chan->flags;
1321 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1322 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1323 channel_changed = true;
1326 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1327 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1328 channel_changed = true;
1331 if (channel_changed)
1332 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1336 * Called when a scan on a wiphy finds a beacon on
1339 static void wiphy_update_new_beacon(struct wiphy *wiphy,
1340 struct reg_beacon *reg_beacon)
1343 struct ieee80211_supported_band *sband;
1345 assert_cfg80211_lock();
1347 if (!wiphy->bands[reg_beacon->chan.band])
1350 sband = wiphy->bands[reg_beacon->chan.band];
1352 for (i = 0; i < sband->n_channels; i++)
1353 handle_reg_beacon(wiphy, i, reg_beacon);
1357 * Called upon reg changes or a new wiphy is added
1359 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1362 struct ieee80211_supported_band *sband;
1363 struct reg_beacon *reg_beacon;
1365 assert_cfg80211_lock();
1367 if (list_empty(®_beacon_list))
1370 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
1371 if (!wiphy->bands[reg_beacon->chan.band])
1373 sband = wiphy->bands[reg_beacon->chan.band];
1374 for (i = 0; i < sband->n_channels; i++)
1375 handle_reg_beacon(wiphy, i, reg_beacon);
1379 static bool reg_is_world_roaming(struct wiphy *wiphy)
1381 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1382 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1385 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1386 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1391 /* Reap the advantages of previously found beacons */
1392 static void reg_process_beacons(struct wiphy *wiphy)
1395 * Means we are just firing up cfg80211, so no beacons would
1396 * have been processed yet.
1400 if (!reg_is_world_roaming(wiphy))
1402 wiphy_update_beacon_reg(wiphy);
1405 static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1409 if (chan->flags & IEEE80211_CHAN_DISABLED)
1411 /* This would happen when regulatory rules disallow HT40 completely */
1412 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1417 static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1418 enum ieee80211_band band,
1419 unsigned int chan_idx)
1421 struct ieee80211_supported_band *sband;
1422 struct ieee80211_channel *channel;
1423 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1426 assert_cfg80211_lock();
1428 sband = wiphy->bands[band];
1429 BUG_ON(chan_idx >= sband->n_channels);
1430 channel = &sband->channels[chan_idx];
1432 if (is_ht40_not_allowed(channel)) {
1433 channel->flags |= IEEE80211_CHAN_NO_HT40;
1438 * We need to ensure the extension channels exist to
1439 * be able to use HT40- or HT40+, this finds them (or not)
1441 for (i = 0; i < sband->n_channels; i++) {
1442 struct ieee80211_channel *c = &sband->channels[i];
1443 if (c->center_freq == (channel->center_freq - 20))
1445 if (c->center_freq == (channel->center_freq + 20))
1450 * Please note that this assumes target bandwidth is 20 MHz,
1451 * if that ever changes we also need to change the below logic
1452 * to include that as well.
1454 if (is_ht40_not_allowed(channel_before))
1455 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1457 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1459 if (is_ht40_not_allowed(channel_after))
1460 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1462 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1465 static void reg_process_ht_flags_band(struct wiphy *wiphy,
1466 enum ieee80211_band band)
1469 struct ieee80211_supported_band *sband;
1471 BUG_ON(!wiphy->bands[band]);
1472 sband = wiphy->bands[band];
1474 for (i = 0; i < sband->n_channels; i++)
1475 reg_process_ht_flags_channel(wiphy, band, i);
1478 static void reg_process_ht_flags(struct wiphy *wiphy)
1480 enum ieee80211_band band;
1485 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1486 if (wiphy->bands[band])
1487 reg_process_ht_flags_band(wiphy, band);
1492 void wiphy_update_regulatory(struct wiphy *wiphy,
1493 enum nl80211_reg_initiator initiator)
1495 enum ieee80211_band band;
1497 if (ignore_reg_update(wiphy, initiator))
1499 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1500 if (wiphy->bands[band])
1501 handle_band(wiphy, band);
1504 reg_process_beacons(wiphy);
1505 reg_process_ht_flags(wiphy);
1506 if (wiphy->reg_notifier)
1507 wiphy->reg_notifier(wiphy, last_request);
1510 static void handle_channel_custom(struct wiphy *wiphy,
1511 enum ieee80211_band band,
1512 unsigned int chan_idx,
1513 const struct ieee80211_regdomain *regd)
1516 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1518 const struct ieee80211_reg_rule *reg_rule = NULL;
1519 const struct ieee80211_power_rule *power_rule = NULL;
1520 const struct ieee80211_freq_range *freq_range = NULL;
1521 struct ieee80211_supported_band *sband;
1522 struct ieee80211_channel *chan;
1526 sband = wiphy->bands[band];
1527 BUG_ON(chan_idx >= sband->n_channels);
1528 chan = &sband->channels[chan_idx];
1530 r = freq_reg_info_regd(wiphy,
1531 MHZ_TO_KHZ(chan->center_freq),
1537 chan->flags = IEEE80211_CHAN_DISABLED;
1541 power_rule = ®_rule->power_rule;
1542 freq_range = ®_rule->freq_range;
1544 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1545 bw_flags = IEEE80211_CHAN_NO_HT40;
1547 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1548 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1549 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1552 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1553 const struct ieee80211_regdomain *regd)
1556 struct ieee80211_supported_band *sband;
1558 BUG_ON(!wiphy->bands[band]);
1559 sband = wiphy->bands[band];
1561 for (i = 0; i < sband->n_channels; i++)
1562 handle_channel_custom(wiphy, band, i, regd);
1565 /* Used by drivers prior to wiphy registration */
1566 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1567 const struct ieee80211_regdomain *regd)
1569 enum ieee80211_band band;
1570 unsigned int bands_set = 0;
1572 mutex_lock(®_mutex);
1573 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1574 if (!wiphy->bands[band])
1576 handle_band_custom(wiphy, band, regd);
1579 mutex_unlock(®_mutex);
1582 * no point in calling this if it won't have any effect
1583 * on your device's supportd bands.
1585 WARN_ON(!bands_set);
1587 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1590 * Return value which can be used by ignore_request() to indicate
1591 * it has been determined we should intersect two regulatory domains
1593 #define REG_INTERSECT 1
1595 /* This has the logic which determines when a new request
1596 * should be ignored. */
1597 static int ignore_request(struct wiphy *wiphy,
1598 struct regulatory_request *pending_request)
1600 struct wiphy *last_wiphy = NULL;
1602 assert_cfg80211_lock();
1604 /* All initial requests are respected */
1608 switch (pending_request->initiator) {
1609 case NL80211_REGDOM_SET_BY_CORE:
1611 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1613 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1615 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
1617 if (last_request->initiator ==
1618 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1619 if (last_wiphy != wiphy) {
1621 * Two cards with two APs claiming different
1622 * Country IE alpha2s. We could
1623 * intersect them, but that seems unlikely
1624 * to be correct. Reject second one for now.
1626 if (regdom_changes(pending_request->alpha2))
1631 * Two consecutive Country IE hints on the same wiphy.
1632 * This should be picked up early by the driver/stack
1634 if (WARN_ON(regdom_changes(pending_request->alpha2)))
1638 return REG_INTERSECT;
1639 case NL80211_REGDOM_SET_BY_DRIVER:
1640 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1641 if (regdom_changes(pending_request->alpha2))
1647 * This would happen if you unplug and plug your card
1648 * back in or if you add a new device for which the previously
1649 * loaded card also agrees on the regulatory domain.
1651 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1652 !regdom_changes(pending_request->alpha2))
1655 return REG_INTERSECT;
1656 case NL80211_REGDOM_SET_BY_USER:
1657 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1658 return REG_INTERSECT;
1660 * If the user knows better the user should set the regdom
1661 * to their country before the IE is picked up
1663 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1664 last_request->intersect)
1667 * Process user requests only after previous user/driver/core
1668 * requests have been processed
1670 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1671 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1672 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1673 if (regdom_changes(last_request->alpha2))
1677 if (!regdom_changes(pending_request->alpha2))
1687 * __regulatory_hint - hint to the wireless core a regulatory domain
1688 * @wiphy: if the hint comes from country information from an AP, this
1689 * is required to be set to the wiphy that received the information
1690 * @pending_request: the regulatory request currently being processed
1692 * The Wireless subsystem can use this function to hint to the wireless core
1693 * what it believes should be the current regulatory domain.
1695 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1696 * already been set or other standard error codes.
1698 * Caller must hold &cfg80211_mutex and ®_mutex
1700 static int __regulatory_hint(struct wiphy *wiphy,
1701 struct regulatory_request *pending_request)
1703 bool intersect = false;
1706 assert_cfg80211_lock();
1708 r = ignore_request(wiphy, pending_request);
1710 if (r == REG_INTERSECT) {
1711 if (pending_request->initiator ==
1712 NL80211_REGDOM_SET_BY_DRIVER) {
1713 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1715 kfree(pending_request);
1722 * If the regulatory domain being requested by the
1723 * driver has already been set just copy it to the
1726 if (r == -EALREADY &&
1727 pending_request->initiator ==
1728 NL80211_REGDOM_SET_BY_DRIVER) {
1729 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1731 kfree(pending_request);
1737 kfree(pending_request);
1742 kfree(last_request);
1744 last_request = pending_request;
1745 last_request->intersect = intersect;
1747 pending_request = NULL;
1749 /* When r == REG_INTERSECT we do need to call CRDA */
1752 * Since CRDA will not be called in this case as we already
1753 * have applied the requested regulatory domain before we just
1754 * inform userspace we have processed the request
1757 nl80211_send_reg_change_event(last_request);
1761 return call_crda(last_request->alpha2);
1764 /* This processes *all* regulatory hints */
1765 static void reg_process_hint(struct regulatory_request *reg_request)
1768 struct wiphy *wiphy = NULL;
1770 BUG_ON(!reg_request->alpha2);
1772 mutex_lock(&cfg80211_mutex);
1773 mutex_lock(®_mutex);
1775 if (wiphy_idx_valid(reg_request->wiphy_idx))
1776 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1778 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1784 r = __regulatory_hint(wiphy, reg_request);
1785 /* This is required so that the orig_* parameters are saved */
1786 if (r == -EALREADY && wiphy &&
1787 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
1788 wiphy_update_regulatory(wiphy, reg_request->initiator);
1790 mutex_unlock(®_mutex);
1791 mutex_unlock(&cfg80211_mutex);
1794 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1795 static void reg_process_pending_hints(void)
1797 struct regulatory_request *reg_request;
1799 spin_lock(®_requests_lock);
1800 while (!list_empty(®_requests_list)) {
1801 reg_request = list_first_entry(®_requests_list,
1802 struct regulatory_request,
1804 list_del_init(®_request->list);
1806 spin_unlock(®_requests_lock);
1807 reg_process_hint(reg_request);
1808 spin_lock(®_requests_lock);
1810 spin_unlock(®_requests_lock);
1813 /* Processes beacon hints -- this has nothing to do with country IEs */
1814 static void reg_process_pending_beacon_hints(void)
1816 struct cfg80211_registered_device *rdev;
1817 struct reg_beacon *pending_beacon, *tmp;
1820 * No need to hold the reg_mutex here as we just touch wiphys
1821 * and do not read or access regulatory variables.
1823 mutex_lock(&cfg80211_mutex);
1825 /* This goes through the _pending_ beacon list */
1826 spin_lock_bh(®_pending_beacons_lock);
1828 if (list_empty(®_pending_beacons)) {
1829 spin_unlock_bh(®_pending_beacons_lock);
1833 list_for_each_entry_safe(pending_beacon, tmp,
1834 ®_pending_beacons, list) {
1836 list_del_init(&pending_beacon->list);
1838 /* Applies the beacon hint to current wiphys */
1839 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1840 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
1842 /* Remembers the beacon hint for new wiphys or reg changes */
1843 list_add_tail(&pending_beacon->list, ®_beacon_list);
1846 spin_unlock_bh(®_pending_beacons_lock);
1848 mutex_unlock(&cfg80211_mutex);
1851 static void reg_todo(struct work_struct *work)
1853 reg_process_pending_hints();
1854 reg_process_pending_beacon_hints();
1857 static DECLARE_WORK(reg_work, reg_todo);
1859 static void queue_regulatory_request(struct regulatory_request *request)
1861 spin_lock(®_requests_lock);
1862 list_add_tail(&request->list, ®_requests_list);
1863 spin_unlock(®_requests_lock);
1865 schedule_work(®_work);
1868 /* Core regulatory hint -- happens once during cfg80211_init() */
1869 static int regulatory_hint_core(const char *alpha2)
1871 struct regulatory_request *request;
1873 BUG_ON(last_request);
1875 request = kzalloc(sizeof(struct regulatory_request),
1880 request->alpha2[0] = alpha2[0];
1881 request->alpha2[1] = alpha2[1];
1882 request->initiator = NL80211_REGDOM_SET_BY_CORE;
1884 queue_regulatory_request(request);
1887 * This ensures last_request is populated once modules
1888 * come swinging in and calling regulatory hints and
1889 * wiphy_apply_custom_regulatory().
1891 flush_scheduled_work();
1897 int regulatory_hint_user(const char *alpha2)
1899 struct regulatory_request *request;
1903 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1907 request->wiphy_idx = WIPHY_IDX_STALE;
1908 request->alpha2[0] = alpha2[0];
1909 request->alpha2[1] = alpha2[1];
1910 request->initiator = NL80211_REGDOM_SET_BY_USER;
1912 queue_regulatory_request(request);
1918 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1920 struct regulatory_request *request;
1925 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1929 request->wiphy_idx = get_wiphy_idx(wiphy);
1931 /* Must have registered wiphy first */
1932 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1934 request->alpha2[0] = alpha2[0];
1935 request->alpha2[1] = alpha2[1];
1936 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1938 queue_regulatory_request(request);
1942 EXPORT_SYMBOL(regulatory_hint);
1944 /* Caller must hold reg_mutex */
1945 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1946 u32 country_ie_checksum)
1948 struct wiphy *request_wiphy;
1952 if (unlikely(last_request->initiator !=
1953 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1956 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1961 if (likely(request_wiphy != wiphy))
1962 return !country_ie_integrity_changes(country_ie_checksum);
1964 * We should not have let these through at this point, they
1965 * should have been picked up earlier by the first alpha2 check
1968 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1974 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1975 * therefore cannot iterate over the rdev list here.
1977 void regulatory_hint_11d(struct wiphy *wiphy,
1981 struct ieee80211_regdomain *rd = NULL;
1984 enum environment_cap env = ENVIRON_ANY;
1985 struct regulatory_request *request;
1987 mutex_lock(®_mutex);
1989 if (unlikely(!last_request))
1992 /* IE len must be evenly divisible by 2 */
1993 if (country_ie_len & 0x01)
1996 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2000 * Pending country IE processing, this can happen after we
2001 * call CRDA and wait for a response if a beacon was received before
2002 * we were able to process the last regulatory_hint_11d() call
2004 if (country_ie_regdomain)
2007 alpha2[0] = country_ie[0];
2008 alpha2[1] = country_ie[1];
2010 if (country_ie[2] == 'I')
2011 env = ENVIRON_INDOOR;
2012 else if (country_ie[2] == 'O')
2013 env = ENVIRON_OUTDOOR;
2016 * We will run this only upon a successful connection on cfg80211.
2017 * We leave conflict resolution to the workqueue, where can hold
2020 if (likely(last_request->initiator ==
2021 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2022 wiphy_idx_valid(last_request->wiphy_idx)))
2025 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
2027 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
2032 * This will not happen right now but we leave it here for the
2033 * the future when we want to add suspend/resume support and having
2034 * the user move to another country after doing so, or having the user
2035 * move to another AP. Right now we just trust the first AP.
2037 * If we hit this before we add this support we want to be informed of
2038 * it as it would indicate a mistake in the current design
2040 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
2043 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2048 * We keep this around for when CRDA comes back with a response so
2049 * we can intersect with that
2051 country_ie_regdomain = rd;
2053 request->wiphy_idx = get_wiphy_idx(wiphy);
2054 request->alpha2[0] = rd->alpha2[0];
2055 request->alpha2[1] = rd->alpha2[1];
2056 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
2057 request->country_ie_checksum = checksum;
2058 request->country_ie_env = env;
2060 mutex_unlock(®_mutex);
2062 queue_regulatory_request(request);
2069 mutex_unlock(®_mutex);
2072 static bool freq_is_chan_12_13_14(u16 freq)
2074 if (freq == ieee80211_channel_to_frequency(12) ||
2075 freq == ieee80211_channel_to_frequency(13) ||
2076 freq == ieee80211_channel_to_frequency(14))
2081 int regulatory_hint_found_beacon(struct wiphy *wiphy,
2082 struct ieee80211_channel *beacon_chan,
2085 struct reg_beacon *reg_beacon;
2087 if (likely((beacon_chan->beacon_found ||
2088 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
2089 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
2090 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
2093 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2097 REG_DBG_PRINT("cfg80211: Found new beacon on "
2098 "frequency: %d MHz (Ch %d) on %s\n",
2099 beacon_chan->center_freq,
2100 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2103 memcpy(®_beacon->chan, beacon_chan,
2104 sizeof(struct ieee80211_channel));
2108 * Since we can be called from BH or and non-BH context
2109 * we must use spin_lock_bh()
2111 spin_lock_bh(®_pending_beacons_lock);
2112 list_add_tail(®_beacon->list, ®_pending_beacons);
2113 spin_unlock_bh(®_pending_beacons_lock);
2115 schedule_work(®_work);
2120 static void print_rd_rules(const struct ieee80211_regdomain *rd)
2123 const struct ieee80211_reg_rule *reg_rule = NULL;
2124 const struct ieee80211_freq_range *freq_range = NULL;
2125 const struct ieee80211_power_rule *power_rule = NULL;
2127 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
2128 "(max_antenna_gain, max_eirp)\n");
2130 for (i = 0; i < rd->n_reg_rules; i++) {
2131 reg_rule = &rd->reg_rules[i];
2132 freq_range = ®_rule->freq_range;
2133 power_rule = ®_rule->power_rule;
2136 * There may not be documentation for max antenna gain
2137 * in certain regions
2139 if (power_rule->max_antenna_gain)
2140 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
2141 "(%d mBi, %d mBm)\n",
2142 freq_range->start_freq_khz,
2143 freq_range->end_freq_khz,
2144 freq_range->max_bandwidth_khz,
2145 power_rule->max_antenna_gain,
2146 power_rule->max_eirp);
2148 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
2150 freq_range->start_freq_khz,
2151 freq_range->end_freq_khz,
2152 freq_range->max_bandwidth_khz,
2153 power_rule->max_eirp);
2157 static void print_regdomain(const struct ieee80211_regdomain *rd)
2160 if (is_intersected_alpha2(rd->alpha2)) {
2162 if (last_request->initiator ==
2163 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2164 struct cfg80211_registered_device *rdev;
2165 rdev = cfg80211_rdev_by_wiphy_idx(
2166 last_request->wiphy_idx);
2168 printk(KERN_INFO "cfg80211: Current regulatory "
2169 "domain updated by AP to: %c%c\n",
2170 rdev->country_ie_alpha2[0],
2171 rdev->country_ie_alpha2[1]);
2173 printk(KERN_INFO "cfg80211: Current regulatory "
2174 "domain intersected: \n");
2176 printk(KERN_INFO "cfg80211: Current regulatory "
2177 "domain intersected: \n");
2178 } else if (is_world_regdom(rd->alpha2))
2179 printk(KERN_INFO "cfg80211: World regulatory "
2180 "domain updated:\n");
2182 if (is_unknown_alpha2(rd->alpha2))
2183 printk(KERN_INFO "cfg80211: Regulatory domain "
2184 "changed to driver built-in settings "
2185 "(unknown country)\n");
2187 printk(KERN_INFO "cfg80211: Regulatory domain "
2188 "changed to country: %c%c\n",
2189 rd->alpha2[0], rd->alpha2[1]);
2194 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
2196 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2197 rd->alpha2[0], rd->alpha2[1]);
2201 #ifdef CONFIG_CFG80211_REG_DEBUG
2202 static void reg_country_ie_process_debug(
2203 const struct ieee80211_regdomain *rd,
2204 const struct ieee80211_regdomain *country_ie_regdomain,
2205 const struct ieee80211_regdomain *intersected_rd)
2207 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2208 print_regdomain_info(country_ie_regdomain);
2209 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2210 print_regdomain_info(rd);
2211 if (intersected_rd) {
2212 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2214 print_regdomain_info(intersected_rd);
2217 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2220 static inline void reg_country_ie_process_debug(
2221 const struct ieee80211_regdomain *rd,
2222 const struct ieee80211_regdomain *country_ie_regdomain,
2223 const struct ieee80211_regdomain *intersected_rd)
2228 /* Takes ownership of rd only if it doesn't fail */
2229 static int __set_regdom(const struct ieee80211_regdomain *rd)
2231 const struct ieee80211_regdomain *intersected_rd = NULL;
2232 struct cfg80211_registered_device *rdev = NULL;
2233 struct wiphy *request_wiphy;
2234 /* Some basic sanity checks first */
2236 if (is_world_regdom(rd->alpha2)) {
2237 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2239 update_world_regdomain(rd);
2243 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2244 !is_unknown_alpha2(rd->alpha2))
2251 * Lets only bother proceeding on the same alpha2 if the current
2252 * rd is non static (it means CRDA was present and was used last)
2253 * and the pending request came in from a country IE
2255 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2257 * If someone else asked us to change the rd lets only bother
2258 * checking if the alpha2 changes if CRDA was already called
2260 if (!regdom_changes(rd->alpha2))
2265 * Now lets set the regulatory domain, update all driver channels
2266 * and finally inform them of what we have done, in case they want
2267 * to review or adjust their own settings based on their own
2268 * internal EEPROM data
2271 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2274 if (!is_valid_rd(rd)) {
2275 printk(KERN_ERR "cfg80211: Invalid "
2276 "regulatory domain detected:\n");
2277 print_regdomain_info(rd);
2281 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2283 if (!last_request->intersect) {
2286 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
2288 cfg80211_regdomain = rd;
2293 * For a driver hint, lets copy the regulatory domain the
2294 * driver wanted to the wiphy to deal with conflicts
2298 * Userspace could have sent two replies with only
2299 * one kernel request.
2301 if (request_wiphy->regd)
2304 r = reg_copy_regd(&request_wiphy->regd, rd);
2309 cfg80211_regdomain = rd;
2313 /* Intersection requires a bit more work */
2315 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2317 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2318 if (!intersected_rd)
2322 * We can trash what CRDA provided now.
2323 * However if a driver requested this specific regulatory
2324 * domain we keep it for its private use
2326 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2327 request_wiphy->regd = rd;
2334 cfg80211_regdomain = intersected_rd;
2340 * Country IE requests are handled a bit differently, we intersect
2341 * the country IE rd with what CRDA believes that country should have
2345 * Userspace could have sent two replies with only
2346 * one kernel request. By the second reply we would have
2347 * already processed and consumed the country_ie_regdomain.
2349 if (!country_ie_regdomain)
2351 BUG_ON(rd == country_ie_regdomain);
2354 * Intersect what CRDA returned and our what we
2355 * had built from the Country IE received
2358 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2360 reg_country_ie_process_debug(rd,
2361 country_ie_regdomain,
2364 kfree(country_ie_regdomain);
2365 country_ie_regdomain = NULL;
2367 if (!intersected_rd)
2370 rdev = wiphy_to_dev(request_wiphy);
2372 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2373 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2374 rdev->env = last_request->country_ie_env;
2376 BUG_ON(intersected_rd == rd);
2382 cfg80211_regdomain = intersected_rd;
2389 * Use this call to set the current regulatory domain. Conflicts with
2390 * multiple drivers can be ironed out later. Caller must've already
2391 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2393 int set_regdom(const struct ieee80211_regdomain *rd)
2397 assert_cfg80211_lock();
2399 mutex_lock(®_mutex);
2401 /* Note that this doesn't update the wiphys, this is done below */
2402 r = __set_regdom(rd);
2405 mutex_unlock(®_mutex);
2409 /* This would make this whole thing pointless */
2410 if (!last_request->intersect)
2411 BUG_ON(rd != cfg80211_regdomain);
2413 /* update all wiphys now with the new established regulatory domain */
2414 update_all_wiphy_regulatory(last_request->initiator);
2416 print_regdomain(cfg80211_regdomain);
2418 nl80211_send_reg_change_event(last_request);
2420 mutex_unlock(®_mutex);
2425 /* Caller must hold cfg80211_mutex */
2426 void reg_device_remove(struct wiphy *wiphy)
2428 struct wiphy *request_wiphy = NULL;
2430 assert_cfg80211_lock();
2432 mutex_lock(®_mutex);
2437 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2439 if (!request_wiphy || request_wiphy != wiphy)
2442 last_request->wiphy_idx = WIPHY_IDX_STALE;
2443 last_request->country_ie_env = ENVIRON_ANY;
2445 mutex_unlock(®_mutex);
2448 int regulatory_init(void)
2452 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2453 if (IS_ERR(reg_pdev))
2454 return PTR_ERR(reg_pdev);
2456 spin_lock_init(®_requests_lock);
2457 spin_lock_init(®_pending_beacons_lock);
2459 cfg80211_regdomain = cfg80211_world_regdom;
2461 /* We always try to get an update for the static regdomain */
2462 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
2467 * N.B. kobject_uevent_env() can fail mainly for when we're out
2468 * memory which is handled and propagated appropriately above
2469 * but it can also fail during a netlink_broadcast() or during
2470 * early boot for call_usermodehelper(). For now treat these
2471 * errors as non-fatal.
2473 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2474 "to call CRDA during init");
2475 #ifdef CONFIG_CFG80211_REG_DEBUG
2476 /* We want to find out exactly why when debugging */
2482 * Finally, if the user set the module parameter treat it
2485 if (!is_world_regdom(ieee80211_regdom))
2486 regulatory_hint_user(ieee80211_regdom);
2491 void regulatory_exit(void)
2493 struct regulatory_request *reg_request, *tmp;
2494 struct reg_beacon *reg_beacon, *btmp;
2496 cancel_work_sync(®_work);
2498 mutex_lock(&cfg80211_mutex);
2499 mutex_lock(®_mutex);
2503 kfree(country_ie_regdomain);
2504 country_ie_regdomain = NULL;
2506 kfree(last_request);
2508 platform_device_unregister(reg_pdev);
2510 spin_lock_bh(®_pending_beacons_lock);
2511 if (!list_empty(®_pending_beacons)) {
2512 list_for_each_entry_safe(reg_beacon, btmp,
2513 ®_pending_beacons, list) {
2514 list_del(®_beacon->list);
2518 spin_unlock_bh(®_pending_beacons_lock);
2520 if (!list_empty(®_beacon_list)) {
2521 list_for_each_entry_safe(reg_beacon, btmp,
2522 ®_beacon_list, list) {
2523 list_del(®_beacon->list);
2528 spin_lock(®_requests_lock);
2529 if (!list_empty(®_requests_list)) {
2530 list_for_each_entry_safe(reg_request, tmp,
2531 ®_requests_list, list) {
2532 list_del(®_request->list);
2536 spin_unlock(®_requests_lock);
2538 mutex_unlock(®_mutex);
2539 mutex_unlock(&cfg80211_mutex);