2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 * DOC: Wireless regulatory infrastructure
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
20 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/random.h>
38 #include <linux/nl80211.h>
39 #include <linux/platform_device.h>
40 #include <net/wireless.h>
41 #include <net/cfg80211.h>
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
;
51 /* Keep the ordering from large to small */
52 static u32 supported_bandwidths
[] = {
57 /* Central wireless core regulatory domains, we only need two,
58 * the current one and a world regulatory domain in case we have no
59 * information to give us an alpha2 */
60 const struct ieee80211_regdomain
*cfg80211_regdomain
;
62 /* We use this as a place for the rd structure built from the
63 * last parsed country IE to rest until CRDA gets back to us with
64 * what it thinks should apply for the same country */
65 static const struct ieee80211_regdomain
*country_ie_regdomain
;
67 /* We keep a static world regulatory domain in case of the absence of CRDA */
68 static const struct ieee80211_regdomain world_regdom
= {
72 REG_RULE(2412-10, 2462+10, 40, 6, 20,
73 NL80211_RRF_PASSIVE_SCAN
|
78 static const struct ieee80211_regdomain
*cfg80211_world_regdom
=
81 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
82 static char *ieee80211_regdom
= "US";
83 module_param(ieee80211_regdom
, charp
, 0444);
84 MODULE_PARM_DESC(ieee80211_regdom
, "IEEE 802.11 regulatory domain code");
86 /* We assume 40 MHz bandwidth for the old regulatory work.
87 * We make emphasis we are using the exact same frequencies
90 static const struct ieee80211_regdomain us_regdom
= {
94 /* IEEE 802.11b/g, channels 1..11 */
95 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
96 /* IEEE 802.11a, channel 36 */
97 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
98 /* IEEE 802.11a, channel 40 */
99 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
100 /* IEEE 802.11a, channel 44 */
101 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
102 /* IEEE 802.11a, channels 48..64 */
103 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
104 /* IEEE 802.11a, channels 149..165, outdoor */
105 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
109 static const struct ieee80211_regdomain jp_regdom
= {
113 /* IEEE 802.11b/g, channels 1..14 */
114 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
115 /* IEEE 802.11a, channels 34..48 */
116 REG_RULE(5170-10, 5240+10, 40, 6, 20,
117 NL80211_RRF_PASSIVE_SCAN
),
118 /* IEEE 802.11a, channels 52..64 */
119 REG_RULE(5260-10, 5320+10, 40, 6, 20,
120 NL80211_RRF_NO_IBSS
|
125 static const struct ieee80211_regdomain eu_regdom
= {
127 /* This alpha2 is bogus, we leave it here just for stupid
128 * backward compatibility */
131 /* IEEE 802.11b/g, channels 1..13 */
132 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
133 /* IEEE 802.11a, channel 36 */
134 REG_RULE(5180-10, 5180+10, 40, 6, 23,
135 NL80211_RRF_PASSIVE_SCAN
),
136 /* IEEE 802.11a, channel 40 */
137 REG_RULE(5200-10, 5200+10, 40, 6, 23,
138 NL80211_RRF_PASSIVE_SCAN
),
139 /* IEEE 802.11a, channel 44 */
140 REG_RULE(5220-10, 5220+10, 40, 6, 23,
141 NL80211_RRF_PASSIVE_SCAN
),
142 /* IEEE 802.11a, channels 48..64 */
143 REG_RULE(5240-10, 5320+10, 40, 6, 20,
144 NL80211_RRF_NO_IBSS
|
146 /* IEEE 802.11a, channels 100..140 */
147 REG_RULE(5500-10, 5700+10, 40, 6, 30,
148 NL80211_RRF_NO_IBSS
|
153 static const struct ieee80211_regdomain
*static_regdom(char *alpha2
)
155 if (alpha2
[0] == 'U' && alpha2
[1] == 'S')
157 if (alpha2
[0] == 'J' && alpha2
[1] == 'P')
159 if (alpha2
[0] == 'E' && alpha2
[1] == 'U')
161 /* Default, as per the old rules */
165 static bool is_old_static_regdom(const struct ieee80211_regdomain
*rd
)
167 if (rd
== &us_regdom
|| rd
== &jp_regdom
|| rd
== &eu_regdom
)
172 static inline bool is_old_static_regdom(const struct ieee80211_regdomain
*rd
)
178 static void reset_regdomains(void)
180 /* avoid freeing static information or freeing something twice */
181 if (cfg80211_regdomain
== cfg80211_world_regdom
)
182 cfg80211_regdomain
= NULL
;
183 if (cfg80211_world_regdom
== &world_regdom
)
184 cfg80211_world_regdom
= NULL
;
185 if (cfg80211_regdomain
== &world_regdom
)
186 cfg80211_regdomain
= NULL
;
187 if (is_old_static_regdom(cfg80211_regdomain
))
188 cfg80211_regdomain
= NULL
;
190 kfree(cfg80211_regdomain
);
191 kfree(cfg80211_world_regdom
);
193 cfg80211_world_regdom
= &world_regdom
;
194 cfg80211_regdomain
= NULL
;
197 /* Dynamic world regulatory domain requested by the wireless
198 * core upon initialization */
199 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
201 BUG_ON(!last_request
);
205 cfg80211_world_regdom
= rd
;
206 cfg80211_regdomain
= rd
;
209 bool is_world_regdom(const char *alpha2
)
213 if (alpha2
[0] == '0' && alpha2
[1] == '0')
218 static bool is_alpha2_set(const char *alpha2
)
222 if (alpha2
[0] != 0 && alpha2
[1] != 0)
227 static bool is_alpha_upper(char letter
)
230 if (letter
>= 65 && letter
<= 90)
235 static bool is_unknown_alpha2(const char *alpha2
)
239 /* Special case where regulatory domain was built by driver
240 * but a specific alpha2 cannot be determined */
241 if (alpha2
[0] == '9' && alpha2
[1] == '9')
246 static bool is_intersected_alpha2(const char *alpha2
)
250 /* Special case where regulatory domain is the
251 * result of an intersection between two regulatory domain
253 if (alpha2
[0] == '9' && alpha2
[1] == '8')
258 static bool is_an_alpha2(const char *alpha2
)
262 if (is_alpha_upper(alpha2
[0]) && is_alpha_upper(alpha2
[1]))
267 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
269 if (!alpha2_x
|| !alpha2_y
)
271 if (alpha2_x
[0] == alpha2_y
[0] &&
272 alpha2_x
[1] == alpha2_y
[1])
277 static bool regdom_changed(const char *alpha2
)
279 if (!cfg80211_regdomain
)
281 if (alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
287 * country_ie_integrity_changes - tells us if the country IE has changed
288 * @checksum: checksum of country IE of fields we are interested in
290 * If the country IE has not changed you can ignore it safely. This is
291 * useful to determine if two devices are seeing two different country IEs
292 * even on the same alpha2. Note that this will return false if no IE has
293 * been set on the wireless core yet.
295 static bool country_ie_integrity_changes(u32 checksum
)
297 /* If no IE has been set then the checksum doesn't change */
298 if (unlikely(!last_request
->country_ie_checksum
))
300 if (unlikely(last_request
->country_ie_checksum
!= checksum
))
305 /* This lets us keep regulatory code which is updated on a regulatory
306 * basis in userspace. */
307 static int call_crda(const char *alpha2
)
309 char country_env
[9 + 2] = "COUNTRY=";
315 if (!is_world_regdom((char *) alpha2
))
316 printk(KERN_INFO
"cfg80211: Calling CRDA for country: %c%c\n",
317 alpha2
[0], alpha2
[1]);
319 printk(KERN_INFO
"cfg80211: Calling CRDA to update world "
320 "regulatory domain\n");
322 country_env
[8] = alpha2
[0];
323 country_env
[9] = alpha2
[1];
325 return kobject_uevent_env(®_pdev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
328 /* Used by nl80211 before kmalloc'ing our regulatory domain */
329 bool reg_is_valid_request(const char *alpha2
)
334 return alpha2_equal(last_request
->alpha2
, alpha2
);
337 /* Sanity check on a regulatory rule */
338 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
340 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
343 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
346 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
349 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
351 if (freq_diff
<= 0 || freq_range
->max_bandwidth_khz
> freq_diff
)
357 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
359 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
362 if (!rd
->n_reg_rules
)
365 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
368 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
369 reg_rule
= &rd
->reg_rules
[i
];
370 if (!is_valid_reg_rule(reg_rule
))
377 /* Returns value in KHz */
378 static u32
freq_max_bandwidth(const struct ieee80211_freq_range
*freq_range
,
382 for (i
= 0; i
< ARRAY_SIZE(supported_bandwidths
); i
++) {
383 u32 start_freq_khz
= freq
- supported_bandwidths
[i
]/2;
384 u32 end_freq_khz
= freq
+ supported_bandwidths
[i
]/2;
385 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
386 end_freq_khz
<= freq_range
->end_freq_khz
)
387 return supported_bandwidths
[i
];
393 * freq_in_rule_band - tells us if a frequency is in a frequency band
394 * @freq_range: frequency rule we want to query
395 * @freq_khz: frequency we are inquiring about
397 * This lets us know if a specific frequency rule is or is not relevant to
398 * a specific frequency's band. Bands are device specific and artificial
399 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
400 * safe for now to assume that a frequency rule should not be part of a
401 * frequency's band if the start freq or end freq are off by more than 2 GHz.
402 * This resolution can be lowered and should be considered as we add
403 * regulatory rule support for other "bands".
405 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
408 #define ONE_GHZ_IN_KHZ 1000000
409 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
411 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
414 #undef ONE_GHZ_IN_KHZ
417 /* Converts a country IE to a regulatory domain. A regulatory domain
418 * structure has a lot of information which the IE doesn't yet have,
419 * so for the other values we use upper max values as we will intersect
420 * with our userspace regulatory agent to get lower bounds. */
421 static struct ieee80211_regdomain
*country_ie_2_rd(
426 struct ieee80211_regdomain
*rd
= NULL
;
430 u32 num_rules
= 0, size_of_regd
= 0;
431 u8
*triplets_start
= NULL
;
432 u8 len_at_triplet
= 0;
433 /* the last channel we have registered in a subband (triplet) */
434 int last_sub_max_channel
= 0;
436 *checksum
= 0xDEADBEEF;
438 /* Country IE requirements */
439 BUG_ON(country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
||
440 country_ie_len
& 0x01);
442 alpha2
[0] = country_ie
[0];
443 alpha2
[1] = country_ie
[1];
446 * Third octet can be:
450 * anything else we assume is no restrictions
452 if (country_ie
[2] == 'I')
453 flags
= NL80211_RRF_NO_OUTDOOR
;
454 else if (country_ie
[2] == 'O')
455 flags
= NL80211_RRF_NO_INDOOR
;
460 triplets_start
= country_ie
;
461 len_at_triplet
= country_ie_len
;
463 *checksum
^= ((flags
^ alpha2
[0] ^ alpha2
[1]) << 8);
465 /* We need to build a reg rule for each triplet, but first we must
466 * calculate the number of reg rules we will need. We will need one
467 * for each channel subband */
468 while (country_ie_len
>= 3) {
470 struct ieee80211_country_ie_triplet
*triplet
=
471 (struct ieee80211_country_ie_triplet
*) country_ie
;
472 int cur_sub_max_channel
= 0, cur_channel
= 0;
474 if (triplet
->ext
.reg_extension_id
>=
475 IEEE80211_COUNTRY_EXTENSION_ID
) {
482 if (triplet
->chans
.first_channel
<= 14)
483 end_channel
= triplet
->chans
.first_channel
+
484 triplet
->chans
.num_channels
;
487 * 5 GHz -- For example in country IEs if the first
488 * channel given is 36 and the number of channels is 4
489 * then the individual channel numbers defined for the
490 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
491 * and not 36, 37, 38, 39.
493 * See: http://tinyurl.com/11d-clarification
495 end_channel
= triplet
->chans
.first_channel
+
496 (4 * (triplet
->chans
.num_channels
- 1));
498 cur_channel
= triplet
->chans
.first_channel
;
499 cur_sub_max_channel
= end_channel
;
501 /* Basic sanity check */
502 if (cur_sub_max_channel
< cur_channel
)
505 /* Do not allow overlapping channels. Also channels
506 * passed in each subband must be monotonically
508 if (last_sub_max_channel
) {
509 if (cur_channel
<= last_sub_max_channel
)
511 if (cur_sub_max_channel
<= last_sub_max_channel
)
515 /* When dot11RegulatoryClassesRequired is supported
516 * we can throw ext triplets as part of this soup,
517 * for now we don't care when those change as we
518 * don't support them */
519 *checksum
^= ((cur_channel
^ cur_sub_max_channel
) << 8) |
520 ((cur_sub_max_channel
^ cur_sub_max_channel
) << 16) |
521 ((triplet
->chans
.max_power
^ cur_sub_max_channel
) << 24);
523 last_sub_max_channel
= cur_sub_max_channel
;
529 /* Note: this is not a IEEE requirement but
530 * simply a memory requirement */
531 if (num_rules
> NL80211_MAX_SUPP_REG_RULES
)
535 country_ie
= triplets_start
;
536 country_ie_len
= len_at_triplet
;
538 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
539 (num_rules
* sizeof(struct ieee80211_reg_rule
));
541 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
545 rd
->n_reg_rules
= num_rules
;
546 rd
->alpha2
[0] = alpha2
[0];
547 rd
->alpha2
[1] = alpha2
[1];
549 /* This time around we fill in the rd */
550 while (country_ie_len
>= 3) {
552 struct ieee80211_country_ie_triplet
*triplet
=
553 (struct ieee80211_country_ie_triplet
*) country_ie
;
554 struct ieee80211_reg_rule
*reg_rule
= NULL
;
555 struct ieee80211_freq_range
*freq_range
= NULL
;
556 struct ieee80211_power_rule
*power_rule
= NULL
;
558 /* Must parse if dot11RegulatoryClassesRequired is true,
559 * we don't support this yet */
560 if (triplet
->ext
.reg_extension_id
>=
561 IEEE80211_COUNTRY_EXTENSION_ID
) {
567 reg_rule
= &rd
->reg_rules
[i
];
568 freq_range
= ®_rule
->freq_range
;
569 power_rule
= ®_rule
->power_rule
;
571 reg_rule
->flags
= flags
;
574 if (triplet
->chans
.first_channel
<= 14)
575 end_channel
= triplet
->chans
.first_channel
+
576 triplet
->chans
.num_channels
;
578 end_channel
= triplet
->chans
.first_channel
+
579 (4 * (triplet
->chans
.num_channels
- 1));
581 /* The +10 is since the regulatory domain expects
582 * the actual band edge, not the center of freq for
583 * its start and end freqs, assuming 20 MHz bandwidth on
584 * the channels passed */
585 freq_range
->start_freq_khz
=
586 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
587 triplet
->chans
.first_channel
) - 10);
588 freq_range
->end_freq_khz
=
589 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
592 /* Large arbitrary values, we intersect later */
593 /* Increment this if we ever support >= 40 MHz channels
595 freq_range
->max_bandwidth_khz
= MHZ_TO_KHZ(40);
596 power_rule
->max_antenna_gain
= DBI_TO_MBI(100);
597 power_rule
->max_eirp
= DBM_TO_MBM(100);
603 BUG_ON(i
> NL80211_MAX_SUPP_REG_RULES
);
610 /* Helper for regdom_intersect(), this does the real
611 * mathematical intersection fun */
612 static int reg_rules_intersect(
613 const struct ieee80211_reg_rule
*rule1
,
614 const struct ieee80211_reg_rule
*rule2
,
615 struct ieee80211_reg_rule
*intersected_rule
)
617 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
618 struct ieee80211_freq_range
*freq_range
;
619 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
620 struct ieee80211_power_rule
*power_rule
;
623 freq_range1
= &rule1
->freq_range
;
624 freq_range2
= &rule2
->freq_range
;
625 freq_range
= &intersected_rule
->freq_range
;
627 power_rule1
= &rule1
->power_rule
;
628 power_rule2
= &rule2
->power_rule
;
629 power_rule
= &intersected_rule
->power_rule
;
631 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
632 freq_range2
->start_freq_khz
);
633 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
634 freq_range2
->end_freq_khz
);
635 freq_range
->max_bandwidth_khz
= min(freq_range1
->max_bandwidth_khz
,
636 freq_range2
->max_bandwidth_khz
);
638 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
639 if (freq_range
->max_bandwidth_khz
> freq_diff
)
640 freq_range
->max_bandwidth_khz
= freq_diff
;
642 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
643 power_rule2
->max_eirp
);
644 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
645 power_rule2
->max_antenna_gain
);
647 intersected_rule
->flags
= (rule1
->flags
| rule2
->flags
);
649 if (!is_valid_reg_rule(intersected_rule
))
656 * regdom_intersect - do the intersection between two regulatory domains
657 * @rd1: first regulatory domain
658 * @rd2: second regulatory domain
660 * Use this function to get the intersection between two regulatory domains.
661 * Once completed we will mark the alpha2 for the rd as intersected, "98",
662 * as no one single alpha2 can represent this regulatory domain.
664 * Returns a pointer to the regulatory domain structure which will hold the
665 * resulting intersection of rules between rd1 and rd2. We will
666 * kzalloc() this structure for you.
668 static struct ieee80211_regdomain
*regdom_intersect(
669 const struct ieee80211_regdomain
*rd1
,
670 const struct ieee80211_regdomain
*rd2
)
674 unsigned int num_rules
= 0, rule_idx
= 0;
675 const struct ieee80211_reg_rule
*rule1
, *rule2
;
676 struct ieee80211_reg_rule
*intersected_rule
;
677 struct ieee80211_regdomain
*rd
;
678 /* This is just a dummy holder to help us count */
679 struct ieee80211_reg_rule irule
;
681 /* Uses the stack temporarily for counter arithmetic */
682 intersected_rule
= &irule
;
684 memset(intersected_rule
, 0, sizeof(struct ieee80211_reg_rule
));
689 /* First we get a count of the rules we'll need, then we actually
690 * build them. This is to so we can malloc() and free() a
691 * regdomain once. The reason we use reg_rules_intersect() here
692 * is it will return -EINVAL if the rule computed makes no sense.
693 * All rules that do check out OK are valid. */
695 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
696 rule1
= &rd1
->reg_rules
[x
];
697 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
698 rule2
= &rd2
->reg_rules
[y
];
699 if (!reg_rules_intersect(rule1
, rule2
,
702 memset(intersected_rule
, 0,
703 sizeof(struct ieee80211_reg_rule
));
710 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
711 ((num_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
713 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
717 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
718 rule1
= &rd1
->reg_rules
[x
];
719 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
720 rule2
= &rd2
->reg_rules
[y
];
721 /* This time around instead of using the stack lets
722 * write to the target rule directly saving ourselves
724 intersected_rule
= &rd
->reg_rules
[rule_idx
];
725 r
= reg_rules_intersect(rule1
, rule2
,
727 /* No need to memset here the intersected rule here as
728 * we're not using the stack anymore */
735 if (rule_idx
!= num_rules
) {
740 rd
->n_reg_rules
= num_rules
;
747 /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
748 * want to just have the channel structure use these */
749 static u32
map_regdom_flags(u32 rd_flags
)
751 u32 channel_flags
= 0;
752 if (rd_flags
& NL80211_RRF_PASSIVE_SCAN
)
753 channel_flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
754 if (rd_flags
& NL80211_RRF_NO_IBSS
)
755 channel_flags
|= IEEE80211_CHAN_NO_IBSS
;
756 if (rd_flags
& NL80211_RRF_DFS
)
757 channel_flags
|= IEEE80211_CHAN_RADAR
;
758 return channel_flags
;
761 static int freq_reg_info_regd(struct wiphy
*wiphy
,
764 const struct ieee80211_reg_rule
**reg_rule
,
765 const struct ieee80211_regdomain
*custom_regd
)
768 bool band_rule_found
= false;
769 const struct ieee80211_regdomain
*regd
;
770 u32 max_bandwidth
= 0;
772 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
774 /* Follow the driver's regulatory domain, if present, unless a country
775 * IE has been processed or a user wants to help complaince further */
776 if (last_request
->initiator
!= REGDOM_SET_BY_COUNTRY_IE
&&
777 last_request
->initiator
!= REGDOM_SET_BY_USER
&&
784 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
785 const struct ieee80211_reg_rule
*rr
;
786 const struct ieee80211_freq_range
*fr
= NULL
;
787 const struct ieee80211_power_rule
*pr
= NULL
;
789 rr
= ®d
->reg_rules
[i
];
790 fr
= &rr
->freq_range
;
791 pr
= &rr
->power_rule
;
793 /* We only need to know if one frequency rule was
794 * was in center_freq's band, that's enough, so lets
795 * not overwrite it once found */
796 if (!band_rule_found
)
797 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
799 max_bandwidth
= freq_max_bandwidth(fr
, center_freq
);
801 if (max_bandwidth
&& *bandwidth
<= max_bandwidth
) {
803 *bandwidth
= max_bandwidth
;
808 if (!band_rule_found
)
811 return !max_bandwidth
;
813 EXPORT_SYMBOL(freq_reg_info
);
815 int freq_reg_info(struct wiphy
*wiphy
, u32 center_freq
, u32
*bandwidth
,
816 const struct ieee80211_reg_rule
**reg_rule
)
818 return freq_reg_info_regd(wiphy
, center_freq
,
819 bandwidth
, reg_rule
, NULL
);
822 static void handle_channel(struct wiphy
*wiphy
, enum ieee80211_band band
,
823 unsigned int chan_idx
)
827 u32 max_bandwidth
= 0;
828 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
829 const struct ieee80211_power_rule
*power_rule
= NULL
;
830 struct ieee80211_supported_band
*sband
;
831 struct ieee80211_channel
*chan
;
833 sband
= wiphy
->bands
[band
];
834 BUG_ON(chan_idx
>= sband
->n_channels
);
835 chan
= &sband
->channels
[chan_idx
];
837 flags
= chan
->orig_flags
;
839 r
= freq_reg_info(wiphy
, MHZ_TO_KHZ(chan
->center_freq
),
840 &max_bandwidth
, ®_rule
);
843 /* This means no regulatory rule was found in the country IE
844 * with a frequency range on the center_freq's band, since
845 * IEEE-802.11 allows for a country IE to have a subset of the
846 * regulatory information provided in a country we ignore
847 * disabling the channel unless at least one reg rule was
848 * found on the center_freq's band. For details see this
851 * http://tinyurl.com/11d-clarification
854 last_request
->initiator
== REGDOM_SET_BY_COUNTRY_IE
) {
855 #ifdef CONFIG_CFG80211_REG_DEBUG
856 printk(KERN_DEBUG
"cfg80211: Leaving channel %d MHz "
857 "intact on %s - no rule found in band on "
859 chan
->center_freq
, wiphy_name(wiphy
));
862 /* In this case we know the country IE has at least one reg rule
863 * for the band so we respect its band definitions */
864 #ifdef CONFIG_CFG80211_REG_DEBUG
865 if (last_request
->initiator
== REGDOM_SET_BY_COUNTRY_IE
)
866 printk(KERN_DEBUG
"cfg80211: Disabling "
867 "channel %d MHz on %s due to "
869 chan
->center_freq
, wiphy_name(wiphy
));
871 flags
|= IEEE80211_CHAN_DISABLED
;
877 power_rule
= ®_rule
->power_rule
;
879 if (last_request
->initiator
== REGDOM_SET_BY_DRIVER
&&
880 last_request
->wiphy
&& last_request
->wiphy
== wiphy
&&
881 last_request
->wiphy
->strict_regulatory
) {
882 /* This gaurantees the driver's requested regulatory domain
883 * will always be used as a base for further regulatory
885 chan
->flags
= chan
->orig_flags
=
886 map_regdom_flags(reg_rule
->flags
);
887 chan
->max_antenna_gain
= chan
->orig_mag
=
888 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
889 chan
->max_bandwidth
= KHZ_TO_MHZ(max_bandwidth
);
890 chan
->max_power
= chan
->orig_mpwr
=
891 (int) MBM_TO_DBM(power_rule
->max_eirp
);
895 chan
->flags
= flags
| map_regdom_flags(reg_rule
->flags
);
896 chan
->max_antenna_gain
= min(chan
->orig_mag
,
897 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
898 chan
->max_bandwidth
= KHZ_TO_MHZ(max_bandwidth
);
900 chan
->max_power
= min(chan
->orig_mpwr
,
901 (int) MBM_TO_DBM(power_rule
->max_eirp
));
903 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
906 static void handle_band(struct wiphy
*wiphy
, enum ieee80211_band band
)
909 struct ieee80211_supported_band
*sband
;
911 BUG_ON(!wiphy
->bands
[band
]);
912 sband
= wiphy
->bands
[band
];
914 for (i
= 0; i
< sband
->n_channels
; i
++)
915 handle_channel(wiphy
, band
, i
);
918 static bool ignore_reg_update(struct wiphy
*wiphy
, enum reg_set_by setby
)
922 if (setby
== REGDOM_SET_BY_CORE
&&
923 wiphy
->custom_regulatory
)
925 /* wiphy->regd will be set once the device has its own
926 * desired regulatory domain set */
927 if (wiphy
->strict_regulatory
&& !wiphy
->regd
&&
928 !is_world_regdom(last_request
->alpha2
))
933 static void update_all_wiphy_regulatory(enum reg_set_by setby
)
935 struct cfg80211_registered_device
*drv
;
937 list_for_each_entry(drv
, &cfg80211_drv_list
, list
)
938 wiphy_update_regulatory(&drv
->wiphy
, setby
);
941 void wiphy_update_regulatory(struct wiphy
*wiphy
, enum reg_set_by setby
)
943 enum ieee80211_band band
;
945 if (ignore_reg_update(wiphy
, setby
))
947 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
948 if (wiphy
->bands
[band
])
949 handle_band(wiphy
, band
);
951 if (wiphy
->reg_notifier
)
952 wiphy
->reg_notifier(wiphy
, last_request
);
955 static void handle_channel_custom(struct wiphy
*wiphy
,
956 enum ieee80211_band band
,
957 unsigned int chan_idx
,
958 const struct ieee80211_regdomain
*regd
)
961 u32 max_bandwidth
= 0;
962 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
963 const struct ieee80211_power_rule
*power_rule
= NULL
;
964 struct ieee80211_supported_band
*sband
;
965 struct ieee80211_channel
*chan
;
967 sband
= wiphy
->bands
[band
];
968 BUG_ON(chan_idx
>= sband
->n_channels
);
969 chan
= &sband
->channels
[chan_idx
];
971 r
= freq_reg_info_regd(wiphy
, MHZ_TO_KHZ(chan
->center_freq
),
972 &max_bandwidth
, ®_rule
, regd
);
975 chan
->flags
= IEEE80211_CHAN_DISABLED
;
979 power_rule
= ®_rule
->power_rule
;
981 chan
->flags
|= map_regdom_flags(reg_rule
->flags
);
982 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
983 chan
->max_bandwidth
= KHZ_TO_MHZ(max_bandwidth
);
984 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
987 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
988 const struct ieee80211_regdomain
*regd
)
991 struct ieee80211_supported_band
*sband
;
993 BUG_ON(!wiphy
->bands
[band
]);
994 sband
= wiphy
->bands
[band
];
996 for (i
= 0; i
< sband
->n_channels
; i
++)
997 handle_channel_custom(wiphy
, band
, i
, regd
);
1000 /* Used by drivers prior to wiphy registration */
1001 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1002 const struct ieee80211_regdomain
*regd
)
1004 enum ieee80211_band band
;
1005 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1006 if (wiphy
->bands
[band
])
1007 handle_band_custom(wiphy
, band
, regd
);
1010 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1012 static int reg_copy_regd(const struct ieee80211_regdomain
**dst_regd
,
1013 const struct ieee80211_regdomain
*src_regd
)
1015 struct ieee80211_regdomain
*regd
;
1016 int size_of_regd
= 0;
1019 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
1020 ((src_regd
->n_reg_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
1022 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
1026 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
1028 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
1029 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
1030 sizeof(struct ieee80211_reg_rule
));
1036 /* Return value which can be used by ignore_request() to indicate
1037 * it has been determined we should intersect two regulatory domains */
1038 #define REG_INTERSECT 1
1040 /* This has the logic which determines when a new request
1041 * should be ignored. */
1042 static int ignore_request(struct wiphy
*wiphy
, enum reg_set_by set_by
,
1045 /* All initial requests are respected */
1050 case REGDOM_SET_BY_INIT
:
1052 case REGDOM_SET_BY_CORE
:
1054 case REGDOM_SET_BY_COUNTRY_IE
:
1055 if (unlikely(!is_an_alpha2(alpha2
)))
1057 if (last_request
->initiator
== REGDOM_SET_BY_COUNTRY_IE
) {
1058 if (last_request
->wiphy
!= wiphy
) {
1060 * Two cards with two APs claiming different
1061 * different Country IE alpha2s. We could
1062 * intersect them, but that seems unlikely
1063 * to be correct. Reject second one for now.
1065 if (!alpha2_equal(alpha2
,
1066 cfg80211_regdomain
->alpha2
))
1070 /* Two consecutive Country IE hints on the same wiphy.
1071 * This should be picked up early by the driver/stack */
1072 if (WARN_ON(!alpha2_equal(cfg80211_regdomain
->alpha2
,
1077 return REG_INTERSECT
;
1078 case REGDOM_SET_BY_DRIVER
:
1079 if (last_request
->initiator
== REGDOM_SET_BY_CORE
) {
1080 if (is_old_static_regdom(cfg80211_regdomain
))
1082 if (!alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
1086 return REG_INTERSECT
;
1087 case REGDOM_SET_BY_USER
:
1088 if (last_request
->initiator
== REGDOM_SET_BY_COUNTRY_IE
)
1089 return REG_INTERSECT
;
1090 /* If the user knows better the user should set the regdom
1091 * to their country before the IE is picked up */
1092 if (last_request
->initiator
== REGDOM_SET_BY_USER
&&
1093 last_request
->intersect
)
1095 /* Process user requests only after previous user/driver/core
1096 * requests have been processed */
1097 if (last_request
->initiator
== REGDOM_SET_BY_CORE
||
1098 last_request
->initiator
== REGDOM_SET_BY_DRIVER
||
1099 last_request
->initiator
== REGDOM_SET_BY_USER
) {
1100 if (!alpha2_equal(last_request
->alpha2
,
1101 cfg80211_regdomain
->alpha2
))
1105 if (!is_old_static_regdom(cfg80211_regdomain
) &&
1106 alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
1115 /* Caller must hold &cfg80211_mutex */
1116 int __regulatory_hint(struct wiphy
*wiphy
, enum reg_set_by set_by
,
1118 u32 country_ie_checksum
,
1119 enum environment_cap env
)
1121 struct regulatory_request
*request
;
1122 bool intersect
= false;
1125 r
= ignore_request(wiphy
, set_by
, alpha2
);
1127 if (r
== REG_INTERSECT
) {
1128 if (set_by
== REGDOM_SET_BY_DRIVER
) {
1129 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1135 /* If the regulatory domain being requested by the
1136 * driver has already been set just copy it to the
1138 if (r
== -EALREADY
&& set_by
== REGDOM_SET_BY_DRIVER
) {
1139 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1149 request
= kzalloc(sizeof(struct regulatory_request
),
1154 request
->alpha2
[0] = alpha2
[0];
1155 request
->alpha2
[1] = alpha2
[1];
1156 request
->initiator
= set_by
;
1157 request
->wiphy
= wiphy
;
1158 request
->intersect
= intersect
;
1159 request
->country_ie_checksum
= country_ie_checksum
;
1160 request
->country_ie_env
= env
;
1162 kfree(last_request
);
1163 last_request
= request
;
1165 /* When r == REG_INTERSECT we do need to call CRDA */
1170 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1171 * AND if CRDA is NOT present nothing will happen, if someone
1172 * wants to bother with 11d with OLD_REG you can add a timer.
1173 * If after x amount of time nothing happens you can call:
1175 * return set_regdom(country_ie_regdomain);
1177 * to intersect with the static rd
1179 return call_crda(alpha2
);
1182 static int regulatory_hint_core(const char *alpha2
)
1184 struct regulatory_request
*request
;
1186 BUG_ON(last_request
);
1188 request
= kzalloc(sizeof(struct regulatory_request
),
1193 request
->alpha2
[0] = alpha2
[0];
1194 request
->alpha2
[1] = alpha2
[1];
1195 request
->initiator
= REGDOM_SET_BY_CORE
;
1197 last_request
= request
;
1199 return call_crda(alpha2
);
1202 void regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1207 mutex_lock(&cfg80211_mutex
);
1208 r
= __regulatory_hint(wiphy
, REGDOM_SET_BY_DRIVER
,
1209 alpha2
, 0, ENVIRON_ANY
);
1210 /* This is required so that the orig_* parameters are saved */
1211 if (r
== -EALREADY
&& wiphy
->strict_regulatory
)
1212 wiphy_update_regulatory(wiphy
, REGDOM_SET_BY_DRIVER
);
1213 mutex_unlock(&cfg80211_mutex
);
1215 EXPORT_SYMBOL(regulatory_hint
);
1217 static bool reg_same_country_ie_hint(struct wiphy
*wiphy
,
1218 u32 country_ie_checksum
)
1220 if (!last_request
->wiphy
)
1222 if (likely(last_request
->wiphy
!= wiphy
))
1223 return !country_ie_integrity_changes(country_ie_checksum
);
1224 /* We should not have let these through at this point, they
1225 * should have been picked up earlier by the first alpha2 check
1227 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum
)))
1232 void regulatory_hint_11d(struct wiphy
*wiphy
,
1236 struct ieee80211_regdomain
*rd
= NULL
;
1239 enum environment_cap env
= ENVIRON_ANY
;
1244 mutex_lock(&cfg80211_mutex
);
1246 /* IE len must be evenly divisible by 2 */
1247 if (country_ie_len
& 0x01)
1250 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
1253 /* Pending country IE processing, this can happen after we
1254 * call CRDA and wait for a response if a beacon was received before
1255 * we were able to process the last regulatory_hint_11d() call */
1256 if (country_ie_regdomain
)
1259 alpha2
[0] = country_ie
[0];
1260 alpha2
[1] = country_ie
[1];
1262 if (country_ie
[2] == 'I')
1263 env
= ENVIRON_INDOOR
;
1264 else if (country_ie
[2] == 'O')
1265 env
= ENVIRON_OUTDOOR
;
1267 /* We will run this for *every* beacon processed for the BSSID, so
1268 * we optimize an early check to exit out early if we don't have to
1270 if (likely(last_request
->wiphy
)) {
1271 struct cfg80211_registered_device
*drv_last_ie
;
1273 drv_last_ie
= wiphy_to_dev(last_request
->wiphy
);
1275 /* Lets keep this simple -- we trust the first AP
1276 * after we intersect with CRDA */
1277 if (likely(last_request
->wiphy
== wiphy
)) {
1278 /* Ignore IEs coming in on this wiphy with
1279 * the same alpha2 and environment cap */
1280 if (likely(alpha2_equal(drv_last_ie
->country_ie_alpha2
,
1282 env
== drv_last_ie
->env
)) {
1285 /* the wiphy moved on to another BSSID or the AP
1286 * was reconfigured. XXX: We need to deal with the
1287 * case where the user suspends and goes to goes
1288 * to another country, and then gets IEs from an
1289 * AP with different settings */
1292 /* Ignore IEs coming in on two separate wiphys with
1293 * the same alpha2 and environment cap */
1294 if (likely(alpha2_equal(drv_last_ie
->country_ie_alpha2
,
1296 env
== drv_last_ie
->env
)) {
1299 /* We could potentially intersect though */
1304 rd
= country_ie_2_rd(country_ie
, country_ie_len
, &checksum
);
1308 /* This will not happen right now but we leave it here for the
1309 * the future when we want to add suspend/resume support and having
1310 * the user move to another country after doing so, or having the user
1311 * move to another AP. Right now we just trust the first AP. This is why
1312 * this is marked as likley(). If we hit this before we add this support
1313 * we want to be informed of it as it would indicate a mistake in the
1315 if (likely(WARN_ON(reg_same_country_ie_hint(wiphy
, checksum
))))
1318 /* We keep this around for when CRDA comes back with a response so
1319 * we can intersect with that */
1320 country_ie_regdomain
= rd
;
1322 __regulatory_hint(wiphy
, REGDOM_SET_BY_COUNTRY_IE
,
1323 country_ie_regdomain
->alpha2
, checksum
, env
);
1326 mutex_unlock(&cfg80211_mutex
);
1328 EXPORT_SYMBOL(regulatory_hint_11d
);
1330 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
1333 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1334 const struct ieee80211_freq_range
*freq_range
= NULL
;
1335 const struct ieee80211_power_rule
*power_rule
= NULL
;
1337 printk(KERN_INFO
"\t(start_freq - end_freq @ bandwidth), "
1338 "(max_antenna_gain, max_eirp)\n");
1340 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
1341 reg_rule
= &rd
->reg_rules
[i
];
1342 freq_range
= ®_rule
->freq_range
;
1343 power_rule
= ®_rule
->power_rule
;
1345 /* There may not be documentation for max antenna gain
1346 * in certain regions */
1347 if (power_rule
->max_antenna_gain
)
1348 printk(KERN_INFO
"\t(%d KHz - %d KHz @ %d KHz), "
1349 "(%d mBi, %d mBm)\n",
1350 freq_range
->start_freq_khz
,
1351 freq_range
->end_freq_khz
,
1352 freq_range
->max_bandwidth_khz
,
1353 power_rule
->max_antenna_gain
,
1354 power_rule
->max_eirp
);
1356 printk(KERN_INFO
"\t(%d KHz - %d KHz @ %d KHz), "
1358 freq_range
->start_freq_khz
,
1359 freq_range
->end_freq_khz
,
1360 freq_range
->max_bandwidth_khz
,
1361 power_rule
->max_eirp
);
1365 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
1368 if (is_intersected_alpha2(rd
->alpha2
)) {
1369 struct wiphy
*wiphy
= NULL
;
1370 struct cfg80211_registered_device
*drv
;
1372 if (last_request
->initiator
== REGDOM_SET_BY_COUNTRY_IE
) {
1373 if (last_request
->wiphy
) {
1374 wiphy
= last_request
->wiphy
;
1375 drv
= wiphy_to_dev(wiphy
);
1376 printk(KERN_INFO
"cfg80211: Current regulatory "
1377 "domain updated by AP to: %c%c\n",
1378 drv
->country_ie_alpha2
[0],
1379 drv
->country_ie_alpha2
[1]);
1381 printk(KERN_INFO
"cfg80211: Current regulatory "
1382 "domain intersected: \n");
1384 printk(KERN_INFO
"cfg80211: Current regulatory "
1385 "domain intersected: \n");
1386 } else if (is_world_regdom(rd
->alpha2
))
1387 printk(KERN_INFO
"cfg80211: World regulatory "
1388 "domain updated:\n");
1390 if (is_unknown_alpha2(rd
->alpha2
))
1391 printk(KERN_INFO
"cfg80211: Regulatory domain "
1392 "changed to driver built-in settings "
1393 "(unknown country)\n");
1395 printk(KERN_INFO
"cfg80211: Regulatory domain "
1396 "changed to country: %c%c\n",
1397 rd
->alpha2
[0], rd
->alpha2
[1]);
1402 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
1404 printk(KERN_INFO
"cfg80211: Regulatory domain: %c%c\n",
1405 rd
->alpha2
[0], rd
->alpha2
[1]);
1409 #ifdef CONFIG_CFG80211_REG_DEBUG
1410 static void reg_country_ie_process_debug(
1411 const struct ieee80211_regdomain
*rd
,
1412 const struct ieee80211_regdomain
*country_ie_regdomain
,
1413 const struct ieee80211_regdomain
*intersected_rd
)
1415 printk(KERN_DEBUG
"cfg80211: Received country IE:\n");
1416 print_regdomain_info(country_ie_regdomain
);
1417 printk(KERN_DEBUG
"cfg80211: CRDA thinks this should applied:\n");
1418 print_regdomain_info(rd
);
1419 if (intersected_rd
) {
1420 printk(KERN_DEBUG
"cfg80211: We intersect both of these "
1422 print_regdomain_info(intersected_rd
);
1425 printk(KERN_DEBUG
"cfg80211: Intersection between both failed\n");
1428 static inline void reg_country_ie_process_debug(
1429 const struct ieee80211_regdomain
*rd
,
1430 const struct ieee80211_regdomain
*country_ie_regdomain
,
1431 const struct ieee80211_regdomain
*intersected_rd
)
1436 /* Takes ownership of rd only if it doesn't fail */
1437 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
1439 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
1440 struct cfg80211_registered_device
*drv
= NULL
;
1441 struct wiphy
*wiphy
= NULL
;
1442 /* Some basic sanity checks first */
1444 if (is_world_regdom(rd
->alpha2
)) {
1445 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
1447 update_world_regdomain(rd
);
1451 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
1452 !is_unknown_alpha2(rd
->alpha2
))
1458 /* Lets only bother proceeding on the same alpha2 if the current
1459 * rd is non static (it means CRDA was present and was used last)
1460 * and the pending request came in from a country IE */
1461 if (last_request
->initiator
!= REGDOM_SET_BY_COUNTRY_IE
) {
1462 /* If someone else asked us to change the rd lets only bother
1463 * checking if the alpha2 changes if CRDA was already called */
1464 if (!is_old_static_regdom(cfg80211_regdomain
) &&
1465 !regdom_changed(rd
->alpha2
))
1469 wiphy
= last_request
->wiphy
;
1471 /* Now lets set the regulatory domain, update all driver channels
1472 * and finally inform them of what we have done, in case they want
1473 * to review or adjust their own settings based on their own
1474 * internal EEPROM data */
1476 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
1479 if (!is_valid_rd(rd
)) {
1480 printk(KERN_ERR
"cfg80211: Invalid "
1481 "regulatory domain detected:\n");
1482 print_regdomain_info(rd
);
1486 if (!last_request
->intersect
) {
1489 if (last_request
->initiator
!= REGDOM_SET_BY_DRIVER
) {
1491 cfg80211_regdomain
= rd
;
1495 /* For a driver hint, lets copy the regulatory domain the
1496 * driver wanted to the wiphy to deal with conflicts */
1498 BUG_ON(last_request
->wiphy
->regd
);
1500 r
= reg_copy_regd(&last_request
->wiphy
->regd
, rd
);
1505 cfg80211_regdomain
= rd
;
1509 /* Intersection requires a bit more work */
1511 if (last_request
->initiator
!= REGDOM_SET_BY_COUNTRY_IE
) {
1513 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
1514 if (!intersected_rd
)
1517 /* We can trash what CRDA provided now.
1518 * However if a driver requested this specific regulatory
1519 * domain we keep it for its private use */
1520 if (last_request
->initiator
== REGDOM_SET_BY_DRIVER
)
1521 last_request
->wiphy
->regd
= rd
;
1528 cfg80211_regdomain
= intersected_rd
;
1534 * Country IE requests are handled a bit differently, we intersect
1535 * the country IE rd with what CRDA believes that country should have
1538 BUG_ON(!country_ie_regdomain
);
1540 if (rd
!= country_ie_regdomain
) {
1541 /* Intersect what CRDA returned and our what we
1542 * had built from the Country IE received */
1544 intersected_rd
= regdom_intersect(rd
, country_ie_regdomain
);
1546 reg_country_ie_process_debug(rd
, country_ie_regdomain
,
1549 kfree(country_ie_regdomain
);
1550 country_ie_regdomain
= NULL
;
1552 /* This would happen when CRDA was not present and
1553 * OLD_REGULATORY was enabled. We intersect our Country
1554 * IE rd and what was set on cfg80211 originally */
1555 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
1558 if (!intersected_rd
)
1561 drv
= wiphy_to_dev(wiphy
);
1563 drv
->country_ie_alpha2
[0] = rd
->alpha2
[0];
1564 drv
->country_ie_alpha2
[1] = rd
->alpha2
[1];
1565 drv
->env
= last_request
->country_ie_env
;
1567 BUG_ON(intersected_rd
== rd
);
1573 cfg80211_regdomain
= intersected_rd
;
1579 /* Use this call to set the current regulatory domain. Conflicts with
1580 * multiple drivers can be ironed out later. Caller must've already
1581 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex */
1582 int set_regdom(const struct ieee80211_regdomain
*rd
)
1586 /* Note that this doesn't update the wiphys, this is done below */
1587 r
= __set_regdom(rd
);
1593 /* This would make this whole thing pointless */
1594 if (!last_request
->intersect
)
1595 BUG_ON(rd
!= cfg80211_regdomain
);
1597 /* update all wiphys now with the new established regulatory domain */
1598 update_all_wiphy_regulatory(last_request
->initiator
);
1600 print_regdomain(cfg80211_regdomain
);
1605 /* Caller must hold cfg80211_mutex */
1606 void reg_device_remove(struct wiphy
*wiphy
)
1609 if (!last_request
|| !last_request
->wiphy
)
1611 if (last_request
->wiphy
!= wiphy
)
1613 last_request
->wiphy
= NULL
;
1614 last_request
->country_ie_env
= ENVIRON_ANY
;
1617 int regulatory_init(void)
1621 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
1622 if (IS_ERR(reg_pdev
))
1623 return PTR_ERR(reg_pdev
);
1625 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
1626 cfg80211_regdomain
= static_regdom(ieee80211_regdom
);
1628 printk(KERN_INFO
"cfg80211: Using static regulatory domain info\n");
1629 print_regdomain_info(cfg80211_regdomain
);
1630 /* The old code still requests for a new regdomain and if
1631 * you have CRDA you get it updated, otherwise you get
1632 * stuck with the static values. We ignore "EU" code as
1633 * that is not a valid ISO / IEC 3166 alpha2 */
1634 if (ieee80211_regdom
[0] != 'E' || ieee80211_regdom
[1] != 'U')
1635 err
= regulatory_hint_core(ieee80211_regdom
);
1637 cfg80211_regdomain
= cfg80211_world_regdom
;
1639 err
= regulatory_hint_core("00");
1645 * N.B. kobject_uevent_env() can fail mainly for when we're out
1646 * memory which is handled and propagated appropriately above
1647 * but it can also fail during a netlink_broadcast() or during
1648 * early boot for call_usermodehelper(). For now treat these
1649 * errors as non-fatal.
1651 printk(KERN_ERR
"cfg80211: kobject_uevent_env() was unable "
1652 "to call CRDA during init");
1653 #ifdef CONFIG_CFG80211_REG_DEBUG
1654 /* We want to find out exactly why when debugging */
1662 void regulatory_exit(void)
1664 mutex_lock(&cfg80211_mutex
);
1668 kfree(country_ie_regdomain
);
1669 country_ie_regdomain
= NULL
;
1671 kfree(last_request
);
1673 platform_device_unregister(reg_pdev
);
1675 mutex_unlock(&cfg80211_mutex
);