2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 * DOC: Wireless regulatory infrastructure
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
20 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/list.h>
38 #include <linux/random.h>
39 #include <linux/ctype.h>
40 #include <linux/nl80211.h>
41 #include <linux/platform_device.h>
42 #include <net/cfg80211.h>
48 #ifdef CONFIG_CFG80211_REG_DEBUG
49 #define REG_DBG_PRINT(format, args...) \
51 printk(KERN_DEBUG format , ## args); \
54 #define REG_DBG_PRINT(args...)
57 /* Receipt of information from last regulatory request */
58 static struct regulatory_request *last_request;
60 /* To trigger userspace events */
61 static struct platform_device *reg_pdev;
64 * Central wireless core regulatory domains, we only need two,
65 * the current one and a world regulatory domain in case we have no
66 * information to give us an alpha2
68 const struct ieee80211_regdomain *cfg80211_regdomain;
71 * Protects static reg.c components:
72 * - cfg80211_world_regdom
76 static DEFINE_MUTEX(reg_mutex);
77 #define assert_reg_lock() WARN_ON(!mutex_is_locked(®_mutex))
79 /* Used to queue up regulatory hints */
80 static LIST_HEAD(reg_requests_list);
81 static spinlock_t reg_requests_lock;
83 /* Used to queue up beacon hints for review */
84 static LIST_HEAD(reg_pending_beacons);
85 static spinlock_t reg_pending_beacons_lock;
87 /* Used to keep track of processed beacon hints */
88 static LIST_HEAD(reg_beacon_list);
91 struct list_head list;
92 struct ieee80211_channel chan;
95 /* We keep a static world regulatory domain in case of the absence of CRDA */
96 static const struct ieee80211_regdomain world_regdom = {
100 /* IEEE 802.11b/g, channels 1..11 */
101 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
102 /* IEEE 802.11b/g, channels 12..13. No HT40
103 * channel fits here. */
104 REG_RULE(2467-10, 2472+10, 20, 6, 20,
105 NL80211_RRF_PASSIVE_SCAN |
106 NL80211_RRF_NO_IBSS),
107 /* IEEE 802.11 channel 14 - Only JP enables
108 * this and for 802.11b only */
109 REG_RULE(2484-10, 2484+10, 20, 6, 20,
110 NL80211_RRF_PASSIVE_SCAN |
111 NL80211_RRF_NO_IBSS |
112 NL80211_RRF_NO_OFDM),
113 /* IEEE 802.11a, channel 36..48 */
114 REG_RULE(5180-10, 5240+10, 40, 6, 20,
115 NL80211_RRF_PASSIVE_SCAN |
116 NL80211_RRF_NO_IBSS),
118 /* NB: 5260 MHz - 5700 MHz requies DFS */
120 /* IEEE 802.11a, channel 149..165 */
121 REG_RULE(5745-10, 5825+10, 40, 6, 20,
122 NL80211_RRF_PASSIVE_SCAN |
123 NL80211_RRF_NO_IBSS),
127 static const struct ieee80211_regdomain *cfg80211_world_regdom =
130 static char *ieee80211_regdom = "00";
131 static char user_alpha2[2];
133 module_param(ieee80211_regdom, charp, 0444);
134 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
136 static void reset_regdomains(void)
138 /* avoid freeing static information or freeing something twice */
139 if (cfg80211_regdomain == cfg80211_world_regdom)
140 cfg80211_regdomain = NULL;
141 if (cfg80211_world_regdom == &world_regdom)
142 cfg80211_world_regdom = NULL;
143 if (cfg80211_regdomain == &world_regdom)
144 cfg80211_regdomain = NULL;
146 kfree(cfg80211_regdomain);
147 kfree(cfg80211_world_regdom);
149 cfg80211_world_regdom = &world_regdom;
150 cfg80211_regdomain = NULL;
154 * Dynamic world regulatory domain requested by the wireless
155 * core upon initialization
157 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
159 BUG_ON(!last_request);
163 cfg80211_world_regdom = rd;
164 cfg80211_regdomain = rd;
167 bool is_world_regdom(const char *alpha2)
171 if (alpha2[0] == '0' && alpha2[1] == '0')
176 static bool is_alpha2_set(const char *alpha2)
180 if (alpha2[0] != 0 && alpha2[1] != 0)
185 static bool is_unknown_alpha2(const char *alpha2)
190 * Special case where regulatory domain was built by driver
191 * but a specific alpha2 cannot be determined
193 if (alpha2[0] == '9' && alpha2[1] == '9')
198 static bool is_intersected_alpha2(const char *alpha2)
203 * Special case where regulatory domain is the
204 * result of an intersection between two regulatory domain
207 if (alpha2[0] == '9' && alpha2[1] == '8')
212 static bool is_an_alpha2(const char *alpha2)
216 if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
221 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
223 if (!alpha2_x || !alpha2_y)
225 if (alpha2_x[0] == alpha2_y[0] &&
226 alpha2_x[1] == alpha2_y[1])
231 static bool regdom_changes(const char *alpha2)
233 assert_cfg80211_lock();
235 if (!cfg80211_regdomain)
237 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
243 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
244 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
245 * has ever been issued.
247 static bool is_user_regdom_saved(void)
249 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
252 /* This would indicate a mistake on the design */
253 if (WARN((!is_world_regdom(user_alpha2) &&
254 !is_an_alpha2(user_alpha2)),
255 "Unexpected user alpha2: %c%c\n",
263 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
264 const struct ieee80211_regdomain *src_regd)
266 struct ieee80211_regdomain *regd;
267 int size_of_regd = 0;
270 size_of_regd = sizeof(struct ieee80211_regdomain) +
271 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
273 regd = kzalloc(size_of_regd, GFP_KERNEL);
277 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
279 for (i = 0; i < src_regd->n_reg_rules; i++)
280 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i],
281 sizeof(struct ieee80211_reg_rule));
287 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
288 struct reg_regdb_search_request {
290 struct list_head list;
293 static LIST_HEAD(reg_regdb_search_list);
294 static DEFINE_MUTEX(reg_regdb_search_mutex);
296 static void reg_regdb_search(struct work_struct *work)
298 struct reg_regdb_search_request *request;
299 const struct ieee80211_regdomain *curdom, *regdom;
302 mutex_lock(®_regdb_search_mutex);
303 while (!list_empty(®_regdb_search_list)) {
304 request = list_first_entry(®_regdb_search_list,
305 struct reg_regdb_search_request,
307 list_del(&request->list);
309 for (i=0; i<reg_regdb_size; i++) {
310 curdom = reg_regdb[i];
312 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
313 r = reg_copy_regd(®dom, curdom);
316 mutex_lock(&cfg80211_mutex);
318 mutex_unlock(&cfg80211_mutex);
325 mutex_unlock(®_regdb_search_mutex);
328 static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
330 static void reg_regdb_query(const char *alpha2)
332 struct reg_regdb_search_request *request;
337 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
341 memcpy(request->alpha2, alpha2, 2);
343 mutex_lock(®_regdb_search_mutex);
344 list_add_tail(&request->list, ®_regdb_search_list);
345 mutex_unlock(®_regdb_search_mutex);
347 schedule_work(®_regdb_work);
350 static inline void reg_regdb_query(const char *alpha2) {}
351 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
354 * This lets us keep regulatory code which is updated on a regulatory
355 * basis in userspace.
357 static int call_crda(const char *alpha2)
359 char country_env[9 + 2] = "COUNTRY=";
365 if (!is_world_regdom((char *) alpha2))
366 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
367 alpha2[0], alpha2[1]);
369 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
370 "regulatory domain\n");
372 /* query internal regulatory database (if it exists) */
373 reg_regdb_query(alpha2);
375 country_env[8] = alpha2[0];
376 country_env[9] = alpha2[1];
378 return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp);
381 /* Used by nl80211 before kmalloc'ing our regulatory domain */
382 bool reg_is_valid_request(const char *alpha2)
384 assert_cfg80211_lock();
389 return alpha2_equal(last_request->alpha2, alpha2);
392 /* Sanity check on a regulatory rule */
393 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
395 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
398 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
401 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
404 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
406 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
407 freq_range->max_bandwidth_khz > freq_diff)
413 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
415 const struct ieee80211_reg_rule *reg_rule = NULL;
418 if (!rd->n_reg_rules)
421 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
424 for (i = 0; i < rd->n_reg_rules; i++) {
425 reg_rule = &rd->reg_rules[i];
426 if (!is_valid_reg_rule(reg_rule))
433 static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
437 u32 start_freq_khz, end_freq_khz;
439 start_freq_khz = center_freq_khz - (bw_khz/2);
440 end_freq_khz = center_freq_khz + (bw_khz/2);
442 if (start_freq_khz >= freq_range->start_freq_khz &&
443 end_freq_khz <= freq_range->end_freq_khz)
450 * freq_in_rule_band - tells us if a frequency is in a frequency band
451 * @freq_range: frequency rule we want to query
452 * @freq_khz: frequency we are inquiring about
454 * This lets us know if a specific frequency rule is or is not relevant to
455 * a specific frequency's band. Bands are device specific and artificial
456 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
457 * safe for now to assume that a frequency rule should not be part of a
458 * frequency's band if the start freq or end freq are off by more than 2 GHz.
459 * This resolution can be lowered and should be considered as we add
460 * regulatory rule support for other "bands".
462 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
465 #define ONE_GHZ_IN_KHZ 1000000
466 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
468 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
471 #undef ONE_GHZ_IN_KHZ
475 * Helper for regdom_intersect(), this does the real
476 * mathematical intersection fun
478 static int reg_rules_intersect(
479 const struct ieee80211_reg_rule *rule1,
480 const struct ieee80211_reg_rule *rule2,
481 struct ieee80211_reg_rule *intersected_rule)
483 const struct ieee80211_freq_range *freq_range1, *freq_range2;
484 struct ieee80211_freq_range *freq_range;
485 const struct ieee80211_power_rule *power_rule1, *power_rule2;
486 struct ieee80211_power_rule *power_rule;
489 freq_range1 = &rule1->freq_range;
490 freq_range2 = &rule2->freq_range;
491 freq_range = &intersected_rule->freq_range;
493 power_rule1 = &rule1->power_rule;
494 power_rule2 = &rule2->power_rule;
495 power_rule = &intersected_rule->power_rule;
497 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
498 freq_range2->start_freq_khz);
499 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
500 freq_range2->end_freq_khz);
501 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
502 freq_range2->max_bandwidth_khz);
504 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
505 if (freq_range->max_bandwidth_khz > freq_diff)
506 freq_range->max_bandwidth_khz = freq_diff;
508 power_rule->max_eirp = min(power_rule1->max_eirp,
509 power_rule2->max_eirp);
510 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
511 power_rule2->max_antenna_gain);
513 intersected_rule->flags = (rule1->flags | rule2->flags);
515 if (!is_valid_reg_rule(intersected_rule))
522 * regdom_intersect - do the intersection between two regulatory domains
523 * @rd1: first regulatory domain
524 * @rd2: second regulatory domain
526 * Use this function to get the intersection between two regulatory domains.
527 * Once completed we will mark the alpha2 for the rd as intersected, "98",
528 * as no one single alpha2 can represent this regulatory domain.
530 * Returns a pointer to the regulatory domain structure which will hold the
531 * resulting intersection of rules between rd1 and rd2. We will
532 * kzalloc() this structure for you.
534 static struct ieee80211_regdomain *regdom_intersect(
535 const struct ieee80211_regdomain *rd1,
536 const struct ieee80211_regdomain *rd2)
540 unsigned int num_rules = 0, rule_idx = 0;
541 const struct ieee80211_reg_rule *rule1, *rule2;
542 struct ieee80211_reg_rule *intersected_rule;
543 struct ieee80211_regdomain *rd;
544 /* This is just a dummy holder to help us count */
545 struct ieee80211_reg_rule irule;
547 /* Uses the stack temporarily for counter arithmetic */
548 intersected_rule = &irule;
550 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
556 * First we get a count of the rules we'll need, then we actually
557 * build them. This is to so we can malloc() and free() a
558 * regdomain once. The reason we use reg_rules_intersect() here
559 * is it will return -EINVAL if the rule computed makes no sense.
560 * All rules that do check out OK are valid.
563 for (x = 0; x < rd1->n_reg_rules; x++) {
564 rule1 = &rd1->reg_rules[x];
565 for (y = 0; y < rd2->n_reg_rules; y++) {
566 rule2 = &rd2->reg_rules[y];
567 if (!reg_rules_intersect(rule1, rule2,
570 memset(intersected_rule, 0,
571 sizeof(struct ieee80211_reg_rule));
578 size_of_regd = sizeof(struct ieee80211_regdomain) +
579 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
581 rd = kzalloc(size_of_regd, GFP_KERNEL);
585 for (x = 0; x < rd1->n_reg_rules; x++) {
586 rule1 = &rd1->reg_rules[x];
587 for (y = 0; y < rd2->n_reg_rules; y++) {
588 rule2 = &rd2->reg_rules[y];
590 * This time around instead of using the stack lets
591 * write to the target rule directly saving ourselves
594 intersected_rule = &rd->reg_rules[rule_idx];
595 r = reg_rules_intersect(rule1, rule2,
598 * No need to memset here the intersected rule here as
599 * we're not using the stack anymore
607 if (rule_idx != num_rules) {
612 rd->n_reg_rules = num_rules;
620 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
621 * want to just have the channel structure use these
623 static u32 map_regdom_flags(u32 rd_flags)
625 u32 channel_flags = 0;
626 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
627 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
628 if (rd_flags & NL80211_RRF_NO_IBSS)
629 channel_flags |= IEEE80211_CHAN_NO_IBSS;
630 if (rd_flags & NL80211_RRF_DFS)
631 channel_flags |= IEEE80211_CHAN_RADAR;
632 return channel_flags;
635 static int freq_reg_info_regd(struct wiphy *wiphy,
638 const struct ieee80211_reg_rule **reg_rule,
639 const struct ieee80211_regdomain *custom_regd)
642 bool band_rule_found = false;
643 const struct ieee80211_regdomain *regd;
644 bool bw_fits = false;
647 desired_bw_khz = MHZ_TO_KHZ(20);
649 regd = custom_regd ? custom_regd : cfg80211_regdomain;
652 * Follow the driver's regulatory domain, if present, unless a country
653 * IE has been processed or a user wants to help complaince further
655 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
656 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
663 for (i = 0; i < regd->n_reg_rules; i++) {
664 const struct ieee80211_reg_rule *rr;
665 const struct ieee80211_freq_range *fr = NULL;
666 const struct ieee80211_power_rule *pr = NULL;
668 rr = ®d->reg_rules[i];
669 fr = &rr->freq_range;
670 pr = &rr->power_rule;
673 * We only need to know if one frequency rule was
674 * was in center_freq's band, that's enough, so lets
675 * not overwrite it once found
677 if (!band_rule_found)
678 band_rule_found = freq_in_rule_band(fr, center_freq);
680 bw_fits = reg_does_bw_fit(fr,
684 if (band_rule_found && bw_fits) {
690 if (!band_rule_found)
696 int freq_reg_info(struct wiphy *wiphy,
699 const struct ieee80211_reg_rule **reg_rule)
701 assert_cfg80211_lock();
702 return freq_reg_info_regd(wiphy,
708 EXPORT_SYMBOL(freq_reg_info);
711 * Note that right now we assume the desired channel bandwidth
712 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
713 * per channel, the primary and the extension channel). To support
714 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
715 * new ieee80211_channel.target_bw and re run the regulatory check
716 * on the wiphy with the target_bw specified. Then we can simply use
717 * that below for the desired_bw_khz below.
719 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
720 unsigned int chan_idx)
723 u32 flags, bw_flags = 0;
724 u32 desired_bw_khz = MHZ_TO_KHZ(20);
725 const struct ieee80211_reg_rule *reg_rule = NULL;
726 const struct ieee80211_power_rule *power_rule = NULL;
727 const struct ieee80211_freq_range *freq_range = NULL;
728 struct ieee80211_supported_band *sband;
729 struct ieee80211_channel *chan;
730 struct wiphy *request_wiphy = NULL;
732 assert_cfg80211_lock();
734 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
736 sband = wiphy->bands[band];
737 BUG_ON(chan_idx >= sband->n_channels);
738 chan = &sband->channels[chan_idx];
740 flags = chan->orig_flags;
742 r = freq_reg_info(wiphy,
743 MHZ_TO_KHZ(chan->center_freq),
750 power_rule = ®_rule->power_rule;
751 freq_range = ®_rule->freq_range;
753 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
754 bw_flags = IEEE80211_CHAN_NO_HT40;
756 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
757 request_wiphy && request_wiphy == wiphy &&
758 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
760 * This gaurantees the driver's requested regulatory domain
761 * will always be used as a base for further regulatory
764 chan->flags = chan->orig_flags =
765 map_regdom_flags(reg_rule->flags) | bw_flags;
766 chan->max_antenna_gain = chan->orig_mag =
767 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
768 chan->max_power = chan->orig_mpwr =
769 (int) MBM_TO_DBM(power_rule->max_eirp);
773 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
774 chan->max_antenna_gain = min(chan->orig_mag,
775 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
777 chan->max_power = min(chan->orig_mpwr,
778 (int) MBM_TO_DBM(power_rule->max_eirp));
780 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
783 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
786 struct ieee80211_supported_band *sband;
788 BUG_ON(!wiphy->bands[band]);
789 sband = wiphy->bands[band];
791 for (i = 0; i < sband->n_channels; i++)
792 handle_channel(wiphy, band, i);
795 static bool ignore_reg_update(struct wiphy *wiphy,
796 enum nl80211_reg_initiator initiator)
800 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
801 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
804 * wiphy->regd will be set once the device has its own
805 * desired regulatory domain set
807 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
808 !is_world_regdom(last_request->alpha2))
813 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
815 struct cfg80211_registered_device *rdev;
817 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
818 wiphy_update_regulatory(&rdev->wiphy, initiator);
821 static void handle_reg_beacon(struct wiphy *wiphy,
822 unsigned int chan_idx,
823 struct reg_beacon *reg_beacon)
825 struct ieee80211_supported_band *sband;
826 struct ieee80211_channel *chan;
827 bool channel_changed = false;
828 struct ieee80211_channel chan_before;
830 assert_cfg80211_lock();
832 sband = wiphy->bands[reg_beacon->chan.band];
833 chan = &sband->channels[chan_idx];
835 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
838 if (chan->beacon_found)
841 chan->beacon_found = true;
843 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
846 chan_before.center_freq = chan->center_freq;
847 chan_before.flags = chan->flags;
849 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
850 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
851 channel_changed = true;
854 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
855 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
856 channel_changed = true;
860 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
864 * Called when a scan on a wiphy finds a beacon on
867 static void wiphy_update_new_beacon(struct wiphy *wiphy,
868 struct reg_beacon *reg_beacon)
871 struct ieee80211_supported_band *sband;
873 assert_cfg80211_lock();
875 if (!wiphy->bands[reg_beacon->chan.band])
878 sband = wiphy->bands[reg_beacon->chan.band];
880 for (i = 0; i < sband->n_channels; i++)
881 handle_reg_beacon(wiphy, i, reg_beacon);
885 * Called upon reg changes or a new wiphy is added
887 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
890 struct ieee80211_supported_band *sband;
891 struct reg_beacon *reg_beacon;
893 assert_cfg80211_lock();
895 if (list_empty(®_beacon_list))
898 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
899 if (!wiphy->bands[reg_beacon->chan.band])
901 sband = wiphy->bands[reg_beacon->chan.band];
902 for (i = 0; i < sband->n_channels; i++)
903 handle_reg_beacon(wiphy, i, reg_beacon);
907 static bool reg_is_world_roaming(struct wiphy *wiphy)
909 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
910 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
913 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
914 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
919 /* Reap the advantages of previously found beacons */
920 static void reg_process_beacons(struct wiphy *wiphy)
923 * Means we are just firing up cfg80211, so no beacons would
924 * have been processed yet.
928 if (!reg_is_world_roaming(wiphy))
930 wiphy_update_beacon_reg(wiphy);
933 static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
937 if (chan->flags & IEEE80211_CHAN_DISABLED)
939 /* This would happen when regulatory rules disallow HT40 completely */
940 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
945 static void reg_process_ht_flags_channel(struct wiphy *wiphy,
946 enum ieee80211_band band,
947 unsigned int chan_idx)
949 struct ieee80211_supported_band *sband;
950 struct ieee80211_channel *channel;
951 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
954 assert_cfg80211_lock();
956 sband = wiphy->bands[band];
957 BUG_ON(chan_idx >= sband->n_channels);
958 channel = &sband->channels[chan_idx];
960 if (is_ht40_not_allowed(channel)) {
961 channel->flags |= IEEE80211_CHAN_NO_HT40;
966 * We need to ensure the extension channels exist to
967 * be able to use HT40- or HT40+, this finds them (or not)
969 for (i = 0; i < sband->n_channels; i++) {
970 struct ieee80211_channel *c = &sband->channels[i];
971 if (c->center_freq == (channel->center_freq - 20))
973 if (c->center_freq == (channel->center_freq + 20))
978 * Please note that this assumes target bandwidth is 20 MHz,
979 * if that ever changes we also need to change the below logic
980 * to include that as well.
982 if (is_ht40_not_allowed(channel_before))
983 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
985 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
987 if (is_ht40_not_allowed(channel_after))
988 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
990 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
993 static void reg_process_ht_flags_band(struct wiphy *wiphy,
994 enum ieee80211_band band)
997 struct ieee80211_supported_band *sband;
999 BUG_ON(!wiphy->bands[band]);
1000 sband = wiphy->bands[band];
1002 for (i = 0; i < sband->n_channels; i++)
1003 reg_process_ht_flags_channel(wiphy, band, i);
1006 static void reg_process_ht_flags(struct wiphy *wiphy)
1008 enum ieee80211_band band;
1013 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1014 if (wiphy->bands[band])
1015 reg_process_ht_flags_band(wiphy, band);
1020 void wiphy_update_regulatory(struct wiphy *wiphy,
1021 enum nl80211_reg_initiator initiator)
1023 enum ieee80211_band band;
1025 if (ignore_reg_update(wiphy, initiator))
1027 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1028 if (wiphy->bands[band])
1029 handle_band(wiphy, band);
1032 reg_process_beacons(wiphy);
1033 reg_process_ht_flags(wiphy);
1034 if (wiphy->reg_notifier)
1035 wiphy->reg_notifier(wiphy, last_request);
1038 static void handle_channel_custom(struct wiphy *wiphy,
1039 enum ieee80211_band band,
1040 unsigned int chan_idx,
1041 const struct ieee80211_regdomain *regd)
1044 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1046 const struct ieee80211_reg_rule *reg_rule = NULL;
1047 const struct ieee80211_power_rule *power_rule = NULL;
1048 const struct ieee80211_freq_range *freq_range = NULL;
1049 struct ieee80211_supported_band *sband;
1050 struct ieee80211_channel *chan;
1054 sband = wiphy->bands[band];
1055 BUG_ON(chan_idx >= sband->n_channels);
1056 chan = &sband->channels[chan_idx];
1058 r = freq_reg_info_regd(wiphy,
1059 MHZ_TO_KHZ(chan->center_freq),
1065 chan->flags = IEEE80211_CHAN_DISABLED;
1069 power_rule = ®_rule->power_rule;
1070 freq_range = ®_rule->freq_range;
1072 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1073 bw_flags = IEEE80211_CHAN_NO_HT40;
1075 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1076 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1077 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1080 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1081 const struct ieee80211_regdomain *regd)
1084 struct ieee80211_supported_band *sband;
1086 BUG_ON(!wiphy->bands[band]);
1087 sband = wiphy->bands[band];
1089 for (i = 0; i < sband->n_channels; i++)
1090 handle_channel_custom(wiphy, band, i, regd);
1093 /* Used by drivers prior to wiphy registration */
1094 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1095 const struct ieee80211_regdomain *regd)
1097 enum ieee80211_band band;
1098 unsigned int bands_set = 0;
1100 mutex_lock(®_mutex);
1101 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1102 if (!wiphy->bands[band])
1104 handle_band_custom(wiphy, band, regd);
1107 mutex_unlock(®_mutex);
1110 * no point in calling this if it won't have any effect
1111 * on your device's supportd bands.
1113 WARN_ON(!bands_set);
1115 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1118 * Return value which can be used by ignore_request() to indicate
1119 * it has been determined we should intersect two regulatory domains
1121 #define REG_INTERSECT 1
1123 /* This has the logic which determines when a new request
1124 * should be ignored. */
1125 static int ignore_request(struct wiphy *wiphy,
1126 struct regulatory_request *pending_request)
1128 struct wiphy *last_wiphy = NULL;
1130 assert_cfg80211_lock();
1132 /* All initial requests are respected */
1136 switch (pending_request->initiator) {
1137 case NL80211_REGDOM_SET_BY_CORE:
1139 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1141 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1143 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
1145 if (last_request->initiator ==
1146 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1147 if (last_wiphy != wiphy) {
1149 * Two cards with two APs claiming different
1150 * Country IE alpha2s. We could
1151 * intersect them, but that seems unlikely
1152 * to be correct. Reject second one for now.
1154 if (regdom_changes(pending_request->alpha2))
1159 * Two consecutive Country IE hints on the same wiphy.
1160 * This should be picked up early by the driver/stack
1162 if (WARN_ON(regdom_changes(pending_request->alpha2)))
1166 return REG_INTERSECT;
1167 case NL80211_REGDOM_SET_BY_DRIVER:
1168 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1169 if (regdom_changes(pending_request->alpha2))
1175 * This would happen if you unplug and plug your card
1176 * back in or if you add a new device for which the previously
1177 * loaded card also agrees on the regulatory domain.
1179 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1180 !regdom_changes(pending_request->alpha2))
1183 return REG_INTERSECT;
1184 case NL80211_REGDOM_SET_BY_USER:
1185 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1186 return REG_INTERSECT;
1188 * If the user knows better the user should set the regdom
1189 * to their country before the IE is picked up
1191 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1192 last_request->intersect)
1195 * Process user requests only after previous user/driver/core
1196 * requests have been processed
1198 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1199 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1200 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1201 if (regdom_changes(last_request->alpha2))
1205 if (!regdom_changes(pending_request->alpha2))
1215 * __regulatory_hint - hint to the wireless core a regulatory domain
1216 * @wiphy: if the hint comes from country information from an AP, this
1217 * is required to be set to the wiphy that received the information
1218 * @pending_request: the regulatory request currently being processed
1220 * The Wireless subsystem can use this function to hint to the wireless core
1221 * what it believes should be the current regulatory domain.
1223 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1224 * already been set or other standard error codes.
1226 * Caller must hold &cfg80211_mutex and ®_mutex
1228 static int __regulatory_hint(struct wiphy *wiphy,
1229 struct regulatory_request *pending_request)
1231 bool intersect = false;
1234 assert_cfg80211_lock();
1236 r = ignore_request(wiphy, pending_request);
1238 if (r == REG_INTERSECT) {
1239 if (pending_request->initiator ==
1240 NL80211_REGDOM_SET_BY_DRIVER) {
1241 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1243 kfree(pending_request);
1250 * If the regulatory domain being requested by the
1251 * driver has already been set just copy it to the
1254 if (r == -EALREADY &&
1255 pending_request->initiator ==
1256 NL80211_REGDOM_SET_BY_DRIVER) {
1257 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1259 kfree(pending_request);
1265 kfree(pending_request);
1270 kfree(last_request);
1272 last_request = pending_request;
1273 last_request->intersect = intersect;
1275 pending_request = NULL;
1277 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1278 user_alpha2[0] = last_request->alpha2[0];
1279 user_alpha2[1] = last_request->alpha2[1];
1282 /* When r == REG_INTERSECT we do need to call CRDA */
1285 * Since CRDA will not be called in this case as we already
1286 * have applied the requested regulatory domain before we just
1287 * inform userspace we have processed the request
1290 nl80211_send_reg_change_event(last_request);
1294 return call_crda(last_request->alpha2);
1297 /* This processes *all* regulatory hints */
1298 static void reg_process_hint(struct regulatory_request *reg_request)
1301 struct wiphy *wiphy = NULL;
1302 enum nl80211_reg_initiator initiator = reg_request->initiator;
1304 BUG_ON(!reg_request->alpha2);
1306 mutex_lock(&cfg80211_mutex);
1307 mutex_lock(®_mutex);
1309 if (wiphy_idx_valid(reg_request->wiphy_idx))
1310 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1312 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1318 r = __regulatory_hint(wiphy, reg_request);
1319 /* This is required so that the orig_* parameters are saved */
1320 if (r == -EALREADY && wiphy &&
1321 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
1322 wiphy_update_regulatory(wiphy, initiator);
1324 mutex_unlock(®_mutex);
1325 mutex_unlock(&cfg80211_mutex);
1328 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1329 static void reg_process_pending_hints(void)
1331 struct regulatory_request *reg_request;
1333 spin_lock(®_requests_lock);
1334 while (!list_empty(®_requests_list)) {
1335 reg_request = list_first_entry(®_requests_list,
1336 struct regulatory_request,
1338 list_del_init(®_request->list);
1340 spin_unlock(®_requests_lock);
1341 reg_process_hint(reg_request);
1342 spin_lock(®_requests_lock);
1344 spin_unlock(®_requests_lock);
1347 /* Processes beacon hints -- this has nothing to do with country IEs */
1348 static void reg_process_pending_beacon_hints(void)
1350 struct cfg80211_registered_device *rdev;
1351 struct reg_beacon *pending_beacon, *tmp;
1354 * No need to hold the reg_mutex here as we just touch wiphys
1355 * and do not read or access regulatory variables.
1357 mutex_lock(&cfg80211_mutex);
1359 /* This goes through the _pending_ beacon list */
1360 spin_lock_bh(®_pending_beacons_lock);
1362 if (list_empty(®_pending_beacons)) {
1363 spin_unlock_bh(®_pending_beacons_lock);
1367 list_for_each_entry_safe(pending_beacon, tmp,
1368 ®_pending_beacons, list) {
1370 list_del_init(&pending_beacon->list);
1372 /* Applies the beacon hint to current wiphys */
1373 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1374 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
1376 /* Remembers the beacon hint for new wiphys or reg changes */
1377 list_add_tail(&pending_beacon->list, ®_beacon_list);
1380 spin_unlock_bh(®_pending_beacons_lock);
1382 mutex_unlock(&cfg80211_mutex);
1385 static void reg_todo(struct work_struct *work)
1387 reg_process_pending_hints();
1388 reg_process_pending_beacon_hints();
1391 static DECLARE_WORK(reg_work, reg_todo);
1393 static void queue_regulatory_request(struct regulatory_request *request)
1395 if (isalpha(request->alpha2[0]))
1396 request->alpha2[0] = toupper(request->alpha2[0]);
1397 if (isalpha(request->alpha2[1]))
1398 request->alpha2[1] = toupper(request->alpha2[1]);
1400 spin_lock(®_requests_lock);
1401 list_add_tail(&request->list, ®_requests_list);
1402 spin_unlock(®_requests_lock);
1404 schedule_work(®_work);
1408 * Core regulatory hint -- happens during cfg80211_init()
1409 * and when we restore regulatory settings.
1411 static int regulatory_hint_core(const char *alpha2)
1413 struct regulatory_request *request;
1415 kfree(last_request);
1416 last_request = NULL;
1418 request = kzalloc(sizeof(struct regulatory_request),
1423 request->alpha2[0] = alpha2[0];
1424 request->alpha2[1] = alpha2[1];
1425 request->initiator = NL80211_REGDOM_SET_BY_CORE;
1428 * This ensures last_request is populated once modules
1429 * come swinging in and calling regulatory hints and
1430 * wiphy_apply_custom_regulatory().
1432 reg_process_hint(request);
1438 int regulatory_hint_user(const char *alpha2)
1440 struct regulatory_request *request;
1444 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1448 request->wiphy_idx = WIPHY_IDX_STALE;
1449 request->alpha2[0] = alpha2[0];
1450 request->alpha2[1] = alpha2[1];
1451 request->initiator = NL80211_REGDOM_SET_BY_USER;
1453 queue_regulatory_request(request);
1459 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1461 struct regulatory_request *request;
1466 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1470 request->wiphy_idx = get_wiphy_idx(wiphy);
1472 /* Must have registered wiphy first */
1473 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1475 request->alpha2[0] = alpha2[0];
1476 request->alpha2[1] = alpha2[1];
1477 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1479 queue_regulatory_request(request);
1483 EXPORT_SYMBOL(regulatory_hint);
1486 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1487 * therefore cannot iterate over the rdev list here.
1489 void regulatory_hint_11d(struct wiphy *wiphy,
1490 enum ieee80211_band band,
1495 enum environment_cap env = ENVIRON_ANY;
1496 struct regulatory_request *request;
1498 mutex_lock(®_mutex);
1500 if (unlikely(!last_request))
1503 /* IE len must be evenly divisible by 2 */
1504 if (country_ie_len & 0x01)
1507 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1510 alpha2[0] = country_ie[0];
1511 alpha2[1] = country_ie[1];
1513 if (country_ie[2] == 'I')
1514 env = ENVIRON_INDOOR;
1515 else if (country_ie[2] == 'O')
1516 env = ENVIRON_OUTDOOR;
1519 * We will run this only upon a successful connection on cfg80211.
1520 * We leave conflict resolution to the workqueue, where can hold
1523 if (likely(last_request->initiator ==
1524 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1525 wiphy_idx_valid(last_request->wiphy_idx)))
1528 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1532 request->wiphy_idx = get_wiphy_idx(wiphy);
1533 request->alpha2[0] = alpha2[0];
1534 request->alpha2[1] = alpha2[1];
1535 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
1536 request->country_ie_env = env;
1538 mutex_unlock(®_mutex);
1540 queue_regulatory_request(request);
1545 mutex_unlock(®_mutex);
1548 static void restore_alpha2(char *alpha2, bool reset_user)
1550 /* indicates there is no alpha2 to consider for restoration */
1554 /* The user setting has precedence over the module parameter */
1555 if (is_user_regdom_saved()) {
1556 /* Unless we're asked to ignore it and reset it */
1558 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
1559 "including user preference\n");
1560 user_alpha2[0] = '9';
1561 user_alpha2[1] = '7';
1564 * If we're ignoring user settings, we still need to
1565 * check the module parameter to ensure we put things
1566 * back as they were for a full restore.
1568 if (!is_world_regdom(ieee80211_regdom)) {
1569 REG_DBG_PRINT("cfg80211: Keeping preference on "
1570 "module parameter ieee80211_regdom: %c%c\n",
1571 ieee80211_regdom[0],
1572 ieee80211_regdom[1]);
1573 alpha2[0] = ieee80211_regdom[0];
1574 alpha2[1] = ieee80211_regdom[1];
1577 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
1578 "while preserving user preference for: %c%c\n",
1581 alpha2[0] = user_alpha2[0];
1582 alpha2[1] = user_alpha2[1];
1584 } else if (!is_world_regdom(ieee80211_regdom)) {
1585 REG_DBG_PRINT("cfg80211: Keeping preference on "
1586 "module parameter ieee80211_regdom: %c%c\n",
1587 ieee80211_regdom[0],
1588 ieee80211_regdom[1]);
1589 alpha2[0] = ieee80211_regdom[0];
1590 alpha2[1] = ieee80211_regdom[1];
1592 REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
1596 * Restoring regulatory settings involves ingoring any
1597 * possibly stale country IE information and user regulatory
1598 * settings if so desired, this includes any beacon hints
1599 * learned as we could have traveled outside to another country
1600 * after disconnection. To restore regulatory settings we do
1601 * exactly what we did at bootup:
1603 * - send a core regulatory hint
1604 * - send a user regulatory hint if applicable
1606 * Device drivers that send a regulatory hint for a specific country
1607 * keep their own regulatory domain on wiphy->regd so that does does
1608 * not need to be remembered.
1610 static void restore_regulatory_settings(bool reset_user)
1613 struct reg_beacon *reg_beacon, *btmp;
1615 mutex_lock(&cfg80211_mutex);
1616 mutex_lock(®_mutex);
1619 restore_alpha2(alpha2, reset_user);
1621 /* Clear beacon hints */
1622 spin_lock_bh(®_pending_beacons_lock);
1623 if (!list_empty(®_pending_beacons)) {
1624 list_for_each_entry_safe(reg_beacon, btmp,
1625 ®_pending_beacons, list) {
1626 list_del(®_beacon->list);
1630 spin_unlock_bh(®_pending_beacons_lock);
1632 if (!list_empty(®_beacon_list)) {
1633 list_for_each_entry_safe(reg_beacon, btmp,
1634 ®_beacon_list, list) {
1635 list_del(®_beacon->list);
1640 /* First restore to the basic regulatory settings */
1641 cfg80211_regdomain = cfg80211_world_regdom;
1643 mutex_unlock(®_mutex);
1644 mutex_unlock(&cfg80211_mutex);
1646 regulatory_hint_core(cfg80211_regdomain->alpha2);
1649 * This restores the ieee80211_regdom module parameter
1650 * preference or the last user requested regulatory
1651 * settings, user regulatory settings takes precedence.
1653 if (is_an_alpha2(alpha2))
1654 regulatory_hint_user(user_alpha2);
1658 void regulatory_hint_disconnect(void)
1660 REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
1661 "restore regulatory settings\n");
1662 restore_regulatory_settings(false);
1665 static bool freq_is_chan_12_13_14(u16 freq)
1667 if (freq == ieee80211_channel_to_frequency(12) ||
1668 freq == ieee80211_channel_to_frequency(13) ||
1669 freq == ieee80211_channel_to_frequency(14))
1674 int regulatory_hint_found_beacon(struct wiphy *wiphy,
1675 struct ieee80211_channel *beacon_chan,
1678 struct reg_beacon *reg_beacon;
1680 if (likely((beacon_chan->beacon_found ||
1681 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1682 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1683 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1686 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1690 REG_DBG_PRINT("cfg80211: Found new beacon on "
1691 "frequency: %d MHz (Ch %d) on %s\n",
1692 beacon_chan->center_freq,
1693 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1696 memcpy(®_beacon->chan, beacon_chan,
1697 sizeof(struct ieee80211_channel));
1701 * Since we can be called from BH or and non-BH context
1702 * we must use spin_lock_bh()
1704 spin_lock_bh(®_pending_beacons_lock);
1705 list_add_tail(®_beacon->list, ®_pending_beacons);
1706 spin_unlock_bh(®_pending_beacons_lock);
1708 schedule_work(®_work);
1713 static void print_rd_rules(const struct ieee80211_regdomain *rd)
1716 const struct ieee80211_reg_rule *reg_rule = NULL;
1717 const struct ieee80211_freq_range *freq_range = NULL;
1718 const struct ieee80211_power_rule *power_rule = NULL;
1720 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
1721 "(max_antenna_gain, max_eirp)\n");
1723 for (i = 0; i < rd->n_reg_rules; i++) {
1724 reg_rule = &rd->reg_rules[i];
1725 freq_range = ®_rule->freq_range;
1726 power_rule = ®_rule->power_rule;
1729 * There may not be documentation for max antenna gain
1730 * in certain regions
1732 if (power_rule->max_antenna_gain)
1733 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
1734 "(%d mBi, %d mBm)\n",
1735 freq_range->start_freq_khz,
1736 freq_range->end_freq_khz,
1737 freq_range->max_bandwidth_khz,
1738 power_rule->max_antenna_gain,
1739 power_rule->max_eirp);
1741 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
1743 freq_range->start_freq_khz,
1744 freq_range->end_freq_khz,
1745 freq_range->max_bandwidth_khz,
1746 power_rule->max_eirp);
1750 static void print_regdomain(const struct ieee80211_regdomain *rd)
1753 if (is_intersected_alpha2(rd->alpha2)) {
1755 if (last_request->initiator ==
1756 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1757 struct cfg80211_registered_device *rdev;
1758 rdev = cfg80211_rdev_by_wiphy_idx(
1759 last_request->wiphy_idx);
1761 printk(KERN_INFO "cfg80211: Current regulatory "
1762 "domain updated by AP to: %c%c\n",
1763 rdev->country_ie_alpha2[0],
1764 rdev->country_ie_alpha2[1]);
1766 printk(KERN_INFO "cfg80211: Current regulatory "
1767 "domain intersected:\n");
1769 printk(KERN_INFO "cfg80211: Current regulatory "
1770 "domain intersected:\n");
1771 } else if (is_world_regdom(rd->alpha2))
1772 printk(KERN_INFO "cfg80211: World regulatory "
1773 "domain updated:\n");
1775 if (is_unknown_alpha2(rd->alpha2))
1776 printk(KERN_INFO "cfg80211: Regulatory domain "
1777 "changed to driver built-in settings "
1778 "(unknown country)\n");
1780 printk(KERN_INFO "cfg80211: Regulatory domain "
1781 "changed to country: %c%c\n",
1782 rd->alpha2[0], rd->alpha2[1]);
1787 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
1789 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1790 rd->alpha2[0], rd->alpha2[1]);
1794 /* Takes ownership of rd only if it doesn't fail */
1795 static int __set_regdom(const struct ieee80211_regdomain *rd)
1797 const struct ieee80211_regdomain *intersected_rd = NULL;
1798 struct cfg80211_registered_device *rdev = NULL;
1799 struct wiphy *request_wiphy;
1800 /* Some basic sanity checks first */
1802 if (is_world_regdom(rd->alpha2)) {
1803 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1805 update_world_regdomain(rd);
1809 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1810 !is_unknown_alpha2(rd->alpha2))
1817 * Lets only bother proceeding on the same alpha2 if the current
1818 * rd is non static (it means CRDA was present and was used last)
1819 * and the pending request came in from a country IE
1821 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1823 * If someone else asked us to change the rd lets only bother
1824 * checking if the alpha2 changes if CRDA was already called
1826 if (!regdom_changes(rd->alpha2))
1831 * Now lets set the regulatory domain, update all driver channels
1832 * and finally inform them of what we have done, in case they want
1833 * to review or adjust their own settings based on their own
1834 * internal EEPROM data
1837 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1840 if (!is_valid_rd(rd)) {
1841 printk(KERN_ERR "cfg80211: Invalid "
1842 "regulatory domain detected:\n");
1843 print_regdomain_info(rd);
1847 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1849 if (!last_request->intersect) {
1852 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
1854 cfg80211_regdomain = rd;
1859 * For a driver hint, lets copy the regulatory domain the
1860 * driver wanted to the wiphy to deal with conflicts
1864 * Userspace could have sent two replies with only
1865 * one kernel request.
1867 if (request_wiphy->regd)
1870 r = reg_copy_regd(&request_wiphy->regd, rd);
1875 cfg80211_regdomain = rd;
1879 /* Intersection requires a bit more work */
1881 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1883 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1884 if (!intersected_rd)
1888 * We can trash what CRDA provided now.
1889 * However if a driver requested this specific regulatory
1890 * domain we keep it for its private use
1892 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
1893 request_wiphy->regd = rd;
1900 cfg80211_regdomain = intersected_rd;
1905 if (!intersected_rd)
1908 rdev = wiphy_to_dev(request_wiphy);
1910 rdev->country_ie_alpha2[0] = rd->alpha2[0];
1911 rdev->country_ie_alpha2[1] = rd->alpha2[1];
1912 rdev->env = last_request->country_ie_env;
1914 BUG_ON(intersected_rd == rd);
1920 cfg80211_regdomain = intersected_rd;
1927 * Use this call to set the current regulatory domain. Conflicts with
1928 * multiple drivers can be ironed out later. Caller must've already
1929 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
1931 int set_regdom(const struct ieee80211_regdomain *rd)
1935 assert_cfg80211_lock();
1937 mutex_lock(®_mutex);
1939 /* Note that this doesn't update the wiphys, this is done below */
1940 r = __set_regdom(rd);
1943 mutex_unlock(®_mutex);
1947 /* This would make this whole thing pointless */
1948 if (!last_request->intersect)
1949 BUG_ON(rd != cfg80211_regdomain);
1951 /* update all wiphys now with the new established regulatory domain */
1952 update_all_wiphy_regulatory(last_request->initiator);
1954 print_regdomain(cfg80211_regdomain);
1956 nl80211_send_reg_change_event(last_request);
1958 mutex_unlock(®_mutex);
1963 /* Caller must hold cfg80211_mutex */
1964 void reg_device_remove(struct wiphy *wiphy)
1966 struct wiphy *request_wiphy = NULL;
1968 assert_cfg80211_lock();
1970 mutex_lock(®_mutex);
1975 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1977 if (!request_wiphy || request_wiphy != wiphy)
1980 last_request->wiphy_idx = WIPHY_IDX_STALE;
1981 last_request->country_ie_env = ENVIRON_ANY;
1983 mutex_unlock(®_mutex);
1986 int __init regulatory_init(void)
1990 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1991 if (IS_ERR(reg_pdev))
1992 return PTR_ERR(reg_pdev);
1994 spin_lock_init(®_requests_lock);
1995 spin_lock_init(®_pending_beacons_lock);
1997 cfg80211_regdomain = cfg80211_world_regdom;
1999 user_alpha2[0] = '9';
2000 user_alpha2[1] = '7';
2002 /* We always try to get an update for the static regdomain */
2003 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
2008 * N.B. kobject_uevent_env() can fail mainly for when we're out
2009 * memory which is handled and propagated appropriately above
2010 * but it can also fail during a netlink_broadcast() or during
2011 * early boot for call_usermodehelper(). For now treat these
2012 * errors as non-fatal.
2014 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2015 "to call CRDA during init");
2016 #ifdef CONFIG_CFG80211_REG_DEBUG
2017 /* We want to find out exactly why when debugging */
2023 * Finally, if the user set the module parameter treat it
2026 if (!is_world_regdom(ieee80211_regdom))
2027 regulatory_hint_user(ieee80211_regdom);
2032 void /* __init_or_exit */ regulatory_exit(void)
2034 struct regulatory_request *reg_request, *tmp;
2035 struct reg_beacon *reg_beacon, *btmp;
2037 cancel_work_sync(®_work);
2039 mutex_lock(&cfg80211_mutex);
2040 mutex_lock(®_mutex);
2044 kfree(last_request);
2046 platform_device_unregister(reg_pdev);
2048 spin_lock_bh(®_pending_beacons_lock);
2049 if (!list_empty(®_pending_beacons)) {
2050 list_for_each_entry_safe(reg_beacon, btmp,
2051 ®_pending_beacons, list) {
2052 list_del(®_beacon->list);
2056 spin_unlock_bh(®_pending_beacons_lock);
2058 if (!list_empty(®_beacon_list)) {
2059 list_for_each_entry_safe(reg_beacon, btmp,
2060 ®_beacon_list, list) {
2061 list_del(®_beacon->list);
2066 spin_lock(®_requests_lock);
2067 if (!list_empty(®_requests_list)) {
2068 list_for_each_entry_safe(reg_request, tmp,
2069 ®_requests_list, list) {
2070 list_del(®_request->list);
2074 spin_unlock(®_requests_lock);
2076 mutex_unlock(®_mutex);
2077 mutex_unlock(&cfg80211_mutex);