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