[SCSI] block: fix the partial mappings with struct rq_map_data
[linux-2.6/mini2440.git] / net / wireless / reg.c
blob4f877535e666ba8f22f9af132628e510d1627b94
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/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>
42 #include "core.h"
43 #include "reg.h"
45 /**
46 * struct regulatory_request - receipt of last regulatory request
48 * @wiphy: this is set if this request's initiator is
49 * %REGDOM_SET_BY_COUNTRY_IE or %REGDOM_SET_BY_DRIVER. This
50 * can be used by the wireless core to deal with conflicts
51 * and potentially inform users of which devices specifically
52 * cased the conflicts.
53 * @initiator: indicates who sent this request, could be any of
54 * of those set in reg_set_by, %REGDOM_SET_BY_*
55 * @alpha2: the ISO / IEC 3166 alpha2 country code of the requested
56 * regulatory domain. We have a few special codes:
57 * 00 - World regulatory domain
58 * 99 - built by driver but a specific alpha2 cannot be determined
59 * 98 - result of an intersection between two regulatory domains
60 * @intersect: indicates whether the wireless core should intersect
61 * the requested regulatory domain with the presently set regulatory
62 * domain.
63 * @country_ie_checksum: checksum of the last processed and accepted
64 * country IE
65 * @country_ie_env: lets us know if the AP is telling us we are outdoor,
66 * indoor, or if it doesn't matter
68 struct regulatory_request {
69 struct wiphy *wiphy;
70 enum reg_set_by initiator;
71 char alpha2[2];
72 bool intersect;
73 u32 country_ie_checksum;
74 enum environment_cap country_ie_env;
77 /* Receipt of information from last regulatory request */
78 static struct regulatory_request *last_request;
80 /* To trigger userspace events */
81 static struct platform_device *reg_pdev;
83 /* Keep the ordering from large to small */
84 static u32 supported_bandwidths[] = {
85 MHZ_TO_KHZ(40),
86 MHZ_TO_KHZ(20),
89 /* Central wireless core regulatory domains, we only need two,
90 * the current one and a world regulatory domain in case we have no
91 * information to give us an alpha2 */
92 static const struct ieee80211_regdomain *cfg80211_regdomain;
94 /* We use this as a place for the rd structure built from the
95 * last parsed country IE to rest until CRDA gets back to us with
96 * what it thinks should apply for the same country */
97 static const struct ieee80211_regdomain *country_ie_regdomain;
99 /* We keep a static world regulatory domain in case of the absence of CRDA */
100 static const struct ieee80211_regdomain world_regdom = {
101 .n_reg_rules = 1,
102 .alpha2 = "00",
103 .reg_rules = {
104 REG_RULE(2412-10, 2462+10, 40, 6, 20,
105 NL80211_RRF_PASSIVE_SCAN |
106 NL80211_RRF_NO_IBSS),
110 static const struct ieee80211_regdomain *cfg80211_world_regdom =
111 &world_regdom;
113 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
114 static char *ieee80211_regdom = "US";
115 module_param(ieee80211_regdom, charp, 0444);
116 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
118 /* We assume 40 MHz bandwidth for the old regulatory work.
119 * We make emphasis we are using the exact same frequencies
120 * as before */
122 static const struct ieee80211_regdomain us_regdom = {
123 .n_reg_rules = 6,
124 .alpha2 = "US",
125 .reg_rules = {
126 /* IEEE 802.11b/g, channels 1..11 */
127 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
128 /* IEEE 802.11a, channel 36 */
129 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
130 /* IEEE 802.11a, channel 40 */
131 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
132 /* IEEE 802.11a, channel 44 */
133 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
134 /* IEEE 802.11a, channels 48..64 */
135 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
136 /* IEEE 802.11a, channels 149..165, outdoor */
137 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
141 static const struct ieee80211_regdomain jp_regdom = {
142 .n_reg_rules = 3,
143 .alpha2 = "JP",
144 .reg_rules = {
145 /* IEEE 802.11b/g, channels 1..14 */
146 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
147 /* IEEE 802.11a, channels 34..48 */
148 REG_RULE(5170-10, 5240+10, 40, 6, 20,
149 NL80211_RRF_PASSIVE_SCAN),
150 /* IEEE 802.11a, channels 52..64 */
151 REG_RULE(5260-10, 5320+10, 40, 6, 20,
152 NL80211_RRF_NO_IBSS |
153 NL80211_RRF_DFS),
157 static const struct ieee80211_regdomain eu_regdom = {
158 .n_reg_rules = 6,
159 /* This alpha2 is bogus, we leave it here just for stupid
160 * backward compatibility */
161 .alpha2 = "EU",
162 .reg_rules = {
163 /* IEEE 802.11b/g, channels 1..13 */
164 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
165 /* IEEE 802.11a, channel 36 */
166 REG_RULE(5180-10, 5180+10, 40, 6, 23,
167 NL80211_RRF_PASSIVE_SCAN),
168 /* IEEE 802.11a, channel 40 */
169 REG_RULE(5200-10, 5200+10, 40, 6, 23,
170 NL80211_RRF_PASSIVE_SCAN),
171 /* IEEE 802.11a, channel 44 */
172 REG_RULE(5220-10, 5220+10, 40, 6, 23,
173 NL80211_RRF_PASSIVE_SCAN),
174 /* IEEE 802.11a, channels 48..64 */
175 REG_RULE(5240-10, 5320+10, 40, 6, 20,
176 NL80211_RRF_NO_IBSS |
177 NL80211_RRF_DFS),
178 /* IEEE 802.11a, channels 100..140 */
179 REG_RULE(5500-10, 5700+10, 40, 6, 30,
180 NL80211_RRF_NO_IBSS |
181 NL80211_RRF_DFS),
185 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
187 if (alpha2[0] == 'U' && alpha2[1] == 'S')
188 return &us_regdom;
189 if (alpha2[0] == 'J' && alpha2[1] == 'P')
190 return &jp_regdom;
191 if (alpha2[0] == 'E' && alpha2[1] == 'U')
192 return &eu_regdom;
193 /* Default, as per the old rules */
194 return &us_regdom;
197 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
199 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
200 return true;
201 return false;
203 #else
204 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
206 return false;
208 #endif
210 static void reset_regdomains(void)
212 /* avoid freeing static information or freeing something twice */
213 if (cfg80211_regdomain == cfg80211_world_regdom)
214 cfg80211_regdomain = NULL;
215 if (cfg80211_world_regdom == &world_regdom)
216 cfg80211_world_regdom = NULL;
217 if (cfg80211_regdomain == &world_regdom)
218 cfg80211_regdomain = NULL;
219 if (is_old_static_regdom(cfg80211_regdomain))
220 cfg80211_regdomain = NULL;
222 kfree(cfg80211_regdomain);
223 kfree(cfg80211_world_regdom);
225 cfg80211_world_regdom = &world_regdom;
226 cfg80211_regdomain = NULL;
229 /* Dynamic world regulatory domain requested by the wireless
230 * core upon initialization */
231 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
233 BUG_ON(!last_request);
235 reset_regdomains();
237 cfg80211_world_regdom = rd;
238 cfg80211_regdomain = rd;
241 bool is_world_regdom(const char *alpha2)
243 if (!alpha2)
244 return false;
245 if (alpha2[0] == '0' && alpha2[1] == '0')
246 return true;
247 return false;
250 static bool is_alpha2_set(const char *alpha2)
252 if (!alpha2)
253 return false;
254 if (alpha2[0] != 0 && alpha2[1] != 0)
255 return true;
256 return false;
259 static bool is_alpha_upper(char letter)
261 /* ASCII A - Z */
262 if (letter >= 65 && letter <= 90)
263 return true;
264 return false;
267 static bool is_unknown_alpha2(const char *alpha2)
269 if (!alpha2)
270 return false;
271 /* Special case where regulatory domain was built by driver
272 * but a specific alpha2 cannot be determined */
273 if (alpha2[0] == '9' && alpha2[1] == '9')
274 return true;
275 return false;
278 static bool is_intersected_alpha2(const char *alpha2)
280 if (!alpha2)
281 return false;
282 /* Special case where regulatory domain is the
283 * result of an intersection between two regulatory domain
284 * structures */
285 if (alpha2[0] == '9' && alpha2[1] == '8')
286 return true;
287 return false;
290 static bool is_an_alpha2(const char *alpha2)
292 if (!alpha2)
293 return false;
294 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
295 return true;
296 return false;
299 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
301 if (!alpha2_x || !alpha2_y)
302 return false;
303 if (alpha2_x[0] == alpha2_y[0] &&
304 alpha2_x[1] == alpha2_y[1])
305 return true;
306 return false;
309 static bool regdom_changed(const char *alpha2)
311 if (!cfg80211_regdomain)
312 return true;
313 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
314 return false;
315 return true;
319 * country_ie_integrity_changes - tells us if the country IE has changed
320 * @checksum: checksum of country IE of fields we are interested in
322 * If the country IE has not changed you can ignore it safely. This is
323 * useful to determine if two devices are seeing two different country IEs
324 * even on the same alpha2. Note that this will return false if no IE has
325 * been set on the wireless core yet.
327 static bool country_ie_integrity_changes(u32 checksum)
329 /* If no IE has been set then the checksum doesn't change */
330 if (unlikely(!last_request->country_ie_checksum))
331 return false;
332 if (unlikely(last_request->country_ie_checksum != checksum))
333 return true;
334 return false;
337 /* This lets us keep regulatory code which is updated on a regulatory
338 * basis in userspace. */
339 static int call_crda(const char *alpha2)
341 char country_env[9 + 2] = "COUNTRY=";
342 char *envp[] = {
343 country_env,
344 NULL
347 if (!is_world_regdom((char *) alpha2))
348 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
349 alpha2[0], alpha2[1]);
350 else
351 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
352 "regulatory domain\n");
354 country_env[8] = alpha2[0];
355 country_env[9] = alpha2[1];
357 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
360 /* Used by nl80211 before kmalloc'ing our regulatory domain */
361 bool reg_is_valid_request(const char *alpha2)
363 if (!last_request)
364 return false;
366 return alpha2_equal(last_request->alpha2, alpha2);
369 /* Sanity check on a regulatory rule */
370 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
372 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
373 u32 freq_diff;
375 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
376 return false;
378 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
379 return false;
381 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
383 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
384 return false;
386 return true;
389 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
391 const struct ieee80211_reg_rule *reg_rule = NULL;
392 unsigned int i;
394 if (!rd->n_reg_rules)
395 return false;
397 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
398 return false;
400 for (i = 0; i < rd->n_reg_rules; i++) {
401 reg_rule = &rd->reg_rules[i];
402 if (!is_valid_reg_rule(reg_rule))
403 return false;
406 return true;
409 /* Returns value in KHz */
410 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
411 u32 freq)
413 unsigned int i;
414 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
415 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
416 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
417 if (start_freq_khz >= freq_range->start_freq_khz &&
418 end_freq_khz <= freq_range->end_freq_khz)
419 return supported_bandwidths[i];
421 return 0;
424 /* Converts a country IE to a regulatory domain. A regulatory domain
425 * structure has a lot of information which the IE doesn't yet have,
426 * so for the other values we use upper max values as we will intersect
427 * with our userspace regulatory agent to get lower bounds. */
428 static struct ieee80211_regdomain *country_ie_2_rd(
429 u8 *country_ie,
430 u8 country_ie_len,
431 u32 *checksum)
433 struct ieee80211_regdomain *rd = NULL;
434 unsigned int i = 0;
435 char alpha2[2];
436 u32 flags = 0;
437 u32 num_rules = 0, size_of_regd = 0;
438 u8 *triplets_start = NULL;
439 u8 len_at_triplet = 0;
440 /* the last channel we have registered in a subband (triplet) */
441 int last_sub_max_channel = 0;
443 *checksum = 0xDEADBEEF;
445 /* Country IE requirements */
446 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
447 country_ie_len & 0x01);
449 alpha2[0] = country_ie[0];
450 alpha2[1] = country_ie[1];
453 * Third octet can be:
454 * 'I' - Indoor
455 * 'O' - Outdoor
457 * anything else we assume is no restrictions
459 if (country_ie[2] == 'I')
460 flags = NL80211_RRF_NO_OUTDOOR;
461 else if (country_ie[2] == 'O')
462 flags = NL80211_RRF_NO_INDOOR;
464 country_ie += 3;
465 country_ie_len -= 3;
467 triplets_start = country_ie;
468 len_at_triplet = country_ie_len;
470 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
472 /* We need to build a reg rule for each triplet, but first we must
473 * calculate the number of reg rules we will need. We will need one
474 * for each channel subband */
475 while (country_ie_len >= 3) {
476 struct ieee80211_country_ie_triplet *triplet =
477 (struct ieee80211_country_ie_triplet *) country_ie;
478 int cur_sub_max_channel = 0, cur_channel = 0;
480 if (triplet->ext.reg_extension_id >=
481 IEEE80211_COUNTRY_EXTENSION_ID) {
482 country_ie += 3;
483 country_ie_len -= 3;
484 continue;
487 cur_channel = triplet->chans.first_channel;
488 cur_sub_max_channel = ieee80211_channel_to_frequency(
489 cur_channel + triplet->chans.num_channels);
491 /* Basic sanity check */
492 if (cur_sub_max_channel < cur_channel)
493 return NULL;
495 /* Do not allow overlapping channels. Also channels
496 * passed in each subband must be monotonically
497 * increasing */
498 if (last_sub_max_channel) {
499 if (cur_channel <= last_sub_max_channel)
500 return NULL;
501 if (cur_sub_max_channel <= last_sub_max_channel)
502 return NULL;
505 /* When dot11RegulatoryClassesRequired is supported
506 * we can throw ext triplets as part of this soup,
507 * for now we don't care when those change as we
508 * don't support them */
509 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
510 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
511 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
513 last_sub_max_channel = cur_sub_max_channel;
515 country_ie += 3;
516 country_ie_len -= 3;
517 num_rules++;
519 /* Note: this is not a IEEE requirement but
520 * simply a memory requirement */
521 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
522 return NULL;
525 country_ie = triplets_start;
526 country_ie_len = len_at_triplet;
528 size_of_regd = sizeof(struct ieee80211_regdomain) +
529 (num_rules * sizeof(struct ieee80211_reg_rule));
531 rd = kzalloc(size_of_regd, GFP_KERNEL);
532 if (!rd)
533 return NULL;
535 rd->n_reg_rules = num_rules;
536 rd->alpha2[0] = alpha2[0];
537 rd->alpha2[1] = alpha2[1];
539 /* This time around we fill in the rd */
540 while (country_ie_len >= 3) {
541 struct ieee80211_country_ie_triplet *triplet =
542 (struct ieee80211_country_ie_triplet *) country_ie;
543 struct ieee80211_reg_rule *reg_rule = NULL;
544 struct ieee80211_freq_range *freq_range = NULL;
545 struct ieee80211_power_rule *power_rule = NULL;
547 /* Must parse if dot11RegulatoryClassesRequired is true,
548 * we don't support this yet */
549 if (triplet->ext.reg_extension_id >=
550 IEEE80211_COUNTRY_EXTENSION_ID) {
551 country_ie += 3;
552 country_ie_len -= 3;
553 continue;
556 reg_rule = &rd->reg_rules[i];
557 freq_range = &reg_rule->freq_range;
558 power_rule = &reg_rule->power_rule;
560 reg_rule->flags = flags;
562 /* The +10 is since the regulatory domain expects
563 * the actual band edge, not the center of freq for
564 * its start and end freqs, assuming 20 MHz bandwidth on
565 * the channels passed */
566 freq_range->start_freq_khz =
567 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
568 triplet->chans.first_channel) - 10);
569 freq_range->end_freq_khz =
570 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
571 triplet->chans.first_channel +
572 triplet->chans.num_channels) + 10);
574 /* Large arbitrary values, we intersect later */
575 /* Increment this if we ever support >= 40 MHz channels
576 * in IEEE 802.11 */
577 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
578 power_rule->max_antenna_gain = DBI_TO_MBI(100);
579 power_rule->max_eirp = DBM_TO_MBM(100);
581 country_ie += 3;
582 country_ie_len -= 3;
583 i++;
585 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
588 return rd;
592 /* Helper for regdom_intersect(), this does the real
593 * mathematical intersection fun */
594 static int reg_rules_intersect(
595 const struct ieee80211_reg_rule *rule1,
596 const struct ieee80211_reg_rule *rule2,
597 struct ieee80211_reg_rule *intersected_rule)
599 const struct ieee80211_freq_range *freq_range1, *freq_range2;
600 struct ieee80211_freq_range *freq_range;
601 const struct ieee80211_power_rule *power_rule1, *power_rule2;
602 struct ieee80211_power_rule *power_rule;
603 u32 freq_diff;
605 freq_range1 = &rule1->freq_range;
606 freq_range2 = &rule2->freq_range;
607 freq_range = &intersected_rule->freq_range;
609 power_rule1 = &rule1->power_rule;
610 power_rule2 = &rule2->power_rule;
611 power_rule = &intersected_rule->power_rule;
613 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
614 freq_range2->start_freq_khz);
615 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
616 freq_range2->end_freq_khz);
617 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
618 freq_range2->max_bandwidth_khz);
620 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
621 if (freq_range->max_bandwidth_khz > freq_diff)
622 freq_range->max_bandwidth_khz = freq_diff;
624 power_rule->max_eirp = min(power_rule1->max_eirp,
625 power_rule2->max_eirp);
626 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
627 power_rule2->max_antenna_gain);
629 intersected_rule->flags = (rule1->flags | rule2->flags);
631 if (!is_valid_reg_rule(intersected_rule))
632 return -EINVAL;
634 return 0;
638 * regdom_intersect - do the intersection between two regulatory domains
639 * @rd1: first regulatory domain
640 * @rd2: second regulatory domain
642 * Use this function to get the intersection between two regulatory domains.
643 * Once completed we will mark the alpha2 for the rd as intersected, "98",
644 * as no one single alpha2 can represent this regulatory domain.
646 * Returns a pointer to the regulatory domain structure which will hold the
647 * resulting intersection of rules between rd1 and rd2. We will
648 * kzalloc() this structure for you.
650 static struct ieee80211_regdomain *regdom_intersect(
651 const struct ieee80211_regdomain *rd1,
652 const struct ieee80211_regdomain *rd2)
654 int r, size_of_regd;
655 unsigned int x, y;
656 unsigned int num_rules = 0, rule_idx = 0;
657 const struct ieee80211_reg_rule *rule1, *rule2;
658 struct ieee80211_reg_rule *intersected_rule;
659 struct ieee80211_regdomain *rd;
660 /* This is just a dummy holder to help us count */
661 struct ieee80211_reg_rule irule;
663 /* Uses the stack temporarily for counter arithmetic */
664 intersected_rule = &irule;
666 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
668 if (!rd1 || !rd2)
669 return NULL;
671 /* First we get a count of the rules we'll need, then we actually
672 * build them. This is to so we can malloc() and free() a
673 * regdomain once. The reason we use reg_rules_intersect() here
674 * is it will return -EINVAL if the rule computed makes no sense.
675 * All rules that do check out OK are valid. */
677 for (x = 0; x < rd1->n_reg_rules; x++) {
678 rule1 = &rd1->reg_rules[x];
679 for (y = 0; y < rd2->n_reg_rules; y++) {
680 rule2 = &rd2->reg_rules[y];
681 if (!reg_rules_intersect(rule1, rule2,
682 intersected_rule))
683 num_rules++;
684 memset(intersected_rule, 0,
685 sizeof(struct ieee80211_reg_rule));
689 if (!num_rules)
690 return NULL;
692 size_of_regd = sizeof(struct ieee80211_regdomain) +
693 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
695 rd = kzalloc(size_of_regd, GFP_KERNEL);
696 if (!rd)
697 return NULL;
699 for (x = 0; x < rd1->n_reg_rules; x++) {
700 rule1 = &rd1->reg_rules[x];
701 for (y = 0; y < rd2->n_reg_rules; y++) {
702 rule2 = &rd2->reg_rules[y];
703 /* This time around instead of using the stack lets
704 * write to the target rule directly saving ourselves
705 * a memcpy() */
706 intersected_rule = &rd->reg_rules[rule_idx];
707 r = reg_rules_intersect(rule1, rule2,
708 intersected_rule);
709 /* No need to memset here the intersected rule here as
710 * we're not using the stack anymore */
711 if (r)
712 continue;
713 rule_idx++;
717 if (rule_idx != num_rules) {
718 kfree(rd);
719 return NULL;
722 rd->n_reg_rules = num_rules;
723 rd->alpha2[0] = '9';
724 rd->alpha2[1] = '8';
726 return rd;
729 /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
730 * want to just have the channel structure use these */
731 static u32 map_regdom_flags(u32 rd_flags)
733 u32 channel_flags = 0;
734 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
735 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
736 if (rd_flags & NL80211_RRF_NO_IBSS)
737 channel_flags |= IEEE80211_CHAN_NO_IBSS;
738 if (rd_flags & NL80211_RRF_DFS)
739 channel_flags |= IEEE80211_CHAN_RADAR;
740 return channel_flags;
744 * freq_reg_info - get regulatory information for the given frequency
745 * @center_freq: Frequency in KHz for which we want regulatory information for
746 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
747 * you can set this to 0. If this frequency is allowed we then set
748 * this value to the maximum allowed bandwidth.
749 * @reg_rule: the regulatory rule which we have for this frequency
751 * Use this function to get the regulatory rule for a specific frequency.
753 static int freq_reg_info(u32 center_freq, u32 *bandwidth,
754 const struct ieee80211_reg_rule **reg_rule)
756 int i;
757 u32 max_bandwidth = 0;
759 if (!cfg80211_regdomain)
760 return -EINVAL;
762 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
763 const struct ieee80211_reg_rule *rr;
764 const struct ieee80211_freq_range *fr = NULL;
765 const struct ieee80211_power_rule *pr = NULL;
767 rr = &cfg80211_regdomain->reg_rules[i];
768 fr = &rr->freq_range;
769 pr = &rr->power_rule;
770 max_bandwidth = freq_max_bandwidth(fr, center_freq);
771 if (max_bandwidth && *bandwidth <= max_bandwidth) {
772 *reg_rule = rr;
773 *bandwidth = max_bandwidth;
774 break;
778 return !max_bandwidth;
781 static void handle_channel(struct ieee80211_channel *chan)
783 int r;
784 u32 flags = chan->orig_flags;
785 u32 max_bandwidth = 0;
786 const struct ieee80211_reg_rule *reg_rule = NULL;
787 const struct ieee80211_power_rule *power_rule = NULL;
789 r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
790 &max_bandwidth, &reg_rule);
792 if (r) {
793 flags |= IEEE80211_CHAN_DISABLED;
794 chan->flags = flags;
795 return;
798 power_rule = &reg_rule->power_rule;
800 chan->flags = flags | map_regdom_flags(reg_rule->flags);
801 chan->max_antenna_gain = min(chan->orig_mag,
802 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
803 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
804 if (chan->orig_mpwr)
805 chan->max_power = min(chan->orig_mpwr,
806 (int) MBM_TO_DBM(power_rule->max_eirp));
807 else
808 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
811 static void handle_band(struct ieee80211_supported_band *sband)
813 int i;
815 for (i = 0; i < sband->n_channels; i++)
816 handle_channel(&sband->channels[i]);
819 static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
821 if (!last_request)
822 return true;
823 if (setby == REGDOM_SET_BY_CORE &&
824 wiphy->fw_handles_regulatory)
825 return true;
826 return false;
829 static void update_all_wiphy_regulatory(enum reg_set_by setby)
831 struct cfg80211_registered_device *drv;
833 list_for_each_entry(drv, &cfg80211_drv_list, list)
834 if (!ignore_reg_update(&drv->wiphy, setby))
835 wiphy_update_regulatory(&drv->wiphy, setby);
838 void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
840 enum ieee80211_band band;
841 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
842 if (wiphy->bands[band])
843 handle_band(wiphy->bands[band]);
844 if (wiphy->reg_notifier)
845 wiphy->reg_notifier(wiphy, setby);
849 /* Return value which can be used by ignore_request() to indicate
850 * it has been determined we should intersect two regulatory domains */
851 #define REG_INTERSECT 1
853 /* This has the logic which determines when a new request
854 * should be ignored. */
855 static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
856 const char *alpha2)
858 /* All initial requests are respected */
859 if (!last_request)
860 return 0;
862 switch (set_by) {
863 case REGDOM_SET_BY_INIT:
864 return -EINVAL;
865 case REGDOM_SET_BY_CORE:
867 * Always respect new wireless core hints, should only happen
868 * when updating the world regulatory domain at init.
870 return 0;
871 case REGDOM_SET_BY_COUNTRY_IE:
872 if (unlikely(!is_an_alpha2(alpha2)))
873 return -EINVAL;
874 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
875 if (last_request->wiphy != wiphy) {
877 * Two cards with two APs claiming different
878 * different Country IE alpha2s. We could
879 * intersect them, but that seems unlikely
880 * to be correct. Reject second one for now.
882 if (!alpha2_equal(alpha2,
883 cfg80211_regdomain->alpha2))
884 return -EOPNOTSUPP;
885 return -EALREADY;
887 /* Two consecutive Country IE hints on the same wiphy.
888 * This should be picked up early by the driver/stack */
889 if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
890 alpha2)))
891 return 0;
892 return -EALREADY;
894 return REG_INTERSECT;
895 case REGDOM_SET_BY_DRIVER:
896 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
897 return -EALREADY;
898 return 0;
899 case REGDOM_SET_BY_USER:
900 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
901 return REG_INTERSECT;
902 /* If the user knows better the user should set the regdom
903 * to their country before the IE is picked up */
904 if (last_request->initiator == REGDOM_SET_BY_USER &&
905 last_request->intersect)
906 return -EOPNOTSUPP;
907 return 0;
910 return -EINVAL;
913 /* Caller must hold &cfg80211_drv_mutex */
914 int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
915 const char *alpha2,
916 u32 country_ie_checksum,
917 enum environment_cap env)
919 struct regulatory_request *request;
920 bool intersect = false;
921 int r = 0;
923 r = ignore_request(wiphy, set_by, alpha2);
925 if (r == REG_INTERSECT)
926 intersect = true;
927 else if (r)
928 return r;
930 request = kzalloc(sizeof(struct regulatory_request),
931 GFP_KERNEL);
932 if (!request)
933 return -ENOMEM;
935 request->alpha2[0] = alpha2[0];
936 request->alpha2[1] = alpha2[1];
937 request->initiator = set_by;
938 request->wiphy = wiphy;
939 request->intersect = intersect;
940 request->country_ie_checksum = country_ie_checksum;
941 request->country_ie_env = env;
943 kfree(last_request);
944 last_request = request;
946 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
947 * AND if CRDA is NOT present nothing will happen, if someone
948 * wants to bother with 11d with OLD_REG you can add a timer.
949 * If after x amount of time nothing happens you can call:
951 * return set_regdom(country_ie_regdomain);
953 * to intersect with the static rd
955 return call_crda(alpha2);
958 void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
960 BUG_ON(!alpha2);
962 mutex_lock(&cfg80211_drv_mutex);
963 __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY);
964 mutex_unlock(&cfg80211_drv_mutex);
966 EXPORT_SYMBOL(regulatory_hint);
968 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
969 u32 country_ie_checksum)
971 if (!last_request->wiphy)
972 return false;
973 if (likely(last_request->wiphy != wiphy))
974 return !country_ie_integrity_changes(country_ie_checksum);
975 /* We should not have let these through at this point, they
976 * should have been picked up earlier by the first alpha2 check
977 * on the device */
978 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
979 return true;
980 return false;
983 void regulatory_hint_11d(struct wiphy *wiphy,
984 u8 *country_ie,
985 u8 country_ie_len)
987 struct ieee80211_regdomain *rd = NULL;
988 char alpha2[2];
989 u32 checksum = 0;
990 enum environment_cap env = ENVIRON_ANY;
992 if (!last_request)
993 return;
995 mutex_lock(&cfg80211_drv_mutex);
997 /* IE len must be evenly divisible by 2 */
998 if (country_ie_len & 0x01)
999 goto out;
1001 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1002 goto out;
1004 /* Pending country IE processing, this can happen after we
1005 * call CRDA and wait for a response if a beacon was received before
1006 * we were able to process the last regulatory_hint_11d() call */
1007 if (country_ie_regdomain)
1008 goto out;
1010 alpha2[0] = country_ie[0];
1011 alpha2[1] = country_ie[1];
1013 if (country_ie[2] == 'I')
1014 env = ENVIRON_INDOOR;
1015 else if (country_ie[2] == 'O')
1016 env = ENVIRON_OUTDOOR;
1018 /* We will run this for *every* beacon processed for the BSSID, so
1019 * we optimize an early check to exit out early if we don't have to
1020 * do anything */
1021 if (likely(last_request->wiphy)) {
1022 struct cfg80211_registered_device *drv_last_ie;
1024 drv_last_ie = wiphy_to_dev(last_request->wiphy);
1026 /* Lets keep this simple -- we trust the first AP
1027 * after we intersect with CRDA */
1028 if (likely(last_request->wiphy == wiphy)) {
1029 /* Ignore IEs coming in on this wiphy with
1030 * the same alpha2 and environment cap */
1031 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1032 alpha2) &&
1033 env == drv_last_ie->env)) {
1034 goto out;
1036 /* the wiphy moved on to another BSSID or the AP
1037 * was reconfigured. XXX: We need to deal with the
1038 * case where the user suspends and goes to goes
1039 * to another country, and then gets IEs from an
1040 * AP with different settings */
1041 goto out;
1042 } else {
1043 /* Ignore IEs coming in on two separate wiphys with
1044 * the same alpha2 and environment cap */
1045 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1046 alpha2) &&
1047 env == drv_last_ie->env)) {
1048 goto out;
1050 /* We could potentially intersect though */
1051 goto out;
1055 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1056 if (!rd)
1057 goto out;
1059 /* This will not happen right now but we leave it here for the
1060 * the future when we want to add suspend/resume support and having
1061 * the user move to another country after doing so, or having the user
1062 * move to another AP. Right now we just trust the first AP. This is why
1063 * this is marked as likley(). If we hit this before we add this support
1064 * we want to be informed of it as it would indicate a mistake in the
1065 * current design */
1066 if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
1067 goto out;
1069 /* We keep this around for when CRDA comes back with a response so
1070 * we can intersect with that */
1071 country_ie_regdomain = rd;
1073 __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
1074 country_ie_regdomain->alpha2, checksum, env);
1076 out:
1077 mutex_unlock(&cfg80211_drv_mutex);
1079 EXPORT_SYMBOL(regulatory_hint_11d);
1081 static void print_rd_rules(const struct ieee80211_regdomain *rd)
1083 unsigned int i;
1084 const struct ieee80211_reg_rule *reg_rule = NULL;
1085 const struct ieee80211_freq_range *freq_range = NULL;
1086 const struct ieee80211_power_rule *power_rule = NULL;
1088 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1089 "(max_antenna_gain, max_eirp)\n");
1091 for (i = 0; i < rd->n_reg_rules; i++) {
1092 reg_rule = &rd->reg_rules[i];
1093 freq_range = &reg_rule->freq_range;
1094 power_rule = &reg_rule->power_rule;
1096 /* There may not be documentation for max antenna gain
1097 * in certain regions */
1098 if (power_rule->max_antenna_gain)
1099 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1100 "(%d mBi, %d mBm)\n",
1101 freq_range->start_freq_khz,
1102 freq_range->end_freq_khz,
1103 freq_range->max_bandwidth_khz,
1104 power_rule->max_antenna_gain,
1105 power_rule->max_eirp);
1106 else
1107 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1108 "(N/A, %d mBm)\n",
1109 freq_range->start_freq_khz,
1110 freq_range->end_freq_khz,
1111 freq_range->max_bandwidth_khz,
1112 power_rule->max_eirp);
1116 static void print_regdomain(const struct ieee80211_regdomain *rd)
1119 if (is_intersected_alpha2(rd->alpha2)) {
1120 struct wiphy *wiphy = NULL;
1121 struct cfg80211_registered_device *drv;
1123 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1124 if (last_request->wiphy) {
1125 wiphy = last_request->wiphy;
1126 drv = wiphy_to_dev(wiphy);
1127 printk(KERN_INFO "cfg80211: Current regulatory "
1128 "domain updated by AP to: %c%c\n",
1129 drv->country_ie_alpha2[0],
1130 drv->country_ie_alpha2[1]);
1131 } else
1132 printk(KERN_INFO "cfg80211: Current regulatory "
1133 "domain intersected: \n");
1134 } else
1135 printk(KERN_INFO "cfg80211: Current regulatory "
1136 "intersected: \n");
1137 } else if (is_world_regdom(rd->alpha2))
1138 printk(KERN_INFO "cfg80211: World regulatory "
1139 "domain updated:\n");
1140 else {
1141 if (is_unknown_alpha2(rd->alpha2))
1142 printk(KERN_INFO "cfg80211: Regulatory domain "
1143 "changed to driver built-in settings "
1144 "(unknown country)\n");
1145 else
1146 printk(KERN_INFO "cfg80211: Regulatory domain "
1147 "changed to country: %c%c\n",
1148 rd->alpha2[0], rd->alpha2[1]);
1150 print_rd_rules(rd);
1153 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
1155 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1156 rd->alpha2[0], rd->alpha2[1]);
1157 print_rd_rules(rd);
1160 #ifdef CONFIG_CFG80211_REG_DEBUG
1161 static void reg_country_ie_process_debug(
1162 const struct ieee80211_regdomain *rd,
1163 const struct ieee80211_regdomain *country_ie_regdomain,
1164 const struct ieee80211_regdomain *intersected_rd)
1166 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1167 print_regdomain_info(country_ie_regdomain);
1168 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1169 print_regdomain_info(rd);
1170 if (intersected_rd) {
1171 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1172 "and get:\n");
1173 print_regdomain_info(rd);
1174 return;
1176 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1178 #else
1179 static inline void reg_country_ie_process_debug(
1180 const struct ieee80211_regdomain *rd,
1181 const struct ieee80211_regdomain *country_ie_regdomain,
1182 const struct ieee80211_regdomain *intersected_rd)
1185 #endif
1187 /* Takes ownership of rd only if it doesn't fail */
1188 static int __set_regdom(const struct ieee80211_regdomain *rd)
1190 const struct ieee80211_regdomain *intersected_rd = NULL;
1191 struct cfg80211_registered_device *drv = NULL;
1192 struct wiphy *wiphy = NULL;
1193 /* Some basic sanity checks first */
1195 if (is_world_regdom(rd->alpha2)) {
1196 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1197 return -EINVAL;
1198 update_world_regdomain(rd);
1199 return 0;
1202 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1203 !is_unknown_alpha2(rd->alpha2))
1204 return -EINVAL;
1206 if (!last_request)
1207 return -EINVAL;
1209 /* Lets only bother proceeding on the same alpha2 if the current
1210 * rd is non static (it means CRDA was present and was used last)
1211 * and the pending request came in from a country IE */
1212 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1213 /* If someone else asked us to change the rd lets only bother
1214 * checking if the alpha2 changes if CRDA was already called */
1215 if (!is_old_static_regdom(cfg80211_regdomain) &&
1216 !regdom_changed(rd->alpha2))
1217 return -EINVAL;
1220 wiphy = last_request->wiphy;
1222 /* Now lets set the regulatory domain, update all driver channels
1223 * and finally inform them of what we have done, in case they want
1224 * to review or adjust their own settings based on their own
1225 * internal EEPROM data */
1227 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1228 return -EINVAL;
1230 if (!is_valid_rd(rd)) {
1231 printk(KERN_ERR "cfg80211: Invalid "
1232 "regulatory domain detected:\n");
1233 print_regdomain_info(rd);
1234 return -EINVAL;
1237 if (!last_request->intersect) {
1238 reset_regdomains();
1239 cfg80211_regdomain = rd;
1240 return 0;
1243 /* Intersection requires a bit more work */
1245 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1247 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1248 if (!intersected_rd)
1249 return -EINVAL;
1251 /* We can trash what CRDA provided now */
1252 kfree(rd);
1253 rd = NULL;
1255 reset_regdomains();
1256 cfg80211_regdomain = intersected_rd;
1258 return 0;
1262 * Country IE requests are handled a bit differently, we intersect
1263 * the country IE rd with what CRDA believes that country should have
1266 BUG_ON(!country_ie_regdomain);
1268 if (rd != country_ie_regdomain) {
1269 /* Intersect what CRDA returned and our what we
1270 * had built from the Country IE received */
1272 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1274 reg_country_ie_process_debug(rd, country_ie_regdomain,
1275 intersected_rd);
1277 kfree(country_ie_regdomain);
1278 country_ie_regdomain = NULL;
1279 } else {
1280 /* This would happen when CRDA was not present and
1281 * OLD_REGULATORY was enabled. We intersect our Country
1282 * IE rd and what was set on cfg80211 originally */
1283 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1286 if (!intersected_rd)
1287 return -EINVAL;
1289 drv = wiphy_to_dev(wiphy);
1291 drv->country_ie_alpha2[0] = rd->alpha2[0];
1292 drv->country_ie_alpha2[1] = rd->alpha2[1];
1293 drv->env = last_request->country_ie_env;
1295 BUG_ON(intersected_rd == rd);
1297 kfree(rd);
1298 rd = NULL;
1300 reset_regdomains();
1301 cfg80211_regdomain = intersected_rd;
1303 return 0;
1307 /* Use this call to set the current regulatory domain. Conflicts with
1308 * multiple drivers can be ironed out later. Caller must've already
1309 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
1310 int set_regdom(const struct ieee80211_regdomain *rd)
1312 int r;
1314 /* Note that this doesn't update the wiphys, this is done below */
1315 r = __set_regdom(rd);
1316 if (r) {
1317 kfree(rd);
1318 return r;
1321 /* This would make this whole thing pointless */
1322 if (!last_request->intersect)
1323 BUG_ON(rd != cfg80211_regdomain);
1325 /* update all wiphys now with the new established regulatory domain */
1326 update_all_wiphy_regulatory(last_request->initiator);
1328 print_regdomain(cfg80211_regdomain);
1330 return r;
1333 /* Caller must hold cfg80211_drv_mutex */
1334 void reg_device_remove(struct wiphy *wiphy)
1336 if (!last_request || !last_request->wiphy)
1337 return;
1338 if (last_request->wiphy != wiphy)
1339 return;
1340 last_request->wiphy = NULL;
1341 last_request->country_ie_env = ENVIRON_ANY;
1344 int regulatory_init(void)
1346 int err;
1348 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1349 if (IS_ERR(reg_pdev))
1350 return PTR_ERR(reg_pdev);
1352 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
1353 cfg80211_regdomain = static_regdom(ieee80211_regdom);
1355 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
1356 print_regdomain_info(cfg80211_regdomain);
1357 /* The old code still requests for a new regdomain and if
1358 * you have CRDA you get it updated, otherwise you get
1359 * stuck with the static values. We ignore "EU" code as
1360 * that is not a valid ISO / IEC 3166 alpha2 */
1361 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
1362 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
1363 ieee80211_regdom, 0, ENVIRON_ANY);
1364 #else
1365 cfg80211_regdomain = cfg80211_world_regdom;
1367 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
1368 if (err)
1369 printk(KERN_ERR "cfg80211: calling CRDA failed - "
1370 "unable to update world regulatory domain, "
1371 "using static definition\n");
1372 #endif
1374 return 0;
1377 void regulatory_exit(void)
1379 mutex_lock(&cfg80211_drv_mutex);
1381 reset_regdomains();
1383 kfree(country_ie_regdomain);
1384 country_ie_regdomain = NULL;
1386 kfree(last_request);
1388 platform_device_unregister(reg_pdev);
1390 mutex_unlock(&cfg80211_drv_mutex);