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