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
)
392 assert_cfg80211_lock();
397 return alpha2_equal(last_request
->alpha2
, alpha2
);
400 /* Sanity check on a regulatory rule */
401 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
403 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
406 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
409 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
412 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
414 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
415 freq_range
->max_bandwidth_khz
> freq_diff
)
421 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
423 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
426 if (!rd
->n_reg_rules
)
429 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
432 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
433 reg_rule
= &rd
->reg_rules
[i
];
434 if (!is_valid_reg_rule(reg_rule
))
441 /* Returns value in KHz */
442 static u32
freq_max_bandwidth(const struct ieee80211_freq_range
*freq_range
,
446 for (i
= 0; i
< ARRAY_SIZE(supported_bandwidths
); i
++) {
447 u32 start_freq_khz
= freq
- supported_bandwidths
[i
]/2;
448 u32 end_freq_khz
= freq
+ supported_bandwidths
[i
]/2;
449 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
450 end_freq_khz
<= freq_range
->end_freq_khz
)
451 return supported_bandwidths
[i
];
457 * freq_in_rule_band - tells us if a frequency is in a frequency band
458 * @freq_range: frequency rule we want to query
459 * @freq_khz: frequency we are inquiring about
461 * This lets us know if a specific frequency rule is or is not relevant to
462 * a specific frequency's band. Bands are device specific and artificial
463 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
464 * safe for now to assume that a frequency rule should not be part of a
465 * frequency's band if the start freq or end freq are off by more than 2 GHz.
466 * This resolution can be lowered and should be considered as we add
467 * regulatory rule support for other "bands".
469 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
472 #define ONE_GHZ_IN_KHZ 1000000
473 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
475 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
478 #undef ONE_GHZ_IN_KHZ
482 * Converts a country IE to a regulatory domain. A regulatory domain
483 * structure has a lot of information which the IE doesn't yet have,
484 * so for the other values we use upper max values as we will intersect
485 * with our userspace regulatory agent to get lower bounds.
487 static struct ieee80211_regdomain
*country_ie_2_rd(
492 struct ieee80211_regdomain
*rd
= NULL
;
496 u32 num_rules
= 0, size_of_regd
= 0;
497 u8
*triplets_start
= NULL
;
498 u8 len_at_triplet
= 0;
499 /* the last channel we have registered in a subband (triplet) */
500 int last_sub_max_channel
= 0;
502 *checksum
= 0xDEADBEEF;
504 /* Country IE requirements */
505 BUG_ON(country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
||
506 country_ie_len
& 0x01);
508 alpha2
[0] = country_ie
[0];
509 alpha2
[1] = country_ie
[1];
512 * Third octet can be:
516 * anything else we assume is no restrictions
518 if (country_ie
[2] == 'I')
519 flags
= NL80211_RRF_NO_OUTDOOR
;
520 else if (country_ie
[2] == 'O')
521 flags
= NL80211_RRF_NO_INDOOR
;
526 triplets_start
= country_ie
;
527 len_at_triplet
= country_ie_len
;
529 *checksum
^= ((flags
^ alpha2
[0] ^ alpha2
[1]) << 8);
532 * We need to build a reg rule for each triplet, but first we must
533 * calculate the number of reg rules we will need. We will need one
534 * for each channel subband
536 while (country_ie_len
>= 3) {
538 struct ieee80211_country_ie_triplet
*triplet
=
539 (struct ieee80211_country_ie_triplet
*) country_ie
;
540 int cur_sub_max_channel
= 0, cur_channel
= 0;
542 if (triplet
->ext
.reg_extension_id
>=
543 IEEE80211_COUNTRY_EXTENSION_ID
) {
550 if (triplet
->chans
.first_channel
<= 14)
551 end_channel
= triplet
->chans
.first_channel
+
552 triplet
->chans
.num_channels
;
555 * 5 GHz -- For example in country IEs if the first
556 * channel given is 36 and the number of channels is 4
557 * then the individual channel numbers defined for the
558 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
559 * and not 36, 37, 38, 39.
561 * See: http://tinyurl.com/11d-clarification
563 end_channel
= triplet
->chans
.first_channel
+
564 (4 * (triplet
->chans
.num_channels
- 1));
566 cur_channel
= triplet
->chans
.first_channel
;
567 cur_sub_max_channel
= end_channel
;
569 /* Basic sanity check */
570 if (cur_sub_max_channel
< cur_channel
)
574 * Do not allow overlapping channels. Also channels
575 * passed in each subband must be monotonically
578 if (last_sub_max_channel
) {
579 if (cur_channel
<= last_sub_max_channel
)
581 if (cur_sub_max_channel
<= last_sub_max_channel
)
586 * When dot11RegulatoryClassesRequired is supported
587 * we can throw ext triplets as part of this soup,
588 * for now we don't care when those change as we
591 *checksum
^= ((cur_channel
^ cur_sub_max_channel
) << 8) |
592 ((cur_sub_max_channel
^ cur_sub_max_channel
) << 16) |
593 ((triplet
->chans
.max_power
^ cur_sub_max_channel
) << 24);
595 last_sub_max_channel
= cur_sub_max_channel
;
602 * Note: this is not a IEEE requirement but
603 * simply a memory requirement
605 if (num_rules
> NL80211_MAX_SUPP_REG_RULES
)
609 country_ie
= triplets_start
;
610 country_ie_len
= len_at_triplet
;
612 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
613 (num_rules
* sizeof(struct ieee80211_reg_rule
));
615 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
619 rd
->n_reg_rules
= num_rules
;
620 rd
->alpha2
[0] = alpha2
[0];
621 rd
->alpha2
[1] = alpha2
[1];
623 /* This time around we fill in the rd */
624 while (country_ie_len
>= 3) {
626 struct ieee80211_country_ie_triplet
*triplet
=
627 (struct ieee80211_country_ie_triplet
*) country_ie
;
628 struct ieee80211_reg_rule
*reg_rule
= NULL
;
629 struct ieee80211_freq_range
*freq_range
= NULL
;
630 struct ieee80211_power_rule
*power_rule
= NULL
;
633 * Must parse if dot11RegulatoryClassesRequired is true,
634 * we don't support this yet
636 if (triplet
->ext
.reg_extension_id
>=
637 IEEE80211_COUNTRY_EXTENSION_ID
) {
643 reg_rule
= &rd
->reg_rules
[i
];
644 freq_range
= ®_rule
->freq_range
;
645 power_rule
= ®_rule
->power_rule
;
647 reg_rule
->flags
= flags
;
650 if (triplet
->chans
.first_channel
<= 14)
651 end_channel
= triplet
->chans
.first_channel
+
652 triplet
->chans
.num_channels
;
654 end_channel
= triplet
->chans
.first_channel
+
655 (4 * (triplet
->chans
.num_channels
- 1));
658 * The +10 is since the regulatory domain expects
659 * the actual band edge, not the center of freq for
660 * its start and end freqs, assuming 20 MHz bandwidth on
661 * the channels passed
663 freq_range
->start_freq_khz
=
664 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
665 triplet
->chans
.first_channel
) - 10);
666 freq_range
->end_freq_khz
=
667 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
671 * These are large arbitrary values we use to intersect later.
672 * Increment this if we ever support >= 40 MHz channels
675 freq_range
->max_bandwidth_khz
= MHZ_TO_KHZ(40);
676 power_rule
->max_antenna_gain
= DBI_TO_MBI(100);
677 power_rule
->max_eirp
= DBM_TO_MBM(100);
683 BUG_ON(i
> NL80211_MAX_SUPP_REG_RULES
);
691 * Helper for regdom_intersect(), this does the real
692 * mathematical intersection fun
694 static int reg_rules_intersect(
695 const struct ieee80211_reg_rule
*rule1
,
696 const struct ieee80211_reg_rule
*rule2
,
697 struct ieee80211_reg_rule
*intersected_rule
)
699 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
700 struct ieee80211_freq_range
*freq_range
;
701 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
702 struct ieee80211_power_rule
*power_rule
;
705 freq_range1
= &rule1
->freq_range
;
706 freq_range2
= &rule2
->freq_range
;
707 freq_range
= &intersected_rule
->freq_range
;
709 power_rule1
= &rule1
->power_rule
;
710 power_rule2
= &rule2
->power_rule
;
711 power_rule
= &intersected_rule
->power_rule
;
713 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
714 freq_range2
->start_freq_khz
);
715 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
716 freq_range2
->end_freq_khz
);
717 freq_range
->max_bandwidth_khz
= min(freq_range1
->max_bandwidth_khz
,
718 freq_range2
->max_bandwidth_khz
);
720 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
721 if (freq_range
->max_bandwidth_khz
> freq_diff
)
722 freq_range
->max_bandwidth_khz
= freq_diff
;
724 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
725 power_rule2
->max_eirp
);
726 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
727 power_rule2
->max_antenna_gain
);
729 intersected_rule
->flags
= (rule1
->flags
| rule2
->flags
);
731 if (!is_valid_reg_rule(intersected_rule
))
738 * regdom_intersect - do the intersection between two regulatory domains
739 * @rd1: first regulatory domain
740 * @rd2: second regulatory domain
742 * Use this function to get the intersection between two regulatory domains.
743 * Once completed we will mark the alpha2 for the rd as intersected, "98",
744 * as no one single alpha2 can represent this regulatory domain.
746 * Returns a pointer to the regulatory domain structure which will hold the
747 * resulting intersection of rules between rd1 and rd2. We will
748 * kzalloc() this structure for you.
750 static struct ieee80211_regdomain
*regdom_intersect(
751 const struct ieee80211_regdomain
*rd1
,
752 const struct ieee80211_regdomain
*rd2
)
756 unsigned int num_rules
= 0, rule_idx
= 0;
757 const struct ieee80211_reg_rule
*rule1
, *rule2
;
758 struct ieee80211_reg_rule
*intersected_rule
;
759 struct ieee80211_regdomain
*rd
;
760 /* This is just a dummy holder to help us count */
761 struct ieee80211_reg_rule irule
;
763 /* Uses the stack temporarily for counter arithmetic */
764 intersected_rule
= &irule
;
766 memset(intersected_rule
, 0, sizeof(struct ieee80211_reg_rule
));
772 * First we get a count of the rules we'll need, then we actually
773 * build them. This is to so we can malloc() and free() a
774 * regdomain once. The reason we use reg_rules_intersect() here
775 * is it will return -EINVAL if the rule computed makes no sense.
776 * All rules that do check out OK are valid.
779 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
780 rule1
= &rd1
->reg_rules
[x
];
781 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
782 rule2
= &rd2
->reg_rules
[y
];
783 if (!reg_rules_intersect(rule1
, rule2
,
786 memset(intersected_rule
, 0,
787 sizeof(struct ieee80211_reg_rule
));
794 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
795 ((num_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
797 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
801 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
802 rule1
= &rd1
->reg_rules
[x
];
803 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
804 rule2
= &rd2
->reg_rules
[y
];
806 * This time around instead of using the stack lets
807 * write to the target rule directly saving ourselves
810 intersected_rule
= &rd
->reg_rules
[rule_idx
];
811 r
= reg_rules_intersect(rule1
, rule2
,
814 * No need to memset here the intersected rule here as
815 * we're not using the stack anymore
823 if (rule_idx
!= num_rules
) {
828 rd
->n_reg_rules
= num_rules
;
836 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
837 * want to just have the channel structure use these
839 static u32
map_regdom_flags(u32 rd_flags
)
841 u32 channel_flags
= 0;
842 if (rd_flags
& NL80211_RRF_PASSIVE_SCAN
)
843 channel_flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
844 if (rd_flags
& NL80211_RRF_NO_IBSS
)
845 channel_flags
|= IEEE80211_CHAN_NO_IBSS
;
846 if (rd_flags
& NL80211_RRF_DFS
)
847 channel_flags
|= IEEE80211_CHAN_RADAR
;
848 return channel_flags
;
851 static int freq_reg_info_regd(struct wiphy
*wiphy
,
854 const struct ieee80211_reg_rule
**reg_rule
,
855 const struct ieee80211_regdomain
*custom_regd
)
858 bool band_rule_found
= false;
859 const struct ieee80211_regdomain
*regd
;
860 u32 max_bandwidth
= 0;
862 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
865 * Follow the driver's regulatory domain, if present, unless a country
866 * IE has been processed or a user wants to help complaince further
868 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
869 last_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
876 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
877 const struct ieee80211_reg_rule
*rr
;
878 const struct ieee80211_freq_range
*fr
= NULL
;
879 const struct ieee80211_power_rule
*pr
= NULL
;
881 rr
= ®d
->reg_rules
[i
];
882 fr
= &rr
->freq_range
;
883 pr
= &rr
->power_rule
;
886 * We only need to know if one frequency rule was
887 * was in center_freq's band, that's enough, so lets
888 * not overwrite it once found
890 if (!band_rule_found
)
891 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
893 max_bandwidth
= freq_max_bandwidth(fr
, center_freq
);
895 if (max_bandwidth
&& *bandwidth
<= max_bandwidth
) {
897 *bandwidth
= max_bandwidth
;
902 if (!band_rule_found
)
905 return !max_bandwidth
;
907 EXPORT_SYMBOL(freq_reg_info
);
909 int freq_reg_info(struct wiphy
*wiphy
, u32 center_freq
, u32
*bandwidth
,
910 const struct ieee80211_reg_rule
**reg_rule
)
912 assert_cfg80211_lock();
913 return freq_reg_info_regd(wiphy
, center_freq
,
914 bandwidth
, reg_rule
, NULL
);
917 static void handle_channel(struct wiphy
*wiphy
, enum ieee80211_band band
,
918 unsigned int chan_idx
)
922 u32 max_bandwidth
= 0;
923 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
924 const struct ieee80211_power_rule
*power_rule
= NULL
;
925 struct ieee80211_supported_band
*sband
;
926 struct ieee80211_channel
*chan
;
927 struct wiphy
*request_wiphy
= NULL
;
929 assert_cfg80211_lock();
931 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
933 sband
= wiphy
->bands
[band
];
934 BUG_ON(chan_idx
>= sband
->n_channels
);
935 chan
= &sband
->channels
[chan_idx
];
937 flags
= chan
->orig_flags
;
939 r
= freq_reg_info(wiphy
, MHZ_TO_KHZ(chan
->center_freq
),
940 &max_bandwidth
, ®_rule
);
944 * This means no regulatory rule was found in the country IE
945 * with a frequency range on the center_freq's band, since
946 * IEEE-802.11 allows for a country IE to have a subset of the
947 * regulatory information provided in a country we ignore
948 * disabling the channel unless at least one reg rule was
949 * found on the center_freq's band. For details see this
952 * http://tinyurl.com/11d-clarification
955 last_request
->initiator
==
956 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
957 #ifdef CONFIG_CFG80211_REG_DEBUG
958 printk(KERN_DEBUG
"cfg80211: Leaving channel %d MHz "
959 "intact on %s - no rule found in band on "
961 chan
->center_freq
, wiphy_name(wiphy
));
965 * In this case we know the country IE has at least one reg rule
966 * for the band so we respect its band definitions
968 #ifdef CONFIG_CFG80211_REG_DEBUG
969 if (last_request
->initiator
==
970 NL80211_REGDOM_SET_BY_COUNTRY_IE
)
971 printk(KERN_DEBUG
"cfg80211: Disabling "
972 "channel %d MHz on %s due to "
974 chan
->center_freq
, wiphy_name(wiphy
));
976 flags
|= IEEE80211_CHAN_DISABLED
;
982 power_rule
= ®_rule
->power_rule
;
984 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
985 request_wiphy
&& request_wiphy
== wiphy
&&
986 request_wiphy
->strict_regulatory
) {
988 * This gaurantees the driver's requested regulatory domain
989 * will always be used as a base for further regulatory
992 chan
->flags
= chan
->orig_flags
=
993 map_regdom_flags(reg_rule
->flags
);
994 chan
->max_antenna_gain
= chan
->orig_mag
=
995 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
996 chan
->max_bandwidth
= KHZ_TO_MHZ(max_bandwidth
);
997 chan
->max_power
= chan
->orig_mpwr
=
998 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1002 chan
->flags
= flags
| map_regdom_flags(reg_rule
->flags
);
1003 chan
->max_antenna_gain
= min(chan
->orig_mag
,
1004 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
1005 chan
->max_bandwidth
= KHZ_TO_MHZ(max_bandwidth
);
1006 if (chan
->orig_mpwr
)
1007 chan
->max_power
= min(chan
->orig_mpwr
,
1008 (int) MBM_TO_DBM(power_rule
->max_eirp
));
1010 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1013 static void handle_band(struct wiphy
*wiphy
, enum ieee80211_band band
)
1016 struct ieee80211_supported_band
*sband
;
1018 BUG_ON(!wiphy
->bands
[band
]);
1019 sband
= wiphy
->bands
[band
];
1021 for (i
= 0; i
< sband
->n_channels
; i
++)
1022 handle_channel(wiphy
, band
, i
);
1025 static bool ignore_reg_update(struct wiphy
*wiphy
,
1026 enum nl80211_reg_initiator initiator
)
1030 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
1031 wiphy
->custom_regulatory
)
1034 * wiphy->regd will be set once the device has its own
1035 * desired regulatory domain set
1037 if (wiphy
->strict_regulatory
&& !wiphy
->regd
&&
1038 !is_world_regdom(last_request
->alpha2
))
1043 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
1045 struct cfg80211_registered_device
*drv
;
1047 list_for_each_entry(drv
, &cfg80211_drv_list
, list
)
1048 wiphy_update_regulatory(&drv
->wiphy
, initiator
);
1051 static void handle_reg_beacon(struct wiphy
*wiphy
,
1052 unsigned int chan_idx
,
1053 struct reg_beacon
*reg_beacon
)
1055 #ifdef CONFIG_CFG80211_REG_DEBUG
1056 #define REG_DEBUG_BEACON_FLAG(desc) \
1057 printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \
1058 "frequency: %d MHz (Ch %d) on %s\n", \
1059 reg_beacon->chan.center_freq, \
1060 ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \
1063 #define REG_DEBUG_BEACON_FLAG(desc) do {} while (0)
1065 struct ieee80211_supported_band
*sband
;
1066 struct ieee80211_channel
*chan
;
1068 assert_cfg80211_lock();
1070 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1071 chan
= &sband
->channels
[chan_idx
];
1073 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
1076 if (chan
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
1077 chan
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
1078 REG_DEBUG_BEACON_FLAG("active scanning");
1081 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
) {
1082 chan
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
1083 REG_DEBUG_BEACON_FLAG("beaconing");
1086 chan
->beacon_found
= true;
1087 #undef REG_DEBUG_BEACON_FLAG
1091 * Called when a scan on a wiphy finds a beacon on
1094 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
1095 struct reg_beacon
*reg_beacon
)
1098 struct ieee80211_supported_band
*sband
;
1100 assert_cfg80211_lock();
1102 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1105 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1107 for (i
= 0; i
< sband
->n_channels
; i
++)
1108 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1112 * Called upon reg changes or a new wiphy is added
1114 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1117 struct ieee80211_supported_band
*sband
;
1118 struct reg_beacon
*reg_beacon
;
1120 assert_cfg80211_lock();
1122 if (list_empty(®_beacon_list
))
1125 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1126 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1128 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1129 for (i
= 0; i
< sband
->n_channels
; i
++)
1130 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1134 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1136 if (is_world_regdom(cfg80211_regdomain
->alpha2
) ||
1137 (wiphy
->regd
&& is_world_regdom(wiphy
->regd
->alpha2
)))
1140 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1141 wiphy
->custom_regulatory
)
1146 /* Reap the advantages of previously found beacons */
1147 static void reg_process_beacons(struct wiphy
*wiphy
)
1150 * Means we are just firing up cfg80211, so no beacons would
1151 * have been processed yet.
1155 if (!reg_is_world_roaming(wiphy
))
1157 wiphy_update_beacon_reg(wiphy
);
1160 void wiphy_update_regulatory(struct wiphy
*wiphy
,
1161 enum nl80211_reg_initiator initiator
)
1163 enum ieee80211_band band
;
1165 if (ignore_reg_update(wiphy
, initiator
))
1167 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1168 if (wiphy
->bands
[band
])
1169 handle_band(wiphy
, band
);
1172 reg_process_beacons(wiphy
);
1173 if (wiphy
->reg_notifier
)
1174 wiphy
->reg_notifier(wiphy
, last_request
);
1177 static void handle_channel_custom(struct wiphy
*wiphy
,
1178 enum ieee80211_band band
,
1179 unsigned int chan_idx
,
1180 const struct ieee80211_regdomain
*regd
)
1183 u32 max_bandwidth
= 0;
1184 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1185 const struct ieee80211_power_rule
*power_rule
= NULL
;
1186 struct ieee80211_supported_band
*sband
;
1187 struct ieee80211_channel
*chan
;
1189 assert_cfg80211_lock();
1191 sband
= wiphy
->bands
[band
];
1192 BUG_ON(chan_idx
>= sband
->n_channels
);
1193 chan
= &sband
->channels
[chan_idx
];
1195 r
= freq_reg_info_regd(wiphy
, MHZ_TO_KHZ(chan
->center_freq
),
1196 &max_bandwidth
, ®_rule
, regd
);
1199 chan
->flags
= IEEE80211_CHAN_DISABLED
;
1203 power_rule
= ®_rule
->power_rule
;
1205 chan
->flags
|= map_regdom_flags(reg_rule
->flags
);
1206 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1207 chan
->max_bandwidth
= KHZ_TO_MHZ(max_bandwidth
);
1208 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1211 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
1212 const struct ieee80211_regdomain
*regd
)
1215 struct ieee80211_supported_band
*sband
;
1217 BUG_ON(!wiphy
->bands
[band
]);
1218 sband
= wiphy
->bands
[band
];
1220 for (i
= 0; i
< sband
->n_channels
; i
++)
1221 handle_channel_custom(wiphy
, band
, i
, regd
);
1224 /* Used by drivers prior to wiphy registration */
1225 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1226 const struct ieee80211_regdomain
*regd
)
1228 enum ieee80211_band band
;
1230 mutex_lock(&cfg80211_mutex
);
1231 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1232 if (wiphy
->bands
[band
])
1233 handle_band_custom(wiphy
, band
, regd
);
1235 mutex_unlock(&cfg80211_mutex
);
1237 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1239 static int reg_copy_regd(const struct ieee80211_regdomain
**dst_regd
,
1240 const struct ieee80211_regdomain
*src_regd
)
1242 struct ieee80211_regdomain
*regd
;
1243 int size_of_regd
= 0;
1246 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
1247 ((src_regd
->n_reg_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
1249 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
1253 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
1255 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
1256 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
1257 sizeof(struct ieee80211_reg_rule
));
1264 * Return value which can be used by ignore_request() to indicate
1265 * it has been determined we should intersect two regulatory domains
1267 #define REG_INTERSECT 1
1269 /* This has the logic which determines when a new request
1270 * should be ignored. */
1271 static int ignore_request(struct wiphy
*wiphy
,
1272 struct regulatory_request
*pending_request
)
1274 struct wiphy
*last_wiphy
= NULL
;
1276 assert_cfg80211_lock();
1278 /* All initial requests are respected */
1282 switch (pending_request
->initiator
) {
1283 case NL80211_REGDOM_SET_BY_CORE
:
1285 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1287 last_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1289 if (unlikely(!is_an_alpha2(pending_request
->alpha2
)))
1291 if (last_request
->initiator
==
1292 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1293 if (last_wiphy
!= wiphy
) {
1295 * Two cards with two APs claiming different
1296 * different Country IE alpha2s. We could
1297 * intersect them, but that seems unlikely
1298 * to be correct. Reject second one for now.
1300 if (regdom_changes(pending_request
->alpha2
))
1305 * Two consecutive Country IE hints on the same wiphy.
1306 * This should be picked up early by the driver/stack
1308 if (WARN_ON(regdom_changes(pending_request
->alpha2
)))
1312 return REG_INTERSECT
;
1313 case NL80211_REGDOM_SET_BY_DRIVER
:
1314 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1315 if (is_old_static_regdom(cfg80211_regdomain
))
1317 if (regdom_changes(pending_request
->alpha2
))
1323 * This would happen if you unplug and plug your card
1324 * back in or if you add a new device for which the previously
1325 * loaded card also agrees on the regulatory domain.
1327 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1328 !regdom_changes(pending_request
->alpha2
))
1331 return REG_INTERSECT
;
1332 case NL80211_REGDOM_SET_BY_USER
:
1333 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1334 return REG_INTERSECT
;
1336 * If the user knows better the user should set the regdom
1337 * to their country before the IE is picked up
1339 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1340 last_request
->intersect
)
1343 * Process user requests only after previous user/driver/core
1344 * requests have been processed
1346 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1347 last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1348 last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1349 if (regdom_changes(last_request
->alpha2
))
1353 if (!is_old_static_regdom(cfg80211_regdomain
) &&
1354 !regdom_changes(pending_request
->alpha2
))
1364 * __regulatory_hint - hint to the wireless core a regulatory domain
1365 * @wiphy: if the hint comes from country information from an AP, this
1366 * is required to be set to the wiphy that received the information
1367 * @pending_request: the regulatory request currently being processed
1369 * The Wireless subsystem can use this function to hint to the wireless core
1370 * what it believes should be the current regulatory domain.
1372 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1373 * already been set or other standard error codes.
1375 * Caller must hold &cfg80211_mutex
1377 static int __regulatory_hint(struct wiphy
*wiphy
,
1378 struct regulatory_request
*pending_request
)
1380 bool intersect
= false;
1383 assert_cfg80211_lock();
1385 r
= ignore_request(wiphy
, pending_request
);
1387 if (r
== REG_INTERSECT
) {
1388 if (pending_request
->initiator
==
1389 NL80211_REGDOM_SET_BY_DRIVER
) {
1390 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1392 kfree(pending_request
);
1399 * If the regulatory domain being requested by the
1400 * driver has already been set just copy it to the
1403 if (r
== -EALREADY
&&
1404 pending_request
->initiator
==
1405 NL80211_REGDOM_SET_BY_DRIVER
) {
1406 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1408 kfree(pending_request
);
1414 kfree(pending_request
);
1419 kfree(last_request
);
1421 last_request
= pending_request
;
1422 last_request
->intersect
= intersect
;
1424 pending_request
= NULL
;
1426 /* When r == REG_INTERSECT we do need to call CRDA */
1429 * Since CRDA will not be called in this case as we already
1430 * have applied the requested regulatory domain before we just
1431 * inform userspace we have processed the request
1434 nl80211_send_reg_change_event(last_request
);
1438 return call_crda(last_request
->alpha2
);
1441 /* This processes *all* regulatory hints */
1442 static void reg_process_hint(struct regulatory_request
*reg_request
)
1445 struct wiphy
*wiphy
= NULL
;
1447 BUG_ON(!reg_request
->alpha2
);
1449 mutex_lock(&cfg80211_mutex
);
1451 if (wiphy_idx_valid(reg_request
->wiphy_idx
))
1452 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1454 if (reg_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1460 r
= __regulatory_hint(wiphy
, reg_request
);
1461 /* This is required so that the orig_* parameters are saved */
1462 if (r
== -EALREADY
&& wiphy
&& wiphy
->strict_regulatory
)
1463 wiphy_update_regulatory(wiphy
, reg_request
->initiator
);
1465 mutex_unlock(&cfg80211_mutex
);
1468 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1469 static void reg_process_pending_hints(void)
1471 struct regulatory_request
*reg_request
;
1473 spin_lock(®_requests_lock
);
1474 while (!list_empty(®_requests_list
)) {
1475 reg_request
= list_first_entry(®_requests_list
,
1476 struct regulatory_request
,
1478 list_del_init(®_request
->list
);
1480 spin_unlock(®_requests_lock
);
1481 reg_process_hint(reg_request
);
1482 spin_lock(®_requests_lock
);
1484 spin_unlock(®_requests_lock
);
1487 /* Processes beacon hints -- this has nothing to do with country IEs */
1488 static void reg_process_pending_beacon_hints(void)
1490 struct cfg80211_registered_device
*drv
;
1491 struct reg_beacon
*pending_beacon
, *tmp
;
1493 mutex_lock(&cfg80211_mutex
);
1495 /* This goes through the _pending_ beacon list */
1496 spin_lock_bh(®_pending_beacons_lock
);
1498 if (list_empty(®_pending_beacons
)) {
1499 spin_unlock_bh(®_pending_beacons_lock
);
1503 list_for_each_entry_safe(pending_beacon
, tmp
,
1504 ®_pending_beacons
, list
) {
1506 list_del_init(&pending_beacon
->list
);
1508 /* Applies the beacon hint to current wiphys */
1509 list_for_each_entry(drv
, &cfg80211_drv_list
, list
)
1510 wiphy_update_new_beacon(&drv
->wiphy
, pending_beacon
);
1512 /* Remembers the beacon hint for new wiphys or reg changes */
1513 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1516 spin_unlock_bh(®_pending_beacons_lock
);
1518 mutex_unlock(&cfg80211_mutex
);
1521 static void reg_todo(struct work_struct
*work
)
1523 reg_process_pending_hints();
1524 reg_process_pending_beacon_hints();
1527 static DECLARE_WORK(reg_work
, reg_todo
);
1529 static void queue_regulatory_request(struct regulatory_request
*request
)
1531 spin_lock(®_requests_lock
);
1532 list_add_tail(&request
->list
, ®_requests_list
);
1533 spin_unlock(®_requests_lock
);
1535 schedule_work(®_work
);
1538 /* Core regulatory hint -- happens once during cfg80211_init() */
1539 static int regulatory_hint_core(const char *alpha2
)
1541 struct regulatory_request
*request
;
1543 BUG_ON(last_request
);
1545 request
= kzalloc(sizeof(struct regulatory_request
),
1550 request
->alpha2
[0] = alpha2
[0];
1551 request
->alpha2
[1] = alpha2
[1];
1552 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1554 queue_regulatory_request(request
);
1557 * This ensures last_request is populated once modules
1558 * come swinging in and calling regulatory hints and
1559 * wiphy_apply_custom_regulatory().
1561 flush_scheduled_work();
1567 int regulatory_hint_user(const char *alpha2
)
1569 struct regulatory_request
*request
;
1573 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1577 request
->wiphy_idx
= WIPHY_IDX_STALE
;
1578 request
->alpha2
[0] = alpha2
[0];
1579 request
->alpha2
[1] = alpha2
[1];
1580 request
->initiator
= NL80211_REGDOM_SET_BY_USER
,
1582 queue_regulatory_request(request
);
1588 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1590 struct regulatory_request
*request
;
1595 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1599 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1601 /* Must have registered wiphy first */
1602 BUG_ON(!wiphy_idx_valid(request
->wiphy_idx
));
1604 request
->alpha2
[0] = alpha2
[0];
1605 request
->alpha2
[1] = alpha2
[1];
1606 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
1608 queue_regulatory_request(request
);
1612 EXPORT_SYMBOL(regulatory_hint
);
1614 static bool reg_same_country_ie_hint(struct wiphy
*wiphy
,
1615 u32 country_ie_checksum
)
1617 struct wiphy
*request_wiphy
;
1619 assert_cfg80211_lock();
1621 if (unlikely(last_request
->initiator
!=
1622 NL80211_REGDOM_SET_BY_COUNTRY_IE
))
1625 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1630 if (likely(request_wiphy
!= wiphy
))
1631 return !country_ie_integrity_changes(country_ie_checksum
);
1633 * We should not have let these through at this point, they
1634 * should have been picked up earlier by the first alpha2 check
1637 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum
)))
1642 void regulatory_hint_11d(struct wiphy
*wiphy
,
1646 struct ieee80211_regdomain
*rd
= NULL
;
1649 enum environment_cap env
= ENVIRON_ANY
;
1650 struct regulatory_request
*request
;
1652 mutex_lock(&cfg80211_mutex
);
1654 if (unlikely(!last_request
)) {
1655 mutex_unlock(&cfg80211_mutex
);
1659 /* IE len must be evenly divisible by 2 */
1660 if (country_ie_len
& 0x01)
1663 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
1667 * Pending country IE processing, this can happen after we
1668 * call CRDA and wait for a response if a beacon was received before
1669 * we were able to process the last regulatory_hint_11d() call
1671 if (country_ie_regdomain
)
1674 alpha2
[0] = country_ie
[0];
1675 alpha2
[1] = country_ie
[1];
1677 if (country_ie
[2] == 'I')
1678 env
= ENVIRON_INDOOR
;
1679 else if (country_ie
[2] == 'O')
1680 env
= ENVIRON_OUTDOOR
;
1683 * We will run this for *every* beacon processed for the BSSID, so
1684 * we optimize an early check to exit out early if we don't have to
1687 if (likely(last_request
->initiator
==
1688 NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1689 wiphy_idx_valid(last_request
->wiphy_idx
))) {
1690 struct cfg80211_registered_device
*drv_last_ie
;
1693 cfg80211_drv_by_wiphy_idx(last_request
->wiphy_idx
);
1696 * Lets keep this simple -- we trust the first AP
1697 * after we intersect with CRDA
1699 if (likely(&drv_last_ie
->wiphy
== wiphy
)) {
1701 * Ignore IEs coming in on this wiphy with
1702 * the same alpha2 and environment cap
1704 if (likely(alpha2_equal(drv_last_ie
->country_ie_alpha2
,
1706 env
== drv_last_ie
->env
)) {
1710 * the wiphy moved on to another BSSID or the AP
1711 * was reconfigured. XXX: We need to deal with the
1712 * case where the user suspends and goes to goes
1713 * to another country, and then gets IEs from an
1714 * AP with different settings
1719 * Ignore IEs coming in on two separate wiphys with
1720 * the same alpha2 and environment cap
1722 if (likely(alpha2_equal(drv_last_ie
->country_ie_alpha2
,
1724 env
== drv_last_ie
->env
)) {
1727 /* We could potentially intersect though */
1732 rd
= country_ie_2_rd(country_ie
, country_ie_len
, &checksum
);
1737 * This will not happen right now but we leave it here for the
1738 * the future when we want to add suspend/resume support and having
1739 * the user move to another country after doing so, or having the user
1740 * move to another AP. Right now we just trust the first AP.
1742 * If we hit this before we add this support we want to be informed of
1743 * it as it would indicate a mistake in the current design
1745 if (WARN_ON(reg_same_country_ie_hint(wiphy
, checksum
)))
1748 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1753 * We keep this around for when CRDA comes back with a response so
1754 * we can intersect with that
1756 country_ie_regdomain
= rd
;
1758 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1759 request
->alpha2
[0] = rd
->alpha2
[0];
1760 request
->alpha2
[1] = rd
->alpha2
[1];
1761 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
1762 request
->country_ie_checksum
= checksum
;
1763 request
->country_ie_env
= env
;
1765 mutex_unlock(&cfg80211_mutex
);
1767 queue_regulatory_request(request
);
1774 mutex_unlock(&cfg80211_mutex
);
1776 EXPORT_SYMBOL(regulatory_hint_11d
);
1778 static bool freq_is_chan_12_13_14(u16 freq
)
1780 if (freq
== ieee80211_channel_to_frequency(12) ||
1781 freq
== ieee80211_channel_to_frequency(13) ||
1782 freq
== ieee80211_channel_to_frequency(14))
1787 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
1788 struct ieee80211_channel
*beacon_chan
,
1791 struct reg_beacon
*reg_beacon
;
1793 if (likely((beacon_chan
->beacon_found
||
1794 (beacon_chan
->flags
& IEEE80211_CHAN_RADAR
) ||
1795 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
1796 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))))
1799 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
1803 #ifdef CONFIG_CFG80211_REG_DEBUG
1804 printk(KERN_DEBUG
"cfg80211: Found new beacon on "
1805 "frequency: %d MHz (Ch %d) on %s\n",
1806 beacon_chan
->center_freq
,
1807 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
1810 memcpy(®_beacon
->chan
, beacon_chan
,
1811 sizeof(struct ieee80211_channel
));
1815 * Since we can be called from BH or and non-BH context
1816 * we must use spin_lock_bh()
1818 spin_lock_bh(®_pending_beacons_lock
);
1819 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
1820 spin_unlock_bh(®_pending_beacons_lock
);
1822 schedule_work(®_work
);
1827 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
1830 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1831 const struct ieee80211_freq_range
*freq_range
= NULL
;
1832 const struct ieee80211_power_rule
*power_rule
= NULL
;
1834 printk(KERN_INFO
"\t(start_freq - end_freq @ bandwidth), "
1835 "(max_antenna_gain, max_eirp)\n");
1837 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
1838 reg_rule
= &rd
->reg_rules
[i
];
1839 freq_range
= ®_rule
->freq_range
;
1840 power_rule
= ®_rule
->power_rule
;
1843 * There may not be documentation for max antenna gain
1844 * in certain regions
1846 if (power_rule
->max_antenna_gain
)
1847 printk(KERN_INFO
"\t(%d KHz - %d KHz @ %d KHz), "
1848 "(%d mBi, %d mBm)\n",
1849 freq_range
->start_freq_khz
,
1850 freq_range
->end_freq_khz
,
1851 freq_range
->max_bandwidth_khz
,
1852 power_rule
->max_antenna_gain
,
1853 power_rule
->max_eirp
);
1855 printk(KERN_INFO
"\t(%d KHz - %d KHz @ %d KHz), "
1857 freq_range
->start_freq_khz
,
1858 freq_range
->end_freq_khz
,
1859 freq_range
->max_bandwidth_khz
,
1860 power_rule
->max_eirp
);
1864 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
1867 if (is_intersected_alpha2(rd
->alpha2
)) {
1869 if (last_request
->initiator
==
1870 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1871 struct cfg80211_registered_device
*drv
;
1872 drv
= cfg80211_drv_by_wiphy_idx(
1873 last_request
->wiphy_idx
);
1875 printk(KERN_INFO
"cfg80211: Current regulatory "
1876 "domain updated by AP to: %c%c\n",
1877 drv
->country_ie_alpha2
[0],
1878 drv
->country_ie_alpha2
[1]);
1880 printk(KERN_INFO
"cfg80211: Current regulatory "
1881 "domain intersected: \n");
1883 printk(KERN_INFO
"cfg80211: Current regulatory "
1884 "domain intersected: \n");
1885 } else if (is_world_regdom(rd
->alpha2
))
1886 printk(KERN_INFO
"cfg80211: World regulatory "
1887 "domain updated:\n");
1889 if (is_unknown_alpha2(rd
->alpha2
))
1890 printk(KERN_INFO
"cfg80211: Regulatory domain "
1891 "changed to driver built-in settings "
1892 "(unknown country)\n");
1894 printk(KERN_INFO
"cfg80211: Regulatory domain "
1895 "changed to country: %c%c\n",
1896 rd
->alpha2
[0], rd
->alpha2
[1]);
1901 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
1903 printk(KERN_INFO
"cfg80211: Regulatory domain: %c%c\n",
1904 rd
->alpha2
[0], rd
->alpha2
[1]);
1908 #ifdef CONFIG_CFG80211_REG_DEBUG
1909 static void reg_country_ie_process_debug(
1910 const struct ieee80211_regdomain
*rd
,
1911 const struct ieee80211_regdomain
*country_ie_regdomain
,
1912 const struct ieee80211_regdomain
*intersected_rd
)
1914 printk(KERN_DEBUG
"cfg80211: Received country IE:\n");
1915 print_regdomain_info(country_ie_regdomain
);
1916 printk(KERN_DEBUG
"cfg80211: CRDA thinks this should applied:\n");
1917 print_regdomain_info(rd
);
1918 if (intersected_rd
) {
1919 printk(KERN_DEBUG
"cfg80211: We intersect both of these "
1921 print_regdomain_info(intersected_rd
);
1924 printk(KERN_DEBUG
"cfg80211: Intersection between both failed\n");
1927 static inline void reg_country_ie_process_debug(
1928 const struct ieee80211_regdomain
*rd
,
1929 const struct ieee80211_regdomain
*country_ie_regdomain
,
1930 const struct ieee80211_regdomain
*intersected_rd
)
1935 /* Takes ownership of rd only if it doesn't fail */
1936 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
1938 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
1939 struct cfg80211_registered_device
*drv
= NULL
;
1940 struct wiphy
*request_wiphy
;
1941 /* Some basic sanity checks first */
1943 if (is_world_regdom(rd
->alpha2
)) {
1944 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
1946 update_world_regdomain(rd
);
1950 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
1951 !is_unknown_alpha2(rd
->alpha2
))
1958 * Lets only bother proceeding on the same alpha2 if the current
1959 * rd is non static (it means CRDA was present and was used last)
1960 * and the pending request came in from a country IE
1962 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1964 * If someone else asked us to change the rd lets only bother
1965 * checking if the alpha2 changes if CRDA was already called
1967 if (!is_old_static_regdom(cfg80211_regdomain
) &&
1968 !regdom_changes(rd
->alpha2
))
1973 * Now lets set the regulatory domain, update all driver channels
1974 * and finally inform them of what we have done, in case they want
1975 * to review or adjust their own settings based on their own
1976 * internal EEPROM data
1979 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
1982 if (!is_valid_rd(rd
)) {
1983 printk(KERN_ERR
"cfg80211: Invalid "
1984 "regulatory domain detected:\n");
1985 print_regdomain_info(rd
);
1989 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1991 if (!last_request
->intersect
) {
1994 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_DRIVER
) {
1996 cfg80211_regdomain
= rd
;
2001 * For a driver hint, lets copy the regulatory domain the
2002 * driver wanted to the wiphy to deal with conflicts
2005 BUG_ON(request_wiphy
->regd
);
2007 r
= reg_copy_regd(&request_wiphy
->regd
, rd
);
2012 cfg80211_regdomain
= rd
;
2016 /* Intersection requires a bit more work */
2018 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2020 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
2021 if (!intersected_rd
)
2025 * We can trash what CRDA provided now.
2026 * However if a driver requested this specific regulatory
2027 * domain we keep it for its private use
2029 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
)
2030 request_wiphy
->regd
= rd
;
2037 cfg80211_regdomain
= intersected_rd
;
2043 * Country IE requests are handled a bit differently, we intersect
2044 * the country IE rd with what CRDA believes that country should have
2048 * Userspace could have sent two replies with only
2049 * one kernel request. By the second reply we would have
2050 * already processed and consumed the country_ie_regdomain.
2052 if (!country_ie_regdomain
)
2054 BUG_ON(rd
== country_ie_regdomain
);
2057 * Intersect what CRDA returned and our what we
2058 * had built from the Country IE received
2061 intersected_rd
= regdom_intersect(rd
, country_ie_regdomain
);
2063 reg_country_ie_process_debug(rd
,
2064 country_ie_regdomain
,
2067 kfree(country_ie_regdomain
);
2068 country_ie_regdomain
= NULL
;
2070 if (!intersected_rd
)
2073 drv
= wiphy_to_dev(request_wiphy
);
2075 drv
->country_ie_alpha2
[0] = rd
->alpha2
[0];
2076 drv
->country_ie_alpha2
[1] = rd
->alpha2
[1];
2077 drv
->env
= last_request
->country_ie_env
;
2079 BUG_ON(intersected_rd
== rd
);
2085 cfg80211_regdomain
= intersected_rd
;
2092 * Use this call to set the current regulatory domain. Conflicts with
2093 * multiple drivers can be ironed out later. Caller must've already
2094 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2096 int set_regdom(const struct ieee80211_regdomain
*rd
)
2100 assert_cfg80211_lock();
2102 /* Note that this doesn't update the wiphys, this is done below */
2103 r
= __set_regdom(rd
);
2109 /* This would make this whole thing pointless */
2110 if (!last_request
->intersect
)
2111 BUG_ON(rd
!= cfg80211_regdomain
);
2113 /* update all wiphys now with the new established regulatory domain */
2114 update_all_wiphy_regulatory(last_request
->initiator
);
2116 print_regdomain(cfg80211_regdomain
);
2118 nl80211_send_reg_change_event(last_request
);
2123 /* Caller must hold cfg80211_mutex */
2124 void reg_device_remove(struct wiphy
*wiphy
)
2126 struct wiphy
*request_wiphy
= NULL
;
2128 assert_cfg80211_lock();
2131 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2134 if (!last_request
|| !request_wiphy
)
2136 if (request_wiphy
!= wiphy
)
2138 last_request
->wiphy_idx
= WIPHY_IDX_STALE
;
2139 last_request
->country_ie_env
= ENVIRON_ANY
;
2142 int regulatory_init(void)
2146 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2147 if (IS_ERR(reg_pdev
))
2148 return PTR_ERR(reg_pdev
);
2150 spin_lock_init(®_requests_lock
);
2151 spin_lock_init(®_pending_beacons_lock
);
2153 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2154 cfg80211_regdomain
= static_regdom(ieee80211_regdom
);
2156 printk(KERN_INFO
"cfg80211: Using static regulatory domain info\n");
2157 print_regdomain_info(cfg80211_regdomain
);
2159 * The old code still requests for a new regdomain and if
2160 * you have CRDA you get it updated, otherwise you get
2161 * stuck with the static values. Since "EU" is not a valid
2162 * ISO / IEC 3166 alpha2 code we can't expect userpace to
2163 * give us a regulatory domain for it. We need last_request
2164 * iniitalized though so lets just send a request which we
2165 * know will be ignored... this crap will be removed once
2168 err
= regulatory_hint_core(ieee80211_regdom
);
2170 cfg80211_regdomain
= cfg80211_world_regdom
;
2172 err
= regulatory_hint_core(ieee80211_regdom
);
2178 * N.B. kobject_uevent_env() can fail mainly for when we're out
2179 * memory which is handled and propagated appropriately above
2180 * but it can also fail during a netlink_broadcast() or during
2181 * early boot for call_usermodehelper(). For now treat these
2182 * errors as non-fatal.
2184 printk(KERN_ERR
"cfg80211: kobject_uevent_env() was unable "
2185 "to call CRDA during init");
2186 #ifdef CONFIG_CFG80211_REG_DEBUG
2187 /* We want to find out exactly why when debugging */
2195 void regulatory_exit(void)
2197 struct regulatory_request
*reg_request
, *tmp
;
2198 struct reg_beacon
*reg_beacon
, *btmp
;
2200 cancel_work_sync(®_work
);
2202 mutex_lock(&cfg80211_mutex
);
2206 kfree(country_ie_regdomain
);
2207 country_ie_regdomain
= NULL
;
2209 kfree(last_request
);
2211 platform_device_unregister(reg_pdev
);
2213 spin_lock_bh(®_pending_beacons_lock
);
2214 if (!list_empty(®_pending_beacons
)) {
2215 list_for_each_entry_safe(reg_beacon
, btmp
,
2216 ®_pending_beacons
, list
) {
2217 list_del(®_beacon
->list
);
2221 spin_unlock_bh(®_pending_beacons_lock
);
2223 if (!list_empty(®_beacon_list
)) {
2224 list_for_each_entry_safe(reg_beacon
, btmp
,
2225 ®_beacon_list
, list
) {
2226 list_del(®_beacon
->list
);
2231 spin_lock(®_requests_lock
);
2232 if (!list_empty(®_requests_list
)) {
2233 list_for_each_entry_safe(reg_request
, tmp
,
2234 ®_requests_list
, list
) {
2235 list_del(®_request
->list
);
2239 spin_unlock(®_requests_lock
);
2241 mutex_unlock(&cfg80211_mutex
);