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 */
145 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
146 /* IEEE 802.11a, channel 40 */
147 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
148 /* IEEE 802.11a, channel 44 */
149 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
150 /* IEEE 802.11a, channels 48..64 */
151 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
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..14 */
162 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
163 /* IEEE 802.11a, channels 34..48 */
164 REG_RULE(5170-10, 5240+10, 40, 6, 20,
165 NL80211_RRF_PASSIVE_SCAN
),
166 /* IEEE 802.11a, channels 52..64 */
167 REG_RULE(5260-10, 5320+10, 40, 6, 20,
168 NL80211_RRF_NO_IBSS
|
173 static const struct ieee80211_regdomain eu_regdom
= {
176 * This alpha2 is bogus, we leave it here just for stupid
177 * backward compatibility
181 /* IEEE 802.11b/g, channels 1..13 */
182 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
183 /* IEEE 802.11a, channel 36 */
184 REG_RULE(5180-10, 5180+10, 40, 6, 23,
185 NL80211_RRF_PASSIVE_SCAN
),
186 /* IEEE 802.11a, channel 40 */
187 REG_RULE(5200-10, 5200+10, 40, 6, 23,
188 NL80211_RRF_PASSIVE_SCAN
),
189 /* IEEE 802.11a, channel 44 */
190 REG_RULE(5220-10, 5220+10, 40, 6, 23,
191 NL80211_RRF_PASSIVE_SCAN
),
192 /* IEEE 802.11a, channels 48..64 */
193 REG_RULE(5240-10, 5320+10, 40, 6, 20,
194 NL80211_RRF_NO_IBSS
|
196 /* IEEE 802.11a, channels 100..140 */
197 REG_RULE(5500-10, 5700+10, 40, 6, 30,
198 NL80211_RRF_NO_IBSS
|
203 static const struct ieee80211_regdomain
*static_regdom(char *alpha2
)
205 if (alpha2
[0] == 'U' && alpha2
[1] == 'S')
207 if (alpha2
[0] == 'J' && alpha2
[1] == 'P')
209 if (alpha2
[0] == 'E' && alpha2
[1] == 'U')
211 /* Default, as per the old rules */
215 static bool is_old_static_regdom(const struct ieee80211_regdomain
*rd
)
217 if (rd
== &us_regdom
|| rd
== &jp_regdom
|| rd
== &eu_regdom
)
222 static inline bool is_old_static_regdom(const struct ieee80211_regdomain
*rd
)
228 static void reset_regdomains(void)
230 /* avoid freeing static information or freeing something twice */
231 if (cfg80211_regdomain
== cfg80211_world_regdom
)
232 cfg80211_regdomain
= NULL
;
233 if (cfg80211_world_regdom
== &world_regdom
)
234 cfg80211_world_regdom
= NULL
;
235 if (cfg80211_regdomain
== &world_regdom
)
236 cfg80211_regdomain
= NULL
;
237 if (is_old_static_regdom(cfg80211_regdomain
))
238 cfg80211_regdomain
= NULL
;
240 kfree(cfg80211_regdomain
);
241 kfree(cfg80211_world_regdom
);
243 cfg80211_world_regdom
= &world_regdom
;
244 cfg80211_regdomain
= NULL
;
248 * Dynamic world regulatory domain requested by the wireless
249 * core upon initialization
251 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
253 BUG_ON(!last_request
);
257 cfg80211_world_regdom
= rd
;
258 cfg80211_regdomain
= rd
;
261 bool is_world_regdom(const char *alpha2
)
265 if (alpha2
[0] == '0' && alpha2
[1] == '0')
270 static bool is_alpha2_set(const char *alpha2
)
274 if (alpha2
[0] != 0 && alpha2
[1] != 0)
279 static bool is_alpha_upper(char letter
)
282 if (letter
>= 65 && letter
<= 90)
287 static bool is_unknown_alpha2(const char *alpha2
)
292 * Special case where regulatory domain was built by driver
293 * but a specific alpha2 cannot be determined
295 if (alpha2
[0] == '9' && alpha2
[1] == '9')
300 static bool is_intersected_alpha2(const char *alpha2
)
305 * Special case where regulatory domain is the
306 * result of an intersection between two regulatory domain
309 if (alpha2
[0] == '9' && alpha2
[1] == '8')
314 static bool is_an_alpha2(const char *alpha2
)
318 if (is_alpha_upper(alpha2
[0]) && is_alpha_upper(alpha2
[1]))
323 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
325 if (!alpha2_x
|| !alpha2_y
)
327 if (alpha2_x
[0] == alpha2_y
[0] &&
328 alpha2_x
[1] == alpha2_y
[1])
333 static bool regdom_changes(const char *alpha2
)
335 assert_cfg80211_lock();
337 if (!cfg80211_regdomain
)
339 if (alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
345 * country_ie_integrity_changes - tells us if the country IE has changed
346 * @checksum: checksum of country IE of fields we are interested in
348 * If the country IE has not changed you can ignore it safely. This is
349 * useful to determine if two devices are seeing two different country IEs
350 * even on the same alpha2. Note that this will return false if no IE has
351 * been set on the wireless core yet.
353 static bool country_ie_integrity_changes(u32 checksum
)
355 /* If no IE has been set then the checksum doesn't change */
356 if (unlikely(!last_request
->country_ie_checksum
))
358 if (unlikely(last_request
->country_ie_checksum
!= checksum
))
364 * This lets us keep regulatory code which is updated on a regulatory
365 * basis in userspace.
367 static int call_crda(const char *alpha2
)
369 char country_env
[9 + 2] = "COUNTRY=";
375 if (!is_world_regdom((char *) alpha2
))
376 printk(KERN_INFO
"cfg80211: Calling CRDA for country: %c%c\n",
377 alpha2
[0], alpha2
[1]);
379 printk(KERN_INFO
"cfg80211: Calling CRDA to update world "
380 "regulatory domain\n");
382 country_env
[8] = alpha2
[0];
383 country_env
[9] = alpha2
[1];
385 return kobject_uevent_env(®_pdev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
388 /* Used by nl80211 before kmalloc'ing our regulatory domain */
389 bool reg_is_valid_request(const char *alpha2
)
391 assert_cfg80211_lock();
396 return alpha2_equal(last_request
->alpha2
, alpha2
);
399 /* Sanity check on a regulatory rule */
400 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
402 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
405 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
408 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
411 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
413 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
414 freq_range
->max_bandwidth_khz
> freq_diff
)
420 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
422 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
425 if (!rd
->n_reg_rules
)
428 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
431 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
432 reg_rule
= &rd
->reg_rules
[i
];
433 if (!is_valid_reg_rule(reg_rule
))
440 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
444 u32 start_freq_khz
, end_freq_khz
;
446 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
447 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
449 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
450 end_freq_khz
<= freq_range
->end_freq_khz
)
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 bool bw_fits
= false;
863 desired_bw_khz
= MHZ_TO_KHZ(20);
865 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
868 * Follow the driver's regulatory domain, if present, unless a country
869 * IE has been processed or a user wants to help complaince further
871 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
872 last_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
879 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
880 const struct ieee80211_reg_rule
*rr
;
881 const struct ieee80211_freq_range
*fr
= NULL
;
882 const struct ieee80211_power_rule
*pr
= NULL
;
884 rr
= ®d
->reg_rules
[i
];
885 fr
= &rr
->freq_range
;
886 pr
= &rr
->power_rule
;
889 * We only need to know if one frequency rule was
890 * was in center_freq's band, that's enough, so lets
891 * not overwrite it once found
893 if (!band_rule_found
)
894 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
896 bw_fits
= reg_does_bw_fit(fr
,
900 if (band_rule_found
&& bw_fits
) {
906 if (!band_rule_found
)
911 EXPORT_SYMBOL(freq_reg_info
);
913 int freq_reg_info(struct wiphy
*wiphy
,
916 const struct ieee80211_reg_rule
**reg_rule
)
918 assert_cfg80211_lock();
919 return freq_reg_info_regd(wiphy
,
927 * Note that right now we assume the desired channel bandwidth
928 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
929 * per channel, the primary and the extension channel). To support
930 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
931 * new ieee80211_channel.target_bw and re run the regulatory check
932 * on the wiphy with the target_bw specified. Then we can simply use
933 * that below for the desired_bw_khz below.
935 static void handle_channel(struct wiphy
*wiphy
, enum ieee80211_band band
,
936 unsigned int chan_idx
)
939 u32 flags
, bw_flags
= 0;
940 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
941 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
942 const struct ieee80211_power_rule
*power_rule
= NULL
;
943 const struct ieee80211_freq_range
*freq_range
= NULL
;
944 struct ieee80211_supported_band
*sband
;
945 struct ieee80211_channel
*chan
;
946 struct wiphy
*request_wiphy
= NULL
;
948 assert_cfg80211_lock();
950 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
952 sband
= wiphy
->bands
[band
];
953 BUG_ON(chan_idx
>= sband
->n_channels
);
954 chan
= &sband
->channels
[chan_idx
];
956 flags
= chan
->orig_flags
;
958 r
= freq_reg_info(wiphy
,
959 MHZ_TO_KHZ(chan
->center_freq
),
965 * This means no regulatory rule was found in the country IE
966 * with a frequency range on the center_freq's band, since
967 * IEEE-802.11 allows for a country IE to have a subset of the
968 * regulatory information provided in a country we ignore
969 * disabling the channel unless at least one reg rule was
970 * found on the center_freq's band. For details see this
973 * http://tinyurl.com/11d-clarification
976 last_request
->initiator
==
977 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
978 #ifdef CONFIG_CFG80211_REG_DEBUG
979 printk(KERN_DEBUG
"cfg80211: Leaving channel %d MHz "
980 "intact on %s - no rule found in band on "
982 chan
->center_freq
, wiphy_name(wiphy
));
986 * In this case we know the country IE has at least one reg rule
987 * for the band so we respect its band definitions
989 #ifdef CONFIG_CFG80211_REG_DEBUG
990 if (last_request
->initiator
==
991 NL80211_REGDOM_SET_BY_COUNTRY_IE
)
992 printk(KERN_DEBUG
"cfg80211: Disabling "
993 "channel %d MHz on %s due to "
995 chan
->center_freq
, wiphy_name(wiphy
));
997 flags
|= IEEE80211_CHAN_DISABLED
;
1003 power_rule
= ®_rule
->power_rule
;
1004 freq_range
= ®_rule
->freq_range
;
1006 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1007 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1009 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1010 request_wiphy
&& request_wiphy
== wiphy
&&
1011 request_wiphy
->strict_regulatory
) {
1013 * This gaurantees the driver's requested regulatory domain
1014 * will always be used as a base for further regulatory
1017 chan
->flags
= chan
->orig_flags
=
1018 map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1019 chan
->max_antenna_gain
= chan
->orig_mag
=
1020 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1021 chan
->max_bandwidth
= KHZ_TO_MHZ(desired_bw_khz
);
1022 chan
->max_power
= chan
->orig_mpwr
=
1023 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1027 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
1028 chan
->max_antenna_gain
= min(chan
->orig_mag
,
1029 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
1030 chan
->max_bandwidth
= KHZ_TO_MHZ(desired_bw_khz
);
1031 if (chan
->orig_mpwr
)
1032 chan
->max_power
= min(chan
->orig_mpwr
,
1033 (int) MBM_TO_DBM(power_rule
->max_eirp
));
1035 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1038 static void handle_band(struct wiphy
*wiphy
, enum ieee80211_band band
)
1041 struct ieee80211_supported_band
*sband
;
1043 BUG_ON(!wiphy
->bands
[band
]);
1044 sband
= wiphy
->bands
[band
];
1046 for (i
= 0; i
< sband
->n_channels
; i
++)
1047 handle_channel(wiphy
, band
, i
);
1050 static bool ignore_reg_update(struct wiphy
*wiphy
,
1051 enum nl80211_reg_initiator initiator
)
1055 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
1056 wiphy
->custom_regulatory
)
1059 * wiphy->regd will be set once the device has its own
1060 * desired regulatory domain set
1062 if (wiphy
->strict_regulatory
&& !wiphy
->regd
&&
1063 !is_world_regdom(last_request
->alpha2
))
1068 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
1070 struct cfg80211_registered_device
*rdev
;
1072 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1073 wiphy_update_regulatory(&rdev
->wiphy
, initiator
);
1076 static void handle_reg_beacon(struct wiphy
*wiphy
,
1077 unsigned int chan_idx
,
1078 struct reg_beacon
*reg_beacon
)
1080 struct ieee80211_supported_band
*sband
;
1081 struct ieee80211_channel
*chan
;
1082 bool channel_changed
= false;
1083 struct ieee80211_channel chan_before
;
1085 assert_cfg80211_lock();
1087 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1088 chan
= &sband
->channels
[chan_idx
];
1090 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
1093 if (chan
->beacon_found
)
1096 chan
->beacon_found
= true;
1098 chan_before
.center_freq
= chan
->center_freq
;
1099 chan_before
.flags
= chan
->flags
;
1101 if ((chan
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
) &&
1102 !(chan
->orig_flags
& IEEE80211_CHAN_PASSIVE_SCAN
)) {
1103 chan
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
1104 channel_changed
= true;
1107 if ((chan
->flags
& IEEE80211_CHAN_NO_IBSS
) &&
1108 !(chan
->orig_flags
& IEEE80211_CHAN_NO_IBSS
)) {
1109 chan
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
1110 channel_changed
= true;
1113 if (channel_changed
)
1114 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
1118 * Called when a scan on a wiphy finds a beacon on
1121 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
1122 struct reg_beacon
*reg_beacon
)
1125 struct ieee80211_supported_band
*sband
;
1127 assert_cfg80211_lock();
1129 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1132 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1134 for (i
= 0; i
< sband
->n_channels
; i
++)
1135 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1139 * Called upon reg changes or a new wiphy is added
1141 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1144 struct ieee80211_supported_band
*sband
;
1145 struct reg_beacon
*reg_beacon
;
1147 assert_cfg80211_lock();
1149 if (list_empty(®_beacon_list
))
1152 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1153 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1155 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1156 for (i
= 0; i
< sband
->n_channels
; i
++)
1157 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1161 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1163 if (is_world_regdom(cfg80211_regdomain
->alpha2
) ||
1164 (wiphy
->regd
&& is_world_regdom(wiphy
->regd
->alpha2
)))
1167 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1168 wiphy
->custom_regulatory
)
1173 /* Reap the advantages of previously found beacons */
1174 static void reg_process_beacons(struct wiphy
*wiphy
)
1177 * Means we are just firing up cfg80211, so no beacons would
1178 * have been processed yet.
1182 if (!reg_is_world_roaming(wiphy
))
1184 wiphy_update_beacon_reg(wiphy
);
1187 static bool is_ht40_not_allowed(struct ieee80211_channel
*chan
)
1191 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1193 /* This would happen when regulatory rules disallow HT40 completely */
1194 if (IEEE80211_CHAN_NO_HT40
== (chan
->flags
& (IEEE80211_CHAN_NO_HT40
)))
1199 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1200 enum ieee80211_band band
,
1201 unsigned int chan_idx
)
1203 struct ieee80211_supported_band
*sband
;
1204 struct ieee80211_channel
*channel
;
1205 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1208 assert_cfg80211_lock();
1210 sband
= wiphy
->bands
[band
];
1211 BUG_ON(chan_idx
>= sband
->n_channels
);
1212 channel
= &sband
->channels
[chan_idx
];
1214 if (is_ht40_not_allowed(channel
)) {
1215 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1220 * We need to ensure the extension channels exist to
1221 * be able to use HT40- or HT40+, this finds them (or not)
1223 for (i
= 0; i
< sband
->n_channels
; i
++) {
1224 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1225 if (c
->center_freq
== (channel
->center_freq
- 20))
1227 if (c
->center_freq
== (channel
->center_freq
+ 20))
1232 * Please note that this assumes target bandwidth is 20 MHz,
1233 * if that ever changes we also need to change the below logic
1234 * to include that as well.
1236 if (is_ht40_not_allowed(channel_before
))
1237 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1239 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1241 if (is_ht40_not_allowed(channel_after
))
1242 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1244 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1247 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1248 enum ieee80211_band band
)
1251 struct ieee80211_supported_band
*sband
;
1253 BUG_ON(!wiphy
->bands
[band
]);
1254 sband
= wiphy
->bands
[band
];
1256 for (i
= 0; i
< sband
->n_channels
; i
++)
1257 reg_process_ht_flags_channel(wiphy
, band
, i
);
1260 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1262 enum ieee80211_band band
;
1267 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1268 if (wiphy
->bands
[band
])
1269 reg_process_ht_flags_band(wiphy
, band
);
1274 void wiphy_update_regulatory(struct wiphy
*wiphy
,
1275 enum nl80211_reg_initiator initiator
)
1277 enum ieee80211_band band
;
1279 if (ignore_reg_update(wiphy
, initiator
))
1281 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1282 if (wiphy
->bands
[band
])
1283 handle_band(wiphy
, band
);
1286 reg_process_beacons(wiphy
);
1287 reg_process_ht_flags(wiphy
);
1288 if (wiphy
->reg_notifier
)
1289 wiphy
->reg_notifier(wiphy
, last_request
);
1292 static void handle_channel_custom(struct wiphy
*wiphy
,
1293 enum ieee80211_band band
,
1294 unsigned int chan_idx
,
1295 const struct ieee80211_regdomain
*regd
)
1298 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
1300 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1301 const struct ieee80211_power_rule
*power_rule
= NULL
;
1302 const struct ieee80211_freq_range
*freq_range
= NULL
;
1303 struct ieee80211_supported_band
*sband
;
1304 struct ieee80211_channel
*chan
;
1308 sband
= wiphy
->bands
[band
];
1309 BUG_ON(chan_idx
>= sband
->n_channels
);
1310 chan
= &sband
->channels
[chan_idx
];
1312 r
= freq_reg_info_regd(wiphy
,
1313 MHZ_TO_KHZ(chan
->center_freq
),
1319 chan
->flags
= IEEE80211_CHAN_DISABLED
;
1323 power_rule
= ®_rule
->power_rule
;
1324 freq_range
= ®_rule
->freq_range
;
1326 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1327 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1329 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1330 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1331 chan
->max_bandwidth
= KHZ_TO_MHZ(desired_bw_khz
);
1332 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1335 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
1336 const struct ieee80211_regdomain
*regd
)
1339 struct ieee80211_supported_band
*sband
;
1341 BUG_ON(!wiphy
->bands
[band
]);
1342 sband
= wiphy
->bands
[band
];
1344 for (i
= 0; i
< sband
->n_channels
; i
++)
1345 handle_channel_custom(wiphy
, band
, i
, regd
);
1348 /* Used by drivers prior to wiphy registration */
1349 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1350 const struct ieee80211_regdomain
*regd
)
1352 enum ieee80211_band band
;
1353 unsigned int bands_set
= 0;
1355 mutex_lock(®_mutex
);
1356 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1357 if (!wiphy
->bands
[band
])
1359 handle_band_custom(wiphy
, band
, regd
);
1362 mutex_unlock(®_mutex
);
1365 * no point in calling this if it won't have any effect
1366 * on your device's supportd bands.
1368 WARN_ON(!bands_set
);
1370 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1372 static int reg_copy_regd(const struct ieee80211_regdomain
**dst_regd
,
1373 const struct ieee80211_regdomain
*src_regd
)
1375 struct ieee80211_regdomain
*regd
;
1376 int size_of_regd
= 0;
1379 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
1380 ((src_regd
->n_reg_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
1382 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
1386 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
1388 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
1389 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
1390 sizeof(struct ieee80211_reg_rule
));
1397 * Return value which can be used by ignore_request() to indicate
1398 * it has been determined we should intersect two regulatory domains
1400 #define REG_INTERSECT 1
1402 /* This has the logic which determines when a new request
1403 * should be ignored. */
1404 static int ignore_request(struct wiphy
*wiphy
,
1405 struct regulatory_request
*pending_request
)
1407 struct wiphy
*last_wiphy
= NULL
;
1409 assert_cfg80211_lock();
1411 /* All initial requests are respected */
1415 switch (pending_request
->initiator
) {
1416 case NL80211_REGDOM_SET_BY_CORE
:
1418 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1420 last_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1422 if (unlikely(!is_an_alpha2(pending_request
->alpha2
)))
1424 if (last_request
->initiator
==
1425 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1426 if (last_wiphy
!= wiphy
) {
1428 * Two cards with two APs claiming different
1429 * different Country IE alpha2s. We could
1430 * intersect them, but that seems unlikely
1431 * to be correct. Reject second one for now.
1433 if (regdom_changes(pending_request
->alpha2
))
1438 * Two consecutive Country IE hints on the same wiphy.
1439 * This should be picked up early by the driver/stack
1441 if (WARN_ON(regdom_changes(pending_request
->alpha2
)))
1445 return REG_INTERSECT
;
1446 case NL80211_REGDOM_SET_BY_DRIVER
:
1447 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1448 if (is_old_static_regdom(cfg80211_regdomain
))
1450 if (regdom_changes(pending_request
->alpha2
))
1456 * This would happen if you unplug and plug your card
1457 * back in or if you add a new device for which the previously
1458 * loaded card also agrees on the regulatory domain.
1460 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1461 !regdom_changes(pending_request
->alpha2
))
1464 return REG_INTERSECT
;
1465 case NL80211_REGDOM_SET_BY_USER
:
1466 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1467 return REG_INTERSECT
;
1469 * If the user knows better the user should set the regdom
1470 * to their country before the IE is picked up
1472 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1473 last_request
->intersect
)
1476 * Process user requests only after previous user/driver/core
1477 * requests have been processed
1479 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1480 last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1481 last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1482 if (regdom_changes(last_request
->alpha2
))
1486 if (!is_old_static_regdom(cfg80211_regdomain
) &&
1487 !regdom_changes(pending_request
->alpha2
))
1497 * __regulatory_hint - hint to the wireless core a regulatory domain
1498 * @wiphy: if the hint comes from country information from an AP, this
1499 * is required to be set to the wiphy that received the information
1500 * @pending_request: the regulatory request currently being processed
1502 * The Wireless subsystem can use this function to hint to the wireless core
1503 * what it believes should be the current regulatory domain.
1505 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1506 * already been set or other standard error codes.
1508 * Caller must hold &cfg80211_mutex and ®_mutex
1510 static int __regulatory_hint(struct wiphy
*wiphy
,
1511 struct regulatory_request
*pending_request
)
1513 bool intersect
= false;
1516 assert_cfg80211_lock();
1518 r
= ignore_request(wiphy
, pending_request
);
1520 if (r
== REG_INTERSECT
) {
1521 if (pending_request
->initiator
==
1522 NL80211_REGDOM_SET_BY_DRIVER
) {
1523 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1525 kfree(pending_request
);
1532 * If the regulatory domain being requested by the
1533 * driver has already been set just copy it to the
1536 if (r
== -EALREADY
&&
1537 pending_request
->initiator
==
1538 NL80211_REGDOM_SET_BY_DRIVER
) {
1539 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1541 kfree(pending_request
);
1547 kfree(pending_request
);
1552 kfree(last_request
);
1554 last_request
= pending_request
;
1555 last_request
->intersect
= intersect
;
1557 pending_request
= NULL
;
1559 /* When r == REG_INTERSECT we do need to call CRDA */
1562 * Since CRDA will not be called in this case as we already
1563 * have applied the requested regulatory domain before we just
1564 * inform userspace we have processed the request
1567 nl80211_send_reg_change_event(last_request
);
1571 return call_crda(last_request
->alpha2
);
1574 /* This processes *all* regulatory hints */
1575 static void reg_process_hint(struct regulatory_request
*reg_request
)
1578 struct wiphy
*wiphy
= NULL
;
1580 BUG_ON(!reg_request
->alpha2
);
1582 mutex_lock(&cfg80211_mutex
);
1583 mutex_lock(®_mutex
);
1585 if (wiphy_idx_valid(reg_request
->wiphy_idx
))
1586 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1588 if (reg_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1594 r
= __regulatory_hint(wiphy
, reg_request
);
1595 /* This is required so that the orig_* parameters are saved */
1596 if (r
== -EALREADY
&& wiphy
&& wiphy
->strict_regulatory
)
1597 wiphy_update_regulatory(wiphy
, reg_request
->initiator
);
1599 mutex_unlock(®_mutex
);
1600 mutex_unlock(&cfg80211_mutex
);
1603 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1604 static void reg_process_pending_hints(void)
1606 struct regulatory_request
*reg_request
;
1608 spin_lock(®_requests_lock
);
1609 while (!list_empty(®_requests_list
)) {
1610 reg_request
= list_first_entry(®_requests_list
,
1611 struct regulatory_request
,
1613 list_del_init(®_request
->list
);
1615 spin_unlock(®_requests_lock
);
1616 reg_process_hint(reg_request
);
1617 spin_lock(®_requests_lock
);
1619 spin_unlock(®_requests_lock
);
1622 /* Processes beacon hints -- this has nothing to do with country IEs */
1623 static void reg_process_pending_beacon_hints(void)
1625 struct cfg80211_registered_device
*rdev
;
1626 struct reg_beacon
*pending_beacon
, *tmp
;
1629 * No need to hold the reg_mutex here as we just touch wiphys
1630 * and do not read or access regulatory variables.
1632 mutex_lock(&cfg80211_mutex
);
1634 /* This goes through the _pending_ beacon list */
1635 spin_lock_bh(®_pending_beacons_lock
);
1637 if (list_empty(®_pending_beacons
)) {
1638 spin_unlock_bh(®_pending_beacons_lock
);
1642 list_for_each_entry_safe(pending_beacon
, tmp
,
1643 ®_pending_beacons
, list
) {
1645 list_del_init(&pending_beacon
->list
);
1647 /* Applies the beacon hint to current wiphys */
1648 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1649 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1651 /* Remembers the beacon hint for new wiphys or reg changes */
1652 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1655 spin_unlock_bh(®_pending_beacons_lock
);
1657 mutex_unlock(&cfg80211_mutex
);
1660 static void reg_todo(struct work_struct
*work
)
1662 reg_process_pending_hints();
1663 reg_process_pending_beacon_hints();
1666 static DECLARE_WORK(reg_work
, reg_todo
);
1668 static void queue_regulatory_request(struct regulatory_request
*request
)
1670 spin_lock(®_requests_lock
);
1671 list_add_tail(&request
->list
, ®_requests_list
);
1672 spin_unlock(®_requests_lock
);
1674 schedule_work(®_work
);
1677 /* Core regulatory hint -- happens once during cfg80211_init() */
1678 static int regulatory_hint_core(const char *alpha2
)
1680 struct regulatory_request
*request
;
1682 BUG_ON(last_request
);
1684 request
= kzalloc(sizeof(struct regulatory_request
),
1689 request
->alpha2
[0] = alpha2
[0];
1690 request
->alpha2
[1] = alpha2
[1];
1691 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1693 queue_regulatory_request(request
);
1696 * This ensures last_request is populated once modules
1697 * come swinging in and calling regulatory hints and
1698 * wiphy_apply_custom_regulatory().
1700 flush_scheduled_work();
1706 int regulatory_hint_user(const char *alpha2
)
1708 struct regulatory_request
*request
;
1712 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1716 request
->wiphy_idx
= WIPHY_IDX_STALE
;
1717 request
->alpha2
[0] = alpha2
[0];
1718 request
->alpha2
[1] = alpha2
[1];
1719 request
->initiator
= NL80211_REGDOM_SET_BY_USER
,
1721 queue_regulatory_request(request
);
1727 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1729 struct regulatory_request
*request
;
1734 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1738 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1740 /* Must have registered wiphy first */
1741 BUG_ON(!wiphy_idx_valid(request
->wiphy_idx
));
1743 request
->alpha2
[0] = alpha2
[0];
1744 request
->alpha2
[1] = alpha2
[1];
1745 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
1747 queue_regulatory_request(request
);
1751 EXPORT_SYMBOL(regulatory_hint
);
1753 /* Caller must hold reg_mutex */
1754 static bool reg_same_country_ie_hint(struct wiphy
*wiphy
,
1755 u32 country_ie_checksum
)
1757 struct wiphy
*request_wiphy
;
1761 if (unlikely(last_request
->initiator
!=
1762 NL80211_REGDOM_SET_BY_COUNTRY_IE
))
1765 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1770 if (likely(request_wiphy
!= wiphy
))
1771 return !country_ie_integrity_changes(country_ie_checksum
);
1773 * We should not have let these through at this point, they
1774 * should have been picked up earlier by the first alpha2 check
1777 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum
)))
1783 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1784 * therefore cannot iterate over the rdev list here.
1786 void regulatory_hint_11d(struct wiphy
*wiphy
,
1790 struct ieee80211_regdomain
*rd
= NULL
;
1793 enum environment_cap env
= ENVIRON_ANY
;
1794 struct regulatory_request
*request
;
1796 mutex_lock(®_mutex
);
1798 if (unlikely(!last_request
))
1801 /* IE len must be evenly divisible by 2 */
1802 if (country_ie_len
& 0x01)
1805 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
1809 * Pending country IE processing, this can happen after we
1810 * call CRDA and wait for a response if a beacon was received before
1811 * we were able to process the last regulatory_hint_11d() call
1813 if (country_ie_regdomain
)
1816 alpha2
[0] = country_ie
[0];
1817 alpha2
[1] = country_ie
[1];
1819 if (country_ie
[2] == 'I')
1820 env
= ENVIRON_INDOOR
;
1821 else if (country_ie
[2] == 'O')
1822 env
= ENVIRON_OUTDOOR
;
1825 * We will run this for *every* beacon processed for the BSSID, so
1826 * we optimize an early check to exit out early if we don't have to
1829 * We leave conflict resolution to the workqueue, where can hold
1832 if (likely(last_request
->initiator
==
1833 NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1834 wiphy_idx_valid(last_request
->wiphy_idx
)))
1837 rd
= country_ie_2_rd(country_ie
, country_ie_len
, &checksum
);
1842 * This will not happen right now but we leave it here for the
1843 * the future when we want to add suspend/resume support and having
1844 * the user move to another country after doing so, or having the user
1845 * move to another AP. Right now we just trust the first AP.
1847 * If we hit this before we add this support we want to be informed of
1848 * it as it would indicate a mistake in the current design
1850 if (WARN_ON(reg_same_country_ie_hint(wiphy
, checksum
)))
1853 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1858 * We keep this around for when CRDA comes back with a response so
1859 * we can intersect with that
1861 country_ie_regdomain
= rd
;
1863 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1864 request
->alpha2
[0] = rd
->alpha2
[0];
1865 request
->alpha2
[1] = rd
->alpha2
[1];
1866 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
1867 request
->country_ie_checksum
= checksum
;
1868 request
->country_ie_env
= env
;
1870 mutex_unlock(®_mutex
);
1872 queue_regulatory_request(request
);
1879 mutex_unlock(®_mutex
);
1881 EXPORT_SYMBOL(regulatory_hint_11d
);
1883 static bool freq_is_chan_12_13_14(u16 freq
)
1885 if (freq
== ieee80211_channel_to_frequency(12) ||
1886 freq
== ieee80211_channel_to_frequency(13) ||
1887 freq
== ieee80211_channel_to_frequency(14))
1892 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
1893 struct ieee80211_channel
*beacon_chan
,
1896 struct reg_beacon
*reg_beacon
;
1898 if (likely((beacon_chan
->beacon_found
||
1899 (beacon_chan
->flags
& IEEE80211_CHAN_RADAR
) ||
1900 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
1901 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))))
1904 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
1908 #ifdef CONFIG_CFG80211_REG_DEBUG
1909 printk(KERN_DEBUG
"cfg80211: Found new beacon on "
1910 "frequency: %d MHz (Ch %d) on %s\n",
1911 beacon_chan
->center_freq
,
1912 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
1915 memcpy(®_beacon
->chan
, beacon_chan
,
1916 sizeof(struct ieee80211_channel
));
1920 * Since we can be called from BH or and non-BH context
1921 * we must use spin_lock_bh()
1923 spin_lock_bh(®_pending_beacons_lock
);
1924 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
1925 spin_unlock_bh(®_pending_beacons_lock
);
1927 schedule_work(®_work
);
1932 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
1935 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1936 const struct ieee80211_freq_range
*freq_range
= NULL
;
1937 const struct ieee80211_power_rule
*power_rule
= NULL
;
1939 printk(KERN_INFO
"\t(start_freq - end_freq @ bandwidth), "
1940 "(max_antenna_gain, max_eirp)\n");
1942 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
1943 reg_rule
= &rd
->reg_rules
[i
];
1944 freq_range
= ®_rule
->freq_range
;
1945 power_rule
= ®_rule
->power_rule
;
1948 * There may not be documentation for max antenna gain
1949 * in certain regions
1951 if (power_rule
->max_antenna_gain
)
1952 printk(KERN_INFO
"\t(%d KHz - %d KHz @ %d KHz), "
1953 "(%d mBi, %d mBm)\n",
1954 freq_range
->start_freq_khz
,
1955 freq_range
->end_freq_khz
,
1956 freq_range
->max_bandwidth_khz
,
1957 power_rule
->max_antenna_gain
,
1958 power_rule
->max_eirp
);
1960 printk(KERN_INFO
"\t(%d KHz - %d KHz @ %d KHz), "
1962 freq_range
->start_freq_khz
,
1963 freq_range
->end_freq_khz
,
1964 freq_range
->max_bandwidth_khz
,
1965 power_rule
->max_eirp
);
1969 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
1972 if (is_intersected_alpha2(rd
->alpha2
)) {
1974 if (last_request
->initiator
==
1975 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1976 struct cfg80211_registered_device
*rdev
;
1977 rdev
= cfg80211_rdev_by_wiphy_idx(
1978 last_request
->wiphy_idx
);
1980 printk(KERN_INFO
"cfg80211: Current regulatory "
1981 "domain updated by AP to: %c%c\n",
1982 rdev
->country_ie_alpha2
[0],
1983 rdev
->country_ie_alpha2
[1]);
1985 printk(KERN_INFO
"cfg80211: Current regulatory "
1986 "domain intersected: \n");
1988 printk(KERN_INFO
"cfg80211: Current regulatory "
1989 "domain intersected: \n");
1990 } else if (is_world_regdom(rd
->alpha2
))
1991 printk(KERN_INFO
"cfg80211: World regulatory "
1992 "domain updated:\n");
1994 if (is_unknown_alpha2(rd
->alpha2
))
1995 printk(KERN_INFO
"cfg80211: Regulatory domain "
1996 "changed to driver built-in settings "
1997 "(unknown country)\n");
1999 printk(KERN_INFO
"cfg80211: Regulatory domain "
2000 "changed to country: %c%c\n",
2001 rd
->alpha2
[0], rd
->alpha2
[1]);
2006 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
2008 printk(KERN_INFO
"cfg80211: Regulatory domain: %c%c\n",
2009 rd
->alpha2
[0], rd
->alpha2
[1]);
2013 #ifdef CONFIG_CFG80211_REG_DEBUG
2014 static void reg_country_ie_process_debug(
2015 const struct ieee80211_regdomain
*rd
,
2016 const struct ieee80211_regdomain
*country_ie_regdomain
,
2017 const struct ieee80211_regdomain
*intersected_rd
)
2019 printk(KERN_DEBUG
"cfg80211: Received country IE:\n");
2020 print_regdomain_info(country_ie_regdomain
);
2021 printk(KERN_DEBUG
"cfg80211: CRDA thinks this should applied:\n");
2022 print_regdomain_info(rd
);
2023 if (intersected_rd
) {
2024 printk(KERN_DEBUG
"cfg80211: We intersect both of these "
2026 print_regdomain_info(intersected_rd
);
2029 printk(KERN_DEBUG
"cfg80211: Intersection between both failed\n");
2032 static inline void reg_country_ie_process_debug(
2033 const struct ieee80211_regdomain
*rd
,
2034 const struct ieee80211_regdomain
*country_ie_regdomain
,
2035 const struct ieee80211_regdomain
*intersected_rd
)
2040 /* Takes ownership of rd only if it doesn't fail */
2041 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
2043 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2044 struct cfg80211_registered_device
*rdev
= NULL
;
2045 struct wiphy
*request_wiphy
;
2046 /* Some basic sanity checks first */
2048 if (is_world_regdom(rd
->alpha2
)) {
2049 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2051 update_world_regdomain(rd
);
2055 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2056 !is_unknown_alpha2(rd
->alpha2
))
2063 * Lets only bother proceeding on the same alpha2 if the current
2064 * rd is non static (it means CRDA was present and was used last)
2065 * and the pending request came in from a country IE
2067 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2069 * If someone else asked us to change the rd lets only bother
2070 * checking if the alpha2 changes if CRDA was already called
2072 if (!is_old_static_regdom(cfg80211_regdomain
) &&
2073 !regdom_changes(rd
->alpha2
))
2078 * Now lets set the regulatory domain, update all driver channels
2079 * and finally inform them of what we have done, in case they want
2080 * to review or adjust their own settings based on their own
2081 * internal EEPROM data
2084 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2087 if (!is_valid_rd(rd
)) {
2088 printk(KERN_ERR
"cfg80211: Invalid "
2089 "regulatory domain detected:\n");
2090 print_regdomain_info(rd
);
2094 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2096 if (!last_request
->intersect
) {
2099 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_DRIVER
) {
2101 cfg80211_regdomain
= rd
;
2106 * For a driver hint, lets copy the regulatory domain the
2107 * driver wanted to the wiphy to deal with conflicts
2111 * Userspace could have sent two replies with only
2112 * one kernel request.
2114 if (request_wiphy
->regd
)
2117 r
= reg_copy_regd(&request_wiphy
->regd
, rd
);
2122 cfg80211_regdomain
= rd
;
2126 /* Intersection requires a bit more work */
2128 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2130 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
2131 if (!intersected_rd
)
2135 * We can trash what CRDA provided now.
2136 * However if a driver requested this specific regulatory
2137 * domain we keep it for its private use
2139 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
)
2140 request_wiphy
->regd
= rd
;
2147 cfg80211_regdomain
= intersected_rd
;
2153 * Country IE requests are handled a bit differently, we intersect
2154 * the country IE rd with what CRDA believes that country should have
2158 * Userspace could have sent two replies with only
2159 * one kernel request. By the second reply we would have
2160 * already processed and consumed the country_ie_regdomain.
2162 if (!country_ie_regdomain
)
2164 BUG_ON(rd
== country_ie_regdomain
);
2167 * Intersect what CRDA returned and our what we
2168 * had built from the Country IE received
2171 intersected_rd
= regdom_intersect(rd
, country_ie_regdomain
);
2173 reg_country_ie_process_debug(rd
,
2174 country_ie_regdomain
,
2177 kfree(country_ie_regdomain
);
2178 country_ie_regdomain
= NULL
;
2180 if (!intersected_rd
)
2183 rdev
= wiphy_to_dev(request_wiphy
);
2185 rdev
->country_ie_alpha2
[0] = rd
->alpha2
[0];
2186 rdev
->country_ie_alpha2
[1] = rd
->alpha2
[1];
2187 rdev
->env
= last_request
->country_ie_env
;
2189 BUG_ON(intersected_rd
== rd
);
2195 cfg80211_regdomain
= intersected_rd
;
2202 * Use this call to set the current regulatory domain. Conflicts with
2203 * multiple drivers can be ironed out later. Caller must've already
2204 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2206 int set_regdom(const struct ieee80211_regdomain
*rd
)
2210 assert_cfg80211_lock();
2212 mutex_lock(®_mutex
);
2214 /* Note that this doesn't update the wiphys, this is done below */
2215 r
= __set_regdom(rd
);
2218 mutex_unlock(®_mutex
);
2222 /* This would make this whole thing pointless */
2223 if (!last_request
->intersect
)
2224 BUG_ON(rd
!= cfg80211_regdomain
);
2226 /* update all wiphys now with the new established regulatory domain */
2227 update_all_wiphy_regulatory(last_request
->initiator
);
2229 print_regdomain(cfg80211_regdomain
);
2231 nl80211_send_reg_change_event(last_request
);
2233 mutex_unlock(®_mutex
);
2238 /* Caller must hold cfg80211_mutex */
2239 void reg_device_remove(struct wiphy
*wiphy
)
2241 struct wiphy
*request_wiphy
= NULL
;
2243 assert_cfg80211_lock();
2245 mutex_lock(®_mutex
);
2250 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2252 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2255 last_request
->wiphy_idx
= WIPHY_IDX_STALE
;
2256 last_request
->country_ie_env
= ENVIRON_ANY
;
2258 mutex_unlock(®_mutex
);
2261 int regulatory_init(void)
2265 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2266 if (IS_ERR(reg_pdev
))
2267 return PTR_ERR(reg_pdev
);
2269 spin_lock_init(®_requests_lock
);
2270 spin_lock_init(®_pending_beacons_lock
);
2272 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2273 cfg80211_regdomain
= static_regdom(ieee80211_regdom
);
2275 printk(KERN_INFO
"cfg80211: Using static regulatory domain info\n");
2276 print_regdomain_info(cfg80211_regdomain
);
2278 cfg80211_regdomain
= cfg80211_world_regdom
;
2281 /* We always try to get an update for the static regdomain */
2282 err
= regulatory_hint_core(cfg80211_regdomain
->alpha2
);
2287 * N.B. kobject_uevent_env() can fail mainly for when we're out
2288 * memory which is handled and propagated appropriately above
2289 * but it can also fail during a netlink_broadcast() or during
2290 * early boot for call_usermodehelper(). For now treat these
2291 * errors as non-fatal.
2293 printk(KERN_ERR
"cfg80211: kobject_uevent_env() was unable "
2294 "to call CRDA during init");
2295 #ifdef CONFIG_CFG80211_REG_DEBUG
2296 /* We want to find out exactly why when debugging */
2302 * Finally, if the user set the module parameter treat it
2305 if (!is_world_regdom(ieee80211_regdom
))
2306 regulatory_hint_user(ieee80211_regdom
);
2311 void regulatory_exit(void)
2313 struct regulatory_request
*reg_request
, *tmp
;
2314 struct reg_beacon
*reg_beacon
, *btmp
;
2316 cancel_work_sync(®_work
);
2318 mutex_lock(&cfg80211_mutex
);
2319 mutex_lock(®_mutex
);
2323 kfree(country_ie_regdomain
);
2324 country_ie_regdomain
= NULL
;
2326 kfree(last_request
);
2328 platform_device_unregister(reg_pdev
);
2330 spin_lock_bh(®_pending_beacons_lock
);
2331 if (!list_empty(®_pending_beacons
)) {
2332 list_for_each_entry_safe(reg_beacon
, btmp
,
2333 ®_pending_beacons
, list
) {
2334 list_del(®_beacon
->list
);
2338 spin_unlock_bh(®_pending_beacons_lock
);
2340 if (!list_empty(®_beacon_list
)) {
2341 list_for_each_entry_safe(reg_beacon
, btmp
,
2342 ®_beacon_list
, list
) {
2343 list_del(®_beacon
->list
);
2348 spin_lock(®_requests_lock
);
2349 if (!list_empty(®_requests_list
)) {
2350 list_for_each_entry_safe(reg_request
, tmp
,
2351 ®_requests_list
, list
) {
2352 list_del(®_request
->list
);
2356 spin_unlock(®_requests_lock
);
2358 mutex_unlock(®_mutex
);
2359 mutex_unlock(&cfg80211_mutex
);