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>
46 /* Receipt of information from last regulatory request */
47 static struct regulatory_request
*last_request
;
49 /* To trigger userspace events */
50 static struct platform_device
*reg_pdev
;
53 * Central wireless core regulatory domains, we only need two,
54 * the current one and a world regulatory domain in case we have no
55 * information to give us an alpha2
57 const struct ieee80211_regdomain
*cfg80211_regdomain
;
60 * We use this as a place for the rd structure built from the
61 * last parsed country IE to rest until CRDA gets back to us with
62 * what it thinks should apply for the same country
64 static const struct ieee80211_regdomain
*country_ie_regdomain
;
67 * Protects static reg.c components:
68 * - cfg80211_world_regdom
70 * - country_ie_regdomain
73 DEFINE_MUTEX(reg_mutex
);
74 #define assert_reg_lock() WARN_ON(!mutex_is_locked(®_mutex))
76 /* Used to queue up regulatory hints */
77 static LIST_HEAD(reg_requests_list
);
78 static spinlock_t reg_requests_lock
;
80 /* Used to queue up beacon hints for review */
81 static LIST_HEAD(reg_pending_beacons
);
82 static spinlock_t reg_pending_beacons_lock
;
84 /* Used to keep track of processed beacon hints */
85 static LIST_HEAD(reg_beacon_list
);
88 struct list_head list
;
89 struct ieee80211_channel chan
;
92 /* We keep a static world regulatory domain in case of the absence of CRDA */
93 static const struct ieee80211_regdomain world_regdom
= {
97 /* IEEE 802.11b/g, channels 1..11 */
98 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
99 /* IEEE 802.11b/g, channels 12..13. No HT40
100 * channel fits here. */
101 REG_RULE(2467-10, 2472+10, 20, 6, 20,
102 NL80211_RRF_PASSIVE_SCAN
|
103 NL80211_RRF_NO_IBSS
),
104 /* IEEE 802.11 channel 14 - Only JP enables
105 * this and for 802.11b only */
106 REG_RULE(2484-10, 2484+10, 20, 6, 20,
107 NL80211_RRF_PASSIVE_SCAN
|
108 NL80211_RRF_NO_IBSS
|
109 NL80211_RRF_NO_OFDM
),
110 /* IEEE 802.11a, channel 36..48 */
111 REG_RULE(5180-10, 5240+10, 40, 6, 20,
112 NL80211_RRF_PASSIVE_SCAN
|
113 NL80211_RRF_NO_IBSS
),
115 /* NB: 5260 MHz - 5700 MHz requies DFS */
117 /* IEEE 802.11a, channel 149..165 */
118 REG_RULE(5745-10, 5825+10, 40, 6, 20,
119 NL80211_RRF_PASSIVE_SCAN
|
120 NL80211_RRF_NO_IBSS
),
124 static const struct ieee80211_regdomain
*cfg80211_world_regdom
=
127 static char *ieee80211_regdom
= "00";
129 module_param(ieee80211_regdom
, charp
, 0444);
130 MODULE_PARM_DESC(ieee80211_regdom
, "IEEE 802.11 regulatory domain code");
132 static void reset_regdomains(void)
134 /* avoid freeing static information or freeing something twice */
135 if (cfg80211_regdomain
== cfg80211_world_regdom
)
136 cfg80211_regdomain
= NULL
;
137 if (cfg80211_world_regdom
== &world_regdom
)
138 cfg80211_world_regdom
= NULL
;
139 if (cfg80211_regdomain
== &world_regdom
)
140 cfg80211_regdomain
= NULL
;
142 kfree(cfg80211_regdomain
);
143 kfree(cfg80211_world_regdom
);
145 cfg80211_world_regdom
= &world_regdom
;
146 cfg80211_regdomain
= NULL
;
150 * Dynamic world regulatory domain requested by the wireless
151 * core upon initialization
153 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
155 BUG_ON(!last_request
);
159 cfg80211_world_regdom
= rd
;
160 cfg80211_regdomain
= rd
;
163 bool is_world_regdom(const char *alpha2
)
167 if (alpha2
[0] == '0' && alpha2
[1] == '0')
172 static bool is_alpha2_set(const char *alpha2
)
176 if (alpha2
[0] != 0 && alpha2
[1] != 0)
181 static bool is_alpha_upper(char letter
)
184 if (letter
>= 65 && letter
<= 90)
189 static bool is_unknown_alpha2(const char *alpha2
)
194 * Special case where regulatory domain was built by driver
195 * but a specific alpha2 cannot be determined
197 if (alpha2
[0] == '9' && alpha2
[1] == '9')
202 static bool is_intersected_alpha2(const char *alpha2
)
207 * Special case where regulatory domain is the
208 * result of an intersection between two regulatory domain
211 if (alpha2
[0] == '9' && alpha2
[1] == '8')
216 static bool is_an_alpha2(const char *alpha2
)
220 if (is_alpha_upper(alpha2
[0]) && is_alpha_upper(alpha2
[1]))
225 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
227 if (!alpha2_x
|| !alpha2_y
)
229 if (alpha2_x
[0] == alpha2_y
[0] &&
230 alpha2_x
[1] == alpha2_y
[1])
235 static bool regdom_changes(const char *alpha2
)
237 assert_cfg80211_lock();
239 if (!cfg80211_regdomain
)
241 if (alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
247 * country_ie_integrity_changes - tells us if the country IE has changed
248 * @checksum: checksum of country IE of fields we are interested in
250 * If the country IE has not changed you can ignore it safely. This is
251 * useful to determine if two devices are seeing two different country IEs
252 * even on the same alpha2. Note that this will return false if no IE has
253 * been set on the wireless core yet.
255 static bool country_ie_integrity_changes(u32 checksum
)
257 /* If no IE has been set then the checksum doesn't change */
258 if (unlikely(!last_request
->country_ie_checksum
))
260 if (unlikely(last_request
->country_ie_checksum
!= checksum
))
265 static int reg_copy_regd(const struct ieee80211_regdomain
**dst_regd
,
266 const struct ieee80211_regdomain
*src_regd
)
268 struct ieee80211_regdomain
*regd
;
269 int size_of_regd
= 0;
272 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
273 ((src_regd
->n_reg_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
275 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
279 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
281 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
282 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
283 sizeof(struct ieee80211_reg_rule
));
289 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
290 struct reg_regdb_search_request
{
292 struct list_head list
;
295 static LIST_HEAD(reg_regdb_search_list
);
296 static DEFINE_SPINLOCK(reg_regdb_search_lock
);
298 static void reg_regdb_search(struct work_struct
*work
)
300 struct reg_regdb_search_request
*request
;
301 const struct ieee80211_regdomain
*curdom
, *regdom
;
304 spin_lock(®_regdb_search_lock
);
305 while (!list_empty(®_regdb_search_list
)) {
306 request
= list_first_entry(®_regdb_search_list
,
307 struct reg_regdb_search_request
,
309 list_del(&request
->list
);
311 for (i
=0; i
<reg_regdb_size
; i
++) {
312 curdom
= reg_regdb
[i
];
314 if (!memcmp(request
->alpha2
, curdom
->alpha2
, 2)) {
315 r
= reg_copy_regd(®dom
, curdom
);
318 spin_unlock(®_regdb_search_lock
);
319 mutex_lock(&cfg80211_mutex
);
321 mutex_unlock(&cfg80211_mutex
);
322 spin_lock(®_regdb_search_lock
);
329 spin_unlock(®_regdb_search_lock
);
332 static DECLARE_WORK(reg_regdb_work
, reg_regdb_search
);
334 static void reg_regdb_query(const char *alpha2
)
336 struct reg_regdb_search_request
*request
;
341 request
= kzalloc(sizeof(struct reg_regdb_search_request
), GFP_KERNEL
);
345 memcpy(request
->alpha2
, alpha2
, 2);
347 spin_lock(®_regdb_search_lock
);
348 list_add_tail(&request
->list
, ®_regdb_search_list
);
349 spin_unlock(®_regdb_search_lock
);
351 schedule_work(®_regdb_work
);
354 static inline void reg_regdb_query(const char *alpha2
) {}
355 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
358 * This lets us keep regulatory code which is updated on a regulatory
359 * basis in userspace.
361 static int call_crda(const char *alpha2
)
363 char country_env
[9 + 2] = "COUNTRY=";
369 if (!is_world_regdom((char *) alpha2
))
370 printk(KERN_INFO
"cfg80211: Calling CRDA for country: %c%c\n",
371 alpha2
[0], alpha2
[1]);
373 printk(KERN_INFO
"cfg80211: Calling CRDA to update world "
374 "regulatory domain\n");
376 /* query internal regulatory database (if it exists) */
377 reg_regdb_query(alpha2
);
379 country_env
[8] = alpha2
[0];
380 country_env
[9] = alpha2
[1];
382 return kobject_uevent_env(®_pdev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
385 /* Used by nl80211 before kmalloc'ing our regulatory domain */
386 bool reg_is_valid_request(const char *alpha2
)
388 assert_cfg80211_lock();
393 return alpha2_equal(last_request
->alpha2
, alpha2
);
396 /* Sanity check on a regulatory rule */
397 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
399 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
402 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
405 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
408 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
410 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
411 freq_range
->max_bandwidth_khz
> freq_diff
)
417 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
419 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
422 if (!rd
->n_reg_rules
)
425 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
428 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
429 reg_rule
= &rd
->reg_rules
[i
];
430 if (!is_valid_reg_rule(reg_rule
))
437 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
441 u32 start_freq_khz
, end_freq_khz
;
443 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
444 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
446 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
447 end_freq_khz
<= freq_range
->end_freq_khz
)
454 * freq_in_rule_band - tells us if a frequency is in a frequency band
455 * @freq_range: frequency rule we want to query
456 * @freq_khz: frequency we are inquiring about
458 * This lets us know if a specific frequency rule is or is not relevant to
459 * a specific frequency's band. Bands are device specific and artificial
460 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
461 * safe for now to assume that a frequency rule should not be part of a
462 * frequency's band if the start freq or end freq are off by more than 2 GHz.
463 * This resolution can be lowered and should be considered as we add
464 * regulatory rule support for other "bands".
466 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
469 #define ONE_GHZ_IN_KHZ 1000000
470 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
472 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
475 #undef ONE_GHZ_IN_KHZ
479 * Converts a country IE to a regulatory domain. A regulatory domain
480 * structure has a lot of information which the IE doesn't yet have,
481 * so for the other values we use upper max values as we will intersect
482 * with our userspace regulatory agent to get lower bounds.
484 static struct ieee80211_regdomain
*country_ie_2_rd(
489 struct ieee80211_regdomain
*rd
= NULL
;
493 u32 num_rules
= 0, size_of_regd
= 0;
494 u8
*triplets_start
= NULL
;
495 u8 len_at_triplet
= 0;
496 /* the last channel we have registered in a subband (triplet) */
497 int last_sub_max_channel
= 0;
499 *checksum
= 0xDEADBEEF;
501 /* Country IE requirements */
502 BUG_ON(country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
||
503 country_ie_len
& 0x01);
505 alpha2
[0] = country_ie
[0];
506 alpha2
[1] = country_ie
[1];
509 * Third octet can be:
513 * anything else we assume is no restrictions
515 if (country_ie
[2] == 'I')
516 flags
= NL80211_RRF_NO_OUTDOOR
;
517 else if (country_ie
[2] == 'O')
518 flags
= NL80211_RRF_NO_INDOOR
;
523 triplets_start
= country_ie
;
524 len_at_triplet
= country_ie_len
;
526 *checksum
^= ((flags
^ alpha2
[0] ^ alpha2
[1]) << 8);
529 * We need to build a reg rule for each triplet, but first we must
530 * calculate the number of reg rules we will need. We will need one
531 * for each channel subband
533 while (country_ie_len
>= 3) {
535 struct ieee80211_country_ie_triplet
*triplet
=
536 (struct ieee80211_country_ie_triplet
*) country_ie
;
537 int cur_sub_max_channel
= 0, cur_channel
= 0;
539 if (triplet
->ext
.reg_extension_id
>=
540 IEEE80211_COUNTRY_EXTENSION_ID
) {
547 if (triplet
->chans
.first_channel
<= 14)
548 end_channel
= triplet
->chans
.first_channel
+
549 triplet
->chans
.num_channels
;
552 * 5 GHz -- For example in country IEs if the first
553 * channel given is 36 and the number of channels is 4
554 * then the individual channel numbers defined for the
555 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
556 * and not 36, 37, 38, 39.
558 * See: http://tinyurl.com/11d-clarification
560 end_channel
= triplet
->chans
.first_channel
+
561 (4 * (triplet
->chans
.num_channels
- 1));
563 cur_channel
= triplet
->chans
.first_channel
;
564 cur_sub_max_channel
= end_channel
;
566 /* Basic sanity check */
567 if (cur_sub_max_channel
< cur_channel
)
571 * Do not allow overlapping channels. Also channels
572 * passed in each subband must be monotonically
575 if (last_sub_max_channel
) {
576 if (cur_channel
<= last_sub_max_channel
)
578 if (cur_sub_max_channel
<= last_sub_max_channel
)
583 * When dot11RegulatoryClassesRequired is supported
584 * we can throw ext triplets as part of this soup,
585 * for now we don't care when those change as we
588 *checksum
^= ((cur_channel
^ cur_sub_max_channel
) << 8) |
589 ((cur_sub_max_channel
^ cur_sub_max_channel
) << 16) |
590 ((triplet
->chans
.max_power
^ cur_sub_max_channel
) << 24);
592 last_sub_max_channel
= cur_sub_max_channel
;
599 * Note: this is not a IEEE requirement but
600 * simply a memory requirement
602 if (num_rules
> NL80211_MAX_SUPP_REG_RULES
)
606 country_ie
= triplets_start
;
607 country_ie_len
= len_at_triplet
;
609 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
610 (num_rules
* sizeof(struct ieee80211_reg_rule
));
612 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
616 rd
->n_reg_rules
= num_rules
;
617 rd
->alpha2
[0] = alpha2
[0];
618 rd
->alpha2
[1] = alpha2
[1];
620 /* This time around we fill in the rd */
621 while (country_ie_len
>= 3) {
623 struct ieee80211_country_ie_triplet
*triplet
=
624 (struct ieee80211_country_ie_triplet
*) country_ie
;
625 struct ieee80211_reg_rule
*reg_rule
= NULL
;
626 struct ieee80211_freq_range
*freq_range
= NULL
;
627 struct ieee80211_power_rule
*power_rule
= NULL
;
630 * Must parse if dot11RegulatoryClassesRequired is true,
631 * we don't support this yet
633 if (triplet
->ext
.reg_extension_id
>=
634 IEEE80211_COUNTRY_EXTENSION_ID
) {
640 reg_rule
= &rd
->reg_rules
[i
];
641 freq_range
= ®_rule
->freq_range
;
642 power_rule
= ®_rule
->power_rule
;
644 reg_rule
->flags
= flags
;
647 if (triplet
->chans
.first_channel
<= 14)
648 end_channel
= triplet
->chans
.first_channel
+
649 triplet
->chans
.num_channels
;
651 end_channel
= triplet
->chans
.first_channel
+
652 (4 * (triplet
->chans
.num_channels
- 1));
655 * The +10 is since the regulatory domain expects
656 * the actual band edge, not the center of freq for
657 * its start and end freqs, assuming 20 MHz bandwidth on
658 * the channels passed
660 freq_range
->start_freq_khz
=
661 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
662 triplet
->chans
.first_channel
) - 10);
663 freq_range
->end_freq_khz
=
664 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
668 * These are large arbitrary values we use to intersect later.
669 * Increment this if we ever support >= 40 MHz channels
672 freq_range
->max_bandwidth_khz
= MHZ_TO_KHZ(40);
673 power_rule
->max_antenna_gain
= DBI_TO_MBI(100);
674 power_rule
->max_eirp
= DBM_TO_MBM(100);
680 BUG_ON(i
> NL80211_MAX_SUPP_REG_RULES
);
688 * Helper for regdom_intersect(), this does the real
689 * mathematical intersection fun
691 static int reg_rules_intersect(
692 const struct ieee80211_reg_rule
*rule1
,
693 const struct ieee80211_reg_rule
*rule2
,
694 struct ieee80211_reg_rule
*intersected_rule
)
696 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
697 struct ieee80211_freq_range
*freq_range
;
698 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
699 struct ieee80211_power_rule
*power_rule
;
702 freq_range1
= &rule1
->freq_range
;
703 freq_range2
= &rule2
->freq_range
;
704 freq_range
= &intersected_rule
->freq_range
;
706 power_rule1
= &rule1
->power_rule
;
707 power_rule2
= &rule2
->power_rule
;
708 power_rule
= &intersected_rule
->power_rule
;
710 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
711 freq_range2
->start_freq_khz
);
712 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
713 freq_range2
->end_freq_khz
);
714 freq_range
->max_bandwidth_khz
= min(freq_range1
->max_bandwidth_khz
,
715 freq_range2
->max_bandwidth_khz
);
717 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
718 if (freq_range
->max_bandwidth_khz
> freq_diff
)
719 freq_range
->max_bandwidth_khz
= freq_diff
;
721 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
722 power_rule2
->max_eirp
);
723 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
724 power_rule2
->max_antenna_gain
);
726 intersected_rule
->flags
= (rule1
->flags
| rule2
->flags
);
728 if (!is_valid_reg_rule(intersected_rule
))
735 * regdom_intersect - do the intersection between two regulatory domains
736 * @rd1: first regulatory domain
737 * @rd2: second regulatory domain
739 * Use this function to get the intersection between two regulatory domains.
740 * Once completed we will mark the alpha2 for the rd as intersected, "98",
741 * as no one single alpha2 can represent this regulatory domain.
743 * Returns a pointer to the regulatory domain structure which will hold the
744 * resulting intersection of rules between rd1 and rd2. We will
745 * kzalloc() this structure for you.
747 static struct ieee80211_regdomain
*regdom_intersect(
748 const struct ieee80211_regdomain
*rd1
,
749 const struct ieee80211_regdomain
*rd2
)
753 unsigned int num_rules
= 0, rule_idx
= 0;
754 const struct ieee80211_reg_rule
*rule1
, *rule2
;
755 struct ieee80211_reg_rule
*intersected_rule
;
756 struct ieee80211_regdomain
*rd
;
757 /* This is just a dummy holder to help us count */
758 struct ieee80211_reg_rule irule
;
760 /* Uses the stack temporarily for counter arithmetic */
761 intersected_rule
= &irule
;
763 memset(intersected_rule
, 0, sizeof(struct ieee80211_reg_rule
));
769 * First we get a count of the rules we'll need, then we actually
770 * build them. This is to so we can malloc() and free() a
771 * regdomain once. The reason we use reg_rules_intersect() here
772 * is it will return -EINVAL if the rule computed makes no sense.
773 * All rules that do check out OK are valid.
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
];
780 if (!reg_rules_intersect(rule1
, rule2
,
783 memset(intersected_rule
, 0,
784 sizeof(struct ieee80211_reg_rule
));
791 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
792 ((num_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
794 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
798 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
799 rule1
= &rd1
->reg_rules
[x
];
800 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
801 rule2
= &rd2
->reg_rules
[y
];
803 * This time around instead of using the stack lets
804 * write to the target rule directly saving ourselves
807 intersected_rule
= &rd
->reg_rules
[rule_idx
];
808 r
= reg_rules_intersect(rule1
, rule2
,
811 * No need to memset here the intersected rule here as
812 * we're not using the stack anymore
820 if (rule_idx
!= num_rules
) {
825 rd
->n_reg_rules
= num_rules
;
833 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
834 * want to just have the channel structure use these
836 static u32
map_regdom_flags(u32 rd_flags
)
838 u32 channel_flags
= 0;
839 if (rd_flags
& NL80211_RRF_PASSIVE_SCAN
)
840 channel_flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
841 if (rd_flags
& NL80211_RRF_NO_IBSS
)
842 channel_flags
|= IEEE80211_CHAN_NO_IBSS
;
843 if (rd_flags
& NL80211_RRF_DFS
)
844 channel_flags
|= IEEE80211_CHAN_RADAR
;
845 return channel_flags
;
848 static int freq_reg_info_regd(struct wiphy
*wiphy
,
851 const struct ieee80211_reg_rule
**reg_rule
,
852 const struct ieee80211_regdomain
*custom_regd
)
855 bool band_rule_found
= false;
856 const struct ieee80211_regdomain
*regd
;
857 bool bw_fits
= false;
860 desired_bw_khz
= MHZ_TO_KHZ(20);
862 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
865 * Follow the driver's regulatory domain, if present, unless a country
866 * IE has been processed or a user wants to help complaince further
868 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
869 last_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
876 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
877 const struct ieee80211_reg_rule
*rr
;
878 const struct ieee80211_freq_range
*fr
= NULL
;
879 const struct ieee80211_power_rule
*pr
= NULL
;
881 rr
= ®d
->reg_rules
[i
];
882 fr
= &rr
->freq_range
;
883 pr
= &rr
->power_rule
;
886 * We only need to know if one frequency rule was
887 * was in center_freq's band, that's enough, so lets
888 * not overwrite it once found
890 if (!band_rule_found
)
891 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
893 bw_fits
= reg_does_bw_fit(fr
,
897 if (band_rule_found
&& bw_fits
) {
903 if (!band_rule_found
)
908 EXPORT_SYMBOL(freq_reg_info
);
910 int freq_reg_info(struct wiphy
*wiphy
,
913 const struct ieee80211_reg_rule
**reg_rule
)
915 assert_cfg80211_lock();
916 return freq_reg_info_regd(wiphy
,
924 * Note that right now we assume the desired channel bandwidth
925 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
926 * per channel, the primary and the extension channel). To support
927 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
928 * new ieee80211_channel.target_bw and re run the regulatory check
929 * on the wiphy with the target_bw specified. Then we can simply use
930 * that below for the desired_bw_khz below.
932 static void handle_channel(struct wiphy
*wiphy
, enum ieee80211_band band
,
933 unsigned int chan_idx
)
936 u32 flags
, bw_flags
= 0;
937 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
938 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
939 const struct ieee80211_power_rule
*power_rule
= NULL
;
940 const struct ieee80211_freq_range
*freq_range
= NULL
;
941 struct ieee80211_supported_band
*sband
;
942 struct ieee80211_channel
*chan
;
943 struct wiphy
*request_wiphy
= NULL
;
945 assert_cfg80211_lock();
947 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
949 sband
= wiphy
->bands
[band
];
950 BUG_ON(chan_idx
>= sband
->n_channels
);
951 chan
= &sband
->channels
[chan_idx
];
953 flags
= chan
->orig_flags
;
955 r
= freq_reg_info(wiphy
,
956 MHZ_TO_KHZ(chan
->center_freq
),
962 * This means no regulatory rule was found in the country IE
963 * with a frequency range on the center_freq's band, since
964 * IEEE-802.11 allows for a country IE to have a subset of the
965 * regulatory information provided in a country we ignore
966 * disabling the channel unless at least one reg rule was
967 * found on the center_freq's band. For details see this
970 * http://tinyurl.com/11d-clarification
973 last_request
->initiator
==
974 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
975 #ifdef CONFIG_CFG80211_REG_DEBUG
976 printk(KERN_DEBUG
"cfg80211: Leaving channel %d MHz "
977 "intact on %s - no rule found in band on "
979 chan
->center_freq
, wiphy_name(wiphy
));
983 * In this case we know the country IE has at least one reg rule
984 * for the band so we respect its band definitions
986 #ifdef CONFIG_CFG80211_REG_DEBUG
987 if (last_request
->initiator
==
988 NL80211_REGDOM_SET_BY_COUNTRY_IE
)
989 printk(KERN_DEBUG
"cfg80211: Disabling "
990 "channel %d MHz on %s due to "
992 chan
->center_freq
, wiphy_name(wiphy
));
994 flags
|= IEEE80211_CHAN_DISABLED
;
1000 power_rule
= ®_rule
->power_rule
;
1001 freq_range
= ®_rule
->freq_range
;
1003 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1004 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1006 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1007 request_wiphy
&& request_wiphy
== wiphy
&&
1008 request_wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
) {
1010 * This gaurantees the driver's requested regulatory domain
1011 * will always be used as a base for further regulatory
1014 chan
->flags
= chan
->orig_flags
=
1015 map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1016 chan
->max_antenna_gain
= chan
->orig_mag
=
1017 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1018 chan
->max_power
= chan
->orig_mpwr
=
1019 (int) MBM_TO_DBM(power_rule
->max_eirp
);
1023 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
1024 chan
->max_antenna_gain
= min(chan
->orig_mag
,
1025 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
1026 if (chan
->orig_mpwr
)
1027 chan
->max_power
= min(chan
->orig_mpwr
,
1028 (int) MBM_TO_DBM(power_rule
->max_eirp
));
1030 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1033 static void handle_band(struct wiphy
*wiphy
, enum ieee80211_band band
)
1036 struct ieee80211_supported_band
*sband
;
1038 BUG_ON(!wiphy
->bands
[band
]);
1039 sband
= wiphy
->bands
[band
];
1041 for (i
= 0; i
< sband
->n_channels
; i
++)
1042 handle_channel(wiphy
, band
, i
);
1045 static bool ignore_reg_update(struct wiphy
*wiphy
,
1046 enum nl80211_reg_initiator initiator
)
1050 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
1051 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1054 * wiphy->regd will be set once the device has its own
1055 * desired regulatory domain set
1057 if (wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
&& !wiphy
->regd
&&
1058 !is_world_regdom(last_request
->alpha2
))
1063 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
1065 struct cfg80211_registered_device
*rdev
;
1067 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1068 wiphy_update_regulatory(&rdev
->wiphy
, initiator
);
1071 static void handle_reg_beacon(struct wiphy
*wiphy
,
1072 unsigned int chan_idx
,
1073 struct reg_beacon
*reg_beacon
)
1075 struct ieee80211_supported_band
*sband
;
1076 struct ieee80211_channel
*chan
;
1077 bool channel_changed
= false;
1078 struct ieee80211_channel chan_before
;
1080 assert_cfg80211_lock();
1082 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1083 chan
= &sband
->channels
[chan_idx
];
1085 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
1088 if (chan
->beacon_found
)
1091 chan
->beacon_found
= true;
1093 if (wiphy
->flags
& WIPHY_FLAG_DISABLE_BEACON_HINTS
)
1096 chan_before
.center_freq
= chan
->center_freq
;
1097 chan_before
.flags
= chan
->flags
;
1099 if (chan
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
1100 chan
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
1101 channel_changed
= true;
1104 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
) {
1105 chan
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
1106 channel_changed
= true;
1109 if (channel_changed
)
1110 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
1114 * Called when a scan on a wiphy finds a beacon on
1117 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
1118 struct reg_beacon
*reg_beacon
)
1121 struct ieee80211_supported_band
*sband
;
1123 assert_cfg80211_lock();
1125 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1128 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 * Called upon reg changes or a new wiphy is added
1137 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1140 struct ieee80211_supported_band
*sband
;
1141 struct reg_beacon
*reg_beacon
;
1143 assert_cfg80211_lock();
1145 if (list_empty(®_beacon_list
))
1148 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1149 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1151 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1152 for (i
= 0; i
< sband
->n_channels
; i
++)
1153 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1157 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1159 if (is_world_regdom(cfg80211_regdomain
->alpha2
) ||
1160 (wiphy
->regd
&& is_world_regdom(wiphy
->regd
->alpha2
)))
1163 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1164 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1169 /* Reap the advantages of previously found beacons */
1170 static void reg_process_beacons(struct wiphy
*wiphy
)
1173 * Means we are just firing up cfg80211, so no beacons would
1174 * have been processed yet.
1178 if (!reg_is_world_roaming(wiphy
))
1180 wiphy_update_beacon_reg(wiphy
);
1183 static bool is_ht40_not_allowed(struct ieee80211_channel
*chan
)
1187 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1189 /* This would happen when regulatory rules disallow HT40 completely */
1190 if (IEEE80211_CHAN_NO_HT40
== (chan
->flags
& (IEEE80211_CHAN_NO_HT40
)))
1195 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1196 enum ieee80211_band band
,
1197 unsigned int chan_idx
)
1199 struct ieee80211_supported_band
*sband
;
1200 struct ieee80211_channel
*channel
;
1201 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1204 assert_cfg80211_lock();
1206 sband
= wiphy
->bands
[band
];
1207 BUG_ON(chan_idx
>= sband
->n_channels
);
1208 channel
= &sband
->channels
[chan_idx
];
1210 if (is_ht40_not_allowed(channel
)) {
1211 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1216 * We need to ensure the extension channels exist to
1217 * be able to use HT40- or HT40+, this finds them (or not)
1219 for (i
= 0; i
< sband
->n_channels
; i
++) {
1220 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1221 if (c
->center_freq
== (channel
->center_freq
- 20))
1223 if (c
->center_freq
== (channel
->center_freq
+ 20))
1228 * Please note that this assumes target bandwidth is 20 MHz,
1229 * if that ever changes we also need to change the below logic
1230 * to include that as well.
1232 if (is_ht40_not_allowed(channel_before
))
1233 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1235 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1237 if (is_ht40_not_allowed(channel_after
))
1238 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1240 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1243 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1244 enum ieee80211_band band
)
1247 struct ieee80211_supported_band
*sband
;
1249 BUG_ON(!wiphy
->bands
[band
]);
1250 sband
= wiphy
->bands
[band
];
1252 for (i
= 0; i
< sband
->n_channels
; i
++)
1253 reg_process_ht_flags_channel(wiphy
, band
, i
);
1256 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1258 enum ieee80211_band band
;
1263 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1264 if (wiphy
->bands
[band
])
1265 reg_process_ht_flags_band(wiphy
, band
);
1270 void wiphy_update_regulatory(struct wiphy
*wiphy
,
1271 enum nl80211_reg_initiator initiator
)
1273 enum ieee80211_band band
;
1275 if (ignore_reg_update(wiphy
, initiator
))
1277 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1278 if (wiphy
->bands
[band
])
1279 handle_band(wiphy
, band
);
1282 reg_process_beacons(wiphy
);
1283 reg_process_ht_flags(wiphy
);
1284 if (wiphy
->reg_notifier
)
1285 wiphy
->reg_notifier(wiphy
, last_request
);
1288 static void handle_channel_custom(struct wiphy
*wiphy
,
1289 enum ieee80211_band band
,
1290 unsigned int chan_idx
,
1291 const struct ieee80211_regdomain
*regd
)
1294 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
1296 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1297 const struct ieee80211_power_rule
*power_rule
= NULL
;
1298 const struct ieee80211_freq_range
*freq_range
= NULL
;
1299 struct ieee80211_supported_band
*sband
;
1300 struct ieee80211_channel
*chan
;
1304 sband
= wiphy
->bands
[band
];
1305 BUG_ON(chan_idx
>= sband
->n_channels
);
1306 chan
= &sband
->channels
[chan_idx
];
1308 r
= freq_reg_info_regd(wiphy
,
1309 MHZ_TO_KHZ(chan
->center_freq
),
1315 chan
->flags
= IEEE80211_CHAN_DISABLED
;
1319 power_rule
= ®_rule
->power_rule
;
1320 freq_range
= ®_rule
->freq_range
;
1322 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1323 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1325 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1326 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1327 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1330 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
1331 const struct ieee80211_regdomain
*regd
)
1334 struct ieee80211_supported_band
*sband
;
1336 BUG_ON(!wiphy
->bands
[band
]);
1337 sband
= wiphy
->bands
[band
];
1339 for (i
= 0; i
< sband
->n_channels
; i
++)
1340 handle_channel_custom(wiphy
, band
, i
, regd
);
1343 /* Used by drivers prior to wiphy registration */
1344 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1345 const struct ieee80211_regdomain
*regd
)
1347 enum ieee80211_band band
;
1348 unsigned int bands_set
= 0;
1350 mutex_lock(®_mutex
);
1351 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1352 if (!wiphy
->bands
[band
])
1354 handle_band_custom(wiphy
, band
, regd
);
1357 mutex_unlock(®_mutex
);
1360 * no point in calling this if it won't have any effect
1361 * on your device's supportd bands.
1363 WARN_ON(!bands_set
);
1365 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1368 * Return value which can be used by ignore_request() to indicate
1369 * it has been determined we should intersect two regulatory domains
1371 #define REG_INTERSECT 1
1373 /* This has the logic which determines when a new request
1374 * should be ignored. */
1375 static int ignore_request(struct wiphy
*wiphy
,
1376 struct regulatory_request
*pending_request
)
1378 struct wiphy
*last_wiphy
= NULL
;
1380 assert_cfg80211_lock();
1382 /* All initial requests are respected */
1386 switch (pending_request
->initiator
) {
1387 case NL80211_REGDOM_SET_BY_CORE
:
1389 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1391 last_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1393 if (unlikely(!is_an_alpha2(pending_request
->alpha2
)))
1395 if (last_request
->initiator
==
1396 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1397 if (last_wiphy
!= wiphy
) {
1399 * Two cards with two APs claiming different
1400 * Country IE alpha2s. We could
1401 * intersect them, but that seems unlikely
1402 * to be correct. Reject second one for now.
1404 if (regdom_changes(pending_request
->alpha2
))
1409 * Two consecutive Country IE hints on the same wiphy.
1410 * This should be picked up early by the driver/stack
1412 if (WARN_ON(regdom_changes(pending_request
->alpha2
)))
1416 return REG_INTERSECT
;
1417 case NL80211_REGDOM_SET_BY_DRIVER
:
1418 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1419 if (regdom_changes(pending_request
->alpha2
))
1425 * This would happen if you unplug and plug your card
1426 * back in or if you add a new device for which the previously
1427 * loaded card also agrees on the regulatory domain.
1429 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1430 !regdom_changes(pending_request
->alpha2
))
1433 return REG_INTERSECT
;
1434 case NL80211_REGDOM_SET_BY_USER
:
1435 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1436 return REG_INTERSECT
;
1438 * If the user knows better the user should set the regdom
1439 * to their country before the IE is picked up
1441 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1442 last_request
->intersect
)
1445 * Process user requests only after previous user/driver/core
1446 * requests have been processed
1448 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1449 last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1450 last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1451 if (regdom_changes(last_request
->alpha2
))
1455 if (!regdom_changes(pending_request
->alpha2
))
1465 * __regulatory_hint - hint to the wireless core a regulatory domain
1466 * @wiphy: if the hint comes from country information from an AP, this
1467 * is required to be set to the wiphy that received the information
1468 * @pending_request: the regulatory request currently being processed
1470 * The Wireless subsystem can use this function to hint to the wireless core
1471 * what it believes should be the current regulatory domain.
1473 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1474 * already been set or other standard error codes.
1476 * Caller must hold &cfg80211_mutex and ®_mutex
1478 static int __regulatory_hint(struct wiphy
*wiphy
,
1479 struct regulatory_request
*pending_request
)
1481 bool intersect
= false;
1484 assert_cfg80211_lock();
1486 r
= ignore_request(wiphy
, pending_request
);
1488 if (r
== REG_INTERSECT
) {
1489 if (pending_request
->initiator
==
1490 NL80211_REGDOM_SET_BY_DRIVER
) {
1491 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1493 kfree(pending_request
);
1500 * If the regulatory domain being requested by the
1501 * driver has already been set just copy it to the
1504 if (r
== -EALREADY
&&
1505 pending_request
->initiator
==
1506 NL80211_REGDOM_SET_BY_DRIVER
) {
1507 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1509 kfree(pending_request
);
1515 kfree(pending_request
);
1520 kfree(last_request
);
1522 last_request
= pending_request
;
1523 last_request
->intersect
= intersect
;
1525 pending_request
= NULL
;
1527 /* When r == REG_INTERSECT we do need to call CRDA */
1530 * Since CRDA will not be called in this case as we already
1531 * have applied the requested regulatory domain before we just
1532 * inform userspace we have processed the request
1535 nl80211_send_reg_change_event(last_request
);
1539 return call_crda(last_request
->alpha2
);
1542 /* This processes *all* regulatory hints */
1543 static void reg_process_hint(struct regulatory_request
*reg_request
)
1546 struct wiphy
*wiphy
= NULL
;
1548 BUG_ON(!reg_request
->alpha2
);
1550 mutex_lock(&cfg80211_mutex
);
1551 mutex_lock(®_mutex
);
1553 if (wiphy_idx_valid(reg_request
->wiphy_idx
))
1554 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1556 if (reg_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1562 r
= __regulatory_hint(wiphy
, reg_request
);
1563 /* This is required so that the orig_* parameters are saved */
1564 if (r
== -EALREADY
&& wiphy
&&
1565 wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
)
1566 wiphy_update_regulatory(wiphy
, reg_request
->initiator
);
1568 mutex_unlock(®_mutex
);
1569 mutex_unlock(&cfg80211_mutex
);
1572 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1573 static void reg_process_pending_hints(void)
1575 struct regulatory_request
*reg_request
;
1577 spin_lock(®_requests_lock
);
1578 while (!list_empty(®_requests_list
)) {
1579 reg_request
= list_first_entry(®_requests_list
,
1580 struct regulatory_request
,
1582 list_del_init(®_request
->list
);
1584 spin_unlock(®_requests_lock
);
1585 reg_process_hint(reg_request
);
1586 spin_lock(®_requests_lock
);
1588 spin_unlock(®_requests_lock
);
1591 /* Processes beacon hints -- this has nothing to do with country IEs */
1592 static void reg_process_pending_beacon_hints(void)
1594 struct cfg80211_registered_device
*rdev
;
1595 struct reg_beacon
*pending_beacon
, *tmp
;
1598 * No need to hold the reg_mutex here as we just touch wiphys
1599 * and do not read or access regulatory variables.
1601 mutex_lock(&cfg80211_mutex
);
1603 /* This goes through the _pending_ beacon list */
1604 spin_lock_bh(®_pending_beacons_lock
);
1606 if (list_empty(®_pending_beacons
)) {
1607 spin_unlock_bh(®_pending_beacons_lock
);
1611 list_for_each_entry_safe(pending_beacon
, tmp
,
1612 ®_pending_beacons
, list
) {
1614 list_del_init(&pending_beacon
->list
);
1616 /* Applies the beacon hint to current wiphys */
1617 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1618 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1620 /* Remembers the beacon hint for new wiphys or reg changes */
1621 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1624 spin_unlock_bh(®_pending_beacons_lock
);
1626 mutex_unlock(&cfg80211_mutex
);
1629 static void reg_todo(struct work_struct
*work
)
1631 reg_process_pending_hints();
1632 reg_process_pending_beacon_hints();
1635 static DECLARE_WORK(reg_work
, reg_todo
);
1637 static void queue_regulatory_request(struct regulatory_request
*request
)
1639 spin_lock(®_requests_lock
);
1640 list_add_tail(&request
->list
, ®_requests_list
);
1641 spin_unlock(®_requests_lock
);
1643 schedule_work(®_work
);
1646 /* Core regulatory hint -- happens once during cfg80211_init() */
1647 static int regulatory_hint_core(const char *alpha2
)
1649 struct regulatory_request
*request
;
1651 BUG_ON(last_request
);
1653 request
= kzalloc(sizeof(struct regulatory_request
),
1658 request
->alpha2
[0] = alpha2
[0];
1659 request
->alpha2
[1] = alpha2
[1];
1660 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1662 queue_regulatory_request(request
);
1665 * This ensures last_request is populated once modules
1666 * come swinging in and calling regulatory hints and
1667 * wiphy_apply_custom_regulatory().
1669 flush_scheduled_work();
1675 int regulatory_hint_user(const char *alpha2
)
1677 struct regulatory_request
*request
;
1681 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1685 request
->wiphy_idx
= WIPHY_IDX_STALE
;
1686 request
->alpha2
[0] = alpha2
[0];
1687 request
->alpha2
[1] = alpha2
[1];
1688 request
->initiator
= NL80211_REGDOM_SET_BY_USER
,
1690 queue_regulatory_request(request
);
1696 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1698 struct regulatory_request
*request
;
1703 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1707 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1709 /* Must have registered wiphy first */
1710 BUG_ON(!wiphy_idx_valid(request
->wiphy_idx
));
1712 request
->alpha2
[0] = alpha2
[0];
1713 request
->alpha2
[1] = alpha2
[1];
1714 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
1716 queue_regulatory_request(request
);
1720 EXPORT_SYMBOL(regulatory_hint
);
1722 /* Caller must hold reg_mutex */
1723 static bool reg_same_country_ie_hint(struct wiphy
*wiphy
,
1724 u32 country_ie_checksum
)
1726 struct wiphy
*request_wiphy
;
1730 if (unlikely(last_request
->initiator
!=
1731 NL80211_REGDOM_SET_BY_COUNTRY_IE
))
1734 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1739 if (likely(request_wiphy
!= wiphy
))
1740 return !country_ie_integrity_changes(country_ie_checksum
);
1742 * We should not have let these through at this point, they
1743 * should have been picked up earlier by the first alpha2 check
1746 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum
)))
1752 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1753 * therefore cannot iterate over the rdev list here.
1755 void regulatory_hint_11d(struct wiphy
*wiphy
,
1759 struct ieee80211_regdomain
*rd
= NULL
;
1762 enum environment_cap env
= ENVIRON_ANY
;
1763 struct regulatory_request
*request
;
1765 mutex_lock(®_mutex
);
1767 if (unlikely(!last_request
))
1770 /* IE len must be evenly divisible by 2 */
1771 if (country_ie_len
& 0x01)
1774 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
1778 * Pending country IE processing, this can happen after we
1779 * call CRDA and wait for a response if a beacon was received before
1780 * we were able to process the last regulatory_hint_11d() call
1782 if (country_ie_regdomain
)
1785 alpha2
[0] = country_ie
[0];
1786 alpha2
[1] = country_ie
[1];
1788 if (country_ie
[2] == 'I')
1789 env
= ENVIRON_INDOOR
;
1790 else if (country_ie
[2] == 'O')
1791 env
= ENVIRON_OUTDOOR
;
1794 * We will run this only upon a successful connection on cfg80211.
1795 * We leave conflict resolution to the workqueue, where can hold
1798 if (likely(last_request
->initiator
==
1799 NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1800 wiphy_idx_valid(last_request
->wiphy_idx
)))
1803 rd
= country_ie_2_rd(country_ie
, country_ie_len
, &checksum
);
1808 * This will not happen right now but we leave it here for the
1809 * the future when we want to add suspend/resume support and having
1810 * the user move to another country after doing so, or having the user
1811 * move to another AP. Right now we just trust the first AP.
1813 * If we hit this before we add this support we want to be informed of
1814 * it as it would indicate a mistake in the current design
1816 if (WARN_ON(reg_same_country_ie_hint(wiphy
, checksum
)))
1819 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1824 * We keep this around for when CRDA comes back with a response so
1825 * we can intersect with that
1827 country_ie_regdomain
= rd
;
1829 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1830 request
->alpha2
[0] = rd
->alpha2
[0];
1831 request
->alpha2
[1] = rd
->alpha2
[1];
1832 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
1833 request
->country_ie_checksum
= checksum
;
1834 request
->country_ie_env
= env
;
1836 mutex_unlock(®_mutex
);
1838 queue_regulatory_request(request
);
1845 mutex_unlock(®_mutex
);
1848 static bool freq_is_chan_12_13_14(u16 freq
)
1850 if (freq
== ieee80211_channel_to_frequency(12) ||
1851 freq
== ieee80211_channel_to_frequency(13) ||
1852 freq
== ieee80211_channel_to_frequency(14))
1857 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
1858 struct ieee80211_channel
*beacon_chan
,
1861 struct reg_beacon
*reg_beacon
;
1863 if (likely((beacon_chan
->beacon_found
||
1864 (beacon_chan
->flags
& IEEE80211_CHAN_RADAR
) ||
1865 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
1866 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))))
1869 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
1873 #ifdef CONFIG_CFG80211_REG_DEBUG
1874 printk(KERN_DEBUG
"cfg80211: Found new beacon on "
1875 "frequency: %d MHz (Ch %d) on %s\n",
1876 beacon_chan
->center_freq
,
1877 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
1880 memcpy(®_beacon
->chan
, beacon_chan
,
1881 sizeof(struct ieee80211_channel
));
1885 * Since we can be called from BH or and non-BH context
1886 * we must use spin_lock_bh()
1888 spin_lock_bh(®_pending_beacons_lock
);
1889 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
1890 spin_unlock_bh(®_pending_beacons_lock
);
1892 schedule_work(®_work
);
1897 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
1900 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1901 const struct ieee80211_freq_range
*freq_range
= NULL
;
1902 const struct ieee80211_power_rule
*power_rule
= NULL
;
1904 printk(KERN_INFO
" (start_freq - end_freq @ bandwidth), "
1905 "(max_antenna_gain, max_eirp)\n");
1907 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
1908 reg_rule
= &rd
->reg_rules
[i
];
1909 freq_range
= ®_rule
->freq_range
;
1910 power_rule
= ®_rule
->power_rule
;
1913 * There may not be documentation for max antenna gain
1914 * in certain regions
1916 if (power_rule
->max_antenna_gain
)
1917 printk(KERN_INFO
" (%d KHz - %d KHz @ %d KHz), "
1918 "(%d mBi, %d mBm)\n",
1919 freq_range
->start_freq_khz
,
1920 freq_range
->end_freq_khz
,
1921 freq_range
->max_bandwidth_khz
,
1922 power_rule
->max_antenna_gain
,
1923 power_rule
->max_eirp
);
1925 printk(KERN_INFO
" (%d KHz - %d KHz @ %d KHz), "
1927 freq_range
->start_freq_khz
,
1928 freq_range
->end_freq_khz
,
1929 freq_range
->max_bandwidth_khz
,
1930 power_rule
->max_eirp
);
1934 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
1937 if (is_intersected_alpha2(rd
->alpha2
)) {
1939 if (last_request
->initiator
==
1940 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1941 struct cfg80211_registered_device
*rdev
;
1942 rdev
= cfg80211_rdev_by_wiphy_idx(
1943 last_request
->wiphy_idx
);
1945 printk(KERN_INFO
"cfg80211: Current regulatory "
1946 "domain updated by AP to: %c%c\n",
1947 rdev
->country_ie_alpha2
[0],
1948 rdev
->country_ie_alpha2
[1]);
1950 printk(KERN_INFO
"cfg80211: Current regulatory "
1951 "domain intersected: \n");
1953 printk(KERN_INFO
"cfg80211: Current regulatory "
1954 "domain intersected: \n");
1955 } else if (is_world_regdom(rd
->alpha2
))
1956 printk(KERN_INFO
"cfg80211: World regulatory "
1957 "domain updated:\n");
1959 if (is_unknown_alpha2(rd
->alpha2
))
1960 printk(KERN_INFO
"cfg80211: Regulatory domain "
1961 "changed to driver built-in settings "
1962 "(unknown country)\n");
1964 printk(KERN_INFO
"cfg80211: Regulatory domain "
1965 "changed to country: %c%c\n",
1966 rd
->alpha2
[0], rd
->alpha2
[1]);
1971 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
1973 printk(KERN_INFO
"cfg80211: Regulatory domain: %c%c\n",
1974 rd
->alpha2
[0], rd
->alpha2
[1]);
1978 #ifdef CONFIG_CFG80211_REG_DEBUG
1979 static void reg_country_ie_process_debug(
1980 const struct ieee80211_regdomain
*rd
,
1981 const struct ieee80211_regdomain
*country_ie_regdomain
,
1982 const struct ieee80211_regdomain
*intersected_rd
)
1984 printk(KERN_DEBUG
"cfg80211: Received country IE:\n");
1985 print_regdomain_info(country_ie_regdomain
);
1986 printk(KERN_DEBUG
"cfg80211: CRDA thinks this should applied:\n");
1987 print_regdomain_info(rd
);
1988 if (intersected_rd
) {
1989 printk(KERN_DEBUG
"cfg80211: We intersect both of these "
1991 print_regdomain_info(intersected_rd
);
1994 printk(KERN_DEBUG
"cfg80211: Intersection between both failed\n");
1997 static inline void reg_country_ie_process_debug(
1998 const struct ieee80211_regdomain
*rd
,
1999 const struct ieee80211_regdomain
*country_ie_regdomain
,
2000 const struct ieee80211_regdomain
*intersected_rd
)
2005 /* Takes ownership of rd only if it doesn't fail */
2006 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
2008 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2009 struct cfg80211_registered_device
*rdev
= NULL
;
2010 struct wiphy
*request_wiphy
;
2011 /* Some basic sanity checks first */
2013 if (is_world_regdom(rd
->alpha2
)) {
2014 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2016 update_world_regdomain(rd
);
2020 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2021 !is_unknown_alpha2(rd
->alpha2
))
2028 * Lets only bother proceeding on the same alpha2 if the current
2029 * rd is non static (it means CRDA was present and was used last)
2030 * and the pending request came in from a country IE
2032 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2034 * If someone else asked us to change the rd lets only bother
2035 * checking if the alpha2 changes if CRDA was already called
2037 if (!regdom_changes(rd
->alpha2
))
2042 * Now lets set the regulatory domain, update all driver channels
2043 * and finally inform them of what we have done, in case they want
2044 * to review or adjust their own settings based on their own
2045 * internal EEPROM data
2048 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2051 if (!is_valid_rd(rd
)) {
2052 printk(KERN_ERR
"cfg80211: Invalid "
2053 "regulatory domain detected:\n");
2054 print_regdomain_info(rd
);
2058 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2060 if (!last_request
->intersect
) {
2063 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_DRIVER
) {
2065 cfg80211_regdomain
= rd
;
2070 * For a driver hint, lets copy the regulatory domain the
2071 * driver wanted to the wiphy to deal with conflicts
2075 * Userspace could have sent two replies with only
2076 * one kernel request.
2078 if (request_wiphy
->regd
)
2081 r
= reg_copy_regd(&request_wiphy
->regd
, rd
);
2086 cfg80211_regdomain
= rd
;
2090 /* Intersection requires a bit more work */
2092 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2094 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
2095 if (!intersected_rd
)
2099 * We can trash what CRDA provided now.
2100 * However if a driver requested this specific regulatory
2101 * domain we keep it for its private use
2103 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
)
2104 request_wiphy
->regd
= rd
;
2111 cfg80211_regdomain
= intersected_rd
;
2117 * Country IE requests are handled a bit differently, we intersect
2118 * the country IE rd with what CRDA believes that country should have
2122 * Userspace could have sent two replies with only
2123 * one kernel request. By the second reply we would have
2124 * already processed and consumed the country_ie_regdomain.
2126 if (!country_ie_regdomain
)
2128 BUG_ON(rd
== country_ie_regdomain
);
2131 * Intersect what CRDA returned and our what we
2132 * had built from the Country IE received
2135 intersected_rd
= regdom_intersect(rd
, country_ie_regdomain
);
2137 reg_country_ie_process_debug(rd
,
2138 country_ie_regdomain
,
2141 kfree(country_ie_regdomain
);
2142 country_ie_regdomain
= NULL
;
2144 if (!intersected_rd
)
2147 rdev
= wiphy_to_dev(request_wiphy
);
2149 rdev
->country_ie_alpha2
[0] = rd
->alpha2
[0];
2150 rdev
->country_ie_alpha2
[1] = rd
->alpha2
[1];
2151 rdev
->env
= last_request
->country_ie_env
;
2153 BUG_ON(intersected_rd
== rd
);
2159 cfg80211_regdomain
= intersected_rd
;
2166 * Use this call to set the current regulatory domain. Conflicts with
2167 * multiple drivers can be ironed out later. Caller must've already
2168 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2170 int set_regdom(const struct ieee80211_regdomain
*rd
)
2174 assert_cfg80211_lock();
2176 mutex_lock(®_mutex
);
2178 /* Note that this doesn't update the wiphys, this is done below */
2179 r
= __set_regdom(rd
);
2182 mutex_unlock(®_mutex
);
2186 /* This would make this whole thing pointless */
2187 if (!last_request
->intersect
)
2188 BUG_ON(rd
!= cfg80211_regdomain
);
2190 /* update all wiphys now with the new established regulatory domain */
2191 update_all_wiphy_regulatory(last_request
->initiator
);
2193 print_regdomain(cfg80211_regdomain
);
2195 nl80211_send_reg_change_event(last_request
);
2197 mutex_unlock(®_mutex
);
2202 /* Caller must hold cfg80211_mutex */
2203 void reg_device_remove(struct wiphy
*wiphy
)
2205 struct wiphy
*request_wiphy
= NULL
;
2207 assert_cfg80211_lock();
2209 mutex_lock(®_mutex
);
2214 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2216 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2219 last_request
->wiphy_idx
= WIPHY_IDX_STALE
;
2220 last_request
->country_ie_env
= ENVIRON_ANY
;
2222 mutex_unlock(®_mutex
);
2225 int regulatory_init(void)
2229 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2230 if (IS_ERR(reg_pdev
))
2231 return PTR_ERR(reg_pdev
);
2233 spin_lock_init(®_requests_lock
);
2234 spin_lock_init(®_pending_beacons_lock
);
2236 cfg80211_regdomain
= cfg80211_world_regdom
;
2238 /* We always try to get an update for the static regdomain */
2239 err
= regulatory_hint_core(cfg80211_regdomain
->alpha2
);
2244 * N.B. kobject_uevent_env() can fail mainly for when we're out
2245 * memory which is handled and propagated appropriately above
2246 * but it can also fail during a netlink_broadcast() or during
2247 * early boot for call_usermodehelper(). For now treat these
2248 * errors as non-fatal.
2250 printk(KERN_ERR
"cfg80211: kobject_uevent_env() was unable "
2251 "to call CRDA during init");
2252 #ifdef CONFIG_CFG80211_REG_DEBUG
2253 /* We want to find out exactly why when debugging */
2259 * Finally, if the user set the module parameter treat it
2262 if (!is_world_regdom(ieee80211_regdom
))
2263 regulatory_hint_user(ieee80211_regdom
);
2268 void regulatory_exit(void)
2270 struct regulatory_request
*reg_request
, *tmp
;
2271 struct reg_beacon
*reg_beacon
, *btmp
;
2273 cancel_work_sync(®_work
);
2275 mutex_lock(&cfg80211_mutex
);
2276 mutex_lock(®_mutex
);
2280 kfree(country_ie_regdomain
);
2281 country_ie_regdomain
= NULL
;
2283 kfree(last_request
);
2285 platform_device_unregister(reg_pdev
);
2287 spin_lock_bh(®_pending_beacons_lock
);
2288 if (!list_empty(®_pending_beacons
)) {
2289 list_for_each_entry_safe(reg_beacon
, btmp
,
2290 ®_pending_beacons
, list
) {
2291 list_del(®_beacon
->list
);
2295 spin_unlock_bh(®_pending_beacons_lock
);
2297 if (!list_empty(®_beacon_list
)) {
2298 list_for_each_entry_safe(reg_beacon
, btmp
,
2299 ®_beacon_list
, list
) {
2300 list_del(®_beacon
->list
);
2305 spin_lock(®_requests_lock
);
2306 if (!list_empty(®_requests_list
)) {
2307 list_for_each_entry_safe(reg_request
, tmp
,
2308 ®_requests_list
, list
) {
2309 list_del(®_request
->list
);
2313 spin_unlock(®_requests_lock
);
2315 mutex_unlock(®_mutex
);
2316 mutex_unlock(&cfg80211_mutex
);