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