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 /* Receipt of information from last regulatory request */
47 static struct regulatory_request *last_request;
49 /* To trigger userspace events */
50 static struct platform_device *reg_pdev;
53 * Central wireless core regulatory domains, we only need two,
54 * the current one and a world regulatory domain in case we have no
55 * information to give us an alpha2
57 const struct ieee80211_regdomain *cfg80211_regdomain;
60 * We use this as a place for the rd structure built from the
61 * last parsed country IE to rest until CRDA gets back to us with
62 * what it thinks should apply for the same country
64 static const struct ieee80211_regdomain *country_ie_regdomain;
67 * Protects static reg.c components:
68 * - cfg80211_world_regdom
70 * - country_ie_regdomain
73 DEFINE_MUTEX(reg_mutex);
74 #define assert_reg_lock() WARN_ON(!mutex_is_locked(®_mutex))
76 /* Used to queue up regulatory hints */
77 static LIST_HEAD(reg_requests_list);
78 static spinlock_t reg_requests_lock;
80 /* Used to queue up beacon hints for review */
81 static LIST_HEAD(reg_pending_beacons);
82 static spinlock_t reg_pending_beacons_lock;
84 /* Used to keep track of processed beacon hints */
85 static LIST_HEAD(reg_beacon_list);
88 struct list_head list;
89 struct ieee80211_channel chan;
92 /* We keep a static world regulatory domain in case of the absence of CRDA */
93 static const struct ieee80211_regdomain world_regdom = {
97 /* IEEE 802.11b/g, channels 1..11 */
98 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
99 /* IEEE 802.11b/g, channels 12..13. No HT40
100 * channel fits here. */
101 REG_RULE(2467-10, 2472+10, 20, 6, 20,
102 NL80211_RRF_PASSIVE_SCAN |
103 NL80211_RRF_NO_IBSS),
104 /* IEEE 802.11 channel 14 - Only JP enables
105 * this and for 802.11b only */
106 REG_RULE(2484-10, 2484+10, 20, 6, 20,
107 NL80211_RRF_PASSIVE_SCAN |
108 NL80211_RRF_NO_IBSS |
109 NL80211_RRF_NO_OFDM),
110 /* IEEE 802.11a, channel 36..48 */
111 REG_RULE(5180-10, 5240+10, 40, 6, 20,
112 NL80211_RRF_PASSIVE_SCAN |
113 NL80211_RRF_NO_IBSS),
115 /* NB: 5260 MHz - 5700 MHz requies DFS */
117 /* IEEE 802.11a, channel 149..165 */
118 REG_RULE(5745-10, 5825+10, 40, 6, 20,
119 NL80211_RRF_PASSIVE_SCAN |
120 NL80211_RRF_NO_IBSS),
124 static const struct ieee80211_regdomain *cfg80211_world_regdom =
127 static char *ieee80211_regdom = "00";
129 module_param(ieee80211_regdom, charp, 0444);
130 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
132 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
134 * We assume 40 MHz bandwidth for the old regulatory work.
135 * We make emphasis we are using the exact same frequencies
139 static const struct ieee80211_regdomain us_regdom = {
143 /* IEEE 802.11b/g, channels 1..11 */
144 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
145 /* IEEE 802.11a, channel 36..48 */
146 REG_RULE(5180-10, 5240+10, 40, 6, 17, 0),
147 /* IEEE 802.11a, channels 48..64 */
148 REG_RULE(5260-10, 5320+10, 40, 6, 20, NL80211_RRF_DFS),
149 /* IEEE 802.11a, channels 100..124 */
150 REG_RULE(5500-10, 5590+10, 40, 6, 20, NL80211_RRF_DFS),
151 /* IEEE 802.11a, channels 132..144 */
152 REG_RULE(5660-10, 5700+10, 40, 6, 20, NL80211_RRF_DFS),
153 /* IEEE 802.11a, channels 149..165, outdoor */
154 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
158 static const struct ieee80211_regdomain jp_regdom = {
162 /* IEEE 802.11b/g, channels 1..11 */
163 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
164 /* IEEE 802.11b/g, channels 12..13 */
165 REG_RULE(2467-10, 2472+10, 20, 6, 20, 0),
166 /* IEEE 802.11b/g, channel 14 */
167 REG_RULE(2484-10, 2484+10, 20, 6, 20, NL80211_RRF_NO_OFDM),
168 /* IEEE 802.11a, channels 36..48 */
169 REG_RULE(5180-10, 5240+10, 40, 6, 20, 0),
170 /* IEEE 802.11a, channels 52..64 */
171 REG_RULE(5260-10, 5320+10, 40, 6, 20, NL80211_RRF_DFS),
172 /* IEEE 802.11a, channels 100..144 */
173 REG_RULE(5500-10, 5700+10, 40, 6, 23, NL80211_RRF_DFS),
177 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
179 if (alpha2[0] == 'U' && alpha2[1] == 'S')
181 if (alpha2[0] == 'J' && alpha2[1] == 'P')
183 /* Use world roaming rules for "EU", since it was a pseudo
185 if (alpha2[0] == 'E' && alpha2[1] == 'U')
186 return &world_regdom;
187 /* Default, world roaming rules */
188 return &world_regdom;
191 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
193 if (rd == &us_regdom || rd == &jp_regdom || rd == &world_regdom)
198 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
204 static void reset_regdomains(void)
206 /* avoid freeing static information or freeing something twice */
207 if (cfg80211_regdomain == cfg80211_world_regdom)
208 cfg80211_regdomain = NULL;
209 if (cfg80211_world_regdom == &world_regdom)
210 cfg80211_world_regdom = NULL;
211 if (cfg80211_regdomain == &world_regdom)
212 cfg80211_regdomain = NULL;
213 if (is_old_static_regdom(cfg80211_regdomain))
214 cfg80211_regdomain = NULL;
216 kfree(cfg80211_regdomain);
217 kfree(cfg80211_world_regdom);
219 cfg80211_world_regdom = &world_regdom;
220 cfg80211_regdomain = NULL;
224 * Dynamic world regulatory domain requested by the wireless
225 * core upon initialization
227 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
229 BUG_ON(!last_request);
233 cfg80211_world_regdom = rd;
234 cfg80211_regdomain = rd;
237 bool is_world_regdom(const char *alpha2)
241 if (alpha2[0] == '0' && alpha2[1] == '0')
246 static bool is_alpha2_set(const char *alpha2)
250 if (alpha2[0] != 0 && alpha2[1] != 0)
255 static bool is_alpha_upper(char letter)
258 if (letter >= 65 && letter <= 90)
263 static bool is_unknown_alpha2(const char *alpha2)
268 * Special case where regulatory domain was built by driver
269 * but a specific alpha2 cannot be determined
271 if (alpha2[0] == '9' && alpha2[1] == '9')
276 static bool is_intersected_alpha2(const char *alpha2)
281 * Special case where regulatory domain is the
282 * result of an intersection between two regulatory domain
285 if (alpha2[0] == '9' && alpha2[1] == '8')
290 static bool is_an_alpha2(const char *alpha2)
294 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
299 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
301 if (!alpha2_x || !alpha2_y)
303 if (alpha2_x[0] == alpha2_y[0] &&
304 alpha2_x[1] == alpha2_y[1])
309 static bool regdom_changes(const char *alpha2)
311 assert_cfg80211_lock();
313 if (!cfg80211_regdomain)
315 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
321 * country_ie_integrity_changes - tells us if the country IE has changed
322 * @checksum: checksum of country IE of fields we are interested in
324 * If the country IE has not changed you can ignore it safely. This is
325 * useful to determine if two devices are seeing two different country IEs
326 * even on the same alpha2. Note that this will return false if no IE has
327 * been set on the wireless core yet.
329 static bool country_ie_integrity_changes(u32 checksum)
331 /* If no IE has been set then the checksum doesn't change */
332 if (unlikely(!last_request->country_ie_checksum))
334 if (unlikely(last_request->country_ie_checksum != checksum))
339 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
340 const struct ieee80211_regdomain *src_regd)
342 struct ieee80211_regdomain *regd;
343 int size_of_regd = 0;
346 size_of_regd = sizeof(struct ieee80211_regdomain) +
347 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
349 regd = kzalloc(size_of_regd, GFP_KERNEL);
353 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
355 for (i = 0; i < src_regd->n_reg_rules; i++)
356 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i],
357 sizeof(struct ieee80211_reg_rule));
363 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
364 struct reg_regdb_search_request {
366 struct list_head list;
369 static LIST_HEAD(reg_regdb_search_list);
370 static DEFINE_SPINLOCK(reg_regdb_search_lock);
372 static void reg_regdb_search(struct work_struct *work)
374 struct reg_regdb_search_request *request;
375 const struct ieee80211_regdomain *curdom, *regdom;
378 spin_lock(®_regdb_search_lock);
379 while (!list_empty(®_regdb_search_list)) {
380 request = list_first_entry(®_regdb_search_list,
381 struct reg_regdb_search_request,
383 list_del(&request->list);
385 for (i=0; i<reg_regdb_size; i++) {
386 curdom = reg_regdb[i];
388 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
389 r = reg_copy_regd(®dom, curdom);
392 spin_unlock(®_regdb_search_lock);
393 mutex_lock(&cfg80211_mutex);
395 mutex_unlock(&cfg80211_mutex);
396 spin_lock(®_regdb_search_lock);
403 spin_unlock(®_regdb_search_lock);
406 static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
408 static void reg_regdb_query(const char *alpha2)
410 struct reg_regdb_search_request *request;
415 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
419 memcpy(request->alpha2, alpha2, 2);
421 spin_lock(®_regdb_search_lock);
422 list_add_tail(&request->list, ®_regdb_search_list);
423 spin_unlock(®_regdb_search_lock);
425 schedule_work(®_regdb_work);
428 static inline void reg_regdb_query(const char *alpha2) {}
429 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
432 * This lets us keep regulatory code which is updated on a regulatory
433 * basis in userspace.
435 static int call_crda(const char *alpha2)
437 char country_env[9 + 2] = "COUNTRY=";
443 if (!is_world_regdom((char *) alpha2))
444 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
445 alpha2[0], alpha2[1]);
447 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
448 "regulatory domain\n");
450 /* query internal regulatory database (if it exists) */
451 reg_regdb_query(alpha2);
453 country_env[8] = alpha2[0];
454 country_env[9] = alpha2[1];
456 return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp);
459 /* Used by nl80211 before kmalloc'ing our regulatory domain */
460 bool reg_is_valid_request(const char *alpha2)
462 assert_cfg80211_lock();
467 return alpha2_equal(last_request->alpha2, alpha2);
470 /* Sanity check on a regulatory rule */
471 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
473 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
476 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
479 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
482 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
484 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
485 freq_range->max_bandwidth_khz > freq_diff)
491 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
493 const struct ieee80211_reg_rule *reg_rule = NULL;
496 if (!rd->n_reg_rules)
499 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
502 for (i = 0; i < rd->n_reg_rules; i++) {
503 reg_rule = &rd->reg_rules[i];
504 if (!is_valid_reg_rule(reg_rule))
511 static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
515 u32 start_freq_khz, end_freq_khz;
517 start_freq_khz = center_freq_khz - (bw_khz/2);
518 end_freq_khz = center_freq_khz + (bw_khz/2);
520 if (start_freq_khz >= freq_range->start_freq_khz &&
521 end_freq_khz <= freq_range->end_freq_khz)
528 * freq_in_rule_band - tells us if a frequency is in a frequency band
529 * @freq_range: frequency rule we want to query
530 * @freq_khz: frequency we are inquiring about
532 * This lets us know if a specific frequency rule is or is not relevant to
533 * a specific frequency's band. Bands are device specific and artificial
534 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
535 * safe for now to assume that a frequency rule should not be part of a
536 * frequency's band if the start freq or end freq are off by more than 2 GHz.
537 * This resolution can be lowered and should be considered as we add
538 * regulatory rule support for other "bands".
540 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
543 #define ONE_GHZ_IN_KHZ 1000000
544 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
546 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
549 #undef ONE_GHZ_IN_KHZ
553 * Converts a country IE to a regulatory domain. A regulatory domain
554 * structure has a lot of information which the IE doesn't yet have,
555 * so for the other values we use upper max values as we will intersect
556 * with our userspace regulatory agent to get lower bounds.
558 static struct ieee80211_regdomain *country_ie_2_rd(
563 struct ieee80211_regdomain *rd = NULL;
567 u32 num_rules = 0, size_of_regd = 0;
568 u8 *triplets_start = NULL;
569 u8 len_at_triplet = 0;
570 /* the last channel we have registered in a subband (triplet) */
571 int last_sub_max_channel = 0;
573 *checksum = 0xDEADBEEF;
575 /* Country IE requirements */
576 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
577 country_ie_len & 0x01);
579 alpha2[0] = country_ie[0];
580 alpha2[1] = country_ie[1];
583 * Third octet can be:
587 * anything else we assume is no restrictions
589 if (country_ie[2] == 'I')
590 flags = NL80211_RRF_NO_OUTDOOR;
591 else if (country_ie[2] == 'O')
592 flags = NL80211_RRF_NO_INDOOR;
597 triplets_start = country_ie;
598 len_at_triplet = country_ie_len;
600 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
603 * We need to build a reg rule for each triplet, but first we must
604 * calculate the number of reg rules we will need. We will need one
605 * for each channel subband
607 while (country_ie_len >= 3) {
609 struct ieee80211_country_ie_triplet *triplet =
610 (struct ieee80211_country_ie_triplet *) country_ie;
611 int cur_sub_max_channel = 0, cur_channel = 0;
613 if (triplet->ext.reg_extension_id >=
614 IEEE80211_COUNTRY_EXTENSION_ID) {
621 if (triplet->chans.first_channel <= 14)
622 end_channel = triplet->chans.first_channel +
623 triplet->chans.num_channels;
626 * 5 GHz -- For example in country IEs if the first
627 * channel given is 36 and the number of channels is 4
628 * then the individual channel numbers defined for the
629 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
630 * and not 36, 37, 38, 39.
632 * See: http://tinyurl.com/11d-clarification
634 end_channel = triplet->chans.first_channel +
635 (4 * (triplet->chans.num_channels - 1));
637 cur_channel = triplet->chans.first_channel;
638 cur_sub_max_channel = end_channel;
640 /* Basic sanity check */
641 if (cur_sub_max_channel < cur_channel)
645 * Do not allow overlapping channels. Also channels
646 * passed in each subband must be monotonically
649 if (last_sub_max_channel) {
650 if (cur_channel <= last_sub_max_channel)
652 if (cur_sub_max_channel <= last_sub_max_channel)
657 * When dot11RegulatoryClassesRequired is supported
658 * we can throw ext triplets as part of this soup,
659 * for now we don't care when those change as we
662 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
663 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
664 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
666 last_sub_max_channel = cur_sub_max_channel;
673 * Note: this is not a IEEE requirement but
674 * simply a memory requirement
676 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
680 country_ie = triplets_start;
681 country_ie_len = len_at_triplet;
683 size_of_regd = sizeof(struct ieee80211_regdomain) +
684 (num_rules * sizeof(struct ieee80211_reg_rule));
686 rd = kzalloc(size_of_regd, GFP_KERNEL);
690 rd->n_reg_rules = num_rules;
691 rd->alpha2[0] = alpha2[0];
692 rd->alpha2[1] = alpha2[1];
694 /* This time around we fill in the rd */
695 while (country_ie_len >= 3) {
697 struct ieee80211_country_ie_triplet *triplet =
698 (struct ieee80211_country_ie_triplet *) country_ie;
699 struct ieee80211_reg_rule *reg_rule = NULL;
700 struct ieee80211_freq_range *freq_range = NULL;
701 struct ieee80211_power_rule *power_rule = NULL;
704 * Must parse if dot11RegulatoryClassesRequired is true,
705 * we don't support this yet
707 if (triplet->ext.reg_extension_id >=
708 IEEE80211_COUNTRY_EXTENSION_ID) {
714 reg_rule = &rd->reg_rules[i];
715 freq_range = ®_rule->freq_range;
716 power_rule = ®_rule->power_rule;
718 reg_rule->flags = flags;
721 if (triplet->chans.first_channel <= 14)
722 end_channel = triplet->chans.first_channel +
723 triplet->chans.num_channels;
725 end_channel = triplet->chans.first_channel +
726 (4 * (triplet->chans.num_channels - 1));
729 * The +10 is since the regulatory domain expects
730 * the actual band edge, not the center of freq for
731 * its start and end freqs, assuming 20 MHz bandwidth on
732 * the channels passed
734 freq_range->start_freq_khz =
735 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
736 triplet->chans.first_channel) - 10);
737 freq_range->end_freq_khz =
738 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
742 * These are large arbitrary values we use to intersect later.
743 * Increment this if we ever support >= 40 MHz channels
746 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
747 power_rule->max_antenna_gain = DBI_TO_MBI(100);
748 power_rule->max_eirp = DBM_TO_MBM(100);
754 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
762 * Helper for regdom_intersect(), this does the real
763 * mathematical intersection fun
765 static int reg_rules_intersect(
766 const struct ieee80211_reg_rule *rule1,
767 const struct ieee80211_reg_rule *rule2,
768 struct ieee80211_reg_rule *intersected_rule)
770 const struct ieee80211_freq_range *freq_range1, *freq_range2;
771 struct ieee80211_freq_range *freq_range;
772 const struct ieee80211_power_rule *power_rule1, *power_rule2;
773 struct ieee80211_power_rule *power_rule;
776 freq_range1 = &rule1->freq_range;
777 freq_range2 = &rule2->freq_range;
778 freq_range = &intersected_rule->freq_range;
780 power_rule1 = &rule1->power_rule;
781 power_rule2 = &rule2->power_rule;
782 power_rule = &intersected_rule->power_rule;
784 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
785 freq_range2->start_freq_khz);
786 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
787 freq_range2->end_freq_khz);
788 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
789 freq_range2->max_bandwidth_khz);
791 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
792 if (freq_range->max_bandwidth_khz > freq_diff)
793 freq_range->max_bandwidth_khz = freq_diff;
795 power_rule->max_eirp = min(power_rule1->max_eirp,
796 power_rule2->max_eirp);
797 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
798 power_rule2->max_antenna_gain);
800 intersected_rule->flags = (rule1->flags | rule2->flags);
802 if (!is_valid_reg_rule(intersected_rule))
809 * regdom_intersect - do the intersection between two regulatory domains
810 * @rd1: first regulatory domain
811 * @rd2: second regulatory domain
813 * Use this function to get the intersection between two regulatory domains.
814 * Once completed we will mark the alpha2 for the rd as intersected, "98",
815 * as no one single alpha2 can represent this regulatory domain.
817 * Returns a pointer to the regulatory domain structure which will hold the
818 * resulting intersection of rules between rd1 and rd2. We will
819 * kzalloc() this structure for you.
821 static struct ieee80211_regdomain *regdom_intersect(
822 const struct ieee80211_regdomain *rd1,
823 const struct ieee80211_regdomain *rd2)
827 unsigned int num_rules = 0, rule_idx = 0;
828 const struct ieee80211_reg_rule *rule1, *rule2;
829 struct ieee80211_reg_rule *intersected_rule;
830 struct ieee80211_regdomain *rd;
831 /* This is just a dummy holder to help us count */
832 struct ieee80211_reg_rule irule;
834 /* Uses the stack temporarily for counter arithmetic */
835 intersected_rule = &irule;
837 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
843 * First we get a count of the rules we'll need, then we actually
844 * build them. This is to so we can malloc() and free() a
845 * regdomain once. The reason we use reg_rules_intersect() here
846 * is it will return -EINVAL if the rule computed makes no sense.
847 * All rules that do check out OK are valid.
850 for (x = 0; x < rd1->n_reg_rules; x++) {
851 rule1 = &rd1->reg_rules[x];
852 for (y = 0; y < rd2->n_reg_rules; y++) {
853 rule2 = &rd2->reg_rules[y];
854 if (!reg_rules_intersect(rule1, rule2,
857 memset(intersected_rule, 0,
858 sizeof(struct ieee80211_reg_rule));
865 size_of_regd = sizeof(struct ieee80211_regdomain) +
866 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
868 rd = kzalloc(size_of_regd, GFP_KERNEL);
872 for (x = 0; x < rd1->n_reg_rules; x++) {
873 rule1 = &rd1->reg_rules[x];
874 for (y = 0; y < rd2->n_reg_rules; y++) {
875 rule2 = &rd2->reg_rules[y];
877 * This time around instead of using the stack lets
878 * write to the target rule directly saving ourselves
881 intersected_rule = &rd->reg_rules[rule_idx];
882 r = reg_rules_intersect(rule1, rule2,
885 * No need to memset here the intersected rule here as
886 * we're not using the stack anymore
894 if (rule_idx != num_rules) {
899 rd->n_reg_rules = num_rules;
907 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
908 * want to just have the channel structure use these
910 static u32 map_regdom_flags(u32 rd_flags)
912 u32 channel_flags = 0;
913 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
914 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
915 if (rd_flags & NL80211_RRF_NO_IBSS)
916 channel_flags |= IEEE80211_CHAN_NO_IBSS;
917 if (rd_flags & NL80211_RRF_DFS)
918 channel_flags |= IEEE80211_CHAN_RADAR;
919 return channel_flags;
922 static int freq_reg_info_regd(struct wiphy *wiphy,
925 const struct ieee80211_reg_rule **reg_rule,
926 const struct ieee80211_regdomain *custom_regd)
929 bool band_rule_found = false;
930 const struct ieee80211_regdomain *regd;
931 bool bw_fits = false;
934 desired_bw_khz = MHZ_TO_KHZ(20);
936 regd = custom_regd ? custom_regd : cfg80211_regdomain;
939 * Follow the driver's regulatory domain, if present, unless a country
940 * IE has been processed or a user wants to help complaince further
942 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
943 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
950 for (i = 0; i < regd->n_reg_rules; i++) {
951 const struct ieee80211_reg_rule *rr;
952 const struct ieee80211_freq_range *fr = NULL;
953 const struct ieee80211_power_rule *pr = NULL;
955 rr = ®d->reg_rules[i];
956 fr = &rr->freq_range;
957 pr = &rr->power_rule;
960 * We only need to know if one frequency rule was
961 * was in center_freq's band, that's enough, so lets
962 * not overwrite it once found
964 if (!band_rule_found)
965 band_rule_found = freq_in_rule_band(fr, center_freq);
967 bw_fits = reg_does_bw_fit(fr,
971 if (band_rule_found && bw_fits) {
977 if (!band_rule_found)
982 EXPORT_SYMBOL(freq_reg_info);
984 int freq_reg_info(struct wiphy *wiphy,
987 const struct ieee80211_reg_rule **reg_rule)
989 assert_cfg80211_lock();
990 return freq_reg_info_regd(wiphy,
998 * Note that right now we assume the desired channel bandwidth
999 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1000 * per channel, the primary and the extension channel). To support
1001 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1002 * new ieee80211_channel.target_bw and re run the regulatory check
1003 * on the wiphy with the target_bw specified. Then we can simply use
1004 * that below for the desired_bw_khz below.
1006 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1007 unsigned int chan_idx)
1010 u32 flags, bw_flags = 0;
1011 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1012 const struct ieee80211_reg_rule *reg_rule = NULL;
1013 const struct ieee80211_power_rule *power_rule = NULL;
1014 const struct ieee80211_freq_range *freq_range = NULL;
1015 struct ieee80211_supported_band *sband;
1016 struct ieee80211_channel *chan;
1017 struct wiphy *request_wiphy = NULL;
1019 assert_cfg80211_lock();
1021 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1023 sband = wiphy->bands[band];
1024 BUG_ON(chan_idx >= sband->n_channels);
1025 chan = &sband->channels[chan_idx];
1027 flags = chan->orig_flags;
1029 r = freq_reg_info(wiphy,
1030 MHZ_TO_KHZ(chan->center_freq),
1036 * This means no regulatory rule was found in the country IE
1037 * with a frequency range on the center_freq's band, since
1038 * IEEE-802.11 allows for a country IE to have a subset of the
1039 * regulatory information provided in a country we ignore
1040 * disabling the channel unless at least one reg rule was
1041 * found on the center_freq's band. For details see this
1044 * http://tinyurl.com/11d-clarification
1047 last_request->initiator ==
1048 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1049 #ifdef CONFIG_CFG80211_REG_DEBUG
1050 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
1051 "intact on %s - no rule found in band on "
1053 chan->center_freq, wiphy_name(wiphy));
1057 * In this case we know the country IE has at least one reg rule
1058 * for the band so we respect its band definitions
1060 #ifdef CONFIG_CFG80211_REG_DEBUG
1061 if (last_request->initiator ==
1062 NL80211_REGDOM_SET_BY_COUNTRY_IE)
1063 printk(KERN_DEBUG "cfg80211: Disabling "
1064 "channel %d MHz on %s due to "
1066 chan->center_freq, wiphy_name(wiphy));
1068 flags |= IEEE80211_CHAN_DISABLED;
1069 chan->flags = flags;
1074 power_rule = ®_rule->power_rule;
1075 freq_range = ®_rule->freq_range;
1077 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1078 bw_flags = IEEE80211_CHAN_NO_HT40;
1080 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1081 request_wiphy && request_wiphy == wiphy &&
1082 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
1084 * This gaurantees the driver's requested regulatory domain
1085 * will always be used as a base for further regulatory
1088 chan->flags = chan->orig_flags =
1089 map_regdom_flags(reg_rule->flags) | bw_flags;
1090 chan->max_antenna_gain = chan->orig_mag =
1091 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1092 chan->max_power = chan->orig_mpwr =
1093 (int) MBM_TO_DBM(power_rule->max_eirp);
1097 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1098 chan->max_antenna_gain = min(chan->orig_mag,
1099 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
1100 if (chan->orig_mpwr)
1101 chan->max_power = min(chan->orig_mpwr,
1102 (int) MBM_TO_DBM(power_rule->max_eirp));
1104 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1107 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
1110 struct ieee80211_supported_band *sband;
1112 BUG_ON(!wiphy->bands[band]);
1113 sband = wiphy->bands[band];
1115 for (i = 0; i < sband->n_channels; i++)
1116 handle_channel(wiphy, band, i);
1119 static bool ignore_reg_update(struct wiphy *wiphy,
1120 enum nl80211_reg_initiator initiator)
1124 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1125 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1128 * wiphy->regd will be set once the device has its own
1129 * desired regulatory domain set
1131 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
1132 !is_world_regdom(last_request->alpha2))
1137 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1139 struct cfg80211_registered_device *rdev;
1141 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1142 wiphy_update_regulatory(&rdev->wiphy, initiator);
1145 static void handle_reg_beacon(struct wiphy *wiphy,
1146 unsigned int chan_idx,
1147 struct reg_beacon *reg_beacon)
1149 struct ieee80211_supported_band *sband;
1150 struct ieee80211_channel *chan;
1151 bool channel_changed = false;
1152 struct ieee80211_channel chan_before;
1154 assert_cfg80211_lock();
1156 sband = wiphy->bands[reg_beacon->chan.band];
1157 chan = &sband->channels[chan_idx];
1159 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1162 if (chan->beacon_found)
1165 chan->beacon_found = true;
1167 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
1170 chan_before.center_freq = chan->center_freq;
1171 chan_before.flags = chan->flags;
1173 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1174 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1175 channel_changed = true;
1178 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1179 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1180 channel_changed = true;
1183 if (channel_changed)
1184 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1188 * Called when a scan on a wiphy finds a beacon on
1191 static void wiphy_update_new_beacon(struct wiphy *wiphy,
1192 struct reg_beacon *reg_beacon)
1195 struct ieee80211_supported_band *sband;
1197 assert_cfg80211_lock();
1199 if (!wiphy->bands[reg_beacon->chan.band])
1202 sband = wiphy->bands[reg_beacon->chan.band];
1204 for (i = 0; i < sband->n_channels; i++)
1205 handle_reg_beacon(wiphy, i, reg_beacon);
1209 * Called upon reg changes or a new wiphy is added
1211 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1214 struct ieee80211_supported_band *sband;
1215 struct reg_beacon *reg_beacon;
1217 assert_cfg80211_lock();
1219 if (list_empty(®_beacon_list))
1222 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
1223 if (!wiphy->bands[reg_beacon->chan.band])
1225 sband = wiphy->bands[reg_beacon->chan.band];
1226 for (i = 0; i < sband->n_channels; i++)
1227 handle_reg_beacon(wiphy, i, reg_beacon);
1231 static bool reg_is_world_roaming(struct wiphy *wiphy)
1233 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1234 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1237 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1238 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1243 /* Reap the advantages of previously found beacons */
1244 static void reg_process_beacons(struct wiphy *wiphy)
1247 * Means we are just firing up cfg80211, so no beacons would
1248 * have been processed yet.
1252 if (!reg_is_world_roaming(wiphy))
1254 wiphy_update_beacon_reg(wiphy);
1257 static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1261 if (chan->flags & IEEE80211_CHAN_DISABLED)
1263 /* This would happen when regulatory rules disallow HT40 completely */
1264 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1269 static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1270 enum ieee80211_band band,
1271 unsigned int chan_idx)
1273 struct ieee80211_supported_band *sband;
1274 struct ieee80211_channel *channel;
1275 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1278 assert_cfg80211_lock();
1280 sband = wiphy->bands[band];
1281 BUG_ON(chan_idx >= sband->n_channels);
1282 channel = &sband->channels[chan_idx];
1284 if (is_ht40_not_allowed(channel)) {
1285 channel->flags |= IEEE80211_CHAN_NO_HT40;
1290 * We need to ensure the extension channels exist to
1291 * be able to use HT40- or HT40+, this finds them (or not)
1293 for (i = 0; i < sband->n_channels; i++) {
1294 struct ieee80211_channel *c = &sband->channels[i];
1295 if (c->center_freq == (channel->center_freq - 20))
1297 if (c->center_freq == (channel->center_freq + 20))
1302 * Please note that this assumes target bandwidth is 20 MHz,
1303 * if that ever changes we also need to change the below logic
1304 * to include that as well.
1306 if (is_ht40_not_allowed(channel_before))
1307 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1309 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1311 if (is_ht40_not_allowed(channel_after))
1312 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1314 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1317 static void reg_process_ht_flags_band(struct wiphy *wiphy,
1318 enum ieee80211_band band)
1321 struct ieee80211_supported_band *sband;
1323 BUG_ON(!wiphy->bands[band]);
1324 sband = wiphy->bands[band];
1326 for (i = 0; i < sband->n_channels; i++)
1327 reg_process_ht_flags_channel(wiphy, band, i);
1330 static void reg_process_ht_flags(struct wiphy *wiphy)
1332 enum ieee80211_band band;
1337 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1338 if (wiphy->bands[band])
1339 reg_process_ht_flags_band(wiphy, band);
1344 void wiphy_update_regulatory(struct wiphy *wiphy,
1345 enum nl80211_reg_initiator initiator)
1347 enum ieee80211_band band;
1349 if (ignore_reg_update(wiphy, initiator))
1351 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1352 if (wiphy->bands[band])
1353 handle_band(wiphy, band);
1356 reg_process_beacons(wiphy);
1357 reg_process_ht_flags(wiphy);
1358 if (wiphy->reg_notifier)
1359 wiphy->reg_notifier(wiphy, last_request);
1362 static void handle_channel_custom(struct wiphy *wiphy,
1363 enum ieee80211_band band,
1364 unsigned int chan_idx,
1365 const struct ieee80211_regdomain *regd)
1368 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1370 const struct ieee80211_reg_rule *reg_rule = NULL;
1371 const struct ieee80211_power_rule *power_rule = NULL;
1372 const struct ieee80211_freq_range *freq_range = NULL;
1373 struct ieee80211_supported_band *sband;
1374 struct ieee80211_channel *chan;
1378 sband = wiphy->bands[band];
1379 BUG_ON(chan_idx >= sband->n_channels);
1380 chan = &sband->channels[chan_idx];
1382 r = freq_reg_info_regd(wiphy,
1383 MHZ_TO_KHZ(chan->center_freq),
1389 chan->flags = IEEE80211_CHAN_DISABLED;
1393 power_rule = ®_rule->power_rule;
1394 freq_range = ®_rule->freq_range;
1396 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1397 bw_flags = IEEE80211_CHAN_NO_HT40;
1399 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1400 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1401 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1404 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1405 const struct ieee80211_regdomain *regd)
1408 struct ieee80211_supported_band *sband;
1410 BUG_ON(!wiphy->bands[band]);
1411 sband = wiphy->bands[band];
1413 for (i = 0; i < sband->n_channels; i++)
1414 handle_channel_custom(wiphy, band, i, regd);
1417 /* Used by drivers prior to wiphy registration */
1418 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1419 const struct ieee80211_regdomain *regd)
1421 enum ieee80211_band band;
1422 unsigned int bands_set = 0;
1424 mutex_lock(®_mutex);
1425 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1426 if (!wiphy->bands[band])
1428 handle_band_custom(wiphy, band, regd);
1431 mutex_unlock(®_mutex);
1434 * no point in calling this if it won't have any effect
1435 * on your device's supportd bands.
1437 WARN_ON(!bands_set);
1439 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1442 * Return value which can be used by ignore_request() to indicate
1443 * it has been determined we should intersect two regulatory domains
1445 #define REG_INTERSECT 1
1447 /* This has the logic which determines when a new request
1448 * should be ignored. */
1449 static int ignore_request(struct wiphy *wiphy,
1450 struct regulatory_request *pending_request)
1452 struct wiphy *last_wiphy = NULL;
1454 assert_cfg80211_lock();
1456 /* All initial requests are respected */
1460 switch (pending_request->initiator) {
1461 case NL80211_REGDOM_SET_BY_CORE:
1463 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1465 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1467 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
1469 if (last_request->initiator ==
1470 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1471 if (last_wiphy != wiphy) {
1473 * Two cards with two APs claiming different
1474 * Country IE alpha2s. We could
1475 * intersect them, but that seems unlikely
1476 * to be correct. Reject second one for now.
1478 if (regdom_changes(pending_request->alpha2))
1483 * Two consecutive Country IE hints on the same wiphy.
1484 * This should be picked up early by the driver/stack
1486 if (WARN_ON(regdom_changes(pending_request->alpha2)))
1490 return REG_INTERSECT;
1491 case NL80211_REGDOM_SET_BY_DRIVER:
1492 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1493 if (is_old_static_regdom(cfg80211_regdomain))
1495 if (regdom_changes(pending_request->alpha2))
1501 * This would happen if you unplug and plug your card
1502 * back in or if you add a new device for which the previously
1503 * loaded card also agrees on the regulatory domain.
1505 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1506 !regdom_changes(pending_request->alpha2))
1509 return REG_INTERSECT;
1510 case NL80211_REGDOM_SET_BY_USER:
1511 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1512 return REG_INTERSECT;
1514 * If the user knows better the user should set the regdom
1515 * to their country before the IE is picked up
1517 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1518 last_request->intersect)
1521 * Process user requests only after previous user/driver/core
1522 * requests have been processed
1524 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1525 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1526 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1527 if (regdom_changes(last_request->alpha2))
1531 if (!is_old_static_regdom(cfg80211_regdomain) &&
1532 !regdom_changes(pending_request->alpha2))
1542 * __regulatory_hint - hint to the wireless core a regulatory domain
1543 * @wiphy: if the hint comes from country information from an AP, this
1544 * is required to be set to the wiphy that received the information
1545 * @pending_request: the regulatory request currently being processed
1547 * The Wireless subsystem can use this function to hint to the wireless core
1548 * what it believes should be the current regulatory domain.
1550 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1551 * already been set or other standard error codes.
1553 * Caller must hold &cfg80211_mutex and ®_mutex
1555 static int __regulatory_hint(struct wiphy *wiphy,
1556 struct regulatory_request *pending_request)
1558 bool intersect = false;
1561 assert_cfg80211_lock();
1563 r = ignore_request(wiphy, pending_request);
1565 if (r == REG_INTERSECT) {
1566 if (pending_request->initiator ==
1567 NL80211_REGDOM_SET_BY_DRIVER) {
1568 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1570 kfree(pending_request);
1577 * If the regulatory domain being requested by the
1578 * driver has already been set just copy it to the
1581 if (r == -EALREADY &&
1582 pending_request->initiator ==
1583 NL80211_REGDOM_SET_BY_DRIVER) {
1584 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1586 kfree(pending_request);
1592 kfree(pending_request);
1597 kfree(last_request);
1599 last_request = pending_request;
1600 last_request->intersect = intersect;
1602 pending_request = NULL;
1604 /* When r == REG_INTERSECT we do need to call CRDA */
1607 * Since CRDA will not be called in this case as we already
1608 * have applied the requested regulatory domain before we just
1609 * inform userspace we have processed the request
1612 nl80211_send_reg_change_event(last_request);
1616 return call_crda(last_request->alpha2);
1619 /* This processes *all* regulatory hints */
1620 static void reg_process_hint(struct regulatory_request *reg_request)
1623 struct wiphy *wiphy = NULL;
1625 BUG_ON(!reg_request->alpha2);
1627 mutex_lock(&cfg80211_mutex);
1628 mutex_lock(®_mutex);
1630 if (wiphy_idx_valid(reg_request->wiphy_idx))
1631 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1633 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1639 r = __regulatory_hint(wiphy, reg_request);
1640 /* This is required so that the orig_* parameters are saved */
1641 if (r == -EALREADY && wiphy &&
1642 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
1643 wiphy_update_regulatory(wiphy, reg_request->initiator);
1645 mutex_unlock(®_mutex);
1646 mutex_unlock(&cfg80211_mutex);
1649 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1650 static void reg_process_pending_hints(void)
1652 struct regulatory_request *reg_request;
1654 spin_lock(®_requests_lock);
1655 while (!list_empty(®_requests_list)) {
1656 reg_request = list_first_entry(®_requests_list,
1657 struct regulatory_request,
1659 list_del_init(®_request->list);
1661 spin_unlock(®_requests_lock);
1662 reg_process_hint(reg_request);
1663 spin_lock(®_requests_lock);
1665 spin_unlock(®_requests_lock);
1668 /* Processes beacon hints -- this has nothing to do with country IEs */
1669 static void reg_process_pending_beacon_hints(void)
1671 struct cfg80211_registered_device *rdev;
1672 struct reg_beacon *pending_beacon, *tmp;
1675 * No need to hold the reg_mutex here as we just touch wiphys
1676 * and do not read or access regulatory variables.
1678 mutex_lock(&cfg80211_mutex);
1680 /* This goes through the _pending_ beacon list */
1681 spin_lock_bh(®_pending_beacons_lock);
1683 if (list_empty(®_pending_beacons)) {
1684 spin_unlock_bh(®_pending_beacons_lock);
1688 list_for_each_entry_safe(pending_beacon, tmp,
1689 ®_pending_beacons, list) {
1691 list_del_init(&pending_beacon->list);
1693 /* Applies the beacon hint to current wiphys */
1694 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1695 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
1697 /* Remembers the beacon hint for new wiphys or reg changes */
1698 list_add_tail(&pending_beacon->list, ®_beacon_list);
1701 spin_unlock_bh(®_pending_beacons_lock);
1703 mutex_unlock(&cfg80211_mutex);
1706 static void reg_todo(struct work_struct *work)
1708 reg_process_pending_hints();
1709 reg_process_pending_beacon_hints();
1712 static DECLARE_WORK(reg_work, reg_todo);
1714 static void queue_regulatory_request(struct regulatory_request *request)
1716 spin_lock(®_requests_lock);
1717 list_add_tail(&request->list, ®_requests_list);
1718 spin_unlock(®_requests_lock);
1720 schedule_work(®_work);
1723 /* Core regulatory hint -- happens once during cfg80211_init() */
1724 static int regulatory_hint_core(const char *alpha2)
1726 struct regulatory_request *request;
1728 BUG_ON(last_request);
1730 request = kzalloc(sizeof(struct regulatory_request),
1735 request->alpha2[0] = alpha2[0];
1736 request->alpha2[1] = alpha2[1];
1737 request->initiator = NL80211_REGDOM_SET_BY_CORE;
1739 queue_regulatory_request(request);
1742 * This ensures last_request is populated once modules
1743 * come swinging in and calling regulatory hints and
1744 * wiphy_apply_custom_regulatory().
1746 flush_scheduled_work();
1752 int regulatory_hint_user(const char *alpha2)
1754 struct regulatory_request *request;
1758 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1762 request->wiphy_idx = WIPHY_IDX_STALE;
1763 request->alpha2[0] = alpha2[0];
1764 request->alpha2[1] = alpha2[1];
1765 request->initiator = NL80211_REGDOM_SET_BY_USER,
1767 queue_regulatory_request(request);
1773 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1775 struct regulatory_request *request;
1780 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1784 request->wiphy_idx = get_wiphy_idx(wiphy);
1786 /* Must have registered wiphy first */
1787 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1789 request->alpha2[0] = alpha2[0];
1790 request->alpha2[1] = alpha2[1];
1791 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1793 queue_regulatory_request(request);
1797 EXPORT_SYMBOL(regulatory_hint);
1799 /* Caller must hold reg_mutex */
1800 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1801 u32 country_ie_checksum)
1803 struct wiphy *request_wiphy;
1807 if (unlikely(last_request->initiator !=
1808 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1811 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1816 if (likely(request_wiphy != wiphy))
1817 return !country_ie_integrity_changes(country_ie_checksum);
1819 * We should not have let these through at this point, they
1820 * should have been picked up earlier by the first alpha2 check
1823 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1829 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1830 * therefore cannot iterate over the rdev list here.
1832 void regulatory_hint_11d(struct wiphy *wiphy,
1836 struct ieee80211_regdomain *rd = NULL;
1839 enum environment_cap env = ENVIRON_ANY;
1840 struct regulatory_request *request;
1842 mutex_lock(®_mutex);
1844 if (unlikely(!last_request))
1847 /* IE len must be evenly divisible by 2 */
1848 if (country_ie_len & 0x01)
1851 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1855 * Pending country IE processing, this can happen after we
1856 * call CRDA and wait for a response if a beacon was received before
1857 * we were able to process the last regulatory_hint_11d() call
1859 if (country_ie_regdomain)
1862 alpha2[0] = country_ie[0];
1863 alpha2[1] = country_ie[1];
1865 if (country_ie[2] == 'I')
1866 env = ENVIRON_INDOOR;
1867 else if (country_ie[2] == 'O')
1868 env = ENVIRON_OUTDOOR;
1871 * We will run this only upon a successful connection on cfg80211.
1872 * We leave conflict resolution to the workqueue, where can hold
1875 if (likely(last_request->initiator ==
1876 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1877 wiphy_idx_valid(last_request->wiphy_idx)))
1880 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1885 * This will not happen right now but we leave it here for the
1886 * the future when we want to add suspend/resume support and having
1887 * the user move to another country after doing so, or having the user
1888 * move to another AP. Right now we just trust the first AP.
1890 * If we hit this before we add this support we want to be informed of
1891 * it as it would indicate a mistake in the current design
1893 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
1896 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1901 * We keep this around for when CRDA comes back with a response so
1902 * we can intersect with that
1904 country_ie_regdomain = rd;
1906 request->wiphy_idx = get_wiphy_idx(wiphy);
1907 request->alpha2[0] = rd->alpha2[0];
1908 request->alpha2[1] = rd->alpha2[1];
1909 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
1910 request->country_ie_checksum = checksum;
1911 request->country_ie_env = env;
1913 mutex_unlock(®_mutex);
1915 queue_regulatory_request(request);
1922 mutex_unlock(®_mutex);
1925 static bool freq_is_chan_12_13_14(u16 freq)
1927 if (freq == ieee80211_channel_to_frequency(12) ||
1928 freq == ieee80211_channel_to_frequency(13) ||
1929 freq == ieee80211_channel_to_frequency(14))
1934 int regulatory_hint_found_beacon(struct wiphy *wiphy,
1935 struct ieee80211_channel *beacon_chan,
1938 struct reg_beacon *reg_beacon;
1940 if (likely((beacon_chan->beacon_found ||
1941 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1942 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1943 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1946 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1950 #ifdef CONFIG_CFG80211_REG_DEBUG
1951 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1952 "frequency: %d MHz (Ch %d) on %s\n",
1953 beacon_chan->center_freq,
1954 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1957 memcpy(®_beacon->chan, beacon_chan,
1958 sizeof(struct ieee80211_channel));
1962 * Since we can be called from BH or and non-BH context
1963 * we must use spin_lock_bh()
1965 spin_lock_bh(®_pending_beacons_lock);
1966 list_add_tail(®_beacon->list, ®_pending_beacons);
1967 spin_unlock_bh(®_pending_beacons_lock);
1969 schedule_work(®_work);
1974 static void print_rd_rules(const struct ieee80211_regdomain *rd)
1977 const struct ieee80211_reg_rule *reg_rule = NULL;
1978 const struct ieee80211_freq_range *freq_range = NULL;
1979 const struct ieee80211_power_rule *power_rule = NULL;
1981 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
1982 "(max_antenna_gain, max_eirp)\n");
1984 for (i = 0; i < rd->n_reg_rules; i++) {
1985 reg_rule = &rd->reg_rules[i];
1986 freq_range = ®_rule->freq_range;
1987 power_rule = ®_rule->power_rule;
1990 * There may not be documentation for max antenna gain
1991 * in certain regions
1993 if (power_rule->max_antenna_gain)
1994 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
1995 "(%d mBi, %d mBm)\n",
1996 freq_range->start_freq_khz,
1997 freq_range->end_freq_khz,
1998 freq_range->max_bandwidth_khz,
1999 power_rule->max_antenna_gain,
2000 power_rule->max_eirp);
2002 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
2004 freq_range->start_freq_khz,
2005 freq_range->end_freq_khz,
2006 freq_range->max_bandwidth_khz,
2007 power_rule->max_eirp);
2011 static void print_regdomain(const struct ieee80211_regdomain *rd)
2014 if (is_intersected_alpha2(rd->alpha2)) {
2016 if (last_request->initiator ==
2017 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2018 struct cfg80211_registered_device *rdev;
2019 rdev = cfg80211_rdev_by_wiphy_idx(
2020 last_request->wiphy_idx);
2022 printk(KERN_INFO "cfg80211: Current regulatory "
2023 "domain updated by AP to: %c%c\n",
2024 rdev->country_ie_alpha2[0],
2025 rdev->country_ie_alpha2[1]);
2027 printk(KERN_INFO "cfg80211: Current regulatory "
2028 "domain intersected: \n");
2030 printk(KERN_INFO "cfg80211: Current regulatory "
2031 "domain intersected: \n");
2032 } else if (is_world_regdom(rd->alpha2))
2033 printk(KERN_INFO "cfg80211: World regulatory "
2034 "domain updated:\n");
2036 if (is_unknown_alpha2(rd->alpha2))
2037 printk(KERN_INFO "cfg80211: Regulatory domain "
2038 "changed to driver built-in settings "
2039 "(unknown country)\n");
2041 printk(KERN_INFO "cfg80211: Regulatory domain "
2042 "changed to country: %c%c\n",
2043 rd->alpha2[0], rd->alpha2[1]);
2048 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
2050 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2051 rd->alpha2[0], rd->alpha2[1]);
2055 #ifdef CONFIG_CFG80211_REG_DEBUG
2056 static void reg_country_ie_process_debug(
2057 const struct ieee80211_regdomain *rd,
2058 const struct ieee80211_regdomain *country_ie_regdomain,
2059 const struct ieee80211_regdomain *intersected_rd)
2061 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2062 print_regdomain_info(country_ie_regdomain);
2063 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2064 print_regdomain_info(rd);
2065 if (intersected_rd) {
2066 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2068 print_regdomain_info(intersected_rd);
2071 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2074 static inline void reg_country_ie_process_debug(
2075 const struct ieee80211_regdomain *rd,
2076 const struct ieee80211_regdomain *country_ie_regdomain,
2077 const struct ieee80211_regdomain *intersected_rd)
2082 /* Takes ownership of rd only if it doesn't fail */
2083 static int __set_regdom(const struct ieee80211_regdomain *rd)
2085 const struct ieee80211_regdomain *intersected_rd = NULL;
2086 struct cfg80211_registered_device *rdev = NULL;
2087 struct wiphy *request_wiphy;
2088 /* Some basic sanity checks first */
2090 if (is_world_regdom(rd->alpha2)) {
2091 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2093 update_world_regdomain(rd);
2097 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2098 !is_unknown_alpha2(rd->alpha2))
2105 * Lets only bother proceeding on the same alpha2 if the current
2106 * rd is non static (it means CRDA was present and was used last)
2107 * and the pending request came in from a country IE
2109 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2111 * If someone else asked us to change the rd lets only bother
2112 * checking if the alpha2 changes if CRDA was already called
2114 if (!is_old_static_regdom(cfg80211_regdomain) &&
2115 !regdom_changes(rd->alpha2))
2120 * Now lets set the regulatory domain, update all driver channels
2121 * and finally inform them of what we have done, in case they want
2122 * to review or adjust their own settings based on their own
2123 * internal EEPROM data
2126 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2129 if (!is_valid_rd(rd)) {
2130 printk(KERN_ERR "cfg80211: Invalid "
2131 "regulatory domain detected:\n");
2132 print_regdomain_info(rd);
2136 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2138 if (!last_request->intersect) {
2141 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
2143 cfg80211_regdomain = rd;
2148 * For a driver hint, lets copy the regulatory domain the
2149 * driver wanted to the wiphy to deal with conflicts
2153 * Userspace could have sent two replies with only
2154 * one kernel request.
2156 if (request_wiphy->regd)
2159 r = reg_copy_regd(&request_wiphy->regd, rd);
2164 cfg80211_regdomain = rd;
2168 /* Intersection requires a bit more work */
2170 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2172 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2173 if (!intersected_rd)
2177 * We can trash what CRDA provided now.
2178 * However if a driver requested this specific regulatory
2179 * domain we keep it for its private use
2181 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2182 request_wiphy->regd = rd;
2189 cfg80211_regdomain = intersected_rd;
2195 * Country IE requests are handled a bit differently, we intersect
2196 * the country IE rd with what CRDA believes that country should have
2200 * Userspace could have sent two replies with only
2201 * one kernel request. By the second reply we would have
2202 * already processed and consumed the country_ie_regdomain.
2204 if (!country_ie_regdomain)
2206 BUG_ON(rd == country_ie_regdomain);
2209 * Intersect what CRDA returned and our what we
2210 * had built from the Country IE received
2213 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2215 reg_country_ie_process_debug(rd,
2216 country_ie_regdomain,
2219 kfree(country_ie_regdomain);
2220 country_ie_regdomain = NULL;
2222 if (!intersected_rd)
2225 rdev = wiphy_to_dev(request_wiphy);
2227 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2228 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2229 rdev->env = last_request->country_ie_env;
2231 BUG_ON(intersected_rd == rd);
2237 cfg80211_regdomain = intersected_rd;
2244 * Use this call to set the current regulatory domain. Conflicts with
2245 * multiple drivers can be ironed out later. Caller must've already
2246 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2248 int set_regdom(const struct ieee80211_regdomain *rd)
2252 assert_cfg80211_lock();
2254 mutex_lock(®_mutex);
2256 /* Note that this doesn't update the wiphys, this is done below */
2257 r = __set_regdom(rd);
2260 mutex_unlock(®_mutex);
2264 /* This would make this whole thing pointless */
2265 if (!last_request->intersect)
2266 BUG_ON(rd != cfg80211_regdomain);
2268 /* update all wiphys now with the new established regulatory domain */
2269 update_all_wiphy_regulatory(last_request->initiator);
2271 print_regdomain(cfg80211_regdomain);
2273 nl80211_send_reg_change_event(last_request);
2275 mutex_unlock(®_mutex);
2280 /* Caller must hold cfg80211_mutex */
2281 void reg_device_remove(struct wiphy *wiphy)
2283 struct wiphy *request_wiphy = NULL;
2285 assert_cfg80211_lock();
2287 mutex_lock(®_mutex);
2292 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2294 if (!request_wiphy || request_wiphy != wiphy)
2297 last_request->wiphy_idx = WIPHY_IDX_STALE;
2298 last_request->country_ie_env = ENVIRON_ANY;
2300 mutex_unlock(®_mutex);
2303 int regulatory_init(void)
2307 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2308 if (IS_ERR(reg_pdev))
2309 return PTR_ERR(reg_pdev);
2311 spin_lock_init(®_requests_lock);
2312 spin_lock_init(®_pending_beacons_lock);
2314 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2315 cfg80211_regdomain = static_regdom(ieee80211_regdom);
2317 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
2318 print_regdomain_info(cfg80211_regdomain);
2320 cfg80211_regdomain = cfg80211_world_regdom;
2323 /* We always try to get an update for the static regdomain */
2324 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
2329 * N.B. kobject_uevent_env() can fail mainly for when we're out
2330 * memory which is handled and propagated appropriately above
2331 * but it can also fail during a netlink_broadcast() or during
2332 * early boot for call_usermodehelper(). For now treat these
2333 * errors as non-fatal.
2335 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2336 "to call CRDA during init");
2337 #ifdef CONFIG_CFG80211_REG_DEBUG
2338 /* We want to find out exactly why when debugging */
2344 * Finally, if the user set the module parameter treat it
2347 if (!is_world_regdom(ieee80211_regdom))
2348 regulatory_hint_user(ieee80211_regdom);
2353 void regulatory_exit(void)
2355 struct regulatory_request *reg_request, *tmp;
2356 struct reg_beacon *reg_beacon, *btmp;
2358 cancel_work_sync(®_work);
2360 mutex_lock(&cfg80211_mutex);
2361 mutex_lock(®_mutex);
2365 kfree(country_ie_regdomain);
2366 country_ie_regdomain = NULL;
2368 kfree(last_request);
2370 platform_device_unregister(reg_pdev);
2372 spin_lock_bh(®_pending_beacons_lock);
2373 if (!list_empty(®_pending_beacons)) {
2374 list_for_each_entry_safe(reg_beacon, btmp,
2375 ®_pending_beacons, list) {
2376 list_del(®_beacon->list);
2380 spin_unlock_bh(®_pending_beacons_lock);
2382 if (!list_empty(®_beacon_list)) {
2383 list_for_each_entry_safe(reg_beacon, btmp,
2384 ®_beacon_list, list) {
2385 list_del(®_beacon->list);
2390 spin_lock(®_requests_lock);
2391 if (!list_empty(®_requests_list)) {
2392 list_for_each_entry_safe(reg_request, tmp,
2393 ®_requests_list, list) {
2394 list_del(®_request->list);
2398 spin_unlock(®_requests_lock);
2400 mutex_unlock(®_mutex);
2401 mutex_unlock(&cfg80211_mutex);