2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 * DOC: Wireless regulatory infrastructure
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
20 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/random.h>
38 #include <linux/nl80211.h>
39 #include <linux/platform_device.h>
40 #include <net/cfg80211.h>
45 /* Receipt of information from last regulatory request */
46 static struct regulatory_request
*last_request
;
48 /* To trigger userspace events */
49 static struct platform_device
*reg_pdev
;
52 * Central wireless core regulatory domains, we only need two,
53 * the current one and a world regulatory domain in case we have no
54 * information to give us an alpha2
56 const struct ieee80211_regdomain
*cfg80211_regdomain
;
59 * We use this as a place for the rd structure built from the
60 * last parsed country IE to rest until CRDA gets back to us with
61 * what it thinks should apply for the same country
63 static const struct ieee80211_regdomain
*country_ie_regdomain
;
66 * Protects static reg.c components:
67 * - cfg80211_world_regdom
69 * - country_ie_regdomain
72 DEFINE_MUTEX(reg_mutex
);
73 #define assert_reg_lock() WARN_ON(!mutex_is_locked(®_mutex))
75 /* Used to queue up regulatory hints */
76 static LIST_HEAD(reg_requests_list
);
77 static spinlock_t reg_requests_lock
;
79 /* Used to queue up beacon hints for review */
80 static LIST_HEAD(reg_pending_beacons
);
81 static spinlock_t reg_pending_beacons_lock
;
83 /* Used to keep track of processed beacon hints */
84 static LIST_HEAD(reg_beacon_list
);
87 struct list_head list
;
88 struct ieee80211_channel chan
;
91 /* We keep a static world regulatory domain in case of the absence of CRDA */
92 static const struct ieee80211_regdomain world_regdom
= {
96 /* IEEE 802.11b/g, channels 1..11 */
97 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
98 /* IEEE 802.11b/g, channels 12..13. No HT40
99 * channel fits here. */
100 REG_RULE(2467-10, 2472+10, 20, 6, 20,
101 NL80211_RRF_PASSIVE_SCAN
|
102 NL80211_RRF_NO_IBSS
),
103 /* IEEE 802.11 channel 14 - Only JP enables
104 * this and for 802.11b only */
105 REG_RULE(2484-10, 2484+10, 20, 6, 20,
106 NL80211_RRF_PASSIVE_SCAN
|
107 NL80211_RRF_NO_IBSS
|
108 NL80211_RRF_NO_OFDM
),
109 /* IEEE 802.11a, channel 36..48 */
110 REG_RULE(5180-10, 5240+10, 40, 6, 20,
111 NL80211_RRF_PASSIVE_SCAN
|
112 NL80211_RRF_NO_IBSS
),
114 /* NB: 5260 MHz - 5700 MHz requies DFS */
116 /* IEEE 802.11a, channel 149..165 */
117 REG_RULE(5745-10, 5825+10, 40, 6, 20,
118 NL80211_RRF_PASSIVE_SCAN
|
119 NL80211_RRF_NO_IBSS
),
123 static const struct ieee80211_regdomain
*cfg80211_world_regdom
=
126 static char *ieee80211_regdom
= "00";
128 module_param(ieee80211_regdom
, charp
, 0444);
129 MODULE_PARM_DESC(ieee80211_regdom
, "IEEE 802.11 regulatory domain code");
131 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
133 * We assume 40 MHz bandwidth for the old regulatory work.
134 * We make emphasis we are using the exact same frequencies
138 static const struct ieee80211_regdomain us_regdom
= {
142 /* IEEE 802.11b/g, channels 1..11 */
143 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
144 /* IEEE 802.11a, channel 36..48 */
145 REG_RULE(5180-10, 5240+10, 40, 6, 17, 0),
146 /* IEEE 802.11a, channels 48..64 */
147 REG_RULE(5260-10, 5320+10, 40, 6, 20, NL80211_RRF_DFS
),
148 /* IEEE 802.11a, channels 100..124 */
149 REG_RULE(5500-10, 5590+10, 40, 6, 20, NL80211_RRF_DFS
),
150 /* IEEE 802.11a, channels 132..144 */
151 REG_RULE(5660-10, 5700+10, 40, 6, 20, NL80211_RRF_DFS
),
152 /* IEEE 802.11a, channels 149..165, outdoor */
153 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
157 static const struct ieee80211_regdomain jp_regdom
= {
161 /* IEEE 802.11b/g, channels 1..11 */
162 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
163 /* IEEE 802.11b/g, channels 12..13 */
164 REG_RULE(2467-10, 2472+10, 20, 6, 20, 0),
165 /* IEEE 802.11b/g, channel 14 */
166 REG_RULE(2484-10, 2484+10, 20, 6, 20, NL80211_RRF_NO_OFDM
),
167 /* IEEE 802.11a, channels 36..48 */
168 REG_RULE(5180-10, 5240+10, 40, 6, 20, 0),
169 /* IEEE 802.11a, channels 52..64 */
170 REG_RULE(5260-10, 5320+10, 40, 6, 20, NL80211_RRF_DFS
),
171 /* IEEE 802.11a, channels 100..144 */
172 REG_RULE(5500-10, 5700+10, 40, 6, 23, NL80211_RRF_DFS
),
176 static const struct ieee80211_regdomain
*static_regdom(char *alpha2
)
178 if (alpha2
[0] == 'U' && alpha2
[1] == 'S')
180 if (alpha2
[0] == 'J' && alpha2
[1] == 'P')
182 /* Use world roaming rules for "EU", since it was a pseudo
184 if (alpha2
[0] == 'E' && alpha2
[1] == 'U')
185 return &world_regdom
;
186 /* Default, world roaming rules */
187 return &world_regdom
;
190 static bool is_old_static_regdom(const struct ieee80211_regdomain
*rd
)
192 if (rd
== &us_regdom
|| rd
== &jp_regdom
|| rd
== &world_regdom
)
197 static inline bool is_old_static_regdom(const struct ieee80211_regdomain
*rd
)
203 static void reset_regdomains(void)
205 /* avoid freeing static information or freeing something twice */
206 if (cfg80211_regdomain
== cfg80211_world_regdom
)
207 cfg80211_regdomain
= NULL
;
208 if (cfg80211_world_regdom
== &world_regdom
)
209 cfg80211_world_regdom
= NULL
;
210 if (cfg80211_regdomain
== &world_regdom
)
211 cfg80211_regdomain
= NULL
;
212 if (is_old_static_regdom(cfg80211_regdomain
))
213 cfg80211_regdomain
= NULL
;
215 kfree(cfg80211_regdomain
);
216 kfree(cfg80211_world_regdom
);
218 cfg80211_world_regdom
= &world_regdom
;
219 cfg80211_regdomain
= NULL
;
223 * Dynamic world regulatory domain requested by the wireless
224 * core upon initialization
226 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
228 BUG_ON(!last_request
);
232 cfg80211_world_regdom
= rd
;
233 cfg80211_regdomain
= rd
;
236 bool is_world_regdom(const char *alpha2
)
240 if (alpha2
[0] == '0' && alpha2
[1] == '0')
245 static bool is_alpha2_set(const char *alpha2
)
249 if (alpha2
[0] != 0 && alpha2
[1] != 0)
254 static bool is_alpha_upper(char letter
)
257 if (letter
>= 65 && letter
<= 90)
262 static bool is_unknown_alpha2(const char *alpha2
)
267 * Special case where regulatory domain was built by driver
268 * but a specific alpha2 cannot be determined
270 if (alpha2
[0] == '9' && alpha2
[1] == '9')
275 static bool is_intersected_alpha2(const char *alpha2
)
280 * Special case where regulatory domain is the
281 * result of an intersection between two regulatory domain
284 if (alpha2
[0] == '9' && alpha2
[1] == '8')
289 static bool is_an_alpha2(const char *alpha2
)
293 if (is_alpha_upper(alpha2
[0]) && is_alpha_upper(alpha2
[1]))
298 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
300 if (!alpha2_x
|| !alpha2_y
)
302 if (alpha2_x
[0] == alpha2_y
[0] &&
303 alpha2_x
[1] == alpha2_y
[1])
308 static bool regdom_changes(const char *alpha2
)
310 assert_cfg80211_lock();
312 if (!cfg80211_regdomain
)
314 if (alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
320 * country_ie_integrity_changes - tells us if the country IE has changed
321 * @checksum: checksum of country IE of fields we are interested in
323 * If the country IE has not changed you can ignore it safely. This is
324 * useful to determine if two devices are seeing two different country IEs
325 * even on the same alpha2. Note that this will return false if no IE has
326 * been set on the wireless core yet.
328 static bool country_ie_integrity_changes(u32 checksum
)
330 /* If no IE has been set then the checksum doesn't change */
331 if (unlikely(!last_request
->country_ie_checksum
))
333 if (unlikely(last_request
->country_ie_checksum
!= checksum
))
339 * This lets us keep regulatory code which is updated on a regulatory
340 * basis in userspace.
342 static int call_crda(const char *alpha2
)
344 char country_env
[9 + 2] = "COUNTRY=";
350 if (!is_world_regdom((char *) alpha2
))
351 printk(KERN_INFO
"cfg80211: Calling CRDA for country: %c%c\n",
352 alpha2
[0], alpha2
[1]);
354 printk(KERN_INFO
"cfg80211: Calling CRDA to update world "
355 "regulatory domain\n");
357 country_env
[8] = alpha2
[0];
358 country_env
[9] = alpha2
[1];
360 return kobject_uevent_env(®_pdev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
363 /* Used by nl80211 before kmalloc'ing our regulatory domain */
364 bool reg_is_valid_request(const char *alpha2
)
366 assert_cfg80211_lock();
371 return alpha2_equal(last_request
->alpha2
, alpha2
);
374 /* Sanity check on a regulatory rule */
375 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
377 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
380 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
383 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
386 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
388 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
389 freq_range
->max_bandwidth_khz
> freq_diff
)
395 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
397 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
400 if (!rd
->n_reg_rules
)
403 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
406 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
407 reg_rule
= &rd
->reg_rules
[i
];
408 if (!is_valid_reg_rule(reg_rule
))
415 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
419 u32 start_freq_khz
, end_freq_khz
;
421 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
422 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
424 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
425 end_freq_khz
<= freq_range
->end_freq_khz
)
432 * freq_in_rule_band - tells us if a frequency is in a frequency band
433 * @freq_range: frequency rule we want to query
434 * @freq_khz: frequency we are inquiring about
436 * This lets us know if a specific frequency rule is or is not relevant to
437 * a specific frequency's band. Bands are device specific and artificial
438 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
439 * safe for now to assume that a frequency rule should not be part of a
440 * frequency's band if the start freq or end freq are off by more than 2 GHz.
441 * This resolution can be lowered and should be considered as we add
442 * regulatory rule support for other "bands".
444 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
447 #define ONE_GHZ_IN_KHZ 1000000
448 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
450 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
453 #undef ONE_GHZ_IN_KHZ
457 * Converts a country IE to a regulatory domain. A regulatory domain
458 * structure has a lot of information which the IE doesn't yet have,
459 * so for the other values we use upper max values as we will intersect
460 * with our userspace regulatory agent to get lower bounds.
462 static struct ieee80211_regdomain
*country_ie_2_rd(
467 struct ieee80211_regdomain
*rd
= NULL
;
471 u32 num_rules
= 0, size_of_regd
= 0;
472 u8
*triplets_start
= NULL
;
473 u8 len_at_triplet
= 0;
474 /* the last channel we have registered in a subband (triplet) */
475 int last_sub_max_channel
= 0;
477 *checksum
= 0xDEADBEEF;
479 /* Country IE requirements */
480 BUG_ON(country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
||
481 country_ie_len
& 0x01);
483 alpha2
[0] = country_ie
[0];
484 alpha2
[1] = country_ie
[1];
487 * Third octet can be:
491 * anything else we assume is no restrictions
493 if (country_ie
[2] == 'I')
494 flags
= NL80211_RRF_NO_OUTDOOR
;
495 else if (country_ie
[2] == 'O')
496 flags
= NL80211_RRF_NO_INDOOR
;
501 triplets_start
= country_ie
;
502 len_at_triplet
= country_ie_len
;
504 *checksum
^= ((flags
^ alpha2
[0] ^ alpha2
[1]) << 8);
507 * We need to build a reg rule for each triplet, but first we must
508 * calculate the number of reg rules we will need. We will need one
509 * for each channel subband
511 while (country_ie_len
>= 3) {
513 struct ieee80211_country_ie_triplet
*triplet
=
514 (struct ieee80211_country_ie_triplet
*) country_ie
;
515 int cur_sub_max_channel
= 0, cur_channel
= 0;
517 if (triplet
->ext
.reg_extension_id
>=
518 IEEE80211_COUNTRY_EXTENSION_ID
) {
525 if (triplet
->chans
.first_channel
<= 14)
526 end_channel
= triplet
->chans
.first_channel
+
527 triplet
->chans
.num_channels
;
530 * 5 GHz -- For example in country IEs if the first
531 * channel given is 36 and the number of channels is 4
532 * then the individual channel numbers defined for the
533 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
534 * and not 36, 37, 38, 39.
536 * See: http://tinyurl.com/11d-clarification
538 end_channel
= triplet
->chans
.first_channel
+
539 (4 * (triplet
->chans
.num_channels
- 1));
541 cur_channel
= triplet
->chans
.first_channel
;
542 cur_sub_max_channel
= end_channel
;
544 /* Basic sanity check */
545 if (cur_sub_max_channel
< cur_channel
)
549 * Do not allow overlapping channels. Also channels
550 * passed in each subband must be monotonically
553 if (last_sub_max_channel
) {
554 if (cur_channel
<= last_sub_max_channel
)
556 if (cur_sub_max_channel
<= last_sub_max_channel
)
561 * When dot11RegulatoryClassesRequired is supported
562 * we can throw ext triplets as part of this soup,
563 * for now we don't care when those change as we
566 *checksum
^= ((cur_channel
^ cur_sub_max_channel
) << 8) |
567 ((cur_sub_max_channel
^ cur_sub_max_channel
) << 16) |
568 ((triplet
->chans
.max_power
^ cur_sub_max_channel
) << 24);
570 last_sub_max_channel
= cur_sub_max_channel
;
577 * Note: this is not a IEEE requirement but
578 * simply a memory requirement
580 if (num_rules
> NL80211_MAX_SUPP_REG_RULES
)
584 country_ie
= triplets_start
;
585 country_ie_len
= len_at_triplet
;
587 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
588 (num_rules
* sizeof(struct ieee80211_reg_rule
));
590 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
594 rd
->n_reg_rules
= num_rules
;
595 rd
->alpha2
[0] = alpha2
[0];
596 rd
->alpha2
[1] = alpha2
[1];
598 /* This time around we fill in the rd */
599 while (country_ie_len
>= 3) {
601 struct ieee80211_country_ie_triplet
*triplet
=
602 (struct ieee80211_country_ie_triplet
*) country_ie
;
603 struct ieee80211_reg_rule
*reg_rule
= NULL
;
604 struct ieee80211_freq_range
*freq_range
= NULL
;
605 struct ieee80211_power_rule
*power_rule
= NULL
;
608 * Must parse if dot11RegulatoryClassesRequired is true,
609 * we don't support this yet
611 if (triplet
->ext
.reg_extension_id
>=
612 IEEE80211_COUNTRY_EXTENSION_ID
) {
618 reg_rule
= &rd
->reg_rules
[i
];
619 freq_range
= ®_rule
->freq_range
;
620 power_rule
= ®_rule
->power_rule
;
622 reg_rule
->flags
= flags
;
625 if (triplet
->chans
.first_channel
<= 14)
626 end_channel
= triplet
->chans
.first_channel
+
627 triplet
->chans
.num_channels
;
629 end_channel
= triplet
->chans
.first_channel
+
630 (4 * (triplet
->chans
.num_channels
- 1));
633 * The +10 is since the regulatory domain expects
634 * the actual band edge, not the center of freq for
635 * its start and end freqs, assuming 20 MHz bandwidth on
636 * the channels passed
638 freq_range
->start_freq_khz
=
639 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
640 triplet
->chans
.first_channel
) - 10);
641 freq_range
->end_freq_khz
=
642 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
646 * These are large arbitrary values we use to intersect later.
647 * Increment this if we ever support >= 40 MHz channels
650 freq_range
->max_bandwidth_khz
= MHZ_TO_KHZ(40);
651 power_rule
->max_antenna_gain
= DBI_TO_MBI(100);
652 power_rule
->max_eirp
= DBM_TO_MBM(100);
658 BUG_ON(i
> NL80211_MAX_SUPP_REG_RULES
);
666 * Helper for regdom_intersect(), this does the real
667 * mathematical intersection fun
669 static int reg_rules_intersect(
670 const struct ieee80211_reg_rule
*rule1
,
671 const struct ieee80211_reg_rule
*rule2
,
672 struct ieee80211_reg_rule
*intersected_rule
)
674 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
675 struct ieee80211_freq_range
*freq_range
;
676 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
677 struct ieee80211_power_rule
*power_rule
;
680 freq_range1
= &rule1
->freq_range
;
681 freq_range2
= &rule2
->freq_range
;
682 freq_range
= &intersected_rule
->freq_range
;
684 power_rule1
= &rule1
->power_rule
;
685 power_rule2
= &rule2
->power_rule
;
686 power_rule
= &intersected_rule
->power_rule
;
688 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
689 freq_range2
->start_freq_khz
);
690 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
691 freq_range2
->end_freq_khz
);
692 freq_range
->max_bandwidth_khz
= min(freq_range1
->max_bandwidth_khz
,
693 freq_range2
->max_bandwidth_khz
);
695 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
696 if (freq_range
->max_bandwidth_khz
> freq_diff
)
697 freq_range
->max_bandwidth_khz
= freq_diff
;
699 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
700 power_rule2
->max_eirp
);
701 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
702 power_rule2
->max_antenna_gain
);
704 intersected_rule
->flags
= (rule1
->flags
| rule2
->flags
);
706 if (!is_valid_reg_rule(intersected_rule
))
713 * regdom_intersect - do the intersection between two regulatory domains
714 * @rd1: first regulatory domain
715 * @rd2: second regulatory domain
717 * Use this function to get the intersection between two regulatory domains.
718 * Once completed we will mark the alpha2 for the rd as intersected, "98",
719 * as no one single alpha2 can represent this regulatory domain.
721 * Returns a pointer to the regulatory domain structure which will hold the
722 * resulting intersection of rules between rd1 and rd2. We will
723 * kzalloc() this structure for you.
725 static struct ieee80211_regdomain
*regdom_intersect(
726 const struct ieee80211_regdomain
*rd1
,
727 const struct ieee80211_regdomain
*rd2
)
731 unsigned int num_rules
= 0, rule_idx
= 0;
732 const struct ieee80211_reg_rule
*rule1
, *rule2
;
733 struct ieee80211_reg_rule
*intersected_rule
;
734 struct ieee80211_regdomain
*rd
;
735 /* This is just a dummy holder to help us count */
736 struct ieee80211_reg_rule irule
;
738 /* Uses the stack temporarily for counter arithmetic */
739 intersected_rule
= &irule
;
741 memset(intersected_rule
, 0, sizeof(struct ieee80211_reg_rule
));
747 * First we get a count of the rules we'll need, then we actually
748 * build them. This is to so we can malloc() and free() a
749 * regdomain once. The reason we use reg_rules_intersect() here
750 * is it will return -EINVAL if the rule computed makes no sense.
751 * All rules that do check out OK are valid.
754 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
755 rule1
= &rd1
->reg_rules
[x
];
756 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
757 rule2
= &rd2
->reg_rules
[y
];
758 if (!reg_rules_intersect(rule1
, rule2
,
761 memset(intersected_rule
, 0,
762 sizeof(struct ieee80211_reg_rule
));
769 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
770 ((num_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
772 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
776 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
777 rule1
= &rd1
->reg_rules
[x
];
778 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
779 rule2
= &rd2
->reg_rules
[y
];
781 * This time around instead of using the stack lets
782 * write to the target rule directly saving ourselves
785 intersected_rule
= &rd
->reg_rules
[rule_idx
];
786 r
= reg_rules_intersect(rule1
, rule2
,
789 * No need to memset here the intersected rule here as
790 * we're not using the stack anymore
798 if (rule_idx
!= num_rules
) {
803 rd
->n_reg_rules
= num_rules
;
811 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
812 * want to just have the channel structure use these
814 static u32
map_regdom_flags(u32 rd_flags
)
816 u32 channel_flags
= 0;
817 if (rd_flags
& NL80211_RRF_PASSIVE_SCAN
)
818 channel_flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
819 if (rd_flags
& NL80211_RRF_NO_IBSS
)
820 channel_flags
|= IEEE80211_CHAN_NO_IBSS
;
821 if (rd_flags
& NL80211_RRF_DFS
)
822 channel_flags
|= IEEE80211_CHAN_RADAR
;
823 return channel_flags
;
826 static int freq_reg_info_regd(struct wiphy
*wiphy
,
829 const struct ieee80211_reg_rule
**reg_rule
,
830 const struct ieee80211_regdomain
*custom_regd
)
833 bool band_rule_found
= false;
834 const struct ieee80211_regdomain
*regd
;
835 bool bw_fits
= false;
838 desired_bw_khz
= MHZ_TO_KHZ(20);
840 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
843 * Follow the driver's regulatory domain, if present, unless a country
844 * IE has been processed or a user wants to help complaince further
846 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
847 last_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
854 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
855 const struct ieee80211_reg_rule
*rr
;
856 const struct ieee80211_freq_range
*fr
= NULL
;
857 const struct ieee80211_power_rule
*pr
= NULL
;
859 rr
= ®d
->reg_rules
[i
];
860 fr
= &rr
->freq_range
;
861 pr
= &rr
->power_rule
;
864 * We only need to know if one frequency rule was
865 * was in center_freq's band, that's enough, so lets
866 * not overwrite it once found
868 if (!band_rule_found
)
869 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
871 bw_fits
= reg_does_bw_fit(fr
,
875 if (band_rule_found
&& bw_fits
) {
881 if (!band_rule_found
)
886 EXPORT_SYMBOL(freq_reg_info
);
888 int freq_reg_info(struct wiphy
*wiphy
,
891 const struct ieee80211_reg_rule
**reg_rule
)
893 assert_cfg80211_lock();
894 return freq_reg_info_regd(wiphy
,
902 * Note that right now we assume the desired channel bandwidth
903 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
904 * per channel, the primary and the extension channel). To support
905 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
906 * new ieee80211_channel.target_bw and re run the regulatory check
907 * on the wiphy with the target_bw specified. Then we can simply use
908 * that below for the desired_bw_khz below.
910 static void handle_channel(struct wiphy
*wiphy
, enum ieee80211_band band
,
911 unsigned int chan_idx
)
914 u32 flags
, bw_flags
= 0;
915 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
916 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
917 const struct ieee80211_power_rule
*power_rule
= NULL
;
918 const struct ieee80211_freq_range
*freq_range
= NULL
;
919 struct ieee80211_supported_band
*sband
;
920 struct ieee80211_channel
*chan
;
921 struct wiphy
*request_wiphy
= NULL
;
923 assert_cfg80211_lock();
925 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
927 sband
= wiphy
->bands
[band
];
928 BUG_ON(chan_idx
>= sband
->n_channels
);
929 chan
= &sband
->channels
[chan_idx
];
931 flags
= chan
->orig_flags
;
933 r
= freq_reg_info(wiphy
,
934 MHZ_TO_KHZ(chan
->center_freq
),
940 * This means no regulatory rule was found in the country IE
941 * with a frequency range on the center_freq's band, since
942 * IEEE-802.11 allows for a country IE to have a subset of the
943 * regulatory information provided in a country we ignore
944 * disabling the channel unless at least one reg rule was
945 * found on the center_freq's band. For details see this
948 * http://tinyurl.com/11d-clarification
951 last_request
->initiator
==
952 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
953 #ifdef CONFIG_CFG80211_REG_DEBUG
954 printk(KERN_DEBUG
"cfg80211: Leaving channel %d MHz "
955 "intact on %s - no rule found in band on "
957 chan
->center_freq
, wiphy_name(wiphy
));
961 * In this case we know the country IE has at least one reg rule
962 * for the band so we respect its band definitions
964 #ifdef CONFIG_CFG80211_REG_DEBUG
965 if (last_request
->initiator
==
966 NL80211_REGDOM_SET_BY_COUNTRY_IE
)
967 printk(KERN_DEBUG
"cfg80211: Disabling "
968 "channel %d MHz on %s due to "
970 chan
->center_freq
, wiphy_name(wiphy
));
972 flags
|= IEEE80211_CHAN_DISABLED
;
978 power_rule
= ®_rule
->power_rule
;
979 freq_range
= ®_rule
->freq_range
;
981 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
982 bw_flags
= IEEE80211_CHAN_NO_HT40
;
984 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
985 request_wiphy
&& request_wiphy
== wiphy
&&
986 request_wiphy
->flags
& WIPHY_FLAG_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
) | bw_flags
;
994 chan
->max_antenna_gain
= chan
->orig_mag
=
995 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
996 chan
->max_power
= chan
->orig_mpwr
=
997 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1001 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
1002 chan
->max_antenna_gain
= min(chan
->orig_mag
,
1003 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
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
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1032 * wiphy->regd will be set once the device has its own
1033 * desired regulatory domain set
1035 if (wiphy
->flags
& WIPHY_FLAG_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
*rdev
;
1045 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1046 wiphy_update_regulatory(&rdev
->wiphy
, initiator
);
1049 static void handle_reg_beacon(struct wiphy
*wiphy
,
1050 unsigned int chan_idx
,
1051 struct reg_beacon
*reg_beacon
)
1053 struct ieee80211_supported_band
*sband
;
1054 struct ieee80211_channel
*chan
;
1055 bool channel_changed
= false;
1056 struct ieee80211_channel chan_before
;
1058 assert_cfg80211_lock();
1060 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1061 chan
= &sband
->channels
[chan_idx
];
1063 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
1066 if (chan
->beacon_found
)
1069 chan
->beacon_found
= true;
1071 if (wiphy
->flags
& WIPHY_FLAG_DISABLE_BEACON_HINTS
)
1074 chan_before
.center_freq
= chan
->center_freq
;
1075 chan_before
.flags
= chan
->flags
;
1077 if (chan
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
1078 chan
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
1079 channel_changed
= true;
1082 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
) {
1083 chan
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
1084 channel_changed
= true;
1087 if (channel_changed
)
1088 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
1092 * Called when a scan on a wiphy finds a beacon on
1095 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
1096 struct reg_beacon
*reg_beacon
)
1099 struct ieee80211_supported_band
*sband
;
1101 assert_cfg80211_lock();
1103 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1106 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1108 for (i
= 0; i
< sband
->n_channels
; i
++)
1109 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1113 * Called upon reg changes or a new wiphy is added
1115 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1118 struct ieee80211_supported_band
*sband
;
1119 struct reg_beacon
*reg_beacon
;
1121 assert_cfg80211_lock();
1123 if (list_empty(®_beacon_list
))
1126 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1127 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1129 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1130 for (i
= 0; i
< sband
->n_channels
; i
++)
1131 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1135 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1137 if (is_world_regdom(cfg80211_regdomain
->alpha2
) ||
1138 (wiphy
->regd
&& is_world_regdom(wiphy
->regd
->alpha2
)))
1141 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1142 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1147 /* Reap the advantages of previously found beacons */
1148 static void reg_process_beacons(struct wiphy
*wiphy
)
1151 * Means we are just firing up cfg80211, so no beacons would
1152 * have been processed yet.
1156 if (!reg_is_world_roaming(wiphy
))
1158 wiphy_update_beacon_reg(wiphy
);
1161 static bool is_ht40_not_allowed(struct ieee80211_channel
*chan
)
1165 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1167 /* This would happen when regulatory rules disallow HT40 completely */
1168 if (IEEE80211_CHAN_NO_HT40
== (chan
->flags
& (IEEE80211_CHAN_NO_HT40
)))
1173 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1174 enum ieee80211_band band
,
1175 unsigned int chan_idx
)
1177 struct ieee80211_supported_band
*sband
;
1178 struct ieee80211_channel
*channel
;
1179 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1182 assert_cfg80211_lock();
1184 sband
= wiphy
->bands
[band
];
1185 BUG_ON(chan_idx
>= sband
->n_channels
);
1186 channel
= &sband
->channels
[chan_idx
];
1188 if (is_ht40_not_allowed(channel
)) {
1189 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1194 * We need to ensure the extension channels exist to
1195 * be able to use HT40- or HT40+, this finds them (or not)
1197 for (i
= 0; i
< sband
->n_channels
; i
++) {
1198 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1199 if (c
->center_freq
== (channel
->center_freq
- 20))
1201 if (c
->center_freq
== (channel
->center_freq
+ 20))
1206 * Please note that this assumes target bandwidth is 20 MHz,
1207 * if that ever changes we also need to change the below logic
1208 * to include that as well.
1210 if (is_ht40_not_allowed(channel_before
))
1211 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1213 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1215 if (is_ht40_not_allowed(channel_after
))
1216 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1218 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1221 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1222 enum ieee80211_band band
)
1225 struct ieee80211_supported_band
*sband
;
1227 BUG_ON(!wiphy
->bands
[band
]);
1228 sband
= wiphy
->bands
[band
];
1230 for (i
= 0; i
< sband
->n_channels
; i
++)
1231 reg_process_ht_flags_channel(wiphy
, band
, i
);
1234 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1236 enum ieee80211_band band
;
1241 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1242 if (wiphy
->bands
[band
])
1243 reg_process_ht_flags_band(wiphy
, band
);
1248 void wiphy_update_regulatory(struct wiphy
*wiphy
,
1249 enum nl80211_reg_initiator initiator
)
1251 enum ieee80211_band band
;
1253 if (ignore_reg_update(wiphy
, initiator
))
1255 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1256 if (wiphy
->bands
[band
])
1257 handle_band(wiphy
, band
);
1260 reg_process_beacons(wiphy
);
1261 reg_process_ht_flags(wiphy
);
1262 if (wiphy
->reg_notifier
)
1263 wiphy
->reg_notifier(wiphy
, last_request
);
1266 static void handle_channel_custom(struct wiphy
*wiphy
,
1267 enum ieee80211_band band
,
1268 unsigned int chan_idx
,
1269 const struct ieee80211_regdomain
*regd
)
1272 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
1274 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1275 const struct ieee80211_power_rule
*power_rule
= NULL
;
1276 const struct ieee80211_freq_range
*freq_range
= NULL
;
1277 struct ieee80211_supported_band
*sband
;
1278 struct ieee80211_channel
*chan
;
1282 sband
= wiphy
->bands
[band
];
1283 BUG_ON(chan_idx
>= sband
->n_channels
);
1284 chan
= &sband
->channels
[chan_idx
];
1286 r
= freq_reg_info_regd(wiphy
,
1287 MHZ_TO_KHZ(chan
->center_freq
),
1293 chan
->flags
= IEEE80211_CHAN_DISABLED
;
1297 power_rule
= ®_rule
->power_rule
;
1298 freq_range
= ®_rule
->freq_range
;
1300 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1301 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1303 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1304 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1305 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1308 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
1309 const struct ieee80211_regdomain
*regd
)
1312 struct ieee80211_supported_band
*sband
;
1314 BUG_ON(!wiphy
->bands
[band
]);
1315 sband
= wiphy
->bands
[band
];
1317 for (i
= 0; i
< sband
->n_channels
; i
++)
1318 handle_channel_custom(wiphy
, band
, i
, regd
);
1321 /* Used by drivers prior to wiphy registration */
1322 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1323 const struct ieee80211_regdomain
*regd
)
1325 enum ieee80211_band band
;
1326 unsigned int bands_set
= 0;
1328 mutex_lock(®_mutex
);
1329 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1330 if (!wiphy
->bands
[band
])
1332 handle_band_custom(wiphy
, band
, regd
);
1335 mutex_unlock(®_mutex
);
1338 * no point in calling this if it won't have any effect
1339 * on your device's supportd bands.
1341 WARN_ON(!bands_set
);
1343 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1345 static int reg_copy_regd(const struct ieee80211_regdomain
**dst_regd
,
1346 const struct ieee80211_regdomain
*src_regd
)
1348 struct ieee80211_regdomain
*regd
;
1349 int size_of_regd
= 0;
1352 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
1353 ((src_regd
->n_reg_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
1355 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
1359 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
1361 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
1362 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
1363 sizeof(struct ieee80211_reg_rule
));
1370 * Return value which can be used by ignore_request() to indicate
1371 * it has been determined we should intersect two regulatory domains
1373 #define REG_INTERSECT 1
1375 /* This has the logic which determines when a new request
1376 * should be ignored. */
1377 static int ignore_request(struct wiphy
*wiphy
,
1378 struct regulatory_request
*pending_request
)
1380 struct wiphy
*last_wiphy
= NULL
;
1382 assert_cfg80211_lock();
1384 /* All initial requests are respected */
1388 switch (pending_request
->initiator
) {
1389 case NL80211_REGDOM_SET_BY_CORE
:
1391 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1393 last_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1395 if (unlikely(!is_an_alpha2(pending_request
->alpha2
)))
1397 if (last_request
->initiator
==
1398 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1399 if (last_wiphy
!= wiphy
) {
1401 * Two cards with two APs claiming different
1402 * Country IE alpha2s. We could
1403 * intersect them, but that seems unlikely
1404 * to be correct. Reject second one for now.
1406 if (regdom_changes(pending_request
->alpha2
))
1411 * Two consecutive Country IE hints on the same wiphy.
1412 * This should be picked up early by the driver/stack
1414 if (WARN_ON(regdom_changes(pending_request
->alpha2
)))
1418 return REG_INTERSECT
;
1419 case NL80211_REGDOM_SET_BY_DRIVER
:
1420 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1421 if (is_old_static_regdom(cfg80211_regdomain
))
1423 if (regdom_changes(pending_request
->alpha2
))
1429 * This would happen if you unplug and plug your card
1430 * back in or if you add a new device for which the previously
1431 * loaded card also agrees on the regulatory domain.
1433 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1434 !regdom_changes(pending_request
->alpha2
))
1437 return REG_INTERSECT
;
1438 case NL80211_REGDOM_SET_BY_USER
:
1439 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1440 return REG_INTERSECT
;
1442 * If the user knows better the user should set the regdom
1443 * to their country before the IE is picked up
1445 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1446 last_request
->intersect
)
1449 * Process user requests only after previous user/driver/core
1450 * requests have been processed
1452 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1453 last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1454 last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1455 if (regdom_changes(last_request
->alpha2
))
1459 if (!is_old_static_regdom(cfg80211_regdomain
) &&
1460 !regdom_changes(pending_request
->alpha2
))
1470 * __regulatory_hint - hint to the wireless core a regulatory domain
1471 * @wiphy: if the hint comes from country information from an AP, this
1472 * is required to be set to the wiphy that received the information
1473 * @pending_request: the regulatory request currently being processed
1475 * The Wireless subsystem can use this function to hint to the wireless core
1476 * what it believes should be the current regulatory domain.
1478 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1479 * already been set or other standard error codes.
1481 * Caller must hold &cfg80211_mutex and ®_mutex
1483 static int __regulatory_hint(struct wiphy
*wiphy
,
1484 struct regulatory_request
*pending_request
)
1486 bool intersect
= false;
1489 assert_cfg80211_lock();
1491 r
= ignore_request(wiphy
, pending_request
);
1493 if (r
== REG_INTERSECT
) {
1494 if (pending_request
->initiator
==
1495 NL80211_REGDOM_SET_BY_DRIVER
) {
1496 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1498 kfree(pending_request
);
1505 * If the regulatory domain being requested by the
1506 * driver has already been set just copy it to the
1509 if (r
== -EALREADY
&&
1510 pending_request
->initiator
==
1511 NL80211_REGDOM_SET_BY_DRIVER
) {
1512 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1514 kfree(pending_request
);
1520 kfree(pending_request
);
1525 kfree(last_request
);
1527 last_request
= pending_request
;
1528 last_request
->intersect
= intersect
;
1530 pending_request
= NULL
;
1532 /* When r == REG_INTERSECT we do need to call CRDA */
1535 * Since CRDA will not be called in this case as we already
1536 * have applied the requested regulatory domain before we just
1537 * inform userspace we have processed the request
1540 nl80211_send_reg_change_event(last_request
);
1544 return call_crda(last_request
->alpha2
);
1547 /* This processes *all* regulatory hints */
1548 static void reg_process_hint(struct regulatory_request
*reg_request
)
1551 struct wiphy
*wiphy
= NULL
;
1553 BUG_ON(!reg_request
->alpha2
);
1555 mutex_lock(&cfg80211_mutex
);
1556 mutex_lock(®_mutex
);
1558 if (wiphy_idx_valid(reg_request
->wiphy_idx
))
1559 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1561 if (reg_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1567 r
= __regulatory_hint(wiphy
, reg_request
);
1568 /* This is required so that the orig_* parameters are saved */
1569 if (r
== -EALREADY
&& wiphy
&&
1570 wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
)
1571 wiphy_update_regulatory(wiphy
, reg_request
->initiator
);
1573 mutex_unlock(®_mutex
);
1574 mutex_unlock(&cfg80211_mutex
);
1577 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1578 static void reg_process_pending_hints(void)
1580 struct regulatory_request
*reg_request
;
1582 spin_lock(®_requests_lock
);
1583 while (!list_empty(®_requests_list
)) {
1584 reg_request
= list_first_entry(®_requests_list
,
1585 struct regulatory_request
,
1587 list_del_init(®_request
->list
);
1589 spin_unlock(®_requests_lock
);
1590 reg_process_hint(reg_request
);
1591 spin_lock(®_requests_lock
);
1593 spin_unlock(®_requests_lock
);
1596 /* Processes beacon hints -- this has nothing to do with country IEs */
1597 static void reg_process_pending_beacon_hints(void)
1599 struct cfg80211_registered_device
*rdev
;
1600 struct reg_beacon
*pending_beacon
, *tmp
;
1603 * No need to hold the reg_mutex here as we just touch wiphys
1604 * and do not read or access regulatory variables.
1606 mutex_lock(&cfg80211_mutex
);
1608 /* This goes through the _pending_ beacon list */
1609 spin_lock_bh(®_pending_beacons_lock
);
1611 if (list_empty(®_pending_beacons
)) {
1612 spin_unlock_bh(®_pending_beacons_lock
);
1616 list_for_each_entry_safe(pending_beacon
, tmp
,
1617 ®_pending_beacons
, list
) {
1619 list_del_init(&pending_beacon
->list
);
1621 /* Applies the beacon hint to current wiphys */
1622 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1623 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1625 /* Remembers the beacon hint for new wiphys or reg changes */
1626 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1629 spin_unlock_bh(®_pending_beacons_lock
);
1631 mutex_unlock(&cfg80211_mutex
);
1634 static void reg_todo(struct work_struct
*work
)
1636 reg_process_pending_hints();
1637 reg_process_pending_beacon_hints();
1640 static DECLARE_WORK(reg_work
, reg_todo
);
1642 static void queue_regulatory_request(struct regulatory_request
*request
)
1644 spin_lock(®_requests_lock
);
1645 list_add_tail(&request
->list
, ®_requests_list
);
1646 spin_unlock(®_requests_lock
);
1648 schedule_work(®_work
);
1651 /* Core regulatory hint -- happens once during cfg80211_init() */
1652 static int regulatory_hint_core(const char *alpha2
)
1654 struct regulatory_request
*request
;
1656 BUG_ON(last_request
);
1658 request
= kzalloc(sizeof(struct regulatory_request
),
1663 request
->alpha2
[0] = alpha2
[0];
1664 request
->alpha2
[1] = alpha2
[1];
1665 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1667 queue_regulatory_request(request
);
1670 * This ensures last_request is populated once modules
1671 * come swinging in and calling regulatory hints and
1672 * wiphy_apply_custom_regulatory().
1674 flush_scheduled_work();
1680 int regulatory_hint_user(const char *alpha2
)
1682 struct regulatory_request
*request
;
1686 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1690 request
->wiphy_idx
= WIPHY_IDX_STALE
;
1691 request
->alpha2
[0] = alpha2
[0];
1692 request
->alpha2
[1] = alpha2
[1];
1693 request
->initiator
= NL80211_REGDOM_SET_BY_USER
;
1695 queue_regulatory_request(request
);
1701 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1703 struct regulatory_request
*request
;
1708 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1712 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1714 /* Must have registered wiphy first */
1715 BUG_ON(!wiphy_idx_valid(request
->wiphy_idx
));
1717 request
->alpha2
[0] = alpha2
[0];
1718 request
->alpha2
[1] = alpha2
[1];
1719 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
1721 queue_regulatory_request(request
);
1725 EXPORT_SYMBOL(regulatory_hint
);
1727 /* Caller must hold reg_mutex */
1728 static bool reg_same_country_ie_hint(struct wiphy
*wiphy
,
1729 u32 country_ie_checksum
)
1731 struct wiphy
*request_wiphy
;
1735 if (unlikely(last_request
->initiator
!=
1736 NL80211_REGDOM_SET_BY_COUNTRY_IE
))
1739 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1744 if (likely(request_wiphy
!= wiphy
))
1745 return !country_ie_integrity_changes(country_ie_checksum
);
1747 * We should not have let these through at this point, they
1748 * should have been picked up earlier by the first alpha2 check
1751 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum
)))
1757 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1758 * therefore cannot iterate over the rdev list here.
1760 void regulatory_hint_11d(struct wiphy
*wiphy
,
1764 struct ieee80211_regdomain
*rd
= NULL
;
1767 enum environment_cap env
= ENVIRON_ANY
;
1768 struct regulatory_request
*request
;
1770 mutex_lock(®_mutex
);
1772 if (unlikely(!last_request
))
1775 /* IE len must be evenly divisible by 2 */
1776 if (country_ie_len
& 0x01)
1779 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
1783 * Pending country IE processing, this can happen after we
1784 * call CRDA and wait for a response if a beacon was received before
1785 * we were able to process the last regulatory_hint_11d() call
1787 if (country_ie_regdomain
)
1790 alpha2
[0] = country_ie
[0];
1791 alpha2
[1] = country_ie
[1];
1793 if (country_ie
[2] == 'I')
1794 env
= ENVIRON_INDOOR
;
1795 else if (country_ie
[2] == 'O')
1796 env
= ENVIRON_OUTDOOR
;
1799 * We will run this only upon a successful connection on cfg80211.
1800 * We leave conflict resolution to the workqueue, where can hold
1803 if (likely(last_request
->initiator
==
1804 NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1805 wiphy_idx_valid(last_request
->wiphy_idx
)))
1808 rd
= country_ie_2_rd(country_ie
, country_ie_len
, &checksum
);
1813 * This will not happen right now but we leave it here for the
1814 * the future when we want to add suspend/resume support and having
1815 * the user move to another country after doing so, or having the user
1816 * move to another AP. Right now we just trust the first AP.
1818 * If we hit this before we add this support we want to be informed of
1819 * it as it would indicate a mistake in the current design
1821 if (WARN_ON(reg_same_country_ie_hint(wiphy
, checksum
)))
1824 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1829 * We keep this around for when CRDA comes back with a response so
1830 * we can intersect with that
1832 country_ie_regdomain
= rd
;
1834 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1835 request
->alpha2
[0] = rd
->alpha2
[0];
1836 request
->alpha2
[1] = rd
->alpha2
[1];
1837 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
1838 request
->country_ie_checksum
= checksum
;
1839 request
->country_ie_env
= env
;
1841 mutex_unlock(®_mutex
);
1843 queue_regulatory_request(request
);
1850 mutex_unlock(®_mutex
);
1853 static bool freq_is_chan_12_13_14(u16 freq
)
1855 if (freq
== ieee80211_channel_to_frequency(12) ||
1856 freq
== ieee80211_channel_to_frequency(13) ||
1857 freq
== ieee80211_channel_to_frequency(14))
1862 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
1863 struct ieee80211_channel
*beacon_chan
,
1866 struct reg_beacon
*reg_beacon
;
1868 if (likely((beacon_chan
->beacon_found
||
1869 (beacon_chan
->flags
& IEEE80211_CHAN_RADAR
) ||
1870 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
1871 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))))
1874 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
1878 #ifdef CONFIG_CFG80211_REG_DEBUG
1879 printk(KERN_DEBUG
"cfg80211: Found new beacon on "
1880 "frequency: %d MHz (Ch %d) on %s\n",
1881 beacon_chan
->center_freq
,
1882 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
1885 memcpy(®_beacon
->chan
, beacon_chan
,
1886 sizeof(struct ieee80211_channel
));
1890 * Since we can be called from BH or and non-BH context
1891 * we must use spin_lock_bh()
1893 spin_lock_bh(®_pending_beacons_lock
);
1894 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
1895 spin_unlock_bh(®_pending_beacons_lock
);
1897 schedule_work(®_work
);
1902 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
1905 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1906 const struct ieee80211_freq_range
*freq_range
= NULL
;
1907 const struct ieee80211_power_rule
*power_rule
= NULL
;
1909 printk(KERN_INFO
" (start_freq - end_freq @ bandwidth), "
1910 "(max_antenna_gain, max_eirp)\n");
1912 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
1913 reg_rule
= &rd
->reg_rules
[i
];
1914 freq_range
= ®_rule
->freq_range
;
1915 power_rule
= ®_rule
->power_rule
;
1918 * There may not be documentation for max antenna gain
1919 * in certain regions
1921 if (power_rule
->max_antenna_gain
)
1922 printk(KERN_INFO
" (%d KHz - %d KHz @ %d KHz), "
1923 "(%d mBi, %d mBm)\n",
1924 freq_range
->start_freq_khz
,
1925 freq_range
->end_freq_khz
,
1926 freq_range
->max_bandwidth_khz
,
1927 power_rule
->max_antenna_gain
,
1928 power_rule
->max_eirp
);
1930 printk(KERN_INFO
" (%d KHz - %d KHz @ %d KHz), "
1932 freq_range
->start_freq_khz
,
1933 freq_range
->end_freq_khz
,
1934 freq_range
->max_bandwidth_khz
,
1935 power_rule
->max_eirp
);
1939 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
1942 if (is_intersected_alpha2(rd
->alpha2
)) {
1944 if (last_request
->initiator
==
1945 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1946 struct cfg80211_registered_device
*rdev
;
1947 rdev
= cfg80211_rdev_by_wiphy_idx(
1948 last_request
->wiphy_idx
);
1950 printk(KERN_INFO
"cfg80211: Current regulatory "
1951 "domain updated by AP to: %c%c\n",
1952 rdev
->country_ie_alpha2
[0],
1953 rdev
->country_ie_alpha2
[1]);
1955 printk(KERN_INFO
"cfg80211: Current regulatory "
1956 "domain intersected: \n");
1958 printk(KERN_INFO
"cfg80211: Current regulatory "
1959 "domain intersected: \n");
1960 } else if (is_world_regdom(rd
->alpha2
))
1961 printk(KERN_INFO
"cfg80211: World regulatory "
1962 "domain updated:\n");
1964 if (is_unknown_alpha2(rd
->alpha2
))
1965 printk(KERN_INFO
"cfg80211: Regulatory domain "
1966 "changed to driver built-in settings "
1967 "(unknown country)\n");
1969 printk(KERN_INFO
"cfg80211: Regulatory domain "
1970 "changed to country: %c%c\n",
1971 rd
->alpha2
[0], rd
->alpha2
[1]);
1976 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
1978 printk(KERN_INFO
"cfg80211: Regulatory domain: %c%c\n",
1979 rd
->alpha2
[0], rd
->alpha2
[1]);
1983 #ifdef CONFIG_CFG80211_REG_DEBUG
1984 static void reg_country_ie_process_debug(
1985 const struct ieee80211_regdomain
*rd
,
1986 const struct ieee80211_regdomain
*country_ie_regdomain
,
1987 const struct ieee80211_regdomain
*intersected_rd
)
1989 printk(KERN_DEBUG
"cfg80211: Received country IE:\n");
1990 print_regdomain_info(country_ie_regdomain
);
1991 printk(KERN_DEBUG
"cfg80211: CRDA thinks this should applied:\n");
1992 print_regdomain_info(rd
);
1993 if (intersected_rd
) {
1994 printk(KERN_DEBUG
"cfg80211: We intersect both of these "
1996 print_regdomain_info(intersected_rd
);
1999 printk(KERN_DEBUG
"cfg80211: Intersection between both failed\n");
2002 static inline void reg_country_ie_process_debug(
2003 const struct ieee80211_regdomain
*rd
,
2004 const struct ieee80211_regdomain
*country_ie_regdomain
,
2005 const struct ieee80211_regdomain
*intersected_rd
)
2010 /* Takes ownership of rd only if it doesn't fail */
2011 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
2013 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2014 struct cfg80211_registered_device
*rdev
= NULL
;
2015 struct wiphy
*request_wiphy
;
2016 /* Some basic sanity checks first */
2018 if (is_world_regdom(rd
->alpha2
)) {
2019 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2021 update_world_regdomain(rd
);
2025 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2026 !is_unknown_alpha2(rd
->alpha2
))
2033 * Lets only bother proceeding on the same alpha2 if the current
2034 * rd is non static (it means CRDA was present and was used last)
2035 * and the pending request came in from a country IE
2037 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2039 * If someone else asked us to change the rd lets only bother
2040 * checking if the alpha2 changes if CRDA was already called
2042 if (!is_old_static_regdom(cfg80211_regdomain
) &&
2043 !regdom_changes(rd
->alpha2
))
2048 * Now lets set the regulatory domain, update all driver channels
2049 * and finally inform them of what we have done, in case they want
2050 * to review or adjust their own settings based on their own
2051 * internal EEPROM data
2054 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2057 if (!is_valid_rd(rd
)) {
2058 printk(KERN_ERR
"cfg80211: Invalid "
2059 "regulatory domain detected:\n");
2060 print_regdomain_info(rd
);
2064 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2066 if (!last_request
->intersect
) {
2069 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_DRIVER
) {
2071 cfg80211_regdomain
= rd
;
2076 * For a driver hint, lets copy the regulatory domain the
2077 * driver wanted to the wiphy to deal with conflicts
2081 * Userspace could have sent two replies with only
2082 * one kernel request.
2084 if (request_wiphy
->regd
)
2087 r
= reg_copy_regd(&request_wiphy
->regd
, rd
);
2092 cfg80211_regdomain
= rd
;
2096 /* Intersection requires a bit more work */
2098 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2100 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
2101 if (!intersected_rd
)
2105 * We can trash what CRDA provided now.
2106 * However if a driver requested this specific regulatory
2107 * domain we keep it for its private use
2109 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
)
2110 request_wiphy
->regd
= rd
;
2117 cfg80211_regdomain
= intersected_rd
;
2123 * Country IE requests are handled a bit differently, we intersect
2124 * the country IE rd with what CRDA believes that country should have
2128 * Userspace could have sent two replies with only
2129 * one kernel request. By the second reply we would have
2130 * already processed and consumed the country_ie_regdomain.
2132 if (!country_ie_regdomain
)
2134 BUG_ON(rd
== country_ie_regdomain
);
2137 * Intersect what CRDA returned and our what we
2138 * had built from the Country IE received
2141 intersected_rd
= regdom_intersect(rd
, country_ie_regdomain
);
2143 reg_country_ie_process_debug(rd
,
2144 country_ie_regdomain
,
2147 kfree(country_ie_regdomain
);
2148 country_ie_regdomain
= NULL
;
2150 if (!intersected_rd
)
2153 rdev
= wiphy_to_dev(request_wiphy
);
2155 rdev
->country_ie_alpha2
[0] = rd
->alpha2
[0];
2156 rdev
->country_ie_alpha2
[1] = rd
->alpha2
[1];
2157 rdev
->env
= last_request
->country_ie_env
;
2159 BUG_ON(intersected_rd
== rd
);
2165 cfg80211_regdomain
= intersected_rd
;
2172 * Use this call to set the current regulatory domain. Conflicts with
2173 * multiple drivers can be ironed out later. Caller must've already
2174 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2176 int set_regdom(const struct ieee80211_regdomain
*rd
)
2180 assert_cfg80211_lock();
2182 mutex_lock(®_mutex
);
2184 /* Note that this doesn't update the wiphys, this is done below */
2185 r
= __set_regdom(rd
);
2188 mutex_unlock(®_mutex
);
2192 /* This would make this whole thing pointless */
2193 if (!last_request
->intersect
)
2194 BUG_ON(rd
!= cfg80211_regdomain
);
2196 /* update all wiphys now with the new established regulatory domain */
2197 update_all_wiphy_regulatory(last_request
->initiator
);
2199 print_regdomain(cfg80211_regdomain
);
2201 nl80211_send_reg_change_event(last_request
);
2203 mutex_unlock(®_mutex
);
2208 /* Caller must hold cfg80211_mutex */
2209 void reg_device_remove(struct wiphy
*wiphy
)
2211 struct wiphy
*request_wiphy
= NULL
;
2213 assert_cfg80211_lock();
2215 mutex_lock(®_mutex
);
2220 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2222 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2225 last_request
->wiphy_idx
= WIPHY_IDX_STALE
;
2226 last_request
->country_ie_env
= ENVIRON_ANY
;
2228 mutex_unlock(®_mutex
);
2231 int regulatory_init(void)
2235 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2236 if (IS_ERR(reg_pdev
))
2237 return PTR_ERR(reg_pdev
);
2239 spin_lock_init(®_requests_lock
);
2240 spin_lock_init(®_pending_beacons_lock
);
2242 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2243 cfg80211_regdomain
= static_regdom(ieee80211_regdom
);
2245 printk(KERN_INFO
"cfg80211: Using static regulatory domain info\n");
2246 print_regdomain_info(cfg80211_regdomain
);
2248 cfg80211_regdomain
= cfg80211_world_regdom
;
2251 /* We always try to get an update for the static regdomain */
2252 err
= regulatory_hint_core(cfg80211_regdomain
->alpha2
);
2257 * N.B. kobject_uevent_env() can fail mainly for when we're out
2258 * memory which is handled and propagated appropriately above
2259 * but it can also fail during a netlink_broadcast() or during
2260 * early boot for call_usermodehelper(). For now treat these
2261 * errors as non-fatal.
2263 printk(KERN_ERR
"cfg80211: kobject_uevent_env() was unable "
2264 "to call CRDA during init");
2265 #ifdef CONFIG_CFG80211_REG_DEBUG
2266 /* We want to find out exactly why when debugging */
2272 * Finally, if the user set the module parameter treat it
2275 if (!is_world_regdom(ieee80211_regdom
))
2276 regulatory_hint_user(ieee80211_regdom
);
2281 void regulatory_exit(void)
2283 struct regulatory_request
*reg_request
, *tmp
;
2284 struct reg_beacon
*reg_beacon
, *btmp
;
2286 cancel_work_sync(®_work
);
2288 mutex_lock(&cfg80211_mutex
);
2289 mutex_lock(®_mutex
);
2293 kfree(country_ie_regdomain
);
2294 country_ie_regdomain
= NULL
;
2296 kfree(last_request
);
2298 platform_device_unregister(reg_pdev
);
2300 spin_lock_bh(®_pending_beacons_lock
);
2301 if (!list_empty(®_pending_beacons
)) {
2302 list_for_each_entry_safe(reg_beacon
, btmp
,
2303 ®_pending_beacons
, list
) {
2304 list_del(®_beacon
->list
);
2308 spin_unlock_bh(®_pending_beacons_lock
);
2310 if (!list_empty(®_beacon_list
)) {
2311 list_for_each_entry_safe(reg_beacon
, btmp
,
2312 ®_beacon_list
, list
) {
2313 list_del(®_beacon
->list
);
2318 spin_lock(®_requests_lock
);
2319 if (!list_empty(®_requests_list
)) {
2320 list_for_each_entry_safe(reg_request
, tmp
,
2321 ®_requests_list
, list
) {
2322 list_del(®_request
->list
);
2326 spin_unlock(®_requests_lock
);
2328 mutex_unlock(®_mutex
);
2329 mutex_unlock(&cfg80211_mutex
);