cfg80211: send regulatory beacon hint events to userspace
[linux-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 #include "nl80211.h"
45
46 /* Receipt of information from last regulatory request */
47 static struct regulatory_request *last_request;
48
49 /* To trigger userspace events */
50 static struct platform_device *reg_pdev;
51
52 /* Keep the ordering from large to small */
53 static u32 supported_bandwidths[] = {
54         MHZ_TO_KHZ(40),
55         MHZ_TO_KHZ(20),
56 };
57
58 /*
59  * Central wireless core regulatory domains, we only need two,
60  * the current one and a world regulatory domain in case we have no
61  * information to give us an alpha2
62  */
63 const struct ieee80211_regdomain *cfg80211_regdomain;
64
65 /*
66  * We use this as a place for the rd structure built from the
67  * last parsed country IE to rest until CRDA gets back to us with
68  * what it thinks should apply for the same country
69  */
70 static const struct ieee80211_regdomain *country_ie_regdomain;
71
72 /* Used to queue up regulatory hints */
73 static LIST_HEAD(reg_requests_list);
74 static spinlock_t reg_requests_lock;
75
76 /* Used to queue up beacon hints for review */
77 static LIST_HEAD(reg_pending_beacons);
78 static spinlock_t reg_pending_beacons_lock;
79
80 /* Used to keep track of processed beacon hints */
81 static LIST_HEAD(reg_beacon_list);
82
83 struct reg_beacon {
84         struct list_head list;
85         struct ieee80211_channel chan;
86 };
87
88 /* We keep a static world regulatory domain in case of the absence of CRDA */
89 static const struct ieee80211_regdomain world_regdom = {
90         .n_reg_rules = 5,
91         .alpha2 =  "00",
92         .reg_rules = {
93                 /* IEEE 802.11b/g, channels 1..11 */
94                 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
95                 /* IEEE 802.11b/g, channels 12..13. No HT40
96                  * channel fits here. */
97                 REG_RULE(2467-10, 2472+10, 20, 6, 20,
98                         NL80211_RRF_PASSIVE_SCAN |
99                         NL80211_RRF_NO_IBSS),
100                 /* IEEE 802.11 channel 14 - Only JP enables
101                  * this and for 802.11b only */
102                 REG_RULE(2484-10, 2484+10, 20, 6, 20,
103                         NL80211_RRF_PASSIVE_SCAN |
104                         NL80211_RRF_NO_IBSS |
105                         NL80211_RRF_NO_OFDM),
106                 /* IEEE 802.11a, channel 36..48 */
107                 REG_RULE(5180-10, 5240+10, 40, 6, 20,
108                         NL80211_RRF_PASSIVE_SCAN |
109                         NL80211_RRF_NO_IBSS),
110
111                 /* NB: 5260 MHz - 5700 MHz requies DFS */
112
113                 /* IEEE 802.11a, channel 149..165 */
114                 REG_RULE(5745-10, 5825+10, 40, 6, 20,
115                         NL80211_RRF_PASSIVE_SCAN |
116                         NL80211_RRF_NO_IBSS),
117         }
118 };
119
120 static const struct ieee80211_regdomain *cfg80211_world_regdom =
121         &world_regdom;
122
123 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
124 static char *ieee80211_regdom = "US";
125 #else
126 static char *ieee80211_regdom = "00";
127 #endif
128
129 module_param(ieee80211_regdom, charp, 0444);
130 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
131
132 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
133 /*
134  * We assume 40 MHz bandwidth for the old regulatory work.
135  * We make emphasis we are using the exact same frequencies
136  * as before
137  */
138
139 static const struct ieee80211_regdomain us_regdom = {
140         .n_reg_rules = 6,
141         .alpha2 =  "US",
142         .reg_rules = {
143                 /* IEEE 802.11b/g, channels 1..11 */
144                 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
145                 /* IEEE 802.11a, channel 36 */
146                 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
147                 /* IEEE 802.11a, channel 40 */
148                 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
149                 /* IEEE 802.11a, channel 44 */
150                 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
151                 /* IEEE 802.11a, channels 48..64 */
152                 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
153                 /* IEEE 802.11a, channels 149..165, outdoor */
154                 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
155         }
156 };
157
158 static const struct ieee80211_regdomain jp_regdom = {
159         .n_reg_rules = 3,
160         .alpha2 =  "JP",
161         .reg_rules = {
162                 /* IEEE 802.11b/g, channels 1..14 */
163                 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
164                 /* IEEE 802.11a, channels 34..48 */
165                 REG_RULE(5170-10, 5240+10, 40, 6, 20,
166                         NL80211_RRF_PASSIVE_SCAN),
167                 /* IEEE 802.11a, channels 52..64 */
168                 REG_RULE(5260-10, 5320+10, 40, 6, 20,
169                         NL80211_RRF_NO_IBSS |
170                         NL80211_RRF_DFS),
171         }
172 };
173
174 static const struct ieee80211_regdomain eu_regdom = {
175         .n_reg_rules = 6,
176         /*
177          * This alpha2 is bogus, we leave it here just for stupid
178          * backward compatibility
179          */
180         .alpha2 =  "EU",
181         .reg_rules = {
182                 /* IEEE 802.11b/g, channels 1..13 */
183                 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
184                 /* IEEE 802.11a, channel 36 */
185                 REG_RULE(5180-10, 5180+10, 40, 6, 23,
186                         NL80211_RRF_PASSIVE_SCAN),
187                 /* IEEE 802.11a, channel 40 */
188                 REG_RULE(5200-10, 5200+10, 40, 6, 23,
189                         NL80211_RRF_PASSIVE_SCAN),
190                 /* IEEE 802.11a, channel 44 */
191                 REG_RULE(5220-10, 5220+10, 40, 6, 23,
192                         NL80211_RRF_PASSIVE_SCAN),
193                 /* IEEE 802.11a, channels 48..64 */
194                 REG_RULE(5240-10, 5320+10, 40, 6, 20,
195                         NL80211_RRF_NO_IBSS |
196                         NL80211_RRF_DFS),
197                 /* IEEE 802.11a, channels 100..140 */
198                 REG_RULE(5500-10, 5700+10, 40, 6, 30,
199                         NL80211_RRF_NO_IBSS |
200                         NL80211_RRF_DFS),
201         }
202 };
203
204 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
205 {
206         if (alpha2[0] == 'U' && alpha2[1] == 'S')
207                 return &us_regdom;
208         if (alpha2[0] == 'J' && alpha2[1] == 'P')
209                 return &jp_regdom;
210         if (alpha2[0] == 'E' && alpha2[1] == 'U')
211                 return &eu_regdom;
212         /* Default, as per the old rules */
213         return &us_regdom;
214 }
215
216 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
217 {
218         if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
219                 return true;
220         return false;
221 }
222 #else
223 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
224 {
225         return false;
226 }
227 #endif
228
229 static void reset_regdomains(void)
230 {
231         /* avoid freeing static information or freeing something twice */
232         if (cfg80211_regdomain == cfg80211_world_regdom)
233                 cfg80211_regdomain = NULL;
234         if (cfg80211_world_regdom == &world_regdom)
235                 cfg80211_world_regdom = NULL;
236         if (cfg80211_regdomain == &world_regdom)
237                 cfg80211_regdomain = NULL;
238         if (is_old_static_regdom(cfg80211_regdomain))
239                 cfg80211_regdomain = NULL;
240
241         kfree(cfg80211_regdomain);
242         kfree(cfg80211_world_regdom);
243
244         cfg80211_world_regdom = &world_regdom;
245         cfg80211_regdomain = NULL;
246 }
247
248 /*
249  * Dynamic world regulatory domain requested by the wireless
250  * core upon initialization
251  */
252 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
253 {
254         BUG_ON(!last_request);
255
256         reset_regdomains();
257
258         cfg80211_world_regdom = rd;
259         cfg80211_regdomain = rd;
260 }
261
262 bool is_world_regdom(const char *alpha2)
263 {
264         if (!alpha2)
265                 return false;
266         if (alpha2[0] == '0' && alpha2[1] == '0')
267                 return true;
268         return false;
269 }
270
271 static bool is_alpha2_set(const char *alpha2)
272 {
273         if (!alpha2)
274                 return false;
275         if (alpha2[0] != 0 && alpha2[1] != 0)
276                 return true;
277         return false;
278 }
279
280 static bool is_alpha_upper(char letter)
281 {
282         /* ASCII A - Z */
283         if (letter >= 65 && letter <= 90)
284                 return true;
285         return false;
286 }
287
288 static bool is_unknown_alpha2(const char *alpha2)
289 {
290         if (!alpha2)
291                 return false;
292         /*
293          * Special case where regulatory domain was built by driver
294          * but a specific alpha2 cannot be determined
295          */
296         if (alpha2[0] == '9' && alpha2[1] == '9')
297                 return true;
298         return false;
299 }
300
301 static bool is_intersected_alpha2(const char *alpha2)
302 {
303         if (!alpha2)
304                 return false;
305         /*
306          * Special case where regulatory domain is the
307          * result of an intersection between two regulatory domain
308          * structures
309          */
310         if (alpha2[0] == '9' && alpha2[1] == '8')
311                 return true;
312         return false;
313 }
314
315 static bool is_an_alpha2(const char *alpha2)
316 {
317         if (!alpha2)
318                 return false;
319         if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
320                 return true;
321         return false;
322 }
323
324 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
325 {
326         if (!alpha2_x || !alpha2_y)
327                 return false;
328         if (alpha2_x[0] == alpha2_y[0] &&
329                 alpha2_x[1] == alpha2_y[1])
330                 return true;
331         return false;
332 }
333
334 static bool regdom_changes(const char *alpha2)
335 {
336         assert_cfg80211_lock();
337
338         if (!cfg80211_regdomain)
339                 return true;
340         if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
341                 return false;
342         return true;
343 }
344
345 /**
346  * country_ie_integrity_changes - tells us if the country IE has changed
347  * @checksum: checksum of country IE of fields we are interested in
348  *
349  * If the country IE has not changed you can ignore it safely. This is
350  * useful to determine if two devices are seeing two different country IEs
351  * even on the same alpha2. Note that this will return false if no IE has
352  * been set on the wireless core yet.
353  */
354 static bool country_ie_integrity_changes(u32 checksum)
355 {
356         /* If no IE has been set then the checksum doesn't change */
357         if (unlikely(!last_request->country_ie_checksum))
358                 return false;
359         if (unlikely(last_request->country_ie_checksum != checksum))
360                 return true;
361         return false;
362 }
363
364 /*
365  * This lets us keep regulatory code which is updated on a regulatory
366  * basis in userspace.
367  */
368 static int call_crda(const char *alpha2)
369 {
370         char country_env[9 + 2] = "COUNTRY=";
371         char *envp[] = {
372                 country_env,
373                 NULL
374         };
375
376         if (!is_world_regdom((char *) alpha2))
377                 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
378                         alpha2[0], alpha2[1]);
379         else
380                 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
381                         "regulatory domain\n");
382
383         country_env[8] = alpha2[0];
384         country_env[9] = alpha2[1];
385
386         return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
387 }
388
389 /* Used by nl80211 before kmalloc'ing our regulatory domain */
390 bool reg_is_valid_request(const char *alpha2)
391 {
392         if (!last_request)
393                 return false;
394
395         return alpha2_equal(last_request->alpha2, alpha2);
396 }
397
398 /* Sanity check on a regulatory rule */
399 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
400 {
401         const struct ieee80211_freq_range *freq_range = &rule->freq_range;
402         u32 freq_diff;
403
404         if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
405                 return false;
406
407         if (freq_range->start_freq_khz > freq_range->end_freq_khz)
408                 return false;
409
410         freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
411
412         if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
413                         freq_range->max_bandwidth_khz > freq_diff)
414                 return false;
415
416         return true;
417 }
418
419 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
420 {
421         const struct ieee80211_reg_rule *reg_rule = NULL;
422         unsigned int i;
423
424         if (!rd->n_reg_rules)
425                 return false;
426
427         if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
428                 return false;
429
430         for (i = 0; i < rd->n_reg_rules; i++) {
431                 reg_rule = &rd->reg_rules[i];
432                 if (!is_valid_reg_rule(reg_rule))
433                         return false;
434         }
435
436         return true;
437 }
438
439 /* Returns value in KHz */
440 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
441         u32 freq)
442 {
443         unsigned int i;
444         for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
445                 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
446                 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
447                 if (start_freq_khz >= freq_range->start_freq_khz &&
448                         end_freq_khz <= freq_range->end_freq_khz)
449                         return supported_bandwidths[i];
450         }
451         return 0;
452 }
453
454 /**
455  * freq_in_rule_band - tells us if a frequency is in a frequency band
456  * @freq_range: frequency rule we want to query
457  * @freq_khz: frequency we are inquiring about
458  *
459  * This lets us know if a specific frequency rule is or is not relevant to
460  * a specific frequency's band. Bands are device specific and artificial
461  * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
462  * safe for now to assume that a frequency rule should not be part of a
463  * frequency's band if the start freq or end freq are off by more than 2 GHz.
464  * This resolution can be lowered and should be considered as we add
465  * regulatory rule support for other "bands".
466  **/
467 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
468         u32 freq_khz)
469 {
470 #define ONE_GHZ_IN_KHZ  1000000
471         if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
472                 return true;
473         if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
474                 return true;
475         return false;
476 #undef ONE_GHZ_IN_KHZ
477 }
478
479 /*
480  * Converts a country IE to a regulatory domain. A regulatory domain
481  * structure has a lot of information which the IE doesn't yet have,
482  * so for the other values we use upper max values as we will intersect
483  * with our userspace regulatory agent to get lower bounds.
484  */
485 static struct ieee80211_regdomain *country_ie_2_rd(
486                                 u8 *country_ie,
487                                 u8 country_ie_len,
488                                 u32 *checksum)
489 {
490         struct ieee80211_regdomain *rd = NULL;
491         unsigned int i = 0;
492         char alpha2[2];
493         u32 flags = 0;
494         u32 num_rules = 0, size_of_regd = 0;
495         u8 *triplets_start = NULL;
496         u8 len_at_triplet = 0;
497         /* the last channel we have registered in a subband (triplet) */
498         int last_sub_max_channel = 0;
499
500         *checksum = 0xDEADBEEF;
501
502         /* Country IE requirements */
503         BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
504                 country_ie_len & 0x01);
505
506         alpha2[0] = country_ie[0];
507         alpha2[1] = country_ie[1];
508
509         /*
510          * Third octet can be:
511          *    'I' - Indoor
512          *    'O' - Outdoor
513          *
514          *  anything else we assume is no restrictions
515          */
516         if (country_ie[2] == 'I')
517                 flags = NL80211_RRF_NO_OUTDOOR;
518         else if (country_ie[2] == 'O')
519                 flags = NL80211_RRF_NO_INDOOR;
520
521         country_ie += 3;
522         country_ie_len -= 3;
523
524         triplets_start = country_ie;
525         len_at_triplet = country_ie_len;
526
527         *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
528
529         /*
530          * We need to build a reg rule for each triplet, but first we must
531          * calculate the number of reg rules we will need. We will need one
532          * for each channel subband
533          */
534         while (country_ie_len >= 3) {
535                 int end_channel = 0;
536                 struct ieee80211_country_ie_triplet *triplet =
537                         (struct ieee80211_country_ie_triplet *) country_ie;
538                 int cur_sub_max_channel = 0, cur_channel = 0;
539
540                 if (triplet->ext.reg_extension_id >=
541                                 IEEE80211_COUNTRY_EXTENSION_ID) {
542                         country_ie += 3;
543                         country_ie_len -= 3;
544                         continue;
545                 }
546
547                 /* 2 GHz */
548                 if (triplet->chans.first_channel <= 14)
549                         end_channel = triplet->chans.first_channel +
550                                 triplet->chans.num_channels;
551                 else
552                         /*
553                          * 5 GHz -- For example in country IEs if the first
554                          * channel given is 36 and the number of channels is 4
555                          * then the individual channel numbers defined for the
556                          * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
557                          * and not 36, 37, 38, 39.
558                          *
559                          * See: http://tinyurl.com/11d-clarification
560                          */
561                         end_channel =  triplet->chans.first_channel +
562                                 (4 * (triplet->chans.num_channels - 1));
563
564                 cur_channel = triplet->chans.first_channel;
565                 cur_sub_max_channel = end_channel;
566
567                 /* Basic sanity check */
568                 if (cur_sub_max_channel < cur_channel)
569                         return NULL;
570
571                 /*
572                  * Do not allow overlapping channels. Also channels
573                  * passed in each subband must be monotonically
574                  * increasing
575                  */
576                 if (last_sub_max_channel) {
577                         if (cur_channel <= last_sub_max_channel)
578                                 return NULL;
579                         if (cur_sub_max_channel <= last_sub_max_channel)
580                                 return NULL;
581                 }
582
583                 /*
584                  * When dot11RegulatoryClassesRequired is supported
585                  * we can throw ext triplets as part of this soup,
586                  * for now we don't care when those change as we
587                  * don't support them
588                  */
589                 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
590                   ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
591                   ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
592
593                 last_sub_max_channel = cur_sub_max_channel;
594
595                 country_ie += 3;
596                 country_ie_len -= 3;
597                 num_rules++;
598
599                 /*
600                  * Note: this is not a IEEE requirement but
601                  * simply a memory requirement
602                  */
603                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
604                         return NULL;
605         }
606
607         country_ie = triplets_start;
608         country_ie_len = len_at_triplet;
609
610         size_of_regd = sizeof(struct ieee80211_regdomain) +
611                 (num_rules * sizeof(struct ieee80211_reg_rule));
612
613         rd = kzalloc(size_of_regd, GFP_KERNEL);
614         if (!rd)
615                 return NULL;
616
617         rd->n_reg_rules = num_rules;
618         rd->alpha2[0] = alpha2[0];
619         rd->alpha2[1] = alpha2[1];
620
621         /* This time around we fill in the rd */
622         while (country_ie_len >= 3) {
623                 int end_channel = 0;
624                 struct ieee80211_country_ie_triplet *triplet =
625                         (struct ieee80211_country_ie_triplet *) country_ie;
626                 struct ieee80211_reg_rule *reg_rule = NULL;
627                 struct ieee80211_freq_range *freq_range = NULL;
628                 struct ieee80211_power_rule *power_rule = NULL;
629
630                 /*
631                  * Must parse if dot11RegulatoryClassesRequired is true,
632                  * we don't support this yet
633                  */
634                 if (triplet->ext.reg_extension_id >=
635                                 IEEE80211_COUNTRY_EXTENSION_ID) {
636                         country_ie += 3;
637                         country_ie_len -= 3;
638                         continue;
639                 }
640
641                 reg_rule = &rd->reg_rules[i];
642                 freq_range = &reg_rule->freq_range;
643                 power_rule = &reg_rule->power_rule;
644
645                 reg_rule->flags = flags;
646
647                 /* 2 GHz */
648                 if (triplet->chans.first_channel <= 14)
649                         end_channel = triplet->chans.first_channel +
650                                 triplet->chans.num_channels;
651                 else
652                         end_channel =  triplet->chans.first_channel +
653                                 (4 * (triplet->chans.num_channels - 1));
654
655                 /*
656                  * The +10 is since the regulatory domain expects
657                  * the actual band edge, not the center of freq for
658                  * its start and end freqs, assuming 20 MHz bandwidth on
659                  * the channels passed
660                  */
661                 freq_range->start_freq_khz =
662                         MHZ_TO_KHZ(ieee80211_channel_to_frequency(
663                                 triplet->chans.first_channel) - 10);
664                 freq_range->end_freq_khz =
665                         MHZ_TO_KHZ(ieee80211_channel_to_frequency(
666                                 end_channel) + 10);
667
668                 /*
669                  * These are large arbitrary values we use to intersect later.
670                  * Increment this if we ever support >= 40 MHz channels
671                  * in IEEE 802.11
672                  */
673                 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
674                 power_rule->max_antenna_gain = DBI_TO_MBI(100);
675                 power_rule->max_eirp = DBM_TO_MBM(100);
676
677                 country_ie += 3;
678                 country_ie_len -= 3;
679                 i++;
680
681                 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
682         }
683
684         return rd;
685 }
686
687
688 /*
689  * Helper for regdom_intersect(), this does the real
690  * mathematical intersection fun
691  */
692 static int reg_rules_intersect(
693         const struct ieee80211_reg_rule *rule1,
694         const struct ieee80211_reg_rule *rule2,
695         struct ieee80211_reg_rule *intersected_rule)
696 {
697         const struct ieee80211_freq_range *freq_range1, *freq_range2;
698         struct ieee80211_freq_range *freq_range;
699         const struct ieee80211_power_rule *power_rule1, *power_rule2;
700         struct ieee80211_power_rule *power_rule;
701         u32 freq_diff;
702
703         freq_range1 = &rule1->freq_range;
704         freq_range2 = &rule2->freq_range;
705         freq_range = &intersected_rule->freq_range;
706
707         power_rule1 = &rule1->power_rule;
708         power_rule2 = &rule2->power_rule;
709         power_rule = &intersected_rule->power_rule;
710
711         freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
712                 freq_range2->start_freq_khz);
713         freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
714                 freq_range2->end_freq_khz);
715         freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
716                 freq_range2->max_bandwidth_khz);
717
718         freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
719         if (freq_range->max_bandwidth_khz > freq_diff)
720                 freq_range->max_bandwidth_khz = freq_diff;
721
722         power_rule->max_eirp = min(power_rule1->max_eirp,
723                 power_rule2->max_eirp);
724         power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
725                 power_rule2->max_antenna_gain);
726
727         intersected_rule->flags = (rule1->flags | rule2->flags);
728
729         if (!is_valid_reg_rule(intersected_rule))
730                 return -EINVAL;
731
732         return 0;
733 }
734
735 /**
736  * regdom_intersect - do the intersection between two regulatory domains
737  * @rd1: first regulatory domain
738  * @rd2: second regulatory domain
739  *
740  * Use this function to get the intersection between two regulatory domains.
741  * Once completed we will mark the alpha2 for the rd as intersected, "98",
742  * as no one single alpha2 can represent this regulatory domain.
743  *
744  * Returns a pointer to the regulatory domain structure which will hold the
745  * resulting intersection of rules between rd1 and rd2. We will
746  * kzalloc() this structure for you.
747  */
748 static struct ieee80211_regdomain *regdom_intersect(
749         const struct ieee80211_regdomain *rd1,
750         const struct ieee80211_regdomain *rd2)
751 {
752         int r, size_of_regd;
753         unsigned int x, y;
754         unsigned int num_rules = 0, rule_idx = 0;
755         const struct ieee80211_reg_rule *rule1, *rule2;
756         struct ieee80211_reg_rule *intersected_rule;
757         struct ieee80211_regdomain *rd;
758         /* This is just a dummy holder to help us count */
759         struct ieee80211_reg_rule irule;
760
761         /* Uses the stack temporarily for counter arithmetic */
762         intersected_rule = &irule;
763
764         memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
765
766         if (!rd1 || !rd2)
767                 return NULL;
768
769         /*
770          * First we get a count of the rules we'll need, then we actually
771          * build them. This is to so we can malloc() and free() a
772          * regdomain once. The reason we use reg_rules_intersect() here
773          * is it will return -EINVAL if the rule computed makes no sense.
774          * All rules that do check out OK are valid.
775          */
776
777         for (x = 0; x < rd1->n_reg_rules; x++) {
778                 rule1 = &rd1->reg_rules[x];
779                 for (y = 0; y < rd2->n_reg_rules; y++) {
780                         rule2 = &rd2->reg_rules[y];
781                         if (!reg_rules_intersect(rule1, rule2,
782                                         intersected_rule))
783                                 num_rules++;
784                         memset(intersected_rule, 0,
785                                         sizeof(struct ieee80211_reg_rule));
786                 }
787         }
788
789         if (!num_rules)
790                 return NULL;
791
792         size_of_regd = sizeof(struct ieee80211_regdomain) +
793                 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
794
795         rd = kzalloc(size_of_regd, GFP_KERNEL);
796         if (!rd)
797                 return NULL;
798
799         for (x = 0; x < rd1->n_reg_rules; x++) {
800                 rule1 = &rd1->reg_rules[x];
801                 for (y = 0; y < rd2->n_reg_rules; y++) {
802                         rule2 = &rd2->reg_rules[y];
803                         /*
804                          * This time around instead of using the stack lets
805                          * write to the target rule directly saving ourselves
806                          * a memcpy()
807                          */
808                         intersected_rule = &rd->reg_rules[rule_idx];
809                         r = reg_rules_intersect(rule1, rule2,
810                                 intersected_rule);
811                         /*
812                          * No need to memset here the intersected rule here as
813                          * we're not using the stack anymore
814                          */
815                         if (r)
816                                 continue;
817                         rule_idx++;
818                 }
819         }
820
821         if (rule_idx != num_rules) {
822                 kfree(rd);
823                 return NULL;
824         }
825
826         rd->n_reg_rules = num_rules;
827         rd->alpha2[0] = '9';
828         rd->alpha2[1] = '8';
829
830         return rd;
831 }
832
833 /*
834  * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
835  * want to just have the channel structure use these
836  */
837 static u32 map_regdom_flags(u32 rd_flags)
838 {
839         u32 channel_flags = 0;
840         if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
841                 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
842         if (rd_flags & NL80211_RRF_NO_IBSS)
843                 channel_flags |= IEEE80211_CHAN_NO_IBSS;
844         if (rd_flags & NL80211_RRF_DFS)
845                 channel_flags |= IEEE80211_CHAN_RADAR;
846         return channel_flags;
847 }
848
849 static int freq_reg_info_regd(struct wiphy *wiphy,
850                               u32 center_freq,
851                               u32 *bandwidth,
852                               const struct ieee80211_reg_rule **reg_rule,
853                               const struct ieee80211_regdomain *custom_regd)
854 {
855         int i;
856         bool band_rule_found = false;
857         const struct ieee80211_regdomain *regd;
858         u32 max_bandwidth = 0;
859
860         regd = custom_regd ? custom_regd : cfg80211_regdomain;
861
862         /*
863          * Follow the driver's regulatory domain, if present, unless a country
864          * IE has been processed or a user wants to help complaince further
865          */
866         if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
867             last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
868             wiphy->regd)
869                 regd = wiphy->regd;
870
871         if (!regd)
872                 return -EINVAL;
873
874         for (i = 0; i < regd->n_reg_rules; i++) {
875                 const struct ieee80211_reg_rule *rr;
876                 const struct ieee80211_freq_range *fr = NULL;
877                 const struct ieee80211_power_rule *pr = NULL;
878
879                 rr = &regd->reg_rules[i];
880                 fr = &rr->freq_range;
881                 pr = &rr->power_rule;
882
883                 /*
884                  * We only need to know if one frequency rule was
885                  * was in center_freq's band, that's enough, so lets
886                  * not overwrite it once found
887                  */
888                 if (!band_rule_found)
889                         band_rule_found = freq_in_rule_band(fr, center_freq);
890
891                 max_bandwidth = freq_max_bandwidth(fr, center_freq);
892
893                 if (max_bandwidth && *bandwidth <= max_bandwidth) {
894                         *reg_rule = rr;
895                         *bandwidth = max_bandwidth;
896                         break;
897                 }
898         }
899
900         if (!band_rule_found)
901                 return -ERANGE;
902
903         return !max_bandwidth;
904 }
905 EXPORT_SYMBOL(freq_reg_info);
906
907 int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
908                          const struct ieee80211_reg_rule **reg_rule)
909 {
910         return freq_reg_info_regd(wiphy, center_freq,
911                 bandwidth, reg_rule, NULL);
912 }
913
914 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
915                            unsigned int chan_idx)
916 {
917         int r;
918         u32 flags;
919         u32 max_bandwidth = 0;
920         const struct ieee80211_reg_rule *reg_rule = NULL;
921         const struct ieee80211_power_rule *power_rule = NULL;
922         struct ieee80211_supported_band *sband;
923         struct ieee80211_channel *chan;
924         struct wiphy *request_wiphy = NULL;
925
926         assert_cfg80211_lock();
927
928         request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
929
930         sband = wiphy->bands[band];
931         BUG_ON(chan_idx >= sband->n_channels);
932         chan = &sband->channels[chan_idx];
933
934         flags = chan->orig_flags;
935
936         r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
937                 &max_bandwidth, &reg_rule);
938
939         if (r) {
940                 /*
941                  * This means no regulatory rule was found in the country IE
942                  * with a frequency range on the center_freq's band, since
943                  * IEEE-802.11 allows for a country IE to have a subset of the
944                  * regulatory information provided in a country we ignore
945                  * disabling the channel unless at least one reg rule was
946                  * found on the center_freq's band. For details see this
947                  * clarification:
948                  *
949                  * http://tinyurl.com/11d-clarification
950                  */
951                 if (r == -ERANGE &&
952                     last_request->initiator ==
953                     NL80211_REGDOM_SET_BY_COUNTRY_IE) {
954 #ifdef CONFIG_CFG80211_REG_DEBUG
955                         printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
956                                 "intact on %s - no rule found in band on "
957                                 "Country IE\n",
958                                 chan->center_freq, wiphy_name(wiphy));
959 #endif
960                 } else {
961                 /*
962                  * In this case we know the country IE has at least one reg rule
963                  * for the band so we respect its band definitions
964                  */
965 #ifdef CONFIG_CFG80211_REG_DEBUG
966                         if (last_request->initiator ==
967                             NL80211_REGDOM_SET_BY_COUNTRY_IE)
968                                 printk(KERN_DEBUG "cfg80211: Disabling "
969                                         "channel %d MHz on %s due to "
970                                         "Country IE\n",
971                                         chan->center_freq, wiphy_name(wiphy));
972 #endif
973                         flags |= IEEE80211_CHAN_DISABLED;
974                         chan->flags = flags;
975                 }
976                 return;
977         }
978
979         power_rule = &reg_rule->power_rule;
980
981         if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
982             request_wiphy && request_wiphy == wiphy &&
983             request_wiphy->strict_regulatory) {
984                 /*
985                  * This gaurantees the driver's requested regulatory domain
986                  * will always be used as a base for further regulatory
987                  * settings
988                  */
989                 chan->flags = chan->orig_flags =
990                         map_regdom_flags(reg_rule->flags);
991                 chan->max_antenna_gain = chan->orig_mag =
992                         (int) MBI_TO_DBI(power_rule->max_antenna_gain);
993                 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
994                 chan->max_power = chan->orig_mpwr =
995                         (int) MBM_TO_DBM(power_rule->max_eirp);
996                 return;
997         }
998
999         chan->flags = flags | map_regdom_flags(reg_rule->flags);
1000         chan->max_antenna_gain = min(chan->orig_mag,
1001                 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
1002         chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1003         if (chan->orig_mpwr)
1004                 chan->max_power = min(chan->orig_mpwr,
1005                         (int) MBM_TO_DBM(power_rule->max_eirp));
1006         else
1007                 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1008 }
1009
1010 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
1011 {
1012         unsigned int i;
1013         struct ieee80211_supported_band *sband;
1014
1015         BUG_ON(!wiphy->bands[band]);
1016         sband = wiphy->bands[band];
1017
1018         for (i = 0; i < sband->n_channels; i++)
1019                 handle_channel(wiphy, band, i);
1020 }
1021
1022 static bool ignore_reg_update(struct wiphy *wiphy,
1023                               enum nl80211_reg_initiator initiator)
1024 {
1025         if (!last_request)
1026                 return true;
1027         if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1028                   wiphy->custom_regulatory)
1029                 return true;
1030         /*
1031          * wiphy->regd will be set once the device has its own
1032          * desired regulatory domain set
1033          */
1034         if (wiphy->strict_regulatory && !wiphy->regd &&
1035             !is_world_regdom(last_request->alpha2))
1036                 return true;
1037         return false;
1038 }
1039
1040 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1041 {
1042         struct cfg80211_registered_device *drv;
1043
1044         list_for_each_entry(drv, &cfg80211_drv_list, list)
1045                 wiphy_update_regulatory(&drv->wiphy, initiator);
1046 }
1047
1048 static void handle_reg_beacon(struct wiphy *wiphy,
1049                               unsigned int chan_idx,
1050                               struct reg_beacon *reg_beacon)
1051 {
1052         struct ieee80211_supported_band *sband;
1053         struct ieee80211_channel *chan;
1054         bool channel_changed = false;
1055         struct ieee80211_channel chan_before;
1056
1057         assert_cfg80211_lock();
1058
1059         sband = wiphy->bands[reg_beacon->chan.band];
1060         chan = &sband->channels[chan_idx];
1061
1062         if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1063                 return;
1064
1065         if (chan->beacon_found)
1066                 return;
1067
1068         chan->beacon_found = true;
1069
1070         chan_before.center_freq = chan->center_freq;
1071         chan_before.flags = chan->flags;
1072
1073         if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
1074             !(chan->orig_flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
1075                 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1076                 channel_changed = true;
1077         }
1078
1079         if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
1080             !(chan->orig_flags & IEEE80211_CHAN_NO_IBSS)) {
1081                 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1082                 channel_changed = true;
1083         }
1084
1085         if (channel_changed)
1086                 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1087 }
1088
1089 /*
1090  * Called when a scan on a wiphy finds a beacon on
1091  * new channel
1092  */
1093 static void wiphy_update_new_beacon(struct wiphy *wiphy,
1094                                     struct reg_beacon *reg_beacon)
1095 {
1096         unsigned int i;
1097         struct ieee80211_supported_band *sband;
1098
1099         assert_cfg80211_lock();
1100
1101         if (!wiphy->bands[reg_beacon->chan.band])
1102                 return;
1103
1104         sband = wiphy->bands[reg_beacon->chan.band];
1105
1106         for (i = 0; i < sband->n_channels; i++)
1107                 handle_reg_beacon(wiphy, i, reg_beacon);
1108 }
1109
1110 /*
1111  * Called upon reg changes or a new wiphy is added
1112  */
1113 static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1114 {
1115         unsigned int i;
1116         struct ieee80211_supported_band *sband;
1117         struct reg_beacon *reg_beacon;
1118
1119         assert_cfg80211_lock();
1120
1121         if (list_empty(&reg_beacon_list))
1122                 return;
1123
1124         list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1125                 if (!wiphy->bands[reg_beacon->chan.band])
1126                         continue;
1127                 sband = wiphy->bands[reg_beacon->chan.band];
1128                 for (i = 0; i < sband->n_channels; i++)
1129                         handle_reg_beacon(wiphy, i, reg_beacon);
1130         }
1131 }
1132
1133 static bool reg_is_world_roaming(struct wiphy *wiphy)
1134 {
1135         if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1136             (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1137                 return true;
1138         if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1139             wiphy->custom_regulatory)
1140                 return true;
1141         return false;
1142 }
1143
1144 /* Reap the advantages of previously found beacons */
1145 static void reg_process_beacons(struct wiphy *wiphy)
1146 {
1147         if (!reg_is_world_roaming(wiphy))
1148                 return;
1149         wiphy_update_beacon_reg(wiphy);
1150 }
1151
1152 void wiphy_update_regulatory(struct wiphy *wiphy,
1153                              enum nl80211_reg_initiator initiator)
1154 {
1155         enum ieee80211_band band;
1156
1157         if (ignore_reg_update(wiphy, initiator))
1158                 goto out;
1159         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1160                 if (wiphy->bands[band])
1161                         handle_band(wiphy, band);
1162         }
1163 out:
1164         reg_process_beacons(wiphy);
1165         if (wiphy->reg_notifier)
1166                 wiphy->reg_notifier(wiphy, last_request);
1167 }
1168
1169 static void handle_channel_custom(struct wiphy *wiphy,
1170                                   enum ieee80211_band band,
1171                                   unsigned int chan_idx,
1172                                   const struct ieee80211_regdomain *regd)
1173 {
1174         int r;
1175         u32 max_bandwidth = 0;
1176         const struct ieee80211_reg_rule *reg_rule = NULL;
1177         const struct ieee80211_power_rule *power_rule = NULL;
1178         struct ieee80211_supported_band *sband;
1179         struct ieee80211_channel *chan;
1180
1181         sband = wiphy->bands[band];
1182         BUG_ON(chan_idx >= sband->n_channels);
1183         chan = &sband->channels[chan_idx];
1184
1185         r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1186                 &max_bandwidth, &reg_rule, regd);
1187
1188         if (r) {
1189                 chan->flags = IEEE80211_CHAN_DISABLED;
1190                 return;
1191         }
1192
1193         power_rule = &reg_rule->power_rule;
1194
1195         chan->flags |= map_regdom_flags(reg_rule->flags);
1196         chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1197         chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1198         chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1199 }
1200
1201 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1202                                const struct ieee80211_regdomain *regd)
1203 {
1204         unsigned int i;
1205         struct ieee80211_supported_band *sband;
1206
1207         BUG_ON(!wiphy->bands[band]);
1208         sband = wiphy->bands[band];
1209
1210         for (i = 0; i < sband->n_channels; i++)
1211                 handle_channel_custom(wiphy, band, i, regd);
1212 }
1213
1214 /* Used by drivers prior to wiphy registration */
1215 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1216                                    const struct ieee80211_regdomain *regd)
1217 {
1218         enum ieee80211_band band;
1219         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1220                 if (wiphy->bands[band])
1221                         handle_band_custom(wiphy, band, regd);
1222         }
1223 }
1224 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1225
1226 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1227                          const struct ieee80211_regdomain *src_regd)
1228 {
1229         struct ieee80211_regdomain *regd;
1230         int size_of_regd = 0;
1231         unsigned int i;
1232
1233         size_of_regd = sizeof(struct ieee80211_regdomain) +
1234           ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1235
1236         regd = kzalloc(size_of_regd, GFP_KERNEL);
1237         if (!regd)
1238                 return -ENOMEM;
1239
1240         memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1241
1242         for (i = 0; i < src_regd->n_reg_rules; i++)
1243                 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1244                         sizeof(struct ieee80211_reg_rule));
1245
1246         *dst_regd = regd;
1247         return 0;
1248 }
1249
1250 /*
1251  * Return value which can be used by ignore_request() to indicate
1252  * it has been determined we should intersect two regulatory domains
1253  */
1254 #define REG_INTERSECT   1
1255
1256 /* This has the logic which determines when a new request
1257  * should be ignored. */
1258 static int ignore_request(struct wiphy *wiphy,
1259                           struct regulatory_request *pending_request)
1260 {
1261         struct wiphy *last_wiphy = NULL;
1262
1263         assert_cfg80211_lock();
1264
1265         /* All initial requests are respected */
1266         if (!last_request)
1267                 return 0;
1268
1269         switch (pending_request->initiator) {
1270         case NL80211_REGDOM_SET_BY_CORE:
1271                 return -EINVAL;
1272         case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1273
1274                 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1275
1276                 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
1277                         return -EINVAL;
1278                 if (last_request->initiator ==
1279                     NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1280                         if (last_wiphy != wiphy) {
1281                                 /*
1282                                  * Two cards with two APs claiming different
1283                                  * different Country IE alpha2s. We could
1284                                  * intersect them, but that seems unlikely
1285                                  * to be correct. Reject second one for now.
1286                                  */
1287                                 if (regdom_changes(pending_request->alpha2))
1288                                         return -EOPNOTSUPP;
1289                                 return -EALREADY;
1290                         }
1291                         /*
1292                          * Two consecutive Country IE hints on the same wiphy.
1293                          * This should be picked up early by the driver/stack
1294                          */
1295                         if (WARN_ON(regdom_changes(pending_request->alpha2)))
1296                                 return 0;
1297                         return -EALREADY;
1298                 }
1299                 return REG_INTERSECT;
1300         case NL80211_REGDOM_SET_BY_DRIVER:
1301                 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1302                         if (is_old_static_regdom(cfg80211_regdomain))
1303                                 return 0;
1304                         if (regdom_changes(pending_request->alpha2))
1305                                 return 0;
1306                         return -EALREADY;
1307                 }
1308
1309                 /*
1310                  * This would happen if you unplug and plug your card
1311                  * back in or if you add a new device for which the previously
1312                  * loaded card also agrees on the regulatory domain.
1313                  */
1314                 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1315                     !regdom_changes(pending_request->alpha2))
1316                         return -EALREADY;
1317
1318                 return REG_INTERSECT;
1319         case NL80211_REGDOM_SET_BY_USER:
1320                 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1321                         return REG_INTERSECT;
1322                 /*
1323                  * If the user knows better the user should set the regdom
1324                  * to their country before the IE is picked up
1325                  */
1326                 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1327                           last_request->intersect)
1328                         return -EOPNOTSUPP;
1329                 /*
1330                  * Process user requests only after previous user/driver/core
1331                  * requests have been processed
1332                  */
1333                 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1334                     last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1335                     last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1336                         if (regdom_changes(last_request->alpha2))
1337                                 return -EAGAIN;
1338                 }
1339
1340                 if (!is_old_static_regdom(cfg80211_regdomain) &&
1341                     !regdom_changes(pending_request->alpha2))
1342                         return -EALREADY;
1343
1344                 return 0;
1345         }
1346
1347         return -EINVAL;
1348 }
1349
1350 /**
1351  * __regulatory_hint - hint to the wireless core a regulatory domain
1352  * @wiphy: if the hint comes from country information from an AP, this
1353  *      is required to be set to the wiphy that received the information
1354  * @pending_request: the regulatory request currently being processed
1355  *
1356  * The Wireless subsystem can use this function to hint to the wireless core
1357  * what it believes should be the current regulatory domain.
1358  *
1359  * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1360  * already been set or other standard error codes.
1361  *
1362  * Caller must hold &cfg80211_mutex
1363  */
1364 static int __regulatory_hint(struct wiphy *wiphy,
1365                              struct regulatory_request *pending_request)
1366 {
1367         bool intersect = false;
1368         int r = 0;
1369
1370         assert_cfg80211_lock();
1371
1372         r = ignore_request(wiphy, pending_request);
1373
1374         if (r == REG_INTERSECT) {
1375                 if (pending_request->initiator ==
1376                     NL80211_REGDOM_SET_BY_DRIVER) {
1377                         r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1378                         if (r) {
1379                                 kfree(pending_request);
1380                                 return r;
1381                         }
1382                 }
1383                 intersect = true;
1384         } else if (r) {
1385                 /*
1386                  * If the regulatory domain being requested by the
1387                  * driver has already been set just copy it to the
1388                  * wiphy
1389                  */
1390                 if (r == -EALREADY &&
1391                     pending_request->initiator ==
1392                     NL80211_REGDOM_SET_BY_DRIVER) {
1393                         r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1394                         if (r) {
1395                                 kfree(pending_request);
1396                                 return r;
1397                         }
1398                         r = -EALREADY;
1399                         goto new_request;
1400                 }
1401                 kfree(pending_request);
1402                 return r;
1403         }
1404
1405 new_request:
1406         kfree(last_request);
1407
1408         last_request = pending_request;
1409         last_request->intersect = intersect;
1410
1411         pending_request = NULL;
1412
1413         /* When r == REG_INTERSECT we do need to call CRDA */
1414         if (r < 0) {
1415                 /*
1416                  * Since CRDA will not be called in this case as we already
1417                  * have applied the requested regulatory domain before we just
1418                  * inform userspace we have processed the request
1419                  */
1420                 if (r == -EALREADY)
1421                         nl80211_send_reg_change_event(last_request);
1422                 return r;
1423         }
1424
1425         return call_crda(last_request->alpha2);
1426 }
1427
1428 /* This currently only processes user and driver regulatory hints */
1429 static void reg_process_hint(struct regulatory_request *reg_request)
1430 {
1431         int r = 0;
1432         struct wiphy *wiphy = NULL;
1433
1434         BUG_ON(!reg_request->alpha2);
1435
1436         mutex_lock(&cfg80211_mutex);
1437
1438         if (wiphy_idx_valid(reg_request->wiphy_idx))
1439                 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1440
1441         if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1442             !wiphy) {
1443                 kfree(reg_request);
1444                 goto out;
1445         }
1446
1447         r = __regulatory_hint(wiphy, reg_request);
1448         /* This is required so that the orig_* parameters are saved */
1449         if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1450                 wiphy_update_regulatory(wiphy, reg_request->initiator);
1451 out:
1452         mutex_unlock(&cfg80211_mutex);
1453 }
1454
1455 /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1456 static void reg_process_pending_hints(void)
1457         {
1458         struct regulatory_request *reg_request;
1459
1460         spin_lock(&reg_requests_lock);
1461         while (!list_empty(&reg_requests_list)) {
1462                 reg_request = list_first_entry(&reg_requests_list,
1463                                                struct regulatory_request,
1464                                                list);
1465                 list_del_init(&reg_request->list);
1466
1467                 spin_unlock(&reg_requests_lock);
1468                 reg_process_hint(reg_request);
1469                 spin_lock(&reg_requests_lock);
1470         }
1471         spin_unlock(&reg_requests_lock);
1472 }
1473
1474 /* Processes beacon hints -- this has nothing to do with country IEs */
1475 static void reg_process_pending_beacon_hints(void)
1476 {
1477         struct cfg80211_registered_device *drv;
1478         struct reg_beacon *pending_beacon, *tmp;
1479
1480         mutex_lock(&cfg80211_mutex);
1481
1482         /* This goes through the _pending_ beacon list */
1483         spin_lock_bh(&reg_pending_beacons_lock);
1484
1485         if (list_empty(&reg_pending_beacons)) {
1486                 spin_unlock_bh(&reg_pending_beacons_lock);
1487                 goto out;
1488         }
1489
1490         list_for_each_entry_safe(pending_beacon, tmp,
1491                                  &reg_pending_beacons, list) {
1492
1493                 list_del_init(&pending_beacon->list);
1494
1495                 /* Applies the beacon hint to current wiphys */
1496                 list_for_each_entry(drv, &cfg80211_drv_list, list)
1497                         wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1498
1499                 /* Remembers the beacon hint for new wiphys or reg changes */
1500                 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1501         }
1502
1503         spin_unlock_bh(&reg_pending_beacons_lock);
1504 out:
1505         mutex_unlock(&cfg80211_mutex);
1506 }
1507
1508 static void reg_todo(struct work_struct *work)
1509 {
1510         reg_process_pending_hints();
1511         reg_process_pending_beacon_hints();
1512 }
1513
1514 static DECLARE_WORK(reg_work, reg_todo);
1515
1516 static void queue_regulatory_request(struct regulatory_request *request)
1517 {
1518         spin_lock(&reg_requests_lock);
1519         list_add_tail(&request->list, &reg_requests_list);
1520         spin_unlock(&reg_requests_lock);
1521
1522         schedule_work(&reg_work);
1523 }
1524
1525 /* Core regulatory hint -- happens once during cfg80211_init() */
1526 static int regulatory_hint_core(const char *alpha2)
1527 {
1528         struct regulatory_request *request;
1529
1530         BUG_ON(last_request);
1531
1532         request = kzalloc(sizeof(struct regulatory_request),
1533                           GFP_KERNEL);
1534         if (!request)
1535                 return -ENOMEM;
1536
1537         request->alpha2[0] = alpha2[0];
1538         request->alpha2[1] = alpha2[1];
1539         request->initiator = NL80211_REGDOM_SET_BY_CORE;
1540
1541         queue_regulatory_request(request);
1542
1543         return 0;
1544 }
1545
1546 /* User hints */
1547 int regulatory_hint_user(const char *alpha2)
1548 {
1549         struct regulatory_request *request;
1550
1551         BUG_ON(!alpha2);
1552
1553         request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1554         if (!request)
1555                 return -ENOMEM;
1556
1557         request->wiphy_idx = WIPHY_IDX_STALE;
1558         request->alpha2[0] = alpha2[0];
1559         request->alpha2[1] = alpha2[1];
1560         request->initiator = NL80211_REGDOM_SET_BY_USER,
1561
1562         queue_regulatory_request(request);
1563
1564         return 0;
1565 }
1566
1567 /* Driver hints */
1568 int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1569 {
1570         struct regulatory_request *request;
1571
1572         BUG_ON(!alpha2);
1573         BUG_ON(!wiphy);
1574
1575         request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1576         if (!request)
1577                 return -ENOMEM;
1578
1579         request->wiphy_idx = get_wiphy_idx(wiphy);
1580
1581         /* Must have registered wiphy first */
1582         BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1583
1584         request->alpha2[0] = alpha2[0];
1585         request->alpha2[1] = alpha2[1];
1586         request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1587
1588         queue_regulatory_request(request);
1589
1590         return 0;
1591 }
1592 EXPORT_SYMBOL(regulatory_hint);
1593
1594 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1595                         u32 country_ie_checksum)
1596 {
1597         struct wiphy *request_wiphy;
1598
1599         assert_cfg80211_lock();
1600
1601         if (unlikely(last_request->initiator !=
1602             NL80211_REGDOM_SET_BY_COUNTRY_IE))
1603                 return false;
1604
1605         request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1606
1607         if (!request_wiphy)
1608                 return false;
1609
1610         if (likely(request_wiphy != wiphy))
1611                 return !country_ie_integrity_changes(country_ie_checksum);
1612         /*
1613          * We should not have let these through at this point, they
1614          * should have been picked up earlier by the first alpha2 check
1615          * on the device
1616          */
1617         if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1618                 return true;
1619         return false;
1620 }
1621
1622 void regulatory_hint_11d(struct wiphy *wiphy,
1623                         u8 *country_ie,
1624                         u8 country_ie_len)
1625 {
1626         struct ieee80211_regdomain *rd = NULL;
1627         char alpha2[2];
1628         u32 checksum = 0;
1629         enum environment_cap env = ENVIRON_ANY;
1630         struct regulatory_request *request;
1631
1632         mutex_lock(&cfg80211_mutex);
1633
1634         if (unlikely(!last_request)) {
1635                 mutex_unlock(&cfg80211_mutex);
1636                 return;
1637         }
1638
1639         /* IE len must be evenly divisible by 2 */
1640         if (country_ie_len & 0x01)
1641                 goto out;
1642
1643         if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1644                 goto out;
1645
1646         /*
1647          * Pending country IE processing, this can happen after we
1648          * call CRDA and wait for a response if a beacon was received before
1649          * we were able to process the last regulatory_hint_11d() call
1650          */
1651         if (country_ie_regdomain)
1652                 goto out;
1653
1654         alpha2[0] = country_ie[0];
1655         alpha2[1] = country_ie[1];
1656
1657         if (country_ie[2] == 'I')
1658                 env = ENVIRON_INDOOR;
1659         else if (country_ie[2] == 'O')
1660                 env = ENVIRON_OUTDOOR;
1661
1662         /*
1663          * We will run this for *every* beacon processed for the BSSID, so
1664          * we optimize an early check to exit out early if we don't have to
1665          * do anything
1666          */
1667         if (likely(last_request->initiator ==
1668             NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1669             wiphy_idx_valid(last_request->wiphy_idx))) {
1670                 struct cfg80211_registered_device *drv_last_ie;
1671
1672                 drv_last_ie =
1673                         cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
1674
1675                 /*
1676                  * Lets keep this simple -- we trust the first AP
1677                  * after we intersect with CRDA
1678                  */
1679                 if (likely(&drv_last_ie->wiphy == wiphy)) {
1680                         /*
1681                          * Ignore IEs coming in on this wiphy with
1682                          * the same alpha2 and environment cap
1683                          */
1684                         if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1685                                   alpha2) &&
1686                                   env == drv_last_ie->env)) {
1687                                 goto out;
1688                         }
1689                         /*
1690                          * the wiphy moved on to another BSSID or the AP
1691                          * was reconfigured. XXX: We need to deal with the
1692                          * case where the user suspends and goes to goes
1693                          * to another country, and then gets IEs from an
1694                          * AP with different settings
1695                          */
1696                         goto out;
1697                 } else {
1698                         /*
1699                          * Ignore IEs coming in on two separate wiphys with
1700                          * the same alpha2 and environment cap
1701                          */
1702                         if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1703                                   alpha2) &&
1704                                   env == drv_last_ie->env)) {
1705                                 goto out;
1706                         }
1707                         /* We could potentially intersect though */
1708                         goto out;
1709                 }
1710         }
1711
1712         rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1713         if (!rd)
1714                 goto out;
1715
1716         /*
1717          * This will not happen right now but we leave it here for the
1718          * the future when we want to add suspend/resume support and having
1719          * the user move to another country after doing so, or having the user
1720          * move to another AP. Right now we just trust the first AP.
1721          *
1722          * If we hit this before we add this support we want to be informed of
1723          * it as it would indicate a mistake in the current design
1724          */
1725         if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
1726                 goto free_rd_out;
1727
1728         request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1729         if (!request)
1730                 goto free_rd_out;
1731
1732         /*
1733          * We keep this around for when CRDA comes back with a response so
1734          * we can intersect with that
1735          */
1736         country_ie_regdomain = rd;
1737
1738         request->wiphy_idx = get_wiphy_idx(wiphy);
1739         request->alpha2[0] = rd->alpha2[0];
1740         request->alpha2[1] = rd->alpha2[1];
1741         request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
1742         request->country_ie_checksum = checksum;
1743         request->country_ie_env = env;
1744
1745         mutex_unlock(&cfg80211_mutex);
1746
1747         queue_regulatory_request(request);
1748
1749         return;
1750
1751 free_rd_out:
1752         kfree(rd);
1753 out:
1754         mutex_unlock(&cfg80211_mutex);
1755 }
1756 EXPORT_SYMBOL(regulatory_hint_11d);
1757
1758 static bool freq_is_chan_12_13_14(u16 freq)
1759 {
1760         if (freq == ieee80211_channel_to_frequency(12) ||
1761             freq == ieee80211_channel_to_frequency(13) ||
1762             freq == ieee80211_channel_to_frequency(14))
1763                 return true;
1764         return false;
1765 }
1766
1767 int regulatory_hint_found_beacon(struct wiphy *wiphy,
1768                                  struct ieee80211_channel *beacon_chan,
1769                                  gfp_t gfp)
1770 {
1771         struct reg_beacon *reg_beacon;
1772
1773         if (likely((beacon_chan->beacon_found ||
1774             (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1775             (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1776              !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1777                 return 0;
1778
1779         reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1780         if (!reg_beacon)
1781                 return -ENOMEM;
1782
1783 #ifdef CONFIG_CFG80211_REG_DEBUG
1784         printk(KERN_DEBUG "cfg80211: Found new beacon on "
1785                 "frequency: %d MHz (Ch %d) on %s\n",
1786                 beacon_chan->center_freq,
1787                 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1788                 wiphy_name(wiphy));
1789 #endif
1790         memcpy(&reg_beacon->chan, beacon_chan,
1791                 sizeof(struct ieee80211_channel));
1792
1793
1794         /*
1795          * Since we can be called from BH or and non-BH context
1796          * we must use spin_lock_bh()
1797          */
1798         spin_lock_bh(&reg_pending_beacons_lock);
1799         list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1800         spin_unlock_bh(&reg_pending_beacons_lock);
1801
1802         schedule_work(&reg_work);
1803
1804         return 0;
1805 }
1806
1807 static void print_rd_rules(const struct ieee80211_regdomain *rd)
1808 {
1809         unsigned int i;
1810         const struct ieee80211_reg_rule *reg_rule = NULL;
1811         const struct ieee80211_freq_range *freq_range = NULL;
1812         const struct ieee80211_power_rule *power_rule = NULL;
1813
1814         printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1815                 "(max_antenna_gain, max_eirp)\n");
1816
1817         for (i = 0; i < rd->n_reg_rules; i++) {
1818                 reg_rule = &rd->reg_rules[i];
1819                 freq_range = &reg_rule->freq_range;
1820                 power_rule = &reg_rule->power_rule;
1821
1822                 /*
1823                  * There may not be documentation for max antenna gain
1824                  * in certain regions
1825                  */
1826                 if (power_rule->max_antenna_gain)
1827                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1828                                 "(%d mBi, %d mBm)\n",
1829                                 freq_range->start_freq_khz,
1830                                 freq_range->end_freq_khz,
1831                                 freq_range->max_bandwidth_khz,
1832                                 power_rule->max_antenna_gain,
1833                                 power_rule->max_eirp);
1834                 else
1835                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1836                                 "(N/A, %d mBm)\n",
1837                                 freq_range->start_freq_khz,
1838                                 freq_range->end_freq_khz,
1839                                 freq_range->max_bandwidth_khz,
1840                                 power_rule->max_eirp);
1841         }
1842 }
1843
1844 static void print_regdomain(const struct ieee80211_regdomain *rd)
1845 {
1846
1847         if (is_intersected_alpha2(rd->alpha2)) {
1848
1849                 if (last_request->initiator ==
1850                     NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1851                         struct cfg80211_registered_device *drv;
1852                         drv = cfg80211_drv_by_wiphy_idx(
1853                                 last_request->wiphy_idx);
1854                         if (drv) {
1855                                 printk(KERN_INFO "cfg80211: Current regulatory "
1856                                         "domain updated by AP to: %c%c\n",
1857                                         drv->country_ie_alpha2[0],
1858                                         drv->country_ie_alpha2[1]);
1859                         } else
1860                                 printk(KERN_INFO "cfg80211: Current regulatory "
1861                                         "domain intersected: \n");
1862                 } else
1863                                 printk(KERN_INFO "cfg80211: Current regulatory "
1864                                         "domain intersected: \n");
1865         } else if (is_world_regdom(rd->alpha2))
1866                 printk(KERN_INFO "cfg80211: World regulatory "
1867                         "domain updated:\n");
1868         else {
1869                 if (is_unknown_alpha2(rd->alpha2))
1870                         printk(KERN_INFO "cfg80211: Regulatory domain "
1871                                 "changed to driver built-in settings "
1872                                 "(unknown country)\n");
1873                 else
1874                         printk(KERN_INFO "cfg80211: Regulatory domain "
1875                                 "changed to country: %c%c\n",
1876                                 rd->alpha2[0], rd->alpha2[1]);
1877         }
1878         print_rd_rules(rd);
1879 }
1880
1881 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
1882 {
1883         printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1884                 rd->alpha2[0], rd->alpha2[1]);
1885         print_rd_rules(rd);
1886 }
1887
1888 #ifdef CONFIG_CFG80211_REG_DEBUG
1889 static void reg_country_ie_process_debug(
1890         const struct ieee80211_regdomain *rd,
1891         const struct ieee80211_regdomain *country_ie_regdomain,
1892         const struct ieee80211_regdomain *intersected_rd)
1893 {
1894         printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1895         print_regdomain_info(country_ie_regdomain);
1896         printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1897         print_regdomain_info(rd);
1898         if (intersected_rd) {
1899                 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1900                         "and get:\n");
1901                 print_regdomain_info(intersected_rd);
1902                 return;
1903         }
1904         printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1905 }
1906 #else
1907 static inline void reg_country_ie_process_debug(
1908         const struct ieee80211_regdomain *rd,
1909         const struct ieee80211_regdomain *country_ie_regdomain,
1910         const struct ieee80211_regdomain *intersected_rd)
1911 {
1912 }
1913 #endif
1914
1915 /* Takes ownership of rd only if it doesn't fail */
1916 static int __set_regdom(const struct ieee80211_regdomain *rd)
1917 {
1918         const struct ieee80211_regdomain *intersected_rd = NULL;
1919         struct cfg80211_registered_device *drv = NULL;
1920         struct wiphy *request_wiphy;
1921         /* Some basic sanity checks first */
1922
1923         if (is_world_regdom(rd->alpha2)) {
1924                 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1925                         return -EINVAL;
1926                 update_world_regdomain(rd);
1927                 return 0;
1928         }
1929
1930         if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1931                         !is_unknown_alpha2(rd->alpha2))
1932                 return -EINVAL;
1933
1934         if (!last_request)
1935                 return -EINVAL;
1936
1937         /*
1938          * Lets only bother proceeding on the same alpha2 if the current
1939          * rd is non static (it means CRDA was present and was used last)
1940          * and the pending request came in from a country IE
1941          */
1942         if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1943                 /*
1944                  * If someone else asked us to change the rd lets only bother
1945                  * checking if the alpha2 changes if CRDA was already called
1946                  */
1947                 if (!is_old_static_regdom(cfg80211_regdomain) &&
1948                     !regdom_changes(rd->alpha2))
1949                         return -EINVAL;
1950         }
1951
1952         /*
1953          * Now lets set the regulatory domain, update all driver channels
1954          * and finally inform them of what we have done, in case they want
1955          * to review or adjust their own settings based on their own
1956          * internal EEPROM data
1957          */
1958
1959         if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1960                 return -EINVAL;
1961
1962         if (!is_valid_rd(rd)) {
1963                 printk(KERN_ERR "cfg80211: Invalid "
1964                         "regulatory domain detected:\n");
1965                 print_regdomain_info(rd);
1966                 return -EINVAL;
1967         }
1968
1969         request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1970
1971         if (!last_request->intersect) {
1972                 int r;
1973
1974                 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
1975                         reset_regdomains();
1976                         cfg80211_regdomain = rd;
1977                         return 0;
1978                 }
1979
1980                 /*
1981                  * For a driver hint, lets copy the regulatory domain the
1982                  * driver wanted to the wiphy to deal with conflicts
1983                  */
1984
1985                 BUG_ON(request_wiphy->regd);
1986
1987                 r = reg_copy_regd(&request_wiphy->regd, rd);
1988                 if (r)
1989                         return r;
1990
1991                 reset_regdomains();
1992                 cfg80211_regdomain = rd;
1993                 return 0;
1994         }
1995
1996         /* Intersection requires a bit more work */
1997
1998         if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1999
2000                 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2001                 if (!intersected_rd)
2002                         return -EINVAL;
2003
2004                 /*
2005                  * We can trash what CRDA provided now.
2006                  * However if a driver requested this specific regulatory
2007                  * domain we keep it for its private use
2008                  */
2009                 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2010                         request_wiphy->regd = rd;
2011                 else
2012                         kfree(rd);
2013
2014                 rd = NULL;
2015
2016                 reset_regdomains();
2017                 cfg80211_regdomain = intersected_rd;
2018
2019                 return 0;
2020         }
2021
2022         /*
2023          * Country IE requests are handled a bit differently, we intersect
2024          * the country IE rd with what CRDA believes that country should have
2025          */
2026
2027         BUG_ON(!country_ie_regdomain);
2028         BUG_ON(rd == country_ie_regdomain);
2029
2030         /*
2031          * Intersect what CRDA returned and our what we
2032          * had built from the Country IE received
2033          */
2034
2035         intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2036
2037         reg_country_ie_process_debug(rd,
2038                                      country_ie_regdomain,
2039                                      intersected_rd);
2040
2041         kfree(country_ie_regdomain);
2042         country_ie_regdomain = NULL;
2043
2044         if (!intersected_rd)
2045                 return -EINVAL;
2046
2047         drv = wiphy_to_dev(request_wiphy);
2048
2049         drv->country_ie_alpha2[0] = rd->alpha2[0];
2050         drv->country_ie_alpha2[1] = rd->alpha2[1];
2051         drv->env = last_request->country_ie_env;
2052
2053         BUG_ON(intersected_rd == rd);
2054
2055         kfree(rd);
2056         rd = NULL;
2057
2058         reset_regdomains();
2059         cfg80211_regdomain = intersected_rd;
2060
2061         return 0;
2062 }
2063
2064
2065 /*
2066  * Use this call to set the current regulatory domain. Conflicts with
2067  * multiple drivers can be ironed out later. Caller must've already
2068  * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2069  */
2070 int set_regdom(const struct ieee80211_regdomain *rd)
2071 {
2072         int r;
2073
2074         assert_cfg80211_lock();
2075
2076         /* Note that this doesn't update the wiphys, this is done below */
2077         r = __set_regdom(rd);
2078         if (r) {
2079                 kfree(rd);
2080                 return r;
2081         }
2082
2083         /* This would make this whole thing pointless */
2084         if (!last_request->intersect)
2085                 BUG_ON(rd != cfg80211_regdomain);
2086
2087         /* update all wiphys now with the new established regulatory domain */
2088         update_all_wiphy_regulatory(last_request->initiator);
2089
2090         print_regdomain(cfg80211_regdomain);
2091
2092         nl80211_send_reg_change_event(last_request);
2093
2094         return r;
2095 }
2096
2097 /* Caller must hold cfg80211_mutex */
2098 void reg_device_remove(struct wiphy *wiphy)
2099 {
2100         struct wiphy *request_wiphy = NULL;
2101
2102         assert_cfg80211_lock();
2103
2104         if (last_request)
2105                 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2106
2107         kfree(wiphy->regd);
2108         if (!last_request || !request_wiphy)
2109                 return;
2110         if (request_wiphy != wiphy)
2111                 return;
2112         last_request->wiphy_idx = WIPHY_IDX_STALE;
2113         last_request->country_ie_env = ENVIRON_ANY;
2114 }
2115
2116 int regulatory_init(void)
2117 {
2118         int err = 0;
2119
2120         reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2121         if (IS_ERR(reg_pdev))
2122                 return PTR_ERR(reg_pdev);
2123
2124         spin_lock_init(&reg_requests_lock);
2125         spin_lock_init(&reg_pending_beacons_lock);
2126
2127 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2128         cfg80211_regdomain = static_regdom(ieee80211_regdom);
2129
2130         printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
2131         print_regdomain_info(cfg80211_regdomain);
2132         /*
2133          * The old code still requests for a new regdomain and if
2134          * you have CRDA you get it updated, otherwise you get
2135          * stuck with the static values. Since "EU" is not a valid
2136          * ISO / IEC 3166 alpha2 code we can't expect userpace to
2137          * give us a regulatory domain for it. We need last_request
2138          * iniitalized though so lets just send a request which we
2139          * know will be ignored... this crap will be removed once
2140          * OLD_REG dies.
2141          */
2142         err = regulatory_hint_core(ieee80211_regdom);
2143 #else
2144         cfg80211_regdomain = cfg80211_world_regdom;
2145
2146         err = regulatory_hint_core(ieee80211_regdom);
2147 #endif
2148         if (err) {
2149                 if (err == -ENOMEM)
2150                         return err;
2151                 /*
2152                  * N.B. kobject_uevent_env() can fail mainly for when we're out
2153                  * memory which is handled and propagated appropriately above
2154                  * but it can also fail during a netlink_broadcast() or during
2155                  * early boot for call_usermodehelper(). For now treat these
2156                  * errors as non-fatal.
2157                  */
2158                 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2159                         "to call CRDA during init");
2160 #ifdef CONFIG_CFG80211_REG_DEBUG
2161                 /* We want to find out exactly why when debugging */
2162                 WARN_ON(err);
2163 #endif
2164         }
2165
2166         return 0;
2167 }
2168
2169 void regulatory_exit(void)
2170 {
2171         struct regulatory_request *reg_request, *tmp;
2172         struct reg_beacon *reg_beacon, *btmp;
2173
2174         cancel_work_sync(&reg_work);
2175
2176         mutex_lock(&cfg80211_mutex);
2177
2178         reset_regdomains();
2179
2180         kfree(country_ie_regdomain);
2181         country_ie_regdomain = NULL;
2182
2183         kfree(last_request);
2184
2185         platform_device_unregister(reg_pdev);
2186
2187         spin_lock_bh(&reg_pending_beacons_lock);
2188         if (!list_empty(&reg_pending_beacons)) {
2189                 list_for_each_entry_safe(reg_beacon, btmp,
2190                                          &reg_pending_beacons, list) {
2191                         list_del(&reg_beacon->list);
2192                         kfree(reg_beacon);
2193                 }
2194         }
2195         spin_unlock_bh(&reg_pending_beacons_lock);
2196
2197         if (!list_empty(&reg_beacon_list)) {
2198                 list_for_each_entry_safe(reg_beacon, btmp,
2199                                          &reg_beacon_list, list) {
2200                         list_del(&reg_beacon->list);
2201                         kfree(reg_beacon);
2202                 }
2203         }
2204
2205         spin_lock(&reg_requests_lock);
2206         if (!list_empty(&reg_requests_list)) {
2207                 list_for_each_entry_safe(reg_request, tmp,
2208                                          &reg_requests_list, list) {
2209                         list_del(&reg_request->list);
2210                         kfree(reg_request);
2211                 }
2212         }
2213         spin_unlock(&reg_requests_lock);
2214
2215         mutex_unlock(&cfg80211_mutex);
2216 }