writeback: split writeback_inodes_wb
[linux-2.6/next.git] / net / wireless / reg.c
blob8f0d97dd3109c54cd85fe01095308171d34d9f01
1 /*
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.
12 /**
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/nl80211.h>
40 #include <linux/platform_device.h>
41 #include <net/cfg80211.h>
42 #include "core.h"
43 #include "reg.h"
44 #include "regdb.h"
45 #include "nl80211.h"
47 #ifdef CONFIG_CFG80211_REG_DEBUG
48 #define REG_DBG_PRINT(format, args...) \
49 do { \
50 printk(KERN_DEBUG format , ## args); \
51 } while (0)
52 #else
53 #define REG_DBG_PRINT(args...)
54 #endif
56 /* Receipt of information from last regulatory request */
57 static struct regulatory_request *last_request;
59 /* To trigger userspace events */
60 static struct platform_device *reg_pdev;
63 * Central wireless core regulatory domains, we only need two,
64 * the current one and a world regulatory domain in case we have no
65 * information to give us an alpha2
67 const struct ieee80211_regdomain *cfg80211_regdomain;
70 * We use this as a place for the rd structure built from the
71 * last parsed country IE to rest until CRDA gets back to us with
72 * what it thinks should apply for the same country
74 static const struct ieee80211_regdomain *country_ie_regdomain;
77 * Protects static reg.c components:
78 * - cfg80211_world_regdom
79 * - cfg80211_regdom
80 * - country_ie_regdomain
81 * - last_request
83 DEFINE_MUTEX(reg_mutex);
84 #define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
86 /* Used to queue up regulatory hints */
87 static LIST_HEAD(reg_requests_list);
88 static spinlock_t reg_requests_lock;
90 /* Used to queue up beacon hints for review */
91 static LIST_HEAD(reg_pending_beacons);
92 static spinlock_t reg_pending_beacons_lock;
94 /* Used to keep track of processed beacon hints */
95 static LIST_HEAD(reg_beacon_list);
97 struct reg_beacon {
98 struct list_head list;
99 struct ieee80211_channel chan;
102 /* We keep a static world regulatory domain in case of the absence of CRDA */
103 static const struct ieee80211_regdomain world_regdom = {
104 .n_reg_rules = 5,
105 .alpha2 = "00",
106 .reg_rules = {
107 /* IEEE 802.11b/g, channels 1..11 */
108 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
109 /* IEEE 802.11b/g, channels 12..13. No HT40
110 * channel fits here. */
111 REG_RULE(2467-10, 2472+10, 20, 6, 20,
112 NL80211_RRF_PASSIVE_SCAN |
113 NL80211_RRF_NO_IBSS),
114 /* IEEE 802.11 channel 14 - Only JP enables
115 * this and for 802.11b only */
116 REG_RULE(2484-10, 2484+10, 20, 6, 20,
117 NL80211_RRF_PASSIVE_SCAN |
118 NL80211_RRF_NO_IBSS |
119 NL80211_RRF_NO_OFDM),
120 /* IEEE 802.11a, channel 36..48 */
121 REG_RULE(5180-10, 5240+10, 40, 6, 20,
122 NL80211_RRF_PASSIVE_SCAN |
123 NL80211_RRF_NO_IBSS),
125 /* NB: 5260 MHz - 5700 MHz requies DFS */
127 /* IEEE 802.11a, channel 149..165 */
128 REG_RULE(5745-10, 5825+10, 40, 6, 20,
129 NL80211_RRF_PASSIVE_SCAN |
130 NL80211_RRF_NO_IBSS),
134 static const struct ieee80211_regdomain *cfg80211_world_regdom =
135 &world_regdom;
137 static char *ieee80211_regdom = "00";
138 static char user_alpha2[2];
140 module_param(ieee80211_regdom, charp, 0444);
141 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
143 static void reset_regdomains(void)
145 /* avoid freeing static information or freeing something twice */
146 if (cfg80211_regdomain == cfg80211_world_regdom)
147 cfg80211_regdomain = NULL;
148 if (cfg80211_world_regdom == &world_regdom)
149 cfg80211_world_regdom = NULL;
150 if (cfg80211_regdomain == &world_regdom)
151 cfg80211_regdomain = NULL;
153 kfree(cfg80211_regdomain);
154 kfree(cfg80211_world_regdom);
156 cfg80211_world_regdom = &world_regdom;
157 cfg80211_regdomain = NULL;
161 * Dynamic world regulatory domain requested by the wireless
162 * core upon initialization
164 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
166 BUG_ON(!last_request);
168 reset_regdomains();
170 cfg80211_world_regdom = rd;
171 cfg80211_regdomain = rd;
174 bool is_world_regdom(const char *alpha2)
176 if (!alpha2)
177 return false;
178 if (alpha2[0] == '0' && alpha2[1] == '0')
179 return true;
180 return false;
183 static bool is_alpha2_set(const char *alpha2)
185 if (!alpha2)
186 return false;
187 if (alpha2[0] != 0 && alpha2[1] != 0)
188 return true;
189 return false;
192 static bool is_alpha_upper(char letter)
194 /* ASCII A - Z */
195 if (letter >= 65 && letter <= 90)
196 return true;
197 return false;
200 static bool is_unknown_alpha2(const char *alpha2)
202 if (!alpha2)
203 return false;
205 * Special case where regulatory domain was built by driver
206 * but a specific alpha2 cannot be determined
208 if (alpha2[0] == '9' && alpha2[1] == '9')
209 return true;
210 return false;
213 static bool is_intersected_alpha2(const char *alpha2)
215 if (!alpha2)
216 return false;
218 * Special case where regulatory domain is the
219 * result of an intersection between two regulatory domain
220 * structures
222 if (alpha2[0] == '9' && alpha2[1] == '8')
223 return true;
224 return false;
227 static bool is_an_alpha2(const char *alpha2)
229 if (!alpha2)
230 return false;
231 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
232 return true;
233 return false;
236 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
238 if (!alpha2_x || !alpha2_y)
239 return false;
240 if (alpha2_x[0] == alpha2_y[0] &&
241 alpha2_x[1] == alpha2_y[1])
242 return true;
243 return false;
246 static bool regdom_changes(const char *alpha2)
248 assert_cfg80211_lock();
250 if (!cfg80211_regdomain)
251 return true;
252 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
253 return false;
254 return true;
258 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
259 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
260 * has ever been issued.
262 static bool is_user_regdom_saved(void)
264 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
265 return false;
267 /* This would indicate a mistake on the design */
268 if (WARN((!is_world_regdom(user_alpha2) &&
269 !is_an_alpha2(user_alpha2)),
270 "Unexpected user alpha2: %c%c\n",
271 user_alpha2[0],
272 user_alpha2[1]))
273 return false;
275 return true;
279 * country_ie_integrity_changes - tells us if the country IE has changed
280 * @checksum: checksum of country IE of fields we are interested in
282 * If the country IE has not changed you can ignore it safely. This is
283 * useful to determine if two devices are seeing two different country IEs
284 * even on the same alpha2. Note that this will return false if no IE has
285 * been set on the wireless core yet.
287 static bool country_ie_integrity_changes(u32 checksum)
289 /* If no IE has been set then the checksum doesn't change */
290 if (unlikely(!last_request->country_ie_checksum))
291 return false;
292 if (unlikely(last_request->country_ie_checksum != checksum))
293 return true;
294 return false;
297 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
298 const struct ieee80211_regdomain *src_regd)
300 struct ieee80211_regdomain *regd;
301 int size_of_regd = 0;
302 unsigned int i;
304 size_of_regd = sizeof(struct ieee80211_regdomain) +
305 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
307 regd = kzalloc(size_of_regd, GFP_KERNEL);
308 if (!regd)
309 return -ENOMEM;
311 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
313 for (i = 0; i < src_regd->n_reg_rules; i++)
314 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
315 sizeof(struct ieee80211_reg_rule));
317 *dst_regd = regd;
318 return 0;
321 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
322 struct reg_regdb_search_request {
323 char alpha2[2];
324 struct list_head list;
327 static LIST_HEAD(reg_regdb_search_list);
328 static DEFINE_MUTEX(reg_regdb_search_mutex);
330 static void reg_regdb_search(struct work_struct *work)
332 struct reg_regdb_search_request *request;
333 const struct ieee80211_regdomain *curdom, *regdom;
334 int i, r;
336 mutex_lock(&reg_regdb_search_mutex);
337 while (!list_empty(&reg_regdb_search_list)) {
338 request = list_first_entry(&reg_regdb_search_list,
339 struct reg_regdb_search_request,
340 list);
341 list_del(&request->list);
343 for (i=0; i<reg_regdb_size; i++) {
344 curdom = reg_regdb[i];
346 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
347 r = reg_copy_regd(&regdom, curdom);
348 if (r)
349 break;
350 mutex_lock(&cfg80211_mutex);
351 set_regdom(regdom);
352 mutex_unlock(&cfg80211_mutex);
353 break;
357 kfree(request);
359 mutex_unlock(&reg_regdb_search_mutex);
362 static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
364 static void reg_regdb_query(const char *alpha2)
366 struct reg_regdb_search_request *request;
368 if (!alpha2)
369 return;
371 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
372 if (!request)
373 return;
375 memcpy(request->alpha2, alpha2, 2);
377 mutex_lock(&reg_regdb_search_mutex);
378 list_add_tail(&request->list, &reg_regdb_search_list);
379 mutex_unlock(&reg_regdb_search_mutex);
381 schedule_work(&reg_regdb_work);
383 #else
384 static inline void reg_regdb_query(const char *alpha2) {}
385 #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
388 * This lets us keep regulatory code which is updated on a regulatory
389 * basis in userspace.
391 static int call_crda(const char *alpha2)
393 char country_env[9 + 2] = "COUNTRY=";
394 char *envp[] = {
395 country_env,
396 NULL
399 if (!is_world_regdom((char *) alpha2))
400 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
401 alpha2[0], alpha2[1]);
402 else
403 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
404 "regulatory domain\n");
406 /* query internal regulatory database (if it exists) */
407 reg_regdb_query(alpha2);
409 country_env[8] = alpha2[0];
410 country_env[9] = alpha2[1];
412 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
415 /* Used by nl80211 before kmalloc'ing our regulatory domain */
416 bool reg_is_valid_request(const char *alpha2)
418 assert_cfg80211_lock();
420 if (!last_request)
421 return false;
423 return alpha2_equal(last_request->alpha2, alpha2);
426 /* Sanity check on a regulatory rule */
427 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
429 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
430 u32 freq_diff;
432 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
433 return false;
435 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
436 return false;
438 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
440 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
441 freq_range->max_bandwidth_khz > freq_diff)
442 return false;
444 return true;
447 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
449 const struct ieee80211_reg_rule *reg_rule = NULL;
450 unsigned int i;
452 if (!rd->n_reg_rules)
453 return false;
455 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
456 return false;
458 for (i = 0; i < rd->n_reg_rules; i++) {
459 reg_rule = &rd->reg_rules[i];
460 if (!is_valid_reg_rule(reg_rule))
461 return false;
464 return true;
467 static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
468 u32 center_freq_khz,
469 u32 bw_khz)
471 u32 start_freq_khz, end_freq_khz;
473 start_freq_khz = center_freq_khz - (bw_khz/2);
474 end_freq_khz = center_freq_khz + (bw_khz/2);
476 if (start_freq_khz >= freq_range->start_freq_khz &&
477 end_freq_khz <= freq_range->end_freq_khz)
478 return true;
480 return false;
484 * freq_in_rule_band - tells us if a frequency is in a frequency band
485 * @freq_range: frequency rule we want to query
486 * @freq_khz: frequency we are inquiring about
488 * This lets us know if a specific frequency rule is or is not relevant to
489 * a specific frequency's band. Bands are device specific and artificial
490 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
491 * safe for now to assume that a frequency rule should not be part of a
492 * frequency's band if the start freq or end freq are off by more than 2 GHz.
493 * This resolution can be lowered and should be considered as we add
494 * regulatory rule support for other "bands".
496 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
497 u32 freq_khz)
499 #define ONE_GHZ_IN_KHZ 1000000
500 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
501 return true;
502 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
503 return true;
504 return false;
505 #undef ONE_GHZ_IN_KHZ
509 * This is a work around for sanity checking ieee80211_channel_to_frequency()'s
510 * work. ieee80211_channel_to_frequency() can for example currently provide a
511 * 2 GHz channel when in fact a 5 GHz channel was desired. An example would be
512 * an AP providing channel 8 on a country IE triplet when it sent this on the
513 * 5 GHz band, that channel is designed to be channel 8 on 5 GHz, not a 2 GHz
514 * channel.
516 * This can be removed once ieee80211_channel_to_frequency() takes in a band.
518 static bool chan_in_band(int chan, enum ieee80211_band band)
520 int center_freq = ieee80211_channel_to_frequency(chan);
522 switch (band) {
523 case IEEE80211_BAND_2GHZ:
524 if (center_freq <= 2484)
525 return true;
526 return false;
527 case IEEE80211_BAND_5GHZ:
528 if (center_freq >= 5005)
529 return true;
530 return false;
531 default:
532 return false;
537 * Some APs may send a country IE triplet for each channel they
538 * support and while this is completely overkill and silly we still
539 * need to support it. We avoid making a single rule for each channel
540 * though and to help us with this we use this helper to find the
541 * actual subband end channel. These type of country IE triplet
542 * scenerios are handled then, all yielding two regulaotry rules from
543 * parsing a country IE:
545 * [1]
546 * [2]
547 * [36]
548 * [40]
550 * [1]
551 * [2-4]
552 * [5-12]
553 * [36]
554 * [40-44]
556 * [1-4]
557 * [5-7]
558 * [36-44]
559 * [48-64]
561 * [36-36]
562 * [40-40]
563 * [44-44]
564 * [48-48]
565 * [52-52]
566 * [56-56]
567 * [60-60]
568 * [64-64]
569 * [100-100]
570 * [104-104]
571 * [108-108]
572 * [112-112]
573 * [116-116]
574 * [120-120]
575 * [124-124]
576 * [128-128]
577 * [132-132]
578 * [136-136]
579 * [140-140]
581 * Returns 0 if the IE has been found to be invalid in the middle
582 * somewhere.
584 static int max_subband_chan(enum ieee80211_band band,
585 int orig_cur_chan,
586 int orig_end_channel,
587 s8 orig_max_power,
588 u8 **country_ie,
589 u8 *country_ie_len)
591 u8 *triplets_start = *country_ie;
592 u8 len_at_triplet = *country_ie_len;
593 int end_subband_chan = orig_end_channel;
596 * We'll deal with padding for the caller unless
597 * its not immediate and we don't process any channels
599 if (*country_ie_len == 1) {
600 *country_ie += 1;
601 *country_ie_len -= 1;
602 return orig_end_channel;
605 /* Move to the next triplet and then start search */
606 *country_ie += 3;
607 *country_ie_len -= 3;
609 if (!chan_in_band(orig_cur_chan, band))
610 return 0;
612 while (*country_ie_len >= 3) {
613 int end_channel = 0;
614 struct ieee80211_country_ie_triplet *triplet =
615 (struct ieee80211_country_ie_triplet *) *country_ie;
616 int cur_channel = 0, next_expected_chan;
618 /* means last triplet is completely unrelated to this one */
619 if (triplet->ext.reg_extension_id >=
620 IEEE80211_COUNTRY_EXTENSION_ID) {
621 *country_ie -= 3;
622 *country_ie_len += 3;
623 break;
626 if (triplet->chans.first_channel == 0) {
627 *country_ie += 1;
628 *country_ie_len -= 1;
629 if (*country_ie_len != 0)
630 return 0;
631 break;
634 if (triplet->chans.num_channels == 0)
635 return 0;
637 /* Monitonically increasing channel order */
638 if (triplet->chans.first_channel <= end_subband_chan)
639 return 0;
641 if (!chan_in_band(triplet->chans.first_channel, band))
642 return 0;
644 /* 2 GHz */
645 if (triplet->chans.first_channel <= 14) {
646 end_channel = triplet->chans.first_channel +
647 triplet->chans.num_channels - 1;
649 else {
650 end_channel = triplet->chans.first_channel +
651 (4 * (triplet->chans.num_channels - 1));
654 if (!chan_in_band(end_channel, band))
655 return 0;
657 if (orig_max_power != triplet->chans.max_power) {
658 *country_ie -= 3;
659 *country_ie_len += 3;
660 break;
663 cur_channel = triplet->chans.first_channel;
665 /* The key is finding the right next expected channel */
666 if (band == IEEE80211_BAND_2GHZ)
667 next_expected_chan = end_subband_chan + 1;
668 else
669 next_expected_chan = end_subband_chan + 4;
671 if (cur_channel != next_expected_chan) {
672 *country_ie -= 3;
673 *country_ie_len += 3;
674 break;
677 end_subband_chan = end_channel;
679 /* Move to the next one */
680 *country_ie += 3;
681 *country_ie_len -= 3;
684 * Padding needs to be dealt with if we processed
685 * some channels.
687 if (*country_ie_len == 1) {
688 *country_ie += 1;
689 *country_ie_len -= 1;
690 break;
693 /* If seen, the IE is invalid */
694 if (*country_ie_len == 2)
695 return 0;
698 if (end_subband_chan == orig_end_channel) {
699 *country_ie = triplets_start;
700 *country_ie_len = len_at_triplet;
701 return orig_end_channel;
704 return end_subband_chan;
708 * Converts a country IE to a regulatory domain. A regulatory domain
709 * structure has a lot of information which the IE doesn't yet have,
710 * so for the other values we use upper max values as we will intersect
711 * with our userspace regulatory agent to get lower bounds.
713 static struct ieee80211_regdomain *country_ie_2_rd(
714 enum ieee80211_band band,
715 u8 *country_ie,
716 u8 country_ie_len,
717 u32 *checksum)
719 struct ieee80211_regdomain *rd = NULL;
720 unsigned int i = 0;
721 char alpha2[2];
722 u32 flags = 0;
723 u32 num_rules = 0, size_of_regd = 0;
724 u8 *triplets_start = NULL;
725 u8 len_at_triplet = 0;
726 /* the last channel we have registered in a subband (triplet) */
727 int last_sub_max_channel = 0;
729 *checksum = 0xDEADBEEF;
731 /* Country IE requirements */
732 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
733 country_ie_len & 0x01);
735 alpha2[0] = country_ie[0];
736 alpha2[1] = country_ie[1];
739 * Third octet can be:
740 * 'I' - Indoor
741 * 'O' - Outdoor
743 * anything else we assume is no restrictions
745 if (country_ie[2] == 'I')
746 flags = NL80211_RRF_NO_OUTDOOR;
747 else if (country_ie[2] == 'O')
748 flags = NL80211_RRF_NO_INDOOR;
750 country_ie += 3;
751 country_ie_len -= 3;
753 triplets_start = country_ie;
754 len_at_triplet = country_ie_len;
756 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
759 * We need to build a reg rule for each triplet, but first we must
760 * calculate the number of reg rules we will need. We will need one
761 * for each channel subband
763 while (country_ie_len >= 3) {
764 int end_channel = 0;
765 struct ieee80211_country_ie_triplet *triplet =
766 (struct ieee80211_country_ie_triplet *) country_ie;
767 int cur_sub_max_channel = 0, cur_channel = 0;
769 if (triplet->ext.reg_extension_id >=
770 IEEE80211_COUNTRY_EXTENSION_ID) {
771 country_ie += 3;
772 country_ie_len -= 3;
773 continue;
777 * APs can add padding to make length divisible
778 * by two, required by the spec.
780 if (triplet->chans.first_channel == 0) {
781 country_ie++;
782 country_ie_len--;
783 /* This is expected to be at the very end only */
784 if (country_ie_len != 0)
785 return NULL;
786 break;
789 if (triplet->chans.num_channels == 0)
790 return NULL;
792 if (!chan_in_band(triplet->chans.first_channel, band))
793 return NULL;
795 /* 2 GHz */
796 if (band == IEEE80211_BAND_2GHZ)
797 end_channel = triplet->chans.first_channel +
798 triplet->chans.num_channels - 1;
799 else
801 * 5 GHz -- For example in country IEs if the first
802 * channel given is 36 and the number of channels is 4
803 * then the individual channel numbers defined for the
804 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
805 * and not 36, 37, 38, 39.
807 * See: http://tinyurl.com/11d-clarification
809 end_channel = triplet->chans.first_channel +
810 (4 * (triplet->chans.num_channels - 1));
812 cur_channel = triplet->chans.first_channel;
815 * Enhancement for APs that send a triplet for every channel
816 * or for whatever reason sends triplets with multiple channels
817 * separated when in fact they should be together.
819 end_channel = max_subband_chan(band,
820 cur_channel,
821 end_channel,
822 triplet->chans.max_power,
823 &country_ie,
824 &country_ie_len);
825 if (!end_channel)
826 return NULL;
828 if (!chan_in_band(end_channel, band))
829 return NULL;
831 cur_sub_max_channel = end_channel;
833 /* Basic sanity check */
834 if (cur_sub_max_channel < cur_channel)
835 return NULL;
838 * Do not allow overlapping channels. Also channels
839 * passed in each subband must be monotonically
840 * increasing
842 if (last_sub_max_channel) {
843 if (cur_channel <= last_sub_max_channel)
844 return NULL;
845 if (cur_sub_max_channel <= last_sub_max_channel)
846 return NULL;
850 * When dot11RegulatoryClassesRequired is supported
851 * we can throw ext triplets as part of this soup,
852 * for now we don't care when those change as we
853 * don't support them
855 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
856 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
857 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
859 last_sub_max_channel = cur_sub_max_channel;
861 num_rules++;
863 if (country_ie_len >= 3) {
864 country_ie += 3;
865 country_ie_len -= 3;
869 * Note: this is not a IEEE requirement but
870 * simply a memory requirement
872 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
873 return NULL;
876 country_ie = triplets_start;
877 country_ie_len = len_at_triplet;
879 size_of_regd = sizeof(struct ieee80211_regdomain) +
880 (num_rules * sizeof(struct ieee80211_reg_rule));
882 rd = kzalloc(size_of_regd, GFP_KERNEL);
883 if (!rd)
884 return NULL;
886 rd->n_reg_rules = num_rules;
887 rd->alpha2[0] = alpha2[0];
888 rd->alpha2[1] = alpha2[1];
890 /* This time around we fill in the rd */
891 while (country_ie_len >= 3) {
892 int end_channel = 0;
893 struct ieee80211_country_ie_triplet *triplet =
894 (struct ieee80211_country_ie_triplet *) country_ie;
895 struct ieee80211_reg_rule *reg_rule = NULL;
896 struct ieee80211_freq_range *freq_range = NULL;
897 struct ieee80211_power_rule *power_rule = NULL;
900 * Must parse if dot11RegulatoryClassesRequired is true,
901 * we don't support this yet
903 if (triplet->ext.reg_extension_id >=
904 IEEE80211_COUNTRY_EXTENSION_ID) {
905 country_ie += 3;
906 country_ie_len -= 3;
907 continue;
910 if (triplet->chans.first_channel == 0) {
911 country_ie++;
912 country_ie_len--;
913 break;
916 reg_rule = &rd->reg_rules[i];
917 freq_range = &reg_rule->freq_range;
918 power_rule = &reg_rule->power_rule;
920 reg_rule->flags = flags;
922 /* 2 GHz */
923 if (band == IEEE80211_BAND_2GHZ)
924 end_channel = triplet->chans.first_channel +
925 triplet->chans.num_channels -1;
926 else
927 end_channel = triplet->chans.first_channel +
928 (4 * (triplet->chans.num_channels - 1));
930 end_channel = max_subband_chan(band,
931 triplet->chans.first_channel,
932 end_channel,
933 triplet->chans.max_power,
934 &country_ie,
935 &country_ie_len);
938 * The +10 is since the regulatory domain expects
939 * the actual band edge, not the center of freq for
940 * its start and end freqs, assuming 20 MHz bandwidth on
941 * the channels passed
943 freq_range->start_freq_khz =
944 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
945 triplet->chans.first_channel) - 10);
946 freq_range->end_freq_khz =
947 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
948 end_channel) + 10);
951 * These are large arbitrary values we use to intersect later.
952 * Increment this if we ever support >= 40 MHz channels
953 * in IEEE 802.11
955 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
956 power_rule->max_antenna_gain = DBI_TO_MBI(100);
957 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
959 i++;
961 if (country_ie_len >= 3) {
962 country_ie += 3;
963 country_ie_len -= 3;
966 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
969 return rd;
974 * Helper for regdom_intersect(), this does the real
975 * mathematical intersection fun
977 static int reg_rules_intersect(
978 const struct ieee80211_reg_rule *rule1,
979 const struct ieee80211_reg_rule *rule2,
980 struct ieee80211_reg_rule *intersected_rule)
982 const struct ieee80211_freq_range *freq_range1, *freq_range2;
983 struct ieee80211_freq_range *freq_range;
984 const struct ieee80211_power_rule *power_rule1, *power_rule2;
985 struct ieee80211_power_rule *power_rule;
986 u32 freq_diff;
988 freq_range1 = &rule1->freq_range;
989 freq_range2 = &rule2->freq_range;
990 freq_range = &intersected_rule->freq_range;
992 power_rule1 = &rule1->power_rule;
993 power_rule2 = &rule2->power_rule;
994 power_rule = &intersected_rule->power_rule;
996 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
997 freq_range2->start_freq_khz);
998 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
999 freq_range2->end_freq_khz);
1000 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
1001 freq_range2->max_bandwidth_khz);
1003 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1004 if (freq_range->max_bandwidth_khz > freq_diff)
1005 freq_range->max_bandwidth_khz = freq_diff;
1007 power_rule->max_eirp = min(power_rule1->max_eirp,
1008 power_rule2->max_eirp);
1009 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
1010 power_rule2->max_antenna_gain);
1012 intersected_rule->flags = (rule1->flags | rule2->flags);
1014 if (!is_valid_reg_rule(intersected_rule))
1015 return -EINVAL;
1017 return 0;
1021 * regdom_intersect - do the intersection between two regulatory domains
1022 * @rd1: first regulatory domain
1023 * @rd2: second regulatory domain
1025 * Use this function to get the intersection between two regulatory domains.
1026 * Once completed we will mark the alpha2 for the rd as intersected, "98",
1027 * as no one single alpha2 can represent this regulatory domain.
1029 * Returns a pointer to the regulatory domain structure which will hold the
1030 * resulting intersection of rules between rd1 and rd2. We will
1031 * kzalloc() this structure for you.
1033 static struct ieee80211_regdomain *regdom_intersect(
1034 const struct ieee80211_regdomain *rd1,
1035 const struct ieee80211_regdomain *rd2)
1037 int r, size_of_regd;
1038 unsigned int x, y;
1039 unsigned int num_rules = 0, rule_idx = 0;
1040 const struct ieee80211_reg_rule *rule1, *rule2;
1041 struct ieee80211_reg_rule *intersected_rule;
1042 struct ieee80211_regdomain *rd;
1043 /* This is just a dummy holder to help us count */
1044 struct ieee80211_reg_rule irule;
1046 /* Uses the stack temporarily for counter arithmetic */
1047 intersected_rule = &irule;
1049 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
1051 if (!rd1 || !rd2)
1052 return NULL;
1055 * First we get a count of the rules we'll need, then we actually
1056 * build them. This is to so we can malloc() and free() a
1057 * regdomain once. The reason we use reg_rules_intersect() here
1058 * is it will return -EINVAL if the rule computed makes no sense.
1059 * All rules that do check out OK are valid.
1062 for (x = 0; x < rd1->n_reg_rules; x++) {
1063 rule1 = &rd1->reg_rules[x];
1064 for (y = 0; y < rd2->n_reg_rules; y++) {
1065 rule2 = &rd2->reg_rules[y];
1066 if (!reg_rules_intersect(rule1, rule2,
1067 intersected_rule))
1068 num_rules++;
1069 memset(intersected_rule, 0,
1070 sizeof(struct ieee80211_reg_rule));
1074 if (!num_rules)
1075 return NULL;
1077 size_of_regd = sizeof(struct ieee80211_regdomain) +
1078 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
1080 rd = kzalloc(size_of_regd, GFP_KERNEL);
1081 if (!rd)
1082 return NULL;
1084 for (x = 0; x < rd1->n_reg_rules; x++) {
1085 rule1 = &rd1->reg_rules[x];
1086 for (y = 0; y < rd2->n_reg_rules; y++) {
1087 rule2 = &rd2->reg_rules[y];
1089 * This time around instead of using the stack lets
1090 * write to the target rule directly saving ourselves
1091 * a memcpy()
1093 intersected_rule = &rd->reg_rules[rule_idx];
1094 r = reg_rules_intersect(rule1, rule2,
1095 intersected_rule);
1097 * No need to memset here the intersected rule here as
1098 * we're not using the stack anymore
1100 if (r)
1101 continue;
1102 rule_idx++;
1106 if (rule_idx != num_rules) {
1107 kfree(rd);
1108 return NULL;
1111 rd->n_reg_rules = num_rules;
1112 rd->alpha2[0] = '9';
1113 rd->alpha2[1] = '8';
1115 return rd;
1119 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1120 * want to just have the channel structure use these
1122 static u32 map_regdom_flags(u32 rd_flags)
1124 u32 channel_flags = 0;
1125 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
1126 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1127 if (rd_flags & NL80211_RRF_NO_IBSS)
1128 channel_flags |= IEEE80211_CHAN_NO_IBSS;
1129 if (rd_flags & NL80211_RRF_DFS)
1130 channel_flags |= IEEE80211_CHAN_RADAR;
1131 return channel_flags;
1134 static int freq_reg_info_regd(struct wiphy *wiphy,
1135 u32 center_freq,
1136 u32 desired_bw_khz,
1137 const struct ieee80211_reg_rule **reg_rule,
1138 const struct ieee80211_regdomain *custom_regd)
1140 int i;
1141 bool band_rule_found = false;
1142 const struct ieee80211_regdomain *regd;
1143 bool bw_fits = false;
1145 if (!desired_bw_khz)
1146 desired_bw_khz = MHZ_TO_KHZ(20);
1148 regd = custom_regd ? custom_regd : cfg80211_regdomain;
1151 * Follow the driver's regulatory domain, if present, unless a country
1152 * IE has been processed or a user wants to help complaince further
1154 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1155 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
1156 wiphy->regd)
1157 regd = wiphy->regd;
1159 if (!regd)
1160 return -EINVAL;
1162 for (i = 0; i < regd->n_reg_rules; i++) {
1163 const struct ieee80211_reg_rule *rr;
1164 const struct ieee80211_freq_range *fr = NULL;
1165 const struct ieee80211_power_rule *pr = NULL;
1167 rr = &regd->reg_rules[i];
1168 fr = &rr->freq_range;
1169 pr = &rr->power_rule;
1172 * We only need to know if one frequency rule was
1173 * was in center_freq's band, that's enough, so lets
1174 * not overwrite it once found
1176 if (!band_rule_found)
1177 band_rule_found = freq_in_rule_band(fr, center_freq);
1179 bw_fits = reg_does_bw_fit(fr,
1180 center_freq,
1181 desired_bw_khz);
1183 if (band_rule_found && bw_fits) {
1184 *reg_rule = rr;
1185 return 0;
1189 if (!band_rule_found)
1190 return -ERANGE;
1192 return -EINVAL;
1194 EXPORT_SYMBOL(freq_reg_info);
1196 int freq_reg_info(struct wiphy *wiphy,
1197 u32 center_freq,
1198 u32 desired_bw_khz,
1199 const struct ieee80211_reg_rule **reg_rule)
1201 assert_cfg80211_lock();
1202 return freq_reg_info_regd(wiphy,
1203 center_freq,
1204 desired_bw_khz,
1205 reg_rule,
1206 NULL);
1210 * Note that right now we assume the desired channel bandwidth
1211 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1212 * per channel, the primary and the extension channel). To support
1213 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1214 * new ieee80211_channel.target_bw and re run the regulatory check
1215 * on the wiphy with the target_bw specified. Then we can simply use
1216 * that below for the desired_bw_khz below.
1218 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1219 unsigned int chan_idx)
1221 int r;
1222 u32 flags, bw_flags = 0;
1223 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1224 const struct ieee80211_reg_rule *reg_rule = NULL;
1225 const struct ieee80211_power_rule *power_rule = NULL;
1226 const struct ieee80211_freq_range *freq_range = NULL;
1227 struct ieee80211_supported_band *sband;
1228 struct ieee80211_channel *chan;
1229 struct wiphy *request_wiphy = NULL;
1231 assert_cfg80211_lock();
1233 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1235 sband = wiphy->bands[band];
1236 BUG_ON(chan_idx >= sband->n_channels);
1237 chan = &sband->channels[chan_idx];
1239 flags = chan->orig_flags;
1241 r = freq_reg_info(wiphy,
1242 MHZ_TO_KHZ(chan->center_freq),
1243 desired_bw_khz,
1244 &reg_rule);
1246 if (r) {
1248 * This means no regulatory rule was found in the country IE
1249 * with a frequency range on the center_freq's band, since
1250 * IEEE-802.11 allows for a country IE to have a subset of the
1251 * regulatory information provided in a country we ignore
1252 * disabling the channel unless at least one reg rule was
1253 * found on the center_freq's band. For details see this
1254 * clarification:
1256 * http://tinyurl.com/11d-clarification
1258 if (r == -ERANGE &&
1259 last_request->initiator ==
1260 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1261 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
1262 "intact on %s - no rule found in band on "
1263 "Country IE\n",
1264 chan->center_freq, wiphy_name(wiphy));
1265 } else {
1267 * In this case we know the country IE has at least one reg rule
1268 * for the band so we respect its band definitions
1270 if (last_request->initiator ==
1271 NL80211_REGDOM_SET_BY_COUNTRY_IE)
1272 REG_DBG_PRINT("cfg80211: Disabling "
1273 "channel %d MHz on %s due to "
1274 "Country IE\n",
1275 chan->center_freq, wiphy_name(wiphy));
1276 flags |= IEEE80211_CHAN_DISABLED;
1277 chan->flags = flags;
1279 return;
1282 power_rule = &reg_rule->power_rule;
1283 freq_range = &reg_rule->freq_range;
1285 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1286 bw_flags = IEEE80211_CHAN_NO_HT40;
1288 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1289 request_wiphy && request_wiphy == wiphy &&
1290 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
1292 * This gaurantees the driver's requested regulatory domain
1293 * will always be used as a base for further regulatory
1294 * settings
1296 chan->flags = chan->orig_flags =
1297 map_regdom_flags(reg_rule->flags) | bw_flags;
1298 chan->max_antenna_gain = chan->orig_mag =
1299 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1300 chan->max_power = chan->orig_mpwr =
1301 (int) MBM_TO_DBM(power_rule->max_eirp);
1302 return;
1305 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1306 chan->max_antenna_gain = min(chan->orig_mag,
1307 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
1308 if (chan->orig_mpwr)
1309 chan->max_power = min(chan->orig_mpwr,
1310 (int) MBM_TO_DBM(power_rule->max_eirp));
1311 else
1312 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1315 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
1317 unsigned int i;
1318 struct ieee80211_supported_band *sband;
1320 BUG_ON(!wiphy->bands[band]);
1321 sband = wiphy->bands[band];
1323 for (i = 0; i < sband->n_channels; i++)
1324 handle_channel(wiphy, band, i);
1327 static bool ignore_reg_update(struct wiphy *wiphy,
1328 enum nl80211_reg_initiator initiator)
1330 if (!last_request)
1331 return true;
1332 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1333 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1334 return true;
1336 * wiphy->regd will be set once the device has its own
1337 * desired regulatory domain set
1339 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
1340 !is_world_regdom(last_request->alpha2))
1341 return true;
1342 return false;
1345 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1347 struct cfg80211_registered_device *rdev;
1349 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1350 wiphy_update_regulatory(&rdev->wiphy, initiator);
1353 static void handle_reg_beacon(struct wiphy *wiphy,
1354 unsigned int chan_idx,
1355 struct reg_beacon *reg_beacon)
1357 struct ieee80211_supported_band *sband;
1358 struct ieee80211_channel *chan;
1359 bool channel_changed = false;
1360 struct ieee80211_channel chan_before;
1362 assert_cfg80211_lock();
1364 sband = wiphy->bands[reg_beacon->chan.band];
1365 chan = &sband->channels[chan_idx];
1367 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1368 return;
1370 if (chan->beacon_found)
1371 return;
1373 chan->beacon_found = true;
1375 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
1376 return;
1378 chan_before.center_freq = chan->center_freq;
1379 chan_before.flags = chan->flags;
1381 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1382 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1383 channel_changed = true;
1386 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1387 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1388 channel_changed = true;
1391 if (channel_changed)
1392 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1396 * Called when a scan on a wiphy finds a beacon on
1397 * new channel
1399 static void wiphy_update_new_beacon(struct wiphy *wiphy,
1400 struct reg_beacon *reg_beacon)
1402 unsigned int i;
1403 struct ieee80211_supported_band *sband;
1405 assert_cfg80211_lock();
1407 if (!wiphy->bands[reg_beacon->chan.band])
1408 return;
1410 sband = wiphy->bands[reg_beacon->chan.band];
1412 for (i = 0; i < sband->n_channels; i++)
1413 handle_reg_beacon(wiphy, i, reg_beacon);
1417 * Called upon reg changes or a new wiphy is added
1419 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1421 unsigned int i;
1422 struct ieee80211_supported_band *sband;
1423 struct reg_beacon *reg_beacon;
1425 assert_cfg80211_lock();
1427 if (list_empty(&reg_beacon_list))
1428 return;
1430 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1431 if (!wiphy->bands[reg_beacon->chan.band])
1432 continue;
1433 sband = wiphy->bands[reg_beacon->chan.band];
1434 for (i = 0; i < sband->n_channels; i++)
1435 handle_reg_beacon(wiphy, i, reg_beacon);
1439 static bool reg_is_world_roaming(struct wiphy *wiphy)
1441 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1442 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1443 return true;
1444 if (last_request &&
1445 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1446 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1447 return true;
1448 return false;
1451 /* Reap the advantages of previously found beacons */
1452 static void reg_process_beacons(struct wiphy *wiphy)
1455 * Means we are just firing up cfg80211, so no beacons would
1456 * have been processed yet.
1458 if (!last_request)
1459 return;
1460 if (!reg_is_world_roaming(wiphy))
1461 return;
1462 wiphy_update_beacon_reg(wiphy);
1465 static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1467 if (!chan)
1468 return true;
1469 if (chan->flags & IEEE80211_CHAN_DISABLED)
1470 return true;
1471 /* This would happen when regulatory rules disallow HT40 completely */
1472 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1473 return true;
1474 return false;
1477 static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1478 enum ieee80211_band band,
1479 unsigned int chan_idx)
1481 struct ieee80211_supported_band *sband;
1482 struct ieee80211_channel *channel;
1483 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1484 unsigned int i;
1486 assert_cfg80211_lock();
1488 sband = wiphy->bands[band];
1489 BUG_ON(chan_idx >= sband->n_channels);
1490 channel = &sband->channels[chan_idx];
1492 if (is_ht40_not_allowed(channel)) {
1493 channel->flags |= IEEE80211_CHAN_NO_HT40;
1494 return;
1498 * We need to ensure the extension channels exist to
1499 * be able to use HT40- or HT40+, this finds them (or not)
1501 for (i = 0; i < sband->n_channels; i++) {
1502 struct ieee80211_channel *c = &sband->channels[i];
1503 if (c->center_freq == (channel->center_freq - 20))
1504 channel_before = c;
1505 if (c->center_freq == (channel->center_freq + 20))
1506 channel_after = c;
1510 * Please note that this assumes target bandwidth is 20 MHz,
1511 * if that ever changes we also need to change the below logic
1512 * to include that as well.
1514 if (is_ht40_not_allowed(channel_before))
1515 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1516 else
1517 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1519 if (is_ht40_not_allowed(channel_after))
1520 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1521 else
1522 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1525 static void reg_process_ht_flags_band(struct wiphy *wiphy,
1526 enum ieee80211_band band)
1528 unsigned int i;
1529 struct ieee80211_supported_band *sband;
1531 BUG_ON(!wiphy->bands[band]);
1532 sband = wiphy->bands[band];
1534 for (i = 0; i < sband->n_channels; i++)
1535 reg_process_ht_flags_channel(wiphy, band, i);
1538 static void reg_process_ht_flags(struct wiphy *wiphy)
1540 enum ieee80211_band band;
1542 if (!wiphy)
1543 return;
1545 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1546 if (wiphy->bands[band])
1547 reg_process_ht_flags_band(wiphy, band);
1552 void wiphy_update_regulatory(struct wiphy *wiphy,
1553 enum nl80211_reg_initiator initiator)
1555 enum ieee80211_band band;
1557 if (ignore_reg_update(wiphy, initiator))
1558 goto out;
1559 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1560 if (wiphy->bands[band])
1561 handle_band(wiphy, band);
1563 out:
1564 reg_process_beacons(wiphy);
1565 reg_process_ht_flags(wiphy);
1566 if (wiphy->reg_notifier)
1567 wiphy->reg_notifier(wiphy, last_request);
1570 static void handle_channel_custom(struct wiphy *wiphy,
1571 enum ieee80211_band band,
1572 unsigned int chan_idx,
1573 const struct ieee80211_regdomain *regd)
1575 int r;
1576 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1577 u32 bw_flags = 0;
1578 const struct ieee80211_reg_rule *reg_rule = NULL;
1579 const struct ieee80211_power_rule *power_rule = NULL;
1580 const struct ieee80211_freq_range *freq_range = NULL;
1581 struct ieee80211_supported_band *sband;
1582 struct ieee80211_channel *chan;
1584 assert_reg_lock();
1586 sband = wiphy->bands[band];
1587 BUG_ON(chan_idx >= sband->n_channels);
1588 chan = &sband->channels[chan_idx];
1590 r = freq_reg_info_regd(wiphy,
1591 MHZ_TO_KHZ(chan->center_freq),
1592 desired_bw_khz,
1593 &reg_rule,
1594 regd);
1596 if (r) {
1597 chan->flags = IEEE80211_CHAN_DISABLED;
1598 return;
1601 power_rule = &reg_rule->power_rule;
1602 freq_range = &reg_rule->freq_range;
1604 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1605 bw_flags = IEEE80211_CHAN_NO_HT40;
1607 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1608 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1609 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1612 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1613 const struct ieee80211_regdomain *regd)
1615 unsigned int i;
1616 struct ieee80211_supported_band *sband;
1618 BUG_ON(!wiphy->bands[band]);
1619 sband = wiphy->bands[band];
1621 for (i = 0; i < sband->n_channels; i++)
1622 handle_channel_custom(wiphy, band, i, regd);
1625 /* Used by drivers prior to wiphy registration */
1626 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1627 const struct ieee80211_regdomain *regd)
1629 enum ieee80211_band band;
1630 unsigned int bands_set = 0;
1632 mutex_lock(&reg_mutex);
1633 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1634 if (!wiphy->bands[band])
1635 continue;
1636 handle_band_custom(wiphy, band, regd);
1637 bands_set++;
1639 mutex_unlock(&reg_mutex);
1642 * no point in calling this if it won't have any effect
1643 * on your device's supportd bands.
1645 WARN_ON(!bands_set);
1647 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1650 * Return value which can be used by ignore_request() to indicate
1651 * it has been determined we should intersect two regulatory domains
1653 #define REG_INTERSECT 1
1655 /* This has the logic which determines when a new request
1656 * should be ignored. */
1657 static int ignore_request(struct wiphy *wiphy,
1658 struct regulatory_request *pending_request)
1660 struct wiphy *last_wiphy = NULL;
1662 assert_cfg80211_lock();
1664 /* All initial requests are respected */
1665 if (!last_request)
1666 return 0;
1668 switch (pending_request->initiator) {
1669 case NL80211_REGDOM_SET_BY_CORE:
1670 return 0;
1671 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1673 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1675 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
1676 return -EINVAL;
1677 if (last_request->initiator ==
1678 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1679 if (last_wiphy != wiphy) {
1681 * Two cards with two APs claiming different
1682 * Country IE alpha2s. We could
1683 * intersect them, but that seems unlikely
1684 * to be correct. Reject second one for now.
1686 if (regdom_changes(pending_request->alpha2))
1687 return -EOPNOTSUPP;
1688 return -EALREADY;
1691 * Two consecutive Country IE hints on the same wiphy.
1692 * This should be picked up early by the driver/stack
1694 if (WARN_ON(regdom_changes(pending_request->alpha2)))
1695 return 0;
1696 return -EALREADY;
1698 return REG_INTERSECT;
1699 case NL80211_REGDOM_SET_BY_DRIVER:
1700 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1701 if (regdom_changes(pending_request->alpha2))
1702 return 0;
1703 return -EALREADY;
1707 * This would happen if you unplug and plug your card
1708 * back in or if you add a new device for which the previously
1709 * loaded card also agrees on the regulatory domain.
1711 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1712 !regdom_changes(pending_request->alpha2))
1713 return -EALREADY;
1715 return REG_INTERSECT;
1716 case NL80211_REGDOM_SET_BY_USER:
1717 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1718 return REG_INTERSECT;
1720 * If the user knows better the user should set the regdom
1721 * to their country before the IE is picked up
1723 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1724 last_request->intersect)
1725 return -EOPNOTSUPP;
1727 * Process user requests only after previous user/driver/core
1728 * requests have been processed
1730 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1731 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1732 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1733 if (regdom_changes(last_request->alpha2))
1734 return -EAGAIN;
1737 if (!regdom_changes(pending_request->alpha2))
1738 return -EALREADY;
1740 return 0;
1743 return -EINVAL;
1747 * __regulatory_hint - hint to the wireless core a regulatory domain
1748 * @wiphy: if the hint comes from country information from an AP, this
1749 * is required to be set to the wiphy that received the information
1750 * @pending_request: the regulatory request currently being processed
1752 * The Wireless subsystem can use this function to hint to the wireless core
1753 * what it believes should be the current regulatory domain.
1755 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1756 * already been set or other standard error codes.
1758 * Caller must hold &cfg80211_mutex and &reg_mutex
1760 static int __regulatory_hint(struct wiphy *wiphy,
1761 struct regulatory_request *pending_request)
1763 bool intersect = false;
1764 int r = 0;
1766 assert_cfg80211_lock();
1768 r = ignore_request(wiphy, pending_request);
1770 if (r == REG_INTERSECT) {
1771 if (pending_request->initiator ==
1772 NL80211_REGDOM_SET_BY_DRIVER) {
1773 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1774 if (r) {
1775 kfree(pending_request);
1776 return r;
1779 intersect = true;
1780 } else if (r) {
1782 * If the regulatory domain being requested by the
1783 * driver has already been set just copy it to the
1784 * wiphy
1786 if (r == -EALREADY &&
1787 pending_request->initiator ==
1788 NL80211_REGDOM_SET_BY_DRIVER) {
1789 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1790 if (r) {
1791 kfree(pending_request);
1792 return r;
1794 r = -EALREADY;
1795 goto new_request;
1797 kfree(pending_request);
1798 return r;
1801 new_request:
1802 kfree(last_request);
1804 last_request = pending_request;
1805 last_request->intersect = intersect;
1807 pending_request = NULL;
1809 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1810 user_alpha2[0] = last_request->alpha2[0];
1811 user_alpha2[1] = last_request->alpha2[1];
1814 /* When r == REG_INTERSECT we do need to call CRDA */
1815 if (r < 0) {
1817 * Since CRDA will not be called in this case as we already
1818 * have applied the requested regulatory domain before we just
1819 * inform userspace we have processed the request
1821 if (r == -EALREADY)
1822 nl80211_send_reg_change_event(last_request);
1823 return r;
1826 return call_crda(last_request->alpha2);
1829 /* This processes *all* regulatory hints */
1830 static void reg_process_hint(struct regulatory_request *reg_request)
1832 int r = 0;
1833 struct wiphy *wiphy = NULL;
1835 BUG_ON(!reg_request->alpha2);
1837 mutex_lock(&cfg80211_mutex);
1838 mutex_lock(&reg_mutex);
1840 if (wiphy_idx_valid(reg_request->wiphy_idx))
1841 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1843 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1844 !wiphy) {
1845 kfree(reg_request);
1846 goto out;
1849 r = __regulatory_hint(wiphy, reg_request);
1850 /* This is required so that the orig_* parameters are saved */
1851 if (r == -EALREADY && wiphy &&
1852 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
1853 wiphy_update_regulatory(wiphy, reg_request->initiator);
1854 out:
1855 mutex_unlock(&reg_mutex);
1856 mutex_unlock(&cfg80211_mutex);
1859 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1860 static void reg_process_pending_hints(void)
1862 struct regulatory_request *reg_request;
1864 spin_lock(&reg_requests_lock);
1865 while (!list_empty(&reg_requests_list)) {
1866 reg_request = list_first_entry(&reg_requests_list,
1867 struct regulatory_request,
1868 list);
1869 list_del_init(&reg_request->list);
1871 spin_unlock(&reg_requests_lock);
1872 reg_process_hint(reg_request);
1873 spin_lock(&reg_requests_lock);
1875 spin_unlock(&reg_requests_lock);
1878 /* Processes beacon hints -- this has nothing to do with country IEs */
1879 static void reg_process_pending_beacon_hints(void)
1881 struct cfg80211_registered_device *rdev;
1882 struct reg_beacon *pending_beacon, *tmp;
1885 * No need to hold the reg_mutex here as we just touch wiphys
1886 * and do not read or access regulatory variables.
1888 mutex_lock(&cfg80211_mutex);
1890 /* This goes through the _pending_ beacon list */
1891 spin_lock_bh(&reg_pending_beacons_lock);
1893 if (list_empty(&reg_pending_beacons)) {
1894 spin_unlock_bh(&reg_pending_beacons_lock);
1895 goto out;
1898 list_for_each_entry_safe(pending_beacon, tmp,
1899 &reg_pending_beacons, list) {
1901 list_del_init(&pending_beacon->list);
1903 /* Applies the beacon hint to current wiphys */
1904 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1905 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
1907 /* Remembers the beacon hint for new wiphys or reg changes */
1908 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1911 spin_unlock_bh(&reg_pending_beacons_lock);
1912 out:
1913 mutex_unlock(&cfg80211_mutex);
1916 static void reg_todo(struct work_struct *work)
1918 reg_process_pending_hints();
1919 reg_process_pending_beacon_hints();
1922 static DECLARE_WORK(reg_work, reg_todo);
1924 static void queue_regulatory_request(struct regulatory_request *request)
1926 spin_lock(&reg_requests_lock);
1927 list_add_tail(&request->list, &reg_requests_list);
1928 spin_unlock(&reg_requests_lock);
1930 schedule_work(&reg_work);
1934 * Core regulatory hint -- happens during cfg80211_init()
1935 * and when we restore regulatory settings.
1937 static int regulatory_hint_core(const char *alpha2)
1939 struct regulatory_request *request;
1941 kfree(last_request);
1942 last_request = NULL;
1944 request = kzalloc(sizeof(struct regulatory_request),
1945 GFP_KERNEL);
1946 if (!request)
1947 return -ENOMEM;
1949 request->alpha2[0] = alpha2[0];
1950 request->alpha2[1] = alpha2[1];
1951 request->initiator = NL80211_REGDOM_SET_BY_CORE;
1954 * This ensures last_request is populated once modules
1955 * come swinging in and calling regulatory hints and
1956 * wiphy_apply_custom_regulatory().
1958 reg_process_hint(request);
1960 return 0;
1963 /* User hints */
1964 int regulatory_hint_user(const char *alpha2)
1966 struct regulatory_request *request;
1968 BUG_ON(!alpha2);
1970 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1971 if (!request)
1972 return -ENOMEM;
1974 request->wiphy_idx = WIPHY_IDX_STALE;
1975 request->alpha2[0] = alpha2[0];
1976 request->alpha2[1] = alpha2[1];
1977 request->initiator = NL80211_REGDOM_SET_BY_USER;
1979 queue_regulatory_request(request);
1981 return 0;
1984 /* Driver hints */
1985 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1987 struct regulatory_request *request;
1989 BUG_ON(!alpha2);
1990 BUG_ON(!wiphy);
1992 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1993 if (!request)
1994 return -ENOMEM;
1996 request->wiphy_idx = get_wiphy_idx(wiphy);
1998 /* Must have registered wiphy first */
1999 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
2001 request->alpha2[0] = alpha2[0];
2002 request->alpha2[1] = alpha2[1];
2003 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
2005 queue_regulatory_request(request);
2007 return 0;
2009 EXPORT_SYMBOL(regulatory_hint);
2011 /* Caller must hold reg_mutex */
2012 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
2013 u32 country_ie_checksum)
2015 struct wiphy *request_wiphy;
2017 assert_reg_lock();
2019 if (unlikely(last_request->initiator !=
2020 NL80211_REGDOM_SET_BY_COUNTRY_IE))
2021 return false;
2023 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2025 if (!request_wiphy)
2026 return false;
2028 if (likely(request_wiphy != wiphy))
2029 return !country_ie_integrity_changes(country_ie_checksum);
2031 * We should not have let these through at this point, they
2032 * should have been picked up earlier by the first alpha2 check
2033 * on the device
2035 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
2036 return true;
2037 return false;
2041 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
2042 * therefore cannot iterate over the rdev list here.
2044 void regulatory_hint_11d(struct wiphy *wiphy,
2045 enum ieee80211_band band,
2046 u8 *country_ie,
2047 u8 country_ie_len)
2049 struct ieee80211_regdomain *rd = NULL;
2050 char alpha2[2];
2051 u32 checksum = 0;
2052 enum environment_cap env = ENVIRON_ANY;
2053 struct regulatory_request *request;
2055 mutex_lock(&reg_mutex);
2057 if (unlikely(!last_request))
2058 goto out;
2060 /* IE len must be evenly divisible by 2 */
2061 if (country_ie_len & 0x01)
2062 goto out;
2064 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2065 goto out;
2068 * Pending country IE processing, this can happen after we
2069 * call CRDA and wait for a response if a beacon was received before
2070 * we were able to process the last regulatory_hint_11d() call
2072 if (country_ie_regdomain)
2073 goto out;
2075 alpha2[0] = country_ie[0];
2076 alpha2[1] = country_ie[1];
2078 if (country_ie[2] == 'I')
2079 env = ENVIRON_INDOOR;
2080 else if (country_ie[2] == 'O')
2081 env = ENVIRON_OUTDOOR;
2084 * We will run this only upon a successful connection on cfg80211.
2085 * We leave conflict resolution to the workqueue, where can hold
2086 * cfg80211_mutex.
2088 if (likely(last_request->initiator ==
2089 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2090 wiphy_idx_valid(last_request->wiphy_idx)))
2091 goto out;
2093 rd = country_ie_2_rd(band, country_ie, country_ie_len, &checksum);
2094 if (!rd) {
2095 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
2096 goto out;
2100 * This will not happen right now but we leave it here for the
2101 * the future when we want to add suspend/resume support and having
2102 * the user move to another country after doing so, or having the user
2103 * move to another AP. Right now we just trust the first AP.
2105 * If we hit this before we add this support we want to be informed of
2106 * it as it would indicate a mistake in the current design
2108 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
2109 goto free_rd_out;
2111 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2112 if (!request)
2113 goto free_rd_out;
2116 * We keep this around for when CRDA comes back with a response so
2117 * we can intersect with that
2119 country_ie_regdomain = rd;
2121 request->wiphy_idx = get_wiphy_idx(wiphy);
2122 request->alpha2[0] = rd->alpha2[0];
2123 request->alpha2[1] = rd->alpha2[1];
2124 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
2125 request->country_ie_checksum = checksum;
2126 request->country_ie_env = env;
2128 mutex_unlock(&reg_mutex);
2130 queue_regulatory_request(request);
2132 return;
2134 free_rd_out:
2135 kfree(rd);
2136 out:
2137 mutex_unlock(&reg_mutex);
2140 static void restore_alpha2(char *alpha2, bool reset_user)
2142 /* indicates there is no alpha2 to consider for restoration */
2143 alpha2[0] = '9';
2144 alpha2[1] = '7';
2146 /* The user setting has precedence over the module parameter */
2147 if (is_user_regdom_saved()) {
2148 /* Unless we're asked to ignore it and reset it */
2149 if (reset_user) {
2150 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2151 "including user preference\n");
2152 user_alpha2[0] = '9';
2153 user_alpha2[1] = '7';
2156 * If we're ignoring user settings, we still need to
2157 * check the module parameter to ensure we put things
2158 * back as they were for a full restore.
2160 if (!is_world_regdom(ieee80211_regdom)) {
2161 REG_DBG_PRINT("cfg80211: Keeping preference on "
2162 "module parameter ieee80211_regdom: %c%c\n",
2163 ieee80211_regdom[0],
2164 ieee80211_regdom[1]);
2165 alpha2[0] = ieee80211_regdom[0];
2166 alpha2[1] = ieee80211_regdom[1];
2168 } else {
2169 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2170 "while preserving user preference for: %c%c\n",
2171 user_alpha2[0],
2172 user_alpha2[1]);
2173 alpha2[0] = user_alpha2[0];
2174 alpha2[1] = user_alpha2[1];
2176 } else if (!is_world_regdom(ieee80211_regdom)) {
2177 REG_DBG_PRINT("cfg80211: Keeping preference on "
2178 "module parameter ieee80211_regdom: %c%c\n",
2179 ieee80211_regdom[0],
2180 ieee80211_regdom[1]);
2181 alpha2[0] = ieee80211_regdom[0];
2182 alpha2[1] = ieee80211_regdom[1];
2183 } else
2184 REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
2188 * Restoring regulatory settings involves ingoring any
2189 * possibly stale country IE information and user regulatory
2190 * settings if so desired, this includes any beacon hints
2191 * learned as we could have traveled outside to another country
2192 * after disconnection. To restore regulatory settings we do
2193 * exactly what we did at bootup:
2195 * - send a core regulatory hint
2196 * - send a user regulatory hint if applicable
2198 * Device drivers that send a regulatory hint for a specific country
2199 * keep their own regulatory domain on wiphy->regd so that does does
2200 * not need to be remembered.
2202 static void restore_regulatory_settings(bool reset_user)
2204 char alpha2[2];
2205 struct reg_beacon *reg_beacon, *btmp;
2207 mutex_lock(&cfg80211_mutex);
2208 mutex_lock(&reg_mutex);
2210 reset_regdomains();
2211 restore_alpha2(alpha2, reset_user);
2213 /* Clear beacon hints */
2214 spin_lock_bh(&reg_pending_beacons_lock);
2215 if (!list_empty(&reg_pending_beacons)) {
2216 list_for_each_entry_safe(reg_beacon, btmp,
2217 &reg_pending_beacons, list) {
2218 list_del(&reg_beacon->list);
2219 kfree(reg_beacon);
2222 spin_unlock_bh(&reg_pending_beacons_lock);
2224 if (!list_empty(&reg_beacon_list)) {
2225 list_for_each_entry_safe(reg_beacon, btmp,
2226 &reg_beacon_list, list) {
2227 list_del(&reg_beacon->list);
2228 kfree(reg_beacon);
2232 /* First restore to the basic regulatory settings */
2233 cfg80211_regdomain = cfg80211_world_regdom;
2235 mutex_unlock(&reg_mutex);
2236 mutex_unlock(&cfg80211_mutex);
2238 regulatory_hint_core(cfg80211_regdomain->alpha2);
2241 * This restores the ieee80211_regdom module parameter
2242 * preference or the last user requested regulatory
2243 * settings, user regulatory settings takes precedence.
2245 if (is_an_alpha2(alpha2))
2246 regulatory_hint_user(user_alpha2);
2250 void regulatory_hint_disconnect(void)
2252 REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
2253 "restore regulatory settings\n");
2254 restore_regulatory_settings(false);
2257 static bool freq_is_chan_12_13_14(u16 freq)
2259 if (freq == ieee80211_channel_to_frequency(12) ||
2260 freq == ieee80211_channel_to_frequency(13) ||
2261 freq == ieee80211_channel_to_frequency(14))
2262 return true;
2263 return false;
2266 int regulatory_hint_found_beacon(struct wiphy *wiphy,
2267 struct ieee80211_channel *beacon_chan,
2268 gfp_t gfp)
2270 struct reg_beacon *reg_beacon;
2272 if (likely((beacon_chan->beacon_found ||
2273 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
2274 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
2275 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
2276 return 0;
2278 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2279 if (!reg_beacon)
2280 return -ENOMEM;
2282 REG_DBG_PRINT("cfg80211: Found new beacon on "
2283 "frequency: %d MHz (Ch %d) on %s\n",
2284 beacon_chan->center_freq,
2285 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2286 wiphy_name(wiphy));
2288 memcpy(&reg_beacon->chan, beacon_chan,
2289 sizeof(struct ieee80211_channel));
2293 * Since we can be called from BH or and non-BH context
2294 * we must use spin_lock_bh()
2296 spin_lock_bh(&reg_pending_beacons_lock);
2297 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2298 spin_unlock_bh(&reg_pending_beacons_lock);
2300 schedule_work(&reg_work);
2302 return 0;
2305 static void print_rd_rules(const struct ieee80211_regdomain *rd)
2307 unsigned int i;
2308 const struct ieee80211_reg_rule *reg_rule = NULL;
2309 const struct ieee80211_freq_range *freq_range = NULL;
2310 const struct ieee80211_power_rule *power_rule = NULL;
2312 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
2313 "(max_antenna_gain, max_eirp)\n");
2315 for (i = 0; i < rd->n_reg_rules; i++) {
2316 reg_rule = &rd->reg_rules[i];
2317 freq_range = &reg_rule->freq_range;
2318 power_rule = &reg_rule->power_rule;
2321 * There may not be documentation for max antenna gain
2322 * in certain regions
2324 if (power_rule->max_antenna_gain)
2325 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
2326 "(%d mBi, %d mBm)\n",
2327 freq_range->start_freq_khz,
2328 freq_range->end_freq_khz,
2329 freq_range->max_bandwidth_khz,
2330 power_rule->max_antenna_gain,
2331 power_rule->max_eirp);
2332 else
2333 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
2334 "(N/A, %d mBm)\n",
2335 freq_range->start_freq_khz,
2336 freq_range->end_freq_khz,
2337 freq_range->max_bandwidth_khz,
2338 power_rule->max_eirp);
2342 static void print_regdomain(const struct ieee80211_regdomain *rd)
2345 if (is_intersected_alpha2(rd->alpha2)) {
2347 if (last_request->initiator ==
2348 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2349 struct cfg80211_registered_device *rdev;
2350 rdev = cfg80211_rdev_by_wiphy_idx(
2351 last_request->wiphy_idx);
2352 if (rdev) {
2353 printk(KERN_INFO "cfg80211: Current regulatory "
2354 "domain updated by AP to: %c%c\n",
2355 rdev->country_ie_alpha2[0],
2356 rdev->country_ie_alpha2[1]);
2357 } else
2358 printk(KERN_INFO "cfg80211: Current regulatory "
2359 "domain intersected:\n");
2360 } else
2361 printk(KERN_INFO "cfg80211: Current regulatory "
2362 "domain intersected:\n");
2363 } else if (is_world_regdom(rd->alpha2))
2364 printk(KERN_INFO "cfg80211: World regulatory "
2365 "domain updated:\n");
2366 else {
2367 if (is_unknown_alpha2(rd->alpha2))
2368 printk(KERN_INFO "cfg80211: Regulatory domain "
2369 "changed to driver built-in settings "
2370 "(unknown country)\n");
2371 else
2372 printk(KERN_INFO "cfg80211: Regulatory domain "
2373 "changed to country: %c%c\n",
2374 rd->alpha2[0], rd->alpha2[1]);
2376 print_rd_rules(rd);
2379 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
2381 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2382 rd->alpha2[0], rd->alpha2[1]);
2383 print_rd_rules(rd);
2386 #ifdef CONFIG_CFG80211_REG_DEBUG
2387 static void reg_country_ie_process_debug(
2388 const struct ieee80211_regdomain *rd,
2389 const struct ieee80211_regdomain *country_ie_regdomain,
2390 const struct ieee80211_regdomain *intersected_rd)
2392 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2393 print_regdomain_info(country_ie_regdomain);
2394 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2395 print_regdomain_info(rd);
2396 if (intersected_rd) {
2397 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2398 "and get:\n");
2399 print_regdomain_info(intersected_rd);
2400 return;
2402 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2404 #else
2405 static inline void reg_country_ie_process_debug(
2406 const struct ieee80211_regdomain *rd,
2407 const struct ieee80211_regdomain *country_ie_regdomain,
2408 const struct ieee80211_regdomain *intersected_rd)
2411 #endif
2413 /* Takes ownership of rd only if it doesn't fail */
2414 static int __set_regdom(const struct ieee80211_regdomain *rd)
2416 const struct ieee80211_regdomain *intersected_rd = NULL;
2417 struct cfg80211_registered_device *rdev = NULL;
2418 struct wiphy *request_wiphy;
2419 /* Some basic sanity checks first */
2421 if (is_world_regdom(rd->alpha2)) {
2422 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2423 return -EINVAL;
2424 update_world_regdomain(rd);
2425 return 0;
2428 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2429 !is_unknown_alpha2(rd->alpha2))
2430 return -EINVAL;
2432 if (!last_request)
2433 return -EINVAL;
2436 * Lets only bother proceeding on the same alpha2 if the current
2437 * rd is non static (it means CRDA was present and was used last)
2438 * and the pending request came in from a country IE
2440 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2442 * If someone else asked us to change the rd lets only bother
2443 * checking if the alpha2 changes if CRDA was already called
2445 if (!regdom_changes(rd->alpha2))
2446 return -EINVAL;
2450 * Now lets set the regulatory domain, update all driver channels
2451 * and finally inform them of what we have done, in case they want
2452 * to review or adjust their own settings based on their own
2453 * internal EEPROM data
2456 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2457 return -EINVAL;
2459 if (!is_valid_rd(rd)) {
2460 printk(KERN_ERR "cfg80211: Invalid "
2461 "regulatory domain detected:\n");
2462 print_regdomain_info(rd);
2463 return -EINVAL;
2466 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2468 if (!last_request->intersect) {
2469 int r;
2471 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
2472 reset_regdomains();
2473 cfg80211_regdomain = rd;
2474 return 0;
2478 * For a driver hint, lets copy the regulatory domain the
2479 * driver wanted to the wiphy to deal with conflicts
2483 * Userspace could have sent two replies with only
2484 * one kernel request.
2486 if (request_wiphy->regd)
2487 return -EALREADY;
2489 r = reg_copy_regd(&request_wiphy->regd, rd);
2490 if (r)
2491 return r;
2493 reset_regdomains();
2494 cfg80211_regdomain = rd;
2495 return 0;
2498 /* Intersection requires a bit more work */
2500 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2502 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2503 if (!intersected_rd)
2504 return -EINVAL;
2507 * We can trash what CRDA provided now.
2508 * However if a driver requested this specific regulatory
2509 * domain we keep it for its private use
2511 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2512 request_wiphy->regd = rd;
2513 else
2514 kfree(rd);
2516 rd = NULL;
2518 reset_regdomains();
2519 cfg80211_regdomain = intersected_rd;
2521 return 0;
2525 * Country IE requests are handled a bit differently, we intersect
2526 * the country IE rd with what CRDA believes that country should have
2530 * Userspace could have sent two replies with only
2531 * one kernel request. By the second reply we would have
2532 * already processed and consumed the country_ie_regdomain.
2534 if (!country_ie_regdomain)
2535 return -EALREADY;
2536 BUG_ON(rd == country_ie_regdomain);
2539 * Intersect what CRDA returned and our what we
2540 * had built from the Country IE received
2543 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2545 reg_country_ie_process_debug(rd,
2546 country_ie_regdomain,
2547 intersected_rd);
2549 kfree(country_ie_regdomain);
2550 country_ie_regdomain = NULL;
2552 if (!intersected_rd)
2553 return -EINVAL;
2555 rdev = wiphy_to_dev(request_wiphy);
2557 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2558 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2559 rdev->env = last_request->country_ie_env;
2561 BUG_ON(intersected_rd == rd);
2563 kfree(rd);
2564 rd = NULL;
2566 reset_regdomains();
2567 cfg80211_regdomain = intersected_rd;
2569 return 0;
2574 * Use this call to set the current regulatory domain. Conflicts with
2575 * multiple drivers can be ironed out later. Caller must've already
2576 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2578 int set_regdom(const struct ieee80211_regdomain *rd)
2580 int r;
2582 assert_cfg80211_lock();
2584 mutex_lock(&reg_mutex);
2586 /* Note that this doesn't update the wiphys, this is done below */
2587 r = __set_regdom(rd);
2588 if (r) {
2589 kfree(rd);
2590 mutex_unlock(&reg_mutex);
2591 return r;
2594 /* This would make this whole thing pointless */
2595 if (!last_request->intersect)
2596 BUG_ON(rd != cfg80211_regdomain);
2598 /* update all wiphys now with the new established regulatory domain */
2599 update_all_wiphy_regulatory(last_request->initiator);
2601 print_regdomain(cfg80211_regdomain);
2603 nl80211_send_reg_change_event(last_request);
2605 mutex_unlock(&reg_mutex);
2607 return r;
2610 /* Caller must hold cfg80211_mutex */
2611 void reg_device_remove(struct wiphy *wiphy)
2613 struct wiphy *request_wiphy = NULL;
2615 assert_cfg80211_lock();
2617 mutex_lock(&reg_mutex);
2619 kfree(wiphy->regd);
2621 if (last_request)
2622 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2624 if (!request_wiphy || request_wiphy != wiphy)
2625 goto out;
2627 last_request->wiphy_idx = WIPHY_IDX_STALE;
2628 last_request->country_ie_env = ENVIRON_ANY;
2629 out:
2630 mutex_unlock(&reg_mutex);
2633 int regulatory_init(void)
2635 int err = 0;
2637 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2638 if (IS_ERR(reg_pdev))
2639 return PTR_ERR(reg_pdev);
2641 spin_lock_init(&reg_requests_lock);
2642 spin_lock_init(&reg_pending_beacons_lock);
2644 cfg80211_regdomain = cfg80211_world_regdom;
2646 user_alpha2[0] = '9';
2647 user_alpha2[1] = '7';
2649 /* We always try to get an update for the static regdomain */
2650 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
2651 if (err) {
2652 if (err == -ENOMEM)
2653 return err;
2655 * N.B. kobject_uevent_env() can fail mainly for when we're out
2656 * memory which is handled and propagated appropriately above
2657 * but it can also fail during a netlink_broadcast() or during
2658 * early boot for call_usermodehelper(). For now treat these
2659 * errors as non-fatal.
2661 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2662 "to call CRDA during init");
2663 #ifdef CONFIG_CFG80211_REG_DEBUG
2664 /* We want to find out exactly why when debugging */
2665 WARN_ON(err);
2666 #endif
2670 * Finally, if the user set the module parameter treat it
2671 * as a user hint.
2673 if (!is_world_regdom(ieee80211_regdom))
2674 regulatory_hint_user(ieee80211_regdom);
2676 return 0;
2679 void regulatory_exit(void)
2681 struct regulatory_request *reg_request, *tmp;
2682 struct reg_beacon *reg_beacon, *btmp;
2684 cancel_work_sync(&reg_work);
2686 mutex_lock(&cfg80211_mutex);
2687 mutex_lock(&reg_mutex);
2689 reset_regdomains();
2691 kfree(country_ie_regdomain);
2692 country_ie_regdomain = NULL;
2694 kfree(last_request);
2696 platform_device_unregister(reg_pdev);
2698 spin_lock_bh(&reg_pending_beacons_lock);
2699 if (!list_empty(&reg_pending_beacons)) {
2700 list_for_each_entry_safe(reg_beacon, btmp,
2701 &reg_pending_beacons, list) {
2702 list_del(&reg_beacon->list);
2703 kfree(reg_beacon);
2706 spin_unlock_bh(&reg_pending_beacons_lock);
2708 if (!list_empty(&reg_beacon_list)) {
2709 list_for_each_entry_safe(reg_beacon, btmp,
2710 &reg_beacon_list, list) {
2711 list_del(&reg_beacon->list);
2712 kfree(reg_beacon);
2716 spin_lock(&reg_requests_lock);
2717 if (!list_empty(&reg_requests_list)) {
2718 list_for_each_entry_safe(reg_request, tmp,
2719 &reg_requests_list, list) {
2720 list_del(&reg_request->list);
2721 kfree(reg_request);
2724 spin_unlock(&reg_requests_lock);
2726 mutex_unlock(&reg_mutex);
2727 mutex_unlock(&cfg80211_mutex);