aboutsummaryrefslogtreecommitdiffstats
path: root/net/wireless/reg.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/wireless/reg.c')
-rw-r--r--net/wireless/reg.c1255
1 files changed, 981 insertions, 274 deletions
diff --git a/net/wireless/reg.c b/net/wireless/reg.c
index bd0a16c3de5e..6327e1617acb 100644
--- a/net/wireless/reg.c
+++ b/net/wireless/reg.c
@@ -41,38 +41,7 @@
41#include <net/cfg80211.h> 41#include <net/cfg80211.h>
42#include "core.h" 42#include "core.h"
43#include "reg.h" 43#include "reg.h"
44 44#include "nl80211.h"
45/**
46 * struct regulatory_request - receipt of last regulatory request
47 *
48 * @wiphy: this is set if this request's initiator is
49 * %REGDOM_SET_BY_COUNTRY_IE or %REGDOM_SET_BY_DRIVER. This
50 * can be used by the wireless core to deal with conflicts
51 * and potentially inform users of which devices specifically
52 * cased the conflicts.
53 * @initiator: indicates who sent this request, could be any of
54 * of those set in reg_set_by, %REGDOM_SET_BY_*
55 * @alpha2: the ISO / IEC 3166 alpha2 country code of the requested
56 * regulatory domain. We have a few special codes:
57 * 00 - World regulatory domain
58 * 99 - built by driver but a specific alpha2 cannot be determined
59 * 98 - result of an intersection between two regulatory domains
60 * @intersect: indicates whether the wireless core should intersect
61 * the requested regulatory domain with the presently set regulatory
62 * domain.
63 * @country_ie_checksum: checksum of the last processed and accepted
64 * country IE
65 * @country_ie_env: lets us know if the AP is telling us we are outdoor,
66 * indoor, or if it doesn't matter
67 */
68struct regulatory_request {
69 struct wiphy *wiphy;
70 enum reg_set_by initiator;
71 char alpha2[2];
72 bool intersect;
73 u32 country_ie_checksum;
74 enum environment_cap country_ie_env;
75};
76 45
77/* Receipt of information from last regulatory request */ 46/* Receipt of information from last regulatory request */
78static struct regulatory_request *last_request; 47static struct regulatory_request *last_request;
@@ -86,22 +55,63 @@ static u32 supported_bandwidths[] = {
86 MHZ_TO_KHZ(20), 55 MHZ_TO_KHZ(20),
87}; 56};
88 57
89/* Central wireless core regulatory domains, we only need two, 58/*
59 * Central wireless core regulatory domains, we only need two,
90 * the current one and a world regulatory domain in case we have no 60 * the current one and a world regulatory domain in case we have no
91 * information to give us an alpha2 */ 61 * information to give us an alpha2
92static const struct ieee80211_regdomain *cfg80211_regdomain; 62 */
63const struct ieee80211_regdomain *cfg80211_regdomain;
93 64
94/* We use this as a place for the rd structure built from the 65/*
66 * We use this as a place for the rd structure built from the
95 * last parsed country IE to rest until CRDA gets back to us with 67 * last parsed country IE to rest until CRDA gets back to us with
96 * what it thinks should apply for the same country */ 68 * what it thinks should apply for the same country
69 */
97static const struct ieee80211_regdomain *country_ie_regdomain; 70static const struct ieee80211_regdomain *country_ie_regdomain;
98 71
72/* Used to queue up regulatory hints */
73static LIST_HEAD(reg_requests_list);
74static spinlock_t reg_requests_lock;
75
76/* Used to queue up beacon hints for review */
77static LIST_HEAD(reg_pending_beacons);
78static spinlock_t reg_pending_beacons_lock;
79
80/* Used to keep track of processed beacon hints */
81static LIST_HEAD(reg_beacon_list);
82
83struct reg_beacon {
84 struct list_head list;
85 struct ieee80211_channel chan;
86};
87
99/* We keep a static world regulatory domain in case of the absence of CRDA */ 88/* We keep a static world regulatory domain in case of the absence of CRDA */
100static const struct ieee80211_regdomain world_regdom = { 89static const struct ieee80211_regdomain world_regdom = {
101 .n_reg_rules = 1, 90 .n_reg_rules = 5,
102 .alpha2 = "00", 91 .alpha2 = "00",
103 .reg_rules = { 92 .reg_rules = {
104 REG_RULE(2412-10, 2462+10, 40, 6, 20, 93 /* IEEE 802.11b/g, channels 1..11 */
94 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
95 /* IEEE 802.11b/g, channels 12..13. No HT40
96 * channel fits here. */
97 REG_RULE(2467-10, 2472+10, 20, 6, 20,
98 NL80211_RRF_PASSIVE_SCAN |
99 NL80211_RRF_NO_IBSS),
100 /* IEEE 802.11 channel 14 - Only JP enables
101 * this and for 802.11b only */
102 REG_RULE(2484-10, 2484+10, 20, 6, 20,
103 NL80211_RRF_PASSIVE_SCAN |
104 NL80211_RRF_NO_IBSS |
105 NL80211_RRF_NO_OFDM),
106 /* IEEE 802.11a, channel 36..48 */
107 REG_RULE(5180-10, 5240+10, 40, 6, 20,
108 NL80211_RRF_PASSIVE_SCAN |
109 NL80211_RRF_NO_IBSS),
110
111 /* NB: 5260 MHz - 5700 MHz requies DFS */
112
113 /* IEEE 802.11a, channel 149..165 */
114 REG_RULE(5745-10, 5825+10, 40, 6, 20,
105 NL80211_RRF_PASSIVE_SCAN | 115 NL80211_RRF_PASSIVE_SCAN |
106 NL80211_RRF_NO_IBSS), 116 NL80211_RRF_NO_IBSS),
107 } 117 }
@@ -112,12 +122,19 @@ static const struct ieee80211_regdomain *cfg80211_world_regdom =
112 122
113#ifdef CONFIG_WIRELESS_OLD_REGULATORY 123#ifdef CONFIG_WIRELESS_OLD_REGULATORY
114static char *ieee80211_regdom = "US"; 124static char *ieee80211_regdom = "US";
125#else
126static char *ieee80211_regdom = "00";
127#endif
128
115module_param(ieee80211_regdom, charp, 0444); 129module_param(ieee80211_regdom, charp, 0444);
116MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); 130MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
117 131
118/* We assume 40 MHz bandwidth for the old regulatory work. 132#ifdef CONFIG_WIRELESS_OLD_REGULATORY
133/*
134 * We assume 40 MHz bandwidth for the old regulatory work.
119 * We make emphasis we are using the exact same frequencies 135 * We make emphasis we are using the exact same frequencies
120 * as before */ 136 * as before
137 */
121 138
122static const struct ieee80211_regdomain us_regdom = { 139static const struct ieee80211_regdomain us_regdom = {
123 .n_reg_rules = 6, 140 .n_reg_rules = 6,
@@ -156,8 +173,10 @@ static const struct ieee80211_regdomain jp_regdom = {
156 173
157static const struct ieee80211_regdomain eu_regdom = { 174static const struct ieee80211_regdomain eu_regdom = {
158 .n_reg_rules = 6, 175 .n_reg_rules = 6,
159 /* This alpha2 is bogus, we leave it here just for stupid 176 /*
160 * backward compatibility */ 177 * This alpha2 is bogus, we leave it here just for stupid
178 * backward compatibility
179 */
161 .alpha2 = "EU", 180 .alpha2 = "EU",
162 .reg_rules = { 181 .reg_rules = {
163 /* IEEE 802.11b/g, channels 1..13 */ 182 /* IEEE 802.11b/g, channels 1..13 */
@@ -226,8 +245,10 @@ static void reset_regdomains(void)
226 cfg80211_regdomain = NULL; 245 cfg80211_regdomain = NULL;
227} 246}
228 247
229/* Dynamic world regulatory domain requested by the wireless 248/*
230 * core upon initialization */ 249 * Dynamic world regulatory domain requested by the wireless
250 * core upon initialization
251 */
231static void update_world_regdomain(const struct ieee80211_regdomain *rd) 252static void update_world_regdomain(const struct ieee80211_regdomain *rd)
232{ 253{
233 BUG_ON(!last_request); 254 BUG_ON(!last_request);
@@ -268,8 +289,10 @@ static bool is_unknown_alpha2(const char *alpha2)
268{ 289{
269 if (!alpha2) 290 if (!alpha2)
270 return false; 291 return false;
271 /* Special case where regulatory domain was built by driver 292 /*
272 * but a specific alpha2 cannot be determined */ 293 * Special case where regulatory domain was built by driver
294 * but a specific alpha2 cannot be determined
295 */
273 if (alpha2[0] == '9' && alpha2[1] == '9') 296 if (alpha2[0] == '9' && alpha2[1] == '9')
274 return true; 297 return true;
275 return false; 298 return false;
@@ -279,9 +302,11 @@ static bool is_intersected_alpha2(const char *alpha2)
279{ 302{
280 if (!alpha2) 303 if (!alpha2)
281 return false; 304 return false;
282 /* Special case where regulatory domain is the 305 /*
306 * Special case where regulatory domain is the
283 * result of an intersection between two regulatory domain 307 * result of an intersection between two regulatory domain
284 * structures */ 308 * structures
309 */
285 if (alpha2[0] == '9' && alpha2[1] == '8') 310 if (alpha2[0] == '9' && alpha2[1] == '8')
286 return true; 311 return true;
287 return false; 312 return false;
@@ -306,8 +331,10 @@ static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
306 return false; 331 return false;
307} 332}
308 333
309static bool regdom_changed(const char *alpha2) 334static bool regdom_changes(const char *alpha2)
310{ 335{
336 assert_cfg80211_lock();
337
311 if (!cfg80211_regdomain) 338 if (!cfg80211_regdomain)
312 return true; 339 return true;
313 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2)) 340 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
@@ -334,8 +361,10 @@ static bool country_ie_integrity_changes(u32 checksum)
334 return false; 361 return false;
335} 362}
336 363
337/* This lets us keep regulatory code which is updated on a regulatory 364/*
338 * basis in userspace. */ 365 * This lets us keep regulatory code which is updated on a regulatory
366 * basis in userspace.
367 */
339static int call_crda(const char *alpha2) 368static int call_crda(const char *alpha2)
340{ 369{
341 char country_env[9 + 2] = "COUNTRY="; 370 char country_env[9 + 2] = "COUNTRY=";
@@ -447,10 +476,12 @@ static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
447#undef ONE_GHZ_IN_KHZ 476#undef ONE_GHZ_IN_KHZ
448} 477}
449 478
450/* Converts a country IE to a regulatory domain. A regulatory domain 479/*
480 * Converts a country IE to a regulatory domain. A regulatory domain
451 * structure has a lot of information which the IE doesn't yet have, 481 * structure has a lot of information which the IE doesn't yet have,
452 * so for the other values we use upper max values as we will intersect 482 * so for the other values we use upper max values as we will intersect
453 * with our userspace regulatory agent to get lower bounds. */ 483 * with our userspace regulatory agent to get lower bounds.
484 */
454static struct ieee80211_regdomain *country_ie_2_rd( 485static struct ieee80211_regdomain *country_ie_2_rd(
455 u8 *country_ie, 486 u8 *country_ie,
456 u8 country_ie_len, 487 u8 country_ie_len,
@@ -495,9 +526,11 @@ static struct ieee80211_regdomain *country_ie_2_rd(
495 526
496 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8); 527 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
497 528
498 /* We need to build a reg rule for each triplet, but first we must 529 /*
530 * We need to build a reg rule for each triplet, but first we must
499 * calculate the number of reg rules we will need. We will need one 531 * calculate the number of reg rules we will need. We will need one
500 * for each channel subband */ 532 * for each channel subband
533 */
501 while (country_ie_len >= 3) { 534 while (country_ie_len >= 3) {
502 int end_channel = 0; 535 int end_channel = 0;
503 struct ieee80211_country_ie_triplet *triplet = 536 struct ieee80211_country_ie_triplet *triplet =
@@ -535,9 +568,11 @@ static struct ieee80211_regdomain *country_ie_2_rd(
535 if (cur_sub_max_channel < cur_channel) 568 if (cur_sub_max_channel < cur_channel)
536 return NULL; 569 return NULL;
537 570
538 /* Do not allow overlapping channels. Also channels 571 /*
572 * Do not allow overlapping channels. Also channels
539 * passed in each subband must be monotonically 573 * passed in each subband must be monotonically
540 * increasing */ 574 * increasing
575 */
541 if (last_sub_max_channel) { 576 if (last_sub_max_channel) {
542 if (cur_channel <= last_sub_max_channel) 577 if (cur_channel <= last_sub_max_channel)
543 return NULL; 578 return NULL;
@@ -545,10 +580,12 @@ static struct ieee80211_regdomain *country_ie_2_rd(
545 return NULL; 580 return NULL;
546 } 581 }
547 582
548 /* When dot11RegulatoryClassesRequired is supported 583 /*
584 * When dot11RegulatoryClassesRequired is supported
549 * we can throw ext triplets as part of this soup, 585 * we can throw ext triplets as part of this soup,
550 * for now we don't care when those change as we 586 * for now we don't care when those change as we
551 * don't support them */ 587 * don't support them
588 */
552 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) | 589 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
553 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) | 590 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
554 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24); 591 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
@@ -559,8 +596,10 @@ static struct ieee80211_regdomain *country_ie_2_rd(
559 country_ie_len -= 3; 596 country_ie_len -= 3;
560 num_rules++; 597 num_rules++;
561 598
562 /* Note: this is not a IEEE requirement but 599 /*
563 * simply a memory requirement */ 600 * Note: this is not a IEEE requirement but
601 * simply a memory requirement
602 */
564 if (num_rules > NL80211_MAX_SUPP_REG_RULES) 603 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
565 return NULL; 604 return NULL;
566 } 605 }
@@ -588,8 +627,10 @@ static struct ieee80211_regdomain *country_ie_2_rd(
588 struct ieee80211_freq_range *freq_range = NULL; 627 struct ieee80211_freq_range *freq_range = NULL;
589 struct ieee80211_power_rule *power_rule = NULL; 628 struct ieee80211_power_rule *power_rule = NULL;
590 629
591 /* Must parse if dot11RegulatoryClassesRequired is true, 630 /*
592 * we don't support this yet */ 631 * Must parse if dot11RegulatoryClassesRequired is true,
632 * we don't support this yet
633 */
593 if (triplet->ext.reg_extension_id >= 634 if (triplet->ext.reg_extension_id >=
594 IEEE80211_COUNTRY_EXTENSION_ID) { 635 IEEE80211_COUNTRY_EXTENSION_ID) {
595 country_ie += 3; 636 country_ie += 3;
@@ -611,10 +652,12 @@ static struct ieee80211_regdomain *country_ie_2_rd(
611 end_channel = triplet->chans.first_channel + 652 end_channel = triplet->chans.first_channel +
612 (4 * (triplet->chans.num_channels - 1)); 653 (4 * (triplet->chans.num_channels - 1));
613 654
614 /* The +10 is since the regulatory domain expects 655 /*
656 * The +10 is since the regulatory domain expects
615 * the actual band edge, not the center of freq for 657 * the actual band edge, not the center of freq for
616 * its start and end freqs, assuming 20 MHz bandwidth on 658 * its start and end freqs, assuming 20 MHz bandwidth on
617 * the channels passed */ 659 * the channels passed
660 */
618 freq_range->start_freq_khz = 661 freq_range->start_freq_khz =
619 MHZ_TO_KHZ(ieee80211_channel_to_frequency( 662 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
620 triplet->chans.first_channel) - 10); 663 triplet->chans.first_channel) - 10);
@@ -622,9 +665,11 @@ static struct ieee80211_regdomain *country_ie_2_rd(
622 MHZ_TO_KHZ(ieee80211_channel_to_frequency( 665 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
623 end_channel) + 10); 666 end_channel) + 10);
624 667
625 /* Large arbitrary values, we intersect later */ 668 /*
626 /* Increment this if we ever support >= 40 MHz channels 669 * These are large arbitrary values we use to intersect later.
627 * in IEEE 802.11 */ 670 * Increment this if we ever support >= 40 MHz channels
671 * in IEEE 802.11
672 */
628 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40); 673 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
629 power_rule->max_antenna_gain = DBI_TO_MBI(100); 674 power_rule->max_antenna_gain = DBI_TO_MBI(100);
630 power_rule->max_eirp = DBM_TO_MBM(100); 675 power_rule->max_eirp = DBM_TO_MBM(100);
@@ -640,8 +685,10 @@ static struct ieee80211_regdomain *country_ie_2_rd(
640} 685}
641 686
642 687
643/* Helper for regdom_intersect(), this does the real 688/*
644 * mathematical intersection fun */ 689 * Helper for regdom_intersect(), this does the real
690 * mathematical intersection fun
691 */
645static int reg_rules_intersect( 692static int reg_rules_intersect(
646 const struct ieee80211_reg_rule *rule1, 693 const struct ieee80211_reg_rule *rule1,
647 const struct ieee80211_reg_rule *rule2, 694 const struct ieee80211_reg_rule *rule2,
@@ -719,11 +766,13 @@ static struct ieee80211_regdomain *regdom_intersect(
719 if (!rd1 || !rd2) 766 if (!rd1 || !rd2)
720 return NULL; 767 return NULL;
721 768
722 /* First we get a count of the rules we'll need, then we actually 769 /*
770 * First we get a count of the rules we'll need, then we actually
723 * build them. This is to so we can malloc() and free() a 771 * build them. This is to so we can malloc() and free() a
724 * regdomain once. The reason we use reg_rules_intersect() here 772 * regdomain once. The reason we use reg_rules_intersect() here
725 * is it will return -EINVAL if the rule computed makes no sense. 773 * is it will return -EINVAL if the rule computed makes no sense.
726 * All rules that do check out OK are valid. */ 774 * All rules that do check out OK are valid.
775 */
727 776
728 for (x = 0; x < rd1->n_reg_rules; x++) { 777 for (x = 0; x < rd1->n_reg_rules; x++) {
729 rule1 = &rd1->reg_rules[x]; 778 rule1 = &rd1->reg_rules[x];
@@ -751,14 +800,18 @@ static struct ieee80211_regdomain *regdom_intersect(
751 rule1 = &rd1->reg_rules[x]; 800 rule1 = &rd1->reg_rules[x];
752 for (y = 0; y < rd2->n_reg_rules; y++) { 801 for (y = 0; y < rd2->n_reg_rules; y++) {
753 rule2 = &rd2->reg_rules[y]; 802 rule2 = &rd2->reg_rules[y];
754 /* This time around instead of using the stack lets 803 /*
804 * This time around instead of using the stack lets
755 * write to the target rule directly saving ourselves 805 * write to the target rule directly saving ourselves
756 * a memcpy() */ 806 * a memcpy()
807 */
757 intersected_rule = &rd->reg_rules[rule_idx]; 808 intersected_rule = &rd->reg_rules[rule_idx];
758 r = reg_rules_intersect(rule1, rule2, 809 r = reg_rules_intersect(rule1, rule2,
759 intersected_rule); 810 intersected_rule);
760 /* No need to memset here the intersected rule here as 811 /*
761 * we're not using the stack anymore */ 812 * No need to memset here the intersected rule here as
813 * we're not using the stack anymore
814 */
762 if (r) 815 if (r)
763 continue; 816 continue;
764 rule_idx++; 817 rule_idx++;
@@ -777,8 +830,10 @@ static struct ieee80211_regdomain *regdom_intersect(
777 return rd; 830 return rd;
778} 831}
779 832
780/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may 833/*
781 * want to just have the channel structure use these */ 834 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
835 * want to just have the channel structure use these
836 */
782static u32 map_regdom_flags(u32 rd_flags) 837static u32 map_regdom_flags(u32 rd_flags)
783{ 838{
784 u32 channel_flags = 0; 839 u32 channel_flags = 0;
@@ -791,48 +846,45 @@ static u32 map_regdom_flags(u32 rd_flags)
791 return channel_flags; 846 return channel_flags;
792} 847}
793 848
794/** 849static int freq_reg_info_regd(struct wiphy *wiphy,
795 * freq_reg_info - get regulatory information for the given frequency 850 u32 center_freq,
796 * @center_freq: Frequency in KHz for which we want regulatory information for 851 u32 *bandwidth,
797 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one 852 const struct ieee80211_reg_rule **reg_rule,
798 * you can set this to 0. If this frequency is allowed we then set 853 const struct ieee80211_regdomain *custom_regd)
799 * this value to the maximum allowed bandwidth.
800 * @reg_rule: the regulatory rule which we have for this frequency
801 *
802 * Use this function to get the regulatory rule for a specific frequency on
803 * a given wireless device. If the device has a specific regulatory domain
804 * it wants to follow we respect that unless a country IE has been received
805 * and processed already.
806 *
807 * Returns 0 if it was able to find a valid regulatory rule which does
808 * apply to the given center_freq otherwise it returns non-zero. It will
809 * also return -ERANGE if we determine the given center_freq does not even have
810 * a regulatory rule for a frequency range in the center_freq's band. See
811 * freq_in_rule_band() for our current definition of a band -- this is purely
812 * subjective and right now its 802.11 specific.
813 */
814static int freq_reg_info(u32 center_freq, u32 *bandwidth,
815 const struct ieee80211_reg_rule **reg_rule)
816{ 854{
817 int i; 855 int i;
818 bool band_rule_found = false; 856 bool band_rule_found = false;
857 const struct ieee80211_regdomain *regd;
819 u32 max_bandwidth = 0; 858 u32 max_bandwidth = 0;
820 859
821 if (!cfg80211_regdomain) 860 regd = custom_regd ? custom_regd : cfg80211_regdomain;
861
862 /*
863 * Follow the driver's regulatory domain, if present, unless a country
864 * IE has been processed or a user wants to help complaince further
865 */
866 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
867 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
868 wiphy->regd)
869 regd = wiphy->regd;
870
871 if (!regd)
822 return -EINVAL; 872 return -EINVAL;
823 873
824 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) { 874 for (i = 0; i < regd->n_reg_rules; i++) {
825 const struct ieee80211_reg_rule *rr; 875 const struct ieee80211_reg_rule *rr;
826 const struct ieee80211_freq_range *fr = NULL; 876 const struct ieee80211_freq_range *fr = NULL;
827 const struct ieee80211_power_rule *pr = NULL; 877 const struct ieee80211_power_rule *pr = NULL;
828 878
829 rr = &cfg80211_regdomain->reg_rules[i]; 879 rr = &regd->reg_rules[i];
830 fr = &rr->freq_range; 880 fr = &rr->freq_range;
831 pr = &rr->power_rule; 881 pr = &rr->power_rule;
832 882
833 /* We only need to know if one frequency rule was 883 /*
884 * We only need to know if one frequency rule was
834 * was in center_freq's band, that's enough, so lets 885 * was in center_freq's band, that's enough, so lets
835 * not overwrite it once found */ 886 * not overwrite it once found
887 */
836 if (!band_rule_found) 888 if (!band_rule_found)
837 band_rule_found = freq_in_rule_band(fr, center_freq); 889 band_rule_found = freq_in_rule_band(fr, center_freq);
838 890
@@ -850,6 +902,14 @@ static int freq_reg_info(u32 center_freq, u32 *bandwidth,
850 902
851 return !max_bandwidth; 903 return !max_bandwidth;
852} 904}
905EXPORT_SYMBOL(freq_reg_info);
906
907int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
908 const struct ieee80211_reg_rule **reg_rule)
909{
910 return freq_reg_info_regd(wiphy, center_freq,
911 bandwidth, reg_rule, NULL);
912}
853 913
854static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band, 914static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
855 unsigned int chan_idx) 915 unsigned int chan_idx)
@@ -861,6 +921,11 @@ static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
861 const struct ieee80211_power_rule *power_rule = NULL; 921 const struct ieee80211_power_rule *power_rule = NULL;
862 struct ieee80211_supported_band *sband; 922 struct ieee80211_supported_band *sband;
863 struct ieee80211_channel *chan; 923 struct ieee80211_channel *chan;
924 struct wiphy *request_wiphy = NULL;
925
926 assert_cfg80211_lock();
927
928 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
864 929
865 sband = wiphy->bands[band]; 930 sband = wiphy->bands[band];
866 BUG_ON(chan_idx >= sband->n_channels); 931 BUG_ON(chan_idx >= sband->n_channels);
@@ -868,11 +933,12 @@ static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
868 933
869 flags = chan->orig_flags; 934 flags = chan->orig_flags;
870 935
871 r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq), 936 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
872 &max_bandwidth, &reg_rule); 937 &max_bandwidth, &reg_rule);
873 938
874 if (r) { 939 if (r) {
875 /* This means no regulatory rule was found in the country IE 940 /*
941 * This means no regulatory rule was found in the country IE
876 * with a frequency range on the center_freq's band, since 942 * with a frequency range on the center_freq's band, since
877 * IEEE-802.11 allows for a country IE to have a subset of the 943 * IEEE-802.11 allows for a country IE to have a subset of the
878 * regulatory information provided in a country we ignore 944 * regulatory information provided in a country we ignore
@@ -883,7 +949,8 @@ static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
883 * http://tinyurl.com/11d-clarification 949 * http://tinyurl.com/11d-clarification
884 */ 950 */
885 if (r == -ERANGE && 951 if (r == -ERANGE &&
886 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { 952 last_request->initiator ==
953 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
887#ifdef CONFIG_CFG80211_REG_DEBUG 954#ifdef CONFIG_CFG80211_REG_DEBUG
888 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz " 955 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
889 "intact on %s - no rule found in band on " 956 "intact on %s - no rule found in band on "
@@ -891,10 +958,13 @@ static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
891 chan->center_freq, wiphy_name(wiphy)); 958 chan->center_freq, wiphy_name(wiphy));
892#endif 959#endif
893 } else { 960 } else {
894 /* In this case we know the country IE has at least one reg rule 961 /*
895 * for the band so we respect its band definitions */ 962 * In this case we know the country IE has at least one reg rule
963 * for the band so we respect its band definitions
964 */
896#ifdef CONFIG_CFG80211_REG_DEBUG 965#ifdef CONFIG_CFG80211_REG_DEBUG
897 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) 966 if (last_request->initiator ==
967 NL80211_REGDOM_SET_BY_COUNTRY_IE)
898 printk(KERN_DEBUG "cfg80211: Disabling " 968 printk(KERN_DEBUG "cfg80211: Disabling "
899 "channel %d MHz on %s due to " 969 "channel %d MHz on %s due to "
900 "Country IE\n", 970 "Country IE\n",
@@ -908,6 +978,24 @@ static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
908 978
909 power_rule = &reg_rule->power_rule; 979 power_rule = &reg_rule->power_rule;
910 980
981 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
982 request_wiphy && request_wiphy == wiphy &&
983 request_wiphy->strict_regulatory) {
984 /*
985 * This gaurantees the driver's requested regulatory domain
986 * will always be used as a base for further regulatory
987 * settings
988 */
989 chan->flags = chan->orig_flags =
990 map_regdom_flags(reg_rule->flags);
991 chan->max_antenna_gain = chan->orig_mag =
992 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
993 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
994 chan->max_power = chan->orig_mpwr =
995 (int) MBM_TO_DBM(power_rule->max_eirp);
996 return;
997 }
998
911 chan->flags = flags | map_regdom_flags(reg_rule->flags); 999 chan->flags = flags | map_regdom_flags(reg_rule->flags);
912 chan->max_antenna_gain = min(chan->orig_mag, 1000 chan->max_antenna_gain = min(chan->orig_mag,
913 (int) MBI_TO_DBI(power_rule->max_antenna_gain)); 1001 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
@@ -931,116 +1019,513 @@ static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
931 handle_channel(wiphy, band, i); 1019 handle_channel(wiphy, band, i);
932} 1020}
933 1021
934static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby) 1022static bool ignore_reg_update(struct wiphy *wiphy,
1023 enum nl80211_reg_initiator initiator)
935{ 1024{
936 if (!last_request) 1025 if (!last_request)
937 return true; 1026 return true;
938 if (setby == REGDOM_SET_BY_CORE && 1027 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
939 wiphy->fw_handles_regulatory) 1028 wiphy->custom_regulatory)
1029 return true;
1030 /*
1031 * wiphy->regd will be set once the device has its own
1032 * desired regulatory domain set
1033 */
1034 if (wiphy->strict_regulatory && !wiphy->regd &&
1035 !is_world_regdom(last_request->alpha2))
940 return true; 1036 return true;
941 return false; 1037 return false;
942} 1038}
943 1039
944static void update_all_wiphy_regulatory(enum reg_set_by setby) 1040static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
945{ 1041{
946 struct cfg80211_registered_device *drv; 1042 struct cfg80211_registered_device *drv;
947 1043
948 list_for_each_entry(drv, &cfg80211_drv_list, list) 1044 list_for_each_entry(drv, &cfg80211_drv_list, list)
949 if (!ignore_reg_update(&drv->wiphy, setby)) 1045 wiphy_update_regulatory(&drv->wiphy, initiator);
950 wiphy_update_regulatory(&drv->wiphy, setby); 1046}
1047
1048static void handle_reg_beacon(struct wiphy *wiphy,
1049 unsigned int chan_idx,
1050 struct reg_beacon *reg_beacon)
1051{
1052#ifdef CONFIG_CFG80211_REG_DEBUG
1053#define REG_DEBUG_BEACON_FLAG(desc) \
1054 printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \
1055 "frequency: %d MHz (Ch %d) on %s\n", \
1056 reg_beacon->chan.center_freq, \
1057 ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \
1058 wiphy_name(wiphy));
1059#else
1060#define REG_DEBUG_BEACON_FLAG(desc) do {} while (0)
1061#endif
1062 struct ieee80211_supported_band *sband;
1063 struct ieee80211_channel *chan;
1064
1065 assert_cfg80211_lock();
1066
1067 sband = wiphy->bands[reg_beacon->chan.band];
1068 chan = &sband->channels[chan_idx];
1069
1070 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1071 return;
1072
1073 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1074 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1075 REG_DEBUG_BEACON_FLAG("active scanning");
1076 }
1077
1078 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1079 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1080 REG_DEBUG_BEACON_FLAG("beaconing");
1081 }
1082
1083 chan->beacon_found = true;
1084#undef REG_DEBUG_BEACON_FLAG
951} 1085}
952 1086
953void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby) 1087/*
1088 * Called when a scan on a wiphy finds a beacon on
1089 * new channel
1090 */
1091static void wiphy_update_new_beacon(struct wiphy *wiphy,
1092 struct reg_beacon *reg_beacon)
1093{
1094 unsigned int i;
1095 struct ieee80211_supported_band *sband;
1096
1097 assert_cfg80211_lock();
1098
1099 if (!wiphy->bands[reg_beacon->chan.band])
1100 return;
1101
1102 sband = wiphy->bands[reg_beacon->chan.band];
1103
1104 for (i = 0; i < sband->n_channels; i++)
1105 handle_reg_beacon(wiphy, i, reg_beacon);
1106}
1107
1108/*
1109 * Called upon reg changes or a new wiphy is added
1110 */
1111static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1112{
1113 unsigned int i;
1114 struct ieee80211_supported_band *sband;
1115 struct reg_beacon *reg_beacon;
1116
1117 assert_cfg80211_lock();
1118
1119 if (list_empty(&reg_beacon_list))
1120 return;
1121
1122 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1123 if (!wiphy->bands[reg_beacon->chan.band])
1124 continue;
1125 sband = wiphy->bands[reg_beacon->chan.band];
1126 for (i = 0; i < sband->n_channels; i++)
1127 handle_reg_beacon(wiphy, i, reg_beacon);
1128 }
1129}
1130
1131static bool reg_is_world_roaming(struct wiphy *wiphy)
1132{
1133 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1134 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1135 return true;
1136 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1137 wiphy->custom_regulatory)
1138 return true;
1139 return false;
1140}
1141
1142/* Reap the advantages of previously found beacons */
1143static void reg_process_beacons(struct wiphy *wiphy)
1144{
1145 if (!reg_is_world_roaming(wiphy))
1146 return;
1147 wiphy_update_beacon_reg(wiphy);
1148}
1149
1150void wiphy_update_regulatory(struct wiphy *wiphy,
1151 enum nl80211_reg_initiator initiator)
954{ 1152{
955 enum ieee80211_band band; 1153 enum ieee80211_band band;
1154
1155 if (ignore_reg_update(wiphy, initiator))
1156 goto out;
956 for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 1157 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
957 if (wiphy->bands[band]) 1158 if (wiphy->bands[band])
958 handle_band(wiphy, band); 1159 handle_band(wiphy, band);
959 if (wiphy->reg_notifier)
960 wiphy->reg_notifier(wiphy, setby);
961 } 1160 }
1161out:
1162 reg_process_beacons(wiphy);
1163 if (wiphy->reg_notifier)
1164 wiphy->reg_notifier(wiphy, last_request);
962} 1165}
963 1166
964/* Return value which can be used by ignore_request() to indicate 1167static void handle_channel_custom(struct wiphy *wiphy,
965 * it has been determined we should intersect two regulatory domains */ 1168 enum ieee80211_band band,
1169 unsigned int chan_idx,
1170 const struct ieee80211_regdomain *regd)
1171{
1172 int r;
1173 u32 max_bandwidth = 0;
1174 const struct ieee80211_reg_rule *reg_rule = NULL;
1175 const struct ieee80211_power_rule *power_rule = NULL;
1176 struct ieee80211_supported_band *sband;
1177 struct ieee80211_channel *chan;
1178
1179 sband = wiphy->bands[band];
1180 BUG_ON(chan_idx >= sband->n_channels);
1181 chan = &sband->channels[chan_idx];
1182
1183 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1184 &max_bandwidth, &reg_rule, regd);
1185
1186 if (r) {
1187 chan->flags = IEEE80211_CHAN_DISABLED;
1188 return;
1189 }
1190
1191 power_rule = &reg_rule->power_rule;
1192
1193 chan->flags |= map_regdom_flags(reg_rule->flags);
1194 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1195 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1196 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1197}
1198
1199static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1200 const struct ieee80211_regdomain *regd)
1201{
1202 unsigned int i;
1203 struct ieee80211_supported_band *sband;
1204
1205 BUG_ON(!wiphy->bands[band]);
1206 sband = wiphy->bands[band];
1207
1208 for (i = 0; i < sband->n_channels; i++)
1209 handle_channel_custom(wiphy, band, i, regd);
1210}
1211
1212/* Used by drivers prior to wiphy registration */
1213void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1214 const struct ieee80211_regdomain *regd)
1215{
1216 enum ieee80211_band band;
1217 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1218 if (wiphy->bands[band])
1219 handle_band_custom(wiphy, band, regd);
1220 }
1221}
1222EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1223
1224static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1225 const struct ieee80211_regdomain *src_regd)
1226{
1227 struct ieee80211_regdomain *regd;
1228 int size_of_regd = 0;
1229 unsigned int i;
1230
1231 size_of_regd = sizeof(struct ieee80211_regdomain) +
1232 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1233
1234 regd = kzalloc(size_of_regd, GFP_KERNEL);
1235 if (!regd)
1236 return -ENOMEM;
1237
1238 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1239
1240 for (i = 0; i < src_regd->n_reg_rules; i++)
1241 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1242 sizeof(struct ieee80211_reg_rule));
1243
1244 *dst_regd = regd;
1245 return 0;
1246}
1247
1248/*
1249 * Return value which can be used by ignore_request() to indicate
1250 * it has been determined we should intersect two regulatory domains
1251 */
966#define REG_INTERSECT 1 1252#define REG_INTERSECT 1
967 1253
968/* This has the logic which determines when a new request 1254/* This has the logic which determines when a new request
969 * should be ignored. */ 1255 * should be ignored. */
970static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by, 1256static int ignore_request(struct wiphy *wiphy,
971 const char *alpha2) 1257 struct regulatory_request *pending_request)
972{ 1258{
1259 struct wiphy *last_wiphy = NULL;
1260
1261 assert_cfg80211_lock();
1262
973 /* All initial requests are respected */ 1263 /* All initial requests are respected */
974 if (!last_request) 1264 if (!last_request)
975 return 0; 1265 return 0;
976 1266
977 switch (set_by) { 1267 switch (pending_request->initiator) {
978 case REGDOM_SET_BY_INIT: 1268 case NL80211_REGDOM_SET_BY_CORE:
979 return -EINVAL; 1269 return -EINVAL;
980 case REGDOM_SET_BY_CORE: 1270 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
981 /* 1271
982 * Always respect new wireless core hints, should only happen 1272 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
983 * when updating the world regulatory domain at init. 1273
984 */ 1274 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
985 return 0;
986 case REGDOM_SET_BY_COUNTRY_IE:
987 if (unlikely(!is_an_alpha2(alpha2)))
988 return -EINVAL; 1275 return -EINVAL;
989 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { 1276 if (last_request->initiator ==
990 if (last_request->wiphy != wiphy) { 1277 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1278 if (last_wiphy != wiphy) {
991 /* 1279 /*
992 * Two cards with two APs claiming different 1280 * Two cards with two APs claiming different
993 * different Country IE alpha2s. We could 1281 * different Country IE alpha2s. We could
994 * intersect them, but that seems unlikely 1282 * intersect them, but that seems unlikely
995 * to be correct. Reject second one for now. 1283 * to be correct. Reject second one for now.
996 */ 1284 */
997 if (!alpha2_equal(alpha2, 1285 if (regdom_changes(pending_request->alpha2))
998 cfg80211_regdomain->alpha2))
999 return -EOPNOTSUPP; 1286 return -EOPNOTSUPP;
1000 return -EALREADY; 1287 return -EALREADY;
1001 } 1288 }
1002 /* Two consecutive Country IE hints on the same wiphy. 1289 /*
1003 * This should be picked up early by the driver/stack */ 1290 * Two consecutive Country IE hints on the same wiphy.
1004 if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2, 1291 * This should be picked up early by the driver/stack
1005 alpha2))) 1292 */
1293 if (WARN_ON(regdom_changes(pending_request->alpha2)))
1006 return 0; 1294 return 0;
1007 return -EALREADY; 1295 return -EALREADY;
1008 } 1296 }
1009 return REG_INTERSECT; 1297 return REG_INTERSECT;
1010 case REGDOM_SET_BY_DRIVER: 1298 case NL80211_REGDOM_SET_BY_DRIVER:
1011 if (last_request->initiator == REGDOM_SET_BY_DRIVER) 1299 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1300 if (is_old_static_regdom(cfg80211_regdomain))
1301 return 0;
1302 if (regdom_changes(pending_request->alpha2))
1303 return 0;
1012 return -EALREADY; 1304 return -EALREADY;
1013 return 0; 1305 }
1014 case REGDOM_SET_BY_USER: 1306
1015 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) 1307 /*
1308 * This would happen if you unplug and plug your card
1309 * back in or if you add a new device for which the previously
1310 * loaded card also agrees on the regulatory domain.
1311 */
1312 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1313 !regdom_changes(pending_request->alpha2))
1314 return -EALREADY;
1315
1316 return REG_INTERSECT;
1317 case NL80211_REGDOM_SET_BY_USER:
1318 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1016 return REG_INTERSECT; 1319 return REG_INTERSECT;
1017 /* If the user knows better the user should set the regdom 1320 /*
1018 * to their country before the IE is picked up */ 1321 * If the user knows better the user should set the regdom
1019 if (last_request->initiator == REGDOM_SET_BY_USER && 1322 * to their country before the IE is picked up
1323 */
1324 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1020 last_request->intersect) 1325 last_request->intersect)
1021 return -EOPNOTSUPP; 1326 return -EOPNOTSUPP;
1327 /*
1328 * Process user requests only after previous user/driver/core
1329 * requests have been processed
1330 */
1331 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1332 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1333 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1334 if (regdom_changes(last_request->alpha2))
1335 return -EAGAIN;
1336 }
1337
1338 if (!is_old_static_regdom(cfg80211_regdomain) &&
1339 !regdom_changes(pending_request->alpha2))
1340 return -EALREADY;
1341
1022 return 0; 1342 return 0;
1023 } 1343 }
1024 1344
1025 return -EINVAL; 1345 return -EINVAL;
1026} 1346}
1027 1347
1028/* Caller must hold &cfg80211_drv_mutex */ 1348/**
1029int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by, 1349 * __regulatory_hint - hint to the wireless core a regulatory domain
1030 const char *alpha2, 1350 * @wiphy: if the hint comes from country information from an AP, this
1031 u32 country_ie_checksum, 1351 * is required to be set to the wiphy that received the information
1032 enum environment_cap env) 1352 * @pending_request: the regulatory request currently being processed
1353 *
1354 * The Wireless subsystem can use this function to hint to the wireless core
1355 * what it believes should be the current regulatory domain.
1356 *
1357 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1358 * already been set or other standard error codes.
1359 *
1360 * Caller must hold &cfg80211_mutex
1361 */
1362static int __regulatory_hint(struct wiphy *wiphy,
1363 struct regulatory_request *pending_request)
1033{ 1364{
1034 struct regulatory_request *request;
1035 bool intersect = false; 1365 bool intersect = false;
1036 int r = 0; 1366 int r = 0;
1037 1367
1038 r = ignore_request(wiphy, set_by, alpha2); 1368 assert_cfg80211_lock();
1369
1370 r = ignore_request(wiphy, pending_request);
1039 1371
1040 if (r == REG_INTERSECT) 1372 if (r == REG_INTERSECT) {
1373 if (pending_request->initiator ==
1374 NL80211_REGDOM_SET_BY_DRIVER) {
1375 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1376 if (r) {
1377 kfree(pending_request);
1378 return r;
1379 }
1380 }
1041 intersect = true; 1381 intersect = true;
1042 else if (r) 1382 } else if (r) {
1383 /*
1384 * If the regulatory domain being requested by the
1385 * driver has already been set just copy it to the
1386 * wiphy
1387 */
1388 if (r == -EALREADY &&
1389 pending_request->initiator ==
1390 NL80211_REGDOM_SET_BY_DRIVER) {
1391 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1392 if (r) {
1393 kfree(pending_request);
1394 return r;
1395 }
1396 r = -EALREADY;
1397 goto new_request;
1398 }
1399 kfree(pending_request);
1400 return r;
1401 }
1402
1403new_request:
1404 kfree(last_request);
1405
1406 last_request = pending_request;
1407 last_request->intersect = intersect;
1408
1409 pending_request = NULL;
1410
1411 /* When r == REG_INTERSECT we do need to call CRDA */
1412 if (r < 0) {
1413 /*
1414 * Since CRDA will not be called in this case as we already
1415 * have applied the requested regulatory domain before we just
1416 * inform userspace we have processed the request
1417 */
1418 if (r == -EALREADY)
1419 nl80211_send_reg_change_event(last_request);
1043 return r; 1420 return r;
1421 }
1422
1423 return call_crda(last_request->alpha2);
1424}
1425
1426/* This currently only processes user and driver regulatory hints */
1427static void reg_process_hint(struct regulatory_request *reg_request)
1428{
1429 int r = 0;
1430 struct wiphy *wiphy = NULL;
1431
1432 BUG_ON(!reg_request->alpha2);
1433
1434 mutex_lock(&cfg80211_mutex);
1435
1436 if (wiphy_idx_valid(reg_request->wiphy_idx))
1437 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1438
1439 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1440 !wiphy) {
1441 kfree(reg_request);
1442 goto out;
1443 }
1444
1445 r = __regulatory_hint(wiphy, reg_request);
1446 /* This is required so that the orig_* parameters are saved */
1447 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1448 wiphy_update_regulatory(wiphy, reg_request->initiator);
1449out:
1450 mutex_unlock(&cfg80211_mutex);
1451}
1452
1453/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1454static void reg_process_pending_hints(void)
1455 {
1456 struct regulatory_request *reg_request;
1457
1458 spin_lock(&reg_requests_lock);
1459 while (!list_empty(&reg_requests_list)) {
1460 reg_request = list_first_entry(&reg_requests_list,
1461 struct regulatory_request,
1462 list);
1463 list_del_init(&reg_request->list);
1464
1465 spin_unlock(&reg_requests_lock);
1466 reg_process_hint(reg_request);
1467 spin_lock(&reg_requests_lock);
1468 }
1469 spin_unlock(&reg_requests_lock);
1470}
1471
1472/* Processes beacon hints -- this has nothing to do with country IEs */
1473static void reg_process_pending_beacon_hints(void)
1474{
1475 struct cfg80211_registered_device *drv;
1476 struct reg_beacon *pending_beacon, *tmp;
1477
1478 mutex_lock(&cfg80211_mutex);
1479
1480 /* This goes through the _pending_ beacon list */
1481 spin_lock_bh(&reg_pending_beacons_lock);
1482
1483 if (list_empty(&reg_pending_beacons)) {
1484 spin_unlock_bh(&reg_pending_beacons_lock);
1485 goto out;
1486 }
1487
1488 list_for_each_entry_safe(pending_beacon, tmp,
1489 &reg_pending_beacons, list) {
1490
1491 list_del_init(&pending_beacon->list);
1492
1493 /* Applies the beacon hint to current wiphys */
1494 list_for_each_entry(drv, &cfg80211_drv_list, list)
1495 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1496
1497 /* Remembers the beacon hint for new wiphys or reg changes */
1498 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1499 }
1500
1501 spin_unlock_bh(&reg_pending_beacons_lock);
1502out:
1503 mutex_unlock(&cfg80211_mutex);
1504}
1505
1506static void reg_todo(struct work_struct *work)
1507{
1508 reg_process_pending_hints();
1509 reg_process_pending_beacon_hints();
1510}
1511
1512static DECLARE_WORK(reg_work, reg_todo);
1513
1514static void queue_regulatory_request(struct regulatory_request *request)
1515{
1516 spin_lock(&reg_requests_lock);
1517 list_add_tail(&request->list, &reg_requests_list);
1518 spin_unlock(&reg_requests_lock);
1519
1520 schedule_work(&reg_work);
1521}
1522
1523/* Core regulatory hint -- happens once during cfg80211_init() */
1524static int regulatory_hint_core(const char *alpha2)
1525{
1526 struct regulatory_request *request;
1527
1528 BUG_ON(last_request);
1044 1529
1045 request = kzalloc(sizeof(struct regulatory_request), 1530 request = kzalloc(sizeof(struct regulatory_request),
1046 GFP_KERNEL); 1531 GFP_KERNEL);
@@ -1049,47 +1534,84 @@ int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
1049 1534
1050 request->alpha2[0] = alpha2[0]; 1535 request->alpha2[0] = alpha2[0];
1051 request->alpha2[1] = alpha2[1]; 1536 request->alpha2[1] = alpha2[1];
1052 request->initiator = set_by; 1537 request->initiator = NL80211_REGDOM_SET_BY_CORE;
1053 request->wiphy = wiphy;
1054 request->intersect = intersect;
1055 request->country_ie_checksum = country_ie_checksum;
1056 request->country_ie_env = env;
1057 1538
1058 kfree(last_request); 1539 queue_regulatory_request(request);
1059 last_request = request; 1540
1060 /* 1541 return 0;
1061 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled 1542}
1062 * AND if CRDA is NOT present nothing will happen, if someone 1543
1063 * wants to bother with 11d with OLD_REG you can add a timer. 1544/* User hints */
1064 * If after x amount of time nothing happens you can call: 1545int regulatory_hint_user(const char *alpha2)
1065 * 1546{
1066 * return set_regdom(country_ie_regdomain); 1547 struct regulatory_request *request;
1067 * 1548
1068 * to intersect with the static rd 1549 BUG_ON(!alpha2);
1069 */ 1550
1070 return call_crda(alpha2); 1551 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1552 if (!request)
1553 return -ENOMEM;
1554
1555 request->wiphy_idx = WIPHY_IDX_STALE;
1556 request->alpha2[0] = alpha2[0];
1557 request->alpha2[1] = alpha2[1];
1558 request->initiator = NL80211_REGDOM_SET_BY_USER,
1559
1560 queue_regulatory_request(request);
1561
1562 return 0;
1071} 1563}
1072 1564
1073void regulatory_hint(struct wiphy *wiphy, const char *alpha2) 1565/* Driver hints */
1566int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1074{ 1567{
1568 struct regulatory_request *request;
1569
1075 BUG_ON(!alpha2); 1570 BUG_ON(!alpha2);
1571 BUG_ON(!wiphy);
1076 1572
1077 mutex_lock(&cfg80211_drv_mutex); 1573 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1078 __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY); 1574 if (!request)
1079 mutex_unlock(&cfg80211_drv_mutex); 1575 return -ENOMEM;
1576
1577 request->wiphy_idx = get_wiphy_idx(wiphy);
1578
1579 /* Must have registered wiphy first */
1580 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1581
1582 request->alpha2[0] = alpha2[0];
1583 request->alpha2[1] = alpha2[1];
1584 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1585
1586 queue_regulatory_request(request);
1587
1588 return 0;
1080} 1589}
1081EXPORT_SYMBOL(regulatory_hint); 1590EXPORT_SYMBOL(regulatory_hint);
1082 1591
1083static bool reg_same_country_ie_hint(struct wiphy *wiphy, 1592static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1084 u32 country_ie_checksum) 1593 u32 country_ie_checksum)
1085{ 1594{
1086 if (!last_request->wiphy) 1595 struct wiphy *request_wiphy;
1596
1597 assert_cfg80211_lock();
1598
1599 if (unlikely(last_request->initiator !=
1600 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1601 return false;
1602
1603 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1604
1605 if (!request_wiphy)
1087 return false; 1606 return false;
1088 if (likely(last_request->wiphy != wiphy)) 1607
1608 if (likely(request_wiphy != wiphy))
1089 return !country_ie_integrity_changes(country_ie_checksum); 1609 return !country_ie_integrity_changes(country_ie_checksum);
1090 /* We should not have let these through at this point, they 1610 /*
1611 * We should not have let these through at this point, they
1091 * should have been picked up earlier by the first alpha2 check 1612 * should have been picked up earlier by the first alpha2 check
1092 * on the device */ 1613 * on the device
1614 */
1093 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum))) 1615 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1094 return true; 1616 return true;
1095 return false; 1617 return false;
@@ -1103,11 +1625,14 @@ void regulatory_hint_11d(struct wiphy *wiphy,
1103 char alpha2[2]; 1625 char alpha2[2];
1104 u32 checksum = 0; 1626 u32 checksum = 0;
1105 enum environment_cap env = ENVIRON_ANY; 1627 enum environment_cap env = ENVIRON_ANY;
1628 struct regulatory_request *request;
1106 1629
1107 if (!last_request) 1630 mutex_lock(&cfg80211_mutex);
1108 return;
1109 1631
1110 mutex_lock(&cfg80211_drv_mutex); 1632 if (unlikely(!last_request)) {
1633 mutex_unlock(&cfg80211_mutex);
1634 return;
1635 }
1111 1636
1112 /* IE len must be evenly divisible by 2 */ 1637 /* IE len must be evenly divisible by 2 */
1113 if (country_ie_len & 0x01) 1638 if (country_ie_len & 0x01)
@@ -1116,9 +1641,11 @@ void regulatory_hint_11d(struct wiphy *wiphy,
1116 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) 1641 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1117 goto out; 1642 goto out;
1118 1643
1119 /* Pending country IE processing, this can happen after we 1644 /*
1645 * Pending country IE processing, this can happen after we
1120 * call CRDA and wait for a response if a beacon was received before 1646 * call CRDA and wait for a response if a beacon was received before
1121 * we were able to process the last regulatory_hint_11d() call */ 1647 * we were able to process the last regulatory_hint_11d() call
1648 */
1122 if (country_ie_regdomain) 1649 if (country_ie_regdomain)
1123 goto out; 1650 goto out;
1124 1651
@@ -1130,33 +1657,46 @@ void regulatory_hint_11d(struct wiphy *wiphy,
1130 else if (country_ie[2] == 'O') 1657 else if (country_ie[2] == 'O')
1131 env = ENVIRON_OUTDOOR; 1658 env = ENVIRON_OUTDOOR;
1132 1659
1133 /* We will run this for *every* beacon processed for the BSSID, so 1660 /*
1661 * We will run this for *every* beacon processed for the BSSID, so
1134 * we optimize an early check to exit out early if we don't have to 1662 * we optimize an early check to exit out early if we don't have to
1135 * do anything */ 1663 * do anything
1136 if (likely(last_request->wiphy)) { 1664 */
1665 if (likely(last_request->initiator ==
1666 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1667 wiphy_idx_valid(last_request->wiphy_idx))) {
1137 struct cfg80211_registered_device *drv_last_ie; 1668 struct cfg80211_registered_device *drv_last_ie;
1138 1669
1139 drv_last_ie = wiphy_to_dev(last_request->wiphy); 1670 drv_last_ie =
1671 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
1140 1672
1141 /* Lets keep this simple -- we trust the first AP 1673 /*
1142 * after we intersect with CRDA */ 1674 * Lets keep this simple -- we trust the first AP
1143 if (likely(last_request->wiphy == wiphy)) { 1675 * after we intersect with CRDA
1144 /* Ignore IEs coming in on this wiphy with 1676 */
1145 * the same alpha2 and environment cap */ 1677 if (likely(&drv_last_ie->wiphy == wiphy)) {
1678 /*
1679 * Ignore IEs coming in on this wiphy with
1680 * the same alpha2 and environment cap
1681 */
1146 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2, 1682 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1147 alpha2) && 1683 alpha2) &&
1148 env == drv_last_ie->env)) { 1684 env == drv_last_ie->env)) {
1149 goto out; 1685 goto out;
1150 } 1686 }
1151 /* the wiphy moved on to another BSSID or the AP 1687 /*
1688 * the wiphy moved on to another BSSID or the AP
1152 * was reconfigured. XXX: We need to deal with the 1689 * was reconfigured. XXX: We need to deal with the
1153 * case where the user suspends and goes to goes 1690 * case where the user suspends and goes to goes
1154 * to another country, and then gets IEs from an 1691 * to another country, and then gets IEs from an
1155 * AP with different settings */ 1692 * AP with different settings
1693 */
1156 goto out; 1694 goto out;
1157 } else { 1695 } else {
1158 /* Ignore IEs coming in on two separate wiphys with 1696 /*
1159 * the same alpha2 and environment cap */ 1697 * Ignore IEs coming in on two separate wiphys with
1698 * the same alpha2 and environment cap
1699 */
1160 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2, 1700 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1161 alpha2) && 1701 alpha2) &&
1162 env == drv_last_ie->env)) { 1702 env == drv_last_ie->env)) {
@@ -1171,28 +1711,97 @@ void regulatory_hint_11d(struct wiphy *wiphy,
1171 if (!rd) 1711 if (!rd)
1172 goto out; 1712 goto out;
1173 1713
1174 /* This will not happen right now but we leave it here for the 1714 /*
1715 * This will not happen right now but we leave it here for the
1175 * the future when we want to add suspend/resume support and having 1716 * the future when we want to add suspend/resume support and having
1176 * the user move to another country after doing so, or having the user 1717 * the user move to another country after doing so, or having the user
1177 * move to another AP. Right now we just trust the first AP. This is why 1718 * move to another AP. Right now we just trust the first AP.
1178 * this is marked as likley(). If we hit this before we add this support 1719 *
1179 * we want to be informed of it as it would indicate a mistake in the 1720 * If we hit this before we add this support we want to be informed of
1180 * current design */ 1721 * it as it would indicate a mistake in the current design
1181 if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))) 1722 */
1182 goto out; 1723 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
1724 goto free_rd_out;
1183 1725
1184 /* We keep this around for when CRDA comes back with a response so 1726 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1185 * we can intersect with that */ 1727 if (!request)
1728 goto free_rd_out;
1729
1730 /*
1731 * We keep this around for when CRDA comes back with a response so
1732 * we can intersect with that
1733 */
1186 country_ie_regdomain = rd; 1734 country_ie_regdomain = rd;
1187 1735
1188 __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE, 1736 request->wiphy_idx = get_wiphy_idx(wiphy);
1189 country_ie_regdomain->alpha2, checksum, env); 1737 request->alpha2[0] = rd->alpha2[0];
1738 request->alpha2[1] = rd->alpha2[1];
1739 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
1740 request->country_ie_checksum = checksum;
1741 request->country_ie_env = env;
1742
1743 mutex_unlock(&cfg80211_mutex);
1744
1745 queue_regulatory_request(request);
1746
1747 return;
1190 1748
1749free_rd_out:
1750 kfree(rd);
1191out: 1751out:
1192 mutex_unlock(&cfg80211_drv_mutex); 1752 mutex_unlock(&cfg80211_mutex);
1193} 1753}
1194EXPORT_SYMBOL(regulatory_hint_11d); 1754EXPORT_SYMBOL(regulatory_hint_11d);
1195 1755
1756static bool freq_is_chan_12_13_14(u16 freq)
1757{
1758 if (freq == ieee80211_channel_to_frequency(12) ||
1759 freq == ieee80211_channel_to_frequency(13) ||
1760 freq == ieee80211_channel_to_frequency(14))
1761 return true;
1762 return false;
1763}
1764
1765int regulatory_hint_found_beacon(struct wiphy *wiphy,
1766 struct ieee80211_channel *beacon_chan,
1767 gfp_t gfp)
1768{
1769 struct reg_beacon *reg_beacon;
1770
1771 if (likely((beacon_chan->beacon_found ||
1772 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1773 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1774 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1775 return 0;
1776
1777 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1778 if (!reg_beacon)
1779 return -ENOMEM;
1780
1781#ifdef CONFIG_CFG80211_REG_DEBUG
1782 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1783 "frequency: %d MHz (Ch %d) on %s\n",
1784 beacon_chan->center_freq,
1785 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1786 wiphy_name(wiphy));
1787#endif
1788 memcpy(&reg_beacon->chan, beacon_chan,
1789 sizeof(struct ieee80211_channel));
1790
1791
1792 /*
1793 * Since we can be called from BH or and non-BH context
1794 * we must use spin_lock_bh()
1795 */
1796 spin_lock_bh(&reg_pending_beacons_lock);
1797 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1798 spin_unlock_bh(&reg_pending_beacons_lock);
1799
1800 schedule_work(&reg_work);
1801
1802 return 0;
1803}
1804
1196static void print_rd_rules(const struct ieee80211_regdomain *rd) 1805static void print_rd_rules(const struct ieee80211_regdomain *rd)
1197{ 1806{
1198 unsigned int i; 1807 unsigned int i;
@@ -1208,8 +1817,10 @@ static void print_rd_rules(const struct ieee80211_regdomain *rd)
1208 freq_range = &reg_rule->freq_range; 1817 freq_range = &reg_rule->freq_range;
1209 power_rule = &reg_rule->power_rule; 1818 power_rule = &reg_rule->power_rule;
1210 1819
1211 /* There may not be documentation for max antenna gain 1820 /*
1212 * in certain regions */ 1821 * There may not be documentation for max antenna gain
1822 * in certain regions
1823 */
1213 if (power_rule->max_antenna_gain) 1824 if (power_rule->max_antenna_gain)
1214 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), " 1825 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1215 "(%d mBi, %d mBm)\n", 1826 "(%d mBi, %d mBm)\n",
@@ -1232,13 +1843,13 @@ static void print_regdomain(const struct ieee80211_regdomain *rd)
1232{ 1843{
1233 1844
1234 if (is_intersected_alpha2(rd->alpha2)) { 1845 if (is_intersected_alpha2(rd->alpha2)) {
1235 struct wiphy *wiphy = NULL;
1236 struct cfg80211_registered_device *drv;
1237 1846
1238 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { 1847 if (last_request->initiator ==
1239 if (last_request->wiphy) { 1848 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1240 wiphy = last_request->wiphy; 1849 struct cfg80211_registered_device *drv;
1241 drv = wiphy_to_dev(wiphy); 1850 drv = cfg80211_drv_by_wiphy_idx(
1851 last_request->wiphy_idx);
1852 if (drv) {
1242 printk(KERN_INFO "cfg80211: Current regulatory " 1853 printk(KERN_INFO "cfg80211: Current regulatory "
1243 "domain updated by AP to: %c%c\n", 1854 "domain updated by AP to: %c%c\n",
1244 drv->country_ie_alpha2[0], 1855 drv->country_ie_alpha2[0],
@@ -1248,7 +1859,7 @@ static void print_regdomain(const struct ieee80211_regdomain *rd)
1248 "domain intersected: \n"); 1859 "domain intersected: \n");
1249 } else 1860 } else
1250 printk(KERN_INFO "cfg80211: Current regulatory " 1861 printk(KERN_INFO "cfg80211: Current regulatory "
1251 "intersected: \n"); 1862 "domain intersected: \n");
1252 } else if (is_world_regdom(rd->alpha2)) 1863 } else if (is_world_regdom(rd->alpha2))
1253 printk(KERN_INFO "cfg80211: World regulatory " 1864 printk(KERN_INFO "cfg80211: World regulatory "
1254 "domain updated:\n"); 1865 "domain updated:\n");
@@ -1304,7 +1915,7 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
1304{ 1915{
1305 const struct ieee80211_regdomain *intersected_rd = NULL; 1916 const struct ieee80211_regdomain *intersected_rd = NULL;
1306 struct cfg80211_registered_device *drv = NULL; 1917 struct cfg80211_registered_device *drv = NULL;
1307 struct wiphy *wiphy = NULL; 1918 struct wiphy *request_wiphy;
1308 /* Some basic sanity checks first */ 1919 /* Some basic sanity checks first */
1309 1920
1310 if (is_world_regdom(rd->alpha2)) { 1921 if (is_world_regdom(rd->alpha2)) {
@@ -1321,23 +1932,27 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
1321 if (!last_request) 1932 if (!last_request)
1322 return -EINVAL; 1933 return -EINVAL;
1323 1934
1324 /* Lets only bother proceeding on the same alpha2 if the current 1935 /*
1936 * Lets only bother proceeding on the same alpha2 if the current
1325 * rd is non static (it means CRDA was present and was used last) 1937 * rd is non static (it means CRDA was present and was used last)
1326 * and the pending request came in from a country IE */ 1938 * and the pending request came in from a country IE
1327 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) { 1939 */
1328 /* If someone else asked us to change the rd lets only bother 1940 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1329 * checking if the alpha2 changes if CRDA was already called */ 1941 /*
1942 * If someone else asked us to change the rd lets only bother
1943 * checking if the alpha2 changes if CRDA was already called
1944 */
1330 if (!is_old_static_regdom(cfg80211_regdomain) && 1945 if (!is_old_static_regdom(cfg80211_regdomain) &&
1331 !regdom_changed(rd->alpha2)) 1946 !regdom_changes(rd->alpha2))
1332 return -EINVAL; 1947 return -EINVAL;
1333 } 1948 }
1334 1949
1335 wiphy = last_request->wiphy; 1950 /*
1336 1951 * Now lets set the regulatory domain, update all driver channels
1337 /* Now lets set the regulatory domain, update all driver channels
1338 * and finally inform them of what we have done, in case they want 1952 * and finally inform them of what we have done, in case they want
1339 * to review or adjust their own settings based on their own 1953 * to review or adjust their own settings based on their own
1340 * internal EEPROM data */ 1954 * internal EEPROM data
1955 */
1341 1956
1342 if (WARN_ON(!reg_is_valid_request(rd->alpha2))) 1957 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1343 return -EINVAL; 1958 return -EINVAL;
@@ -1349,7 +1964,28 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
1349 return -EINVAL; 1964 return -EINVAL;
1350 } 1965 }
1351 1966
1967 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1968
1352 if (!last_request->intersect) { 1969 if (!last_request->intersect) {
1970 int r;
1971
1972 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
1973 reset_regdomains();
1974 cfg80211_regdomain = rd;
1975 return 0;
1976 }
1977
1978 /*
1979 * For a driver hint, lets copy the regulatory domain the
1980 * driver wanted to the wiphy to deal with conflicts
1981 */
1982
1983 BUG_ON(request_wiphy->regd);
1984
1985 r = reg_copy_regd(&request_wiphy->regd, rd);
1986 if (r)
1987 return r;
1988
1353 reset_regdomains(); 1989 reset_regdomains();
1354 cfg80211_regdomain = rd; 1990 cfg80211_regdomain = rd;
1355 return 0; 1991 return 0;
@@ -1357,14 +1993,22 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
1357 1993
1358 /* Intersection requires a bit more work */ 1994 /* Intersection requires a bit more work */
1359 1995
1360 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) { 1996 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1361 1997
1362 intersected_rd = regdom_intersect(rd, cfg80211_regdomain); 1998 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1363 if (!intersected_rd) 1999 if (!intersected_rd)
1364 return -EINVAL; 2000 return -EINVAL;
1365 2001
1366 /* We can trash what CRDA provided now */ 2002 /*
1367 kfree(rd); 2003 * We can trash what CRDA provided now.
2004 * However if a driver requested this specific regulatory
2005 * domain we keep it for its private use
2006 */
2007 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2008 request_wiphy->regd = rd;
2009 else
2010 kfree(rd);
2011
1368 rd = NULL; 2012 rd = NULL;
1369 2013
1370 reset_regdomains(); 2014 reset_regdomains();
@@ -1379,29 +2023,26 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
1379 */ 2023 */
1380 2024
1381 BUG_ON(!country_ie_regdomain); 2025 BUG_ON(!country_ie_regdomain);
2026 BUG_ON(rd == country_ie_regdomain);
1382 2027
1383 if (rd != country_ie_regdomain) { 2028 /*
1384 /* Intersect what CRDA returned and our what we 2029 * Intersect what CRDA returned and our what we
1385 * had built from the Country IE received */ 2030 * had built from the Country IE received
2031 */
1386 2032
1387 intersected_rd = regdom_intersect(rd, country_ie_regdomain); 2033 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1388 2034
1389 reg_country_ie_process_debug(rd, country_ie_regdomain, 2035 reg_country_ie_process_debug(rd,
1390 intersected_rd); 2036 country_ie_regdomain,
2037 intersected_rd);
1391 2038
1392 kfree(country_ie_regdomain); 2039 kfree(country_ie_regdomain);
1393 country_ie_regdomain = NULL; 2040 country_ie_regdomain = NULL;
1394 } else {
1395 /* This would happen when CRDA was not present and
1396 * OLD_REGULATORY was enabled. We intersect our Country
1397 * IE rd and what was set on cfg80211 originally */
1398 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1399 }
1400 2041
1401 if (!intersected_rd) 2042 if (!intersected_rd)
1402 return -EINVAL; 2043 return -EINVAL;
1403 2044
1404 drv = wiphy_to_dev(wiphy); 2045 drv = wiphy_to_dev(request_wiphy);
1405 2046
1406 drv->country_ie_alpha2[0] = rd->alpha2[0]; 2047 drv->country_ie_alpha2[0] = rd->alpha2[0];
1407 drv->country_ie_alpha2[1] = rd->alpha2[1]; 2048 drv->country_ie_alpha2[1] = rd->alpha2[1];
@@ -1419,13 +2060,17 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
1419} 2060}
1420 2061
1421 2062
1422/* Use this call to set the current regulatory domain. Conflicts with 2063/*
2064 * Use this call to set the current regulatory domain. Conflicts with
1423 * multiple drivers can be ironed out later. Caller must've already 2065 * multiple drivers can be ironed out later. Caller must've already
1424 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */ 2066 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2067 */
1425int set_regdom(const struct ieee80211_regdomain *rd) 2068int set_regdom(const struct ieee80211_regdomain *rd)
1426{ 2069{
1427 int r; 2070 int r;
1428 2071
2072 assert_cfg80211_lock();
2073
1429 /* Note that this doesn't update the wiphys, this is done below */ 2074 /* Note that this doesn't update the wiphys, this is done below */
1430 r = __set_regdom(rd); 2075 r = __set_regdom(rd);
1431 if (r) { 2076 if (r) {
@@ -1442,56 +2087,90 @@ int set_regdom(const struct ieee80211_regdomain *rd)
1442 2087
1443 print_regdomain(cfg80211_regdomain); 2088 print_regdomain(cfg80211_regdomain);
1444 2089
2090 nl80211_send_reg_change_event(last_request);
2091
1445 return r; 2092 return r;
1446} 2093}
1447 2094
1448/* Caller must hold cfg80211_drv_mutex */ 2095/* Caller must hold cfg80211_mutex */
1449void reg_device_remove(struct wiphy *wiphy) 2096void reg_device_remove(struct wiphy *wiphy)
1450{ 2097{
1451 if (!last_request || !last_request->wiphy) 2098 struct wiphy *request_wiphy;
2099
2100 assert_cfg80211_lock();
2101
2102 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2103
2104 kfree(wiphy->regd);
2105 if (!last_request || !request_wiphy)
1452 return; 2106 return;
1453 if (last_request->wiphy != wiphy) 2107 if (request_wiphy != wiphy)
1454 return; 2108 return;
1455 last_request->wiphy = NULL; 2109 last_request->wiphy_idx = WIPHY_IDX_STALE;
1456 last_request->country_ie_env = ENVIRON_ANY; 2110 last_request->country_ie_env = ENVIRON_ANY;
1457} 2111}
1458 2112
1459int regulatory_init(void) 2113int regulatory_init(void)
1460{ 2114{
1461 int err; 2115 int err = 0;
1462 2116
1463 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0); 2117 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1464 if (IS_ERR(reg_pdev)) 2118 if (IS_ERR(reg_pdev))
1465 return PTR_ERR(reg_pdev); 2119 return PTR_ERR(reg_pdev);
1466 2120
2121 spin_lock_init(&reg_requests_lock);
2122 spin_lock_init(&reg_pending_beacons_lock);
2123
1467#ifdef CONFIG_WIRELESS_OLD_REGULATORY 2124#ifdef CONFIG_WIRELESS_OLD_REGULATORY
1468 cfg80211_regdomain = static_regdom(ieee80211_regdom); 2125 cfg80211_regdomain = static_regdom(ieee80211_regdom);
1469 2126
1470 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n"); 2127 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
1471 print_regdomain_info(cfg80211_regdomain); 2128 print_regdomain_info(cfg80211_regdomain);
1472 /* The old code still requests for a new regdomain and if 2129 /*
2130 * The old code still requests for a new regdomain and if
1473 * you have CRDA you get it updated, otherwise you get 2131 * you have CRDA you get it updated, otherwise you get
1474 * stuck with the static values. We ignore "EU" code as 2132 * stuck with the static values. Since "EU" is not a valid
1475 * that is not a valid ISO / IEC 3166 alpha2 */ 2133 * ISO / IEC 3166 alpha2 code we can't expect userpace to
1476 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U') 2134 * give us a regulatory domain for it. We need last_request
1477 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, 2135 * iniitalized though so lets just send a request which we
1478 ieee80211_regdom, 0, ENVIRON_ANY); 2136 * know will be ignored... this crap will be removed once
2137 * OLD_REG dies.
2138 */
2139 err = regulatory_hint_core(ieee80211_regdom);
1479#else 2140#else
1480 cfg80211_regdomain = cfg80211_world_regdom; 2141 cfg80211_regdomain = cfg80211_world_regdom;
1481 2142
1482 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY); 2143 err = regulatory_hint_core(ieee80211_regdom);
1483 if (err) 2144#endif
1484 printk(KERN_ERR "cfg80211: calling CRDA failed - " 2145 if (err) {
1485 "unable to update world regulatory domain, " 2146 if (err == -ENOMEM)
1486 "using static definition\n"); 2147 return err;
2148 /*
2149 * N.B. kobject_uevent_env() can fail mainly for when we're out
2150 * memory which is handled and propagated appropriately above
2151 * but it can also fail during a netlink_broadcast() or during
2152 * early boot for call_usermodehelper(). For now treat these
2153 * errors as non-fatal.
2154 */
2155 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2156 "to call CRDA during init");
2157#ifdef CONFIG_CFG80211_REG_DEBUG
2158 /* We want to find out exactly why when debugging */
2159 WARN_ON(err);
1487#endif 2160#endif
2161 }
1488 2162
1489 return 0; 2163 return 0;
1490} 2164}
1491 2165
1492void regulatory_exit(void) 2166void regulatory_exit(void)
1493{ 2167{
1494 mutex_lock(&cfg80211_drv_mutex); 2168 struct regulatory_request *reg_request, *tmp;
2169 struct reg_beacon *reg_beacon, *btmp;
2170
2171 cancel_work_sync(&reg_work);
2172
2173 mutex_lock(&cfg80211_mutex);
1495 2174
1496 reset_regdomains(); 2175 reset_regdomains();
1497 2176
@@ -1502,5 +2181,33 @@ void regulatory_exit(void)
1502 2181
1503 platform_device_unregister(reg_pdev); 2182 platform_device_unregister(reg_pdev);
1504 2183
1505 mutex_unlock(&cfg80211_drv_mutex); 2184 spin_lock_bh(&reg_pending_beacons_lock);
2185 if (!list_empty(&reg_pending_beacons)) {
2186 list_for_each_entry_safe(reg_beacon, btmp,
2187 &reg_pending_beacons, list) {
2188 list_del(&reg_beacon->list);
2189 kfree(reg_beacon);
2190 }
2191 }
2192 spin_unlock_bh(&reg_pending_beacons_lock);
2193
2194 if (!list_empty(&reg_beacon_list)) {
2195 list_for_each_entry_safe(reg_beacon, btmp,
2196 &reg_beacon_list, list) {
2197 list_del(&reg_beacon->list);
2198 kfree(reg_beacon);
2199 }
2200 }
2201
2202 spin_lock(&reg_requests_lock);
2203 if (!list_empty(&reg_requests_list)) {
2204 list_for_each_entry_safe(reg_request, tmp,
2205 &reg_requests_list, list) {
2206 list_del(&reg_request->list);
2207 kfree(reg_request);
2208 }
2209 }
2210 spin_unlock(&reg_requests_lock);
2211
2212 mutex_unlock(&cfg80211_mutex);
1506} 2213}