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