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