2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 * DOC: Wireless regulatory infrastructure
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
20 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/random.h>
38 #include <linux/nl80211.h>
39 #include <linux/platform_device.h>
40 #include <net/wireless.h>
41 #include <net/cfg80211.h>
45 /* To trigger userspace events */
46 static struct platform_device *reg_pdev;
48 /* Keep the ordering from large to small */
49 static u32 supported_bandwidths[] = {
54 bool is_world_regdom(char *alpha2)
58 if (alpha2[0] == '0' && alpha2[1] == '0')
63 static bool is_alpha2_set(char *alpha2)
67 if (alpha2[0] != 0 && alpha2[1] != 0)
72 static bool is_alpha_upper(char letter)
75 if (letter >= 65 && letter <= 90)
80 static bool is_unknown_alpha2(char *alpha2)
84 /* Special case where regulatory domain was built by driver
85 * but a specific alpha2 cannot be determined */
86 if (alpha2[0] == '9' && alpha2[1] == '9')
91 static bool is_an_alpha2(char *alpha2)
95 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
100 static bool alpha2_equal(char *alpha2_x, char *alpha2_y)
102 if (!alpha2_x || !alpha2_y)
104 if (alpha2_x[0] == alpha2_y[0] &&
105 alpha2_x[1] == alpha2_y[1])
110 static bool regdom_changed(char *alpha2)
112 if (!cfg80211_regdomain)
114 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
119 /* This lets us keep regulatory code which is updated on a regulatory
120 * basis in userspace. */
121 static int call_crda(const char *alpha2)
123 char country_env[9 + 2] = "COUNTRY=";
129 if (!is_world_regdom((char *) alpha2))
130 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
131 alpha2[0], alpha2[1]);
133 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
136 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
137 "regulatory domain\n");
140 country_env[8] = alpha2[0];
141 country_env[9] = alpha2[1];
143 return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp);
146 /* This has the logic which determines when a new request
147 * should be ignored. */
148 static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
149 char *alpha2, struct ieee80211_regdomain *rd)
151 struct regulatory_request *last_request = NULL;
153 /* All initial requests are respected */
154 if (list_empty(®ulatory_requests))
157 last_request = list_first_entry(®ulatory_requests,
158 struct regulatory_request, list);
161 case REGDOM_SET_BY_INIT:
163 case REGDOM_SET_BY_CORE:
164 /* Always respect new wireless core hints, should only
165 * come in for updating the world regulatory domain at init
168 case REGDOM_SET_BY_COUNTRY_IE:
169 if (last_request->initiator == set_by) {
170 if (last_request->wiphy != wiphy) {
171 /* Two cards with two APs claiming different
172 * different Country IE alpha2s!
173 * You're special!! */
174 if (!alpha2_equal(last_request->alpha2,
175 cfg80211_regdomain->alpha2)) {
176 /* XXX: Deal with conflict, consider
177 * building a new one out of the
184 /* Two consecutive Country IE hints on the same wiphy */
185 if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
189 if (WARN_ON(!is_alpha2_set(alpha2) || !is_an_alpha2(alpha2)),
190 "Invalid Country IE regulatory hint passed "
191 "to the wireless core\n")
193 /* We ignore Country IE hints for now, as we haven't yet
194 * added the dot11MultiDomainCapabilityEnabled flag
197 case REGDOM_SET_BY_DRIVER:
199 if (last_request->initiator == set_by) {
200 /* Two separate drivers hinting different things,
201 * this is possible if you have two devices present
202 * on a system with different EEPROM regulatory
203 * readings. XXX: Do intersection, we support only
204 * the first regulatory hint for now */
205 if (last_request->wiphy != wiphy)
209 /* Driver should not be trying to hint different
210 * regulatory domains! */
211 BUG_ON(!alpha2_equal(alpha2,
212 cfg80211_regdomain->alpha2));
215 if (last_request->initiator == REGDOM_SET_BY_CORE)
217 /* XXX: Handle intersection, and add the
218 * dot11MultiDomainCapabilityEnabled flag to wiphy. For now
219 * we assume the driver has this set to false, following the
220 * 802.11d dot11MultiDomainCapabilityEnabled documentation */
221 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
224 case REGDOM_SET_BY_USER:
225 if (last_request->initiator == set_by ||
226 last_request->initiator == REGDOM_SET_BY_CORE)
228 /* Drivers can use their wiphy's reg_notifier()
229 * to override any information */
230 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
232 /* XXX: Handle intersection */
233 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
241 static bool __reg_is_valid_request(char *alpha2,
242 struct regulatory_request **request)
244 struct regulatory_request *req;
245 if (list_empty(®ulatory_requests))
247 list_for_each_entry(req, ®ulatory_requests, list) {
248 if (alpha2_equal(req->alpha2, alpha2)) {
256 /* Used by nl80211 before kmalloc'ing our regulatory domain */
257 bool reg_is_valid_request(char *alpha2)
259 struct regulatory_request *request = NULL;
260 return __reg_is_valid_request(alpha2, &request);
263 /* Sanity check on a regulatory rule */
264 static bool is_valid_reg_rule(struct ieee80211_reg_rule *rule)
266 struct ieee80211_freq_range *freq_range = &rule->freq_range;
269 if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
272 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
275 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
277 if (freq_range->max_bandwidth_khz > freq_diff)
283 static bool is_valid_rd(struct ieee80211_regdomain *rd)
285 struct ieee80211_reg_rule *reg_rule = NULL;
288 if (!rd->n_reg_rules)
291 for (i = 0; i < rd->n_reg_rules; i++) {
292 reg_rule = &rd->reg_rules[i];
293 if (!is_valid_reg_rule(reg_rule))
300 /* Returns value in KHz */
301 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
305 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
306 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
307 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
308 if (start_freq_khz >= freq_range->start_freq_khz &&
309 end_freq_khz <= freq_range->end_freq_khz)
310 return supported_bandwidths[i];
315 /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
316 * want to just have the channel structure use these */
317 static u32 map_regdom_flags(u32 rd_flags)
319 u32 channel_flags = 0;
320 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
321 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
322 if (rd_flags & NL80211_RRF_NO_IBSS)
323 channel_flags |= IEEE80211_CHAN_NO_IBSS;
324 if (rd_flags & NL80211_RRF_DFS)
325 channel_flags |= IEEE80211_CHAN_RADAR;
326 return channel_flags;
330 * freq_reg_info - get regulatory information for the given frequency
331 * @center_freq: Frequency in KHz for which we want regulatory information for
332 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
333 * you can set this to 0. If this frequency is allowed we then set
334 * this value to the maximum allowed bandwidth.
335 * @reg_rule: the regulatory rule which we have for this frequency
337 * Use this function to get the regulatory rule for a specific frequency.
339 static int freq_reg_info(u32 center_freq, u32 *bandwidth,
340 const struct ieee80211_reg_rule **reg_rule)
343 u32 max_bandwidth = 0;
345 if (!cfg80211_regdomain)
348 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
349 const struct ieee80211_reg_rule *rr;
350 const struct ieee80211_freq_range *fr = NULL;
351 const struct ieee80211_power_rule *pr = NULL;
353 rr = &cfg80211_regdomain->reg_rules[i];
354 fr = &rr->freq_range;
355 pr = &rr->power_rule;
356 max_bandwidth = freq_max_bandwidth(fr, center_freq);
357 if (max_bandwidth && *bandwidth <= max_bandwidth) {
359 *bandwidth = max_bandwidth;
364 return !max_bandwidth;
367 static void handle_channel(struct ieee80211_channel *chan)
370 u32 flags = chan->orig_flags;
371 u32 max_bandwidth = 0;
372 const struct ieee80211_reg_rule *reg_rule = NULL;
373 const struct ieee80211_power_rule *power_rule = NULL;
375 r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
376 &max_bandwidth, ®_rule);
379 flags |= IEEE80211_CHAN_DISABLED;
384 power_rule = ®_rule->power_rule;
386 chan->flags = flags | map_regdom_flags(reg_rule->flags);
387 chan->max_antenna_gain = min(chan->orig_mag,
388 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
389 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
391 chan->max_power = min(chan->orig_mpwr,
392 (int) MBM_TO_DBM(power_rule->max_eirp));
394 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
397 static void handle_band(struct ieee80211_supported_band *sband)
401 for (i = 0; i < sband->n_channels; i++)
402 handle_channel(&sband->channels[i]);
405 static void update_all_wiphy_regulatory(enum reg_set_by setby)
407 struct cfg80211_registered_device *drv;
409 list_for_each_entry(drv, &cfg80211_drv_list, list)
410 wiphy_update_regulatory(&drv->wiphy, setby);
413 void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
415 enum ieee80211_band band;
416 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
417 if (wiphy->bands[band])
418 handle_band(wiphy->bands[band]);
419 if (wiphy->reg_notifier)
420 wiphy->reg_notifier(wiphy, setby);
424 /* Caller must hold &cfg80211_drv_mutex */
425 int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
426 const char *alpha2, struct ieee80211_regdomain *rd)
428 struct regulatory_request *request;
432 r = ignore_request(wiphy, set_by, (char *) alpha2, rd);
437 rd_alpha2 = rd->alpha2;
439 rd_alpha2 = (char *) alpha2;
442 case REGDOM_SET_BY_CORE:
443 case REGDOM_SET_BY_COUNTRY_IE:
444 case REGDOM_SET_BY_DRIVER:
445 case REGDOM_SET_BY_USER:
446 request = kzalloc(sizeof(struct regulatory_request),
451 request->alpha2[0] = rd_alpha2[0];
452 request->alpha2[1] = rd_alpha2[1];
453 request->initiator = set_by;
454 request->wiphy = wiphy;
456 list_add_tail(&request->list, ®ulatory_requests);
459 r = call_crda(alpha2);
460 #ifndef CONFIG_WIRELESS_OLD_REGULATORY
462 printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
473 /* If rd is not NULL and if this call fails the caller must free it */
474 int regulatory_hint(struct wiphy *wiphy, const char *alpha2,
475 struct ieee80211_regdomain *rd)
478 BUG_ON(!rd && !alpha2);
480 mutex_lock(&cfg80211_drv_mutex);
482 r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, rd);
484 goto unlock_and_exit;
486 /* If the driver passed a regulatory domain we skipped asking
487 * userspace for one so we can now go ahead and set it */
491 mutex_unlock(&cfg80211_drv_mutex);
494 EXPORT_SYMBOL(regulatory_hint);
497 static void print_rd_rules(struct ieee80211_regdomain *rd)
500 struct ieee80211_reg_rule *reg_rule = NULL;
501 struct ieee80211_freq_range *freq_range = NULL;
502 struct ieee80211_power_rule *power_rule = NULL;
504 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
505 "(max_antenna_gain, max_eirp)\n");
507 for (i = 0; i < rd->n_reg_rules; i++) {
508 reg_rule = &rd->reg_rules[i];
509 freq_range = ®_rule->freq_range;
510 power_rule = ®_rule->power_rule;
512 /* There may not be documentation for max antenna gain
513 * in certain regions */
514 if (power_rule->max_antenna_gain)
515 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
516 "(%d mBi, %d mBm)\n",
517 freq_range->start_freq_khz,
518 freq_range->end_freq_khz,
519 freq_range->max_bandwidth_khz,
520 power_rule->max_antenna_gain,
521 power_rule->max_eirp);
523 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
525 freq_range->start_freq_khz,
526 freq_range->end_freq_khz,
527 freq_range->max_bandwidth_khz,
528 power_rule->max_eirp);
532 static void print_regdomain(struct ieee80211_regdomain *rd)
535 if (is_world_regdom(rd->alpha2))
536 printk(KERN_INFO "cfg80211: World regulatory "
537 "domain updated:\n");
539 if (is_unknown_alpha2(rd->alpha2))
540 printk(KERN_INFO "cfg80211: Regulatory domain "
541 "changed to driver built-in settings "
542 "(unknown country)\n");
544 printk(KERN_INFO "cfg80211: Regulatory domain "
545 "changed to country: %c%c\n",
546 rd->alpha2[0], rd->alpha2[1]);
551 void print_regdomain_info(struct ieee80211_regdomain *rd)
553 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
554 rd->alpha2[0], rd->alpha2[1]);
558 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
560 static bool is_old_static_regdom(struct ieee80211_regdomain *rd)
562 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
567 /* The old crap never deals with a world regulatory domain, it only
568 * deals with the static regulatory domain passed and if possible
569 * an updated "US" or "JP" regulatory domain. We do however store the
570 * old static regulatory domain in cfg80211_world_regdom for convenience
572 static void reset_regdomains_static(void)
574 if (!is_old_static_regdom(cfg80211_regdomain))
575 kfree(cfg80211_regdomain);
576 /* This is setting the regdom to the old static regdom */
578 (struct ieee80211_regdomain *) cfg80211_world_regdom;
581 static void reset_regdomains(void)
583 if (cfg80211_world_regdom && cfg80211_world_regdom != &world_regdom) {
584 if (cfg80211_world_regdom == cfg80211_regdomain) {
585 kfree(cfg80211_regdomain);
587 kfree(cfg80211_world_regdom);
588 kfree(cfg80211_regdomain);
590 } else if (cfg80211_regdomain && cfg80211_regdomain != &world_regdom)
591 kfree(cfg80211_regdomain);
593 cfg80211_world_regdom = (struct ieee80211_regdomain *) &world_regdom;
594 cfg80211_regdomain = NULL;
597 /* Dynamic world regulatory domain requested by the wireless
598 * core upon initialization */
599 static void update_world_regdomain(struct ieee80211_regdomain *rd)
601 BUG_ON(list_empty(®ulatory_requests));
605 cfg80211_world_regdom = rd;
606 cfg80211_regdomain = rd;
610 static int __set_regdom(struct ieee80211_regdomain *rd)
612 struct regulatory_request *request = NULL;
614 /* Some basic sanity checks first */
616 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
617 /* We ignore the world regdom with the old static regdomains setup
618 * as there is no point to it with satic regulatory definitions :(
619 * Don't worry this shit will be removed soon... */
620 if (is_world_regdom(rd->alpha2))
623 if (is_world_regdom(rd->alpha2)) {
624 if (WARN_ON(!__reg_is_valid_request(rd->alpha2, &request)))
626 update_world_regdomain(rd);
631 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
632 !is_unknown_alpha2(rd->alpha2))
635 if (list_empty(®ulatory_requests))
638 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
639 /* Static "US" and "JP" will be overridden, but just once */
640 if (!is_old_static_regdom(cfg80211_regdomain) &&
641 !regdom_changed(rd->alpha2))
644 if (!regdom_changed(rd->alpha2))
648 /* Now lets set the regulatory domain, update all driver channels
649 * and finally inform them of what we have done, in case they want
650 * to review or adjust their own settings based on their own
651 * internal EEPROM data */
653 if (WARN_ON(!__reg_is_valid_request(rd->alpha2, &request)))
656 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
657 reset_regdomains_static();
662 /* Country IE parsing coming soon */
663 switch (request->initiator) {
664 case REGDOM_SET_BY_CORE:
665 case REGDOM_SET_BY_DRIVER:
666 case REGDOM_SET_BY_USER:
667 if (!is_valid_rd(rd)) {
668 printk(KERN_ERR "cfg80211: Invalid "
669 "regulatory domain detected:\n");
670 print_regdomain_info(rd);
674 case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
681 cfg80211_regdomain = rd;
682 request->granted = 1;
688 /* Use this call to set the current regulatory domain. Conflicts with
689 * multiple drivers can be ironed out later. Caller must've already
690 * kmalloc'd the rd structure. If this calls fails you should kfree()
691 * the passed rd. Caller must hold cfg80211_drv_mutex */
692 int set_regdom(struct ieee80211_regdomain *rd)
694 struct regulatory_request *this_request = NULL, *prev_request = NULL;
697 if (!list_empty(®ulatory_requests))
698 prev_request = list_first_entry(®ulatory_requests,
699 struct regulatory_request, list);
701 /* Note that this doesn't update the wiphys, this is done below */
702 r = __set_regdom(rd);
706 BUG_ON((!__reg_is_valid_request(rd->alpha2, &this_request)));
708 /* The initial standard core update of the world regulatory domain, no
709 * need to keep that request info around if it didn't fail. */
710 if (is_world_regdom(rd->alpha2) &&
711 this_request->initiator == REGDOM_SET_BY_CORE &&
712 this_request->granted) {
713 list_del(&this_request->list);
718 /* Remove old requests, we only leave behind the last one */
720 list_del(&prev_request->list);
725 /* This would make this whole thing pointless */
726 BUG_ON(rd != cfg80211_regdomain);
728 /* update all wiphys now with the new established regulatory domain */
729 update_all_wiphy_regulatory(this_request->initiator);
736 int regulatory_init(void)
738 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
739 if (IS_ERR(reg_pdev))
740 return PTR_ERR(reg_pdev);
744 void regulatory_exit(void)
746 struct regulatory_request *req, *req_tmp;
747 mutex_lock(&cfg80211_drv_mutex);
748 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
749 reset_regdomains_static();
753 list_for_each_entry_safe(req, req_tmp, ®ulatory_requests, list) {
754 list_del(&req->list);
757 platform_device_unregister(reg_pdev);
758 mutex_unlock(&cfg80211_drv_mutex);