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.
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/list.h>
41 #include <linux/random.h>
42 #include <linux/ctype.h>
43 #include <linux/nl80211.h>
44 #include <linux/platform_device.h>
45 #include <net/cfg80211.h>
51 #ifdef CONFIG_CFG80211_REG_DEBUG
52 #define REG_DBG_PRINT(format, args...) \
54 printk(KERN_DEBUG pr_fmt(format), ##args); \
57 #define REG_DBG_PRINT(args...)
60 static struct regulatory_request core_request_world
= {
61 .initiator
= NL80211_REGDOM_SET_BY_CORE
,
66 .country_ie_env
= ENVIRON_ANY
,
69 /* Receipt of information from last regulatory request */
70 static struct regulatory_request
*last_request
= &core_request_world
;
72 /* To trigger userspace events */
73 static struct platform_device
*reg_pdev
;
75 static struct device_type reg_device_type
= {
76 .uevent
= reg_device_uevent
,
80 * Central wireless core regulatory domains, we only need two,
81 * the current one and a world regulatory domain in case we have no
82 * information to give us an alpha2
84 const struct ieee80211_regdomain
*cfg80211_regdomain
;
87 * Protects static reg.c components:
88 * - cfg80211_world_regdom
92 static DEFINE_MUTEX(reg_mutex
);
94 static inline void assert_reg_lock(void)
96 lockdep_assert_held(®_mutex
);
99 /* Used to queue up regulatory hints */
100 static LIST_HEAD(reg_requests_list
);
101 static spinlock_t reg_requests_lock
;
103 /* Used to queue up beacon hints for review */
104 static LIST_HEAD(reg_pending_beacons
);
105 static spinlock_t reg_pending_beacons_lock
;
107 /* Used to keep track of processed beacon hints */
108 static LIST_HEAD(reg_beacon_list
);
111 struct list_head list
;
112 struct ieee80211_channel chan
;
115 static void reg_todo(struct work_struct
*work
);
116 static DECLARE_WORK(reg_work
, reg_todo
);
118 static void reg_timeout_work(struct work_struct
*work
);
119 static DECLARE_DELAYED_WORK(reg_timeout
, reg_timeout_work
);
121 /* We keep a static world regulatory domain in case of the absence of CRDA */
122 static const struct ieee80211_regdomain world_regdom
= {
126 /* IEEE 802.11b/g, channels 1..11 */
127 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
128 /* IEEE 802.11b/g, channels 12..13. No HT40
129 * channel fits here. */
130 REG_RULE(2467-10, 2472+10, 20, 6, 20,
131 NL80211_RRF_PASSIVE_SCAN
|
132 NL80211_RRF_NO_IBSS
),
133 /* IEEE 802.11 channel 14 - Only JP enables
134 * this and for 802.11b only */
135 REG_RULE(2484-10, 2484+10, 20, 6, 20,
136 NL80211_RRF_PASSIVE_SCAN
|
137 NL80211_RRF_NO_IBSS
|
138 NL80211_RRF_NO_OFDM
),
139 /* IEEE 802.11a, channel 36..48 */
140 REG_RULE(5180-10, 5240+10, 40, 6, 20,
141 NL80211_RRF_PASSIVE_SCAN
|
142 NL80211_RRF_NO_IBSS
),
144 /* NB: 5260 MHz - 5700 MHz requies DFS */
146 /* IEEE 802.11a, channel 149..165 */
147 REG_RULE(5745-10, 5825+10, 40, 6, 20,
148 NL80211_RRF_PASSIVE_SCAN
|
149 NL80211_RRF_NO_IBSS
),
153 static const struct ieee80211_regdomain
*cfg80211_world_regdom
=
156 static char *ieee80211_regdom
= "00";
157 static char user_alpha2
[2];
159 module_param(ieee80211_regdom
, charp
, 0444);
160 MODULE_PARM_DESC(ieee80211_regdom
, "IEEE 802.11 regulatory domain code");
162 static void reset_regdomains(bool full_reset
)
164 /* avoid freeing static information or freeing something twice */
165 if (cfg80211_regdomain
== cfg80211_world_regdom
)
166 cfg80211_regdomain
= NULL
;
167 if (cfg80211_world_regdom
== &world_regdom
)
168 cfg80211_world_regdom
= NULL
;
169 if (cfg80211_regdomain
== &world_regdom
)
170 cfg80211_regdomain
= NULL
;
172 kfree(cfg80211_regdomain
);
173 kfree(cfg80211_world_regdom
);
175 cfg80211_world_regdom
= &world_regdom
;
176 cfg80211_regdomain
= NULL
;
181 if (last_request
!= &core_request_world
)
183 last_request
= &core_request_world
;
187 * Dynamic world regulatory domain requested by the wireless
188 * core upon initialization
190 static void update_world_regdomain(const struct ieee80211_regdomain
*rd
)
192 BUG_ON(!last_request
);
194 reset_regdomains(false);
196 cfg80211_world_regdom
= rd
;
197 cfg80211_regdomain
= rd
;
200 bool is_world_regdom(const char *alpha2
)
204 if (alpha2
[0] == '0' && alpha2
[1] == '0')
209 static bool is_alpha2_set(const char *alpha2
)
213 if (alpha2
[0] != 0 && alpha2
[1] != 0)
218 static bool is_unknown_alpha2(const char *alpha2
)
223 * Special case where regulatory domain was built by driver
224 * but a specific alpha2 cannot be determined
226 if (alpha2
[0] == '9' && alpha2
[1] == '9')
231 static bool is_intersected_alpha2(const char *alpha2
)
236 * Special case where regulatory domain is the
237 * result of an intersection between two regulatory domain
240 if (alpha2
[0] == '9' && alpha2
[1] == '8')
245 static bool is_an_alpha2(const char *alpha2
)
249 if (isalpha(alpha2
[0]) && isalpha(alpha2
[1]))
254 static bool alpha2_equal(const char *alpha2_x
, const char *alpha2_y
)
256 if (!alpha2_x
|| !alpha2_y
)
258 if (alpha2_x
[0] == alpha2_y
[0] &&
259 alpha2_x
[1] == alpha2_y
[1])
264 static bool regdom_changes(const char *alpha2
)
266 assert_cfg80211_lock();
268 if (!cfg80211_regdomain
)
270 if (alpha2_equal(cfg80211_regdomain
->alpha2
, alpha2
))
276 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
277 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
278 * has ever been issued.
280 static bool is_user_regdom_saved(void)
282 if (user_alpha2
[0] == '9' && user_alpha2
[1] == '7')
285 /* This would indicate a mistake on the design */
286 if (WARN((!is_world_regdom(user_alpha2
) &&
287 !is_an_alpha2(user_alpha2
)),
288 "Unexpected user alpha2: %c%c\n",
296 static int reg_copy_regd(const struct ieee80211_regdomain
**dst_regd
,
297 const struct ieee80211_regdomain
*src_regd
)
299 struct ieee80211_regdomain
*regd
;
300 int size_of_regd
= 0;
303 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
304 ((src_regd
->n_reg_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
306 regd
= kzalloc(size_of_regd
, GFP_KERNEL
);
310 memcpy(regd
, src_regd
, sizeof(struct ieee80211_regdomain
));
312 for (i
= 0; i
< src_regd
->n_reg_rules
; i
++)
313 memcpy(®d
->reg_rules
[i
], &src_regd
->reg_rules
[i
],
314 sizeof(struct ieee80211_reg_rule
));
320 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
321 struct reg_regdb_search_request
{
323 struct list_head list
;
326 static LIST_HEAD(reg_regdb_search_list
);
327 static DEFINE_MUTEX(reg_regdb_search_mutex
);
329 static void reg_regdb_search(struct work_struct
*work
)
331 struct reg_regdb_search_request
*request
;
332 const struct ieee80211_regdomain
*curdom
, *regdom
;
335 mutex_lock(®_regdb_search_mutex
);
336 while (!list_empty(®_regdb_search_list
)) {
337 request
= list_first_entry(®_regdb_search_list
,
338 struct reg_regdb_search_request
,
340 list_del(&request
->list
);
342 for (i
=0; i
<reg_regdb_size
; i
++) {
343 curdom
= reg_regdb
[i
];
345 if (!memcmp(request
->alpha2
, curdom
->alpha2
, 2)) {
346 r
= reg_copy_regd(®dom
, curdom
);
349 mutex_lock(&cfg80211_mutex
);
351 mutex_unlock(&cfg80211_mutex
);
358 mutex_unlock(®_regdb_search_mutex
);
361 static DECLARE_WORK(reg_regdb_work
, reg_regdb_search
);
363 static void reg_regdb_query(const char *alpha2
)
365 struct reg_regdb_search_request
*request
;
370 request
= kzalloc(sizeof(struct reg_regdb_search_request
), GFP_KERNEL
);
374 memcpy(request
->alpha2
, alpha2
, 2);
376 mutex_lock(®_regdb_search_mutex
);
377 list_add_tail(&request
->list
, ®_regdb_search_list
);
378 mutex_unlock(®_regdb_search_mutex
);
380 schedule_work(®_regdb_work
);
383 static inline void reg_regdb_query(const char *alpha2
) {}
384 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
387 * This lets us keep regulatory code which is updated on a regulatory
388 * basis in userspace. Country information is filled in by
391 static int call_crda(const char *alpha2
)
393 if (!is_world_regdom((char *) alpha2
))
394 pr_info("Calling CRDA for country: %c%c\n",
395 alpha2
[0], alpha2
[1]);
397 pr_info("Calling CRDA to update world regulatory domain\n");
399 /* query internal regulatory database (if it exists) */
400 reg_regdb_query(alpha2
);
402 return kobject_uevent(®_pdev
->dev
.kobj
, KOBJ_CHANGE
);
405 /* Used by nl80211 before kmalloc'ing our regulatory domain */
406 bool reg_is_valid_request(const char *alpha2
)
408 assert_cfg80211_lock();
413 return alpha2_equal(last_request
->alpha2
, alpha2
);
416 /* Sanity check on a regulatory rule */
417 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
419 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
422 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
425 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
428 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
430 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
431 freq_range
->max_bandwidth_khz
> freq_diff
)
437 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
439 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
442 if (!rd
->n_reg_rules
)
445 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
448 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
449 reg_rule
= &rd
->reg_rules
[i
];
450 if (!is_valid_reg_rule(reg_rule
))
457 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
461 u32 start_freq_khz
, end_freq_khz
;
463 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
464 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
466 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
467 end_freq_khz
<= freq_range
->end_freq_khz
)
474 * freq_in_rule_band - tells us if a frequency is in a frequency band
475 * @freq_range: frequency rule we want to query
476 * @freq_khz: frequency we are inquiring about
478 * This lets us know if a specific frequency rule is or is not relevant to
479 * a specific frequency's band. Bands are device specific and artificial
480 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
481 * safe for now to assume that a frequency rule should not be part of a
482 * frequency's band if the start freq or end freq are off by more than 2 GHz.
483 * This resolution can be lowered and should be considered as we add
484 * regulatory rule support for other "bands".
486 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
489 #define ONE_GHZ_IN_KHZ 1000000
490 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
492 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
495 #undef ONE_GHZ_IN_KHZ
499 * Helper for regdom_intersect(), this does the real
500 * mathematical intersection fun
502 static int reg_rules_intersect(
503 const struct ieee80211_reg_rule
*rule1
,
504 const struct ieee80211_reg_rule
*rule2
,
505 struct ieee80211_reg_rule
*intersected_rule
)
507 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
508 struct ieee80211_freq_range
*freq_range
;
509 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
510 struct ieee80211_power_rule
*power_rule
;
513 freq_range1
= &rule1
->freq_range
;
514 freq_range2
= &rule2
->freq_range
;
515 freq_range
= &intersected_rule
->freq_range
;
517 power_rule1
= &rule1
->power_rule
;
518 power_rule2
= &rule2
->power_rule
;
519 power_rule
= &intersected_rule
->power_rule
;
521 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
522 freq_range2
->start_freq_khz
);
523 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
524 freq_range2
->end_freq_khz
);
525 freq_range
->max_bandwidth_khz
= min(freq_range1
->max_bandwidth_khz
,
526 freq_range2
->max_bandwidth_khz
);
528 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
529 if (freq_range
->max_bandwidth_khz
> freq_diff
)
530 freq_range
->max_bandwidth_khz
= freq_diff
;
532 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
533 power_rule2
->max_eirp
);
534 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
535 power_rule2
->max_antenna_gain
);
537 intersected_rule
->flags
= (rule1
->flags
| rule2
->flags
);
539 if (!is_valid_reg_rule(intersected_rule
))
546 * regdom_intersect - do the intersection between two regulatory domains
547 * @rd1: first regulatory domain
548 * @rd2: second regulatory domain
550 * Use this function to get the intersection between two regulatory domains.
551 * Once completed we will mark the alpha2 for the rd as intersected, "98",
552 * as no one single alpha2 can represent this regulatory domain.
554 * Returns a pointer to the regulatory domain structure which will hold the
555 * resulting intersection of rules between rd1 and rd2. We will
556 * kzalloc() this structure for you.
558 static struct ieee80211_regdomain
*regdom_intersect(
559 const struct ieee80211_regdomain
*rd1
,
560 const struct ieee80211_regdomain
*rd2
)
564 unsigned int num_rules
= 0, rule_idx
= 0;
565 const struct ieee80211_reg_rule
*rule1
, *rule2
;
566 struct ieee80211_reg_rule
*intersected_rule
;
567 struct ieee80211_regdomain
*rd
;
568 /* This is just a dummy holder to help us count */
569 struct ieee80211_reg_rule irule
;
571 /* Uses the stack temporarily for counter arithmetic */
572 intersected_rule
= &irule
;
574 memset(intersected_rule
, 0, sizeof(struct ieee80211_reg_rule
));
580 * First we get a count of the rules we'll need, then we actually
581 * build them. This is to so we can malloc() and free() a
582 * regdomain once. The reason we use reg_rules_intersect() here
583 * is it will return -EINVAL if the rule computed makes no sense.
584 * All rules that do check out OK are valid.
587 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
588 rule1
= &rd1
->reg_rules
[x
];
589 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
590 rule2
= &rd2
->reg_rules
[y
];
591 if (!reg_rules_intersect(rule1
, rule2
,
594 memset(intersected_rule
, 0,
595 sizeof(struct ieee80211_reg_rule
));
602 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
603 ((num_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
605 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
609 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
610 rule1
= &rd1
->reg_rules
[x
];
611 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
612 rule2
= &rd2
->reg_rules
[y
];
614 * This time around instead of using the stack lets
615 * write to the target rule directly saving ourselves
618 intersected_rule
= &rd
->reg_rules
[rule_idx
];
619 r
= reg_rules_intersect(rule1
, rule2
,
622 * No need to memset here the intersected rule here as
623 * we're not using the stack anymore
631 if (rule_idx
!= num_rules
) {
636 rd
->n_reg_rules
= num_rules
;
644 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
645 * want to just have the channel structure use these
647 static u32
map_regdom_flags(u32 rd_flags
)
649 u32 channel_flags
= 0;
650 if (rd_flags
& NL80211_RRF_PASSIVE_SCAN
)
651 channel_flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
652 if (rd_flags
& NL80211_RRF_NO_IBSS
)
653 channel_flags
|= IEEE80211_CHAN_NO_IBSS
;
654 if (rd_flags
& NL80211_RRF_DFS
)
655 channel_flags
|= IEEE80211_CHAN_RADAR
;
656 return channel_flags
;
659 static int freq_reg_info_regd(struct wiphy
*wiphy
,
662 const struct ieee80211_reg_rule
**reg_rule
,
663 const struct ieee80211_regdomain
*custom_regd
)
666 bool band_rule_found
= false;
667 const struct ieee80211_regdomain
*regd
;
668 bool bw_fits
= false;
671 desired_bw_khz
= MHZ_TO_KHZ(20);
673 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
676 * Follow the driver's regulatory domain, if present, unless a country
677 * IE has been processed or a user wants to help complaince further
680 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
681 last_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
688 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
689 const struct ieee80211_reg_rule
*rr
;
690 const struct ieee80211_freq_range
*fr
= NULL
;
692 rr
= ®d
->reg_rules
[i
];
693 fr
= &rr
->freq_range
;
696 * We only need to know if one frequency rule was
697 * was in center_freq's band, that's enough, so lets
698 * not overwrite it once found
700 if (!band_rule_found
)
701 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
703 bw_fits
= reg_does_bw_fit(fr
,
707 if (band_rule_found
&& bw_fits
) {
713 if (!band_rule_found
)
719 int freq_reg_info(struct wiphy
*wiphy
,
722 const struct ieee80211_reg_rule
**reg_rule
)
724 assert_cfg80211_lock();
725 return freq_reg_info_regd(wiphy
,
731 EXPORT_SYMBOL(freq_reg_info
);
733 #ifdef CONFIG_CFG80211_REG_DEBUG
734 static const char *reg_initiator_name(enum nl80211_reg_initiator initiator
)
737 case NL80211_REGDOM_SET_BY_CORE
:
738 return "Set by core";
739 case NL80211_REGDOM_SET_BY_USER
:
740 return "Set by user";
741 case NL80211_REGDOM_SET_BY_DRIVER
:
742 return "Set by driver";
743 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
744 return "Set by country IE";
751 static void chan_reg_rule_print_dbg(struct ieee80211_channel
*chan
,
753 const struct ieee80211_reg_rule
*reg_rule
)
755 const struct ieee80211_power_rule
*power_rule
;
756 const struct ieee80211_freq_range
*freq_range
;
757 char max_antenna_gain
[32];
759 power_rule
= ®_rule
->power_rule
;
760 freq_range
= ®_rule
->freq_range
;
762 if (!power_rule
->max_antenna_gain
)
763 snprintf(max_antenna_gain
, 32, "N/A");
765 snprintf(max_antenna_gain
, 32, "%d", power_rule
->max_antenna_gain
);
767 REG_DBG_PRINT("Updating information on frequency %d MHz "
768 "for a %d MHz width channel with regulatory rule:\n",
770 KHZ_TO_MHZ(desired_bw_khz
));
772 REG_DBG_PRINT("%d KHz - %d KHz @ KHz), (%s mBi, %d mBm)\n",
773 freq_range
->start_freq_khz
,
774 freq_range
->end_freq_khz
,
776 power_rule
->max_eirp
);
779 static void chan_reg_rule_print_dbg(struct ieee80211_channel
*chan
,
781 const struct ieee80211_reg_rule
*reg_rule
)
788 * Note that right now we assume the desired channel bandwidth
789 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
790 * per channel, the primary and the extension channel). To support
791 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
792 * new ieee80211_channel.target_bw and re run the regulatory check
793 * on the wiphy with the target_bw specified. Then we can simply use
794 * that below for the desired_bw_khz below.
796 static void handle_channel(struct wiphy
*wiphy
,
797 enum nl80211_reg_initiator initiator
,
798 enum ieee80211_band band
,
799 unsigned int chan_idx
)
802 u32 flags
, bw_flags
= 0;
803 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
804 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
805 const struct ieee80211_power_rule
*power_rule
= NULL
;
806 const struct ieee80211_freq_range
*freq_range
= NULL
;
807 struct ieee80211_supported_band
*sband
;
808 struct ieee80211_channel
*chan
;
809 struct wiphy
*request_wiphy
= NULL
;
811 assert_cfg80211_lock();
813 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
815 sband
= wiphy
->bands
[band
];
816 BUG_ON(chan_idx
>= sband
->n_channels
);
817 chan
= &sband
->channels
[chan_idx
];
819 flags
= chan
->orig_flags
;
821 r
= freq_reg_info(wiphy
,
822 MHZ_TO_KHZ(chan
->center_freq
),
828 * We will disable all channels that do not match our
829 * received regulatory rule unless the hint is coming
830 * from a Country IE and the Country IE had no information
831 * about a band. The IEEE 802.11 spec allows for an AP
832 * to send only a subset of the regulatory rules allowed,
833 * so an AP in the US that only supports 2.4 GHz may only send
834 * a country IE with information for the 2.4 GHz band
835 * while 5 GHz is still supported.
837 if (initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
841 REG_DBG_PRINT("Disabling freq %d MHz\n", chan
->center_freq
);
842 chan
->flags
= IEEE80211_CHAN_DISABLED
;
846 chan_reg_rule_print_dbg(chan
, desired_bw_khz
, reg_rule
);
848 power_rule
= ®_rule
->power_rule
;
849 freq_range
= ®_rule
->freq_range
;
851 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
852 bw_flags
= IEEE80211_CHAN_NO_HT40
;
854 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
855 request_wiphy
&& request_wiphy
== wiphy
&&
856 request_wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
) {
858 * This guarantees the driver's requested regulatory domain
859 * will always be used as a base for further regulatory
862 chan
->flags
= chan
->orig_flags
=
863 map_regdom_flags(reg_rule
->flags
) | bw_flags
;
864 chan
->max_antenna_gain
= chan
->orig_mag
=
865 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
866 chan
->max_power
= chan
->orig_mpwr
=
867 (int) MBM_TO_DBM(power_rule
->max_eirp
);
871 chan
->beacon_found
= false;
872 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
873 chan
->max_antenna_gain
= min(chan
->orig_mag
,
874 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
876 chan
->max_power
= min(chan
->orig_mpwr
,
877 (int) MBM_TO_DBM(power_rule
->max_eirp
));
879 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
882 static void handle_band(struct wiphy
*wiphy
,
883 enum ieee80211_band band
,
884 enum nl80211_reg_initiator initiator
)
887 struct ieee80211_supported_band
*sband
;
889 BUG_ON(!wiphy
->bands
[band
]);
890 sband
= wiphy
->bands
[band
];
892 for (i
= 0; i
< sband
->n_channels
; i
++)
893 handle_channel(wiphy
, initiator
, band
, i
);
896 static bool ignore_reg_update(struct wiphy
*wiphy
,
897 enum nl80211_reg_initiator initiator
)
900 REG_DBG_PRINT("Ignoring regulatory request %s since "
901 "last_request is not set\n",
902 reg_initiator_name(initiator
));
906 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
907 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
) {
908 REG_DBG_PRINT("Ignoring regulatory request %s "
909 "since the driver uses its own custom "
910 "regulatory domain ",
911 reg_initiator_name(initiator
));
916 * wiphy->regd will be set once the device has its own
917 * desired regulatory domain set
919 if (wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
&& !wiphy
->regd
&&
920 initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
921 !is_world_regdom(last_request
->alpha2
)) {
922 REG_DBG_PRINT("Ignoring regulatory request %s "
923 "since the driver requires its own regulaotry "
924 "domain to be set first",
925 reg_initiator_name(initiator
));
932 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
934 struct cfg80211_registered_device
*rdev
;
936 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
937 wiphy_update_regulatory(&rdev
->wiphy
, initiator
);
940 static void handle_reg_beacon(struct wiphy
*wiphy
,
941 unsigned int chan_idx
,
942 struct reg_beacon
*reg_beacon
)
944 struct ieee80211_supported_band
*sband
;
945 struct ieee80211_channel
*chan
;
946 bool channel_changed
= false;
947 struct ieee80211_channel chan_before
;
949 assert_cfg80211_lock();
951 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
952 chan
= &sband
->channels
[chan_idx
];
954 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
957 if (chan
->beacon_found
)
960 chan
->beacon_found
= true;
962 if (wiphy
->flags
& WIPHY_FLAG_DISABLE_BEACON_HINTS
)
965 chan_before
.center_freq
= chan
->center_freq
;
966 chan_before
.flags
= chan
->flags
;
968 if (chan
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
969 chan
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
970 channel_changed
= true;
973 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
) {
974 chan
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
975 channel_changed
= true;
979 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
983 * Called when a scan on a wiphy finds a beacon on
986 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
987 struct reg_beacon
*reg_beacon
)
990 struct ieee80211_supported_band
*sband
;
992 assert_cfg80211_lock();
994 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
997 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
999 for (i
= 0; i
< sband
->n_channels
; i
++)
1000 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1004 * Called upon reg changes or a new wiphy is added
1006 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1009 struct ieee80211_supported_band
*sband
;
1010 struct reg_beacon
*reg_beacon
;
1012 assert_cfg80211_lock();
1014 if (list_empty(®_beacon_list
))
1017 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1018 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1020 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1021 for (i
= 0; i
< sband
->n_channels
; i
++)
1022 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1026 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1028 if (is_world_regdom(cfg80211_regdomain
->alpha2
) ||
1029 (wiphy
->regd
&& is_world_regdom(wiphy
->regd
->alpha2
)))
1032 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1033 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1038 /* Reap the advantages of previously found beacons */
1039 static void reg_process_beacons(struct wiphy
*wiphy
)
1042 * Means we are just firing up cfg80211, so no beacons would
1043 * have been processed yet.
1047 if (!reg_is_world_roaming(wiphy
))
1049 wiphy_update_beacon_reg(wiphy
);
1052 static bool is_ht40_not_allowed(struct ieee80211_channel
*chan
)
1056 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1058 /* This would happen when regulatory rules disallow HT40 completely */
1059 if (IEEE80211_CHAN_NO_HT40
== (chan
->flags
& (IEEE80211_CHAN_NO_HT40
)))
1064 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1065 enum ieee80211_band band
,
1066 unsigned int chan_idx
)
1068 struct ieee80211_supported_band
*sband
;
1069 struct ieee80211_channel
*channel
;
1070 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1073 assert_cfg80211_lock();
1075 sband
= wiphy
->bands
[band
];
1076 BUG_ON(chan_idx
>= sband
->n_channels
);
1077 channel
= &sband
->channels
[chan_idx
];
1079 if (is_ht40_not_allowed(channel
)) {
1080 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1085 * We need to ensure the extension channels exist to
1086 * be able to use HT40- or HT40+, this finds them (or not)
1088 for (i
= 0; i
< sband
->n_channels
; i
++) {
1089 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1090 if (c
->center_freq
== (channel
->center_freq
- 20))
1092 if (c
->center_freq
== (channel
->center_freq
+ 20))
1097 * Please note that this assumes target bandwidth is 20 MHz,
1098 * if that ever changes we also need to change the below logic
1099 * to include that as well.
1101 if (is_ht40_not_allowed(channel_before
))
1102 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1104 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1106 if (is_ht40_not_allowed(channel_after
))
1107 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1109 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1112 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1113 enum ieee80211_band band
)
1116 struct ieee80211_supported_band
*sband
;
1118 BUG_ON(!wiphy
->bands
[band
]);
1119 sband
= wiphy
->bands
[band
];
1121 for (i
= 0; i
< sband
->n_channels
; i
++)
1122 reg_process_ht_flags_channel(wiphy
, band
, i
);
1125 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1127 enum ieee80211_band band
;
1132 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1133 if (wiphy
->bands
[band
])
1134 reg_process_ht_flags_band(wiphy
, band
);
1139 void wiphy_update_regulatory(struct wiphy
*wiphy
,
1140 enum nl80211_reg_initiator initiator
)
1142 enum ieee80211_band band
;
1144 if (ignore_reg_update(wiphy
, initiator
))
1147 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1148 if (wiphy
->bands
[band
])
1149 handle_band(wiphy
, band
, initiator
);
1152 reg_process_beacons(wiphy
);
1153 reg_process_ht_flags(wiphy
);
1154 if (wiphy
->reg_notifier
)
1155 wiphy
->reg_notifier(wiphy
, last_request
);
1158 static void handle_channel_custom(struct wiphy
*wiphy
,
1159 enum ieee80211_band band
,
1160 unsigned int chan_idx
,
1161 const struct ieee80211_regdomain
*regd
)
1164 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
1166 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1167 const struct ieee80211_power_rule
*power_rule
= NULL
;
1168 const struct ieee80211_freq_range
*freq_range
= NULL
;
1169 struct ieee80211_supported_band
*sband
;
1170 struct ieee80211_channel
*chan
;
1174 sband
= wiphy
->bands
[band
];
1175 BUG_ON(chan_idx
>= sband
->n_channels
);
1176 chan
= &sband
->channels
[chan_idx
];
1178 r
= freq_reg_info_regd(wiphy
,
1179 MHZ_TO_KHZ(chan
->center_freq
),
1185 REG_DBG_PRINT("Disabling freq %d MHz as custom "
1186 "regd has no rule that fits a %d MHz "
1189 KHZ_TO_MHZ(desired_bw_khz
));
1190 chan
->flags
= IEEE80211_CHAN_DISABLED
;
1194 chan_reg_rule_print_dbg(chan
, desired_bw_khz
, reg_rule
);
1196 power_rule
= ®_rule
->power_rule
;
1197 freq_range
= ®_rule
->freq_range
;
1199 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1200 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1202 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1203 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1204 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1207 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
1208 const struct ieee80211_regdomain
*regd
)
1211 struct ieee80211_supported_band
*sband
;
1213 BUG_ON(!wiphy
->bands
[band
]);
1214 sband
= wiphy
->bands
[band
];
1216 for (i
= 0; i
< sband
->n_channels
; i
++)
1217 handle_channel_custom(wiphy
, band
, i
, regd
);
1220 /* Used by drivers prior to wiphy registration */
1221 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1222 const struct ieee80211_regdomain
*regd
)
1224 enum ieee80211_band band
;
1225 unsigned int bands_set
= 0;
1227 mutex_lock(®_mutex
);
1228 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1229 if (!wiphy
->bands
[band
])
1231 handle_band_custom(wiphy
, band
, regd
);
1234 mutex_unlock(®_mutex
);
1237 * no point in calling this if it won't have any effect
1238 * on your device's supportd bands.
1240 WARN_ON(!bands_set
);
1242 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1245 * Return value which can be used by ignore_request() to indicate
1246 * it has been determined we should intersect two regulatory domains
1248 #define REG_INTERSECT 1
1250 /* This has the logic which determines when a new request
1251 * should be ignored. */
1252 static int ignore_request(struct wiphy
*wiphy
,
1253 struct regulatory_request
*pending_request
)
1255 struct wiphy
*last_wiphy
= NULL
;
1257 assert_cfg80211_lock();
1259 /* All initial requests are respected */
1263 switch (pending_request
->initiator
) {
1264 case NL80211_REGDOM_SET_BY_CORE
:
1266 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1268 last_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1270 if (unlikely(!is_an_alpha2(pending_request
->alpha2
)))
1272 if (last_request
->initiator
==
1273 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1274 if (last_wiphy
!= wiphy
) {
1276 * Two cards with two APs claiming different
1277 * Country IE alpha2s. We could
1278 * intersect them, but that seems unlikely
1279 * to be correct. Reject second one for now.
1281 if (regdom_changes(pending_request
->alpha2
))
1286 * Two consecutive Country IE hints on the same wiphy.
1287 * This should be picked up early by the driver/stack
1289 if (WARN_ON(regdom_changes(pending_request
->alpha2
)))
1294 case NL80211_REGDOM_SET_BY_DRIVER
:
1295 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1296 if (regdom_changes(pending_request
->alpha2
))
1302 * This would happen if you unplug and plug your card
1303 * back in or if you add a new device for which the previously
1304 * loaded card also agrees on the regulatory domain.
1306 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1307 !regdom_changes(pending_request
->alpha2
))
1310 return REG_INTERSECT
;
1311 case NL80211_REGDOM_SET_BY_USER
:
1312 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1313 return REG_INTERSECT
;
1315 * If the user knows better the user should set the regdom
1316 * to their country before the IE is picked up
1318 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1319 last_request
->intersect
)
1322 * Process user requests only after previous user/driver/core
1323 * requests have been processed
1325 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1326 last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1327 last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1328 if (regdom_changes(last_request
->alpha2
))
1332 if (!regdom_changes(pending_request
->alpha2
))
1341 static void reg_set_request_processed(void)
1343 bool need_more_processing
= false;
1345 last_request
->processed
= true;
1347 spin_lock(®_requests_lock
);
1348 if (!list_empty(®_requests_list
))
1349 need_more_processing
= true;
1350 spin_unlock(®_requests_lock
);
1352 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
)
1353 cancel_delayed_work_sync(®_timeout
);
1355 if (need_more_processing
)
1356 schedule_work(®_work
);
1360 * __regulatory_hint - hint to the wireless core a regulatory domain
1361 * @wiphy: if the hint comes from country information from an AP, this
1362 * is required to be set to the wiphy that received the information
1363 * @pending_request: the regulatory request currently being processed
1365 * The Wireless subsystem can use this function to hint to the wireless core
1366 * what it believes should be the current regulatory domain.
1368 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1369 * already been set or other standard error codes.
1371 * Caller must hold &cfg80211_mutex and ®_mutex
1373 static int __regulatory_hint(struct wiphy
*wiphy
,
1374 struct regulatory_request
*pending_request
)
1376 bool intersect
= false;
1379 assert_cfg80211_lock();
1381 r
= ignore_request(wiphy
, pending_request
);
1383 if (r
== REG_INTERSECT
) {
1384 if (pending_request
->initiator
==
1385 NL80211_REGDOM_SET_BY_DRIVER
) {
1386 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1388 kfree(pending_request
);
1395 * If the regulatory domain being requested by the
1396 * driver has already been set just copy it to the
1399 if (r
== -EALREADY
&&
1400 pending_request
->initiator
==
1401 NL80211_REGDOM_SET_BY_DRIVER
) {
1402 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1404 kfree(pending_request
);
1410 kfree(pending_request
);
1415 if (last_request
!= &core_request_world
)
1416 kfree(last_request
);
1418 last_request
= pending_request
;
1419 last_request
->intersect
= intersect
;
1421 pending_request
= NULL
;
1423 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1424 user_alpha2
[0] = last_request
->alpha2
[0];
1425 user_alpha2
[1] = last_request
->alpha2
[1];
1428 /* When r == REG_INTERSECT we do need to call CRDA */
1431 * Since CRDA will not be called in this case as we already
1432 * have applied the requested regulatory domain before we just
1433 * inform userspace we have processed the request
1435 if (r
== -EALREADY
) {
1436 nl80211_send_reg_change_event(last_request
);
1437 reg_set_request_processed();
1442 return call_crda(last_request
->alpha2
);
1445 /* This processes *all* regulatory hints */
1446 static void reg_process_hint(struct regulatory_request
*reg_request
)
1449 struct wiphy
*wiphy
= NULL
;
1450 enum nl80211_reg_initiator initiator
= reg_request
->initiator
;
1452 BUG_ON(!reg_request
->alpha2
);
1454 if (wiphy_idx_valid(reg_request
->wiphy_idx
))
1455 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1457 if (reg_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1463 r
= __regulatory_hint(wiphy
, reg_request
);
1464 /* This is required so that the orig_* parameters are saved */
1465 if (r
== -EALREADY
&& wiphy
&&
1466 wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
) {
1467 wiphy_update_regulatory(wiphy
, initiator
);
1472 * We only time out user hints, given that they should be the only
1473 * source of bogus requests.
1475 if (r
!= -EALREADY
&&
1476 reg_request
->initiator
== NL80211_REGDOM_SET_BY_USER
)
1477 schedule_delayed_work(®_timeout
, msecs_to_jiffies(3142));
1481 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1482 * Regulatory hints come on a first come first serve basis and we
1483 * must process each one atomically.
1485 static void reg_process_pending_hints(void)
1487 struct regulatory_request
*reg_request
;
1489 mutex_lock(&cfg80211_mutex
);
1490 mutex_lock(®_mutex
);
1492 /* When last_request->processed becomes true this will be rescheduled */
1493 if (last_request
&& !last_request
->processed
) {
1494 REG_DBG_PRINT("Pending regulatory request, waiting "
1495 "for it to be processed...");
1499 spin_lock(®_requests_lock
);
1501 if (list_empty(®_requests_list
)) {
1502 spin_unlock(®_requests_lock
);
1506 reg_request
= list_first_entry(®_requests_list
,
1507 struct regulatory_request
,
1509 list_del_init(®_request
->list
);
1511 spin_unlock(®_requests_lock
);
1513 reg_process_hint(reg_request
);
1516 mutex_unlock(®_mutex
);
1517 mutex_unlock(&cfg80211_mutex
);
1520 /* Processes beacon hints -- this has nothing to do with country IEs */
1521 static void reg_process_pending_beacon_hints(void)
1523 struct cfg80211_registered_device
*rdev
;
1524 struct reg_beacon
*pending_beacon
, *tmp
;
1527 * No need to hold the reg_mutex here as we just touch wiphys
1528 * and do not read or access regulatory variables.
1530 mutex_lock(&cfg80211_mutex
);
1532 /* This goes through the _pending_ beacon list */
1533 spin_lock_bh(®_pending_beacons_lock
);
1535 if (list_empty(®_pending_beacons
)) {
1536 spin_unlock_bh(®_pending_beacons_lock
);
1540 list_for_each_entry_safe(pending_beacon
, tmp
,
1541 ®_pending_beacons
, list
) {
1543 list_del_init(&pending_beacon
->list
);
1545 /* Applies the beacon hint to current wiphys */
1546 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1547 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1549 /* Remembers the beacon hint for new wiphys or reg changes */
1550 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1553 spin_unlock_bh(®_pending_beacons_lock
);
1555 mutex_unlock(&cfg80211_mutex
);
1558 static void reg_todo(struct work_struct
*work
)
1560 reg_process_pending_hints();
1561 reg_process_pending_beacon_hints();
1564 static void queue_regulatory_request(struct regulatory_request
*request
)
1566 if (isalpha(request
->alpha2
[0]))
1567 request
->alpha2
[0] = toupper(request
->alpha2
[0]);
1568 if (isalpha(request
->alpha2
[1]))
1569 request
->alpha2
[1] = toupper(request
->alpha2
[1]);
1571 spin_lock(®_requests_lock
);
1572 list_add_tail(&request
->list
, ®_requests_list
);
1573 spin_unlock(®_requests_lock
);
1575 schedule_work(®_work
);
1579 * Core regulatory hint -- happens during cfg80211_init()
1580 * and when we restore regulatory settings.
1582 static int regulatory_hint_core(const char *alpha2
)
1584 struct regulatory_request
*request
;
1586 request
= kzalloc(sizeof(struct regulatory_request
),
1591 request
->alpha2
[0] = alpha2
[0];
1592 request
->alpha2
[1] = alpha2
[1];
1593 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1595 queue_regulatory_request(request
);
1601 int regulatory_hint_user(const char *alpha2
)
1603 struct regulatory_request
*request
;
1607 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1611 request
->wiphy_idx
= WIPHY_IDX_STALE
;
1612 request
->alpha2
[0] = alpha2
[0];
1613 request
->alpha2
[1] = alpha2
[1];
1614 request
->initiator
= NL80211_REGDOM_SET_BY_USER
;
1616 queue_regulatory_request(request
);
1622 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1624 struct regulatory_request
*request
;
1629 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1633 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1635 /* Must have registered wiphy first */
1636 BUG_ON(!wiphy_idx_valid(request
->wiphy_idx
));
1638 request
->alpha2
[0] = alpha2
[0];
1639 request
->alpha2
[1] = alpha2
[1];
1640 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
1642 queue_regulatory_request(request
);
1646 EXPORT_SYMBOL(regulatory_hint
);
1649 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1650 * therefore cannot iterate over the rdev list here.
1652 void regulatory_hint_11d(struct wiphy
*wiphy
,
1653 enum ieee80211_band band
,
1658 enum environment_cap env
= ENVIRON_ANY
;
1659 struct regulatory_request
*request
;
1661 mutex_lock(®_mutex
);
1663 if (unlikely(!last_request
))
1666 /* IE len must be evenly divisible by 2 */
1667 if (country_ie_len
& 0x01)
1670 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
1673 alpha2
[0] = country_ie
[0];
1674 alpha2
[1] = country_ie
[1];
1676 if (country_ie
[2] == 'I')
1677 env
= ENVIRON_INDOOR
;
1678 else if (country_ie
[2] == 'O')
1679 env
= ENVIRON_OUTDOOR
;
1682 * We will run this only upon a successful connection on cfg80211.
1683 * We leave conflict resolution to the workqueue, where can hold
1686 if (likely(last_request
->initiator
==
1687 NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1688 wiphy_idx_valid(last_request
->wiphy_idx
)))
1691 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1695 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1696 request
->alpha2
[0] = alpha2
[0];
1697 request
->alpha2
[1] = alpha2
[1];
1698 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
1699 request
->country_ie_env
= env
;
1701 mutex_unlock(®_mutex
);
1703 queue_regulatory_request(request
);
1708 mutex_unlock(®_mutex
);
1711 static void restore_alpha2(char *alpha2
, bool reset_user
)
1713 /* indicates there is no alpha2 to consider for restoration */
1717 /* The user setting has precedence over the module parameter */
1718 if (is_user_regdom_saved()) {
1719 /* Unless we're asked to ignore it and reset it */
1721 REG_DBG_PRINT("Restoring regulatory settings "
1722 "including user preference\n");
1723 user_alpha2
[0] = '9';
1724 user_alpha2
[1] = '7';
1727 * If we're ignoring user settings, we still need to
1728 * check the module parameter to ensure we put things
1729 * back as they were for a full restore.
1731 if (!is_world_regdom(ieee80211_regdom
)) {
1732 REG_DBG_PRINT("Keeping preference on "
1733 "module parameter ieee80211_regdom: %c%c\n",
1734 ieee80211_regdom
[0],
1735 ieee80211_regdom
[1]);
1736 alpha2
[0] = ieee80211_regdom
[0];
1737 alpha2
[1] = ieee80211_regdom
[1];
1740 REG_DBG_PRINT("Restoring regulatory settings "
1741 "while preserving user preference for: %c%c\n",
1744 alpha2
[0] = user_alpha2
[0];
1745 alpha2
[1] = user_alpha2
[1];
1747 } else if (!is_world_regdom(ieee80211_regdom
)) {
1748 REG_DBG_PRINT("Keeping preference on "
1749 "module parameter ieee80211_regdom: %c%c\n",
1750 ieee80211_regdom
[0],
1751 ieee80211_regdom
[1]);
1752 alpha2
[0] = ieee80211_regdom
[0];
1753 alpha2
[1] = ieee80211_regdom
[1];
1755 REG_DBG_PRINT("Restoring regulatory settings\n");
1759 * Restoring regulatory settings involves ingoring any
1760 * possibly stale country IE information and user regulatory
1761 * settings if so desired, this includes any beacon hints
1762 * learned as we could have traveled outside to another country
1763 * after disconnection. To restore regulatory settings we do
1764 * exactly what we did at bootup:
1766 * - send a core regulatory hint
1767 * - send a user regulatory hint if applicable
1769 * Device drivers that send a regulatory hint for a specific country
1770 * keep their own regulatory domain on wiphy->regd so that does does
1771 * not need to be remembered.
1773 static void restore_regulatory_settings(bool reset_user
)
1776 struct reg_beacon
*reg_beacon
, *btmp
;
1777 struct regulatory_request
*reg_request
, *tmp
;
1778 LIST_HEAD(tmp_reg_req_list
);
1780 mutex_lock(&cfg80211_mutex
);
1781 mutex_lock(®_mutex
);
1783 reset_regdomains(true);
1784 restore_alpha2(alpha2
, reset_user
);
1787 * If there's any pending requests we simply
1788 * stash them to a temporary pending queue and
1789 * add then after we've restored regulatory
1792 spin_lock(®_requests_lock
);
1793 if (!list_empty(®_requests_list
)) {
1794 list_for_each_entry_safe(reg_request
, tmp
,
1795 ®_requests_list
, list
) {
1796 if (reg_request
->initiator
!=
1797 NL80211_REGDOM_SET_BY_USER
)
1799 list_del(®_request
->list
);
1800 list_add_tail(®_request
->list
, &tmp_reg_req_list
);
1803 spin_unlock(®_requests_lock
);
1805 /* Clear beacon hints */
1806 spin_lock_bh(®_pending_beacons_lock
);
1807 if (!list_empty(®_pending_beacons
)) {
1808 list_for_each_entry_safe(reg_beacon
, btmp
,
1809 ®_pending_beacons
, list
) {
1810 list_del(®_beacon
->list
);
1814 spin_unlock_bh(®_pending_beacons_lock
);
1816 if (!list_empty(®_beacon_list
)) {
1817 list_for_each_entry_safe(reg_beacon
, btmp
,
1818 ®_beacon_list
, list
) {
1819 list_del(®_beacon
->list
);
1824 /* First restore to the basic regulatory settings */
1825 cfg80211_regdomain
= cfg80211_world_regdom
;
1827 mutex_unlock(®_mutex
);
1828 mutex_unlock(&cfg80211_mutex
);
1830 regulatory_hint_core(cfg80211_regdomain
->alpha2
);
1833 * This restores the ieee80211_regdom module parameter
1834 * preference or the last user requested regulatory
1835 * settings, user regulatory settings takes precedence.
1837 if (is_an_alpha2(alpha2
))
1838 regulatory_hint_user(user_alpha2
);
1840 if (list_empty(&tmp_reg_req_list
))
1843 mutex_lock(&cfg80211_mutex
);
1844 mutex_lock(®_mutex
);
1846 spin_lock(®_requests_lock
);
1847 list_for_each_entry_safe(reg_request
, tmp
, &tmp_reg_req_list
, list
) {
1848 REG_DBG_PRINT("Adding request for country %c%c back "
1850 reg_request
->alpha2
[0],
1851 reg_request
->alpha2
[1]);
1852 list_del(®_request
->list
);
1853 list_add_tail(®_request
->list
, ®_requests_list
);
1855 spin_unlock(®_requests_lock
);
1857 mutex_unlock(®_mutex
);
1858 mutex_unlock(&cfg80211_mutex
);
1860 REG_DBG_PRINT("Kicking the queue\n");
1862 schedule_work(®_work
);
1865 void regulatory_hint_disconnect(void)
1867 REG_DBG_PRINT("All devices are disconnected, going to "
1868 "restore regulatory settings\n");
1869 restore_regulatory_settings(false);
1872 static bool freq_is_chan_12_13_14(u16 freq
)
1874 if (freq
== ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ
) ||
1875 freq
== ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ
) ||
1876 freq
== ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ
))
1881 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
1882 struct ieee80211_channel
*beacon_chan
,
1885 struct reg_beacon
*reg_beacon
;
1887 if (likely((beacon_chan
->beacon_found
||
1888 (beacon_chan
->flags
& IEEE80211_CHAN_RADAR
) ||
1889 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
1890 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))))
1893 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
1897 REG_DBG_PRINT("Found new beacon on "
1898 "frequency: %d MHz (Ch %d) on %s\n",
1899 beacon_chan
->center_freq
,
1900 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
1903 memcpy(®_beacon
->chan
, beacon_chan
,
1904 sizeof(struct ieee80211_channel
));
1908 * Since we can be called from BH or and non-BH context
1909 * we must use spin_lock_bh()
1911 spin_lock_bh(®_pending_beacons_lock
);
1912 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
1913 spin_unlock_bh(®_pending_beacons_lock
);
1915 schedule_work(®_work
);
1920 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
1923 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1924 const struct ieee80211_freq_range
*freq_range
= NULL
;
1925 const struct ieee80211_power_rule
*power_rule
= NULL
;
1927 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
1929 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
1930 reg_rule
= &rd
->reg_rules
[i
];
1931 freq_range
= ®_rule
->freq_range
;
1932 power_rule
= ®_rule
->power_rule
;
1935 * There may not be documentation for max antenna gain
1936 * in certain regions
1938 if (power_rule
->max_antenna_gain
)
1939 pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
1940 freq_range
->start_freq_khz
,
1941 freq_range
->end_freq_khz
,
1942 freq_range
->max_bandwidth_khz
,
1943 power_rule
->max_antenna_gain
,
1944 power_rule
->max_eirp
);
1946 pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
1947 freq_range
->start_freq_khz
,
1948 freq_range
->end_freq_khz
,
1949 freq_range
->max_bandwidth_khz
,
1950 power_rule
->max_eirp
);
1954 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
1957 if (is_intersected_alpha2(rd
->alpha2
)) {
1959 if (last_request
->initiator
==
1960 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1961 struct cfg80211_registered_device
*rdev
;
1962 rdev
= cfg80211_rdev_by_wiphy_idx(
1963 last_request
->wiphy_idx
);
1965 pr_info("Current regulatory domain updated by AP to: %c%c\n",
1966 rdev
->country_ie_alpha2
[0],
1967 rdev
->country_ie_alpha2
[1]);
1969 pr_info("Current regulatory domain intersected:\n");
1971 pr_info("Current regulatory domain intersected:\n");
1972 } else if (is_world_regdom(rd
->alpha2
))
1973 pr_info("World regulatory domain updated:\n");
1975 if (is_unknown_alpha2(rd
->alpha2
))
1976 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
1978 pr_info("Regulatory domain changed to country: %c%c\n",
1979 rd
->alpha2
[0], rd
->alpha2
[1]);
1984 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
1986 pr_info("Regulatory domain: %c%c\n", rd
->alpha2
[0], rd
->alpha2
[1]);
1990 /* Takes ownership of rd only if it doesn't fail */
1991 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
1993 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
1994 struct cfg80211_registered_device
*rdev
= NULL
;
1995 struct wiphy
*request_wiphy
;
1996 /* Some basic sanity checks first */
1998 if (is_world_regdom(rd
->alpha2
)) {
1999 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2001 update_world_regdomain(rd
);
2005 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2006 !is_unknown_alpha2(rd
->alpha2
))
2013 * Lets only bother proceeding on the same alpha2 if the current
2014 * rd is non static (it means CRDA was present and was used last)
2015 * and the pending request came in from a country IE
2017 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2019 * If someone else asked us to change the rd lets only bother
2020 * checking if the alpha2 changes if CRDA was already called
2022 if (!regdom_changes(rd
->alpha2
))
2027 * Now lets set the regulatory domain, update all driver channels
2028 * and finally inform them of what we have done, in case they want
2029 * to review or adjust their own settings based on their own
2030 * internal EEPROM data
2033 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2036 if (!is_valid_rd(rd
)) {
2037 pr_err("Invalid regulatory domain detected:\n");
2038 print_regdomain_info(rd
);
2042 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2043 if (!request_wiphy
&&
2044 (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
2045 last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)) {
2046 schedule_delayed_work(®_timeout
, 0);
2050 if (!last_request
->intersect
) {
2053 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_DRIVER
) {
2054 reset_regdomains(false);
2055 cfg80211_regdomain
= rd
;
2060 * For a driver hint, lets copy the regulatory domain the
2061 * driver wanted to the wiphy to deal with conflicts
2065 * Userspace could have sent two replies with only
2066 * one kernel request.
2068 if (request_wiphy
->regd
)
2071 r
= reg_copy_regd(&request_wiphy
->regd
, rd
);
2075 reset_regdomains(false);
2076 cfg80211_regdomain
= rd
;
2080 /* Intersection requires a bit more work */
2082 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2084 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
2085 if (!intersected_rd
)
2089 * We can trash what CRDA provided now.
2090 * However if a driver requested this specific regulatory
2091 * domain we keep it for its private use
2093 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
)
2094 request_wiphy
->regd
= rd
;
2100 reset_regdomains(false);
2101 cfg80211_regdomain
= intersected_rd
;
2106 if (!intersected_rd
)
2109 rdev
= wiphy_to_dev(request_wiphy
);
2111 rdev
->country_ie_alpha2
[0] = rd
->alpha2
[0];
2112 rdev
->country_ie_alpha2
[1] = rd
->alpha2
[1];
2113 rdev
->env
= last_request
->country_ie_env
;
2115 BUG_ON(intersected_rd
== rd
);
2120 reset_regdomains(false);
2121 cfg80211_regdomain
= intersected_rd
;
2128 * Use this call to set the current regulatory domain. Conflicts with
2129 * multiple drivers can be ironed out later. Caller must've already
2130 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2132 int set_regdom(const struct ieee80211_regdomain
*rd
)
2136 assert_cfg80211_lock();
2138 mutex_lock(®_mutex
);
2140 /* Note that this doesn't update the wiphys, this is done below */
2141 r
= __set_regdom(rd
);
2144 mutex_unlock(®_mutex
);
2148 /* This would make this whole thing pointless */
2149 if (!last_request
->intersect
)
2150 BUG_ON(rd
!= cfg80211_regdomain
);
2152 /* update all wiphys now with the new established regulatory domain */
2153 update_all_wiphy_regulatory(last_request
->initiator
);
2155 print_regdomain(cfg80211_regdomain
);
2157 nl80211_send_reg_change_event(last_request
);
2159 reg_set_request_processed();
2161 mutex_unlock(®_mutex
);
2166 #ifdef CONFIG_HOTPLUG
2167 int reg_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
2169 if (last_request
&& !last_request
->processed
) {
2170 if (add_uevent_var(env
, "COUNTRY=%c%c",
2171 last_request
->alpha2
[0],
2172 last_request
->alpha2
[1]))
2179 int reg_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
2183 #endif /* CONFIG_HOTPLUG */
2185 /* Caller must hold cfg80211_mutex */
2186 void reg_device_remove(struct wiphy
*wiphy
)
2188 struct wiphy
*request_wiphy
= NULL
;
2190 assert_cfg80211_lock();
2192 mutex_lock(®_mutex
);
2197 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2199 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2202 last_request
->wiphy_idx
= WIPHY_IDX_STALE
;
2203 last_request
->country_ie_env
= ENVIRON_ANY
;
2205 mutex_unlock(®_mutex
);
2208 static void reg_timeout_work(struct work_struct
*work
)
2210 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, "
2211 "restoring regulatory settings");
2212 restore_regulatory_settings(true);
2215 int __init
regulatory_init(void)
2219 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2220 if (IS_ERR(reg_pdev
))
2221 return PTR_ERR(reg_pdev
);
2223 reg_pdev
->dev
.type
= ®_device_type
;
2225 spin_lock_init(®_requests_lock
);
2226 spin_lock_init(®_pending_beacons_lock
);
2228 cfg80211_regdomain
= cfg80211_world_regdom
;
2230 user_alpha2
[0] = '9';
2231 user_alpha2
[1] = '7';
2233 /* We always try to get an update for the static regdomain */
2234 err
= regulatory_hint_core(cfg80211_regdomain
->alpha2
);
2239 * N.B. kobject_uevent_env() can fail mainly for when we're out
2240 * memory which is handled and propagated appropriately above
2241 * but it can also fail during a netlink_broadcast() or during
2242 * early boot for call_usermodehelper(). For now treat these
2243 * errors as non-fatal.
2245 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
2246 #ifdef CONFIG_CFG80211_REG_DEBUG
2247 /* We want to find out exactly why when debugging */
2253 * Finally, if the user set the module parameter treat it
2256 if (!is_world_regdom(ieee80211_regdom
))
2257 regulatory_hint_user(ieee80211_regdom
);
2262 void /* __init_or_exit */ regulatory_exit(void)
2264 struct regulatory_request
*reg_request
, *tmp
;
2265 struct reg_beacon
*reg_beacon
, *btmp
;
2267 cancel_work_sync(®_work
);
2268 cancel_delayed_work_sync(®_timeout
);
2270 mutex_lock(&cfg80211_mutex
);
2271 mutex_lock(®_mutex
);
2273 reset_regdomains(true);
2275 dev_set_uevent_suppress(®_pdev
->dev
, true);
2277 platform_device_unregister(reg_pdev
);
2279 spin_lock_bh(®_pending_beacons_lock
);
2280 if (!list_empty(®_pending_beacons
)) {
2281 list_for_each_entry_safe(reg_beacon
, btmp
,
2282 ®_pending_beacons
, list
) {
2283 list_del(®_beacon
->list
);
2287 spin_unlock_bh(®_pending_beacons_lock
);
2289 if (!list_empty(®_beacon_list
)) {
2290 list_for_each_entry_safe(reg_beacon
, btmp
,
2291 ®_beacon_list
, list
) {
2292 list_del(®_beacon
->list
);
2297 spin_lock(®_requests_lock
);
2298 if (!list_empty(®_requests_list
)) {
2299 list_for_each_entry_safe(reg_request
, tmp
,
2300 ®_requests_list
, list
) {
2301 list_del(®_request
->list
);
2305 spin_unlock(®_requests_lock
);
2307 mutex_unlock(®_mutex
);
2308 mutex_unlock(&cfg80211_mutex
);