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>
46 /* Receipt of information from last regulatory request */
47 static struct regulatory_request *last_request;
49 /* To trigger userspace events */
50 static struct platform_device *reg_pdev;
52 /* Keep the ordering from large to small */
53 static u32 supported_bandwidths[] = {
59 * Central wireless core regulatory domains, we only need two,
60 * the current one and a world regulatory domain in case we have no
61 * information to give us an alpha2
63 const struct ieee80211_regdomain *cfg80211_regdomain;
66 * We use this as a place for the rd structure built from the
67 * last parsed country IE to rest until CRDA gets back to us with
68 * what it thinks should apply for the same country
70 static const struct ieee80211_regdomain *country_ie_regdomain;
72 /* Used to queue up regulatory hints */
73 static LIST_HEAD(reg_requests_list);
74 static spinlock_t reg_requests_lock;
76 /* Used to queue up beacon hints for review */
77 static LIST_HEAD(reg_pending_beacons);
78 static spinlock_t reg_pending_beacons_lock;
80 /* Used to keep track of processed beacon hints */
81 static LIST_HEAD(reg_beacon_list);
84 struct list_head list;
85 struct ieee80211_channel chan;
88 /* We keep a static world regulatory domain in case of the absence of CRDA */
89 static const struct ieee80211_regdomain world_regdom = {
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 |
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),
111 /* NB: 5260 MHz - 5700 MHz requies DFS */
113 /* IEEE 802.11a, channel 149..165 */
114 REG_RULE(5745-10, 5825+10, 40, 6, 20,
115 NL80211_RRF_PASSIVE_SCAN |
116 NL80211_RRF_NO_IBSS),
120 static const struct ieee80211_regdomain *cfg80211_world_regdom =
123 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
124 static char *ieee80211_regdom = "US";
126 static char *ieee80211_regdom = "00";
129 module_param(ieee80211_regdom, charp, 0444);
130 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
132 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
134 * We assume 40 MHz bandwidth for the old regulatory work.
135 * We make emphasis we are using the exact same frequencies
139 static const struct ieee80211_regdomain us_regdom = {
143 /* IEEE 802.11b/g, channels 1..11 */
144 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
145 /* IEEE 802.11a, channel 36 */
146 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
147 /* IEEE 802.11a, channel 40 */
148 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
149 /* IEEE 802.11a, channel 44 */
150 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
151 /* IEEE 802.11a, channels 48..64 */
152 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
153 /* IEEE 802.11a, channels 149..165, outdoor */
154 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
158 static const struct ieee80211_regdomain jp_regdom = {
162 /* IEEE 802.11b/g, channels 1..14 */
163 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
164 /* IEEE 802.11a, channels 34..48 */
165 REG_RULE(5170-10, 5240+10, 40, 6, 20,
166 NL80211_RRF_PASSIVE_SCAN),
167 /* IEEE 802.11a, channels 52..64 */
168 REG_RULE(5260-10, 5320+10, 40, 6, 20,
169 NL80211_RRF_NO_IBSS |
174 static const struct ieee80211_regdomain eu_regdom = {
177 * This alpha2 is bogus, we leave it here just for stupid
178 * backward compatibility
182 /* IEEE 802.11b/g, channels 1..13 */
183 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
184 /* IEEE 802.11a, channel 36 */
185 REG_RULE(5180-10, 5180+10, 40, 6, 23,
186 NL80211_RRF_PASSIVE_SCAN),
187 /* IEEE 802.11a, channel 40 */
188 REG_RULE(5200-10, 5200+10, 40, 6, 23,
189 NL80211_RRF_PASSIVE_SCAN),
190 /* IEEE 802.11a, channel 44 */
191 REG_RULE(5220-10, 5220+10, 40, 6, 23,
192 NL80211_RRF_PASSIVE_SCAN),
193 /* IEEE 802.11a, channels 48..64 */
194 REG_RULE(5240-10, 5320+10, 40, 6, 20,
195 NL80211_RRF_NO_IBSS |
197 /* IEEE 802.11a, channels 100..140 */
198 REG_RULE(5500-10, 5700+10, 40, 6, 30,
199 NL80211_RRF_NO_IBSS |
204 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
206 if (alpha2[0] == 'U' && alpha2[1] == 'S')
208 if (alpha2[0] == 'J' && alpha2[1] == 'P')
210 if (alpha2[0] == 'E' && alpha2[1] == 'U')
212 /* Default, as per the old rules */
216 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
218 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
223 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
229 static void reset_regdomains(void)
231 /* avoid freeing static information or freeing something twice */
232 if (cfg80211_regdomain == cfg80211_world_regdom)
233 cfg80211_regdomain = NULL;
234 if (cfg80211_world_regdom == &world_regdom)
235 cfg80211_world_regdom = NULL;
236 if (cfg80211_regdomain == &world_regdom)
237 cfg80211_regdomain = NULL;
238 if (is_old_static_regdom(cfg80211_regdomain))
239 cfg80211_regdomain = NULL;
241 kfree(cfg80211_regdomain);
242 kfree(cfg80211_world_regdom);
244 cfg80211_world_regdom = &world_regdom;
245 cfg80211_regdomain = NULL;
249 * Dynamic world regulatory domain requested by the wireless
250 * core upon initialization
252 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
254 BUG_ON(!last_request);
258 cfg80211_world_regdom = rd;
259 cfg80211_regdomain = rd;
262 bool is_world_regdom(const char *alpha2)
266 if (alpha2[0] == '0' && alpha2[1] == '0')
271 static bool is_alpha2_set(const char *alpha2)
275 if (alpha2[0] != 0 && alpha2[1] != 0)
280 static bool is_alpha_upper(char letter)
283 if (letter >= 65 && letter <= 90)
288 static bool is_unknown_alpha2(const char *alpha2)
293 * Special case where regulatory domain was built by driver
294 * but a specific alpha2 cannot be determined
296 if (alpha2[0] == '9' && alpha2[1] == '9')
301 static bool is_intersected_alpha2(const char *alpha2)
306 * Special case where regulatory domain is the
307 * result of an intersection between two regulatory domain
310 if (alpha2[0] == '9' && alpha2[1] == '8')
315 static bool is_an_alpha2(const char *alpha2)
319 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
324 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
326 if (!alpha2_x || !alpha2_y)
328 if (alpha2_x[0] == alpha2_y[0] &&
329 alpha2_x[1] == alpha2_y[1])
334 static bool regdom_changes(const char *alpha2)
336 assert_cfg80211_lock();
338 if (!cfg80211_regdomain)
340 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
346 * country_ie_integrity_changes - tells us if the country IE has changed
347 * @checksum: checksum of country IE of fields we are interested in
349 * If the country IE has not changed you can ignore it safely. This is
350 * useful to determine if two devices are seeing two different country IEs
351 * even on the same alpha2. Note that this will return false if no IE has
352 * been set on the wireless core yet.
354 static bool country_ie_integrity_changes(u32 checksum)
356 /* If no IE has been set then the checksum doesn't change */
357 if (unlikely(!last_request->country_ie_checksum))
359 if (unlikely(last_request->country_ie_checksum != checksum))
365 * This lets us keep regulatory code which is updated on a regulatory
366 * basis in userspace.
368 static int call_crda(const char *alpha2)
370 char country_env[9 + 2] = "COUNTRY=";
376 if (!is_world_regdom((char *) alpha2))
377 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
378 alpha2[0], alpha2[1]);
380 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
381 "regulatory domain\n");
383 country_env[8] = alpha2[0];
384 country_env[9] = alpha2[1];
386 return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp);
389 /* Used by nl80211 before kmalloc'ing our regulatory domain */
390 bool reg_is_valid_request(const char *alpha2)
395 return alpha2_equal(last_request->alpha2, alpha2);
398 /* Sanity check on a regulatory rule */
399 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
401 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
404 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
407 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
410 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
412 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
413 freq_range->max_bandwidth_khz > freq_diff)
419 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
421 const struct ieee80211_reg_rule *reg_rule = NULL;
424 if (!rd->n_reg_rules)
427 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
430 for (i = 0; i < rd->n_reg_rules; i++) {
431 reg_rule = &rd->reg_rules[i];
432 if (!is_valid_reg_rule(reg_rule))
439 /* Returns value in KHz */
440 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
444 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
445 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
446 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
447 if (start_freq_khz >= freq_range->start_freq_khz &&
448 end_freq_khz <= freq_range->end_freq_khz)
449 return supported_bandwidths[i];
455 * freq_in_rule_band - tells us if a frequency is in a frequency band
456 * @freq_range: frequency rule we want to query
457 * @freq_khz: frequency we are inquiring about
459 * This lets us know if a specific frequency rule is or is not relevant to
460 * a specific frequency's band. Bands are device specific and artificial
461 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
462 * safe for now to assume that a frequency rule should not be part of a
463 * frequency's band if the start freq or end freq are off by more than 2 GHz.
464 * This resolution can be lowered and should be considered as we add
465 * regulatory rule support for other "bands".
467 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
470 #define ONE_GHZ_IN_KHZ 1000000
471 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
473 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
476 #undef ONE_GHZ_IN_KHZ
480 * Converts a country IE to a regulatory domain. A regulatory domain
481 * structure has a lot of information which the IE doesn't yet have,
482 * so for the other values we use upper max values as we will intersect
483 * with our userspace regulatory agent to get lower bounds.
485 static struct ieee80211_regdomain *country_ie_2_rd(
490 struct ieee80211_regdomain *rd = NULL;
494 u32 num_rules = 0, size_of_regd = 0;
495 u8 *triplets_start = NULL;
496 u8 len_at_triplet = 0;
497 /* the last channel we have registered in a subband (triplet) */
498 int last_sub_max_channel = 0;
500 *checksum = 0xDEADBEEF;
502 /* Country IE requirements */
503 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
504 country_ie_len & 0x01);
506 alpha2[0] = country_ie[0];
507 alpha2[1] = country_ie[1];
510 * Third octet can be:
514 * anything else we assume is no restrictions
516 if (country_ie[2] == 'I')
517 flags = NL80211_RRF_NO_OUTDOOR;
518 else if (country_ie[2] == 'O')
519 flags = NL80211_RRF_NO_INDOOR;
524 triplets_start = country_ie;
525 len_at_triplet = country_ie_len;
527 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
530 * We need to build a reg rule for each triplet, but first we must
531 * calculate the number of reg rules we will need. We will need one
532 * for each channel subband
534 while (country_ie_len >= 3) {
536 struct ieee80211_country_ie_triplet *triplet =
537 (struct ieee80211_country_ie_triplet *) country_ie;
538 int cur_sub_max_channel = 0, cur_channel = 0;
540 if (triplet->ext.reg_extension_id >=
541 IEEE80211_COUNTRY_EXTENSION_ID) {
548 if (triplet->chans.first_channel <= 14)
549 end_channel = triplet->chans.first_channel +
550 triplet->chans.num_channels;
553 * 5 GHz -- For example in country IEs if the first
554 * channel given is 36 and the number of channels is 4
555 * then the individual channel numbers defined for the
556 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
557 * and not 36, 37, 38, 39.
559 * See: http://tinyurl.com/11d-clarification
561 end_channel = triplet->chans.first_channel +
562 (4 * (triplet->chans.num_channels - 1));
564 cur_channel = triplet->chans.first_channel;
565 cur_sub_max_channel = end_channel;
567 /* Basic sanity check */
568 if (cur_sub_max_channel < cur_channel)
572 * Do not allow overlapping channels. Also channels
573 * passed in each subband must be monotonically
576 if (last_sub_max_channel) {
577 if (cur_channel <= last_sub_max_channel)
579 if (cur_sub_max_channel <= last_sub_max_channel)
584 * When dot11RegulatoryClassesRequired is supported
585 * we can throw ext triplets as part of this soup,
586 * for now we don't care when those change as we
589 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
590 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
591 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
593 last_sub_max_channel = cur_sub_max_channel;
600 * Note: this is not a IEEE requirement but
601 * simply a memory requirement
603 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
607 country_ie = triplets_start;
608 country_ie_len = len_at_triplet;
610 size_of_regd = sizeof(struct ieee80211_regdomain) +
611 (num_rules * sizeof(struct ieee80211_reg_rule));
613 rd = kzalloc(size_of_regd, GFP_KERNEL);
617 rd->n_reg_rules = num_rules;
618 rd->alpha2[0] = alpha2[0];
619 rd->alpha2[1] = alpha2[1];
621 /* This time around we fill in the rd */
622 while (country_ie_len >= 3) {
624 struct ieee80211_country_ie_triplet *triplet =
625 (struct ieee80211_country_ie_triplet *) country_ie;
626 struct ieee80211_reg_rule *reg_rule = NULL;
627 struct ieee80211_freq_range *freq_range = NULL;
628 struct ieee80211_power_rule *power_rule = NULL;
631 * Must parse if dot11RegulatoryClassesRequired is true,
632 * we don't support this yet
634 if (triplet->ext.reg_extension_id >=
635 IEEE80211_COUNTRY_EXTENSION_ID) {
641 reg_rule = &rd->reg_rules[i];
642 freq_range = ®_rule->freq_range;
643 power_rule = ®_rule->power_rule;
645 reg_rule->flags = flags;
648 if (triplet->chans.first_channel <= 14)
649 end_channel = triplet->chans.first_channel +
650 triplet->chans.num_channels;
652 end_channel = triplet->chans.first_channel +
653 (4 * (triplet->chans.num_channels - 1));
656 * The +10 is since the regulatory domain expects
657 * the actual band edge, not the center of freq for
658 * its start and end freqs, assuming 20 MHz bandwidth on
659 * the channels passed
661 freq_range->start_freq_khz =
662 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
663 triplet->chans.first_channel) - 10);
664 freq_range->end_freq_khz =
665 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
669 * These are large arbitrary values we use to intersect later.
670 * Increment this if we ever support >= 40 MHz channels
673 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
674 power_rule->max_antenna_gain = DBI_TO_MBI(100);
675 power_rule->max_eirp = DBM_TO_MBM(100);
681 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
689 * Helper for regdom_intersect(), this does the real
690 * mathematical intersection fun
692 static int reg_rules_intersect(
693 const struct ieee80211_reg_rule *rule1,
694 const struct ieee80211_reg_rule *rule2,
695 struct ieee80211_reg_rule *intersected_rule)
697 const struct ieee80211_freq_range *freq_range1, *freq_range2;
698 struct ieee80211_freq_range *freq_range;
699 const struct ieee80211_power_rule *power_rule1, *power_rule2;
700 struct ieee80211_power_rule *power_rule;
703 freq_range1 = &rule1->freq_range;
704 freq_range2 = &rule2->freq_range;
705 freq_range = &intersected_rule->freq_range;
707 power_rule1 = &rule1->power_rule;
708 power_rule2 = &rule2->power_rule;
709 power_rule = &intersected_rule->power_rule;
711 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
712 freq_range2->start_freq_khz);
713 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
714 freq_range2->end_freq_khz);
715 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
716 freq_range2->max_bandwidth_khz);
718 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
719 if (freq_range->max_bandwidth_khz > freq_diff)
720 freq_range->max_bandwidth_khz = freq_diff;
722 power_rule->max_eirp = min(power_rule1->max_eirp,
723 power_rule2->max_eirp);
724 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
725 power_rule2->max_antenna_gain);
727 intersected_rule->flags = (rule1->flags | rule2->flags);
729 if (!is_valid_reg_rule(intersected_rule))
736 * regdom_intersect - do the intersection between two regulatory domains
737 * @rd1: first regulatory domain
738 * @rd2: second regulatory domain
740 * Use this function to get the intersection between two regulatory domains.
741 * Once completed we will mark the alpha2 for the rd as intersected, "98",
742 * as no one single alpha2 can represent this regulatory domain.
744 * Returns a pointer to the regulatory domain structure which will hold the
745 * resulting intersection of rules between rd1 and rd2. We will
746 * kzalloc() this structure for you.
748 static struct ieee80211_regdomain *regdom_intersect(
749 const struct ieee80211_regdomain *rd1,
750 const struct ieee80211_regdomain *rd2)
754 unsigned int num_rules = 0, rule_idx = 0;
755 const struct ieee80211_reg_rule *rule1, *rule2;
756 struct ieee80211_reg_rule *intersected_rule;
757 struct ieee80211_regdomain *rd;
758 /* This is just a dummy holder to help us count */
759 struct ieee80211_reg_rule irule;
761 /* Uses the stack temporarily for counter arithmetic */
762 intersected_rule = &irule;
764 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
770 * First we get a count of the rules we'll need, then we actually
771 * build them. This is to so we can malloc() and free() a
772 * regdomain once. The reason we use reg_rules_intersect() here
773 * is it will return -EINVAL if the rule computed makes no sense.
774 * All rules that do check out OK are valid.
777 for (x = 0; x < rd1->n_reg_rules; x++) {
778 rule1 = &rd1->reg_rules[x];
779 for (y = 0; y < rd2->n_reg_rules; y++) {
780 rule2 = &rd2->reg_rules[y];
781 if (!reg_rules_intersect(rule1, rule2,
784 memset(intersected_rule, 0,
785 sizeof(struct ieee80211_reg_rule));
792 size_of_regd = sizeof(struct ieee80211_regdomain) +
793 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
795 rd = kzalloc(size_of_regd, GFP_KERNEL);
799 for (x = 0; x < rd1->n_reg_rules; x++) {
800 rule1 = &rd1->reg_rules[x];
801 for (y = 0; y < rd2->n_reg_rules; y++) {
802 rule2 = &rd2->reg_rules[y];
804 * This time around instead of using the stack lets
805 * write to the target rule directly saving ourselves
808 intersected_rule = &rd->reg_rules[rule_idx];
809 r = reg_rules_intersect(rule1, rule2,
812 * No need to memset here the intersected rule here as
813 * we're not using the stack anymore
821 if (rule_idx != num_rules) {
826 rd->n_reg_rules = num_rules;
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
837 static u32 map_regdom_flags(u32 rd_flags)
839 u32 channel_flags = 0;
840 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
841 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
842 if (rd_flags & NL80211_RRF_NO_IBSS)
843 channel_flags |= IEEE80211_CHAN_NO_IBSS;
844 if (rd_flags & NL80211_RRF_DFS)
845 channel_flags |= IEEE80211_CHAN_RADAR;
846 return channel_flags;
849 static int freq_reg_info_regd(struct wiphy *wiphy,
852 const struct ieee80211_reg_rule **reg_rule,
853 const struct ieee80211_regdomain *custom_regd)
856 bool band_rule_found = false;
857 const struct ieee80211_regdomain *regd;
858 u32 max_bandwidth = 0;
860 regd = custom_regd ? custom_regd : cfg80211_regdomain;
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
866 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
867 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
874 for (i = 0; i < regd->n_reg_rules; i++) {
875 const struct ieee80211_reg_rule *rr;
876 const struct ieee80211_freq_range *fr = NULL;
877 const struct ieee80211_power_rule *pr = NULL;
879 rr = ®d->reg_rules[i];
880 fr = &rr->freq_range;
881 pr = &rr->power_rule;
884 * We only need to know if one frequency rule was
885 * was in center_freq's band, that's enough, so lets
886 * not overwrite it once found
888 if (!band_rule_found)
889 band_rule_found = freq_in_rule_band(fr, center_freq);
891 max_bandwidth = freq_max_bandwidth(fr, center_freq);
893 if (max_bandwidth && *bandwidth <= max_bandwidth) {
895 *bandwidth = max_bandwidth;
900 if (!band_rule_found)
903 return !max_bandwidth;
905 EXPORT_SYMBOL(freq_reg_info);
907 int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
908 const struct ieee80211_reg_rule **reg_rule)
910 assert_cfg80211_lock();
911 return freq_reg_info_regd(wiphy, center_freq,
912 bandwidth, reg_rule, NULL);
915 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
916 unsigned int chan_idx)
920 u32 max_bandwidth = 0;
921 const struct ieee80211_reg_rule *reg_rule = NULL;
922 const struct ieee80211_power_rule *power_rule = NULL;
923 struct ieee80211_supported_band *sband;
924 struct ieee80211_channel *chan;
925 struct wiphy *request_wiphy = NULL;
927 assert_cfg80211_lock();
929 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
931 sband = wiphy->bands[band];
932 BUG_ON(chan_idx >= sband->n_channels);
933 chan = &sband->channels[chan_idx];
935 flags = chan->orig_flags;
937 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
938 &max_bandwidth, ®_rule);
942 * This means no regulatory rule was found in the country IE
943 * with a frequency range on the center_freq's band, since
944 * IEEE-802.11 allows for a country IE to have a subset of the
945 * regulatory information provided in a country we ignore
946 * disabling the channel unless at least one reg rule was
947 * found on the center_freq's band. For details see this
950 * http://tinyurl.com/11d-clarification
953 last_request->initiator ==
954 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
955 #ifdef CONFIG_CFG80211_REG_DEBUG
956 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
957 "intact on %s - no rule found in band on "
959 chan->center_freq, wiphy_name(wiphy));
963 * In this case we know the country IE has at least one reg rule
964 * for the band so we respect its band definitions
966 #ifdef CONFIG_CFG80211_REG_DEBUG
967 if (last_request->initiator ==
968 NL80211_REGDOM_SET_BY_COUNTRY_IE)
969 printk(KERN_DEBUG "cfg80211: Disabling "
970 "channel %d MHz on %s due to "
972 chan->center_freq, wiphy_name(wiphy));
974 flags |= IEEE80211_CHAN_DISABLED;
980 power_rule = ®_rule->power_rule;
982 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
983 request_wiphy && request_wiphy == wiphy &&
984 request_wiphy->strict_regulatory) {
986 * This gaurantees the driver's requested regulatory domain
987 * will always be used as a base for further regulatory
990 chan->flags = chan->orig_flags =
991 map_regdom_flags(reg_rule->flags);
992 chan->max_antenna_gain = chan->orig_mag =
993 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
994 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
995 chan->max_power = chan->orig_mpwr =
996 (int) MBM_TO_DBM(power_rule->max_eirp);
1000 chan->flags = flags | map_regdom_flags(reg_rule->flags);
1001 chan->max_antenna_gain = min(chan->orig_mag,
1002 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
1003 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1004 if (chan->orig_mpwr)
1005 chan->max_power = min(chan->orig_mpwr,
1006 (int) MBM_TO_DBM(power_rule->max_eirp));
1008 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1011 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
1014 struct ieee80211_supported_band *sband;
1016 BUG_ON(!wiphy->bands[band]);
1017 sband = wiphy->bands[band];
1019 for (i = 0; i < sband->n_channels; i++)
1020 handle_channel(wiphy, band, i);
1023 static bool ignore_reg_update(struct wiphy *wiphy,
1024 enum nl80211_reg_initiator initiator)
1028 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1029 wiphy->custom_regulatory)
1032 * wiphy->regd will be set once the device has its own
1033 * desired regulatory domain set
1035 if (wiphy->strict_regulatory && !wiphy->regd &&
1036 !is_world_regdom(last_request->alpha2))
1041 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1043 struct cfg80211_registered_device *drv;
1045 list_for_each_entry(drv, &cfg80211_drv_list, list)
1046 wiphy_update_regulatory(&drv->wiphy, initiator);
1049 static void handle_reg_beacon(struct wiphy *wiphy,
1050 unsigned int chan_idx,
1051 struct reg_beacon *reg_beacon)
1053 #ifdef CONFIG_CFG80211_REG_DEBUG
1054 #define REG_DEBUG_BEACON_FLAG(desc) \
1055 printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \
1056 "frequency: %d MHz (Ch %d) on %s\n", \
1057 reg_beacon->chan.center_freq, \
1058 ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \
1061 #define REG_DEBUG_BEACON_FLAG(desc) do {} while (0)
1063 struct ieee80211_supported_band *sband;
1064 struct ieee80211_channel *chan;
1066 assert_cfg80211_lock();
1068 sband = wiphy->bands[reg_beacon->chan.band];
1069 chan = &sband->channels[chan_idx];
1071 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1074 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1075 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1076 REG_DEBUG_BEACON_FLAG("active scanning");
1079 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1080 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1081 REG_DEBUG_BEACON_FLAG("beaconing");
1084 chan->beacon_found = true;
1085 #undef REG_DEBUG_BEACON_FLAG
1089 * Called when a scan on a wiphy finds a beacon on
1092 static void wiphy_update_new_beacon(struct wiphy *wiphy,
1093 struct reg_beacon *reg_beacon)
1096 struct ieee80211_supported_band *sband;
1098 assert_cfg80211_lock();
1100 if (!wiphy->bands[reg_beacon->chan.band])
1103 sband = wiphy->bands[reg_beacon->chan.band];
1105 for (i = 0; i < sband->n_channels; i++)
1106 handle_reg_beacon(wiphy, i, reg_beacon);
1110 * Called upon reg changes or a new wiphy is added
1112 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1115 struct ieee80211_supported_band *sband;
1116 struct reg_beacon *reg_beacon;
1118 assert_cfg80211_lock();
1120 if (list_empty(®_beacon_list))
1123 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
1124 if (!wiphy->bands[reg_beacon->chan.band])
1126 sband = wiphy->bands[reg_beacon->chan.band];
1127 for (i = 0; i < sband->n_channels; i++)
1128 handle_reg_beacon(wiphy, i, reg_beacon);
1132 static bool reg_is_world_roaming(struct wiphy *wiphy)
1134 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1135 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1138 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1139 wiphy->custom_regulatory)
1144 /* Reap the advantages of previously found beacons */
1145 static void reg_process_beacons(struct wiphy *wiphy)
1148 * Means we are just firing up cfg80211, so no beacons would
1149 * have been processed yet.
1153 if (!reg_is_world_roaming(wiphy))
1155 wiphy_update_beacon_reg(wiphy);
1158 void wiphy_update_regulatory(struct wiphy *wiphy,
1159 enum nl80211_reg_initiator initiator)
1161 enum ieee80211_band band;
1163 if (ignore_reg_update(wiphy, initiator))
1165 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1166 if (wiphy->bands[band])
1167 handle_band(wiphy, band);
1170 reg_process_beacons(wiphy);
1171 if (wiphy->reg_notifier)
1172 wiphy->reg_notifier(wiphy, last_request);
1175 static void handle_channel_custom(struct wiphy *wiphy,
1176 enum ieee80211_band band,
1177 unsigned int chan_idx,
1178 const struct ieee80211_regdomain *regd)
1181 u32 max_bandwidth = 0;
1182 const struct ieee80211_reg_rule *reg_rule = NULL;
1183 const struct ieee80211_power_rule *power_rule = NULL;
1184 struct ieee80211_supported_band *sband;
1185 struct ieee80211_channel *chan;
1187 assert_cfg80211_lock();
1189 sband = wiphy->bands[band];
1190 BUG_ON(chan_idx >= sband->n_channels);
1191 chan = &sband->channels[chan_idx];
1193 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1194 &max_bandwidth, ®_rule, regd);
1197 chan->flags = IEEE80211_CHAN_DISABLED;
1201 power_rule = ®_rule->power_rule;
1203 chan->flags |= map_regdom_flags(reg_rule->flags);
1204 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1205 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1206 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1209 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1210 const struct ieee80211_regdomain *regd)
1213 struct ieee80211_supported_band *sband;
1215 BUG_ON(!wiphy->bands[band]);
1216 sband = wiphy->bands[band];
1218 for (i = 0; i < sband->n_channels; i++)
1219 handle_channel_custom(wiphy, band, i, regd);
1222 /* Used by drivers prior to wiphy registration */
1223 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1224 const struct ieee80211_regdomain *regd)
1226 enum ieee80211_band band;
1228 mutex_lock(&cfg80211_mutex);
1229 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1230 if (wiphy->bands[band])
1231 handle_band_custom(wiphy, band, regd);
1233 mutex_unlock(&cfg80211_mutex);
1235 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1237 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1238 const struct ieee80211_regdomain *src_regd)
1240 struct ieee80211_regdomain *regd;
1241 int size_of_regd = 0;
1244 size_of_regd = sizeof(struct ieee80211_regdomain) +
1245 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1247 regd = kzalloc(size_of_regd, GFP_KERNEL);
1251 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1253 for (i = 0; i < src_regd->n_reg_rules; i++)
1254 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i],
1255 sizeof(struct ieee80211_reg_rule));
1262 * Return value which can be used by ignore_request() to indicate
1263 * it has been determined we should intersect two regulatory domains
1265 #define REG_INTERSECT 1
1267 /* This has the logic which determines when a new request
1268 * should be ignored. */
1269 static int ignore_request(struct wiphy *wiphy,
1270 struct regulatory_request *pending_request)
1272 struct wiphy *last_wiphy = NULL;
1274 assert_cfg80211_lock();
1276 /* All initial requests are respected */
1280 switch (pending_request->initiator) {
1281 case NL80211_REGDOM_SET_BY_CORE:
1283 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1285 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1287 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
1289 if (last_request->initiator ==
1290 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1291 if (last_wiphy != wiphy) {
1293 * Two cards with two APs claiming different
1294 * different Country IE alpha2s. We could
1295 * intersect them, but that seems unlikely
1296 * to be correct. Reject second one for now.
1298 if (regdom_changes(pending_request->alpha2))
1303 * Two consecutive Country IE hints on the same wiphy.
1304 * This should be picked up early by the driver/stack
1306 if (WARN_ON(regdom_changes(pending_request->alpha2)))
1310 return REG_INTERSECT;
1311 case NL80211_REGDOM_SET_BY_DRIVER:
1312 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1313 if (is_old_static_regdom(cfg80211_regdomain))
1315 if (regdom_changes(pending_request->alpha2))
1321 * This would happen if you unplug and plug your card
1322 * back in or if you add a new device for which the previously
1323 * loaded card also agrees on the regulatory domain.
1325 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1326 !regdom_changes(pending_request->alpha2))
1329 return REG_INTERSECT;
1330 case NL80211_REGDOM_SET_BY_USER:
1331 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1332 return REG_INTERSECT;
1334 * If the user knows better the user should set the regdom
1335 * to their country before the IE is picked up
1337 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1338 last_request->intersect)
1341 * Process user requests only after previous user/driver/core
1342 * requests have been processed
1344 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1345 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1346 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1347 if (regdom_changes(last_request->alpha2))
1351 if (!is_old_static_regdom(cfg80211_regdomain) &&
1352 !regdom_changes(pending_request->alpha2))
1362 * __regulatory_hint - hint to the wireless core a regulatory domain
1363 * @wiphy: if the hint comes from country information from an AP, this
1364 * is required to be set to the wiphy that received the information
1365 * @pending_request: the regulatory request currently being processed
1367 * The Wireless subsystem can use this function to hint to the wireless core
1368 * what it believes should be the current regulatory domain.
1370 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1371 * already been set or other standard error codes.
1373 * Caller must hold &cfg80211_mutex
1375 static int __regulatory_hint(struct wiphy *wiphy,
1376 struct regulatory_request *pending_request)
1378 bool intersect = false;
1381 assert_cfg80211_lock();
1383 r = ignore_request(wiphy, pending_request);
1385 if (r == REG_INTERSECT) {
1386 if (pending_request->initiator ==
1387 NL80211_REGDOM_SET_BY_DRIVER) {
1388 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1390 kfree(pending_request);
1397 * If the regulatory domain being requested by the
1398 * driver has already been set just copy it to the
1401 if (r == -EALREADY &&
1402 pending_request->initiator ==
1403 NL80211_REGDOM_SET_BY_DRIVER) {
1404 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1406 kfree(pending_request);
1412 kfree(pending_request);
1417 kfree(last_request);
1419 last_request = pending_request;
1420 last_request->intersect = intersect;
1422 pending_request = NULL;
1424 /* When r == REG_INTERSECT we do need to call CRDA */
1427 * Since CRDA will not be called in this case as we already
1428 * have applied the requested regulatory domain before we just
1429 * inform userspace we have processed the request
1432 nl80211_send_reg_change_event(last_request);
1436 return call_crda(last_request->alpha2);
1439 /* This processes *all* regulatory hints */
1440 static void reg_process_hint(struct regulatory_request *reg_request)
1443 struct wiphy *wiphy = NULL;
1445 BUG_ON(!reg_request->alpha2);
1447 mutex_lock(&cfg80211_mutex);
1449 if (wiphy_idx_valid(reg_request->wiphy_idx))
1450 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1452 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1458 r = __regulatory_hint(wiphy, reg_request);
1459 /* This is required so that the orig_* parameters are saved */
1460 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1461 wiphy_update_regulatory(wiphy, reg_request->initiator);
1463 mutex_unlock(&cfg80211_mutex);
1466 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1467 static void reg_process_pending_hints(void)
1469 struct regulatory_request *reg_request;
1471 spin_lock(®_requests_lock);
1472 while (!list_empty(®_requests_list)) {
1473 reg_request = list_first_entry(®_requests_list,
1474 struct regulatory_request,
1476 list_del_init(®_request->list);
1478 spin_unlock(®_requests_lock);
1479 reg_process_hint(reg_request);
1480 spin_lock(®_requests_lock);
1482 spin_unlock(®_requests_lock);
1485 /* Processes beacon hints -- this has nothing to do with country IEs */
1486 static void reg_process_pending_beacon_hints(void)
1488 struct cfg80211_registered_device *drv;
1489 struct reg_beacon *pending_beacon, *tmp;
1491 mutex_lock(&cfg80211_mutex);
1493 /* This goes through the _pending_ beacon list */
1494 spin_lock_bh(®_pending_beacons_lock);
1496 if (list_empty(®_pending_beacons)) {
1497 spin_unlock_bh(®_pending_beacons_lock);
1501 list_for_each_entry_safe(pending_beacon, tmp,
1502 ®_pending_beacons, list) {
1504 list_del_init(&pending_beacon->list);
1506 /* Applies the beacon hint to current wiphys */
1507 list_for_each_entry(drv, &cfg80211_drv_list, list)
1508 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1510 /* Remembers the beacon hint for new wiphys or reg changes */
1511 list_add_tail(&pending_beacon->list, ®_beacon_list);
1514 spin_unlock_bh(®_pending_beacons_lock);
1516 mutex_unlock(&cfg80211_mutex);
1519 static void reg_todo(struct work_struct *work)
1521 reg_process_pending_hints();
1522 reg_process_pending_beacon_hints();
1525 static DECLARE_WORK(reg_work, reg_todo);
1527 static void queue_regulatory_request(struct regulatory_request *request)
1529 spin_lock(®_requests_lock);
1530 list_add_tail(&request->list, ®_requests_list);
1531 spin_unlock(®_requests_lock);
1533 schedule_work(®_work);
1536 /* Core regulatory hint -- happens once during cfg80211_init() */
1537 static int regulatory_hint_core(const char *alpha2)
1539 struct regulatory_request *request;
1541 BUG_ON(last_request);
1543 request = kzalloc(sizeof(struct regulatory_request),
1548 request->alpha2[0] = alpha2[0];
1549 request->alpha2[1] = alpha2[1];
1550 request->initiator = NL80211_REGDOM_SET_BY_CORE;
1552 queue_regulatory_request(request);
1555 * This ensures last_request is populated once modules
1556 * come swinging in and calling regulatory hints and
1557 * wiphy_apply_custom_regulatory().
1559 flush_scheduled_work();
1565 int regulatory_hint_user(const char *alpha2)
1567 struct regulatory_request *request;
1571 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1575 request->wiphy_idx = WIPHY_IDX_STALE;
1576 request->alpha2[0] = alpha2[0];
1577 request->alpha2[1] = alpha2[1];
1578 request->initiator = NL80211_REGDOM_SET_BY_USER,
1580 queue_regulatory_request(request);
1586 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1588 struct regulatory_request *request;
1593 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1597 request->wiphy_idx = get_wiphy_idx(wiphy);
1599 /* Must have registered wiphy first */
1600 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1602 request->alpha2[0] = alpha2[0];
1603 request->alpha2[1] = alpha2[1];
1604 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1606 queue_regulatory_request(request);
1610 EXPORT_SYMBOL(regulatory_hint);
1612 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1613 u32 country_ie_checksum)
1615 struct wiphy *request_wiphy;
1617 assert_cfg80211_lock();
1619 if (unlikely(last_request->initiator !=
1620 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1623 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1628 if (likely(request_wiphy != wiphy))
1629 return !country_ie_integrity_changes(country_ie_checksum);
1631 * We should not have let these through at this point, they
1632 * should have been picked up earlier by the first alpha2 check
1635 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1640 void regulatory_hint_11d(struct wiphy *wiphy,
1644 struct ieee80211_regdomain *rd = NULL;
1647 enum environment_cap env = ENVIRON_ANY;
1648 struct regulatory_request *request;
1650 mutex_lock(&cfg80211_mutex);
1652 if (unlikely(!last_request)) {
1653 mutex_unlock(&cfg80211_mutex);
1657 /* IE len must be evenly divisible by 2 */
1658 if (country_ie_len & 0x01)
1661 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1665 * Pending country IE processing, this can happen after we
1666 * call CRDA and wait for a response if a beacon was received before
1667 * we were able to process the last regulatory_hint_11d() call
1669 if (country_ie_regdomain)
1672 alpha2[0] = country_ie[0];
1673 alpha2[1] = country_ie[1];
1675 if (country_ie[2] == 'I')
1676 env = ENVIRON_INDOOR;
1677 else if (country_ie[2] == 'O')
1678 env = ENVIRON_OUTDOOR;
1681 * We will run this for *every* beacon processed for the BSSID, so
1682 * we optimize an early check to exit out early if we don't have to
1685 if (likely(last_request->initiator ==
1686 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1687 wiphy_idx_valid(last_request->wiphy_idx))) {
1688 struct cfg80211_registered_device *drv_last_ie;
1691 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
1694 * Lets keep this simple -- we trust the first AP
1695 * after we intersect with CRDA
1697 if (likely(&drv_last_ie->wiphy == wiphy)) {
1699 * Ignore IEs coming in on this wiphy with
1700 * the same alpha2 and environment cap
1702 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1704 env == drv_last_ie->env)) {
1708 * the wiphy moved on to another BSSID or the AP
1709 * was reconfigured. XXX: We need to deal with the
1710 * case where the user suspends and goes to goes
1711 * to another country, and then gets IEs from an
1712 * AP with different settings
1717 * Ignore IEs coming in on two separate wiphys with
1718 * the same alpha2 and environment cap
1720 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1722 env == drv_last_ie->env)) {
1725 /* We could potentially intersect though */
1730 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1735 * This will not happen right now but we leave it here for the
1736 * the future when we want to add suspend/resume support and having
1737 * the user move to another country after doing so, or having the user
1738 * move to another AP. Right now we just trust the first AP.
1740 * If we hit this before we add this support we want to be informed of
1741 * it as it would indicate a mistake in the current design
1743 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
1746 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1751 * We keep this around for when CRDA comes back with a response so
1752 * we can intersect with that
1754 country_ie_regdomain = rd;
1756 request->wiphy_idx = get_wiphy_idx(wiphy);
1757 request->alpha2[0] = rd->alpha2[0];
1758 request->alpha2[1] = rd->alpha2[1];
1759 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
1760 request->country_ie_checksum = checksum;
1761 request->country_ie_env = env;
1763 mutex_unlock(&cfg80211_mutex);
1765 queue_regulatory_request(request);
1772 mutex_unlock(&cfg80211_mutex);
1774 EXPORT_SYMBOL(regulatory_hint_11d);
1776 static bool freq_is_chan_12_13_14(u16 freq)
1778 if (freq == ieee80211_channel_to_frequency(12) ||
1779 freq == ieee80211_channel_to_frequency(13) ||
1780 freq == ieee80211_channel_to_frequency(14))
1785 int regulatory_hint_found_beacon(struct wiphy *wiphy,
1786 struct ieee80211_channel *beacon_chan,
1789 struct reg_beacon *reg_beacon;
1791 if (likely((beacon_chan->beacon_found ||
1792 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1793 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1794 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1797 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1801 #ifdef CONFIG_CFG80211_REG_DEBUG
1802 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1803 "frequency: %d MHz (Ch %d) on %s\n",
1804 beacon_chan->center_freq,
1805 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1808 memcpy(®_beacon->chan, beacon_chan,
1809 sizeof(struct ieee80211_channel));
1813 * Since we can be called from BH or and non-BH context
1814 * we must use spin_lock_bh()
1816 spin_lock_bh(®_pending_beacons_lock);
1817 list_add_tail(®_beacon->list, ®_pending_beacons);
1818 spin_unlock_bh(®_pending_beacons_lock);
1820 schedule_work(®_work);
1825 static void print_rd_rules(const struct ieee80211_regdomain *rd)
1828 const struct ieee80211_reg_rule *reg_rule = NULL;
1829 const struct ieee80211_freq_range *freq_range = NULL;
1830 const struct ieee80211_power_rule *power_rule = NULL;
1832 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1833 "(max_antenna_gain, max_eirp)\n");
1835 for (i = 0; i < rd->n_reg_rules; i++) {
1836 reg_rule = &rd->reg_rules[i];
1837 freq_range = ®_rule->freq_range;
1838 power_rule = ®_rule->power_rule;
1841 * There may not be documentation for max antenna gain
1842 * in certain regions
1844 if (power_rule->max_antenna_gain)
1845 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1846 "(%d mBi, %d mBm)\n",
1847 freq_range->start_freq_khz,
1848 freq_range->end_freq_khz,
1849 freq_range->max_bandwidth_khz,
1850 power_rule->max_antenna_gain,
1851 power_rule->max_eirp);
1853 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1855 freq_range->start_freq_khz,
1856 freq_range->end_freq_khz,
1857 freq_range->max_bandwidth_khz,
1858 power_rule->max_eirp);
1862 static void print_regdomain(const struct ieee80211_regdomain *rd)
1865 if (is_intersected_alpha2(rd->alpha2)) {
1867 if (last_request->initiator ==
1868 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1869 struct cfg80211_registered_device *drv;
1870 drv = cfg80211_drv_by_wiphy_idx(
1871 last_request->wiphy_idx);
1873 printk(KERN_INFO "cfg80211: Current regulatory "
1874 "domain updated by AP to: %c%c\n",
1875 drv->country_ie_alpha2[0],
1876 drv->country_ie_alpha2[1]);
1878 printk(KERN_INFO "cfg80211: Current regulatory "
1879 "domain intersected: \n");
1881 printk(KERN_INFO "cfg80211: Current regulatory "
1882 "domain intersected: \n");
1883 } else if (is_world_regdom(rd->alpha2))
1884 printk(KERN_INFO "cfg80211: World regulatory "
1885 "domain updated:\n");
1887 if (is_unknown_alpha2(rd->alpha2))
1888 printk(KERN_INFO "cfg80211: Regulatory domain "
1889 "changed to driver built-in settings "
1890 "(unknown country)\n");
1892 printk(KERN_INFO "cfg80211: Regulatory domain "
1893 "changed to country: %c%c\n",
1894 rd->alpha2[0], rd->alpha2[1]);
1899 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
1901 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1902 rd->alpha2[0], rd->alpha2[1]);
1906 #ifdef CONFIG_CFG80211_REG_DEBUG
1907 static void reg_country_ie_process_debug(
1908 const struct ieee80211_regdomain *rd,
1909 const struct ieee80211_regdomain *country_ie_regdomain,
1910 const struct ieee80211_regdomain *intersected_rd)
1912 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1913 print_regdomain_info(country_ie_regdomain);
1914 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1915 print_regdomain_info(rd);
1916 if (intersected_rd) {
1917 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1919 print_regdomain_info(intersected_rd);
1922 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1925 static inline void reg_country_ie_process_debug(
1926 const struct ieee80211_regdomain *rd,
1927 const struct ieee80211_regdomain *country_ie_regdomain,
1928 const struct ieee80211_regdomain *intersected_rd)
1933 /* Takes ownership of rd only if it doesn't fail */
1934 static int __set_regdom(const struct ieee80211_regdomain *rd)
1936 const struct ieee80211_regdomain *intersected_rd = NULL;
1937 struct cfg80211_registered_device *drv = NULL;
1938 struct wiphy *request_wiphy;
1939 /* Some basic sanity checks first */
1941 if (is_world_regdom(rd->alpha2)) {
1942 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1944 update_world_regdomain(rd);
1948 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1949 !is_unknown_alpha2(rd->alpha2))
1956 * Lets only bother proceeding on the same alpha2 if the current
1957 * rd is non static (it means CRDA was present and was used last)
1958 * and the pending request came in from a country IE
1960 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1962 * If someone else asked us to change the rd lets only bother
1963 * checking if the alpha2 changes if CRDA was already called
1965 if (!is_old_static_regdom(cfg80211_regdomain) &&
1966 !regdom_changes(rd->alpha2))
1971 * Now lets set the regulatory domain, update all driver channels
1972 * and finally inform them of what we have done, in case they want
1973 * to review or adjust their own settings based on their own
1974 * internal EEPROM data
1977 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1980 if (!is_valid_rd(rd)) {
1981 printk(KERN_ERR "cfg80211: Invalid "
1982 "regulatory domain detected:\n");
1983 print_regdomain_info(rd);
1987 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1989 if (!last_request->intersect) {
1992 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
1994 cfg80211_regdomain = rd;
1999 * For a driver hint, lets copy the regulatory domain the
2000 * driver wanted to the wiphy to deal with conflicts
2003 BUG_ON(request_wiphy->regd);
2005 r = reg_copy_regd(&request_wiphy->regd, rd);
2010 cfg80211_regdomain = rd;
2014 /* Intersection requires a bit more work */
2016 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2018 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2019 if (!intersected_rd)
2023 * We can trash what CRDA provided now.
2024 * However if a driver requested this specific regulatory
2025 * domain we keep it for its private use
2027 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2028 request_wiphy->regd = rd;
2035 cfg80211_regdomain = intersected_rd;
2041 * Country IE requests are handled a bit differently, we intersect
2042 * the country IE rd with what CRDA believes that country should have
2045 BUG_ON(!country_ie_regdomain);
2046 BUG_ON(rd == country_ie_regdomain);
2049 * Intersect what CRDA returned and our what we
2050 * had built from the Country IE received
2053 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2055 reg_country_ie_process_debug(rd,
2056 country_ie_regdomain,
2059 kfree(country_ie_regdomain);
2060 country_ie_regdomain = NULL;
2062 if (!intersected_rd)
2065 drv = wiphy_to_dev(request_wiphy);
2067 drv->country_ie_alpha2[0] = rd->alpha2[0];
2068 drv->country_ie_alpha2[1] = rd->alpha2[1];
2069 drv->env = last_request->country_ie_env;
2071 BUG_ON(intersected_rd == rd);
2077 cfg80211_regdomain = intersected_rd;
2084 * Use this call to set the current regulatory domain. Conflicts with
2085 * multiple drivers can be ironed out later. Caller must've already
2086 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2088 int set_regdom(const struct ieee80211_regdomain *rd)
2092 assert_cfg80211_lock();
2094 /* Note that this doesn't update the wiphys, this is done below */
2095 r = __set_regdom(rd);
2101 /* This would make this whole thing pointless */
2102 if (!last_request->intersect)
2103 BUG_ON(rd != cfg80211_regdomain);
2105 /* update all wiphys now with the new established regulatory domain */
2106 update_all_wiphy_regulatory(last_request->initiator);
2108 print_regdomain(cfg80211_regdomain);
2110 nl80211_send_reg_change_event(last_request);
2115 /* Caller must hold cfg80211_mutex */
2116 void reg_device_remove(struct wiphy *wiphy)
2118 struct wiphy *request_wiphy = NULL;
2120 assert_cfg80211_lock();
2123 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2126 if (!last_request || !request_wiphy)
2128 if (request_wiphy != wiphy)
2130 last_request->wiphy_idx = WIPHY_IDX_STALE;
2131 last_request->country_ie_env = ENVIRON_ANY;
2134 int regulatory_init(void)
2138 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2139 if (IS_ERR(reg_pdev))
2140 return PTR_ERR(reg_pdev);
2142 spin_lock_init(®_requests_lock);
2143 spin_lock_init(®_pending_beacons_lock);
2145 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2146 cfg80211_regdomain = static_regdom(ieee80211_regdom);
2148 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
2149 print_regdomain_info(cfg80211_regdomain);
2151 * The old code still requests for a new regdomain and if
2152 * you have CRDA you get it updated, otherwise you get
2153 * stuck with the static values. Since "EU" is not a valid
2154 * ISO / IEC 3166 alpha2 code we can't expect userpace to
2155 * give us a regulatory domain for it. We need last_request
2156 * iniitalized though so lets just send a request which we
2157 * know will be ignored... this crap will be removed once
2160 err = regulatory_hint_core(ieee80211_regdom);
2162 cfg80211_regdomain = cfg80211_world_regdom;
2164 err = regulatory_hint_core(ieee80211_regdom);
2170 * N.B. kobject_uevent_env() can fail mainly for when we're out
2171 * memory which is handled and propagated appropriately above
2172 * but it can also fail during a netlink_broadcast() or during
2173 * early boot for call_usermodehelper(). For now treat these
2174 * errors as non-fatal.
2176 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2177 "to call CRDA during init");
2178 #ifdef CONFIG_CFG80211_REG_DEBUG
2179 /* We want to find out exactly why when debugging */
2187 void regulatory_exit(void)
2189 struct regulatory_request *reg_request, *tmp;
2190 struct reg_beacon *reg_beacon, *btmp;
2192 cancel_work_sync(®_work);
2194 mutex_lock(&cfg80211_mutex);
2198 kfree(country_ie_regdomain);
2199 country_ie_regdomain = NULL;
2201 kfree(last_request);
2203 platform_device_unregister(reg_pdev);
2205 spin_lock_bh(®_pending_beacons_lock);
2206 if (!list_empty(®_pending_beacons)) {
2207 list_for_each_entry_safe(reg_beacon, btmp,
2208 ®_pending_beacons, list) {
2209 list_del(®_beacon->list);
2213 spin_unlock_bh(®_pending_beacons_lock);
2215 if (!list_empty(®_beacon_list)) {
2216 list_for_each_entry_safe(reg_beacon, btmp,
2217 ®_beacon_list, list) {
2218 list_del(®_beacon->list);
2223 spin_lock(®_requests_lock);
2224 if (!list_empty(®_requests_list)) {
2225 list_for_each_entry_safe(reg_request, tmp,
2226 ®_requests_list, list) {
2227 list_del(®_request->list);
2231 spin_unlock(®_requests_lock);
2233 mutex_unlock(&cfg80211_mutex);