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