Merge branch 'linus' into core/printk
[sfrench/cifs-2.6.git] / net / wireless / reg.c
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>
6  *
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.
10  */
11
12 /**
13  * DOC: Wireless regulatory infrastructure
14  *
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.
19  *
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.
23  *
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.
29  *
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.
33  *
34  */
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"
44
45 /**
46  * struct regulatory_request - receipt of last regulatory request
47  *
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
67  */
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;
75 };
76
77 /* Receipt of information from last regulatory request */
78 static struct regulatory_request *last_request;
79
80 /* To trigger userspace events */
81 static struct platform_device *reg_pdev;
82
83 /* Keep the ordering from large to small */
84 static u32 supported_bandwidths[] = {
85         MHZ_TO_KHZ(40),
86         MHZ_TO_KHZ(20),
87 };
88
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;
93
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;
98
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),
107         }
108 };
109
110 static const struct ieee80211_regdomain *cfg80211_world_regdom =
111         &world_regdom;
112
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");
117
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 */
121
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),
138         }
139 };
140
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),
154         }
155 };
156
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),
182         }
183 };
184
185 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
186 {
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;
195 }
196
197 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
198 {
199         if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
200                 return true;
201         return false;
202 }
203 #else
204 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
205 {
206         return false;
207 }
208 #endif
209
210 static void reset_regdomains(void)
211 {
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;
221
222         kfree(cfg80211_regdomain);
223         kfree(cfg80211_world_regdom);
224
225         cfg80211_world_regdom = &world_regdom;
226         cfg80211_regdomain = NULL;
227 }
228
229 /* Dynamic world regulatory domain requested by the wireless
230  * core upon initialization */
231 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
232 {
233         BUG_ON(!last_request);
234
235         reset_regdomains();
236
237         cfg80211_world_regdom = rd;
238         cfg80211_regdomain = rd;
239 }
240
241 bool is_world_regdom(const char *alpha2)
242 {
243         if (!alpha2)
244                 return false;
245         if (alpha2[0] == '0' && alpha2[1] == '0')
246                 return true;
247         return false;
248 }
249
250 static bool is_alpha2_set(const char *alpha2)
251 {
252         if (!alpha2)
253                 return false;
254         if (alpha2[0] != 0 && alpha2[1] != 0)
255                 return true;
256         return false;
257 }
258
259 static bool is_alpha_upper(char letter)
260 {
261         /* ASCII A - Z */
262         if (letter >= 65 && letter <= 90)
263                 return true;
264         return false;
265 }
266
267 static bool is_unknown_alpha2(const char *alpha2)
268 {
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;
276 }
277
278 static bool is_intersected_alpha2(const char *alpha2)
279 {
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;
288 }
289
290 static bool is_an_alpha2(const char *alpha2)
291 {
292         if (!alpha2)
293                 return false;
294         if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
295                 return true;
296         return false;
297 }
298
299 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
300 {
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;
307 }
308
309 static bool regdom_changed(const char *alpha2)
310 {
311         if (!cfg80211_regdomain)
312                 return true;
313         if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
314                 return false;
315         return true;
316 }
317
318 /**
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
321  *
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.
326  */
327 static bool country_ie_integrity_changes(u32 checksum)
328 {
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;
335 }
336
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)
340 {
341         char country_env[9 + 2] = "COUNTRY=";
342         char *envp[] = {
343                 country_env,
344                 NULL
345         };
346
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");
353
354         country_env[8] = alpha2[0];
355         country_env[9] = alpha2[1];
356
357         return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
358 }
359
360 /* Used by nl80211 before kmalloc'ing our regulatory domain */
361 bool reg_is_valid_request(const char *alpha2)
362 {
363         if (!last_request)
364                 return false;
365
366         return alpha2_equal(last_request->alpha2, alpha2);
367 }
368
369 /* Sanity check on a regulatory rule */
370 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
371 {
372         const struct ieee80211_freq_range *freq_range = &rule->freq_range;
373         u32 freq_diff;
374
375         if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
376                 return false;
377
378         if (freq_range->start_freq_khz > freq_range->end_freq_khz)
379                 return false;
380
381         freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
382
383         if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
384                         freq_range->max_bandwidth_khz > freq_diff)
385                 return false;
386
387         return true;
388 }
389
390 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
391 {
392         const struct ieee80211_reg_rule *reg_rule = NULL;
393         unsigned int i;
394
395         if (!rd->n_reg_rules)
396                 return false;
397
398         if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
399                 return false;
400
401         for (i = 0; i < rd->n_reg_rules; i++) {
402                 reg_rule = &rd->reg_rules[i];
403                 if (!is_valid_reg_rule(reg_rule))
404                         return false;
405         }
406
407         return true;
408 }
409
410 /* Returns value in KHz */
411 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
412         u32 freq)
413 {
414         unsigned int i;
415         for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
416                 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
417                 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
418                 if (start_freq_khz >= freq_range->start_freq_khz &&
419                         end_freq_khz <= freq_range->end_freq_khz)
420                         return supported_bandwidths[i];
421         }
422         return 0;
423 }
424
425 /**
426  * freq_in_rule_band - tells us if a frequency is in a frequency band
427  * @freq_range: frequency rule we want to query
428  * @freq_khz: frequency we are inquiring about
429  *
430  * This lets us know if a specific frequency rule is or is not relevant to
431  * a specific frequency's band. Bands are device specific and artificial
432  * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
433  * safe for now to assume that a frequency rule should not be part of a
434  * frequency's band if the start freq or end freq are off by more than 2 GHz.
435  * This resolution can be lowered and should be considered as we add
436  * regulatory rule support for other "bands".
437  **/
438 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
439         u32 freq_khz)
440 {
441 #define ONE_GHZ_IN_KHZ  1000000
442         if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
443                 return true;
444         if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
445                 return true;
446         return false;
447 #undef ONE_GHZ_IN_KHZ
448 }
449
450 /* Converts a country IE to a regulatory domain. A regulatory domain
451  * structure has a lot of information which the IE doesn't yet have,
452  * so for the other values we use upper max values as we will intersect
453  * with our userspace regulatory agent to get lower bounds. */
454 static struct ieee80211_regdomain *country_ie_2_rd(
455                                 u8 *country_ie,
456                                 u8 country_ie_len,
457                                 u32 *checksum)
458 {
459         struct ieee80211_regdomain *rd = NULL;
460         unsigned int i = 0;
461         char alpha2[2];
462         u32 flags = 0;
463         u32 num_rules = 0, size_of_regd = 0;
464         u8 *triplets_start = NULL;
465         u8 len_at_triplet = 0;
466         /* the last channel we have registered in a subband (triplet) */
467         int last_sub_max_channel = 0;
468
469         *checksum = 0xDEADBEEF;
470
471         /* Country IE requirements */
472         BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
473                 country_ie_len & 0x01);
474
475         alpha2[0] = country_ie[0];
476         alpha2[1] = country_ie[1];
477
478         /*
479          * Third octet can be:
480          *    'I' - Indoor
481          *    'O' - Outdoor
482          *
483          *  anything else we assume is no restrictions
484          */
485         if (country_ie[2] == 'I')
486                 flags = NL80211_RRF_NO_OUTDOOR;
487         else if (country_ie[2] == 'O')
488                 flags = NL80211_RRF_NO_INDOOR;
489
490         country_ie += 3;
491         country_ie_len -= 3;
492
493         triplets_start = country_ie;
494         len_at_triplet = country_ie_len;
495
496         *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
497
498         /* We need to build a reg rule for each triplet, but first we must
499          * calculate the number of reg rules we will need. We will need one
500          * for each channel subband */
501         while (country_ie_len >= 3) {
502                 int end_channel = 0;
503                 struct ieee80211_country_ie_triplet *triplet =
504                         (struct ieee80211_country_ie_triplet *) country_ie;
505                 int cur_sub_max_channel = 0, cur_channel = 0;
506
507                 if (triplet->ext.reg_extension_id >=
508                                 IEEE80211_COUNTRY_EXTENSION_ID) {
509                         country_ie += 3;
510                         country_ie_len -= 3;
511                         continue;
512                 }
513
514                 /* 2 GHz */
515                 if (triplet->chans.first_channel <= 14)
516                         end_channel = triplet->chans.first_channel +
517                                 triplet->chans.num_channels;
518                 else
519                         /*
520                          * 5 GHz -- For example in country IEs if the first
521                          * channel given is 36 and the number of channels is 4
522                          * then the individual channel numbers defined for the
523                          * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
524                          * and not 36, 37, 38, 39.
525                          *
526                          * See: http://tinyurl.com/11d-clarification
527                          */
528                         end_channel =  triplet->chans.first_channel +
529                                 (4 * (triplet->chans.num_channels - 1));
530
531                 cur_channel = triplet->chans.first_channel;
532                 cur_sub_max_channel = end_channel;
533
534                 /* Basic sanity check */
535                 if (cur_sub_max_channel < cur_channel)
536                         return NULL;
537
538                 /* Do not allow overlapping channels. Also channels
539                  * passed in each subband must be monotonically
540                  * increasing */
541                 if (last_sub_max_channel) {
542                         if (cur_channel <= last_sub_max_channel)
543                                 return NULL;
544                         if (cur_sub_max_channel <= last_sub_max_channel)
545                                 return NULL;
546                 }
547
548                 /* When dot11RegulatoryClassesRequired is supported
549                  * we can throw ext triplets as part of this soup,
550                  * for now we don't care when those change as we
551                  * don't support them */
552                 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
553                   ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
554                   ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
555
556                 last_sub_max_channel = cur_sub_max_channel;
557
558                 country_ie += 3;
559                 country_ie_len -= 3;
560                 num_rules++;
561
562                 /* Note: this is not a IEEE requirement but
563                  * simply a memory requirement */
564                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
565                         return NULL;
566         }
567
568         country_ie = triplets_start;
569         country_ie_len = len_at_triplet;
570
571         size_of_regd = sizeof(struct ieee80211_regdomain) +
572                 (num_rules * sizeof(struct ieee80211_reg_rule));
573
574         rd = kzalloc(size_of_regd, GFP_KERNEL);
575         if (!rd)
576                 return NULL;
577
578         rd->n_reg_rules = num_rules;
579         rd->alpha2[0] = alpha2[0];
580         rd->alpha2[1] = alpha2[1];
581
582         /* This time around we fill in the rd */
583         while (country_ie_len >= 3) {
584                 int end_channel = 0;
585                 struct ieee80211_country_ie_triplet *triplet =
586                         (struct ieee80211_country_ie_triplet *) country_ie;
587                 struct ieee80211_reg_rule *reg_rule = NULL;
588                 struct ieee80211_freq_range *freq_range = NULL;
589                 struct ieee80211_power_rule *power_rule = NULL;
590
591                 /* Must parse if dot11RegulatoryClassesRequired is true,
592                  * we don't support this yet */
593                 if (triplet->ext.reg_extension_id >=
594                                 IEEE80211_COUNTRY_EXTENSION_ID) {
595                         country_ie += 3;
596                         country_ie_len -= 3;
597                         continue;
598                 }
599
600                 reg_rule = &rd->reg_rules[i];
601                 freq_range = &reg_rule->freq_range;
602                 power_rule = &reg_rule->power_rule;
603
604                 reg_rule->flags = flags;
605
606                 /* 2 GHz */
607                 if (triplet->chans.first_channel <= 14)
608                         end_channel = triplet->chans.first_channel +
609                                 triplet->chans.num_channels;
610                 else
611                         end_channel =  triplet->chans.first_channel +
612                                 (4 * (triplet->chans.num_channels - 1));
613
614                 /* The +10 is since the regulatory domain expects
615                  * the actual band edge, not the center of freq for
616                  * its start and end freqs, assuming 20 MHz bandwidth on
617                  * the channels passed */
618                 freq_range->start_freq_khz =
619                         MHZ_TO_KHZ(ieee80211_channel_to_frequency(
620                                 triplet->chans.first_channel) - 10);
621                 freq_range->end_freq_khz =
622                         MHZ_TO_KHZ(ieee80211_channel_to_frequency(
623                                 end_channel) + 10);
624
625                 /* Large arbitrary values, we intersect later */
626                 /* Increment this if we ever support >= 40 MHz channels
627                  * in IEEE 802.11 */
628                 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
629                 power_rule->max_antenna_gain = DBI_TO_MBI(100);
630                 power_rule->max_eirp = DBM_TO_MBM(100);
631
632                 country_ie += 3;
633                 country_ie_len -= 3;
634                 i++;
635
636                 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
637         }
638
639         return rd;
640 }
641
642
643 /* Helper for regdom_intersect(), this does the real
644  * mathematical intersection fun */
645 static int reg_rules_intersect(
646         const struct ieee80211_reg_rule *rule1,
647         const struct ieee80211_reg_rule *rule2,
648         struct ieee80211_reg_rule *intersected_rule)
649 {
650         const struct ieee80211_freq_range *freq_range1, *freq_range2;
651         struct ieee80211_freq_range *freq_range;
652         const struct ieee80211_power_rule *power_rule1, *power_rule2;
653         struct ieee80211_power_rule *power_rule;
654         u32 freq_diff;
655
656         freq_range1 = &rule1->freq_range;
657         freq_range2 = &rule2->freq_range;
658         freq_range = &intersected_rule->freq_range;
659
660         power_rule1 = &rule1->power_rule;
661         power_rule2 = &rule2->power_rule;
662         power_rule = &intersected_rule->power_rule;
663
664         freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
665                 freq_range2->start_freq_khz);
666         freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
667                 freq_range2->end_freq_khz);
668         freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
669                 freq_range2->max_bandwidth_khz);
670
671         freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
672         if (freq_range->max_bandwidth_khz > freq_diff)
673                 freq_range->max_bandwidth_khz = freq_diff;
674
675         power_rule->max_eirp = min(power_rule1->max_eirp,
676                 power_rule2->max_eirp);
677         power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
678                 power_rule2->max_antenna_gain);
679
680         intersected_rule->flags = (rule1->flags | rule2->flags);
681
682         if (!is_valid_reg_rule(intersected_rule))
683                 return -EINVAL;
684
685         return 0;
686 }
687
688 /**
689  * regdom_intersect - do the intersection between two regulatory domains
690  * @rd1: first regulatory domain
691  * @rd2: second regulatory domain
692  *
693  * Use this function to get the intersection between two regulatory domains.
694  * Once completed we will mark the alpha2 for the rd as intersected, "98",
695  * as no one single alpha2 can represent this regulatory domain.
696  *
697  * Returns a pointer to the regulatory domain structure which will hold the
698  * resulting intersection of rules between rd1 and rd2. We will
699  * kzalloc() this structure for you.
700  */
701 static struct ieee80211_regdomain *regdom_intersect(
702         const struct ieee80211_regdomain *rd1,
703         const struct ieee80211_regdomain *rd2)
704 {
705         int r, size_of_regd;
706         unsigned int x, y;
707         unsigned int num_rules = 0, rule_idx = 0;
708         const struct ieee80211_reg_rule *rule1, *rule2;
709         struct ieee80211_reg_rule *intersected_rule;
710         struct ieee80211_regdomain *rd;
711         /* This is just a dummy holder to help us count */
712         struct ieee80211_reg_rule irule;
713
714         /* Uses the stack temporarily for counter arithmetic */
715         intersected_rule = &irule;
716
717         memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
718
719         if (!rd1 || !rd2)
720                 return NULL;
721
722         /* First we get a count of the rules we'll need, then we actually
723          * build them. This is to so we can malloc() and free() a
724          * regdomain once. The reason we use reg_rules_intersect() here
725          * is it will return -EINVAL if the rule computed makes no sense.
726          * All rules that do check out OK are valid. */
727
728         for (x = 0; x < rd1->n_reg_rules; x++) {
729                 rule1 = &rd1->reg_rules[x];
730                 for (y = 0; y < rd2->n_reg_rules; y++) {
731                         rule2 = &rd2->reg_rules[y];
732                         if (!reg_rules_intersect(rule1, rule2,
733                                         intersected_rule))
734                                 num_rules++;
735                         memset(intersected_rule, 0,
736                                         sizeof(struct ieee80211_reg_rule));
737                 }
738         }
739
740         if (!num_rules)
741                 return NULL;
742
743         size_of_regd = sizeof(struct ieee80211_regdomain) +
744                 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
745
746         rd = kzalloc(size_of_regd, GFP_KERNEL);
747         if (!rd)
748                 return NULL;
749
750         for (x = 0; x < rd1->n_reg_rules; x++) {
751                 rule1 = &rd1->reg_rules[x];
752                 for (y = 0; y < rd2->n_reg_rules; y++) {
753                         rule2 = &rd2->reg_rules[y];
754                         /* This time around instead of using the stack lets
755                          * write to the target rule directly saving ourselves
756                          * a memcpy() */
757                         intersected_rule = &rd->reg_rules[rule_idx];
758                         r = reg_rules_intersect(rule1, rule2,
759                                 intersected_rule);
760                         /* No need to memset here the intersected rule here as
761                          * we're not using the stack anymore */
762                         if (r)
763                                 continue;
764                         rule_idx++;
765                 }
766         }
767
768         if (rule_idx != num_rules) {
769                 kfree(rd);
770                 return NULL;
771         }
772
773         rd->n_reg_rules = num_rules;
774         rd->alpha2[0] = '9';
775         rd->alpha2[1] = '8';
776
777         return rd;
778 }
779
780 /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
781  * want to just have the channel structure use these */
782 static u32 map_regdom_flags(u32 rd_flags)
783 {
784         u32 channel_flags = 0;
785         if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
786                 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
787         if (rd_flags & NL80211_RRF_NO_IBSS)
788                 channel_flags |= IEEE80211_CHAN_NO_IBSS;
789         if (rd_flags & NL80211_RRF_DFS)
790                 channel_flags |= IEEE80211_CHAN_RADAR;
791         return channel_flags;
792 }
793
794 /**
795  * freq_reg_info - get regulatory information for the given frequency
796  * @center_freq: Frequency in KHz for which we want regulatory information for
797  * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
798  *      you can set this to 0. If this frequency is allowed we then set
799  *      this value to the maximum allowed bandwidth.
800  * @reg_rule: the regulatory rule which we have for this frequency
801  *
802  * Use this function to get the regulatory rule for a specific frequency on
803  * a given wireless device. If the device has a specific regulatory domain
804  * it wants to follow we respect that unless a country IE has been received
805  * and processed already.
806  *
807  * Returns 0 if it was able to find a valid regulatory rule which does
808  * apply to the given center_freq otherwise it returns non-zero. It will
809  * also return -ERANGE if we determine the given center_freq does not even have
810  * a regulatory rule for a frequency range in the center_freq's band. See
811  * freq_in_rule_band() for our current definition of a band -- this is purely
812  * subjective and right now its 802.11 specific.
813  */
814 static int freq_reg_info(u32 center_freq, u32 *bandwidth,
815                          const struct ieee80211_reg_rule **reg_rule)
816 {
817         int i;
818         bool band_rule_found = false;
819         u32 max_bandwidth = 0;
820
821         if (!cfg80211_regdomain)
822                 return -EINVAL;
823
824         for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
825                 const struct ieee80211_reg_rule *rr;
826                 const struct ieee80211_freq_range *fr = NULL;
827                 const struct ieee80211_power_rule *pr = NULL;
828
829                 rr = &cfg80211_regdomain->reg_rules[i];
830                 fr = &rr->freq_range;
831                 pr = &rr->power_rule;
832
833                 /* We only need to know if one frequency rule was
834                  * was in center_freq's band, that's enough, so lets
835                  * not overwrite it once found */
836                 if (!band_rule_found)
837                         band_rule_found = freq_in_rule_band(fr, center_freq);
838
839                 max_bandwidth = freq_max_bandwidth(fr, center_freq);
840
841                 if (max_bandwidth && *bandwidth <= max_bandwidth) {
842                         *reg_rule = rr;
843                         *bandwidth = max_bandwidth;
844                         break;
845                 }
846         }
847
848         if (!band_rule_found)
849                 return -ERANGE;
850
851         return !max_bandwidth;
852 }
853
854 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
855                            unsigned int chan_idx)
856 {
857         int r;
858         u32 flags;
859         u32 max_bandwidth = 0;
860         const struct ieee80211_reg_rule *reg_rule = NULL;
861         const struct ieee80211_power_rule *power_rule = NULL;
862         struct ieee80211_supported_band *sband;
863         struct ieee80211_channel *chan;
864
865         sband = wiphy->bands[band];
866         BUG_ON(chan_idx >= sband->n_channels);
867         chan = &sband->channels[chan_idx];
868
869         flags = chan->orig_flags;
870
871         r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
872                 &max_bandwidth, &reg_rule);
873
874         if (r) {
875                 /* This means no regulatory rule was found in the country IE
876                  * with a frequency range on the center_freq's band, since
877                  * IEEE-802.11 allows for a country IE to have a subset of the
878                  * regulatory information provided in a country we ignore
879                  * disabling the channel unless at least one reg rule was
880                  * found on the center_freq's band. For details see this
881                  * clarification:
882                  *
883                  * http://tinyurl.com/11d-clarification
884                  */
885                 if (r == -ERANGE &&
886                     last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
887 #ifdef CONFIG_CFG80211_REG_DEBUG
888                         printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
889                                 "intact on %s - no rule found in band on "
890                                 "Country IE\n",
891                                 chan->center_freq, wiphy_name(wiphy));
892 #endif
893                 } else {
894                 /* In this case we know the country IE has at least one reg rule
895                  * for the band so we respect its band definitions */
896 #ifdef CONFIG_CFG80211_REG_DEBUG
897                         if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
898                                 printk(KERN_DEBUG "cfg80211: Disabling "
899                                         "channel %d MHz on %s due to "
900                                         "Country IE\n",
901                                         chan->center_freq, wiphy_name(wiphy));
902 #endif
903                         flags |= IEEE80211_CHAN_DISABLED;
904                         chan->flags = flags;
905                 }
906                 return;
907         }
908
909         power_rule = &reg_rule->power_rule;
910
911         chan->flags = flags | map_regdom_flags(reg_rule->flags);
912         chan->max_antenna_gain = min(chan->orig_mag,
913                 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
914         chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
915         if (chan->orig_mpwr)
916                 chan->max_power = min(chan->orig_mpwr,
917                         (int) MBM_TO_DBM(power_rule->max_eirp));
918         else
919                 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
920 }
921
922 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
923 {
924         unsigned int i;
925         struct ieee80211_supported_band *sband;
926
927         BUG_ON(!wiphy->bands[band]);
928         sband = wiphy->bands[band];
929
930         for (i = 0; i < sband->n_channels; i++)
931                 handle_channel(wiphy, band, i);
932 }
933
934 static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
935 {
936         if (!last_request)
937                 return true;
938         if (setby == REGDOM_SET_BY_CORE &&
939                   wiphy->fw_handles_regulatory)
940                 return true;
941         return false;
942 }
943
944 static void update_all_wiphy_regulatory(enum reg_set_by setby)
945 {
946         struct cfg80211_registered_device *drv;
947
948         list_for_each_entry(drv, &cfg80211_drv_list, list)
949                 if (!ignore_reg_update(&drv->wiphy, setby))
950                         wiphy_update_regulatory(&drv->wiphy, setby);
951 }
952
953 void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
954 {
955         enum ieee80211_band band;
956         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
957                 if (wiphy->bands[band])
958                         handle_band(wiphy, band);
959                 if (wiphy->reg_notifier)
960                         wiphy->reg_notifier(wiphy, setby);
961         }
962 }
963
964 /* Return value which can be used by ignore_request() to indicate
965  * it has been determined we should intersect two regulatory domains */
966 #define REG_INTERSECT   1
967
968 /* This has the logic which determines when a new request
969  * should be ignored. */
970 static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
971                           const char *alpha2)
972 {
973         /* All initial requests are respected */
974         if (!last_request)
975                 return 0;
976
977         switch (set_by) {
978         case REGDOM_SET_BY_INIT:
979                 return -EINVAL;
980         case REGDOM_SET_BY_CORE:
981                 /*
982                  * Always respect new wireless core hints, should only happen
983                  * when updating the world regulatory domain at init.
984                  */
985                 return 0;
986         case REGDOM_SET_BY_COUNTRY_IE:
987                 if (unlikely(!is_an_alpha2(alpha2)))
988                         return -EINVAL;
989                 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
990                         if (last_request->wiphy != wiphy) {
991                                 /*
992                                  * Two cards with two APs claiming different
993                                  * different Country IE alpha2s. We could
994                                  * intersect them, but that seems unlikely
995                                  * to be correct. Reject second one for now.
996                                  */
997                                 if (!alpha2_equal(alpha2,
998                                                   cfg80211_regdomain->alpha2))
999                                         return -EOPNOTSUPP;
1000                                 return -EALREADY;
1001                         }
1002                         /* Two consecutive Country IE hints on the same wiphy.
1003                          * This should be picked up early by the driver/stack */
1004                         if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
1005                                   alpha2)))
1006                                 return 0;
1007                         return -EALREADY;
1008                 }
1009                 return REG_INTERSECT;
1010         case REGDOM_SET_BY_DRIVER:
1011                 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
1012                         return -EALREADY;
1013                 return 0;
1014         case REGDOM_SET_BY_USER:
1015                 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
1016                         return REG_INTERSECT;
1017                 /* If the user knows better the user should set the regdom
1018                  * to their country before the IE is picked up */
1019                 if (last_request->initiator == REGDOM_SET_BY_USER &&
1020                           last_request->intersect)
1021                         return -EOPNOTSUPP;
1022                 return 0;
1023         }
1024
1025         return -EINVAL;
1026 }
1027
1028 /* Caller must hold &cfg80211_drv_mutex */
1029 int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
1030                         const char *alpha2,
1031                         u32 country_ie_checksum,
1032                         enum environment_cap env)
1033 {
1034         struct regulatory_request *request;
1035         bool intersect = false;
1036         int r = 0;
1037
1038         r = ignore_request(wiphy, set_by, alpha2);
1039
1040         if (r == REG_INTERSECT)
1041                 intersect = true;
1042         else if (r)
1043                 return r;
1044
1045         request = kzalloc(sizeof(struct regulatory_request),
1046                           GFP_KERNEL);
1047         if (!request)
1048                 return -ENOMEM;
1049
1050         request->alpha2[0] = alpha2[0];
1051         request->alpha2[1] = alpha2[1];
1052         request->initiator = set_by;
1053         request->wiphy = wiphy;
1054         request->intersect = intersect;
1055         request->country_ie_checksum = country_ie_checksum;
1056         request->country_ie_env = env;
1057
1058         kfree(last_request);
1059         last_request = request;
1060         /*
1061          * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1062          * AND if CRDA is NOT present nothing will happen, if someone
1063          * wants to bother with 11d with OLD_REG you can add a timer.
1064          * If after x amount of time nothing happens you can call:
1065          *
1066          * return set_regdom(country_ie_regdomain);
1067          *
1068          * to intersect with the static rd
1069          */
1070         return call_crda(alpha2);
1071 }
1072
1073 void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1074 {
1075         BUG_ON(!alpha2);
1076
1077         mutex_lock(&cfg80211_drv_mutex);
1078         __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY);
1079         mutex_unlock(&cfg80211_drv_mutex);
1080 }
1081 EXPORT_SYMBOL(regulatory_hint);
1082
1083 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1084                         u32 country_ie_checksum)
1085 {
1086         if (!last_request->wiphy)
1087                 return false;
1088         if (likely(last_request->wiphy != wiphy))
1089                 return !country_ie_integrity_changes(country_ie_checksum);
1090         /* We should not have let these through at this point, they
1091          * should have been picked up earlier by the first alpha2 check
1092          * on the device */
1093         if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1094                 return true;
1095         return false;
1096 }
1097
1098 void regulatory_hint_11d(struct wiphy *wiphy,
1099                         u8 *country_ie,
1100                         u8 country_ie_len)
1101 {
1102         struct ieee80211_regdomain *rd = NULL;
1103         char alpha2[2];
1104         u32 checksum = 0;
1105         enum environment_cap env = ENVIRON_ANY;
1106
1107         if (!last_request)
1108                 return;
1109
1110         mutex_lock(&cfg80211_drv_mutex);
1111
1112         /* IE len must be evenly divisible by 2 */
1113         if (country_ie_len & 0x01)
1114                 goto out;
1115
1116         if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1117                 goto out;
1118
1119         /* Pending country IE processing, this can happen after we
1120          * call CRDA and wait for a response if a beacon was received before
1121          * we were able to process the last regulatory_hint_11d() call */
1122         if (country_ie_regdomain)
1123                 goto out;
1124
1125         alpha2[0] = country_ie[0];
1126         alpha2[1] = country_ie[1];
1127
1128         if (country_ie[2] == 'I')
1129                 env = ENVIRON_INDOOR;
1130         else if (country_ie[2] == 'O')
1131                 env = ENVIRON_OUTDOOR;
1132
1133         /* We will run this for *every* beacon processed for the BSSID, so
1134          * we optimize an early check to exit out early if we don't have to
1135          * do anything */
1136         if (likely(last_request->wiphy)) {
1137                 struct cfg80211_registered_device *drv_last_ie;
1138
1139                 drv_last_ie = wiphy_to_dev(last_request->wiphy);
1140
1141                 /* Lets keep this simple -- we trust the first AP
1142                  * after we intersect with CRDA */
1143                 if (likely(last_request->wiphy == wiphy)) {
1144                         /* Ignore IEs coming in on this wiphy with
1145                          * the same alpha2 and environment cap */
1146                         if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1147                                   alpha2) &&
1148                                   env == drv_last_ie->env)) {
1149                                 goto out;
1150                         }
1151                         /* the wiphy moved on to another BSSID or the AP
1152                          * was reconfigured. XXX: We need to deal with the
1153                          * case where the user suspends and goes to goes
1154                          * to another country, and then gets IEs from an
1155                          * AP with different settings */
1156                         goto out;
1157                 } else {
1158                         /* Ignore IEs coming in on two separate wiphys with
1159                          * the same alpha2 and environment cap */
1160                         if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1161                                   alpha2) &&
1162                                   env == drv_last_ie->env)) {
1163                                 goto out;
1164                         }
1165                         /* We could potentially intersect though */
1166                         goto out;
1167                 }
1168         }
1169
1170         rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1171         if (!rd)
1172                 goto out;
1173
1174         /* This will not happen right now but we leave it here for the
1175          * the future when we want to add suspend/resume support and having
1176          * the user move to another country after doing so, or having the user
1177          * move to another AP. Right now we just trust the first AP. This is why
1178          * this is marked as likley(). If we hit this before we add this support
1179          * we want to be informed of it as it would indicate a mistake in the
1180          * current design  */
1181         if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
1182                 goto out;
1183
1184         /* We keep this around for when CRDA comes back with a response so
1185          * we can intersect with that */
1186         country_ie_regdomain = rd;
1187
1188         __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
1189                 country_ie_regdomain->alpha2, checksum, env);
1190
1191 out:
1192         mutex_unlock(&cfg80211_drv_mutex);
1193 }
1194 EXPORT_SYMBOL(regulatory_hint_11d);
1195
1196 static void print_rd_rules(const struct ieee80211_regdomain *rd)
1197 {
1198         unsigned int i;
1199         const struct ieee80211_reg_rule *reg_rule = NULL;
1200         const struct ieee80211_freq_range *freq_range = NULL;
1201         const struct ieee80211_power_rule *power_rule = NULL;
1202
1203         printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1204                 "(max_antenna_gain, max_eirp)\n");
1205
1206         for (i = 0; i < rd->n_reg_rules; i++) {
1207                 reg_rule = &rd->reg_rules[i];
1208                 freq_range = &reg_rule->freq_range;
1209                 power_rule = &reg_rule->power_rule;
1210
1211                 /* There may not be documentation for max antenna gain
1212                  * in certain regions */
1213                 if (power_rule->max_antenna_gain)
1214                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1215                                 "(%d mBi, %d mBm)\n",
1216                                 freq_range->start_freq_khz,
1217                                 freq_range->end_freq_khz,
1218                                 freq_range->max_bandwidth_khz,
1219                                 power_rule->max_antenna_gain,
1220                                 power_rule->max_eirp);
1221                 else
1222                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1223                                 "(N/A, %d mBm)\n",
1224                                 freq_range->start_freq_khz,
1225                                 freq_range->end_freq_khz,
1226                                 freq_range->max_bandwidth_khz,
1227                                 power_rule->max_eirp);
1228         }
1229 }
1230
1231 static void print_regdomain(const struct ieee80211_regdomain *rd)
1232 {
1233
1234         if (is_intersected_alpha2(rd->alpha2)) {
1235                 struct wiphy *wiphy = NULL;
1236                 struct cfg80211_registered_device *drv;
1237
1238                 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1239                         if (last_request->wiphy) {
1240                                 wiphy = last_request->wiphy;
1241                                 drv = wiphy_to_dev(wiphy);
1242                                 printk(KERN_INFO "cfg80211: Current regulatory "
1243                                         "domain updated by AP to: %c%c\n",
1244                                         drv->country_ie_alpha2[0],
1245                                         drv->country_ie_alpha2[1]);
1246                         } else
1247                                 printk(KERN_INFO "cfg80211: Current regulatory "
1248                                         "domain intersected: \n");
1249                 } else
1250                                 printk(KERN_INFO "cfg80211: Current regulatory "
1251                                         "intersected: \n");
1252         } else if (is_world_regdom(rd->alpha2))
1253                 printk(KERN_INFO "cfg80211: World regulatory "
1254                         "domain updated:\n");
1255         else {
1256                 if (is_unknown_alpha2(rd->alpha2))
1257                         printk(KERN_INFO "cfg80211: Regulatory domain "
1258                                 "changed to driver built-in settings "
1259                                 "(unknown country)\n");
1260                 else
1261                         printk(KERN_INFO "cfg80211: Regulatory domain "
1262                                 "changed to country: %c%c\n",
1263                                 rd->alpha2[0], rd->alpha2[1]);
1264         }
1265         print_rd_rules(rd);
1266 }
1267
1268 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
1269 {
1270         printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1271                 rd->alpha2[0], rd->alpha2[1]);
1272         print_rd_rules(rd);
1273 }
1274
1275 #ifdef CONFIG_CFG80211_REG_DEBUG
1276 static void reg_country_ie_process_debug(
1277         const struct ieee80211_regdomain *rd,
1278         const struct ieee80211_regdomain *country_ie_regdomain,
1279         const struct ieee80211_regdomain *intersected_rd)
1280 {
1281         printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1282         print_regdomain_info(country_ie_regdomain);
1283         printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1284         print_regdomain_info(rd);
1285         if (intersected_rd) {
1286                 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1287                         "and get:\n");
1288                 print_regdomain_info(intersected_rd);
1289                 return;
1290         }
1291         printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1292 }
1293 #else
1294 static inline void reg_country_ie_process_debug(
1295         const struct ieee80211_regdomain *rd,
1296         const struct ieee80211_regdomain *country_ie_regdomain,
1297         const struct ieee80211_regdomain *intersected_rd)
1298 {
1299 }
1300 #endif
1301
1302 /* Takes ownership of rd only if it doesn't fail */
1303 static int __set_regdom(const struct ieee80211_regdomain *rd)
1304 {
1305         const struct ieee80211_regdomain *intersected_rd = NULL;
1306         struct cfg80211_registered_device *drv = NULL;
1307         struct wiphy *wiphy = NULL;
1308         /* Some basic sanity checks first */
1309
1310         if (is_world_regdom(rd->alpha2)) {
1311                 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1312                         return -EINVAL;
1313                 update_world_regdomain(rd);
1314                 return 0;
1315         }
1316
1317         if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1318                         !is_unknown_alpha2(rd->alpha2))
1319                 return -EINVAL;
1320
1321         if (!last_request)
1322                 return -EINVAL;
1323
1324         /* Lets only bother proceeding on the same alpha2 if the current
1325          * rd is non static (it means CRDA was present and was used last)
1326          * and the pending request came in from a country IE */
1327         if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1328                 /* If someone else asked us to change the rd lets only bother
1329                  * checking if the alpha2 changes if CRDA was already called */
1330                 if (!is_old_static_regdom(cfg80211_regdomain) &&
1331                     !regdom_changed(rd->alpha2))
1332                         return -EINVAL;
1333         }
1334
1335         wiphy = last_request->wiphy;
1336
1337         /* Now lets set the regulatory domain, update all driver channels
1338          * and finally inform them of what we have done, in case they want
1339          * to review or adjust their own settings based on their own
1340          * internal EEPROM data */
1341
1342         if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1343                 return -EINVAL;
1344
1345         if (!is_valid_rd(rd)) {
1346                 printk(KERN_ERR "cfg80211: Invalid "
1347                         "regulatory domain detected:\n");
1348                 print_regdomain_info(rd);
1349                 return -EINVAL;
1350         }
1351
1352         if (!last_request->intersect) {
1353                 reset_regdomains();
1354                 cfg80211_regdomain = rd;
1355                 return 0;
1356         }
1357
1358         /* Intersection requires a bit more work */
1359
1360         if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1361
1362                 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1363                 if (!intersected_rd)
1364                         return -EINVAL;
1365
1366                 /* We can trash what CRDA provided now */
1367                 kfree(rd);
1368                 rd = NULL;
1369
1370                 reset_regdomains();
1371                 cfg80211_regdomain = intersected_rd;
1372
1373                 return 0;
1374         }
1375
1376         /*
1377          * Country IE requests are handled a bit differently, we intersect
1378          * the country IE rd with what CRDA believes that country should have
1379          */
1380
1381         BUG_ON(!country_ie_regdomain);
1382
1383         if (rd != country_ie_regdomain) {
1384                 /* Intersect what CRDA returned and our what we
1385                  * had built from the Country IE received */
1386
1387                 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1388
1389                 reg_country_ie_process_debug(rd, country_ie_regdomain,
1390                         intersected_rd);
1391
1392                 kfree(country_ie_regdomain);
1393                 country_ie_regdomain = NULL;
1394         } else {
1395                 /* This would happen when CRDA was not present and
1396                  * OLD_REGULATORY was enabled. We intersect our Country
1397                  * IE rd and what was set on cfg80211 originally */
1398                 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1399         }
1400
1401         if (!intersected_rd)
1402                 return -EINVAL;
1403
1404         drv = wiphy_to_dev(wiphy);
1405
1406         drv->country_ie_alpha2[0] = rd->alpha2[0];
1407         drv->country_ie_alpha2[1] = rd->alpha2[1];
1408         drv->env = last_request->country_ie_env;
1409
1410         BUG_ON(intersected_rd == rd);
1411
1412         kfree(rd);
1413         rd = NULL;
1414
1415         reset_regdomains();
1416         cfg80211_regdomain = intersected_rd;
1417
1418         return 0;
1419 }
1420
1421
1422 /* Use this call to set the current regulatory domain. Conflicts with
1423  * multiple drivers can be ironed out later. Caller must've already
1424  * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
1425 int set_regdom(const struct ieee80211_regdomain *rd)
1426 {
1427         int r;
1428
1429         /* Note that this doesn't update the wiphys, this is done below */
1430         r = __set_regdom(rd);
1431         if (r) {
1432                 kfree(rd);
1433                 return r;
1434         }
1435
1436         /* This would make this whole thing pointless */
1437         if (!last_request->intersect)
1438                 BUG_ON(rd != cfg80211_regdomain);
1439
1440         /* update all wiphys now with the new established regulatory domain */
1441         update_all_wiphy_regulatory(last_request->initiator);
1442
1443         print_regdomain(cfg80211_regdomain);
1444
1445         return r;
1446 }
1447
1448 /* Caller must hold cfg80211_drv_mutex */
1449 void reg_device_remove(struct wiphy *wiphy)
1450 {
1451         if (!last_request || !last_request->wiphy)
1452                 return;
1453         if (last_request->wiphy != wiphy)
1454                 return;
1455         last_request->wiphy = NULL;
1456         last_request->country_ie_env = ENVIRON_ANY;
1457 }
1458
1459 int regulatory_init(void)
1460 {
1461         int err;
1462
1463         reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1464         if (IS_ERR(reg_pdev))
1465                 return PTR_ERR(reg_pdev);
1466
1467 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
1468         cfg80211_regdomain = static_regdom(ieee80211_regdom);
1469
1470         printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
1471         print_regdomain_info(cfg80211_regdomain);
1472         /* The old code still requests for a new regdomain and if
1473          * you have CRDA you get it updated, otherwise you get
1474          * stuck with the static values. We ignore "EU" code as
1475          * that is not a valid ISO / IEC 3166 alpha2 */
1476         if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
1477                 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
1478                                         ieee80211_regdom, 0, ENVIRON_ANY);
1479 #else
1480         cfg80211_regdomain = cfg80211_world_regdom;
1481
1482         err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
1483         if (err)
1484                 printk(KERN_ERR "cfg80211: calling CRDA failed - "
1485                        "unable to update world regulatory domain, "
1486                        "using static definition\n");
1487 #endif
1488
1489         return 0;
1490 }
1491
1492 void regulatory_exit(void)
1493 {
1494         mutex_lock(&cfg80211_drv_mutex);
1495
1496         reset_regdomains();
1497
1498         kfree(country_ie_regdomain);
1499         country_ie_regdomain = NULL;
1500
1501         kfree(last_request);
1502
1503         platform_device_unregister(reg_pdev);
1504
1505         mutex_unlock(&cfg80211_drv_mutex);
1506 }