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 if (triplet->chans.num_channels == 0)
591 /* Monitonically increasing channel order */
592 if (triplet->chans.first_channel <= end_subband_chan)
596 if (triplet->chans.first_channel <= 14) {
597 end_channel = triplet->chans.first_channel +
598 triplet->chans.num_channels - 1;
601 end_channel = triplet->chans.first_channel +
602 (4 * (triplet->chans.num_channels - 1));
603 next_band = IEEE80211_BAND_5GHZ;
606 if (band != next_band) {
608 *country_ie_len += 3;
612 if (orig_max_power != triplet->chans.max_power) {
614 *country_ie_len += 3;
618 cur_channel = triplet->chans.first_channel;
620 /* The key is finding the right next expected channel */
621 if (band == IEEE80211_BAND_2GHZ)
622 next_expected_chan = end_subband_chan + 1;
624 next_expected_chan = end_subband_chan + 4;
626 if (cur_channel != next_expected_chan) {
628 *country_ie_len += 3;
632 end_subband_chan = end_channel;
634 /* Move to the next one */
636 *country_ie_len -= 3;
639 * Padding needs to be dealt with if we processed
642 if (*country_ie_len == 1) {
644 *country_ie_len -= 1;
648 /* If seen, the IE is invalid */
649 if (*country_ie_len == 2)
653 if (end_subband_chan == orig_end_channel) {
654 *country_ie = triplets_start;
655 *country_ie_len = len_at_triplet;
656 return orig_end_channel;
659 return end_subband_chan;
663 * Converts a country IE to a regulatory domain. A regulatory domain
664 * structure has a lot of information which the IE doesn't yet have,
665 * so for the other values we use upper max values as we will intersect
666 * with our userspace regulatory agent to get lower bounds.
668 static struct ieee80211_regdomain *country_ie_2_rd(
673 struct ieee80211_regdomain *rd = NULL;
677 u32 num_rules = 0, size_of_regd = 0;
678 u8 *triplets_start = NULL;
679 u8 len_at_triplet = 0;
680 /* the last channel we have registered in a subband (triplet) */
681 int last_sub_max_channel = 0;
683 *checksum = 0xDEADBEEF;
685 /* Country IE requirements */
686 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
687 country_ie_len & 0x01);
689 alpha2[0] = country_ie[0];
690 alpha2[1] = country_ie[1];
693 * Third octet can be:
697 * anything else we assume is no restrictions
699 if (country_ie[2] == 'I')
700 flags = NL80211_RRF_NO_OUTDOOR;
701 else if (country_ie[2] == 'O')
702 flags = NL80211_RRF_NO_INDOOR;
707 triplets_start = country_ie;
708 len_at_triplet = country_ie_len;
710 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
713 * We need to build a reg rule for each triplet, but first we must
714 * calculate the number of reg rules we will need. We will need one
715 * for each channel subband
717 while (country_ie_len >= 3) {
719 struct ieee80211_country_ie_triplet *triplet =
720 (struct ieee80211_country_ie_triplet *) country_ie;
721 int cur_sub_max_channel = 0, cur_channel = 0;
723 if (triplet->ext.reg_extension_id >=
724 IEEE80211_COUNTRY_EXTENSION_ID) {
731 * APs can add padding to make length divisible
732 * by two, required by the spec.
734 if (triplet->chans.first_channel == 0) {
737 /* This is expected to be at the very end only */
738 if (country_ie_len != 0)
743 if (triplet->chans.num_channels == 0)
747 if (triplet->chans.first_channel <= 14)
748 end_channel = triplet->chans.first_channel +
749 triplet->chans.num_channels - 1;
752 * 5 GHz -- For example in country IEs if the first
753 * channel given is 36 and the number of channels is 4
754 * then the individual channel numbers defined for the
755 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
756 * and not 36, 37, 38, 39.
758 * See: http://tinyurl.com/11d-clarification
760 end_channel = triplet->chans.first_channel +
761 (4 * (triplet->chans.num_channels - 1));
763 cur_channel = triplet->chans.first_channel;
766 * Enhancement for APs that send a triplet for every channel
767 * or for whatever reason sends triplets with multiple channels
768 * separated when in fact they should be together.
770 end_channel = max_subband_chan(cur_channel,
772 triplet->chans.max_power,
778 cur_sub_max_channel = end_channel;
780 /* Basic sanity check */
781 if (cur_sub_max_channel < cur_channel)
785 * Do not allow overlapping channels. Also channels
786 * passed in each subband must be monotonically
789 if (last_sub_max_channel) {
790 if (cur_channel <= last_sub_max_channel)
792 if (cur_sub_max_channel <= last_sub_max_channel)
797 * When dot11RegulatoryClassesRequired is supported
798 * we can throw ext triplets as part of this soup,
799 * for now we don't care when those change as we
802 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
803 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
804 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
806 last_sub_max_channel = cur_sub_max_channel;
810 if (country_ie_len >= 3) {
816 * Note: this is not a IEEE requirement but
817 * simply a memory requirement
819 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
823 country_ie = triplets_start;
824 country_ie_len = len_at_triplet;
826 size_of_regd = sizeof(struct ieee80211_regdomain) +
827 (num_rules * sizeof(struct ieee80211_reg_rule));
829 rd = kzalloc(size_of_regd, GFP_KERNEL);
833 rd->n_reg_rules = num_rules;
834 rd->alpha2[0] = alpha2[0];
835 rd->alpha2[1] = alpha2[1];
837 /* This time around we fill in the rd */
838 while (country_ie_len >= 3) {
840 struct ieee80211_country_ie_triplet *triplet =
841 (struct ieee80211_country_ie_triplet *) country_ie;
842 struct ieee80211_reg_rule *reg_rule = NULL;
843 struct ieee80211_freq_range *freq_range = NULL;
844 struct ieee80211_power_rule *power_rule = NULL;
847 * Must parse if dot11RegulatoryClassesRequired is true,
848 * we don't support this yet
850 if (triplet->ext.reg_extension_id >=
851 IEEE80211_COUNTRY_EXTENSION_ID) {
857 if (triplet->chans.first_channel == 0) {
863 reg_rule = &rd->reg_rules[i];
864 freq_range = ®_rule->freq_range;
865 power_rule = ®_rule->power_rule;
867 reg_rule->flags = flags;
870 if (triplet->chans.first_channel <= 14)
871 end_channel = triplet->chans.first_channel +
872 triplet->chans.num_channels -1;
874 end_channel = triplet->chans.first_channel +
875 (4 * (triplet->chans.num_channels - 1));
877 end_channel = max_subband_chan(triplet->chans.first_channel,
879 triplet->chans.max_power,
884 * The +10 is since the regulatory domain expects
885 * the actual band edge, not the center of freq for
886 * its start and end freqs, assuming 20 MHz bandwidth on
887 * the channels passed
889 freq_range->start_freq_khz =
890 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
891 triplet->chans.first_channel) - 10);
892 freq_range->end_freq_khz =
893 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
897 * These are large arbitrary values we use to intersect later.
898 * Increment this if we ever support >= 40 MHz channels
901 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
902 power_rule->max_antenna_gain = DBI_TO_MBI(100);
903 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
907 if (country_ie_len >= 3) {
912 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
920 * Helper for regdom_intersect(), this does the real
921 * mathematical intersection fun
923 static int reg_rules_intersect(
924 const struct ieee80211_reg_rule *rule1,
925 const struct ieee80211_reg_rule *rule2,
926 struct ieee80211_reg_rule *intersected_rule)
928 const struct ieee80211_freq_range *freq_range1, *freq_range2;
929 struct ieee80211_freq_range *freq_range;
930 const struct ieee80211_power_rule *power_rule1, *power_rule2;
931 struct ieee80211_power_rule *power_rule;
934 freq_range1 = &rule1->freq_range;
935 freq_range2 = &rule2->freq_range;
936 freq_range = &intersected_rule->freq_range;
938 power_rule1 = &rule1->power_rule;
939 power_rule2 = &rule2->power_rule;
940 power_rule = &intersected_rule->power_rule;
942 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
943 freq_range2->start_freq_khz);
944 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
945 freq_range2->end_freq_khz);
946 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
947 freq_range2->max_bandwidth_khz);
949 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
950 if (freq_range->max_bandwidth_khz > freq_diff)
951 freq_range->max_bandwidth_khz = freq_diff;
953 power_rule->max_eirp = min(power_rule1->max_eirp,
954 power_rule2->max_eirp);
955 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
956 power_rule2->max_antenna_gain);
958 intersected_rule->flags = (rule1->flags | rule2->flags);
960 if (!is_valid_reg_rule(intersected_rule))
967 * regdom_intersect - do the intersection between two regulatory domains
968 * @rd1: first regulatory domain
969 * @rd2: second regulatory domain
971 * Use this function to get the intersection between two regulatory domains.
972 * Once completed we will mark the alpha2 for the rd as intersected, "98",
973 * as no one single alpha2 can represent this regulatory domain.
975 * Returns a pointer to the regulatory domain structure which will hold the
976 * resulting intersection of rules between rd1 and rd2. We will
977 * kzalloc() this structure for you.
979 static struct ieee80211_regdomain *regdom_intersect(
980 const struct ieee80211_regdomain *rd1,
981 const struct ieee80211_regdomain *rd2)
985 unsigned int num_rules = 0, rule_idx = 0;
986 const struct ieee80211_reg_rule *rule1, *rule2;
987 struct ieee80211_reg_rule *intersected_rule;
988 struct ieee80211_regdomain *rd;
989 /* This is just a dummy holder to help us count */
990 struct ieee80211_reg_rule irule;
992 /* Uses the stack temporarily for counter arithmetic */
993 intersected_rule = &irule;
995 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
1001 * First we get a count of the rules we'll need, then we actually
1002 * build them. This is to so we can malloc() and free() a
1003 * regdomain once. The reason we use reg_rules_intersect() here
1004 * is it will return -EINVAL if the rule computed makes no sense.
1005 * All rules that do check out OK are valid.
1008 for (x = 0; x < rd1->n_reg_rules; x++) {
1009 rule1 = &rd1->reg_rules[x];
1010 for (y = 0; y < rd2->n_reg_rules; y++) {
1011 rule2 = &rd2->reg_rules[y];
1012 if (!reg_rules_intersect(rule1, rule2,
1015 memset(intersected_rule, 0,
1016 sizeof(struct ieee80211_reg_rule));
1023 size_of_regd = sizeof(struct ieee80211_regdomain) +
1024 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
1026 rd = kzalloc(size_of_regd, GFP_KERNEL);
1030 for (x = 0; x < rd1->n_reg_rules; x++) {
1031 rule1 = &rd1->reg_rules[x];
1032 for (y = 0; y < rd2->n_reg_rules; y++) {
1033 rule2 = &rd2->reg_rules[y];
1035 * This time around instead of using the stack lets
1036 * write to the target rule directly saving ourselves
1039 intersected_rule = &rd->reg_rules[rule_idx];
1040 r = reg_rules_intersect(rule1, rule2,
1043 * No need to memset here the intersected rule here as
1044 * we're not using the stack anymore
1052 if (rule_idx != num_rules) {
1057 rd->n_reg_rules = num_rules;
1058 rd->alpha2[0] = '9';
1059 rd->alpha2[1] = '8';
1065 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1066 * want to just have the channel structure use these
1068 static u32 map_regdom_flags(u32 rd_flags)
1070 u32 channel_flags = 0;
1071 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
1072 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1073 if (rd_flags & NL80211_RRF_NO_IBSS)
1074 channel_flags |= IEEE80211_CHAN_NO_IBSS;
1075 if (rd_flags & NL80211_RRF_DFS)
1076 channel_flags |= IEEE80211_CHAN_RADAR;
1077 return channel_flags;
1080 static int freq_reg_info_regd(struct wiphy *wiphy,
1083 const struct ieee80211_reg_rule **reg_rule,
1084 const struct ieee80211_regdomain *custom_regd)
1087 bool band_rule_found = false;
1088 const struct ieee80211_regdomain *regd;
1089 bool bw_fits = false;
1091 if (!desired_bw_khz)
1092 desired_bw_khz = MHZ_TO_KHZ(20);
1094 regd = custom_regd ? custom_regd : cfg80211_regdomain;
1097 * Follow the driver's regulatory domain, if present, unless a country
1098 * IE has been processed or a user wants to help complaince further
1100 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1101 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
1108 for (i = 0; i < regd->n_reg_rules; i++) {
1109 const struct ieee80211_reg_rule *rr;
1110 const struct ieee80211_freq_range *fr = NULL;
1111 const struct ieee80211_power_rule *pr = NULL;
1113 rr = ®d->reg_rules[i];
1114 fr = &rr->freq_range;
1115 pr = &rr->power_rule;
1118 * We only need to know if one frequency rule was
1119 * was in center_freq's band, that's enough, so lets
1120 * not overwrite it once found
1122 if (!band_rule_found)
1123 band_rule_found = freq_in_rule_band(fr, center_freq);
1125 bw_fits = reg_does_bw_fit(fr,
1129 if (band_rule_found && bw_fits) {
1135 if (!band_rule_found)
1140 EXPORT_SYMBOL(freq_reg_info);
1142 int freq_reg_info(struct wiphy *wiphy,
1145 const struct ieee80211_reg_rule **reg_rule)
1147 assert_cfg80211_lock();
1148 return freq_reg_info_regd(wiphy,
1156 * Note that right now we assume the desired channel bandwidth
1157 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1158 * per channel, the primary and the extension channel). To support
1159 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1160 * new ieee80211_channel.target_bw and re run the regulatory check
1161 * on the wiphy with the target_bw specified. Then we can simply use
1162 * that below for the desired_bw_khz below.
1164 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1165 unsigned int chan_idx)
1168 u32 flags, bw_flags = 0;
1169 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1170 const struct ieee80211_reg_rule *reg_rule = NULL;
1171 const struct ieee80211_power_rule *power_rule = NULL;
1172 const struct ieee80211_freq_range *freq_range = NULL;
1173 struct ieee80211_supported_band *sband;
1174 struct ieee80211_channel *chan;
1175 struct wiphy *request_wiphy = NULL;
1177 assert_cfg80211_lock();
1179 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1181 sband = wiphy->bands[band];
1182 BUG_ON(chan_idx >= sband->n_channels);
1183 chan = &sband->channels[chan_idx];
1185 flags = chan->orig_flags;
1187 r = freq_reg_info(wiphy,
1188 MHZ_TO_KHZ(chan->center_freq),
1194 * This means no regulatory rule was found in the country IE
1195 * with a frequency range on the center_freq's band, since
1196 * IEEE-802.11 allows for a country IE to have a subset of the
1197 * regulatory information provided in a country we ignore
1198 * disabling the channel unless at least one reg rule was
1199 * found on the center_freq's band. For details see this
1202 * http://tinyurl.com/11d-clarification
1205 last_request->initiator ==
1206 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1207 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
1208 "intact on %s - no rule found in band on "
1210 chan->center_freq, wiphy_name(wiphy));
1213 * In this case we know the country IE has at least one reg rule
1214 * for the band so we respect its band definitions
1216 if (last_request->initiator ==
1217 NL80211_REGDOM_SET_BY_COUNTRY_IE)
1218 REG_DBG_PRINT("cfg80211: Disabling "
1219 "channel %d MHz on %s due to "
1221 chan->center_freq, wiphy_name(wiphy));
1222 flags |= IEEE80211_CHAN_DISABLED;
1223 chan->flags = flags;
1228 power_rule = ®_rule->power_rule;
1229 freq_range = ®_rule->freq_range;
1231 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1232 bw_flags = IEEE80211_CHAN_NO_HT40;
1234 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1235 request_wiphy && request_wiphy == wiphy &&
1236 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
1238 * This gaurantees the driver's requested regulatory domain
1239 * will always be used as a base for further regulatory
1242 chan->flags = chan->orig_flags =
1243 map_regdom_flags(reg_rule->flags) | bw_flags;
1244 chan->max_antenna_gain = chan->orig_mag =
1245 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1246 chan->max_power = chan->orig_mpwr =
1247 (int) MBM_TO_DBM(power_rule->max_eirp);
1251 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1252 chan->max_antenna_gain = min(chan->orig_mag,
1253 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
1254 if (chan->orig_mpwr)
1255 chan->max_power = min(chan->orig_mpwr,
1256 (int) MBM_TO_DBM(power_rule->max_eirp));
1258 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1261 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
1264 struct ieee80211_supported_band *sband;
1266 BUG_ON(!wiphy->bands[band]);
1267 sband = wiphy->bands[band];
1269 for (i = 0; i < sband->n_channels; i++)
1270 handle_channel(wiphy, band, i);
1273 static bool ignore_reg_update(struct wiphy *wiphy,
1274 enum nl80211_reg_initiator initiator)
1278 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1279 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1282 * wiphy->regd will be set once the device has its own
1283 * desired regulatory domain set
1285 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
1286 !is_world_regdom(last_request->alpha2))
1291 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1293 struct cfg80211_registered_device *rdev;
1295 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1296 wiphy_update_regulatory(&rdev->wiphy, initiator);
1299 static void handle_reg_beacon(struct wiphy *wiphy,
1300 unsigned int chan_idx,
1301 struct reg_beacon *reg_beacon)
1303 struct ieee80211_supported_band *sband;
1304 struct ieee80211_channel *chan;
1305 bool channel_changed = false;
1306 struct ieee80211_channel chan_before;
1308 assert_cfg80211_lock();
1310 sband = wiphy->bands[reg_beacon->chan.band];
1311 chan = &sband->channels[chan_idx];
1313 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1316 if (chan->beacon_found)
1319 chan->beacon_found = true;
1321 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
1324 chan_before.center_freq = chan->center_freq;
1325 chan_before.flags = chan->flags;
1327 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1328 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1329 channel_changed = true;
1332 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1333 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1334 channel_changed = true;
1337 if (channel_changed)
1338 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1342 * Called when a scan on a wiphy finds a beacon on
1345 static void wiphy_update_new_beacon(struct wiphy *wiphy,
1346 struct reg_beacon *reg_beacon)
1349 struct ieee80211_supported_band *sband;
1351 assert_cfg80211_lock();
1353 if (!wiphy->bands[reg_beacon->chan.band])
1356 sband = wiphy->bands[reg_beacon->chan.band];
1358 for (i = 0; i < sband->n_channels; i++)
1359 handle_reg_beacon(wiphy, i, reg_beacon);
1363 * Called upon reg changes or a new wiphy is added
1365 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1368 struct ieee80211_supported_band *sband;
1369 struct reg_beacon *reg_beacon;
1371 assert_cfg80211_lock();
1373 if (list_empty(®_beacon_list))
1376 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
1377 if (!wiphy->bands[reg_beacon->chan.band])
1379 sband = wiphy->bands[reg_beacon->chan.band];
1380 for (i = 0; i < sband->n_channels; i++)
1381 handle_reg_beacon(wiphy, i, reg_beacon);
1385 static bool reg_is_world_roaming(struct wiphy *wiphy)
1387 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1388 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1391 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1392 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1397 /* Reap the advantages of previously found beacons */
1398 static void reg_process_beacons(struct wiphy *wiphy)
1401 * Means we are just firing up cfg80211, so no beacons would
1402 * have been processed yet.
1406 if (!reg_is_world_roaming(wiphy))
1408 wiphy_update_beacon_reg(wiphy);
1411 static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1415 if (chan->flags & IEEE80211_CHAN_DISABLED)
1417 /* This would happen when regulatory rules disallow HT40 completely */
1418 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1423 static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1424 enum ieee80211_band band,
1425 unsigned int chan_idx)
1427 struct ieee80211_supported_band *sband;
1428 struct ieee80211_channel *channel;
1429 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1432 assert_cfg80211_lock();
1434 sband = wiphy->bands[band];
1435 BUG_ON(chan_idx >= sband->n_channels);
1436 channel = &sband->channels[chan_idx];
1438 if (is_ht40_not_allowed(channel)) {
1439 channel->flags |= IEEE80211_CHAN_NO_HT40;
1444 * We need to ensure the extension channels exist to
1445 * be able to use HT40- or HT40+, this finds them (or not)
1447 for (i = 0; i < sband->n_channels; i++) {
1448 struct ieee80211_channel *c = &sband->channels[i];
1449 if (c->center_freq == (channel->center_freq - 20))
1451 if (c->center_freq == (channel->center_freq + 20))
1456 * Please note that this assumes target bandwidth is 20 MHz,
1457 * if that ever changes we also need to change the below logic
1458 * to include that as well.
1460 if (is_ht40_not_allowed(channel_before))
1461 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1463 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1465 if (is_ht40_not_allowed(channel_after))
1466 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1468 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1471 static void reg_process_ht_flags_band(struct wiphy *wiphy,
1472 enum ieee80211_band band)
1475 struct ieee80211_supported_band *sband;
1477 BUG_ON(!wiphy->bands[band]);
1478 sband = wiphy->bands[band];
1480 for (i = 0; i < sband->n_channels; i++)
1481 reg_process_ht_flags_channel(wiphy, band, i);
1484 static void reg_process_ht_flags(struct wiphy *wiphy)
1486 enum ieee80211_band band;
1491 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1492 if (wiphy->bands[band])
1493 reg_process_ht_flags_band(wiphy, band);
1498 void wiphy_update_regulatory(struct wiphy *wiphy,
1499 enum nl80211_reg_initiator initiator)
1501 enum ieee80211_band band;
1503 if (ignore_reg_update(wiphy, initiator))
1505 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1506 if (wiphy->bands[band])
1507 handle_band(wiphy, band);
1510 reg_process_beacons(wiphy);
1511 reg_process_ht_flags(wiphy);
1512 if (wiphy->reg_notifier)
1513 wiphy->reg_notifier(wiphy, last_request);
1516 static void handle_channel_custom(struct wiphy *wiphy,
1517 enum ieee80211_band band,
1518 unsigned int chan_idx,
1519 const struct ieee80211_regdomain *regd)
1522 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1524 const struct ieee80211_reg_rule *reg_rule = NULL;
1525 const struct ieee80211_power_rule *power_rule = NULL;
1526 const struct ieee80211_freq_range *freq_range = NULL;
1527 struct ieee80211_supported_band *sband;
1528 struct ieee80211_channel *chan;
1532 sband = wiphy->bands[band];
1533 BUG_ON(chan_idx >= sband->n_channels);
1534 chan = &sband->channels[chan_idx];
1536 r = freq_reg_info_regd(wiphy,
1537 MHZ_TO_KHZ(chan->center_freq),
1543 chan->flags = IEEE80211_CHAN_DISABLED;
1547 power_rule = ®_rule->power_rule;
1548 freq_range = ®_rule->freq_range;
1550 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1551 bw_flags = IEEE80211_CHAN_NO_HT40;
1553 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1554 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1555 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1558 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1559 const struct ieee80211_regdomain *regd)
1562 struct ieee80211_supported_band *sband;
1564 BUG_ON(!wiphy->bands[band]);
1565 sband = wiphy->bands[band];
1567 for (i = 0; i < sband->n_channels; i++)
1568 handle_channel_custom(wiphy, band, i, regd);
1571 /* Used by drivers prior to wiphy registration */
1572 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1573 const struct ieee80211_regdomain *regd)
1575 enum ieee80211_band band;
1576 unsigned int bands_set = 0;
1578 mutex_lock(®_mutex);
1579 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1580 if (!wiphy->bands[band])
1582 handle_band_custom(wiphy, band, regd);
1585 mutex_unlock(®_mutex);
1588 * no point in calling this if it won't have any effect
1589 * on your device's supportd bands.
1591 WARN_ON(!bands_set);
1593 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1596 * Return value which can be used by ignore_request() to indicate
1597 * it has been determined we should intersect two regulatory domains
1599 #define REG_INTERSECT 1
1601 /* This has the logic which determines when a new request
1602 * should be ignored. */
1603 static int ignore_request(struct wiphy *wiphy,
1604 struct regulatory_request *pending_request)
1606 struct wiphy *last_wiphy = NULL;
1608 assert_cfg80211_lock();
1610 /* All initial requests are respected */
1614 switch (pending_request->initiator) {
1615 case NL80211_REGDOM_SET_BY_CORE:
1617 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1619 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1621 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
1623 if (last_request->initiator ==
1624 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1625 if (last_wiphy != wiphy) {
1627 * Two cards with two APs claiming different
1628 * Country IE alpha2s. We could
1629 * intersect them, but that seems unlikely
1630 * to be correct. Reject second one for now.
1632 if (regdom_changes(pending_request->alpha2))
1637 * Two consecutive Country IE hints on the same wiphy.
1638 * This should be picked up early by the driver/stack
1640 if (WARN_ON(regdom_changes(pending_request->alpha2)))
1644 return REG_INTERSECT;
1645 case NL80211_REGDOM_SET_BY_DRIVER:
1646 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1647 if (regdom_changes(pending_request->alpha2))
1653 * This would happen if you unplug and plug your card
1654 * back in or if you add a new device for which the previously
1655 * loaded card also agrees on the regulatory domain.
1657 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1658 !regdom_changes(pending_request->alpha2))
1661 return REG_INTERSECT;
1662 case NL80211_REGDOM_SET_BY_USER:
1663 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1664 return REG_INTERSECT;
1666 * If the user knows better the user should set the regdom
1667 * to their country before the IE is picked up
1669 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1670 last_request->intersect)
1673 * Process user requests only after previous user/driver/core
1674 * requests have been processed
1676 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1677 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1678 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1679 if (regdom_changes(last_request->alpha2))
1683 if (!regdom_changes(pending_request->alpha2))
1693 * __regulatory_hint - hint to the wireless core a regulatory domain
1694 * @wiphy: if the hint comes from country information from an AP, this
1695 * is required to be set to the wiphy that received the information
1696 * @pending_request: the regulatory request currently being processed
1698 * The Wireless subsystem can use this function to hint to the wireless core
1699 * what it believes should be the current regulatory domain.
1701 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1702 * already been set or other standard error codes.
1704 * Caller must hold &cfg80211_mutex and ®_mutex
1706 static int __regulatory_hint(struct wiphy *wiphy,
1707 struct regulatory_request *pending_request)
1709 bool intersect = false;
1712 assert_cfg80211_lock();
1714 r = ignore_request(wiphy, pending_request);
1716 if (r == REG_INTERSECT) {
1717 if (pending_request->initiator ==
1718 NL80211_REGDOM_SET_BY_DRIVER) {
1719 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1721 kfree(pending_request);
1728 * If the regulatory domain being requested by the
1729 * driver has already been set just copy it to the
1732 if (r == -EALREADY &&
1733 pending_request->initiator ==
1734 NL80211_REGDOM_SET_BY_DRIVER) {
1735 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1737 kfree(pending_request);
1743 kfree(pending_request);
1748 kfree(last_request);
1750 last_request = pending_request;
1751 last_request->intersect = intersect;
1753 pending_request = NULL;
1755 /* When r == REG_INTERSECT we do need to call CRDA */
1758 * Since CRDA will not be called in this case as we already
1759 * have applied the requested regulatory domain before we just
1760 * inform userspace we have processed the request
1763 nl80211_send_reg_change_event(last_request);
1767 return call_crda(last_request->alpha2);
1770 /* This processes *all* regulatory hints */
1771 static void reg_process_hint(struct regulatory_request *reg_request)
1774 struct wiphy *wiphy = NULL;
1776 BUG_ON(!reg_request->alpha2);
1778 mutex_lock(&cfg80211_mutex);
1779 mutex_lock(®_mutex);
1781 if (wiphy_idx_valid(reg_request->wiphy_idx))
1782 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1784 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1790 r = __regulatory_hint(wiphy, reg_request);
1791 /* This is required so that the orig_* parameters are saved */
1792 if (r == -EALREADY && wiphy &&
1793 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
1794 wiphy_update_regulatory(wiphy, reg_request->initiator);
1796 mutex_unlock(®_mutex);
1797 mutex_unlock(&cfg80211_mutex);
1800 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1801 static void reg_process_pending_hints(void)
1803 struct regulatory_request *reg_request;
1805 spin_lock(®_requests_lock);
1806 while (!list_empty(®_requests_list)) {
1807 reg_request = list_first_entry(®_requests_list,
1808 struct regulatory_request,
1810 list_del_init(®_request->list);
1812 spin_unlock(®_requests_lock);
1813 reg_process_hint(reg_request);
1814 spin_lock(®_requests_lock);
1816 spin_unlock(®_requests_lock);
1819 /* Processes beacon hints -- this has nothing to do with country IEs */
1820 static void reg_process_pending_beacon_hints(void)
1822 struct cfg80211_registered_device *rdev;
1823 struct reg_beacon *pending_beacon, *tmp;
1826 * No need to hold the reg_mutex here as we just touch wiphys
1827 * and do not read or access regulatory variables.
1829 mutex_lock(&cfg80211_mutex);
1831 /* This goes through the _pending_ beacon list */
1832 spin_lock_bh(®_pending_beacons_lock);
1834 if (list_empty(®_pending_beacons)) {
1835 spin_unlock_bh(®_pending_beacons_lock);
1839 list_for_each_entry_safe(pending_beacon, tmp,
1840 ®_pending_beacons, list) {
1842 list_del_init(&pending_beacon->list);
1844 /* Applies the beacon hint to current wiphys */
1845 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1846 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
1848 /* Remembers the beacon hint for new wiphys or reg changes */
1849 list_add_tail(&pending_beacon->list, ®_beacon_list);
1852 spin_unlock_bh(®_pending_beacons_lock);
1854 mutex_unlock(&cfg80211_mutex);
1857 static void reg_todo(struct work_struct *work)
1859 reg_process_pending_hints();
1860 reg_process_pending_beacon_hints();
1863 static DECLARE_WORK(reg_work, reg_todo);
1865 static void queue_regulatory_request(struct regulatory_request *request)
1867 spin_lock(®_requests_lock);
1868 list_add_tail(&request->list, ®_requests_list);
1869 spin_unlock(®_requests_lock);
1871 schedule_work(®_work);
1874 /* Core regulatory hint -- happens once during cfg80211_init() */
1875 static int regulatory_hint_core(const char *alpha2)
1877 struct regulatory_request *request;
1879 BUG_ON(last_request);
1881 request = kzalloc(sizeof(struct regulatory_request),
1886 request->alpha2[0] = alpha2[0];
1887 request->alpha2[1] = alpha2[1];
1888 request->initiator = NL80211_REGDOM_SET_BY_CORE;
1890 queue_regulatory_request(request);
1893 * This ensures last_request is populated once modules
1894 * come swinging in and calling regulatory hints and
1895 * wiphy_apply_custom_regulatory().
1897 flush_scheduled_work();
1903 int regulatory_hint_user(const char *alpha2)
1905 struct regulatory_request *request;
1909 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1913 request->wiphy_idx = WIPHY_IDX_STALE;
1914 request->alpha2[0] = alpha2[0];
1915 request->alpha2[1] = alpha2[1];
1916 request->initiator = NL80211_REGDOM_SET_BY_USER;
1918 queue_regulatory_request(request);
1924 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1926 struct regulatory_request *request;
1931 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1935 request->wiphy_idx = get_wiphy_idx(wiphy);
1937 /* Must have registered wiphy first */
1938 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1940 request->alpha2[0] = alpha2[0];
1941 request->alpha2[1] = alpha2[1];
1942 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1944 queue_regulatory_request(request);
1948 EXPORT_SYMBOL(regulatory_hint);
1950 /* Caller must hold reg_mutex */
1951 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1952 u32 country_ie_checksum)
1954 struct wiphy *request_wiphy;
1958 if (unlikely(last_request->initiator !=
1959 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1962 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1967 if (likely(request_wiphy != wiphy))
1968 return !country_ie_integrity_changes(country_ie_checksum);
1970 * We should not have let these through at this point, they
1971 * should have been picked up earlier by the first alpha2 check
1974 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1980 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1981 * therefore cannot iterate over the rdev list here.
1983 void regulatory_hint_11d(struct wiphy *wiphy,
1987 struct ieee80211_regdomain *rd = NULL;
1990 enum environment_cap env = ENVIRON_ANY;
1991 struct regulatory_request *request;
1993 mutex_lock(®_mutex);
1995 if (unlikely(!last_request))
1998 /* IE len must be evenly divisible by 2 */
1999 if (country_ie_len & 0x01)
2002 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2006 * Pending country IE processing, this can happen after we
2007 * call CRDA and wait for a response if a beacon was received before
2008 * we were able to process the last regulatory_hint_11d() call
2010 if (country_ie_regdomain)
2013 alpha2[0] = country_ie[0];
2014 alpha2[1] = country_ie[1];
2016 if (country_ie[2] == 'I')
2017 env = ENVIRON_INDOOR;
2018 else if (country_ie[2] == 'O')
2019 env = ENVIRON_OUTDOOR;
2022 * We will run this only upon a successful connection on cfg80211.
2023 * We leave conflict resolution to the workqueue, where can hold
2026 if (likely(last_request->initiator ==
2027 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2028 wiphy_idx_valid(last_request->wiphy_idx)))
2031 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
2033 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
2038 * This will not happen right now but we leave it here for the
2039 * the future when we want to add suspend/resume support and having
2040 * the user move to another country after doing so, or having the user
2041 * move to another AP. Right now we just trust the first AP.
2043 * If we hit this before we add this support we want to be informed of
2044 * it as it would indicate a mistake in the current design
2046 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
2049 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2054 * We keep this around for when CRDA comes back with a response so
2055 * we can intersect with that
2057 country_ie_regdomain = rd;
2059 request->wiphy_idx = get_wiphy_idx(wiphy);
2060 request->alpha2[0] = rd->alpha2[0];
2061 request->alpha2[1] = rd->alpha2[1];
2062 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
2063 request->country_ie_checksum = checksum;
2064 request->country_ie_env = env;
2066 mutex_unlock(®_mutex);
2068 queue_regulatory_request(request);
2075 mutex_unlock(®_mutex);
2078 static bool freq_is_chan_12_13_14(u16 freq)
2080 if (freq == ieee80211_channel_to_frequency(12) ||
2081 freq == ieee80211_channel_to_frequency(13) ||
2082 freq == ieee80211_channel_to_frequency(14))
2087 int regulatory_hint_found_beacon(struct wiphy *wiphy,
2088 struct ieee80211_channel *beacon_chan,
2091 struct reg_beacon *reg_beacon;
2093 if (likely((beacon_chan->beacon_found ||
2094 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
2095 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
2096 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
2099 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2103 REG_DBG_PRINT("cfg80211: Found new beacon on "
2104 "frequency: %d MHz (Ch %d) on %s\n",
2105 beacon_chan->center_freq,
2106 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2109 memcpy(®_beacon->chan, beacon_chan,
2110 sizeof(struct ieee80211_channel));
2114 * Since we can be called from BH or and non-BH context
2115 * we must use spin_lock_bh()
2117 spin_lock_bh(®_pending_beacons_lock);
2118 list_add_tail(®_beacon->list, ®_pending_beacons);
2119 spin_unlock_bh(®_pending_beacons_lock);
2121 schedule_work(®_work);
2126 static void print_rd_rules(const struct ieee80211_regdomain *rd)
2129 const struct ieee80211_reg_rule *reg_rule = NULL;
2130 const struct ieee80211_freq_range *freq_range = NULL;
2131 const struct ieee80211_power_rule *power_rule = NULL;
2133 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
2134 "(max_antenna_gain, max_eirp)\n");
2136 for (i = 0; i < rd->n_reg_rules; i++) {
2137 reg_rule = &rd->reg_rules[i];
2138 freq_range = ®_rule->freq_range;
2139 power_rule = ®_rule->power_rule;
2142 * There may not be documentation for max antenna gain
2143 * in certain regions
2145 if (power_rule->max_antenna_gain)
2146 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
2147 "(%d mBi, %d mBm)\n",
2148 freq_range->start_freq_khz,
2149 freq_range->end_freq_khz,
2150 freq_range->max_bandwidth_khz,
2151 power_rule->max_antenna_gain,
2152 power_rule->max_eirp);
2154 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
2156 freq_range->start_freq_khz,
2157 freq_range->end_freq_khz,
2158 freq_range->max_bandwidth_khz,
2159 power_rule->max_eirp);
2163 static void print_regdomain(const struct ieee80211_regdomain *rd)
2166 if (is_intersected_alpha2(rd->alpha2)) {
2168 if (last_request->initiator ==
2169 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2170 struct cfg80211_registered_device *rdev;
2171 rdev = cfg80211_rdev_by_wiphy_idx(
2172 last_request->wiphy_idx);
2174 printk(KERN_INFO "cfg80211: Current regulatory "
2175 "domain updated by AP to: %c%c\n",
2176 rdev->country_ie_alpha2[0],
2177 rdev->country_ie_alpha2[1]);
2179 printk(KERN_INFO "cfg80211: Current regulatory "
2180 "domain intersected: \n");
2182 printk(KERN_INFO "cfg80211: Current regulatory "
2183 "domain intersected: \n");
2184 } else if (is_world_regdom(rd->alpha2))
2185 printk(KERN_INFO "cfg80211: World regulatory "
2186 "domain updated:\n");
2188 if (is_unknown_alpha2(rd->alpha2))
2189 printk(KERN_INFO "cfg80211: Regulatory domain "
2190 "changed to driver built-in settings "
2191 "(unknown country)\n");
2193 printk(KERN_INFO "cfg80211: Regulatory domain "
2194 "changed to country: %c%c\n",
2195 rd->alpha2[0], rd->alpha2[1]);
2200 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
2202 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2203 rd->alpha2[0], rd->alpha2[1]);
2207 #ifdef CONFIG_CFG80211_REG_DEBUG
2208 static void reg_country_ie_process_debug(
2209 const struct ieee80211_regdomain *rd,
2210 const struct ieee80211_regdomain *country_ie_regdomain,
2211 const struct ieee80211_regdomain *intersected_rd)
2213 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2214 print_regdomain_info(country_ie_regdomain);
2215 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2216 print_regdomain_info(rd);
2217 if (intersected_rd) {
2218 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2220 print_regdomain_info(intersected_rd);
2223 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2226 static inline void reg_country_ie_process_debug(
2227 const struct ieee80211_regdomain *rd,
2228 const struct ieee80211_regdomain *country_ie_regdomain,
2229 const struct ieee80211_regdomain *intersected_rd)
2234 /* Takes ownership of rd only if it doesn't fail */
2235 static int __set_regdom(const struct ieee80211_regdomain *rd)
2237 const struct ieee80211_regdomain *intersected_rd = NULL;
2238 struct cfg80211_registered_device *rdev = NULL;
2239 struct wiphy *request_wiphy;
2240 /* Some basic sanity checks first */
2242 if (is_world_regdom(rd->alpha2)) {
2243 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2245 update_world_regdomain(rd);
2249 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2250 !is_unknown_alpha2(rd->alpha2))
2257 * Lets only bother proceeding on the same alpha2 if the current
2258 * rd is non static (it means CRDA was present and was used last)
2259 * and the pending request came in from a country IE
2261 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2263 * If someone else asked us to change the rd lets only bother
2264 * checking if the alpha2 changes if CRDA was already called
2266 if (!regdom_changes(rd->alpha2))
2271 * Now lets set the regulatory domain, update all driver channels
2272 * and finally inform them of what we have done, in case they want
2273 * to review or adjust their own settings based on their own
2274 * internal EEPROM data
2277 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2280 if (!is_valid_rd(rd)) {
2281 printk(KERN_ERR "cfg80211: Invalid "
2282 "regulatory domain detected:\n");
2283 print_regdomain_info(rd);
2287 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2289 if (!last_request->intersect) {
2292 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
2294 cfg80211_regdomain = rd;
2299 * For a driver hint, lets copy the regulatory domain the
2300 * driver wanted to the wiphy to deal with conflicts
2304 * Userspace could have sent two replies with only
2305 * one kernel request.
2307 if (request_wiphy->regd)
2310 r = reg_copy_regd(&request_wiphy->regd, rd);
2315 cfg80211_regdomain = rd;
2319 /* Intersection requires a bit more work */
2321 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2323 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2324 if (!intersected_rd)
2328 * We can trash what CRDA provided now.
2329 * However if a driver requested this specific regulatory
2330 * domain we keep it for its private use
2332 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2333 request_wiphy->regd = rd;
2340 cfg80211_regdomain = intersected_rd;
2346 * Country IE requests are handled a bit differently, we intersect
2347 * the country IE rd with what CRDA believes that country should have
2351 * Userspace could have sent two replies with only
2352 * one kernel request. By the second reply we would have
2353 * already processed and consumed the country_ie_regdomain.
2355 if (!country_ie_regdomain)
2357 BUG_ON(rd == country_ie_regdomain);
2360 * Intersect what CRDA returned and our what we
2361 * had built from the Country IE received
2364 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2366 reg_country_ie_process_debug(rd,
2367 country_ie_regdomain,
2370 kfree(country_ie_regdomain);
2371 country_ie_regdomain = NULL;
2373 if (!intersected_rd)
2376 rdev = wiphy_to_dev(request_wiphy);
2378 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2379 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2380 rdev->env = last_request->country_ie_env;
2382 BUG_ON(intersected_rd == rd);
2388 cfg80211_regdomain = intersected_rd;
2395 * Use this call to set the current regulatory domain. Conflicts with
2396 * multiple drivers can be ironed out later. Caller must've already
2397 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2399 int set_regdom(const struct ieee80211_regdomain *rd)
2403 assert_cfg80211_lock();
2405 mutex_lock(®_mutex);
2407 /* Note that this doesn't update the wiphys, this is done below */
2408 r = __set_regdom(rd);
2411 mutex_unlock(®_mutex);
2415 /* This would make this whole thing pointless */
2416 if (!last_request->intersect)
2417 BUG_ON(rd != cfg80211_regdomain);
2419 /* update all wiphys now with the new established regulatory domain */
2420 update_all_wiphy_regulatory(last_request->initiator);
2422 print_regdomain(cfg80211_regdomain);
2424 nl80211_send_reg_change_event(last_request);
2426 mutex_unlock(®_mutex);
2431 /* Caller must hold cfg80211_mutex */
2432 void reg_device_remove(struct wiphy *wiphy)
2434 struct wiphy *request_wiphy = NULL;
2436 assert_cfg80211_lock();
2438 mutex_lock(®_mutex);
2443 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2445 if (!request_wiphy || request_wiphy != wiphy)
2448 last_request->wiphy_idx = WIPHY_IDX_STALE;
2449 last_request->country_ie_env = ENVIRON_ANY;
2451 mutex_unlock(®_mutex);
2454 int regulatory_init(void)
2458 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2459 if (IS_ERR(reg_pdev))
2460 return PTR_ERR(reg_pdev);
2462 spin_lock_init(®_requests_lock);
2463 spin_lock_init(®_pending_beacons_lock);
2465 cfg80211_regdomain = cfg80211_world_regdom;
2467 /* We always try to get an update for the static regdomain */
2468 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
2473 * N.B. kobject_uevent_env() can fail mainly for when we're out
2474 * memory which is handled and propagated appropriately above
2475 * but it can also fail during a netlink_broadcast() or during
2476 * early boot for call_usermodehelper(). For now treat these
2477 * errors as non-fatal.
2479 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2480 "to call CRDA during init");
2481 #ifdef CONFIG_CFG80211_REG_DEBUG
2482 /* We want to find out exactly why when debugging */
2488 * Finally, if the user set the module parameter treat it
2491 if (!is_world_regdom(ieee80211_regdom))
2492 regulatory_hint_user(ieee80211_regdom);
2497 void regulatory_exit(void)
2499 struct regulatory_request *reg_request, *tmp;
2500 struct reg_beacon *reg_beacon, *btmp;
2502 cancel_work_sync(®_work);
2504 mutex_lock(&cfg80211_mutex);
2505 mutex_lock(®_mutex);
2509 kfree(country_ie_regdomain);
2510 country_ie_regdomain = NULL;
2512 kfree(last_request);
2514 platform_device_unregister(reg_pdev);
2516 spin_lock_bh(®_pending_beacons_lock);
2517 if (!list_empty(®_pending_beacons)) {
2518 list_for_each_entry_safe(reg_beacon, btmp,
2519 ®_pending_beacons, list) {
2520 list_del(®_beacon->list);
2524 spin_unlock_bh(®_pending_beacons_lock);
2526 if (!list_empty(®_beacon_list)) {
2527 list_for_each_entry_safe(reg_beacon, btmp,
2528 ®_beacon_list, list) {
2529 list_del(®_beacon->list);
2534 spin_lock(®_requests_lock);
2535 if (!list_empty(®_requests_list)) {
2536 list_for_each_entry_safe(reg_request, tmp,
2537 ®_requests_list, list) {
2538 list_del(®_request->list);
2542 spin_unlock(®_requests_lock);
2544 mutex_unlock(®_mutex);
2545 mutex_unlock(&cfg80211_mutex);