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 /* Feel free to add any other sanity checks here */
384 static void reg_regdb_size_check(void)
386 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
387 WARN_ONCE(!reg_regdb_size
, "db.txt is empty, you should update it...");
390 static inline void reg_regdb_size_check(void) {}
391 static inline void reg_regdb_query(const char *alpha2
) {}
392 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
395 * This lets us keep regulatory code which is updated on a regulatory
396 * basis in userspace. Country information is filled in by
399 static int call_crda(const char *alpha2
)
401 if (!is_world_regdom((char *) alpha2
))
402 pr_info("Calling CRDA for country: %c%c\n",
403 alpha2
[0], alpha2
[1]);
405 pr_info("Calling CRDA to update world regulatory domain\n");
407 /* query internal regulatory database (if it exists) */
408 reg_regdb_query(alpha2
);
410 return kobject_uevent(®_pdev
->dev
.kobj
, KOBJ_CHANGE
);
413 /* Used by nl80211 before kmalloc'ing our regulatory domain */
414 bool reg_is_valid_request(const char *alpha2
)
416 assert_cfg80211_lock();
421 return alpha2_equal(last_request
->alpha2
, alpha2
);
424 /* Sanity check on a regulatory rule */
425 static bool is_valid_reg_rule(const struct ieee80211_reg_rule
*rule
)
427 const struct ieee80211_freq_range
*freq_range
= &rule
->freq_range
;
430 if (freq_range
->start_freq_khz
<= 0 || freq_range
->end_freq_khz
<= 0)
433 if (freq_range
->start_freq_khz
> freq_range
->end_freq_khz
)
436 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
438 if (freq_range
->end_freq_khz
<= freq_range
->start_freq_khz
||
439 freq_range
->max_bandwidth_khz
> freq_diff
)
445 static bool is_valid_rd(const struct ieee80211_regdomain
*rd
)
447 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
450 if (!rd
->n_reg_rules
)
453 if (WARN_ON(rd
->n_reg_rules
> NL80211_MAX_SUPP_REG_RULES
))
456 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
457 reg_rule
= &rd
->reg_rules
[i
];
458 if (!is_valid_reg_rule(reg_rule
))
465 static bool reg_does_bw_fit(const struct ieee80211_freq_range
*freq_range
,
469 u32 start_freq_khz
, end_freq_khz
;
471 start_freq_khz
= center_freq_khz
- (bw_khz
/2);
472 end_freq_khz
= center_freq_khz
+ (bw_khz
/2);
474 if (start_freq_khz
>= freq_range
->start_freq_khz
&&
475 end_freq_khz
<= freq_range
->end_freq_khz
)
482 * freq_in_rule_band - tells us if a frequency is in a frequency band
483 * @freq_range: frequency rule we want to query
484 * @freq_khz: frequency we are inquiring about
486 * This lets us know if a specific frequency rule is or is not relevant to
487 * a specific frequency's band. Bands are device specific and artificial
488 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
489 * safe for now to assume that a frequency rule should not be part of a
490 * frequency's band if the start freq or end freq are off by more than 2 GHz.
491 * This resolution can be lowered and should be considered as we add
492 * regulatory rule support for other "bands".
494 static bool freq_in_rule_band(const struct ieee80211_freq_range
*freq_range
,
497 #define ONE_GHZ_IN_KHZ 1000000
498 if (abs(freq_khz
- freq_range
->start_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
500 if (abs(freq_khz
- freq_range
->end_freq_khz
) <= (2 * ONE_GHZ_IN_KHZ
))
503 #undef ONE_GHZ_IN_KHZ
507 * Helper for regdom_intersect(), this does the real
508 * mathematical intersection fun
510 static int reg_rules_intersect(
511 const struct ieee80211_reg_rule
*rule1
,
512 const struct ieee80211_reg_rule
*rule2
,
513 struct ieee80211_reg_rule
*intersected_rule
)
515 const struct ieee80211_freq_range
*freq_range1
, *freq_range2
;
516 struct ieee80211_freq_range
*freq_range
;
517 const struct ieee80211_power_rule
*power_rule1
, *power_rule2
;
518 struct ieee80211_power_rule
*power_rule
;
521 freq_range1
= &rule1
->freq_range
;
522 freq_range2
= &rule2
->freq_range
;
523 freq_range
= &intersected_rule
->freq_range
;
525 power_rule1
= &rule1
->power_rule
;
526 power_rule2
= &rule2
->power_rule
;
527 power_rule
= &intersected_rule
->power_rule
;
529 freq_range
->start_freq_khz
= max(freq_range1
->start_freq_khz
,
530 freq_range2
->start_freq_khz
);
531 freq_range
->end_freq_khz
= min(freq_range1
->end_freq_khz
,
532 freq_range2
->end_freq_khz
);
533 freq_range
->max_bandwidth_khz
= min(freq_range1
->max_bandwidth_khz
,
534 freq_range2
->max_bandwidth_khz
);
536 freq_diff
= freq_range
->end_freq_khz
- freq_range
->start_freq_khz
;
537 if (freq_range
->max_bandwidth_khz
> freq_diff
)
538 freq_range
->max_bandwidth_khz
= freq_diff
;
540 power_rule
->max_eirp
= min(power_rule1
->max_eirp
,
541 power_rule2
->max_eirp
);
542 power_rule
->max_antenna_gain
= min(power_rule1
->max_antenna_gain
,
543 power_rule2
->max_antenna_gain
);
545 intersected_rule
->flags
= (rule1
->flags
| rule2
->flags
);
547 if (!is_valid_reg_rule(intersected_rule
))
554 * regdom_intersect - do the intersection between two regulatory domains
555 * @rd1: first regulatory domain
556 * @rd2: second regulatory domain
558 * Use this function to get the intersection between two regulatory domains.
559 * Once completed we will mark the alpha2 for the rd as intersected, "98",
560 * as no one single alpha2 can represent this regulatory domain.
562 * Returns a pointer to the regulatory domain structure which will hold the
563 * resulting intersection of rules between rd1 and rd2. We will
564 * kzalloc() this structure for you.
566 static struct ieee80211_regdomain
*regdom_intersect(
567 const struct ieee80211_regdomain
*rd1
,
568 const struct ieee80211_regdomain
*rd2
)
572 unsigned int num_rules
= 0, rule_idx
= 0;
573 const struct ieee80211_reg_rule
*rule1
, *rule2
;
574 struct ieee80211_reg_rule
*intersected_rule
;
575 struct ieee80211_regdomain
*rd
;
576 /* This is just a dummy holder to help us count */
577 struct ieee80211_reg_rule irule
;
579 /* Uses the stack temporarily for counter arithmetic */
580 intersected_rule
= &irule
;
582 memset(intersected_rule
, 0, sizeof(struct ieee80211_reg_rule
));
588 * First we get a count of the rules we'll need, then we actually
589 * build them. This is to so we can malloc() and free() a
590 * regdomain once. The reason we use reg_rules_intersect() here
591 * is it will return -EINVAL if the rule computed makes no sense.
592 * All rules that do check out OK are valid.
595 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
596 rule1
= &rd1
->reg_rules
[x
];
597 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
598 rule2
= &rd2
->reg_rules
[y
];
599 if (!reg_rules_intersect(rule1
, rule2
,
602 memset(intersected_rule
, 0,
603 sizeof(struct ieee80211_reg_rule
));
610 size_of_regd
= sizeof(struct ieee80211_regdomain
) +
611 ((num_rules
+ 1) * sizeof(struct ieee80211_reg_rule
));
613 rd
= kzalloc(size_of_regd
, GFP_KERNEL
);
617 for (x
= 0; x
< rd1
->n_reg_rules
; x
++) {
618 rule1
= &rd1
->reg_rules
[x
];
619 for (y
= 0; y
< rd2
->n_reg_rules
; y
++) {
620 rule2
= &rd2
->reg_rules
[y
];
622 * This time around instead of using the stack lets
623 * write to the target rule directly saving ourselves
626 intersected_rule
= &rd
->reg_rules
[rule_idx
];
627 r
= reg_rules_intersect(rule1
, rule2
,
630 * No need to memset here the intersected rule here as
631 * we're not using the stack anymore
639 if (rule_idx
!= num_rules
) {
644 rd
->n_reg_rules
= num_rules
;
652 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
653 * want to just have the channel structure use these
655 static u32
map_regdom_flags(u32 rd_flags
)
657 u32 channel_flags
= 0;
658 if (rd_flags
& NL80211_RRF_PASSIVE_SCAN
)
659 channel_flags
|= IEEE80211_CHAN_PASSIVE_SCAN
;
660 if (rd_flags
& NL80211_RRF_NO_IBSS
)
661 channel_flags
|= IEEE80211_CHAN_NO_IBSS
;
662 if (rd_flags
& NL80211_RRF_DFS
)
663 channel_flags
|= IEEE80211_CHAN_RADAR
;
664 return channel_flags
;
667 static int freq_reg_info_regd(struct wiphy
*wiphy
,
670 const struct ieee80211_reg_rule
**reg_rule
,
671 const struct ieee80211_regdomain
*custom_regd
)
674 bool band_rule_found
= false;
675 const struct ieee80211_regdomain
*regd
;
676 bool bw_fits
= false;
679 desired_bw_khz
= MHZ_TO_KHZ(20);
681 regd
= custom_regd
? custom_regd
: cfg80211_regdomain
;
684 * Follow the driver's regulatory domain, if present, unless a country
685 * IE has been processed or a user wants to help complaince further
688 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
689 last_request
->initiator
!= NL80211_REGDOM_SET_BY_USER
&&
696 for (i
= 0; i
< regd
->n_reg_rules
; i
++) {
697 const struct ieee80211_reg_rule
*rr
;
698 const struct ieee80211_freq_range
*fr
= NULL
;
700 rr
= ®d
->reg_rules
[i
];
701 fr
= &rr
->freq_range
;
704 * We only need to know if one frequency rule was
705 * was in center_freq's band, that's enough, so lets
706 * not overwrite it once found
708 if (!band_rule_found
)
709 band_rule_found
= freq_in_rule_band(fr
, center_freq
);
711 bw_fits
= reg_does_bw_fit(fr
,
715 if (band_rule_found
&& bw_fits
) {
721 if (!band_rule_found
)
727 int freq_reg_info(struct wiphy
*wiphy
,
730 const struct ieee80211_reg_rule
**reg_rule
)
732 assert_cfg80211_lock();
733 return freq_reg_info_regd(wiphy
,
739 EXPORT_SYMBOL(freq_reg_info
);
741 #ifdef CONFIG_CFG80211_REG_DEBUG
742 static const char *reg_initiator_name(enum nl80211_reg_initiator initiator
)
745 case NL80211_REGDOM_SET_BY_CORE
:
746 return "Set by core";
747 case NL80211_REGDOM_SET_BY_USER
:
748 return "Set by user";
749 case NL80211_REGDOM_SET_BY_DRIVER
:
750 return "Set by driver";
751 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
752 return "Set by country IE";
759 static void chan_reg_rule_print_dbg(struct ieee80211_channel
*chan
,
761 const struct ieee80211_reg_rule
*reg_rule
)
763 const struct ieee80211_power_rule
*power_rule
;
764 const struct ieee80211_freq_range
*freq_range
;
765 char max_antenna_gain
[32];
767 power_rule
= ®_rule
->power_rule
;
768 freq_range
= ®_rule
->freq_range
;
770 if (!power_rule
->max_antenna_gain
)
771 snprintf(max_antenna_gain
, 32, "N/A");
773 snprintf(max_antenna_gain
, 32, "%d", power_rule
->max_antenna_gain
);
775 REG_DBG_PRINT("Updating information on frequency %d MHz "
776 "for a %d MHz width channel with regulatory rule:\n",
778 KHZ_TO_MHZ(desired_bw_khz
));
780 REG_DBG_PRINT("%d KHz - %d KHz @ KHz), (%s mBi, %d mBm)\n",
781 freq_range
->start_freq_khz
,
782 freq_range
->end_freq_khz
,
784 power_rule
->max_eirp
);
787 static void chan_reg_rule_print_dbg(struct ieee80211_channel
*chan
,
789 const struct ieee80211_reg_rule
*reg_rule
)
796 * Note that right now we assume the desired channel bandwidth
797 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
798 * per channel, the primary and the extension channel). To support
799 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
800 * new ieee80211_channel.target_bw and re run the regulatory check
801 * on the wiphy with the target_bw specified. Then we can simply use
802 * that below for the desired_bw_khz below.
804 static void handle_channel(struct wiphy
*wiphy
,
805 enum nl80211_reg_initiator initiator
,
806 enum ieee80211_band band
,
807 unsigned int chan_idx
)
810 u32 flags
, bw_flags
= 0;
811 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
812 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
813 const struct ieee80211_power_rule
*power_rule
= NULL
;
814 const struct ieee80211_freq_range
*freq_range
= NULL
;
815 struct ieee80211_supported_band
*sband
;
816 struct ieee80211_channel
*chan
;
817 struct wiphy
*request_wiphy
= NULL
;
819 assert_cfg80211_lock();
821 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
823 sband
= wiphy
->bands
[band
];
824 BUG_ON(chan_idx
>= sband
->n_channels
);
825 chan
= &sband
->channels
[chan_idx
];
827 flags
= chan
->orig_flags
;
829 r
= freq_reg_info(wiphy
,
830 MHZ_TO_KHZ(chan
->center_freq
),
836 * We will disable all channels that do not match our
837 * received regulatory rule unless the hint is coming
838 * from a Country IE and the Country IE had no information
839 * about a band. The IEEE 802.11 spec allows for an AP
840 * to send only a subset of the regulatory rules allowed,
841 * so an AP in the US that only supports 2.4 GHz may only send
842 * a country IE with information for the 2.4 GHz band
843 * while 5 GHz is still supported.
845 if (initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
849 REG_DBG_PRINT("Disabling freq %d MHz\n", chan
->center_freq
);
850 chan
->flags
= IEEE80211_CHAN_DISABLED
;
854 chan_reg_rule_print_dbg(chan
, desired_bw_khz
, reg_rule
);
856 power_rule
= ®_rule
->power_rule
;
857 freq_range
= ®_rule
->freq_range
;
859 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
860 bw_flags
= IEEE80211_CHAN_NO_HT40
;
862 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
863 request_wiphy
&& request_wiphy
== wiphy
&&
864 request_wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
) {
866 * This guarantees the driver's requested regulatory domain
867 * will always be used as a base for further regulatory
870 chan
->flags
= chan
->orig_flags
=
871 map_regdom_flags(reg_rule
->flags
) | bw_flags
;
872 chan
->max_antenna_gain
= chan
->orig_mag
=
873 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
874 chan
->max_power
= chan
->orig_mpwr
=
875 (int) MBM_TO_DBM(power_rule
->max_eirp
);
879 chan
->beacon_found
= false;
880 chan
->flags
= flags
| bw_flags
| map_regdom_flags(reg_rule
->flags
);
881 chan
->max_antenna_gain
= min(chan
->orig_mag
,
882 (int) MBI_TO_DBI(power_rule
->max_antenna_gain
));
884 chan
->max_power
= min(chan
->orig_mpwr
,
885 (int) MBM_TO_DBM(power_rule
->max_eirp
));
887 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
890 static void handle_band(struct wiphy
*wiphy
,
891 enum ieee80211_band band
,
892 enum nl80211_reg_initiator initiator
)
895 struct ieee80211_supported_band
*sband
;
897 BUG_ON(!wiphy
->bands
[band
]);
898 sband
= wiphy
->bands
[band
];
900 for (i
= 0; i
< sband
->n_channels
; i
++)
901 handle_channel(wiphy
, initiator
, band
, i
);
904 static bool ignore_reg_update(struct wiphy
*wiphy
,
905 enum nl80211_reg_initiator initiator
)
908 REG_DBG_PRINT("Ignoring regulatory request %s since "
909 "last_request is not set\n",
910 reg_initiator_name(initiator
));
914 if (initiator
== NL80211_REGDOM_SET_BY_CORE
&&
915 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
) {
916 REG_DBG_PRINT("Ignoring regulatory request %s "
917 "since the driver uses its own custom "
918 "regulatory domain ",
919 reg_initiator_name(initiator
));
924 * wiphy->regd will be set once the device has its own
925 * desired regulatory domain set
927 if (wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
&& !wiphy
->regd
&&
928 initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
929 !is_world_regdom(last_request
->alpha2
)) {
930 REG_DBG_PRINT("Ignoring regulatory request %s "
931 "since the driver requires its own regulaotry "
932 "domain to be set first",
933 reg_initiator_name(initiator
));
940 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator
)
942 struct cfg80211_registered_device
*rdev
;
944 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
945 wiphy_update_regulatory(&rdev
->wiphy
, initiator
);
948 static void handle_reg_beacon(struct wiphy
*wiphy
,
949 unsigned int chan_idx
,
950 struct reg_beacon
*reg_beacon
)
952 struct ieee80211_supported_band
*sband
;
953 struct ieee80211_channel
*chan
;
954 bool channel_changed
= false;
955 struct ieee80211_channel chan_before
;
957 assert_cfg80211_lock();
959 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
960 chan
= &sband
->channels
[chan_idx
];
962 if (likely(chan
->center_freq
!= reg_beacon
->chan
.center_freq
))
965 if (chan
->beacon_found
)
968 chan
->beacon_found
= true;
970 if (wiphy
->flags
& WIPHY_FLAG_DISABLE_BEACON_HINTS
)
973 chan_before
.center_freq
= chan
->center_freq
;
974 chan_before
.flags
= chan
->flags
;
976 if (chan
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
) {
977 chan
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
978 channel_changed
= true;
981 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
) {
982 chan
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
983 channel_changed
= true;
987 nl80211_send_beacon_hint_event(wiphy
, &chan_before
, chan
);
991 * Called when a scan on a wiphy finds a beacon on
994 static void wiphy_update_new_beacon(struct wiphy
*wiphy
,
995 struct reg_beacon
*reg_beacon
)
998 struct ieee80211_supported_band
*sband
;
1000 assert_cfg80211_lock();
1002 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1005 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1007 for (i
= 0; i
< sband
->n_channels
; i
++)
1008 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1012 * Called upon reg changes or a new wiphy is added
1014 static void wiphy_update_beacon_reg(struct wiphy
*wiphy
)
1017 struct ieee80211_supported_band
*sband
;
1018 struct reg_beacon
*reg_beacon
;
1020 assert_cfg80211_lock();
1022 if (list_empty(®_beacon_list
))
1025 list_for_each_entry(reg_beacon
, ®_beacon_list
, list
) {
1026 if (!wiphy
->bands
[reg_beacon
->chan
.band
])
1028 sband
= wiphy
->bands
[reg_beacon
->chan
.band
];
1029 for (i
= 0; i
< sband
->n_channels
; i
++)
1030 handle_reg_beacon(wiphy
, i
, reg_beacon
);
1034 static bool reg_is_world_roaming(struct wiphy
*wiphy
)
1036 if (is_world_regdom(cfg80211_regdomain
->alpha2
) ||
1037 (wiphy
->regd
&& is_world_regdom(wiphy
->regd
->alpha2
)))
1040 last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1041 wiphy
->flags
& WIPHY_FLAG_CUSTOM_REGULATORY
)
1046 /* Reap the advantages of previously found beacons */
1047 static void reg_process_beacons(struct wiphy
*wiphy
)
1050 * Means we are just firing up cfg80211, so no beacons would
1051 * have been processed yet.
1055 if (!reg_is_world_roaming(wiphy
))
1057 wiphy_update_beacon_reg(wiphy
);
1060 static bool is_ht40_not_allowed(struct ieee80211_channel
*chan
)
1064 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
1066 /* This would happen when regulatory rules disallow HT40 completely */
1067 if (IEEE80211_CHAN_NO_HT40
== (chan
->flags
& (IEEE80211_CHAN_NO_HT40
)))
1072 static void reg_process_ht_flags_channel(struct wiphy
*wiphy
,
1073 enum ieee80211_band band
,
1074 unsigned int chan_idx
)
1076 struct ieee80211_supported_band
*sband
;
1077 struct ieee80211_channel
*channel
;
1078 struct ieee80211_channel
*channel_before
= NULL
, *channel_after
= NULL
;
1081 assert_cfg80211_lock();
1083 sband
= wiphy
->bands
[band
];
1084 BUG_ON(chan_idx
>= sband
->n_channels
);
1085 channel
= &sband
->channels
[chan_idx
];
1087 if (is_ht40_not_allowed(channel
)) {
1088 channel
->flags
|= IEEE80211_CHAN_NO_HT40
;
1093 * We need to ensure the extension channels exist to
1094 * be able to use HT40- or HT40+, this finds them (or not)
1096 for (i
= 0; i
< sband
->n_channels
; i
++) {
1097 struct ieee80211_channel
*c
= &sband
->channels
[i
];
1098 if (c
->center_freq
== (channel
->center_freq
- 20))
1100 if (c
->center_freq
== (channel
->center_freq
+ 20))
1105 * Please note that this assumes target bandwidth is 20 MHz,
1106 * if that ever changes we also need to change the below logic
1107 * to include that as well.
1109 if (is_ht40_not_allowed(channel_before
))
1110 channel
->flags
|= IEEE80211_CHAN_NO_HT40MINUS
;
1112 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40MINUS
;
1114 if (is_ht40_not_allowed(channel_after
))
1115 channel
->flags
|= IEEE80211_CHAN_NO_HT40PLUS
;
1117 channel
->flags
&= ~IEEE80211_CHAN_NO_HT40PLUS
;
1120 static void reg_process_ht_flags_band(struct wiphy
*wiphy
,
1121 enum ieee80211_band band
)
1124 struct ieee80211_supported_band
*sband
;
1126 BUG_ON(!wiphy
->bands
[band
]);
1127 sband
= wiphy
->bands
[band
];
1129 for (i
= 0; i
< sband
->n_channels
; i
++)
1130 reg_process_ht_flags_channel(wiphy
, band
, i
);
1133 static void reg_process_ht_flags(struct wiphy
*wiphy
)
1135 enum ieee80211_band band
;
1140 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1141 if (wiphy
->bands
[band
])
1142 reg_process_ht_flags_band(wiphy
, band
);
1147 void wiphy_update_regulatory(struct wiphy
*wiphy
,
1148 enum nl80211_reg_initiator initiator
)
1150 enum ieee80211_band band
;
1152 if (ignore_reg_update(wiphy
, initiator
))
1155 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1156 if (wiphy
->bands
[band
])
1157 handle_band(wiphy
, band
, initiator
);
1160 reg_process_beacons(wiphy
);
1161 reg_process_ht_flags(wiphy
);
1162 if (wiphy
->reg_notifier
)
1163 wiphy
->reg_notifier(wiphy
, last_request
);
1166 static void handle_channel_custom(struct wiphy
*wiphy
,
1167 enum ieee80211_band band
,
1168 unsigned int chan_idx
,
1169 const struct ieee80211_regdomain
*regd
)
1172 u32 desired_bw_khz
= MHZ_TO_KHZ(20);
1174 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1175 const struct ieee80211_power_rule
*power_rule
= NULL
;
1176 const struct ieee80211_freq_range
*freq_range
= NULL
;
1177 struct ieee80211_supported_band
*sband
;
1178 struct ieee80211_channel
*chan
;
1182 sband
= wiphy
->bands
[band
];
1183 BUG_ON(chan_idx
>= sband
->n_channels
);
1184 chan
= &sband
->channels
[chan_idx
];
1186 r
= freq_reg_info_regd(wiphy
,
1187 MHZ_TO_KHZ(chan
->center_freq
),
1193 REG_DBG_PRINT("Disabling freq %d MHz as custom "
1194 "regd has no rule that fits a %d MHz "
1197 KHZ_TO_MHZ(desired_bw_khz
));
1198 chan
->flags
= IEEE80211_CHAN_DISABLED
;
1202 chan_reg_rule_print_dbg(chan
, desired_bw_khz
, reg_rule
);
1204 power_rule
= ®_rule
->power_rule
;
1205 freq_range
= ®_rule
->freq_range
;
1207 if (freq_range
->max_bandwidth_khz
< MHZ_TO_KHZ(40))
1208 bw_flags
= IEEE80211_CHAN_NO_HT40
;
1210 chan
->flags
|= map_regdom_flags(reg_rule
->flags
) | bw_flags
;
1211 chan
->max_antenna_gain
= (int) MBI_TO_DBI(power_rule
->max_antenna_gain
);
1212 chan
->max_power
= (int) MBM_TO_DBM(power_rule
->max_eirp
);
1215 static void handle_band_custom(struct wiphy
*wiphy
, enum ieee80211_band band
,
1216 const struct ieee80211_regdomain
*regd
)
1219 struct ieee80211_supported_band
*sband
;
1221 BUG_ON(!wiphy
->bands
[band
]);
1222 sband
= wiphy
->bands
[band
];
1224 for (i
= 0; i
< sband
->n_channels
; i
++)
1225 handle_channel_custom(wiphy
, band
, i
, regd
);
1228 /* Used by drivers prior to wiphy registration */
1229 void wiphy_apply_custom_regulatory(struct wiphy
*wiphy
,
1230 const struct ieee80211_regdomain
*regd
)
1232 enum ieee80211_band band
;
1233 unsigned int bands_set
= 0;
1235 mutex_lock(®_mutex
);
1236 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1237 if (!wiphy
->bands
[band
])
1239 handle_band_custom(wiphy
, band
, regd
);
1242 mutex_unlock(®_mutex
);
1245 * no point in calling this if it won't have any effect
1246 * on your device's supportd bands.
1248 WARN_ON(!bands_set
);
1250 EXPORT_SYMBOL(wiphy_apply_custom_regulatory
);
1253 * Return value which can be used by ignore_request() to indicate
1254 * it has been determined we should intersect two regulatory domains
1256 #define REG_INTERSECT 1
1258 /* This has the logic which determines when a new request
1259 * should be ignored. */
1260 static int ignore_request(struct wiphy
*wiphy
,
1261 struct regulatory_request
*pending_request
)
1263 struct wiphy
*last_wiphy
= NULL
;
1265 assert_cfg80211_lock();
1267 /* All initial requests are respected */
1271 switch (pending_request
->initiator
) {
1272 case NL80211_REGDOM_SET_BY_CORE
:
1274 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
1276 last_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
1278 if (unlikely(!is_an_alpha2(pending_request
->alpha2
)))
1280 if (last_request
->initiator
==
1281 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1282 if (last_wiphy
!= wiphy
) {
1284 * Two cards with two APs claiming different
1285 * Country IE alpha2s. We could
1286 * intersect them, but that seems unlikely
1287 * to be correct. Reject second one for now.
1289 if (regdom_changes(pending_request
->alpha2
))
1294 * Two consecutive Country IE hints on the same wiphy.
1295 * This should be picked up early by the driver/stack
1297 if (WARN_ON(regdom_changes(pending_request
->alpha2
)))
1302 case NL80211_REGDOM_SET_BY_DRIVER
:
1303 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
) {
1304 if (regdom_changes(pending_request
->alpha2
))
1310 * This would happen if you unplug and plug your card
1311 * back in or if you add a new device for which the previously
1312 * loaded card also agrees on the regulatory domain.
1314 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1315 !regdom_changes(pending_request
->alpha2
))
1318 return REG_INTERSECT
;
1319 case NL80211_REGDOM_SET_BY_USER
:
1320 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)
1321 return REG_INTERSECT
;
1323 * If the user knows better the user should set the regdom
1324 * to their country before the IE is picked up
1326 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
&&
1327 last_request
->intersect
)
1330 * Process user requests only after previous user/driver/core
1331 * requests have been processed
1333 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_CORE
||
1334 last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
1335 last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1336 if (regdom_changes(last_request
->alpha2
))
1340 if (!regdom_changes(pending_request
->alpha2
))
1349 static void reg_set_request_processed(void)
1351 bool need_more_processing
= false;
1353 last_request
->processed
= true;
1355 spin_lock(®_requests_lock
);
1356 if (!list_empty(®_requests_list
))
1357 need_more_processing
= true;
1358 spin_unlock(®_requests_lock
);
1360 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
)
1361 cancel_delayed_work_sync(®_timeout
);
1363 if (need_more_processing
)
1364 schedule_work(®_work
);
1368 * __regulatory_hint - hint to the wireless core a regulatory domain
1369 * @wiphy: if the hint comes from country information from an AP, this
1370 * is required to be set to the wiphy that received the information
1371 * @pending_request: the regulatory request currently being processed
1373 * The Wireless subsystem can use this function to hint to the wireless core
1374 * what it believes should be the current regulatory domain.
1376 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1377 * already been set or other standard error codes.
1379 * Caller must hold &cfg80211_mutex and ®_mutex
1381 static int __regulatory_hint(struct wiphy
*wiphy
,
1382 struct regulatory_request
*pending_request
)
1384 bool intersect
= false;
1387 assert_cfg80211_lock();
1389 r
= ignore_request(wiphy
, pending_request
);
1391 if (r
== REG_INTERSECT
) {
1392 if (pending_request
->initiator
==
1393 NL80211_REGDOM_SET_BY_DRIVER
) {
1394 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1396 kfree(pending_request
);
1403 * If the regulatory domain being requested by the
1404 * driver has already been set just copy it to the
1407 if (r
== -EALREADY
&&
1408 pending_request
->initiator
==
1409 NL80211_REGDOM_SET_BY_DRIVER
) {
1410 r
= reg_copy_regd(&wiphy
->regd
, cfg80211_regdomain
);
1412 kfree(pending_request
);
1418 kfree(pending_request
);
1423 if (last_request
!= &core_request_world
)
1424 kfree(last_request
);
1426 last_request
= pending_request
;
1427 last_request
->intersect
= intersect
;
1429 pending_request
= NULL
;
1431 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_USER
) {
1432 user_alpha2
[0] = last_request
->alpha2
[0];
1433 user_alpha2
[1] = last_request
->alpha2
[1];
1436 /* When r == REG_INTERSECT we do need to call CRDA */
1439 * Since CRDA will not be called in this case as we already
1440 * have applied the requested regulatory domain before we just
1441 * inform userspace we have processed the request
1443 if (r
== -EALREADY
) {
1444 nl80211_send_reg_change_event(last_request
);
1445 reg_set_request_processed();
1450 return call_crda(last_request
->alpha2
);
1453 /* This processes *all* regulatory hints */
1454 static void reg_process_hint(struct regulatory_request
*reg_request
)
1457 struct wiphy
*wiphy
= NULL
;
1458 enum nl80211_reg_initiator initiator
= reg_request
->initiator
;
1460 BUG_ON(!reg_request
->alpha2
);
1462 if (wiphy_idx_valid(reg_request
->wiphy_idx
))
1463 wiphy
= wiphy_idx_to_wiphy(reg_request
->wiphy_idx
);
1465 if (reg_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
&&
1471 r
= __regulatory_hint(wiphy
, reg_request
);
1472 /* This is required so that the orig_* parameters are saved */
1473 if (r
== -EALREADY
&& wiphy
&&
1474 wiphy
->flags
& WIPHY_FLAG_STRICT_REGULATORY
) {
1475 wiphy_update_regulatory(wiphy
, initiator
);
1480 * We only time out user hints, given that they should be the only
1481 * source of bogus requests.
1483 if (r
!= -EALREADY
&&
1484 reg_request
->initiator
== NL80211_REGDOM_SET_BY_USER
)
1485 schedule_delayed_work(®_timeout
, msecs_to_jiffies(3142));
1489 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1490 * Regulatory hints come on a first come first serve basis and we
1491 * must process each one atomically.
1493 static void reg_process_pending_hints(void)
1495 struct regulatory_request
*reg_request
;
1497 mutex_lock(&cfg80211_mutex
);
1498 mutex_lock(®_mutex
);
1500 /* When last_request->processed becomes true this will be rescheduled */
1501 if (last_request
&& !last_request
->processed
) {
1502 REG_DBG_PRINT("Pending regulatory request, waiting "
1503 "for it to be processed...");
1507 spin_lock(®_requests_lock
);
1509 if (list_empty(®_requests_list
)) {
1510 spin_unlock(®_requests_lock
);
1514 reg_request
= list_first_entry(®_requests_list
,
1515 struct regulatory_request
,
1517 list_del_init(®_request
->list
);
1519 spin_unlock(®_requests_lock
);
1521 reg_process_hint(reg_request
);
1524 mutex_unlock(®_mutex
);
1525 mutex_unlock(&cfg80211_mutex
);
1528 /* Processes beacon hints -- this has nothing to do with country IEs */
1529 static void reg_process_pending_beacon_hints(void)
1531 struct cfg80211_registered_device
*rdev
;
1532 struct reg_beacon
*pending_beacon
, *tmp
;
1535 * No need to hold the reg_mutex here as we just touch wiphys
1536 * and do not read or access regulatory variables.
1538 mutex_lock(&cfg80211_mutex
);
1540 /* This goes through the _pending_ beacon list */
1541 spin_lock_bh(®_pending_beacons_lock
);
1543 if (list_empty(®_pending_beacons
)) {
1544 spin_unlock_bh(®_pending_beacons_lock
);
1548 list_for_each_entry_safe(pending_beacon
, tmp
,
1549 ®_pending_beacons
, list
) {
1551 list_del_init(&pending_beacon
->list
);
1553 /* Applies the beacon hint to current wiphys */
1554 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
)
1555 wiphy_update_new_beacon(&rdev
->wiphy
, pending_beacon
);
1557 /* Remembers the beacon hint for new wiphys or reg changes */
1558 list_add_tail(&pending_beacon
->list
, ®_beacon_list
);
1561 spin_unlock_bh(®_pending_beacons_lock
);
1563 mutex_unlock(&cfg80211_mutex
);
1566 static void reg_todo(struct work_struct
*work
)
1568 reg_process_pending_hints();
1569 reg_process_pending_beacon_hints();
1572 static void queue_regulatory_request(struct regulatory_request
*request
)
1574 if (isalpha(request
->alpha2
[0]))
1575 request
->alpha2
[0] = toupper(request
->alpha2
[0]);
1576 if (isalpha(request
->alpha2
[1]))
1577 request
->alpha2
[1] = toupper(request
->alpha2
[1]);
1579 spin_lock(®_requests_lock
);
1580 list_add_tail(&request
->list
, ®_requests_list
);
1581 spin_unlock(®_requests_lock
);
1583 schedule_work(®_work
);
1587 * Core regulatory hint -- happens during cfg80211_init()
1588 * and when we restore regulatory settings.
1590 static int regulatory_hint_core(const char *alpha2
)
1592 struct regulatory_request
*request
;
1594 request
= kzalloc(sizeof(struct regulatory_request
),
1599 request
->alpha2
[0] = alpha2
[0];
1600 request
->alpha2
[1] = alpha2
[1];
1601 request
->initiator
= NL80211_REGDOM_SET_BY_CORE
;
1603 queue_regulatory_request(request
);
1609 int regulatory_hint_user(const char *alpha2
)
1611 struct regulatory_request
*request
;
1615 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1619 request
->wiphy_idx
= WIPHY_IDX_STALE
;
1620 request
->alpha2
[0] = alpha2
[0];
1621 request
->alpha2
[1] = alpha2
[1];
1622 request
->initiator
= NL80211_REGDOM_SET_BY_USER
;
1624 queue_regulatory_request(request
);
1630 int regulatory_hint(struct wiphy
*wiphy
, const char *alpha2
)
1632 struct regulatory_request
*request
;
1637 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1641 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1643 /* Must have registered wiphy first */
1644 BUG_ON(!wiphy_idx_valid(request
->wiphy_idx
));
1646 request
->alpha2
[0] = alpha2
[0];
1647 request
->alpha2
[1] = alpha2
[1];
1648 request
->initiator
= NL80211_REGDOM_SET_BY_DRIVER
;
1650 queue_regulatory_request(request
);
1654 EXPORT_SYMBOL(regulatory_hint
);
1657 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1658 * therefore cannot iterate over the rdev list here.
1660 void regulatory_hint_11d(struct wiphy
*wiphy
,
1661 enum ieee80211_band band
,
1666 enum environment_cap env
= ENVIRON_ANY
;
1667 struct regulatory_request
*request
;
1669 mutex_lock(®_mutex
);
1671 if (unlikely(!last_request
))
1674 /* IE len must be evenly divisible by 2 */
1675 if (country_ie_len
& 0x01)
1678 if (country_ie_len
< IEEE80211_COUNTRY_IE_MIN_LEN
)
1681 alpha2
[0] = country_ie
[0];
1682 alpha2
[1] = country_ie
[1];
1684 if (country_ie
[2] == 'I')
1685 env
= ENVIRON_INDOOR
;
1686 else if (country_ie
[2] == 'O')
1687 env
= ENVIRON_OUTDOOR
;
1690 * We will run this only upon a successful connection on cfg80211.
1691 * We leave conflict resolution to the workqueue, where can hold
1694 if (likely(last_request
->initiator
==
1695 NL80211_REGDOM_SET_BY_COUNTRY_IE
&&
1696 wiphy_idx_valid(last_request
->wiphy_idx
)))
1699 request
= kzalloc(sizeof(struct regulatory_request
), GFP_KERNEL
);
1703 request
->wiphy_idx
= get_wiphy_idx(wiphy
);
1704 request
->alpha2
[0] = alpha2
[0];
1705 request
->alpha2
[1] = alpha2
[1];
1706 request
->initiator
= NL80211_REGDOM_SET_BY_COUNTRY_IE
;
1707 request
->country_ie_env
= env
;
1709 mutex_unlock(®_mutex
);
1711 queue_regulatory_request(request
);
1716 mutex_unlock(®_mutex
);
1719 static void restore_alpha2(char *alpha2
, bool reset_user
)
1721 /* indicates there is no alpha2 to consider for restoration */
1725 /* The user setting has precedence over the module parameter */
1726 if (is_user_regdom_saved()) {
1727 /* Unless we're asked to ignore it and reset it */
1729 REG_DBG_PRINT("Restoring regulatory settings "
1730 "including user preference\n");
1731 user_alpha2
[0] = '9';
1732 user_alpha2
[1] = '7';
1735 * If we're ignoring user settings, we still need to
1736 * check the module parameter to ensure we put things
1737 * back as they were for a full restore.
1739 if (!is_world_regdom(ieee80211_regdom
)) {
1740 REG_DBG_PRINT("Keeping preference on "
1741 "module parameter ieee80211_regdom: %c%c\n",
1742 ieee80211_regdom
[0],
1743 ieee80211_regdom
[1]);
1744 alpha2
[0] = ieee80211_regdom
[0];
1745 alpha2
[1] = ieee80211_regdom
[1];
1748 REG_DBG_PRINT("Restoring regulatory settings "
1749 "while preserving user preference for: %c%c\n",
1752 alpha2
[0] = user_alpha2
[0];
1753 alpha2
[1] = user_alpha2
[1];
1755 } else if (!is_world_regdom(ieee80211_regdom
)) {
1756 REG_DBG_PRINT("Keeping preference on "
1757 "module parameter ieee80211_regdom: %c%c\n",
1758 ieee80211_regdom
[0],
1759 ieee80211_regdom
[1]);
1760 alpha2
[0] = ieee80211_regdom
[0];
1761 alpha2
[1] = ieee80211_regdom
[1];
1763 REG_DBG_PRINT("Restoring regulatory settings\n");
1767 * Restoring regulatory settings involves ingoring any
1768 * possibly stale country IE information and user regulatory
1769 * settings if so desired, this includes any beacon hints
1770 * learned as we could have traveled outside to another country
1771 * after disconnection. To restore regulatory settings we do
1772 * exactly what we did at bootup:
1774 * - send a core regulatory hint
1775 * - send a user regulatory hint if applicable
1777 * Device drivers that send a regulatory hint for a specific country
1778 * keep their own regulatory domain on wiphy->regd so that does does
1779 * not need to be remembered.
1781 static void restore_regulatory_settings(bool reset_user
)
1784 struct reg_beacon
*reg_beacon
, *btmp
;
1785 struct regulatory_request
*reg_request
, *tmp
;
1786 LIST_HEAD(tmp_reg_req_list
);
1788 mutex_lock(&cfg80211_mutex
);
1789 mutex_lock(®_mutex
);
1791 reset_regdomains(true);
1792 restore_alpha2(alpha2
, reset_user
);
1795 * If there's any pending requests we simply
1796 * stash them to a temporary pending queue and
1797 * add then after we've restored regulatory
1800 spin_lock(®_requests_lock
);
1801 if (!list_empty(®_requests_list
)) {
1802 list_for_each_entry_safe(reg_request
, tmp
,
1803 ®_requests_list
, list
) {
1804 if (reg_request
->initiator
!=
1805 NL80211_REGDOM_SET_BY_USER
)
1807 list_del(®_request
->list
);
1808 list_add_tail(®_request
->list
, &tmp_reg_req_list
);
1811 spin_unlock(®_requests_lock
);
1813 /* Clear beacon hints */
1814 spin_lock_bh(®_pending_beacons_lock
);
1815 if (!list_empty(®_pending_beacons
)) {
1816 list_for_each_entry_safe(reg_beacon
, btmp
,
1817 ®_pending_beacons
, list
) {
1818 list_del(®_beacon
->list
);
1822 spin_unlock_bh(®_pending_beacons_lock
);
1824 if (!list_empty(®_beacon_list
)) {
1825 list_for_each_entry_safe(reg_beacon
, btmp
,
1826 ®_beacon_list
, list
) {
1827 list_del(®_beacon
->list
);
1832 /* First restore to the basic regulatory settings */
1833 cfg80211_regdomain
= cfg80211_world_regdom
;
1835 mutex_unlock(®_mutex
);
1836 mutex_unlock(&cfg80211_mutex
);
1838 regulatory_hint_core(cfg80211_regdomain
->alpha2
);
1841 * This restores the ieee80211_regdom module parameter
1842 * preference or the last user requested regulatory
1843 * settings, user regulatory settings takes precedence.
1845 if (is_an_alpha2(alpha2
))
1846 regulatory_hint_user(user_alpha2
);
1848 if (list_empty(&tmp_reg_req_list
))
1851 mutex_lock(&cfg80211_mutex
);
1852 mutex_lock(®_mutex
);
1854 spin_lock(®_requests_lock
);
1855 list_for_each_entry_safe(reg_request
, tmp
, &tmp_reg_req_list
, list
) {
1856 REG_DBG_PRINT("Adding request for country %c%c back "
1858 reg_request
->alpha2
[0],
1859 reg_request
->alpha2
[1]);
1860 list_del(®_request
->list
);
1861 list_add_tail(®_request
->list
, ®_requests_list
);
1863 spin_unlock(®_requests_lock
);
1865 mutex_unlock(®_mutex
);
1866 mutex_unlock(&cfg80211_mutex
);
1868 REG_DBG_PRINT("Kicking the queue\n");
1870 schedule_work(®_work
);
1873 void regulatory_hint_disconnect(void)
1875 REG_DBG_PRINT("All devices are disconnected, going to "
1876 "restore regulatory settings\n");
1877 restore_regulatory_settings(false);
1880 static bool freq_is_chan_12_13_14(u16 freq
)
1882 if (freq
== ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ
) ||
1883 freq
== ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ
) ||
1884 freq
== ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ
))
1889 int regulatory_hint_found_beacon(struct wiphy
*wiphy
,
1890 struct ieee80211_channel
*beacon_chan
,
1893 struct reg_beacon
*reg_beacon
;
1895 if (likely((beacon_chan
->beacon_found
||
1896 (beacon_chan
->flags
& IEEE80211_CHAN_RADAR
) ||
1897 (beacon_chan
->band
== IEEE80211_BAND_2GHZ
&&
1898 !freq_is_chan_12_13_14(beacon_chan
->center_freq
)))))
1901 reg_beacon
= kzalloc(sizeof(struct reg_beacon
), gfp
);
1905 REG_DBG_PRINT("Found new beacon on "
1906 "frequency: %d MHz (Ch %d) on %s\n",
1907 beacon_chan
->center_freq
,
1908 ieee80211_frequency_to_channel(beacon_chan
->center_freq
),
1911 memcpy(®_beacon
->chan
, beacon_chan
,
1912 sizeof(struct ieee80211_channel
));
1916 * Since we can be called from BH or and non-BH context
1917 * we must use spin_lock_bh()
1919 spin_lock_bh(®_pending_beacons_lock
);
1920 list_add_tail(®_beacon
->list
, ®_pending_beacons
);
1921 spin_unlock_bh(®_pending_beacons_lock
);
1923 schedule_work(®_work
);
1928 static void print_rd_rules(const struct ieee80211_regdomain
*rd
)
1931 const struct ieee80211_reg_rule
*reg_rule
= NULL
;
1932 const struct ieee80211_freq_range
*freq_range
= NULL
;
1933 const struct ieee80211_power_rule
*power_rule
= NULL
;
1935 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
1937 for (i
= 0; i
< rd
->n_reg_rules
; i
++) {
1938 reg_rule
= &rd
->reg_rules
[i
];
1939 freq_range
= ®_rule
->freq_range
;
1940 power_rule
= ®_rule
->power_rule
;
1943 * There may not be documentation for max antenna gain
1944 * in certain regions
1946 if (power_rule
->max_antenna_gain
)
1947 pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
1948 freq_range
->start_freq_khz
,
1949 freq_range
->end_freq_khz
,
1950 freq_range
->max_bandwidth_khz
,
1951 power_rule
->max_antenna_gain
,
1952 power_rule
->max_eirp
);
1954 pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
1955 freq_range
->start_freq_khz
,
1956 freq_range
->end_freq_khz
,
1957 freq_range
->max_bandwidth_khz
,
1958 power_rule
->max_eirp
);
1962 static void print_regdomain(const struct ieee80211_regdomain
*rd
)
1965 if (is_intersected_alpha2(rd
->alpha2
)) {
1967 if (last_request
->initiator
==
1968 NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
1969 struct cfg80211_registered_device
*rdev
;
1970 rdev
= cfg80211_rdev_by_wiphy_idx(
1971 last_request
->wiphy_idx
);
1973 pr_info("Current regulatory domain updated by AP to: %c%c\n",
1974 rdev
->country_ie_alpha2
[0],
1975 rdev
->country_ie_alpha2
[1]);
1977 pr_info("Current regulatory domain intersected:\n");
1979 pr_info("Current regulatory domain intersected:\n");
1980 } else if (is_world_regdom(rd
->alpha2
))
1981 pr_info("World regulatory domain updated:\n");
1983 if (is_unknown_alpha2(rd
->alpha2
))
1984 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
1986 pr_info("Regulatory domain changed to country: %c%c\n",
1987 rd
->alpha2
[0], rd
->alpha2
[1]);
1992 static void print_regdomain_info(const struct ieee80211_regdomain
*rd
)
1994 pr_info("Regulatory domain: %c%c\n", rd
->alpha2
[0], rd
->alpha2
[1]);
1998 /* Takes ownership of rd only if it doesn't fail */
1999 static int __set_regdom(const struct ieee80211_regdomain
*rd
)
2001 const struct ieee80211_regdomain
*intersected_rd
= NULL
;
2002 struct cfg80211_registered_device
*rdev
= NULL
;
2003 struct wiphy
*request_wiphy
;
2004 /* Some basic sanity checks first */
2006 if (is_world_regdom(rd
->alpha2
)) {
2007 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2009 update_world_regdomain(rd
);
2013 if (!is_alpha2_set(rd
->alpha2
) && !is_an_alpha2(rd
->alpha2
) &&
2014 !is_unknown_alpha2(rd
->alpha2
))
2021 * Lets only bother proceeding on the same alpha2 if the current
2022 * rd is non static (it means CRDA was present and was used last)
2023 * and the pending request came in from a country IE
2025 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2027 * If someone else asked us to change the rd lets only bother
2028 * checking if the alpha2 changes if CRDA was already called
2030 if (!regdom_changes(rd
->alpha2
))
2035 * Now lets set the regulatory domain, update all driver channels
2036 * and finally inform them of what we have done, in case they want
2037 * to review or adjust their own settings based on their own
2038 * internal EEPROM data
2041 if (WARN_ON(!reg_is_valid_request(rd
->alpha2
)))
2044 if (!is_valid_rd(rd
)) {
2045 pr_err("Invalid regulatory domain detected:\n");
2046 print_regdomain_info(rd
);
2050 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2051 if (!request_wiphy
&&
2052 (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
||
2053 last_request
->initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
)) {
2054 schedule_delayed_work(®_timeout
, 0);
2058 if (!last_request
->intersect
) {
2061 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_DRIVER
) {
2062 reset_regdomains(false);
2063 cfg80211_regdomain
= rd
;
2068 * For a driver hint, lets copy the regulatory domain the
2069 * driver wanted to the wiphy to deal with conflicts
2073 * Userspace could have sent two replies with only
2074 * one kernel request.
2076 if (request_wiphy
->regd
)
2079 r
= reg_copy_regd(&request_wiphy
->regd
, rd
);
2083 reset_regdomains(false);
2084 cfg80211_regdomain
= rd
;
2088 /* Intersection requires a bit more work */
2090 if (last_request
->initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
2092 intersected_rd
= regdom_intersect(rd
, cfg80211_regdomain
);
2093 if (!intersected_rd
)
2097 * We can trash what CRDA provided now.
2098 * However if a driver requested this specific regulatory
2099 * domain we keep it for its private use
2101 if (last_request
->initiator
== NL80211_REGDOM_SET_BY_DRIVER
)
2102 request_wiphy
->regd
= rd
;
2108 reset_regdomains(false);
2109 cfg80211_regdomain
= intersected_rd
;
2114 if (!intersected_rd
)
2117 rdev
= wiphy_to_dev(request_wiphy
);
2119 rdev
->country_ie_alpha2
[0] = rd
->alpha2
[0];
2120 rdev
->country_ie_alpha2
[1] = rd
->alpha2
[1];
2121 rdev
->env
= last_request
->country_ie_env
;
2123 BUG_ON(intersected_rd
== rd
);
2128 reset_regdomains(false);
2129 cfg80211_regdomain
= intersected_rd
;
2136 * Use this call to set the current regulatory domain. Conflicts with
2137 * multiple drivers can be ironed out later. Caller must've already
2138 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2140 int set_regdom(const struct ieee80211_regdomain
*rd
)
2144 assert_cfg80211_lock();
2146 mutex_lock(®_mutex
);
2148 /* Note that this doesn't update the wiphys, this is done below */
2149 r
= __set_regdom(rd
);
2152 mutex_unlock(®_mutex
);
2156 /* This would make this whole thing pointless */
2157 if (!last_request
->intersect
)
2158 BUG_ON(rd
!= cfg80211_regdomain
);
2160 /* update all wiphys now with the new established regulatory domain */
2161 update_all_wiphy_regulatory(last_request
->initiator
);
2163 print_regdomain(cfg80211_regdomain
);
2165 nl80211_send_reg_change_event(last_request
);
2167 reg_set_request_processed();
2169 mutex_unlock(®_mutex
);
2174 #ifdef CONFIG_HOTPLUG
2175 int reg_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
2177 if (last_request
&& !last_request
->processed
) {
2178 if (add_uevent_var(env
, "COUNTRY=%c%c",
2179 last_request
->alpha2
[0],
2180 last_request
->alpha2
[1]))
2187 int reg_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
2191 #endif /* CONFIG_HOTPLUG */
2193 /* Caller must hold cfg80211_mutex */
2194 void reg_device_remove(struct wiphy
*wiphy
)
2196 struct wiphy
*request_wiphy
= NULL
;
2198 assert_cfg80211_lock();
2200 mutex_lock(®_mutex
);
2205 request_wiphy
= wiphy_idx_to_wiphy(last_request
->wiphy_idx
);
2207 if (!request_wiphy
|| request_wiphy
!= wiphy
)
2210 last_request
->wiphy_idx
= WIPHY_IDX_STALE
;
2211 last_request
->country_ie_env
= ENVIRON_ANY
;
2213 mutex_unlock(®_mutex
);
2216 static void reg_timeout_work(struct work_struct
*work
)
2218 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, "
2219 "restoring regulatory settings");
2220 restore_regulatory_settings(true);
2223 int __init
regulatory_init(void)
2227 reg_pdev
= platform_device_register_simple("regulatory", 0, NULL
, 0);
2228 if (IS_ERR(reg_pdev
))
2229 return PTR_ERR(reg_pdev
);
2231 reg_pdev
->dev
.type
= ®_device_type
;
2233 spin_lock_init(®_requests_lock
);
2234 spin_lock_init(®_pending_beacons_lock
);
2236 reg_regdb_size_check();
2238 cfg80211_regdomain
= cfg80211_world_regdom
;
2240 user_alpha2
[0] = '9';
2241 user_alpha2
[1] = '7';
2243 /* We always try to get an update for the static regdomain */
2244 err
= regulatory_hint_core(cfg80211_regdomain
->alpha2
);
2249 * N.B. kobject_uevent_env() can fail mainly for when we're out
2250 * memory which is handled and propagated appropriately above
2251 * but it can also fail during a netlink_broadcast() or during
2252 * early boot for call_usermodehelper(). For now treat these
2253 * errors as non-fatal.
2255 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
2256 #ifdef CONFIG_CFG80211_REG_DEBUG
2257 /* We want to find out exactly why when debugging */
2263 * Finally, if the user set the module parameter treat it
2266 if (!is_world_regdom(ieee80211_regdom
))
2267 regulatory_hint_user(ieee80211_regdom
);
2272 void /* __init_or_exit */ regulatory_exit(void)
2274 struct regulatory_request
*reg_request
, *tmp
;
2275 struct reg_beacon
*reg_beacon
, *btmp
;
2277 cancel_work_sync(®_work
);
2278 cancel_delayed_work_sync(®_timeout
);
2280 mutex_lock(&cfg80211_mutex
);
2281 mutex_lock(®_mutex
);
2283 reset_regdomains(true);
2285 dev_set_uevent_suppress(®_pdev
->dev
, true);
2287 platform_device_unregister(reg_pdev
);
2289 spin_lock_bh(®_pending_beacons_lock
);
2290 if (!list_empty(®_pending_beacons
)) {
2291 list_for_each_entry_safe(reg_beacon
, btmp
,
2292 ®_pending_beacons
, list
) {
2293 list_del(®_beacon
->list
);
2297 spin_unlock_bh(®_pending_beacons_lock
);
2299 if (!list_empty(®_beacon_list
)) {
2300 list_for_each_entry_safe(reg_beacon
, btmp
,
2301 ®_beacon_list
, list
) {
2302 list_del(®_beacon
->list
);
2307 spin_lock(®_requests_lock
);
2308 if (!list_empty(®_requests_list
)) {
2309 list_for_each_entry_safe(reg_request
, tmp
,
2310 ®_requests_list
, list
) {
2311 list_del(®_request
->list
);
2315 spin_unlock(®_requests_lock
);
2317 mutex_unlock(®_mutex
);
2318 mutex_unlock(&cfg80211_mutex
);