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/wireless.h>
41 #include <net/cfg80211.h>
45 /* Receipt of information from last regulatory request */
46 static struct regulatory_request *last_request;
48 /* To trigger userspace events */
49 static struct platform_device *reg_pdev;
51 /* Keep the ordering from large to small */
52 static u32 supported_bandwidths[] = {
58 * Central wireless core regulatory domains, we only need two,
59 * the current one and a world regulatory domain in case we have no
60 * information to give us an alpha2
62 const struct ieee80211_regdomain *cfg80211_regdomain;
65 * We use this as a place for the rd structure built from the
66 * last parsed country IE to rest until CRDA gets back to us with
67 * what it thinks should apply for the same country
69 static const struct ieee80211_regdomain *country_ie_regdomain;
71 static LIST_HEAD(reg_requests_list);
72 static spinlock_t reg_requests_lock;
74 /* We keep a static world regulatory domain in case of the absence of CRDA */
75 static const struct ieee80211_regdomain world_regdom = {
79 REG_RULE(2412-10, 2462+10, 40, 6, 20,
80 NL80211_RRF_PASSIVE_SCAN |
85 static const struct ieee80211_regdomain *cfg80211_world_regdom =
88 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
89 static char *ieee80211_regdom = "US";
90 module_param(ieee80211_regdom, charp, 0444);
91 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
94 * We assume 40 MHz bandwidth for the old regulatory work.
95 * We make emphasis we are using the exact same frequencies
99 static const struct ieee80211_regdomain us_regdom = {
103 /* IEEE 802.11b/g, channels 1..11 */
104 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
105 /* IEEE 802.11a, channel 36 */
106 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
107 /* IEEE 802.11a, channel 40 */
108 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
109 /* IEEE 802.11a, channel 44 */
110 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
111 /* IEEE 802.11a, channels 48..64 */
112 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
113 /* IEEE 802.11a, channels 149..165, outdoor */
114 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
118 static const struct ieee80211_regdomain jp_regdom = {
122 /* IEEE 802.11b/g, channels 1..14 */
123 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
124 /* IEEE 802.11a, channels 34..48 */
125 REG_RULE(5170-10, 5240+10, 40, 6, 20,
126 NL80211_RRF_PASSIVE_SCAN),
127 /* IEEE 802.11a, channels 52..64 */
128 REG_RULE(5260-10, 5320+10, 40, 6, 20,
129 NL80211_RRF_NO_IBSS |
134 static const struct ieee80211_regdomain eu_regdom = {
137 * This alpha2 is bogus, we leave it here just for stupid
138 * backward compatibility
142 /* IEEE 802.11b/g, channels 1..13 */
143 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
144 /* IEEE 802.11a, channel 36 */
145 REG_RULE(5180-10, 5180+10, 40, 6, 23,
146 NL80211_RRF_PASSIVE_SCAN),
147 /* IEEE 802.11a, channel 40 */
148 REG_RULE(5200-10, 5200+10, 40, 6, 23,
149 NL80211_RRF_PASSIVE_SCAN),
150 /* IEEE 802.11a, channel 44 */
151 REG_RULE(5220-10, 5220+10, 40, 6, 23,
152 NL80211_RRF_PASSIVE_SCAN),
153 /* IEEE 802.11a, channels 48..64 */
154 REG_RULE(5240-10, 5320+10, 40, 6, 20,
155 NL80211_RRF_NO_IBSS |
157 /* IEEE 802.11a, channels 100..140 */
158 REG_RULE(5500-10, 5700+10, 40, 6, 30,
159 NL80211_RRF_NO_IBSS |
164 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
166 if (alpha2[0] == 'U' && alpha2[1] == 'S')
168 if (alpha2[0] == 'J' && alpha2[1] == 'P')
170 if (alpha2[0] == 'E' && alpha2[1] == 'U')
172 /* Default, as per the old rules */
176 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
178 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
183 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
189 static void reset_regdomains(void)
191 /* avoid freeing static information or freeing something twice */
192 if (cfg80211_regdomain == cfg80211_world_regdom)
193 cfg80211_regdomain = NULL;
194 if (cfg80211_world_regdom == &world_regdom)
195 cfg80211_world_regdom = NULL;
196 if (cfg80211_regdomain == &world_regdom)
197 cfg80211_regdomain = NULL;
198 if (is_old_static_regdom(cfg80211_regdomain))
199 cfg80211_regdomain = NULL;
201 kfree(cfg80211_regdomain);
202 kfree(cfg80211_world_regdom);
204 cfg80211_world_regdom = &world_regdom;
205 cfg80211_regdomain = NULL;
209 * Dynamic world regulatory domain requested by the wireless
210 * core upon initialization
212 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
214 BUG_ON(!last_request);
218 cfg80211_world_regdom = rd;
219 cfg80211_regdomain = rd;
222 bool is_world_regdom(const char *alpha2)
226 if (alpha2[0] == '0' && alpha2[1] == '0')
231 static bool is_alpha2_set(const char *alpha2)
235 if (alpha2[0] != 0 && alpha2[1] != 0)
240 static bool is_alpha_upper(char letter)
243 if (letter >= 65 && letter <= 90)
248 static bool is_unknown_alpha2(const char *alpha2)
253 * Special case where regulatory domain was built by driver
254 * but a specific alpha2 cannot be determined
256 if (alpha2[0] == '9' && alpha2[1] == '9')
261 static bool is_intersected_alpha2(const char *alpha2)
266 * Special case where regulatory domain is the
267 * result of an intersection between two regulatory domain
270 if (alpha2[0] == '9' && alpha2[1] == '8')
275 static bool is_an_alpha2(const char *alpha2)
279 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
284 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
286 if (!alpha2_x || !alpha2_y)
288 if (alpha2_x[0] == alpha2_y[0] &&
289 alpha2_x[1] == alpha2_y[1])
294 static bool regdom_changes(const char *alpha2)
296 assert_cfg80211_lock();
298 if (!cfg80211_regdomain)
300 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
306 * country_ie_integrity_changes - tells us if the country IE has changed
307 * @checksum: checksum of country IE of fields we are interested in
309 * If the country IE has not changed you can ignore it safely. This is
310 * useful to determine if two devices are seeing two different country IEs
311 * even on the same alpha2. Note that this will return false if no IE has
312 * been set on the wireless core yet.
314 static bool country_ie_integrity_changes(u32 checksum)
316 /* If no IE has been set then the checksum doesn't change */
317 if (unlikely(!last_request->country_ie_checksum))
319 if (unlikely(last_request->country_ie_checksum != checksum))
325 * This lets us keep regulatory code which is updated on a regulatory
326 * basis in userspace.
328 static int call_crda(const char *alpha2)
330 char country_env[9 + 2] = "COUNTRY=";
336 if (!is_world_regdom((char *) alpha2))
337 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
338 alpha2[0], alpha2[1]);
340 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
341 "regulatory domain\n");
343 country_env[8] = alpha2[0];
344 country_env[9] = alpha2[1];
346 return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp);
349 /* Used by nl80211 before kmalloc'ing our regulatory domain */
350 bool reg_is_valid_request(const char *alpha2)
355 return alpha2_equal(last_request->alpha2, alpha2);
358 /* Sanity check on a regulatory rule */
359 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
361 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
364 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
367 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
370 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
372 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
378 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
380 const struct ieee80211_reg_rule *reg_rule = NULL;
383 if (!rd->n_reg_rules)
386 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
389 for (i = 0; i < rd->n_reg_rules; i++) {
390 reg_rule = &rd->reg_rules[i];
391 if (!is_valid_reg_rule(reg_rule))
398 /* Returns value in KHz */
399 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
403 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
404 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
405 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
406 if (start_freq_khz >= freq_range->start_freq_khz &&
407 end_freq_khz <= freq_range->end_freq_khz)
408 return supported_bandwidths[i];
414 * freq_in_rule_band - tells us if a frequency is in a frequency band
415 * @freq_range: frequency rule we want to query
416 * @freq_khz: frequency we are inquiring about
418 * This lets us know if a specific frequency rule is or is not relevant to
419 * a specific frequency's band. Bands are device specific and artificial
420 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
421 * safe for now to assume that a frequency rule should not be part of a
422 * frequency's band if the start freq or end freq are off by more than 2 GHz.
423 * This resolution can be lowered and should be considered as we add
424 * regulatory rule support for other "bands".
426 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
429 #define ONE_GHZ_IN_KHZ 1000000
430 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
432 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
435 #undef ONE_GHZ_IN_KHZ
439 * Converts a country IE to a regulatory domain. A regulatory domain
440 * structure has a lot of information which the IE doesn't yet have,
441 * so for the other values we use upper max values as we will intersect
442 * with our userspace regulatory agent to get lower bounds.
444 static struct ieee80211_regdomain *country_ie_2_rd(
449 struct ieee80211_regdomain *rd = NULL;
453 u32 num_rules = 0, size_of_regd = 0;
454 u8 *triplets_start = NULL;
455 u8 len_at_triplet = 0;
456 /* the last channel we have registered in a subband (triplet) */
457 int last_sub_max_channel = 0;
459 *checksum = 0xDEADBEEF;
461 /* Country IE requirements */
462 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
463 country_ie_len & 0x01);
465 alpha2[0] = country_ie[0];
466 alpha2[1] = country_ie[1];
469 * Third octet can be:
473 * anything else we assume is no restrictions
475 if (country_ie[2] == 'I')
476 flags = NL80211_RRF_NO_OUTDOOR;
477 else if (country_ie[2] == 'O')
478 flags = NL80211_RRF_NO_INDOOR;
483 triplets_start = country_ie;
484 len_at_triplet = country_ie_len;
486 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
489 * We need to build a reg rule for each triplet, but first we must
490 * calculate the number of reg rules we will need. We will need one
491 * for each channel subband
493 while (country_ie_len >= 3) {
495 struct ieee80211_country_ie_triplet *triplet =
496 (struct ieee80211_country_ie_triplet *) country_ie;
497 int cur_sub_max_channel = 0, cur_channel = 0;
499 if (triplet->ext.reg_extension_id >=
500 IEEE80211_COUNTRY_EXTENSION_ID) {
507 if (triplet->chans.first_channel <= 14)
508 end_channel = triplet->chans.first_channel +
509 triplet->chans.num_channels;
512 * 5 GHz -- For example in country IEs if the first
513 * channel given is 36 and the number of channels is 4
514 * then the individual channel numbers defined for the
515 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
516 * and not 36, 37, 38, 39.
518 * See: http://tinyurl.com/11d-clarification
520 end_channel = triplet->chans.first_channel +
521 (4 * (triplet->chans.num_channels - 1));
523 cur_channel = triplet->chans.first_channel;
524 cur_sub_max_channel = end_channel;
526 /* Basic sanity check */
527 if (cur_sub_max_channel < cur_channel)
531 * Do not allow overlapping channels. Also channels
532 * passed in each subband must be monotonically
535 if (last_sub_max_channel) {
536 if (cur_channel <= last_sub_max_channel)
538 if (cur_sub_max_channel <= last_sub_max_channel)
543 * When dot11RegulatoryClassesRequired is supported
544 * we can throw ext triplets as part of this soup,
545 * for now we don't care when those change as we
548 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
549 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
550 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
552 last_sub_max_channel = cur_sub_max_channel;
559 * Note: this is not a IEEE requirement but
560 * simply a memory requirement
562 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
566 country_ie = triplets_start;
567 country_ie_len = len_at_triplet;
569 size_of_regd = sizeof(struct ieee80211_regdomain) +
570 (num_rules * sizeof(struct ieee80211_reg_rule));
572 rd = kzalloc(size_of_regd, GFP_KERNEL);
576 rd->n_reg_rules = num_rules;
577 rd->alpha2[0] = alpha2[0];
578 rd->alpha2[1] = alpha2[1];
580 /* This time around we fill in the rd */
581 while (country_ie_len >= 3) {
583 struct ieee80211_country_ie_triplet *triplet =
584 (struct ieee80211_country_ie_triplet *) country_ie;
585 struct ieee80211_reg_rule *reg_rule = NULL;
586 struct ieee80211_freq_range *freq_range = NULL;
587 struct ieee80211_power_rule *power_rule = NULL;
590 * Must parse if dot11RegulatoryClassesRequired is true,
591 * we don't support this yet
593 if (triplet->ext.reg_extension_id >=
594 IEEE80211_COUNTRY_EXTENSION_ID) {
600 reg_rule = &rd->reg_rules[i];
601 freq_range = ®_rule->freq_range;
602 power_rule = ®_rule->power_rule;
604 reg_rule->flags = flags;
607 if (triplet->chans.first_channel <= 14)
608 end_channel = triplet->chans.first_channel +
609 triplet->chans.num_channels;
611 end_channel = triplet->chans.first_channel +
612 (4 * (triplet->chans.num_channels - 1));
615 * The +10 is since the regulatory domain expects
616 * the actual band edge, not the center of freq for
617 * its start and end freqs, assuming 20 MHz bandwidth on
618 * the channels passed
620 freq_range->start_freq_khz =
621 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
622 triplet->chans.first_channel) - 10);
623 freq_range->end_freq_khz =
624 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
628 * These are large arbitrary values we use to intersect later.
629 * Increment this if we ever support >= 40 MHz channels
632 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
633 power_rule->max_antenna_gain = DBI_TO_MBI(100);
634 power_rule->max_eirp = DBM_TO_MBM(100);
640 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
648 * Helper for regdom_intersect(), this does the real
649 * mathematical intersection fun
651 static int reg_rules_intersect(
652 const struct ieee80211_reg_rule *rule1,
653 const struct ieee80211_reg_rule *rule2,
654 struct ieee80211_reg_rule *intersected_rule)
656 const struct ieee80211_freq_range *freq_range1, *freq_range2;
657 struct ieee80211_freq_range *freq_range;
658 const struct ieee80211_power_rule *power_rule1, *power_rule2;
659 struct ieee80211_power_rule *power_rule;
662 freq_range1 = &rule1->freq_range;
663 freq_range2 = &rule2->freq_range;
664 freq_range = &intersected_rule->freq_range;
666 power_rule1 = &rule1->power_rule;
667 power_rule2 = &rule2->power_rule;
668 power_rule = &intersected_rule->power_rule;
670 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
671 freq_range2->start_freq_khz);
672 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
673 freq_range2->end_freq_khz);
674 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
675 freq_range2->max_bandwidth_khz);
677 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
678 if (freq_range->max_bandwidth_khz > freq_diff)
679 freq_range->max_bandwidth_khz = freq_diff;
681 power_rule->max_eirp = min(power_rule1->max_eirp,
682 power_rule2->max_eirp);
683 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
684 power_rule2->max_antenna_gain);
686 intersected_rule->flags = (rule1->flags | rule2->flags);
688 if (!is_valid_reg_rule(intersected_rule))
695 * regdom_intersect - do the intersection between two regulatory domains
696 * @rd1: first regulatory domain
697 * @rd2: second regulatory domain
699 * Use this function to get the intersection between two regulatory domains.
700 * Once completed we will mark the alpha2 for the rd as intersected, "98",
701 * as no one single alpha2 can represent this regulatory domain.
703 * Returns a pointer to the regulatory domain structure which will hold the
704 * resulting intersection of rules between rd1 and rd2. We will
705 * kzalloc() this structure for you.
707 static struct ieee80211_regdomain *regdom_intersect(
708 const struct ieee80211_regdomain *rd1,
709 const struct ieee80211_regdomain *rd2)
713 unsigned int num_rules = 0, rule_idx = 0;
714 const struct ieee80211_reg_rule *rule1, *rule2;
715 struct ieee80211_reg_rule *intersected_rule;
716 struct ieee80211_regdomain *rd;
717 /* This is just a dummy holder to help us count */
718 struct ieee80211_reg_rule irule;
720 /* Uses the stack temporarily for counter arithmetic */
721 intersected_rule = &irule;
723 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
729 * First we get a count of the rules we'll need, then we actually
730 * build them. This is to so we can malloc() and free() a
731 * regdomain once. The reason we use reg_rules_intersect() here
732 * is it will return -EINVAL if the rule computed makes no sense.
733 * All rules that do check out OK are valid.
736 for (x = 0; x < rd1->n_reg_rules; x++) {
737 rule1 = &rd1->reg_rules[x];
738 for (y = 0; y < rd2->n_reg_rules; y++) {
739 rule2 = &rd2->reg_rules[y];
740 if (!reg_rules_intersect(rule1, rule2,
743 memset(intersected_rule, 0,
744 sizeof(struct ieee80211_reg_rule));
751 size_of_regd = sizeof(struct ieee80211_regdomain) +
752 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
754 rd = kzalloc(size_of_regd, GFP_KERNEL);
758 for (x = 0; x < rd1->n_reg_rules; x++) {
759 rule1 = &rd1->reg_rules[x];
760 for (y = 0; y < rd2->n_reg_rules; y++) {
761 rule2 = &rd2->reg_rules[y];
763 * This time around instead of using the stack lets
764 * write to the target rule directly saving ourselves
767 intersected_rule = &rd->reg_rules[rule_idx];
768 r = reg_rules_intersect(rule1, rule2,
771 * No need to memset here the intersected rule here as
772 * we're not using the stack anymore
780 if (rule_idx != num_rules) {
785 rd->n_reg_rules = num_rules;
793 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
794 * want to just have the channel structure use these
796 static u32 map_regdom_flags(u32 rd_flags)
798 u32 channel_flags = 0;
799 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
800 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
801 if (rd_flags & NL80211_RRF_NO_IBSS)
802 channel_flags |= IEEE80211_CHAN_NO_IBSS;
803 if (rd_flags & NL80211_RRF_DFS)
804 channel_flags |= IEEE80211_CHAN_RADAR;
805 return channel_flags;
808 static int freq_reg_info_regd(struct wiphy *wiphy,
811 const struct ieee80211_reg_rule **reg_rule,
812 const struct ieee80211_regdomain *custom_regd)
815 bool band_rule_found = false;
816 const struct ieee80211_regdomain *regd;
817 u32 max_bandwidth = 0;
819 regd = custom_regd ? custom_regd : cfg80211_regdomain;
822 * Follow the driver's regulatory domain, if present, unless a country
823 * IE has been processed or a user wants to help complaince further
825 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
826 last_request->initiator != REGDOM_SET_BY_USER &&
833 for (i = 0; i < regd->n_reg_rules; i++) {
834 const struct ieee80211_reg_rule *rr;
835 const struct ieee80211_freq_range *fr = NULL;
836 const struct ieee80211_power_rule *pr = NULL;
838 rr = ®d->reg_rules[i];
839 fr = &rr->freq_range;
840 pr = &rr->power_rule;
843 * We only need to know if one frequency rule was
844 * was in center_freq's band, that's enough, so lets
845 * not overwrite it once found
847 if (!band_rule_found)
848 band_rule_found = freq_in_rule_band(fr, center_freq);
850 max_bandwidth = freq_max_bandwidth(fr, center_freq);
852 if (max_bandwidth && *bandwidth <= max_bandwidth) {
854 *bandwidth = max_bandwidth;
859 if (!band_rule_found)
862 return !max_bandwidth;
864 EXPORT_SYMBOL(freq_reg_info);
866 int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
867 const struct ieee80211_reg_rule **reg_rule)
869 return freq_reg_info_regd(wiphy, center_freq,
870 bandwidth, reg_rule, NULL);
873 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
874 unsigned int chan_idx)
878 u32 max_bandwidth = 0;
879 const struct ieee80211_reg_rule *reg_rule = NULL;
880 const struct ieee80211_power_rule *power_rule = NULL;
881 struct ieee80211_supported_band *sband;
882 struct ieee80211_channel *chan;
883 struct wiphy *request_wiphy = NULL;
885 assert_cfg80211_lock();
887 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
889 sband = wiphy->bands[band];
890 BUG_ON(chan_idx >= sband->n_channels);
891 chan = &sband->channels[chan_idx];
893 flags = chan->orig_flags;
895 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
896 &max_bandwidth, ®_rule);
900 * This means no regulatory rule was found in the country IE
901 * with a frequency range on the center_freq's band, since
902 * IEEE-802.11 allows for a country IE to have a subset of the
903 * regulatory information provided in a country we ignore
904 * disabling the channel unless at least one reg rule was
905 * found on the center_freq's band. For details see this
908 * http://tinyurl.com/11d-clarification
911 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
912 #ifdef CONFIG_CFG80211_REG_DEBUG
913 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
914 "intact on %s - no rule found in band on "
916 chan->center_freq, wiphy_name(wiphy));
920 * In this case we know the country IE has at least one reg rule
921 * for the band so we respect its band definitions
923 #ifdef CONFIG_CFG80211_REG_DEBUG
924 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
925 printk(KERN_DEBUG "cfg80211: Disabling "
926 "channel %d MHz on %s due to "
928 chan->center_freq, wiphy_name(wiphy));
930 flags |= IEEE80211_CHAN_DISABLED;
936 power_rule = ®_rule->power_rule;
938 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
939 request_wiphy && request_wiphy == wiphy &&
940 request_wiphy->strict_regulatory) {
942 * This gaurantees the driver's requested regulatory domain
943 * will always be used as a base for further regulatory
946 chan->flags = chan->orig_flags =
947 map_regdom_flags(reg_rule->flags);
948 chan->max_antenna_gain = chan->orig_mag =
949 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
950 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
951 chan->max_power = chan->orig_mpwr =
952 (int) MBM_TO_DBM(power_rule->max_eirp);
956 chan->flags = flags | map_regdom_flags(reg_rule->flags);
957 chan->max_antenna_gain = min(chan->orig_mag,
958 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
959 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
961 chan->max_power = min(chan->orig_mpwr,
962 (int) MBM_TO_DBM(power_rule->max_eirp));
964 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
967 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
970 struct ieee80211_supported_band *sband;
972 BUG_ON(!wiphy->bands[band]);
973 sband = wiphy->bands[band];
975 for (i = 0; i < sband->n_channels; i++)
976 handle_channel(wiphy, band, i);
979 static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
983 if (setby == REGDOM_SET_BY_CORE &&
984 wiphy->custom_regulatory)
987 * wiphy->regd will be set once the device has its own
988 * desired regulatory domain set
990 if (wiphy->strict_regulatory && !wiphy->regd &&
991 !is_world_regdom(last_request->alpha2))
996 static void update_all_wiphy_regulatory(enum reg_set_by setby)
998 struct cfg80211_registered_device *drv;
1000 list_for_each_entry(drv, &cfg80211_drv_list, list)
1001 wiphy_update_regulatory(&drv->wiphy, setby);
1004 void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
1006 enum ieee80211_band band;
1008 if (ignore_reg_update(wiphy, setby))
1010 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1011 if (wiphy->bands[band])
1012 handle_band(wiphy, band);
1014 if (wiphy->reg_notifier)
1015 wiphy->reg_notifier(wiphy, last_request);
1018 static void handle_channel_custom(struct wiphy *wiphy,
1019 enum ieee80211_band band,
1020 unsigned int chan_idx,
1021 const struct ieee80211_regdomain *regd)
1024 u32 max_bandwidth = 0;
1025 const struct ieee80211_reg_rule *reg_rule = NULL;
1026 const struct ieee80211_power_rule *power_rule = NULL;
1027 struct ieee80211_supported_band *sband;
1028 struct ieee80211_channel *chan;
1030 sband = wiphy->bands[band];
1031 BUG_ON(chan_idx >= sband->n_channels);
1032 chan = &sband->channels[chan_idx];
1034 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1035 &max_bandwidth, ®_rule, regd);
1038 chan->flags = IEEE80211_CHAN_DISABLED;
1042 power_rule = ®_rule->power_rule;
1044 chan->flags |= map_regdom_flags(reg_rule->flags);
1045 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1046 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1047 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1050 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1051 const struct ieee80211_regdomain *regd)
1054 struct ieee80211_supported_band *sband;
1056 BUG_ON(!wiphy->bands[band]);
1057 sband = wiphy->bands[band];
1059 for (i = 0; i < sband->n_channels; i++)
1060 handle_channel_custom(wiphy, band, i, regd);
1063 /* Used by drivers prior to wiphy registration */
1064 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1065 const struct ieee80211_regdomain *regd)
1067 enum ieee80211_band band;
1068 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1069 if (wiphy->bands[band])
1070 handle_band_custom(wiphy, band, regd);
1073 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1075 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1076 const struct ieee80211_regdomain *src_regd)
1078 struct ieee80211_regdomain *regd;
1079 int size_of_regd = 0;
1082 size_of_regd = sizeof(struct ieee80211_regdomain) +
1083 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1085 regd = kzalloc(size_of_regd, GFP_KERNEL);
1089 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1091 for (i = 0; i < src_regd->n_reg_rules; i++)
1092 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i],
1093 sizeof(struct ieee80211_reg_rule));
1100 * Return value which can be used by ignore_request() to indicate
1101 * it has been determined we should intersect two regulatory domains
1103 #define REG_INTERSECT 1
1105 /* This has the logic which determines when a new request
1106 * should be ignored. */
1107 static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
1110 struct wiphy *last_wiphy = NULL;
1112 assert_cfg80211_lock();
1114 /* All initial requests are respected */
1119 case REGDOM_SET_BY_INIT:
1121 case REGDOM_SET_BY_CORE:
1123 case REGDOM_SET_BY_COUNTRY_IE:
1125 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1127 if (unlikely(!is_an_alpha2(alpha2)))
1129 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1130 if (last_wiphy != wiphy) {
1132 * Two cards with two APs claiming different
1133 * different Country IE alpha2s. We could
1134 * intersect them, but that seems unlikely
1135 * to be correct. Reject second one for now.
1137 if (regdom_changes(alpha2))
1142 * Two consecutive Country IE hints on the same wiphy.
1143 * This should be picked up early by the driver/stack
1145 if (WARN_ON(regdom_changes(alpha2)))
1149 return REG_INTERSECT;
1150 case REGDOM_SET_BY_DRIVER:
1151 if (last_request->initiator == REGDOM_SET_BY_CORE) {
1152 if (is_old_static_regdom(cfg80211_regdomain))
1154 if (regdom_changes(alpha2))
1160 * This would happen if you unplug and plug your card
1161 * back in or if you add a new device for which the previously
1162 * loaded card also agrees on the regulatory domain.
1164 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
1165 !regdom_changes(alpha2))
1168 return REG_INTERSECT;
1169 case REGDOM_SET_BY_USER:
1170 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
1171 return REG_INTERSECT;
1173 * If the user knows better the user should set the regdom
1174 * to their country before the IE is picked up
1176 if (last_request->initiator == REGDOM_SET_BY_USER &&
1177 last_request->intersect)
1180 * Process user requests only after previous user/driver/core
1181 * requests have been processed
1183 if (last_request->initiator == REGDOM_SET_BY_CORE ||
1184 last_request->initiator == REGDOM_SET_BY_DRIVER ||
1185 last_request->initiator == REGDOM_SET_BY_USER) {
1186 if (regdom_changes(last_request->alpha2))
1190 if (!is_old_static_regdom(cfg80211_regdomain) &&
1191 !regdom_changes(alpha2))
1200 /* Caller must hold &cfg80211_mutex */
1201 int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
1203 u32 country_ie_checksum,
1204 enum environment_cap env)
1206 struct regulatory_request *request;
1207 bool intersect = false;
1210 assert_cfg80211_lock();
1212 r = ignore_request(wiphy, set_by, alpha2);
1214 if (r == REG_INTERSECT) {
1215 if (set_by == REGDOM_SET_BY_DRIVER) {
1216 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1223 * If the regulatory domain being requested by the
1224 * driver has already been set just copy it to the
1227 if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
1228 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1238 request = kzalloc(sizeof(struct regulatory_request),
1243 request->alpha2[0] = alpha2[0];
1244 request->alpha2[1] = alpha2[1];
1245 request->initiator = set_by;
1246 request->wiphy_idx = get_wiphy_idx(wiphy);
1247 request->intersect = intersect;
1248 request->country_ie_checksum = country_ie_checksum;
1249 request->country_ie_env = env;
1251 kfree(last_request);
1252 last_request = request;
1254 /* When r == REG_INTERSECT we do need to call CRDA */
1259 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1260 * AND if CRDA is NOT present nothing will happen, if someone
1261 * wants to bother with 11d with OLD_REG you can add a timer.
1262 * If after x amount of time nothing happens you can call:
1264 * return set_regdom(country_ie_regdomain);
1266 * to intersect with the static rd
1268 return call_crda(alpha2);
1271 /* This currently only processes user and driver regulatory hints */
1272 static int reg_process_hint(struct regulatory_request *reg_request)
1275 struct wiphy *wiphy = NULL;
1277 BUG_ON(!reg_request->alpha2);
1279 mutex_lock(&cfg80211_mutex);
1281 if (wiphy_idx_valid(reg_request->wiphy_idx))
1282 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1284 if (reg_request->initiator == REGDOM_SET_BY_DRIVER &&
1290 r = __regulatory_hint(wiphy,
1291 reg_request->initiator,
1292 reg_request->alpha2,
1293 reg_request->country_ie_checksum,
1294 reg_request->country_ie_env);
1295 /* This is required so that the orig_* parameters are saved */
1296 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1297 wiphy_update_regulatory(wiphy, reg_request->initiator);
1299 mutex_unlock(&cfg80211_mutex);
1307 static void reg_process_pending_hints(void)
1309 struct regulatory_request *reg_request;
1312 spin_lock(®_requests_lock);
1313 while (!list_empty(®_requests_list)) {
1314 reg_request = list_first_entry(®_requests_list,
1315 struct regulatory_request,
1317 list_del_init(®_request->list);
1318 spin_unlock(®_requests_lock);
1320 r = reg_process_hint(reg_request);
1321 #ifdef CONFIG_CFG80211_REG_DEBUG
1322 if (r && (reg_request->initiator == REGDOM_SET_BY_DRIVER ||
1323 reg_request->initiator == REGDOM_SET_BY_COUNTRY_IE))
1324 printk(KERN_ERR "cfg80211: wiphy_idx %d sent a "
1325 "regulatory hint for %c%c but now has "
1326 "gone fishing, ignoring request\n",
1327 reg_request->wiphy_idx,
1328 reg_request->alpha2[0],
1329 reg_request->alpha2[1]);
1332 spin_lock(®_requests_lock);
1334 spin_unlock(®_requests_lock);
1337 static void reg_todo(struct work_struct *work)
1339 reg_process_pending_hints();
1342 static DECLARE_WORK(reg_work, reg_todo);
1344 static void queue_regulatory_request(struct regulatory_request *request)
1346 spin_lock(®_requests_lock);
1347 list_add_tail(&request->list, ®_requests_list);
1348 spin_unlock(®_requests_lock);
1350 schedule_work(®_work);
1353 /* Core regulatory hint -- happens once during cfg80211_init() */
1354 static int regulatory_hint_core(const char *alpha2)
1356 struct regulatory_request *request;
1358 BUG_ON(last_request);
1360 request = kzalloc(sizeof(struct regulatory_request),
1365 request->alpha2[0] = alpha2[0];
1366 request->alpha2[1] = alpha2[1];
1367 request->initiator = REGDOM_SET_BY_CORE;
1369 queue_regulatory_request(request);
1375 int regulatory_hint_user(const char *alpha2)
1377 struct regulatory_request *request;
1381 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1385 request->wiphy_idx = WIPHY_IDX_STALE;
1386 request->alpha2[0] = alpha2[0];
1387 request->alpha2[1] = alpha2[1];
1388 request->initiator = REGDOM_SET_BY_USER,
1390 queue_regulatory_request(request);
1396 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1398 struct regulatory_request *request;
1403 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1407 request->wiphy_idx = get_wiphy_idx(wiphy);
1409 /* Must have registered wiphy first */
1410 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1412 request->alpha2[0] = alpha2[0];
1413 request->alpha2[1] = alpha2[1];
1414 request->initiator = REGDOM_SET_BY_DRIVER;
1416 queue_regulatory_request(request);
1420 EXPORT_SYMBOL(regulatory_hint);
1422 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1423 u32 country_ie_checksum)
1425 struct wiphy *request_wiphy;
1427 assert_cfg80211_lock();
1429 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1434 if (likely(request_wiphy != wiphy))
1435 return !country_ie_integrity_changes(country_ie_checksum);
1437 * We should not have let these through at this point, they
1438 * should have been picked up earlier by the first alpha2 check
1441 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1446 void regulatory_hint_11d(struct wiphy *wiphy,
1450 struct ieee80211_regdomain *rd = NULL;
1453 enum environment_cap env = ENVIRON_ANY;
1454 struct regulatory_request *request;
1456 mutex_lock(&cfg80211_mutex);
1458 if (unlikely(!last_request)) {
1459 mutex_unlock(&cfg80211_mutex);
1463 /* IE len must be evenly divisible by 2 */
1464 if (country_ie_len & 0x01)
1467 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1471 * Pending country IE processing, this can happen after we
1472 * call CRDA and wait for a response if a beacon was received before
1473 * we were able to process the last regulatory_hint_11d() call
1475 if (country_ie_regdomain)
1478 alpha2[0] = country_ie[0];
1479 alpha2[1] = country_ie[1];
1481 if (country_ie[2] == 'I')
1482 env = ENVIRON_INDOOR;
1483 else if (country_ie[2] == 'O')
1484 env = ENVIRON_OUTDOOR;
1487 * We will run this for *every* beacon processed for the BSSID, so
1488 * we optimize an early check to exit out early if we don't have to
1491 if (likely(wiphy_idx_valid(last_request->wiphy_idx))) {
1492 struct cfg80211_registered_device *drv_last_ie;
1495 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
1498 * Lets keep this simple -- we trust the first AP
1499 * after we intersect with CRDA
1501 if (likely(&drv_last_ie->wiphy == wiphy)) {
1503 * Ignore IEs coming in on this wiphy with
1504 * the same alpha2 and environment cap
1506 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1508 env == drv_last_ie->env)) {
1512 * the wiphy moved on to another BSSID or the AP
1513 * was reconfigured. XXX: We need to deal with the
1514 * case where the user suspends and goes to goes
1515 * to another country, and then gets IEs from an
1516 * AP with different settings
1521 * Ignore IEs coming in on two separate wiphys with
1522 * the same alpha2 and environment cap
1524 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1526 env == drv_last_ie->env)) {
1529 /* We could potentially intersect though */
1534 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1539 * This will not happen right now but we leave it here for the
1540 * the future when we want to add suspend/resume support and having
1541 * the user move to another country after doing so, or having the user
1542 * move to another AP. Right now we just trust the first AP.
1544 * If we hit this before we add this support we want to be informed of
1545 * it as it would indicate a mistake in the current design
1547 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
1550 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1555 * We keep this around for when CRDA comes back with a response so
1556 * we can intersect with that
1558 country_ie_regdomain = rd;
1560 request->wiphy_idx = get_wiphy_idx(wiphy);
1561 request->alpha2[0] = rd->alpha2[0];
1562 request->alpha2[1] = rd->alpha2[1];
1563 request->initiator = REGDOM_SET_BY_COUNTRY_IE;
1564 request->country_ie_checksum = checksum;
1565 request->country_ie_env = env;
1567 mutex_unlock(&cfg80211_mutex);
1569 queue_regulatory_request(request);
1576 mutex_unlock(&cfg80211_mutex);
1578 EXPORT_SYMBOL(regulatory_hint_11d);
1580 static void print_rd_rules(const struct ieee80211_regdomain *rd)
1583 const struct ieee80211_reg_rule *reg_rule = NULL;
1584 const struct ieee80211_freq_range *freq_range = NULL;
1585 const struct ieee80211_power_rule *power_rule = NULL;
1587 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1588 "(max_antenna_gain, max_eirp)\n");
1590 for (i = 0; i < rd->n_reg_rules; i++) {
1591 reg_rule = &rd->reg_rules[i];
1592 freq_range = ®_rule->freq_range;
1593 power_rule = ®_rule->power_rule;
1596 * There may not be documentation for max antenna gain
1597 * in certain regions
1599 if (power_rule->max_antenna_gain)
1600 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1601 "(%d mBi, %d mBm)\n",
1602 freq_range->start_freq_khz,
1603 freq_range->end_freq_khz,
1604 freq_range->max_bandwidth_khz,
1605 power_rule->max_antenna_gain,
1606 power_rule->max_eirp);
1608 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1610 freq_range->start_freq_khz,
1611 freq_range->end_freq_khz,
1612 freq_range->max_bandwidth_khz,
1613 power_rule->max_eirp);
1617 static void print_regdomain(const struct ieee80211_regdomain *rd)
1620 if (is_intersected_alpha2(rd->alpha2)) {
1622 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1623 struct cfg80211_registered_device *drv;
1624 drv = cfg80211_drv_by_wiphy_idx(
1625 last_request->wiphy_idx);
1627 printk(KERN_INFO "cfg80211: Current regulatory "
1628 "domain updated by AP to: %c%c\n",
1629 drv->country_ie_alpha2[0],
1630 drv->country_ie_alpha2[1]);
1632 printk(KERN_INFO "cfg80211: Current regulatory "
1633 "domain intersected: \n");
1635 printk(KERN_INFO "cfg80211: Current regulatory "
1636 "domain intersected: \n");
1637 } else if (is_world_regdom(rd->alpha2))
1638 printk(KERN_INFO "cfg80211: World regulatory "
1639 "domain updated:\n");
1641 if (is_unknown_alpha2(rd->alpha2))
1642 printk(KERN_INFO "cfg80211: Regulatory domain "
1643 "changed to driver built-in settings "
1644 "(unknown country)\n");
1646 printk(KERN_INFO "cfg80211: Regulatory domain "
1647 "changed to country: %c%c\n",
1648 rd->alpha2[0], rd->alpha2[1]);
1653 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
1655 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1656 rd->alpha2[0], rd->alpha2[1]);
1660 #ifdef CONFIG_CFG80211_REG_DEBUG
1661 static void reg_country_ie_process_debug(
1662 const struct ieee80211_regdomain *rd,
1663 const struct ieee80211_regdomain *country_ie_regdomain,
1664 const struct ieee80211_regdomain *intersected_rd)
1666 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1667 print_regdomain_info(country_ie_regdomain);
1668 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1669 print_regdomain_info(rd);
1670 if (intersected_rd) {
1671 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1673 print_regdomain_info(intersected_rd);
1676 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1679 static inline void reg_country_ie_process_debug(
1680 const struct ieee80211_regdomain *rd,
1681 const struct ieee80211_regdomain *country_ie_regdomain,
1682 const struct ieee80211_regdomain *intersected_rd)
1687 /* Takes ownership of rd only if it doesn't fail */
1688 static int __set_regdom(const struct ieee80211_regdomain *rd)
1690 const struct ieee80211_regdomain *intersected_rd = NULL;
1691 struct cfg80211_registered_device *drv = NULL;
1692 struct wiphy *request_wiphy;
1693 /* Some basic sanity checks first */
1695 if (is_world_regdom(rd->alpha2)) {
1696 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1698 update_world_regdomain(rd);
1702 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1703 !is_unknown_alpha2(rd->alpha2))
1710 * Lets only bother proceeding on the same alpha2 if the current
1711 * rd is non static (it means CRDA was present and was used last)
1712 * and the pending request came in from a country IE
1714 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1716 * If someone else asked us to change the rd lets only bother
1717 * checking if the alpha2 changes if CRDA was already called
1719 if (!is_old_static_regdom(cfg80211_regdomain) &&
1720 !regdom_changes(rd->alpha2))
1725 * Now lets set the regulatory domain, update all driver channels
1726 * and finally inform them of what we have done, in case they want
1727 * to review or adjust their own settings based on their own
1728 * internal EEPROM data
1731 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1734 if (!is_valid_rd(rd)) {
1735 printk(KERN_ERR "cfg80211: Invalid "
1736 "regulatory domain detected:\n");
1737 print_regdomain_info(rd);
1741 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1743 if (!last_request->intersect) {
1746 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1748 cfg80211_regdomain = rd;
1753 * For a driver hint, lets copy the regulatory domain the
1754 * driver wanted to the wiphy to deal with conflicts
1757 BUG_ON(request_wiphy->regd);
1759 r = reg_copy_regd(&request_wiphy->regd, rd);
1764 cfg80211_regdomain = rd;
1768 /* Intersection requires a bit more work */
1770 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1772 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1773 if (!intersected_rd)
1777 * We can trash what CRDA provided now.
1778 * However if a driver requested this specific regulatory
1779 * domain we keep it for its private use
1781 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
1782 request_wiphy->regd = rd;
1789 cfg80211_regdomain = intersected_rd;
1795 * Country IE requests are handled a bit differently, we intersect
1796 * the country IE rd with what CRDA believes that country should have
1799 BUG_ON(!country_ie_regdomain);
1801 if (rd != country_ie_regdomain) {
1803 * Intersect what CRDA returned and our what we
1804 * had built from the Country IE received
1807 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1809 reg_country_ie_process_debug(rd, country_ie_regdomain,
1812 kfree(country_ie_regdomain);
1813 country_ie_regdomain = NULL;
1816 * This would happen when CRDA was not present and
1817 * OLD_REGULATORY was enabled. We intersect our Country
1818 * IE rd and what was set on cfg80211 originally
1820 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1823 if (!intersected_rd)
1826 drv = wiphy_to_dev(request_wiphy);
1828 drv->country_ie_alpha2[0] = rd->alpha2[0];
1829 drv->country_ie_alpha2[1] = rd->alpha2[1];
1830 drv->env = last_request->country_ie_env;
1832 BUG_ON(intersected_rd == rd);
1838 cfg80211_regdomain = intersected_rd;
1845 * Use this call to set the current regulatory domain. Conflicts with
1846 * multiple drivers can be ironed out later. Caller must've already
1847 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
1849 int set_regdom(const struct ieee80211_regdomain *rd)
1853 assert_cfg80211_lock();
1855 /* Note that this doesn't update the wiphys, this is done below */
1856 r = __set_regdom(rd);
1862 /* This would make this whole thing pointless */
1863 if (!last_request->intersect)
1864 BUG_ON(rd != cfg80211_regdomain);
1866 /* update all wiphys now with the new established regulatory domain */
1867 update_all_wiphy_regulatory(last_request->initiator);
1869 print_regdomain(cfg80211_regdomain);
1874 /* Caller must hold cfg80211_mutex */
1875 void reg_device_remove(struct wiphy *wiphy)
1877 struct wiphy *request_wiphy;
1879 assert_cfg80211_lock();
1881 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1884 if (!last_request || !request_wiphy)
1886 if (request_wiphy != wiphy)
1888 last_request->wiphy_idx = WIPHY_IDX_STALE;
1889 last_request->country_ie_env = ENVIRON_ANY;
1892 int regulatory_init(void)
1896 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1897 if (IS_ERR(reg_pdev))
1898 return PTR_ERR(reg_pdev);
1900 spin_lock_init(®_requests_lock);
1902 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
1903 cfg80211_regdomain = static_regdom(ieee80211_regdom);
1905 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
1906 print_regdomain_info(cfg80211_regdomain);
1908 * The old code still requests for a new regdomain and if
1909 * you have CRDA you get it updated, otherwise you get
1910 * stuck with the static values. We ignore "EU" code as
1911 * that is not a valid ISO / IEC 3166 alpha2
1913 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
1914 err = regulatory_hint_core(ieee80211_regdom);
1916 cfg80211_regdomain = cfg80211_world_regdom;
1918 err = regulatory_hint_core("00");
1924 * N.B. kobject_uevent_env() can fail mainly for when we're out
1925 * memory which is handled and propagated appropriately above
1926 * but it can also fail during a netlink_broadcast() or during
1927 * early boot for call_usermodehelper(). For now treat these
1928 * errors as non-fatal.
1930 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
1931 "to call CRDA during init");
1932 #ifdef CONFIG_CFG80211_REG_DEBUG
1933 /* We want to find out exactly why when debugging */
1941 void regulatory_exit(void)
1943 struct regulatory_request *reg_request, *tmp;
1945 cancel_work_sync(®_work);
1947 mutex_lock(&cfg80211_mutex);
1951 kfree(country_ie_regdomain);
1952 country_ie_regdomain = NULL;
1954 kfree(last_request);
1956 platform_device_unregister(reg_pdev);
1958 spin_lock(®_requests_lock);
1959 if (!list_empty(®_requests_list)) {
1960 list_for_each_entry_safe(reg_request, tmp,
1961 ®_requests_list, list) {
1962 list_del(®_request->list);
1966 spin_unlock(®_requests_lock);
1968 mutex_unlock(&cfg80211_mutex);