]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/wireless/reg.c
cfg80211: extend range deviation for DMG
[thirdparty/kernel/stable.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>
3b77d5ec 5 * Copyright 2008-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com>
2740f0cf 6 * Copyright 2013-2014 Intel Mobile Communications GmbH
4e0854a7 7 * Copyright 2017 Intel Deutschland GmbH
8318d78a 8 *
3b77d5ec
LR
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
8318d78a
JB
20 */
21
3b77d5ec 22
b2e1b302
LR
23/**
24 * DOC: Wireless regulatory infrastructure
8318d78a
JB
25 *
26 * The usual implementation is for a driver to read a device EEPROM to
27 * determine which regulatory domain it should be operating under, then
28 * looking up the allowable channels in a driver-local table and finally
29 * registering those channels in the wiphy structure.
30 *
b2e1b302
LR
31 * Another set of compliance enforcement is for drivers to use their
32 * own compliance limits which can be stored on the EEPROM. The host
33 * driver or firmware may ensure these are used.
34 *
35 * In addition to all this we provide an extra layer of regulatory
36 * conformance. For drivers which do not have any regulatory
37 * information CRDA provides the complete regulatory solution.
38 * For others it provides a community effort on further restrictions
39 * to enhance compliance.
40 *
41 * Note: When number of rules --> infinity we will not be able to
42 * index on alpha2 any more, instead we'll probably have to
43 * rely on some SHA1 checksum of the regdomain for example.
44 *
8318d78a 45 */
e9c0268f
JP
46
47#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48
8318d78a 49#include <linux/kernel.h>
bc3b2d7f 50#include <linux/export.h>
5a0e3ad6 51#include <linux/slab.h>
b2e1b302 52#include <linux/list.h>
c61029c7 53#include <linux/ctype.h>
b2e1b302
LR
54#include <linux/nl80211.h>
55#include <linux/platform_device.h>
d9b93842 56#include <linux/moduleparam.h>
b2e1b302 57#include <net/cfg80211.h>
8318d78a 58#include "core.h"
b2e1b302 59#include "reg.h"
ad932f04 60#include "rdev-ops.h"
3b377ea9 61#include "regdb.h"
73d54c9e 62#include "nl80211.h"
8318d78a 63
ad932f04
AN
64/*
65 * Grace period we give before making sure all current interfaces reside on
66 * channels allowed by the current regulatory domain.
67 */
68#define REG_ENFORCE_GRACE_MS 60000
69
52616f2b
IP
70/**
71 * enum reg_request_treatment - regulatory request treatment
72 *
73 * @REG_REQ_OK: continue processing the regulatory request
74 * @REG_REQ_IGNORE: ignore the regulatory request
75 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
76 * be intersected with the current one.
77 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
78 * regulatory settings, and no further processing is required.
52616f2b 79 */
2f92212b
JB
80enum reg_request_treatment {
81 REG_REQ_OK,
82 REG_REQ_IGNORE,
83 REG_REQ_INTERSECT,
84 REG_REQ_ALREADY_SET,
85};
86
a042994d
LR
87static struct regulatory_request core_request_world = {
88 .initiator = NL80211_REGDOM_SET_BY_CORE,
89 .alpha2[0] = '0',
90 .alpha2[1] = '0',
91 .intersect = false,
92 .processed = true,
93 .country_ie_env = ENVIRON_ANY,
94};
95
38fd2143
JB
96/*
97 * Receipt of information from last regulatory request,
98 * protected by RTNL (and can be accessed with RCU protection)
99 */
c492db37 100static struct regulatory_request __rcu *last_request =
cec3f0ed 101 (void __force __rcu *)&core_request_world;
734366de 102
b2e1b302
LR
103/* To trigger userspace events */
104static struct platform_device *reg_pdev;
8318d78a 105
fb1fc7ad
LR
106/*
107 * Central wireless core regulatory domains, we only need two,
734366de 108 * the current one and a world regulatory domain in case we have no
e8da2bb4 109 * information to give us an alpha2.
38fd2143 110 * (protected by RTNL, can be read under RCU)
fb1fc7ad 111 */
458f4f9e 112const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
734366de 113
57b5ce07
LR
114/*
115 * Number of devices that registered to the core
116 * that support cellular base station regulatory hints
38fd2143 117 * (protected by RTNL)
57b5ce07
LR
118 */
119static int reg_num_devs_support_basehint;
120
52616f2b
IP
121/*
122 * State variable indicating if the platform on which the devices
123 * are attached is operating in an indoor environment. The state variable
124 * is relevant for all registered devices.
52616f2b
IP
125 */
126static bool reg_is_indoor;
05050753
I
127static spinlock_t reg_indoor_lock;
128
129/* Used to track the userspace process controlling the indoor setting */
130static u32 reg_is_indoor_portid;
52616f2b 131
b6863036 132static void restore_regulatory_settings(bool reset_user);
c37722bd 133
458f4f9e
JB
134static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
135{
38fd2143 136 return rtnl_dereference(cfg80211_regdomain);
458f4f9e
JB
137}
138
ad30ca2c 139const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
458f4f9e 140{
38fd2143 141 return rtnl_dereference(wiphy->regd);
458f4f9e
JB
142}
143
3ef121b5
LR
144static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
145{
146 switch (dfs_region) {
147 case NL80211_DFS_UNSET:
148 return "unset";
149 case NL80211_DFS_FCC:
150 return "FCC";
151 case NL80211_DFS_ETSI:
152 return "ETSI";
153 case NL80211_DFS_JP:
154 return "JP";
155 }
156 return "Unknown";
157}
158
6c474799
LR
159enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
160{
161 const struct ieee80211_regdomain *regd = NULL;
162 const struct ieee80211_regdomain *wiphy_regd = NULL;
163
164 regd = get_cfg80211_regdom();
165 if (!wiphy)
166 goto out;
167
168 wiphy_regd = get_wiphy_regdom(wiphy);
169 if (!wiphy_regd)
170 goto out;
171
172 if (wiphy_regd->dfs_region == regd->dfs_region)
173 goto out;
174
c799ba6e
JB
175 pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n",
176 dev_name(&wiphy->dev),
177 reg_dfs_region_str(wiphy_regd->dfs_region),
178 reg_dfs_region_str(regd->dfs_region));
6c474799
LR
179
180out:
181 return regd->dfs_region;
182}
183
458f4f9e
JB
184static void rcu_free_regdom(const struct ieee80211_regdomain *r)
185{
186 if (!r)
187 return;
188 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
189}
190
c492db37
JB
191static struct regulatory_request *get_last_request(void)
192{
38fd2143 193 return rcu_dereference_rtnl(last_request);
c492db37
JB
194}
195
e38f8a7a 196/* Used to queue up regulatory hints */
fe33eb39
LR
197static LIST_HEAD(reg_requests_list);
198static spinlock_t reg_requests_lock;
199
e38f8a7a
LR
200/* Used to queue up beacon hints for review */
201static LIST_HEAD(reg_pending_beacons);
202static spinlock_t reg_pending_beacons_lock;
203
204/* Used to keep track of processed beacon hints */
205static LIST_HEAD(reg_beacon_list);
206
207struct reg_beacon {
208 struct list_head list;
209 struct ieee80211_channel chan;
210};
211
ad932f04
AN
212static void reg_check_chans_work(struct work_struct *work);
213static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);
214
f333a7a2
LR
215static void reg_todo(struct work_struct *work);
216static DECLARE_WORK(reg_work, reg_todo);
217
734366de
JB
218/* We keep a static world regulatory domain in case of the absence of CRDA */
219static const struct ieee80211_regdomain world_regdom = {
28981e5e 220 .n_reg_rules = 8,
734366de
JB
221 .alpha2 = "00",
222 .reg_rules = {
68798a62
LR
223 /* IEEE 802.11b/g, channels 1..11 */
224 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
43c771a1 225 /* IEEE 802.11b/g, channels 12..13. */
c3826807
JB
226 REG_RULE(2467-10, 2472+10, 20, 6, 20,
227 NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW),
611b6a82
LR
228 /* IEEE 802.11 channel 14 - Only JP enables
229 * this and for 802.11b only */
230 REG_RULE(2484-10, 2484+10, 20, 6, 20,
8fe02e16 231 NL80211_RRF_NO_IR |
611b6a82
LR
232 NL80211_RRF_NO_OFDM),
233 /* IEEE 802.11a, channel 36..48 */
c3826807
JB
234 REG_RULE(5180-10, 5240+10, 80, 6, 20,
235 NL80211_RRF_NO_IR |
236 NL80211_RRF_AUTO_BW),
3fc71f77 237
131a19bc 238 /* IEEE 802.11a, channel 52..64 - DFS required */
c3826807 239 REG_RULE(5260-10, 5320+10, 80, 6, 20,
8fe02e16 240 NL80211_RRF_NO_IR |
c3826807 241 NL80211_RRF_AUTO_BW |
131a19bc
JB
242 NL80211_RRF_DFS),
243
244 /* IEEE 802.11a, channel 100..144 - DFS required */
245 REG_RULE(5500-10, 5720+10, 160, 6, 20,
8fe02e16 246 NL80211_RRF_NO_IR |
131a19bc 247 NL80211_RRF_DFS),
3fc71f77
LR
248
249 /* IEEE 802.11a, channel 149..165 */
8ab9d85c 250 REG_RULE(5745-10, 5825+10, 80, 6, 20,
8fe02e16 251 NL80211_RRF_NO_IR),
90cdc6df 252
8047d261 253 /* IEEE 802.11ad (60GHz), channels 1..3 */
90cdc6df 254 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
734366de
JB
255 }
256};
257
38fd2143 258/* protected by RTNL */
a3d2eaf0
JB
259static const struct ieee80211_regdomain *cfg80211_world_regdom =
260 &world_regdom;
734366de 261
6ee7d330 262static char *ieee80211_regdom = "00";
09d989d1 263static char user_alpha2[2];
6ee7d330 264
734366de
JB
265module_param(ieee80211_regdom, charp, 0444);
266MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
267
c888393b 268static void reg_free_request(struct regulatory_request *request)
5ad6ef5e 269{
d34265a3
JB
270 if (request == &core_request_world)
271 return;
272
c888393b
AN
273 if (request != get_last_request())
274 kfree(request);
275}
276
277static void reg_free_last_request(void)
278{
279 struct regulatory_request *lr = get_last_request();
280
5ad6ef5e
LR
281 if (lr != &core_request_world && lr)
282 kfree_rcu(lr, rcu_head);
283}
284
05f1a3ea
LR
285static void reg_update_last_request(struct regulatory_request *request)
286{
255e25b0
LR
287 struct regulatory_request *lr;
288
289 lr = get_last_request();
290 if (lr == request)
291 return;
292
c888393b 293 reg_free_last_request();
05f1a3ea
LR
294 rcu_assign_pointer(last_request, request);
295}
296
379b82f4
JB
297static void reset_regdomains(bool full_reset,
298 const struct ieee80211_regdomain *new_regdom)
734366de 299{
458f4f9e
JB
300 const struct ieee80211_regdomain *r;
301
38fd2143 302 ASSERT_RTNL();
e8da2bb4 303
458f4f9e
JB
304 r = get_cfg80211_regdom();
305
942b25cf 306 /* avoid freeing static information or freeing something twice */
458f4f9e
JB
307 if (r == cfg80211_world_regdom)
308 r = NULL;
942b25cf
JB
309 if (cfg80211_world_regdom == &world_regdom)
310 cfg80211_world_regdom = NULL;
458f4f9e
JB
311 if (r == &world_regdom)
312 r = NULL;
942b25cf 313
458f4f9e
JB
314 rcu_free_regdom(r);
315 rcu_free_regdom(cfg80211_world_regdom);
734366de 316
a3d2eaf0 317 cfg80211_world_regdom = &world_regdom;
458f4f9e 318 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
a042994d
LR
319
320 if (!full_reset)
321 return;
322
05f1a3ea 323 reg_update_last_request(&core_request_world);
734366de
JB
324}
325
fb1fc7ad
LR
326/*
327 * Dynamic world regulatory domain requested by the wireless
328 * core upon initialization
329 */
a3d2eaf0 330static void update_world_regdomain(const struct ieee80211_regdomain *rd)
734366de 331{
c492db37 332 struct regulatory_request *lr;
734366de 333
c492db37
JB
334 lr = get_last_request();
335
336 WARN_ON(!lr);
734366de 337
379b82f4 338 reset_regdomains(false, rd);
734366de
JB
339
340 cfg80211_world_regdom = rd;
734366de 341}
734366de 342
a3d2eaf0 343bool is_world_regdom(const char *alpha2)
b2e1b302
LR
344{
345 if (!alpha2)
346 return false;
1a919318 347 return alpha2[0] == '0' && alpha2[1] == '0';
b2e1b302 348}
8318d78a 349
a3d2eaf0 350static bool is_alpha2_set(const char *alpha2)
b2e1b302
LR
351{
352 if (!alpha2)
353 return false;
1a919318 354 return alpha2[0] && alpha2[1];
b2e1b302 355}
8318d78a 356
a3d2eaf0 357static bool is_unknown_alpha2(const char *alpha2)
b2e1b302
LR
358{
359 if (!alpha2)
360 return false;
fb1fc7ad
LR
361 /*
362 * Special case where regulatory domain was built by driver
363 * but a specific alpha2 cannot be determined
364 */
1a919318 365 return alpha2[0] == '9' && alpha2[1] == '9';
b2e1b302 366}
8318d78a 367
3f2355cb
LR
368static bool is_intersected_alpha2(const char *alpha2)
369{
370 if (!alpha2)
371 return false;
fb1fc7ad
LR
372 /*
373 * Special case where regulatory domain is the
3f2355cb 374 * result of an intersection between two regulatory domain
fb1fc7ad
LR
375 * structures
376 */
1a919318 377 return alpha2[0] == '9' && alpha2[1] == '8';
3f2355cb
LR
378}
379
a3d2eaf0 380static bool is_an_alpha2(const char *alpha2)
b2e1b302
LR
381{
382 if (!alpha2)
383 return false;
1a919318 384 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
b2e1b302 385}
8318d78a 386
a3d2eaf0 387static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
b2e1b302
LR
388{
389 if (!alpha2_x || !alpha2_y)
390 return false;
1a919318 391 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
b2e1b302
LR
392}
393
69b1572b 394static bool regdom_changes(const char *alpha2)
b2e1b302 395{
458f4f9e 396 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
761cf7ec 397
458f4f9e 398 if (!r)
b2e1b302 399 return true;
458f4f9e 400 return !alpha2_equal(r->alpha2, alpha2);
b2e1b302
LR
401}
402
09d989d1
LR
403/*
404 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
405 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
406 * has ever been issued.
407 */
408static bool is_user_regdom_saved(void)
409{
410 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
411 return false;
412
413 /* This would indicate a mistake on the design */
1a919318 414 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
09d989d1 415 "Unexpected user alpha2: %c%c\n",
1a919318 416 user_alpha2[0], user_alpha2[1]))
09d989d1
LR
417 return false;
418
419 return true;
420}
421
e9763c3c
JB
422static const struct ieee80211_regdomain *
423reg_copy_regd(const struct ieee80211_regdomain *src_regd)
3b377ea9
JL
424{
425 struct ieee80211_regdomain *regd;
e9763c3c 426 int size_of_regd;
3b377ea9
JL
427 unsigned int i;
428
82f20856
JB
429 size_of_regd =
430 sizeof(struct ieee80211_regdomain) +
431 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
3b377ea9
JL
432
433 regd = kzalloc(size_of_regd, GFP_KERNEL);
434 if (!regd)
e9763c3c 435 return ERR_PTR(-ENOMEM);
3b377ea9
JL
436
437 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
438
439 for (i = 0; i < src_regd->n_reg_rules; i++)
440 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
e9763c3c 441 sizeof(struct ieee80211_reg_rule));
3b377ea9 442
e9763c3c 443 return regd;
3b377ea9
JL
444}
445
446#ifdef CONFIG_CFG80211_INTERNAL_REGDB
c7d319e5 447struct reg_regdb_apply_request {
3b377ea9 448 struct list_head list;
c7d319e5 449 const struct ieee80211_regdomain *regdom;
3b377ea9
JL
450};
451
c7d319e5
JB
452static LIST_HEAD(reg_regdb_apply_list);
453static DEFINE_MUTEX(reg_regdb_apply_mutex);
3b377ea9 454
c7d319e5 455static void reg_regdb_apply(struct work_struct *work)
3b377ea9 456{
c7d319e5 457 struct reg_regdb_apply_request *request;
a85d0d7f 458
5fe231e8 459 rtnl_lock();
3b377ea9 460
c7d319e5
JB
461 mutex_lock(&reg_regdb_apply_mutex);
462 while (!list_empty(&reg_regdb_apply_list)) {
463 request = list_first_entry(&reg_regdb_apply_list,
464 struct reg_regdb_apply_request,
3b377ea9
JL
465 list);
466 list_del(&request->list);
467
c7d319e5 468 set_regdom(request->regdom, REGD_SOURCE_INTERNAL_DB);
3b377ea9
JL
469 kfree(request);
470 }
c7d319e5 471 mutex_unlock(&reg_regdb_apply_mutex);
a85d0d7f 472
5fe231e8 473 rtnl_unlock();
3b377ea9
JL
474}
475
c7d319e5 476static DECLARE_WORK(reg_regdb_work, reg_regdb_apply);
3b377ea9 477
fd453d3c 478static int reg_query_builtin(const char *alpha2)
3b377ea9 479{
c7d319e5
JB
480 const struct ieee80211_regdomain *regdom = NULL;
481 struct reg_regdb_apply_request *request;
482 unsigned int i;
483
484 for (i = 0; i < reg_regdb_size; i++) {
485 if (alpha2_equal(alpha2, reg_regdb[i]->alpha2)) {
486 regdom = reg_regdb[i];
487 break;
488 }
489 }
490
491 if (!regdom)
492 return -ENODATA;
3b377ea9 493
c7d319e5 494 request = kzalloc(sizeof(struct reg_regdb_apply_request), GFP_KERNEL);
3b377ea9 495 if (!request)
c7d319e5 496 return -ENOMEM;
3b377ea9 497
c7d319e5
JB
498 request->regdom = reg_copy_regd(regdom);
499 if (IS_ERR_OR_NULL(request->regdom)) {
500 kfree(request);
501 return -ENOMEM;
502 }
3b377ea9 503
c7d319e5
JB
504 mutex_lock(&reg_regdb_apply_mutex);
505 list_add_tail(&request->list, &reg_regdb_apply_list);
506 mutex_unlock(&reg_regdb_apply_mutex);
3b377ea9
JL
507
508 schedule_work(&reg_regdb_work);
c7d319e5
JB
509
510 return 0;
3b377ea9 511}
80007efe
LR
512
513/* Feel free to add any other sanity checks here */
514static void reg_regdb_size_check(void)
515{
516 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
517 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
518}
3b377ea9 519#else
80007efe 520static inline void reg_regdb_size_check(void) {}
fd453d3c 521static inline int reg_query_builtin(const char *alpha2)
c7d319e5
JB
522{
523 return -ENODATA;
524}
3b377ea9
JL
525#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
526
b6863036
JB
527#ifdef CONFIG_CFG80211_CRDA_SUPPORT
528/* Max number of consecutive attempts to communicate with CRDA */
529#define REG_MAX_CRDA_TIMEOUTS 10
530
531static u32 reg_crda_timeouts;
532
533static void crda_timeout_work(struct work_struct *work);
534static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work);
535
536static void crda_timeout_work(struct work_struct *work)
537{
c799ba6e 538 pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
b6863036
JB
539 rtnl_lock();
540 reg_crda_timeouts++;
541 restore_regulatory_settings(true);
542 rtnl_unlock();
543}
544
545static void cancel_crda_timeout(void)
546{
547 cancel_delayed_work(&crda_timeout);
548}
549
550static void cancel_crda_timeout_sync(void)
551{
552 cancel_delayed_work_sync(&crda_timeout);
553}
554
555static void reset_crda_timeouts(void)
556{
557 reg_crda_timeouts = 0;
558}
559
fb1fc7ad
LR
560/*
561 * This lets us keep regulatory code which is updated on a regulatory
1226d258 562 * basis in userspace.
fb1fc7ad 563 */
b2e1b302
LR
564static int call_crda(const char *alpha2)
565{
1226d258
JB
566 char country[12];
567 char *env[] = { country, NULL };
c7d319e5 568 int ret;
1226d258
JB
569
570 snprintf(country, sizeof(country), "COUNTRY=%c%c",
571 alpha2[0], alpha2[1]);
572
c37722bd 573 if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) {
042ab5fc 574 pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n");
c37722bd
I
575 return -EINVAL;
576 }
577
b2e1b302 578 if (!is_world_regdom((char *) alpha2))
042ab5fc 579 pr_debug("Calling CRDA for country: %c%c\n",
c799ba6e 580 alpha2[0], alpha2[1]);
b2e1b302 581 else
042ab5fc 582 pr_debug("Calling CRDA to update world regulatory domain\n");
b2e1b302 583
c7d319e5
JB
584 ret = kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
585 if (ret)
586 return ret;
587
588 queue_delayed_work(system_power_efficient_wq,
b6863036 589 &crda_timeout, msecs_to_jiffies(3142));
c7d319e5 590 return 0;
b2e1b302 591}
b6863036
JB
592#else
593static inline void cancel_crda_timeout(void) {}
594static inline void cancel_crda_timeout_sync(void) {}
595static inline void reset_crda_timeouts(void) {}
596static inline int call_crda(const char *alpha2)
597{
598 return -ENODATA;
599}
600#endif /* CONFIG_CFG80211_CRDA_SUPPORT */
b2e1b302 601
cecbb069 602static bool reg_query_database(struct regulatory_request *request)
fe6631ff 603{
c7d319e5 604 /* query internal regulatory database (if it exists) */
fd453d3c 605 if (reg_query_builtin(request->alpha2) == 0)
c7d319e5 606 return true;
eeca9fce 607
c7d319e5
JB
608 if (call_crda(request->alpha2) == 0)
609 return true;
610
611 return false;
fe6631ff
LR
612}
613
e438768f 614bool reg_is_valid_request(const char *alpha2)
b2e1b302 615{
c492db37 616 struct regulatory_request *lr = get_last_request();
61405e97 617
c492db37 618 if (!lr || lr->processed)
f6037d09
JB
619 return false;
620
c492db37 621 return alpha2_equal(lr->alpha2, alpha2);
b2e1b302 622}
8318d78a 623
e3961af1
JD
624static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
625{
626 struct regulatory_request *lr = get_last_request();
627
628 /*
629 * Follow the driver's regulatory domain, if present, unless a country
630 * IE has been processed or a user wants to help complaince further
631 */
632 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
633 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
634 wiphy->regd)
635 return get_wiphy_regdom(wiphy);
636
637 return get_cfg80211_regdom();
638}
639
a6d4a534
AN
640static unsigned int
641reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
642 const struct ieee80211_reg_rule *rule)
97524820
JD
643{
644 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
645 const struct ieee80211_freq_range *freq_range_tmp;
646 const struct ieee80211_reg_rule *tmp;
647 u32 start_freq, end_freq, idx, no;
648
649 for (idx = 0; idx < rd->n_reg_rules; idx++)
650 if (rule == &rd->reg_rules[idx])
651 break;
652
653 if (idx == rd->n_reg_rules)
654 return 0;
655
656 /* get start_freq */
657 no = idx;
658
659 while (no) {
660 tmp = &rd->reg_rules[--no];
661 freq_range_tmp = &tmp->freq_range;
662
663 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
664 break;
665
97524820
JD
666 freq_range = freq_range_tmp;
667 }
668
669 start_freq = freq_range->start_freq_khz;
670
671 /* get end_freq */
672 freq_range = &rule->freq_range;
673 no = idx;
674
675 while (no < rd->n_reg_rules - 1) {
676 tmp = &rd->reg_rules[++no];
677 freq_range_tmp = &tmp->freq_range;
678
679 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
680 break;
681
97524820
JD
682 freq_range = freq_range_tmp;
683 }
684
685 end_freq = freq_range->end_freq_khz;
686
687 return end_freq - start_freq;
688}
689
a6d4a534
AN
690unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
691 const struct ieee80211_reg_rule *rule)
692{
693 unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);
694
695 if (rule->flags & NL80211_RRF_NO_160MHZ)
696 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
697 if (rule->flags & NL80211_RRF_NO_80MHZ)
698 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));
699
700 /*
701 * HT40+/HT40- limits are handled per-channel. Only limit BW if both
702 * are not allowed.
703 */
704 if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
705 rule->flags & NL80211_RRF_NO_HT40PLUS)
706 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));
707
708 return bw;
709}
710
b2e1b302 711/* Sanity check on a regulatory rule */
a3d2eaf0 712static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
8318d78a 713{
a3d2eaf0 714 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
b2e1b302
LR
715 u32 freq_diff;
716
91e99004 717 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
b2e1b302
LR
718 return false;
719
720 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
721 return false;
722
723 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
724
bd05f28e 725 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
1a919318 726 freq_range->max_bandwidth_khz > freq_diff)
b2e1b302
LR
727 return false;
728
729 return true;
730}
731
a3d2eaf0 732static bool is_valid_rd(const struct ieee80211_regdomain *rd)
b2e1b302 733{
a3d2eaf0 734 const struct ieee80211_reg_rule *reg_rule = NULL;
b2e1b302 735 unsigned int i;
8318d78a 736
b2e1b302
LR
737 if (!rd->n_reg_rules)
738 return false;
8318d78a 739
88dc1c3f
LR
740 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
741 return false;
742
b2e1b302
LR
743 for (i = 0; i < rd->n_reg_rules; i++) {
744 reg_rule = &rd->reg_rules[i];
745 if (!is_valid_reg_rule(reg_rule))
746 return false;
747 }
748
749 return true;
8318d78a
JB
750}
751
0c7dc45d
LR
752/**
753 * freq_in_rule_band - tells us if a frequency is in a frequency band
754 * @freq_range: frequency rule we want to query
755 * @freq_khz: frequency we are inquiring about
756 *
757 * This lets us know if a specific frequency rule is or is not relevant to
758 * a specific frequency's band. Bands are device specific and artificial
64629b9d
VK
759 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
760 * however it is safe for now to assume that a frequency rule should not be
761 * part of a frequency's band if the start freq or end freq are off by more
4b26d66a 762 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 20 GHz for the
64629b9d 763 * 60 GHz band.
0c7dc45d
LR
764 * This resolution can be lowered and should be considered as we add
765 * regulatory rule support for other "bands".
766 **/
767static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
1a919318 768 u32 freq_khz)
0c7dc45d
LR
769{
770#define ONE_GHZ_IN_KHZ 1000000
64629b9d
VK
771 /*
772 * From 802.11ad: directional multi-gigabit (DMG):
773 * Pertaining to operation in a frequency band containing a channel
774 * with the Channel starting frequency above 45 GHz.
775 */
776 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
4b26d66a 777 20 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
64629b9d 778 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
0c7dc45d 779 return true;
64629b9d 780 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
0c7dc45d
LR
781 return true;
782 return false;
783#undef ONE_GHZ_IN_KHZ
784}
785
adbfb058
LR
786/*
787 * Later on we can perhaps use the more restrictive DFS
788 * region but we don't have information for that yet so
789 * for now simply disallow conflicts.
790 */
791static enum nl80211_dfs_regions
792reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
793 const enum nl80211_dfs_regions dfs_region2)
794{
795 if (dfs_region1 != dfs_region2)
796 return NL80211_DFS_UNSET;
797 return dfs_region1;
798}
799
fb1fc7ad
LR
800/*
801 * Helper for regdom_intersect(), this does the real
802 * mathematical intersection fun
803 */
97524820
JD
804static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
805 const struct ieee80211_regdomain *rd2,
806 const struct ieee80211_reg_rule *rule1,
1a919318
JB
807 const struct ieee80211_reg_rule *rule2,
808 struct ieee80211_reg_rule *intersected_rule)
9c96477d
LR
809{
810 const struct ieee80211_freq_range *freq_range1, *freq_range2;
811 struct ieee80211_freq_range *freq_range;
812 const struct ieee80211_power_rule *power_rule1, *power_rule2;
813 struct ieee80211_power_rule *power_rule;
97524820 814 u32 freq_diff, max_bandwidth1, max_bandwidth2;
9c96477d
LR
815
816 freq_range1 = &rule1->freq_range;
817 freq_range2 = &rule2->freq_range;
818 freq_range = &intersected_rule->freq_range;
819
820 power_rule1 = &rule1->power_rule;
821 power_rule2 = &rule2->power_rule;
822 power_rule = &intersected_rule->power_rule;
823
824 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
1a919318 825 freq_range2->start_freq_khz);
9c96477d 826 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
1a919318 827 freq_range2->end_freq_khz);
97524820
JD
828
829 max_bandwidth1 = freq_range1->max_bandwidth_khz;
830 max_bandwidth2 = freq_range2->max_bandwidth_khz;
831
b0dfd2ea
JD
832 if (rule1->flags & NL80211_RRF_AUTO_BW)
833 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
834 if (rule2->flags & NL80211_RRF_AUTO_BW)
835 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
97524820
JD
836
837 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
9c96477d 838
b0dfd2ea
JD
839 intersected_rule->flags = rule1->flags | rule2->flags;
840
841 /*
842 * In case NL80211_RRF_AUTO_BW requested for both rules
843 * set AUTO_BW in intersected rule also. Next we will
844 * calculate BW correctly in handle_channel function.
845 * In other case remove AUTO_BW flag while we calculate
846 * maximum bandwidth correctly and auto calculation is
847 * not required.
848 */
849 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
850 (rule2->flags & NL80211_RRF_AUTO_BW))
851 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
852 else
853 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
854
9c96477d
LR
855 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
856 if (freq_range->max_bandwidth_khz > freq_diff)
857 freq_range->max_bandwidth_khz = freq_diff;
858
859 power_rule->max_eirp = min(power_rule1->max_eirp,
860 power_rule2->max_eirp);
861 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
862 power_rule2->max_antenna_gain);
863
089027e5
JD
864 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
865 rule2->dfs_cac_ms);
866
9c96477d
LR
867 if (!is_valid_reg_rule(intersected_rule))
868 return -EINVAL;
869
870 return 0;
871}
872
a62a1aed
EP
873/* check whether old rule contains new rule */
874static bool rule_contains(struct ieee80211_reg_rule *r1,
875 struct ieee80211_reg_rule *r2)
876{
877 /* for simplicity, currently consider only same flags */
878 if (r1->flags != r2->flags)
879 return false;
880
881 /* verify r1 is more restrictive */
882 if ((r1->power_rule.max_antenna_gain >
883 r2->power_rule.max_antenna_gain) ||
884 r1->power_rule.max_eirp > r2->power_rule.max_eirp)
885 return false;
886
887 /* make sure r2's range is contained within r1 */
888 if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
889 r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
890 return false;
891
892 /* and finally verify that r1.max_bw >= r2.max_bw */
893 if (r1->freq_range.max_bandwidth_khz <
894 r2->freq_range.max_bandwidth_khz)
895 return false;
896
897 return true;
898}
899
900/* add or extend current rules. do nothing if rule is already contained */
901static void add_rule(struct ieee80211_reg_rule *rule,
902 struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
903{
904 struct ieee80211_reg_rule *tmp_rule;
905 int i;
906
907 for (i = 0; i < *n_rules; i++) {
908 tmp_rule = &reg_rules[i];
909 /* rule is already contained - do nothing */
910 if (rule_contains(tmp_rule, rule))
911 return;
912
913 /* extend rule if possible */
914 if (rule_contains(rule, tmp_rule)) {
915 memcpy(tmp_rule, rule, sizeof(*rule));
916 return;
917 }
918 }
919
920 memcpy(&reg_rules[*n_rules], rule, sizeof(*rule));
921 (*n_rules)++;
922}
923
9c96477d
LR
924/**
925 * regdom_intersect - do the intersection between two regulatory domains
926 * @rd1: first regulatory domain
927 * @rd2: second regulatory domain
928 *
929 * Use this function to get the intersection between two regulatory domains.
930 * Once completed we will mark the alpha2 for the rd as intersected, "98",
931 * as no one single alpha2 can represent this regulatory domain.
932 *
933 * Returns a pointer to the regulatory domain structure which will hold the
934 * resulting intersection of rules between rd1 and rd2. We will
935 * kzalloc() this structure for you.
936 */
1a919318
JB
937static struct ieee80211_regdomain *
938regdom_intersect(const struct ieee80211_regdomain *rd1,
939 const struct ieee80211_regdomain *rd2)
9c96477d
LR
940{
941 int r, size_of_regd;
942 unsigned int x, y;
a62a1aed 943 unsigned int num_rules = 0;
9c96477d 944 const struct ieee80211_reg_rule *rule1, *rule2;
a62a1aed 945 struct ieee80211_reg_rule intersected_rule;
9c96477d 946 struct ieee80211_regdomain *rd;
9c96477d
LR
947
948 if (!rd1 || !rd2)
949 return NULL;
950
fb1fc7ad
LR
951 /*
952 * First we get a count of the rules we'll need, then we actually
9c96477d
LR
953 * build them. This is to so we can malloc() and free() a
954 * regdomain once. The reason we use reg_rules_intersect() here
955 * is it will return -EINVAL if the rule computed makes no sense.
fb1fc7ad
LR
956 * All rules that do check out OK are valid.
957 */
9c96477d
LR
958
959 for (x = 0; x < rd1->n_reg_rules; x++) {
960 rule1 = &rd1->reg_rules[x];
961 for (y = 0; y < rd2->n_reg_rules; y++) {
962 rule2 = &rd2->reg_rules[y];
97524820 963 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
a62a1aed 964 &intersected_rule))
9c96477d 965 num_rules++;
9c96477d
LR
966 }
967 }
968
969 if (!num_rules)
970 return NULL;
971
972 size_of_regd = sizeof(struct ieee80211_regdomain) +
82f20856 973 num_rules * sizeof(struct ieee80211_reg_rule);
9c96477d
LR
974
975 rd = kzalloc(size_of_regd, GFP_KERNEL);
976 if (!rd)
977 return NULL;
978
a62a1aed 979 for (x = 0; x < rd1->n_reg_rules; x++) {
9c96477d 980 rule1 = &rd1->reg_rules[x];
a62a1aed 981 for (y = 0; y < rd2->n_reg_rules; y++) {
9c96477d 982 rule2 = &rd2->reg_rules[y];
97524820 983 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
a62a1aed 984 &intersected_rule);
fb1fc7ad
LR
985 /*
986 * No need to memset here the intersected rule here as
987 * we're not using the stack anymore
988 */
9c96477d
LR
989 if (r)
990 continue;
9c96477d 991
a62a1aed
EP
992 add_rule(&intersected_rule, rd->reg_rules,
993 &rd->n_reg_rules);
994 }
9c96477d
LR
995 }
996
9c96477d
LR
997 rd->alpha2[0] = '9';
998 rd->alpha2[1] = '8';
adbfb058
LR
999 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
1000 rd2->dfs_region);
9c96477d
LR
1001
1002 return rd;
1003}
1004
fb1fc7ad
LR
1005/*
1006 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1007 * want to just have the channel structure use these
1008 */
b2e1b302
LR
1009static u32 map_regdom_flags(u32 rd_flags)
1010{
1011 u32 channel_flags = 0;
8fe02e16
LR
1012 if (rd_flags & NL80211_RRF_NO_IR_ALL)
1013 channel_flags |= IEEE80211_CHAN_NO_IR;
b2e1b302
LR
1014 if (rd_flags & NL80211_RRF_DFS)
1015 channel_flags |= IEEE80211_CHAN_RADAR;
03f6b084
SF
1016 if (rd_flags & NL80211_RRF_NO_OFDM)
1017 channel_flags |= IEEE80211_CHAN_NO_OFDM;
570dbde1
DS
1018 if (rd_flags & NL80211_RRF_NO_OUTDOOR)
1019 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
06f207fc
AN
1020 if (rd_flags & NL80211_RRF_IR_CONCURRENT)
1021 channel_flags |= IEEE80211_CHAN_IR_CONCURRENT;
a6d4a534
AN
1022 if (rd_flags & NL80211_RRF_NO_HT40MINUS)
1023 channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
1024 if (rd_flags & NL80211_RRF_NO_HT40PLUS)
1025 channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
1026 if (rd_flags & NL80211_RRF_NO_80MHZ)
1027 channel_flags |= IEEE80211_CHAN_NO_80MHZ;
1028 if (rd_flags & NL80211_RRF_NO_160MHZ)
1029 channel_flags |= IEEE80211_CHAN_NO_160MHZ;
b2e1b302
LR
1030 return channel_flags;
1031}
1032
361c9c8b 1033static const struct ieee80211_reg_rule *
49172874 1034freq_reg_info_regd(u32 center_freq,
4edd5698 1035 const struct ieee80211_regdomain *regd, u32 bw)
8318d78a
JB
1036{
1037 int i;
0c7dc45d 1038 bool band_rule_found = false;
038659e7
LR
1039 bool bw_fits = false;
1040
3e0c3ff3 1041 if (!regd)
361c9c8b 1042 return ERR_PTR(-EINVAL);
b2e1b302 1043
3e0c3ff3 1044 for (i = 0; i < regd->n_reg_rules; i++) {
b2e1b302
LR
1045 const struct ieee80211_reg_rule *rr;
1046 const struct ieee80211_freq_range *fr = NULL;
b2e1b302 1047
3e0c3ff3 1048 rr = &regd->reg_rules[i];
b2e1b302 1049 fr = &rr->freq_range;
0c7dc45d 1050
fb1fc7ad
LR
1051 /*
1052 * We only need to know if one frequency rule was
0c7dc45d 1053 * was in center_freq's band, that's enough, so lets
fb1fc7ad
LR
1054 * not overwrite it once found
1055 */
0c7dc45d
LR
1056 if (!band_rule_found)
1057 band_rule_found = freq_in_rule_band(fr, center_freq);
1058
4787cfa0 1059 bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw);
0c7dc45d 1060
361c9c8b
JB
1061 if (band_rule_found && bw_fits)
1062 return rr;
8318d78a
JB
1063 }
1064
0c7dc45d 1065 if (!band_rule_found)
361c9c8b 1066 return ERR_PTR(-ERANGE);
0c7dc45d 1067
361c9c8b 1068 return ERR_PTR(-EINVAL);
b2e1b302
LR
1069}
1070
8de1c63b
JB
1071static const struct ieee80211_reg_rule *
1072__freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw)
1fa25e41 1073{
4edd5698
MM
1074 const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy);
1075 const struct ieee80211_reg_rule *reg_rule = NULL;
1076 u32 bw;
1a919318 1077
4edd5698 1078 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
49172874 1079 reg_rule = freq_reg_info_regd(center_freq, regd, bw);
4edd5698
MM
1080 if (!IS_ERR(reg_rule))
1081 return reg_rule;
1082 }
5d885b99 1083
4edd5698
MM
1084 return reg_rule;
1085}
1086
1087const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
1088 u32 center_freq)
1089{
1090 return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(20));
1fa25e41 1091}
4f366c5d 1092EXPORT_SYMBOL(freq_reg_info);
b2e1b302 1093
034c6d6e 1094const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
926a0a09
LR
1095{
1096 switch (initiator) {
1097 case NL80211_REGDOM_SET_BY_CORE:
034c6d6e 1098 return "core";
926a0a09 1099 case NL80211_REGDOM_SET_BY_USER:
034c6d6e 1100 return "user";
926a0a09 1101 case NL80211_REGDOM_SET_BY_DRIVER:
034c6d6e 1102 return "driver";
926a0a09 1103 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
034c6d6e 1104 return "country IE";
926a0a09
LR
1105 default:
1106 WARN_ON(1);
034c6d6e 1107 return "bug";
926a0a09
LR
1108 }
1109}
034c6d6e 1110EXPORT_SYMBOL(reg_initiator_name);
e702d3cf 1111
1aeb135f
MS
1112static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd,
1113 const struct ieee80211_reg_rule *reg_rule,
1114 const struct ieee80211_channel *chan)
1115{
1116 const struct ieee80211_freq_range *freq_range = NULL;
1117 u32 max_bandwidth_khz, bw_flags = 0;
1118
1119 freq_range = &reg_rule->freq_range;
1120
1121 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1122 /* Check if auto calculation requested */
1123 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
1124 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1125
1126 /* If we get a reg_rule we can assume that at least 5Mhz fit */
4787cfa0
RM
1127 if (!cfg80211_does_bw_fit_range(freq_range,
1128 MHZ_TO_KHZ(chan->center_freq),
1129 MHZ_TO_KHZ(10)))
1aeb135f 1130 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
4787cfa0
RM
1131 if (!cfg80211_does_bw_fit_range(freq_range,
1132 MHZ_TO_KHZ(chan->center_freq),
1133 MHZ_TO_KHZ(20)))
1aeb135f
MS
1134 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1135
1136 if (max_bandwidth_khz < MHZ_TO_KHZ(10))
1137 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1138 if (max_bandwidth_khz < MHZ_TO_KHZ(20))
1139 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1140 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
1141 bw_flags |= IEEE80211_CHAN_NO_HT40;
1142 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
1143 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
1144 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
1145 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
1146 return bw_flags;
1147}
1148
e33e2241
JB
1149/*
1150 * Note that right now we assume the desired channel bandwidth
1151 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1152 * per channel, the primary and the extension channel).
038659e7 1153 */
7ca43d03
LR
1154static void handle_channel(struct wiphy *wiphy,
1155 enum nl80211_reg_initiator initiator,
fdc9d7b2 1156 struct ieee80211_channel *chan)
b2e1b302 1157{
038659e7 1158 u32 flags, bw_flags = 0;
b2e1b302
LR
1159 const struct ieee80211_reg_rule *reg_rule = NULL;
1160 const struct ieee80211_power_rule *power_rule = NULL;
fe33eb39 1161 struct wiphy *request_wiphy = NULL;
c492db37 1162 struct regulatory_request *lr = get_last_request();
97524820 1163 const struct ieee80211_regdomain *regd;
a92a3ce7 1164
c492db37 1165 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
a92a3ce7
LR
1166
1167 flags = chan->orig_flags;
b2e1b302 1168
361c9c8b
JB
1169 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1170 if (IS_ERR(reg_rule)) {
ca4ffe8f
LR
1171 /*
1172 * We will disable all channels that do not match our
25985edc 1173 * received regulatory rule unless the hint is coming
ca4ffe8f
LR
1174 * from a Country IE and the Country IE had no information
1175 * about a band. The IEEE 802.11 spec allows for an AP
1176 * to send only a subset of the regulatory rules allowed,
1177 * so an AP in the US that only supports 2.4 GHz may only send
1178 * a country IE with information for the 2.4 GHz band
1179 * while 5 GHz is still supported.
1180 */
1181 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
361c9c8b 1182 PTR_ERR(reg_rule) == -ERANGE)
ca4ffe8f
LR
1183 return;
1184
cc493e4f
LR
1185 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1186 request_wiphy && request_wiphy == wiphy &&
a2f73b6c 1187 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
c799ba6e
JB
1188 pr_debug("Disabling freq %d MHz for good\n",
1189 chan->center_freq);
cc493e4f
LR
1190 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1191 chan->flags = chan->orig_flags;
1192 } else {
c799ba6e
JB
1193 pr_debug("Disabling freq %d MHz\n",
1194 chan->center_freq);
cc493e4f
LR
1195 chan->flags |= IEEE80211_CHAN_DISABLED;
1196 }
8318d78a 1197 return;
ca4ffe8f 1198 }
8318d78a 1199
b0dfd2ea 1200 regd = reg_get_regdomain(wiphy);
e702d3cf 1201
b2e1b302 1202 power_rule = &reg_rule->power_rule;
1aeb135f 1203 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
b2e1b302 1204
c492db37 1205 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
806a9e39 1206 request_wiphy && request_wiphy == wiphy &&
a2f73b6c 1207 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
fb1fc7ad 1208 /*
25985edc 1209 * This guarantees the driver's requested regulatory domain
f976376d 1210 * will always be used as a base for further regulatory
fb1fc7ad
LR
1211 * settings
1212 */
f976376d 1213 chan->flags = chan->orig_flags =
038659e7 1214 map_regdom_flags(reg_rule->flags) | bw_flags;
f976376d
LR
1215 chan->max_antenna_gain = chan->orig_mag =
1216 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
279f0f55 1217 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
f976376d 1218 (int) MBM_TO_DBM(power_rule->max_eirp);
4f267c11
JD
1219
1220 if (chan->flags & IEEE80211_CHAN_RADAR) {
1221 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1222 if (reg_rule->dfs_cac_ms)
1223 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1224 }
1225
f976376d
LR
1226 return;
1227 }
1228
04f39047
SW
1229 chan->dfs_state = NL80211_DFS_USABLE;
1230 chan->dfs_state_entered = jiffies;
1231
aa3d7eef 1232 chan->beacon_found = false;
038659e7 1233 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1a919318
JB
1234 chan->max_antenna_gain =
1235 min_t(int, chan->orig_mag,
1236 MBI_TO_DBI(power_rule->max_antenna_gain));
eccc068e 1237 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
089027e5
JD
1238
1239 if (chan->flags & IEEE80211_CHAN_RADAR) {
1240 if (reg_rule->dfs_cac_ms)
1241 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1242 else
1243 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1244 }
1245
5e31fc08
SG
1246 if (chan->orig_mpwr) {
1247 /*
a09a85a0
LR
1248 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1249 * will always follow the passed country IE power settings.
5e31fc08
SG
1250 */
1251 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
a09a85a0 1252 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
5e31fc08
SG
1253 chan->max_power = chan->max_reg_power;
1254 else
1255 chan->max_power = min(chan->orig_mpwr,
1256 chan->max_reg_power);
1257 } else
1258 chan->max_power = chan->max_reg_power;
8318d78a
JB
1259}
1260
7ca43d03 1261static void handle_band(struct wiphy *wiphy,
fdc9d7b2
JB
1262 enum nl80211_reg_initiator initiator,
1263 struct ieee80211_supported_band *sband)
8318d78a 1264{
a92a3ce7 1265 unsigned int i;
a92a3ce7 1266
fdc9d7b2
JB
1267 if (!sband)
1268 return;
8318d78a
JB
1269
1270 for (i = 0; i < sband->n_channels; i++)
fdc9d7b2 1271 handle_channel(wiphy, initiator, &sband->channels[i]);
8318d78a
JB
1272}
1273
57b5ce07
LR
1274static bool reg_request_cell_base(struct regulatory_request *request)
1275{
1276 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1277 return false;
1a919318 1278 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
57b5ce07
LR
1279}
1280
1281bool reg_last_request_cell_base(void)
1282{
38fd2143 1283 return reg_request_cell_base(get_last_request());
57b5ce07
LR
1284}
1285
94fc661f 1286#ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
57b5ce07 1287/* Core specific check */
2f92212b
JB
1288static enum reg_request_treatment
1289reg_ignore_cell_hint(struct regulatory_request *pending_request)
57b5ce07 1290{
c492db37
JB
1291 struct regulatory_request *lr = get_last_request();
1292
57b5ce07 1293 if (!reg_num_devs_support_basehint)
2f92212b 1294 return REG_REQ_IGNORE;
57b5ce07 1295
c492db37 1296 if (reg_request_cell_base(lr) &&
1a919318 1297 !regdom_changes(pending_request->alpha2))
2f92212b 1298 return REG_REQ_ALREADY_SET;
1a919318 1299
2f92212b 1300 return REG_REQ_OK;
57b5ce07
LR
1301}
1302
1303/* Device specific check */
1304static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1305{
1a919318 1306 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
57b5ce07
LR
1307}
1308#else
a515de66
JB
1309static enum reg_request_treatment
1310reg_ignore_cell_hint(struct regulatory_request *pending_request)
57b5ce07 1311{
2f92212b 1312 return REG_REQ_IGNORE;
57b5ce07 1313}
1a919318
JB
1314
1315static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
57b5ce07
LR
1316{
1317 return true;
1318}
1319#endif
1320
fa1fb9cb
LR
1321static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1322{
a2f73b6c
LR
1323 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1324 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
fa1fb9cb
LR
1325 return true;
1326 return false;
1327}
57b5ce07 1328
7db90f4a
LR
1329static bool ignore_reg_update(struct wiphy *wiphy,
1330 enum nl80211_reg_initiator initiator)
14b9815a 1331{
c492db37
JB
1332 struct regulatory_request *lr = get_last_request();
1333
b0d7aa59
JD
1334 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1335 return true;
1336
c492db37 1337 if (!lr) {
c799ba6e
JB
1338 pr_debug("Ignoring regulatory request set by %s since last_request is not set\n",
1339 reg_initiator_name(initiator));
14b9815a 1340 return true;
926a0a09
LR
1341 }
1342
7db90f4a 1343 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
a2f73b6c 1344 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
c799ba6e
JB
1345 pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n",
1346 reg_initiator_name(initiator));
14b9815a 1347 return true;
926a0a09
LR
1348 }
1349
fb1fc7ad
LR
1350 /*
1351 * wiphy->regd will be set once the device has its own
1352 * desired regulatory domain set
1353 */
fa1fb9cb 1354 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
749b527b 1355 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
c492db37 1356 !is_world_regdom(lr->alpha2)) {
c799ba6e
JB
1357 pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n",
1358 reg_initiator_name(initiator));
14b9815a 1359 return true;
926a0a09
LR
1360 }
1361
c492db37 1362 if (reg_request_cell_base(lr))
57b5ce07
LR
1363 return reg_dev_ignore_cell_hint(wiphy);
1364
14b9815a
LR
1365 return false;
1366}
1367
3195e489
LR
1368static bool reg_is_world_roaming(struct wiphy *wiphy)
1369{
1370 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1371 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1372 struct regulatory_request *lr = get_last_request();
1373
1374 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1375 return true;
1376
1377 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
a2f73b6c 1378 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
3195e489
LR
1379 return true;
1380
1381 return false;
1382}
1383
1a919318 1384static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
e38f8a7a
LR
1385 struct reg_beacon *reg_beacon)
1386{
e38f8a7a
LR
1387 struct ieee80211_supported_band *sband;
1388 struct ieee80211_channel *chan;
6bad8766
LR
1389 bool channel_changed = false;
1390 struct ieee80211_channel chan_before;
e38f8a7a 1391
e38f8a7a
LR
1392 sband = wiphy->bands[reg_beacon->chan.band];
1393 chan = &sband->channels[chan_idx];
1394
1395 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1396 return;
1397
6bad8766
LR
1398 if (chan->beacon_found)
1399 return;
1400
1401 chan->beacon_found = true;
1402
0f500a5f
LR
1403 if (!reg_is_world_roaming(wiphy))
1404 return;
1405
a2f73b6c 1406 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
37184244
LR
1407 return;
1408
6bad8766
LR
1409 chan_before.center_freq = chan->center_freq;
1410 chan_before.flags = chan->flags;
1411
8fe02e16
LR
1412 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1413 chan->flags &= ~IEEE80211_CHAN_NO_IR;
6bad8766 1414 channel_changed = true;
e38f8a7a
LR
1415 }
1416
6bad8766
LR
1417 if (channel_changed)
1418 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
e38f8a7a
LR
1419}
1420
1421/*
1422 * Called when a scan on a wiphy finds a beacon on
1423 * new channel
1424 */
1425static void wiphy_update_new_beacon(struct wiphy *wiphy,
1426 struct reg_beacon *reg_beacon)
1427{
1428 unsigned int i;
1429 struct ieee80211_supported_band *sband;
1430
e38f8a7a
LR
1431 if (!wiphy->bands[reg_beacon->chan.band])
1432 return;
1433
1434 sband = wiphy->bands[reg_beacon->chan.band];
1435
1436 for (i = 0; i < sband->n_channels; i++)
1437 handle_reg_beacon(wiphy, i, reg_beacon);
1438}
1439
1440/*
1441 * Called upon reg changes or a new wiphy is added
1442 */
1443static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1444{
1445 unsigned int i;
1446 struct ieee80211_supported_band *sband;
1447 struct reg_beacon *reg_beacon;
1448
e38f8a7a
LR
1449 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1450 if (!wiphy->bands[reg_beacon->chan.band])
1451 continue;
1452 sband = wiphy->bands[reg_beacon->chan.band];
1453 for (i = 0; i < sband->n_channels; i++)
1454 handle_reg_beacon(wiphy, i, reg_beacon);
1455 }
1456}
1457
e38f8a7a
LR
1458/* Reap the advantages of previously found beacons */
1459static void reg_process_beacons(struct wiphy *wiphy)
1460{
b1ed8ddd
LR
1461 /*
1462 * Means we are just firing up cfg80211, so no beacons would
1463 * have been processed yet.
1464 */
1465 if (!last_request)
1466 return;
e38f8a7a
LR
1467 wiphy_update_beacon_reg(wiphy);
1468}
1469
1a919318 1470static bool is_ht40_allowed(struct ieee80211_channel *chan)
038659e7
LR
1471{
1472 if (!chan)
1a919318 1473 return false;
038659e7 1474 if (chan->flags & IEEE80211_CHAN_DISABLED)
1a919318 1475 return false;
038659e7 1476 /* This would happen when regulatory rules disallow HT40 completely */
55b183ad
FF
1477 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1478 return false;
1479 return true;
038659e7
LR
1480}
1481
1482static void reg_process_ht_flags_channel(struct wiphy *wiphy,
fdc9d7b2 1483 struct ieee80211_channel *channel)
038659e7 1484{
fdc9d7b2 1485 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
038659e7 1486 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
4e0854a7 1487 const struct ieee80211_regdomain *regd;
038659e7 1488 unsigned int i;
4e0854a7 1489 u32 flags;
038659e7 1490
1a919318 1491 if (!is_ht40_allowed(channel)) {
038659e7
LR
1492 channel->flags |= IEEE80211_CHAN_NO_HT40;
1493 return;
1494 }
1495
1496 /*
1497 * We need to ensure the extension channels exist to
1498 * be able to use HT40- or HT40+, this finds them (or not)
1499 */
1500 for (i = 0; i < sband->n_channels; i++) {
1501 struct ieee80211_channel *c = &sband->channels[i];
1a919318 1502
038659e7
LR
1503 if (c->center_freq == (channel->center_freq - 20))
1504 channel_before = c;
1505 if (c->center_freq == (channel->center_freq + 20))
1506 channel_after = c;
1507 }
1508
4e0854a7
EG
1509 flags = 0;
1510 regd = get_wiphy_regdom(wiphy);
1511 if (regd) {
1512 const struct ieee80211_reg_rule *reg_rule =
1513 freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq),
1514 regd, MHZ_TO_KHZ(20));
1515
1516 if (!IS_ERR(reg_rule))
1517 flags = reg_rule->flags;
1518 }
1519
038659e7
LR
1520 /*
1521 * Please note that this assumes target bandwidth is 20 MHz,
1522 * if that ever changes we also need to change the below logic
1523 * to include that as well.
1524 */
4e0854a7
EG
1525 if (!is_ht40_allowed(channel_before) ||
1526 flags & NL80211_RRF_NO_HT40MINUS)
689da1b3 1527 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
038659e7 1528 else
689da1b3 1529 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
038659e7 1530
4e0854a7
EG
1531 if (!is_ht40_allowed(channel_after) ||
1532 flags & NL80211_RRF_NO_HT40PLUS)
689da1b3 1533 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
038659e7 1534 else
689da1b3 1535 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
038659e7
LR
1536}
1537
1538static void reg_process_ht_flags_band(struct wiphy *wiphy,
fdc9d7b2 1539 struct ieee80211_supported_band *sband)
038659e7
LR
1540{
1541 unsigned int i;
038659e7 1542
fdc9d7b2
JB
1543 if (!sband)
1544 return;
038659e7
LR
1545
1546 for (i = 0; i < sband->n_channels; i++)
fdc9d7b2 1547 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
038659e7
LR
1548}
1549
1550static void reg_process_ht_flags(struct wiphy *wiphy)
1551{
57fbcce3 1552 enum nl80211_band band;
038659e7
LR
1553
1554 if (!wiphy)
1555 return;
1556
57fbcce3 1557 for (band = 0; band < NUM_NL80211_BANDS; band++)
fdc9d7b2 1558 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
038659e7
LR
1559}
1560
0e3802db
LR
1561static void reg_call_notifier(struct wiphy *wiphy,
1562 struct regulatory_request *request)
1563{
1564 if (wiphy->reg_notifier)
1565 wiphy->reg_notifier(wiphy, request);
1566}
1567
ad932f04
AN
1568static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev)
1569{
ad932f04
AN
1570 struct cfg80211_chan_def chandef;
1571 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
20658702 1572 enum nl80211_iftype iftype;
ad932f04
AN
1573
1574 wdev_lock(wdev);
20658702 1575 iftype = wdev->iftype;
ad932f04 1576
20658702 1577 /* make sure the interface is active */
ad932f04 1578 if (!wdev->netdev || !netif_running(wdev->netdev))
20658702 1579 goto wdev_inactive_unlock;
ad932f04 1580
20658702 1581 switch (iftype) {
ad932f04
AN
1582 case NL80211_IFTYPE_AP:
1583 case NL80211_IFTYPE_P2P_GO:
1584 if (!wdev->beacon_interval)
20658702
AN
1585 goto wdev_inactive_unlock;
1586 chandef = wdev->chandef;
185076d6
AN
1587 break;
1588 case NL80211_IFTYPE_ADHOC:
1589 if (!wdev->ssid_len)
20658702
AN
1590 goto wdev_inactive_unlock;
1591 chandef = wdev->chandef;
ad932f04
AN
1592 break;
1593 case NL80211_IFTYPE_STATION:
1594 case NL80211_IFTYPE_P2P_CLIENT:
ad932f04
AN
1595 if (!wdev->current_bss ||
1596 !wdev->current_bss->pub.channel)
20658702 1597 goto wdev_inactive_unlock;
ad932f04 1598
20658702
AN
1599 if (!rdev->ops->get_channel ||
1600 rdev_get_channel(rdev, wdev, &chandef))
1601 cfg80211_chandef_create(&chandef,
1602 wdev->current_bss->pub.channel,
1603 NL80211_CHAN_NO_HT);
ad932f04
AN
1604 break;
1605 case NL80211_IFTYPE_MONITOR:
1606 case NL80211_IFTYPE_AP_VLAN:
1607 case NL80211_IFTYPE_P2P_DEVICE:
1608 /* no enforcement required */
1609 break;
1610 default:
1611 /* others not implemented for now */
1612 WARN_ON(1);
1613 break;
1614 }
1615
ad932f04 1616 wdev_unlock(wdev);
20658702
AN
1617
1618 switch (iftype) {
1619 case NL80211_IFTYPE_AP:
1620 case NL80211_IFTYPE_P2P_GO:
1621 case NL80211_IFTYPE_ADHOC:
923b352f 1622 return cfg80211_reg_can_beacon_relax(wiphy, &chandef, iftype);
20658702
AN
1623 case NL80211_IFTYPE_STATION:
1624 case NL80211_IFTYPE_P2P_CLIENT:
1625 return cfg80211_chandef_usable(wiphy, &chandef,
1626 IEEE80211_CHAN_DISABLED);
1627 default:
1628 break;
1629 }
1630
1631 return true;
1632
1633wdev_inactive_unlock:
1634 wdev_unlock(wdev);
1635 return true;
ad932f04
AN
1636}
1637
1638static void reg_leave_invalid_chans(struct wiphy *wiphy)
1639{
1640 struct wireless_dev *wdev;
1641 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1642
1643 ASSERT_RTNL();
1644
53873f13 1645 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
ad932f04
AN
1646 if (!reg_wdev_chan_valid(wiphy, wdev))
1647 cfg80211_leave(rdev, wdev);
1648}
1649
1650static void reg_check_chans_work(struct work_struct *work)
1651{
1652 struct cfg80211_registered_device *rdev;
1653
c799ba6e 1654 pr_debug("Verifying active interfaces after reg change\n");
ad932f04
AN
1655 rtnl_lock();
1656
1657 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1658 if (!(rdev->wiphy.regulatory_flags &
1659 REGULATORY_IGNORE_STALE_KICKOFF))
1660 reg_leave_invalid_chans(&rdev->wiphy);
1661
1662 rtnl_unlock();
1663}
1664
1665static void reg_check_channels(void)
1666{
1667 /*
1668 * Give usermode a chance to do something nicer (move to another
1669 * channel, orderly disconnection), before forcing a disconnection.
1670 */
1671 mod_delayed_work(system_power_efficient_wq,
1672 &reg_check_chans,
1673 msecs_to_jiffies(REG_ENFORCE_GRACE_MS));
1674}
1675
eac03e38
SN
1676static void wiphy_update_regulatory(struct wiphy *wiphy,
1677 enum nl80211_reg_initiator initiator)
b2e1b302 1678{
57fbcce3 1679 enum nl80211_band band;
c492db37 1680 struct regulatory_request *lr = get_last_request();
eac03e38 1681
0e3802db
LR
1682 if (ignore_reg_update(wiphy, initiator)) {
1683 /*
1684 * Regulatory updates set by CORE are ignored for custom
1685 * regulatory cards. Let us notify the changes to the driver,
1686 * as some drivers used this to restore its orig_* reg domain.
1687 */
1688 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
a2f73b6c 1689 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
0e3802db 1690 reg_call_notifier(wiphy, lr);
a203c2aa 1691 return;
0e3802db 1692 }
a203c2aa 1693
c492db37 1694 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
b68e6b3b 1695
57fbcce3 1696 for (band = 0; band < NUM_NL80211_BANDS; band++)
fdc9d7b2 1697 handle_band(wiphy, initiator, wiphy->bands[band]);
a203c2aa 1698
e38f8a7a 1699 reg_process_beacons(wiphy);
038659e7 1700 reg_process_ht_flags(wiphy);
0e3802db 1701 reg_call_notifier(wiphy, lr);
b2e1b302
LR
1702}
1703
d7549cbb
SN
1704static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1705{
1706 struct cfg80211_registered_device *rdev;
4a38994f 1707 struct wiphy *wiphy;
d7549cbb 1708
5fe231e8 1709 ASSERT_RTNL();
458f4f9e 1710
4a38994f
RM
1711 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1712 wiphy = &rdev->wiphy;
1713 wiphy_update_regulatory(wiphy, initiator);
4a38994f 1714 }
ad932f04
AN
1715
1716 reg_check_channels();
d7549cbb
SN
1717}
1718
1fa25e41 1719static void handle_channel_custom(struct wiphy *wiphy,
fdc9d7b2 1720 struct ieee80211_channel *chan,
1fa25e41
LR
1721 const struct ieee80211_regdomain *regd)
1722{
038659e7 1723 u32 bw_flags = 0;
1fa25e41
LR
1724 const struct ieee80211_reg_rule *reg_rule = NULL;
1725 const struct ieee80211_power_rule *power_rule = NULL;
4edd5698 1726 u32 bw;
ac46d48e 1727
4edd5698 1728 for (bw = MHZ_TO_KHZ(20); bw >= MHZ_TO_KHZ(5); bw = bw / 2) {
49172874 1729 reg_rule = freq_reg_info_regd(MHZ_TO_KHZ(chan->center_freq),
4edd5698
MM
1730 regd, bw);
1731 if (!IS_ERR(reg_rule))
1732 break;
1733 }
1fa25e41 1734
361c9c8b 1735 if (IS_ERR(reg_rule)) {
c799ba6e
JB
1736 pr_debug("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1737 chan->center_freq);
db8dfee5
AN
1738 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
1739 chan->flags |= IEEE80211_CHAN_DISABLED;
1740 } else {
1741 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1742 chan->flags = chan->orig_flags;
1743 }
1fa25e41
LR
1744 return;
1745 }
1746
1747 power_rule = &reg_rule->power_rule;
1aeb135f 1748 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
1fa25e41 1749
2e18b38f 1750 chan->dfs_state_entered = jiffies;
c7ab5081
AN
1751 chan->dfs_state = NL80211_DFS_USABLE;
1752
1753 chan->beacon_found = false;
db8dfee5
AN
1754
1755 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1756 chan->flags = chan->orig_flags | bw_flags |
1757 map_regdom_flags(reg_rule->flags);
1758 else
1759 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1760
1fa25e41 1761 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
279f0f55
FF
1762 chan->max_reg_power = chan->max_power =
1763 (int) MBM_TO_DBM(power_rule->max_eirp);
2e18b38f
AN
1764
1765 if (chan->flags & IEEE80211_CHAN_RADAR) {
1766 if (reg_rule->dfs_cac_ms)
1767 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1768 else
1769 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1770 }
1771
1772 chan->max_power = chan->max_reg_power;
1fa25e41
LR
1773}
1774
fdc9d7b2
JB
1775static void handle_band_custom(struct wiphy *wiphy,
1776 struct ieee80211_supported_band *sband,
1fa25e41
LR
1777 const struct ieee80211_regdomain *regd)
1778{
1779 unsigned int i;
1fa25e41 1780
fdc9d7b2
JB
1781 if (!sband)
1782 return;
1fa25e41
LR
1783
1784 for (i = 0; i < sband->n_channels; i++)
fdc9d7b2 1785 handle_channel_custom(wiphy, &sband->channels[i], regd);
1fa25e41
LR
1786}
1787
1788/* Used by drivers prior to wiphy registration */
1789void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1790 const struct ieee80211_regdomain *regd)
1791{
57fbcce3 1792 enum nl80211_band band;
bbcf3f02 1793 unsigned int bands_set = 0;
ac46d48e 1794
a2f73b6c
LR
1795 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
1796 "wiphy should have REGULATORY_CUSTOM_REG\n");
1797 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
222ea581 1798
57fbcce3 1799 for (band = 0; band < NUM_NL80211_BANDS; band++) {
bbcf3f02
LR
1800 if (!wiphy->bands[band])
1801 continue;
fdc9d7b2 1802 handle_band_custom(wiphy, wiphy->bands[band], regd);
bbcf3f02 1803 bands_set++;
b2e1b302 1804 }
bbcf3f02
LR
1805
1806 /*
1807 * no point in calling this if it won't have any effect
1a919318 1808 * on your device's supported bands.
bbcf3f02
LR
1809 */
1810 WARN_ON(!bands_set);
b2e1b302 1811}
1fa25e41
LR
1812EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1813
b2e253cf
LR
1814static void reg_set_request_processed(void)
1815{
1816 bool need_more_processing = false;
c492db37 1817 struct regulatory_request *lr = get_last_request();
b2e253cf 1818
c492db37 1819 lr->processed = true;
b2e253cf
LR
1820
1821 spin_lock(&reg_requests_lock);
1822 if (!list_empty(&reg_requests_list))
1823 need_more_processing = true;
1824 spin_unlock(&reg_requests_lock);
1825
b6863036 1826 cancel_crda_timeout();
a90c7a31 1827
b2e253cf
LR
1828 if (need_more_processing)
1829 schedule_work(&reg_work);
1830}
1831
b3eb7f3f
LR
1832/**
1833 * reg_process_hint_core - process core regulatory requests
1834 * @pending_request: a pending core regulatory request
1835 *
1836 * The wireless subsystem can use this function to process
1837 * a regulatory request issued by the regulatory core.
b3eb7f3f 1838 */
d34265a3
JB
1839static enum reg_request_treatment
1840reg_process_hint_core(struct regulatory_request *core_request)
b3eb7f3f 1841{
cecbb069 1842 if (reg_query_database(core_request)) {
25b20dbd
JB
1843 core_request->intersect = false;
1844 core_request->processed = false;
1845 reg_update_last_request(core_request);
d34265a3 1846 return REG_REQ_OK;
25b20dbd 1847 }
d34265a3
JB
1848
1849 return REG_REQ_IGNORE;
b3eb7f3f
LR
1850}
1851
0d97a619
LR
1852static enum reg_request_treatment
1853__reg_process_hint_user(struct regulatory_request *user_request)
1854{
1855 struct regulatory_request *lr = get_last_request();
1856
1857 if (reg_request_cell_base(user_request))
1858 return reg_ignore_cell_hint(user_request);
1859
1860 if (reg_request_cell_base(lr))
1861 return REG_REQ_IGNORE;
1862
1863 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1864 return REG_REQ_INTERSECT;
1865 /*
1866 * If the user knows better the user should set the regdom
1867 * to their country before the IE is picked up
1868 */
1869 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
1870 lr->intersect)
1871 return REG_REQ_IGNORE;
1872 /*
1873 * Process user requests only after previous user/driver/core
1874 * requests have been processed
1875 */
1876 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
1877 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1878 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
1879 regdom_changes(lr->alpha2))
1880 return REG_REQ_IGNORE;
1881
1882 if (!regdom_changes(user_request->alpha2))
1883 return REG_REQ_ALREADY_SET;
1884
1885 return REG_REQ_OK;
1886}
1887
1888/**
1889 * reg_process_hint_user - process user regulatory requests
1890 * @user_request: a pending user regulatory request
1891 *
1892 * The wireless subsystem can use this function to process
1893 * a regulatory request initiated by userspace.
0d97a619 1894 */
d34265a3
JB
1895static enum reg_request_treatment
1896reg_process_hint_user(struct regulatory_request *user_request)
0d97a619
LR
1897{
1898 enum reg_request_treatment treatment;
0d97a619
LR
1899
1900 treatment = __reg_process_hint_user(user_request);
1901 if (treatment == REG_REQ_IGNORE ||
d34265a3
JB
1902 treatment == REG_REQ_ALREADY_SET)
1903 return REG_REQ_IGNORE;
0d97a619 1904
0d97a619
LR
1905 user_request->intersect = treatment == REG_REQ_INTERSECT;
1906 user_request->processed = false;
5ad6ef5e 1907
cecbb069 1908 if (reg_query_database(user_request)) {
25b20dbd
JB
1909 reg_update_last_request(user_request);
1910 user_alpha2[0] = user_request->alpha2[0];
1911 user_alpha2[1] = user_request->alpha2[1];
d34265a3 1912 return REG_REQ_OK;
25b20dbd 1913 }
d34265a3
JB
1914
1915 return REG_REQ_IGNORE;
0d97a619
LR
1916}
1917
21636c7f
LR
1918static enum reg_request_treatment
1919__reg_process_hint_driver(struct regulatory_request *driver_request)
1920{
1921 struct regulatory_request *lr = get_last_request();
1922
1923 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
1924 if (regdom_changes(driver_request->alpha2))
1925 return REG_REQ_OK;
1926 return REG_REQ_ALREADY_SET;
1927 }
1928
1929 /*
1930 * This would happen if you unplug and plug your card
1931 * back in or if you add a new device for which the previously
1932 * loaded card also agrees on the regulatory domain.
1933 */
1934 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1935 !regdom_changes(driver_request->alpha2))
1936 return REG_REQ_ALREADY_SET;
1937
1938 return REG_REQ_INTERSECT;
1939}
1940
1941/**
1942 * reg_process_hint_driver - process driver regulatory requests
1943 * @driver_request: a pending driver regulatory request
1944 *
1945 * The wireless subsystem can use this function to process
1946 * a regulatory request issued by an 802.11 driver.
1947 *
1948 * Returns one of the different reg request treatment values.
1949 */
1950static enum reg_request_treatment
1951reg_process_hint_driver(struct wiphy *wiphy,
1952 struct regulatory_request *driver_request)
1953{
34f05f54 1954 const struct ieee80211_regdomain *regd, *tmp;
21636c7f 1955 enum reg_request_treatment treatment;
21636c7f
LR
1956
1957 treatment = __reg_process_hint_driver(driver_request);
1958
1959 switch (treatment) {
1960 case REG_REQ_OK:
1961 break;
1962 case REG_REQ_IGNORE:
d34265a3 1963 return REG_REQ_IGNORE;
21636c7f 1964 case REG_REQ_INTERSECT:
21636c7f
LR
1965 case REG_REQ_ALREADY_SET:
1966 regd = reg_copy_regd(get_cfg80211_regdom());
d34265a3
JB
1967 if (IS_ERR(regd))
1968 return REG_REQ_IGNORE;
34f05f54
AN
1969
1970 tmp = get_wiphy_regdom(wiphy);
21636c7f 1971 rcu_assign_pointer(wiphy->regd, regd);
34f05f54 1972 rcu_free_regdom(tmp);
21636c7f
LR
1973 }
1974
21636c7f
LR
1975
1976 driver_request->intersect = treatment == REG_REQ_INTERSECT;
1977 driver_request->processed = false;
5ad6ef5e 1978
21636c7f
LR
1979 /*
1980 * Since CRDA will not be called in this case as we already
1981 * have applied the requested regulatory domain before we just
1982 * inform userspace we have processed the request
1983 */
1984 if (treatment == REG_REQ_ALREADY_SET) {
1985 nl80211_send_reg_change_event(driver_request);
25b20dbd 1986 reg_update_last_request(driver_request);
21636c7f 1987 reg_set_request_processed();
480908a7 1988 return REG_REQ_ALREADY_SET;
21636c7f
LR
1989 }
1990
d34265a3 1991 if (reg_query_database(driver_request)) {
25b20dbd 1992 reg_update_last_request(driver_request);
d34265a3
JB
1993 return REG_REQ_OK;
1994 }
25b20dbd 1995
d34265a3 1996 return REG_REQ_IGNORE;
21636c7f
LR
1997}
1998
b23e7a9e
LR
1999static enum reg_request_treatment
2000__reg_process_hint_country_ie(struct wiphy *wiphy,
2001 struct regulatory_request *country_ie_request)
2002{
2003 struct wiphy *last_wiphy = NULL;
2004 struct regulatory_request *lr = get_last_request();
2005
2006 if (reg_request_cell_base(lr)) {
2007 /* Trust a Cell base station over the AP's country IE */
2008 if (regdom_changes(country_ie_request->alpha2))
2009 return REG_REQ_IGNORE;
2010 return REG_REQ_ALREADY_SET;
2a901468
LR
2011 } else {
2012 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
2013 return REG_REQ_IGNORE;
b23e7a9e
LR
2014 }
2015
b23e7a9e
LR
2016 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
2017 return -EINVAL;
2f1c6c57
LR
2018
2019 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
2020 return REG_REQ_OK;
2021
2022 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
2023
2024 if (last_wiphy != wiphy) {
b23e7a9e 2025 /*
2f1c6c57
LR
2026 * Two cards with two APs claiming different
2027 * Country IE alpha2s. We could
2028 * intersect them, but that seems unlikely
2029 * to be correct. Reject second one for now.
b23e7a9e 2030 */
2f1c6c57
LR
2031 if (regdom_changes(country_ie_request->alpha2))
2032 return REG_REQ_IGNORE;
b23e7a9e
LR
2033 return REG_REQ_ALREADY_SET;
2034 }
70dcec5a
EG
2035
2036 if (regdom_changes(country_ie_request->alpha2))
2f1c6c57
LR
2037 return REG_REQ_OK;
2038 return REG_REQ_ALREADY_SET;
b23e7a9e
LR
2039}
2040
d1c96a9a 2041/**
b23e7a9e
LR
2042 * reg_process_hint_country_ie - process regulatory requests from country IEs
2043 * @country_ie_request: a regulatory request from a country IE
d1c96a9a 2044 *
b23e7a9e
LR
2045 * The wireless subsystem can use this function to process
2046 * a regulatory request issued by a country Information Element.
d1c96a9a 2047 *
2f92212b 2048 * Returns one of the different reg request treatment values.
d1c96a9a 2049 */
2f92212b 2050static enum reg_request_treatment
b23e7a9e
LR
2051reg_process_hint_country_ie(struct wiphy *wiphy,
2052 struct regulatory_request *country_ie_request)
b2e1b302 2053{
2f92212b 2054 enum reg_request_treatment treatment;
761cf7ec 2055
b23e7a9e 2056 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
9c96477d 2057
2f92212b 2058 switch (treatment) {
2f92212b
JB
2059 case REG_REQ_OK:
2060 break;
b23e7a9e 2061 case REG_REQ_IGNORE:
d34265a3 2062 return REG_REQ_IGNORE;
b23e7a9e 2063 case REG_REQ_ALREADY_SET:
c888393b 2064 reg_free_request(country_ie_request);
480908a7 2065 return REG_REQ_ALREADY_SET;
b23e7a9e 2066 case REG_REQ_INTERSECT:
fb1fc7ad 2067 /*
b23e7a9e
LR
2068 * This doesn't happen yet, not sure we
2069 * ever want to support it for this case.
fb1fc7ad 2070 */
b23e7a9e 2071 WARN_ONCE(1, "Unexpected intersection for country IEs");
d34265a3 2072 return REG_REQ_IGNORE;
3e0c3ff3 2073 }
b2e1b302 2074
b23e7a9e
LR
2075 country_ie_request->intersect = false;
2076 country_ie_request->processed = false;
5ad6ef5e 2077
d34265a3 2078 if (reg_query_database(country_ie_request)) {
25b20dbd 2079 reg_update_last_request(country_ie_request);
d34265a3
JB
2080 return REG_REQ_OK;
2081 }
3e0c3ff3 2082
d34265a3 2083 return REG_REQ_IGNORE;
b2e1b302
LR
2084}
2085
89766727
VT
2086bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2)
2087{
2088 const struct ieee80211_regdomain *wiphy1_regd = NULL;
2089 const struct ieee80211_regdomain *wiphy2_regd = NULL;
2090 const struct ieee80211_regdomain *cfg80211_regd = NULL;
2091 bool dfs_domain_same;
2092
2093 rcu_read_lock();
2094
2095 cfg80211_regd = rcu_dereference(cfg80211_regdomain);
2096 wiphy1_regd = rcu_dereference(wiphy1->regd);
2097 if (!wiphy1_regd)
2098 wiphy1_regd = cfg80211_regd;
2099
2100 wiphy2_regd = rcu_dereference(wiphy2->regd);
2101 if (!wiphy2_regd)
2102 wiphy2_regd = cfg80211_regd;
2103
2104 dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region;
2105
2106 rcu_read_unlock();
2107
2108 return dfs_domain_same;
2109}
2110
2111static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan,
2112 struct ieee80211_channel *src_chan)
2113{
2114 if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) ||
2115 !(src_chan->flags & IEEE80211_CHAN_RADAR))
2116 return;
2117
2118 if (dst_chan->flags & IEEE80211_CHAN_DISABLED ||
2119 src_chan->flags & IEEE80211_CHAN_DISABLED)
2120 return;
2121
2122 if (src_chan->center_freq == dst_chan->center_freq &&
2123 dst_chan->dfs_state == NL80211_DFS_USABLE) {
2124 dst_chan->dfs_state = src_chan->dfs_state;
2125 dst_chan->dfs_state_entered = src_chan->dfs_state_entered;
2126 }
2127}
2128
2129static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy,
2130 struct wiphy *src_wiphy)
2131{
2132 struct ieee80211_supported_band *src_sband, *dst_sband;
2133 struct ieee80211_channel *src_chan, *dst_chan;
2134 int i, j, band;
2135
2136 if (!reg_dfs_domain_same(dst_wiphy, src_wiphy))
2137 return;
2138
2139 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2140 dst_sband = dst_wiphy->bands[band];
2141 src_sband = src_wiphy->bands[band];
2142 if (!dst_sband || !src_sband)
2143 continue;
2144
2145 for (i = 0; i < dst_sband->n_channels; i++) {
2146 dst_chan = &dst_sband->channels[i];
2147 for (j = 0; j < src_sband->n_channels; j++) {
2148 src_chan = &src_sband->channels[j];
2149 reg_copy_dfs_chan_state(dst_chan, src_chan);
2150 }
2151 }
2152 }
2153}
2154
2155static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy)
2156{
2157 struct cfg80211_registered_device *rdev;
2158
2159 ASSERT_RTNL();
2160
2161 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2162 if (wiphy == &rdev->wiphy)
2163 continue;
2164 wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy);
2165 }
2166}
2167
30a548c7 2168/* This processes *all* regulatory hints */
1daa37c7 2169static void reg_process_hint(struct regulatory_request *reg_request)
fe33eb39 2170{
fe33eb39 2171 struct wiphy *wiphy = NULL;
b3eb7f3f 2172 enum reg_request_treatment treatment;
492a8131 2173 enum nl80211_reg_initiator initiator = reg_request->initiator;
fe33eb39 2174
f4173766 2175 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
fe33eb39
LR
2176 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
2177
492a8131 2178 switch (initiator) {
b3eb7f3f 2179 case NL80211_REGDOM_SET_BY_CORE:
d34265a3
JB
2180 treatment = reg_process_hint_core(reg_request);
2181 break;
b3eb7f3f 2182 case NL80211_REGDOM_SET_BY_USER:
d34265a3
JB
2183 treatment = reg_process_hint_user(reg_request);
2184 break;
b3eb7f3f 2185 case NL80211_REGDOM_SET_BY_DRIVER:
772f0389
IP
2186 if (!wiphy)
2187 goto out_free;
21636c7f
LR
2188 treatment = reg_process_hint_driver(wiphy, reg_request);
2189 break;
b3eb7f3f 2190 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
772f0389
IP
2191 if (!wiphy)
2192 goto out_free;
b23e7a9e 2193 treatment = reg_process_hint_country_ie(wiphy, reg_request);
b3eb7f3f
LR
2194 break;
2195 default:
492a8131 2196 WARN(1, "invalid initiator %d\n", initiator);
772f0389 2197 goto out_free;
b3eb7f3f
LR
2198 }
2199
d34265a3
JB
2200 if (treatment == REG_REQ_IGNORE)
2201 goto out_free;
2202
480908a7
JB
2203 WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET,
2204 "unexpected treatment value %d\n", treatment);
2205
841b351c
JL
2206 /* This is required so that the orig_* parameters are saved.
2207 * NOTE: treatment must be set for any case that reaches here!
2208 */
b23e7a9e 2209 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
ad932f04 2210 wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
492a8131 2211 wiphy_update_regulatory(wiphy, initiator);
89766727 2212 wiphy_all_share_dfs_chan_state(wiphy);
ad932f04
AN
2213 reg_check_channels();
2214 }
772f0389
IP
2215
2216 return;
2217
2218out_free:
c888393b 2219 reg_free_request(reg_request);
fe33eb39
LR
2220}
2221
ef51fb1d
AN
2222static bool reg_only_self_managed_wiphys(void)
2223{
2224 struct cfg80211_registered_device *rdev;
2225 struct wiphy *wiphy;
2226 bool self_managed_found = false;
2227
2228 ASSERT_RTNL();
2229
2230 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2231 wiphy = &rdev->wiphy;
2232 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2233 self_managed_found = true;
2234 else
2235 return false;
2236 }
2237
2238 /* make sure at least one self-managed wiphy exists */
2239 return self_managed_found;
2240}
2241
b2e253cf
LR
2242/*
2243 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
2244 * Regulatory hints come on a first come first serve basis and we
2245 * must process each one atomically.
2246 */
fe33eb39 2247static void reg_process_pending_hints(void)
b0e2880b 2248{
c492db37 2249 struct regulatory_request *reg_request, *lr;
fe33eb39 2250
c492db37 2251 lr = get_last_request();
b0e2880b 2252
b2e253cf 2253 /* When last_request->processed becomes true this will be rescheduled */
c492db37 2254 if (lr && !lr->processed) {
96cce12f 2255 reg_process_hint(lr);
5fe231e8 2256 return;
b2e253cf
LR
2257 }
2258
fe33eb39 2259 spin_lock(&reg_requests_lock);
fe33eb39 2260
b2e253cf 2261 if (list_empty(&reg_requests_list)) {
d951c1dd 2262 spin_unlock(&reg_requests_lock);
5fe231e8 2263 return;
fe33eb39 2264 }
b2e253cf
LR
2265
2266 reg_request = list_first_entry(&reg_requests_list,
2267 struct regulatory_request,
2268 list);
2269 list_del_init(&reg_request->list);
2270
fe33eb39 2271 spin_unlock(&reg_requests_lock);
b0e2880b 2272
ef51fb1d
AN
2273 if (reg_only_self_managed_wiphys()) {
2274 reg_free_request(reg_request);
2275 return;
2276 }
2277
1daa37c7 2278 reg_process_hint(reg_request);
2e54a689
B
2279
2280 lr = get_last_request();
2281
2282 spin_lock(&reg_requests_lock);
2283 if (!list_empty(&reg_requests_list) && lr && lr->processed)
2284 schedule_work(&reg_work);
2285 spin_unlock(&reg_requests_lock);
fe33eb39
LR
2286}
2287
e38f8a7a
LR
2288/* Processes beacon hints -- this has nothing to do with country IEs */
2289static void reg_process_pending_beacon_hints(void)
2290{
79c97e97 2291 struct cfg80211_registered_device *rdev;
e38f8a7a
LR
2292 struct reg_beacon *pending_beacon, *tmp;
2293
e38f8a7a
LR
2294 /* This goes through the _pending_ beacon list */
2295 spin_lock_bh(&reg_pending_beacons_lock);
2296
e38f8a7a
LR
2297 list_for_each_entry_safe(pending_beacon, tmp,
2298 &reg_pending_beacons, list) {
e38f8a7a
LR
2299 list_del_init(&pending_beacon->list);
2300
2301 /* Applies the beacon hint to current wiphys */
79c97e97
JB
2302 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2303 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
e38f8a7a
LR
2304
2305 /* Remembers the beacon hint for new wiphys or reg changes */
2306 list_add_tail(&pending_beacon->list, &reg_beacon_list);
2307 }
2308
2309 spin_unlock_bh(&reg_pending_beacons_lock);
e38f8a7a
LR
2310}
2311
b0d7aa59
JD
2312static void reg_process_self_managed_hints(void)
2313{
2314 struct cfg80211_registered_device *rdev;
2315 struct wiphy *wiphy;
2316 const struct ieee80211_regdomain *tmp;
2317 const struct ieee80211_regdomain *regd;
57fbcce3 2318 enum nl80211_band band;
b0d7aa59
JD
2319 struct regulatory_request request = {};
2320
2321 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2322 wiphy = &rdev->wiphy;
2323
2324 spin_lock(&reg_requests_lock);
2325 regd = rdev->requested_regd;
2326 rdev->requested_regd = NULL;
2327 spin_unlock(&reg_requests_lock);
2328
2329 if (regd == NULL)
2330 continue;
2331
2332 tmp = get_wiphy_regdom(wiphy);
2333 rcu_assign_pointer(wiphy->regd, regd);
2334 rcu_free_regdom(tmp);
2335
57fbcce3 2336 for (band = 0; band < NUM_NL80211_BANDS; band++)
b0d7aa59
JD
2337 handle_band_custom(wiphy, wiphy->bands[band], regd);
2338
2339 reg_process_ht_flags(wiphy);
2340
2341 request.wiphy_idx = get_wiphy_idx(wiphy);
2342 request.alpha2[0] = regd->alpha2[0];
2343 request.alpha2[1] = regd->alpha2[1];
2344 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
2345
2346 nl80211_send_wiphy_reg_change_event(&request);
2347 }
2348
2349 reg_check_channels();
2350}
2351
fe33eb39
LR
2352static void reg_todo(struct work_struct *work)
2353{
5fe231e8 2354 rtnl_lock();
fe33eb39 2355 reg_process_pending_hints();
e38f8a7a 2356 reg_process_pending_beacon_hints();
b0d7aa59 2357 reg_process_self_managed_hints();
5fe231e8 2358 rtnl_unlock();
fe33eb39
LR
2359}
2360
fe33eb39
LR
2361static void queue_regulatory_request(struct regulatory_request *request)
2362{
d4f2c881
JB
2363 request->alpha2[0] = toupper(request->alpha2[0]);
2364 request->alpha2[1] = toupper(request->alpha2[1]);
c61029c7 2365
fe33eb39
LR
2366 spin_lock(&reg_requests_lock);
2367 list_add_tail(&request->list, &reg_requests_list);
2368 spin_unlock(&reg_requests_lock);
2369
2370 schedule_work(&reg_work);
2371}
2372
09d989d1
LR
2373/*
2374 * Core regulatory hint -- happens during cfg80211_init()
2375 * and when we restore regulatory settings.
2376 */
ba25c141
LR
2377static int regulatory_hint_core(const char *alpha2)
2378{
2379 struct regulatory_request *request;
2380
1a919318 2381 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
ba25c141
LR
2382 if (!request)
2383 return -ENOMEM;
2384
2385 request->alpha2[0] = alpha2[0];
2386 request->alpha2[1] = alpha2[1];
7db90f4a 2387 request->initiator = NL80211_REGDOM_SET_BY_CORE;
d46c334f 2388 request->wiphy_idx = WIPHY_IDX_INVALID;
ba25c141 2389
31e99729 2390 queue_regulatory_request(request);
5078b2e3 2391
fe33eb39 2392 return 0;
ba25c141
LR
2393}
2394
fe33eb39 2395/* User hints */
57b5ce07
LR
2396int regulatory_hint_user(const char *alpha2,
2397 enum nl80211_user_reg_hint_type user_reg_hint_type)
b2e1b302 2398{
fe33eb39
LR
2399 struct regulatory_request *request;
2400
fdc9d7b2
JB
2401 if (WARN_ON(!alpha2))
2402 return -EINVAL;
b2e1b302 2403
fe33eb39
LR
2404 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2405 if (!request)
2406 return -ENOMEM;
2407
f4173766 2408 request->wiphy_idx = WIPHY_IDX_INVALID;
fe33eb39
LR
2409 request->alpha2[0] = alpha2[0];
2410 request->alpha2[1] = alpha2[1];
e12822e1 2411 request->initiator = NL80211_REGDOM_SET_BY_USER;
57b5ce07 2412 request->user_reg_hint_type = user_reg_hint_type;
fe33eb39 2413
c37722bd 2414 /* Allow calling CRDA again */
b6863036 2415 reset_crda_timeouts();
c37722bd 2416
fe33eb39
LR
2417 queue_regulatory_request(request);
2418
2419 return 0;
2420}
2421
05050753 2422int regulatory_hint_indoor(bool is_indoor, u32 portid)
52616f2b 2423{
05050753 2424 spin_lock(&reg_indoor_lock);
52616f2b 2425
05050753
I
2426 /* It is possible that more than one user space process is trying to
2427 * configure the indoor setting. To handle such cases, clear the indoor
2428 * setting in case that some process does not think that the device
2429 * is operating in an indoor environment. In addition, if a user space
2430 * process indicates that it is controlling the indoor setting, save its
2431 * portid, i.e., make it the owner.
2432 */
2433 reg_is_indoor = is_indoor;
2434 if (reg_is_indoor) {
2435 if (!reg_is_indoor_portid)
2436 reg_is_indoor_portid = portid;
2437 } else {
2438 reg_is_indoor_portid = 0;
2439 }
52616f2b 2440
05050753 2441 spin_unlock(&reg_indoor_lock);
52616f2b 2442
05050753
I
2443 if (!is_indoor)
2444 reg_check_channels();
52616f2b
IP
2445
2446 return 0;
2447}
2448
05050753
I
2449void regulatory_netlink_notify(u32 portid)
2450{
2451 spin_lock(&reg_indoor_lock);
2452
2453 if (reg_is_indoor_portid != portid) {
2454 spin_unlock(&reg_indoor_lock);
2455 return;
2456 }
2457
2458 reg_is_indoor = false;
2459 reg_is_indoor_portid = 0;
2460
2461 spin_unlock(&reg_indoor_lock);
2462
2463 reg_check_channels();
2464}
2465
fe33eb39
LR
2466/* Driver hints */
2467int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2468{
2469 struct regulatory_request *request;
2470
fdc9d7b2
JB
2471 if (WARN_ON(!alpha2 || !wiphy))
2472 return -EINVAL;
fe33eb39 2473
4f7b9140
LR
2474 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
2475
fe33eb39
LR
2476 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2477 if (!request)
2478 return -ENOMEM;
2479
2480 request->wiphy_idx = get_wiphy_idx(wiphy);
2481
fe33eb39
LR
2482 request->alpha2[0] = alpha2[0];
2483 request->alpha2[1] = alpha2[1];
7db90f4a 2484 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
fe33eb39 2485
c37722bd 2486 /* Allow calling CRDA again */
b6863036 2487 reset_crda_timeouts();
c37722bd 2488
fe33eb39
LR
2489 queue_regulatory_request(request);
2490
2491 return 0;
b2e1b302
LR
2492}
2493EXPORT_SYMBOL(regulatory_hint);
2494
57fbcce3 2495void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band,
789fd033 2496 const u8 *country_ie, u8 country_ie_len)
3f2355cb 2497{
3f2355cb 2498 char alpha2[2];
3f2355cb 2499 enum environment_cap env = ENVIRON_ANY;
db2424c5 2500 struct regulatory_request *request = NULL, *lr;
d335fe63 2501
3f2355cb
LR
2502 /* IE len must be evenly divisible by 2 */
2503 if (country_ie_len & 0x01)
db2424c5 2504 return;
3f2355cb
LR
2505
2506 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
db2424c5
JB
2507 return;
2508
2509 request = kzalloc(sizeof(*request), GFP_KERNEL);
2510 if (!request)
2511 return;
3f2355cb 2512
3f2355cb
LR
2513 alpha2[0] = country_ie[0];
2514 alpha2[1] = country_ie[1];
2515
2516 if (country_ie[2] == 'I')
2517 env = ENVIRON_INDOOR;
2518 else if (country_ie[2] == 'O')
2519 env = ENVIRON_OUTDOOR;
2520
db2424c5
JB
2521 rcu_read_lock();
2522 lr = get_last_request();
2523
2524 if (unlikely(!lr))
2525 goto out;
2526
fb1fc7ad 2527 /*
8b19e6ca 2528 * We will run this only upon a successful connection on cfg80211.
4b44c8bc 2529 * We leave conflict resolution to the workqueue, where can hold
5fe231e8 2530 * the RTNL.
fb1fc7ad 2531 */
c492db37
JB
2532 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2533 lr->wiphy_idx != WIPHY_IDX_INVALID)
4b44c8bc 2534 goto out;
3f2355cb 2535
fe33eb39 2536 request->wiphy_idx = get_wiphy_idx(wiphy);
4f366c5d
JL
2537 request->alpha2[0] = alpha2[0];
2538 request->alpha2[1] = alpha2[1];
7db90f4a 2539 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
fe33eb39
LR
2540 request->country_ie_env = env;
2541
c37722bd 2542 /* Allow calling CRDA again */
b6863036 2543 reset_crda_timeouts();
c37722bd 2544
fe33eb39 2545 queue_regulatory_request(request);
db2424c5 2546 request = NULL;
3f2355cb 2547out:
db2424c5
JB
2548 kfree(request);
2549 rcu_read_unlock();
3f2355cb 2550}
b2e1b302 2551
09d989d1
LR
2552static void restore_alpha2(char *alpha2, bool reset_user)
2553{
2554 /* indicates there is no alpha2 to consider for restoration */
2555 alpha2[0] = '9';
2556 alpha2[1] = '7';
2557
2558 /* The user setting has precedence over the module parameter */
2559 if (is_user_regdom_saved()) {
2560 /* Unless we're asked to ignore it and reset it */
2561 if (reset_user) {
c799ba6e 2562 pr_debug("Restoring regulatory settings including user preference\n");
09d989d1
LR
2563 user_alpha2[0] = '9';
2564 user_alpha2[1] = '7';
2565
2566 /*
2567 * If we're ignoring user settings, we still need to
2568 * check the module parameter to ensure we put things
2569 * back as they were for a full restore.
2570 */
2571 if (!is_world_regdom(ieee80211_regdom)) {
c799ba6e
JB
2572 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2573 ieee80211_regdom[0], ieee80211_regdom[1]);
09d989d1
LR
2574 alpha2[0] = ieee80211_regdom[0];
2575 alpha2[1] = ieee80211_regdom[1];
2576 }
2577 } else {
c799ba6e
JB
2578 pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n",
2579 user_alpha2[0], user_alpha2[1]);
09d989d1
LR
2580 alpha2[0] = user_alpha2[0];
2581 alpha2[1] = user_alpha2[1];
2582 }
2583 } else if (!is_world_regdom(ieee80211_regdom)) {
c799ba6e
JB
2584 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2585 ieee80211_regdom[0], ieee80211_regdom[1]);
09d989d1
LR
2586 alpha2[0] = ieee80211_regdom[0];
2587 alpha2[1] = ieee80211_regdom[1];
2588 } else
c799ba6e 2589 pr_debug("Restoring regulatory settings\n");
09d989d1
LR
2590}
2591
5ce543d1
RM
2592static void restore_custom_reg_settings(struct wiphy *wiphy)
2593{
2594 struct ieee80211_supported_band *sband;
57fbcce3 2595 enum nl80211_band band;
5ce543d1
RM
2596 struct ieee80211_channel *chan;
2597 int i;
2598
57fbcce3 2599 for (band = 0; band < NUM_NL80211_BANDS; band++) {
5ce543d1
RM
2600 sband = wiphy->bands[band];
2601 if (!sband)
2602 continue;
2603 for (i = 0; i < sband->n_channels; i++) {
2604 chan = &sband->channels[i];
2605 chan->flags = chan->orig_flags;
2606 chan->max_antenna_gain = chan->orig_mag;
2607 chan->max_power = chan->orig_mpwr;
899852af 2608 chan->beacon_found = false;
5ce543d1
RM
2609 }
2610 }
2611}
2612
09d989d1
LR
2613/*
2614 * Restoring regulatory settings involves ingoring any
2615 * possibly stale country IE information and user regulatory
2616 * settings if so desired, this includes any beacon hints
2617 * learned as we could have traveled outside to another country
2618 * after disconnection. To restore regulatory settings we do
2619 * exactly what we did at bootup:
2620 *
2621 * - send a core regulatory hint
2622 * - send a user regulatory hint if applicable
2623 *
2624 * Device drivers that send a regulatory hint for a specific country
2625 * keep their own regulatory domain on wiphy->regd so that does does
2626 * not need to be remembered.
2627 */
2628static void restore_regulatory_settings(bool reset_user)
2629{
2630 char alpha2[2];
cee0bec5 2631 char world_alpha2[2];
09d989d1 2632 struct reg_beacon *reg_beacon, *btmp;
14609555 2633 LIST_HEAD(tmp_reg_req_list);
5ce543d1 2634 struct cfg80211_registered_device *rdev;
09d989d1 2635
5fe231e8
JB
2636 ASSERT_RTNL();
2637
05050753
I
2638 /*
2639 * Clear the indoor setting in case that it is not controlled by user
2640 * space, as otherwise there is no guarantee that the device is still
2641 * operating in an indoor environment.
2642 */
2643 spin_lock(&reg_indoor_lock);
2644 if (reg_is_indoor && !reg_is_indoor_portid) {
2645 reg_is_indoor = false;
2646 reg_check_channels();
2647 }
2648 spin_unlock(&reg_indoor_lock);
52616f2b 2649
2d319867 2650 reset_regdomains(true, &world_regdom);
09d989d1
LR
2651 restore_alpha2(alpha2, reset_user);
2652
14609555
LR
2653 /*
2654 * If there's any pending requests we simply
2655 * stash them to a temporary pending queue and
2656 * add then after we've restored regulatory
2657 * settings.
2658 */
2659 spin_lock(&reg_requests_lock);
eeca9fce 2660 list_splice_tail_init(&reg_requests_list, &tmp_reg_req_list);
14609555
LR
2661 spin_unlock(&reg_requests_lock);
2662
09d989d1
LR
2663 /* Clear beacon hints */
2664 spin_lock_bh(&reg_pending_beacons_lock);
fea9bced
JB
2665 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2666 list_del(&reg_beacon->list);
2667 kfree(reg_beacon);
09d989d1
LR
2668 }
2669 spin_unlock_bh(&reg_pending_beacons_lock);
2670
fea9bced
JB
2671 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2672 list_del(&reg_beacon->list);
2673 kfree(reg_beacon);
09d989d1
LR
2674 }
2675
2676 /* First restore to the basic regulatory settings */
379b82f4
JB
2677 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
2678 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
09d989d1 2679
5ce543d1 2680 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
b0d7aa59
JD
2681 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2682 continue;
a2f73b6c 2683 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
5ce543d1
RM
2684 restore_custom_reg_settings(&rdev->wiphy);
2685 }
2686
cee0bec5 2687 regulatory_hint_core(world_alpha2);
09d989d1
LR
2688
2689 /*
2690 * This restores the ieee80211_regdom module parameter
2691 * preference or the last user requested regulatory
2692 * settings, user regulatory settings takes precedence.
2693 */
2694 if (is_an_alpha2(alpha2))
549cc1c5 2695 regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER);
09d989d1 2696
14609555 2697 spin_lock(&reg_requests_lock);
11cff96c 2698 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
14609555
LR
2699 spin_unlock(&reg_requests_lock);
2700
c799ba6e 2701 pr_debug("Kicking the queue\n");
14609555
LR
2702
2703 schedule_work(&reg_work);
2704}
09d989d1
LR
2705
2706void regulatory_hint_disconnect(void)
2707{
c799ba6e 2708 pr_debug("All devices are disconnected, going to restore regulatory settings\n");
09d989d1
LR
2709 restore_regulatory_settings(false);
2710}
2711
e38f8a7a
LR
2712static bool freq_is_chan_12_13_14(u16 freq)
2713{
57fbcce3
JB
2714 if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) ||
2715 freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) ||
2716 freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ))
e38f8a7a
LR
2717 return true;
2718 return false;
2719}
2720
3ebfa6e7
LR
2721static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
2722{
2723 struct reg_beacon *pending_beacon;
2724
2725 list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
2726 if (beacon_chan->center_freq ==
2727 pending_beacon->chan.center_freq)
2728 return true;
2729 return false;
2730}
2731
e38f8a7a
LR
2732int regulatory_hint_found_beacon(struct wiphy *wiphy,
2733 struct ieee80211_channel *beacon_chan,
2734 gfp_t gfp)
2735{
2736 struct reg_beacon *reg_beacon;
3ebfa6e7 2737 bool processing;
e38f8a7a 2738
1a919318
JB
2739 if (beacon_chan->beacon_found ||
2740 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
57fbcce3 2741 (beacon_chan->band == NL80211_BAND_2GHZ &&
1a919318 2742 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
e38f8a7a
LR
2743 return 0;
2744
3ebfa6e7
LR
2745 spin_lock_bh(&reg_pending_beacons_lock);
2746 processing = pending_reg_beacon(beacon_chan);
2747 spin_unlock_bh(&reg_pending_beacons_lock);
2748
2749 if (processing)
e38f8a7a
LR
2750 return 0;
2751
2752 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2753 if (!reg_beacon)
2754 return -ENOMEM;
2755
c799ba6e
JB
2756 pr_debug("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
2757 beacon_chan->center_freq,
2758 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2759 wiphy_name(wiphy));
4113f751 2760
e38f8a7a 2761 memcpy(&reg_beacon->chan, beacon_chan,
1a919318 2762 sizeof(struct ieee80211_channel));
e38f8a7a
LR
2763
2764 /*
2765 * Since we can be called from BH or and non-BH context
2766 * we must use spin_lock_bh()
2767 */
2768 spin_lock_bh(&reg_pending_beacons_lock);
2769 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2770 spin_unlock_bh(&reg_pending_beacons_lock);
2771
2772 schedule_work(&reg_work);
2773
2774 return 0;
2775}
2776
a3d2eaf0 2777static void print_rd_rules(const struct ieee80211_regdomain *rd)
b2e1b302
LR
2778{
2779 unsigned int i;
a3d2eaf0
JB
2780 const struct ieee80211_reg_rule *reg_rule = NULL;
2781 const struct ieee80211_freq_range *freq_range = NULL;
2782 const struct ieee80211_power_rule *power_rule = NULL;
089027e5 2783 char bw[32], cac_time[32];
b2e1b302 2784
94c4fd64 2785 pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
b2e1b302
LR
2786
2787 for (i = 0; i < rd->n_reg_rules; i++) {
2788 reg_rule = &rd->reg_rules[i];
2789 freq_range = &reg_rule->freq_range;
2790 power_rule = &reg_rule->power_rule;
2791
b0dfd2ea
JD
2792 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
2793 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
2794 freq_range->max_bandwidth_khz,
97524820
JD
2795 reg_get_max_bandwidth(rd, reg_rule));
2796 else
b0dfd2ea 2797 snprintf(bw, sizeof(bw), "%d KHz",
97524820
JD
2798 freq_range->max_bandwidth_khz);
2799
089027e5
JD
2800 if (reg_rule->flags & NL80211_RRF_DFS)
2801 scnprintf(cac_time, sizeof(cac_time), "%u s",
2802 reg_rule->dfs_cac_ms/1000);
2803 else
2804 scnprintf(cac_time, sizeof(cac_time), "N/A");
2805
2806
fb1fc7ad
LR
2807 /*
2808 * There may not be documentation for max antenna gain
2809 * in certain regions
2810 */
b2e1b302 2811 if (power_rule->max_antenna_gain)
94c4fd64 2812 pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
b2e1b302
LR
2813 freq_range->start_freq_khz,
2814 freq_range->end_freq_khz,
97524820 2815 bw,
b2e1b302 2816 power_rule->max_antenna_gain,
089027e5
JD
2817 power_rule->max_eirp,
2818 cac_time);
b2e1b302 2819 else
94c4fd64 2820 pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
b2e1b302
LR
2821 freq_range->start_freq_khz,
2822 freq_range->end_freq_khz,
97524820 2823 bw,
089027e5
JD
2824 power_rule->max_eirp,
2825 cac_time);
b2e1b302
LR
2826 }
2827}
2828
4c7d3982 2829bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
8b60b078
LR
2830{
2831 switch (dfs_region) {
2832 case NL80211_DFS_UNSET:
2833 case NL80211_DFS_FCC:
2834 case NL80211_DFS_ETSI:
2835 case NL80211_DFS_JP:
2836 return true;
2837 default:
c799ba6e 2838 pr_debug("Ignoring uknown DFS master region: %d\n", dfs_region);
8b60b078
LR
2839 return false;
2840 }
2841}
2842
a3d2eaf0 2843static void print_regdomain(const struct ieee80211_regdomain *rd)
b2e1b302 2844{
c492db37 2845 struct regulatory_request *lr = get_last_request();
b2e1b302 2846
3f2355cb 2847 if (is_intersected_alpha2(rd->alpha2)) {
c492db37 2848 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
79c97e97 2849 struct cfg80211_registered_device *rdev;
c492db37 2850 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
79c97e97 2851 if (rdev) {
94c4fd64 2852 pr_debug("Current regulatory domain updated by AP to: %c%c\n",
79c97e97
JB
2853 rdev->country_ie_alpha2[0],
2854 rdev->country_ie_alpha2[1]);
3f2355cb 2855 } else
94c4fd64 2856 pr_debug("Current regulatory domain intersected:\n");
3f2355cb 2857 } else
94c4fd64 2858 pr_debug("Current regulatory domain intersected:\n");
1a919318 2859 } else if (is_world_regdom(rd->alpha2)) {
94c4fd64 2860 pr_debug("World regulatory domain updated:\n");
1a919318 2861 } else {
b2e1b302 2862 if (is_unknown_alpha2(rd->alpha2))
94c4fd64 2863 pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n");
57b5ce07 2864 else {
c492db37 2865 if (reg_request_cell_base(lr))
94c4fd64 2866 pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n",
57b5ce07
LR
2867 rd->alpha2[0], rd->alpha2[1]);
2868 else
94c4fd64 2869 pr_debug("Regulatory domain changed to country: %c%c\n",
57b5ce07
LR
2870 rd->alpha2[0], rd->alpha2[1]);
2871 }
b2e1b302 2872 }
1a919318 2873
94c4fd64 2874 pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
b2e1b302
LR
2875 print_rd_rules(rd);
2876}
2877
2df78167 2878static void print_regdomain_info(const struct ieee80211_regdomain *rd)
b2e1b302 2879{
94c4fd64 2880 pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
b2e1b302
LR
2881 print_rd_rules(rd);
2882}
2883
3b9e5aca
LR
2884static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
2885{
2886 if (!is_world_regdom(rd->alpha2))
2887 return -EINVAL;
2888 update_world_regdomain(rd);
2889 return 0;
2890}
2891
84721d44
LR
2892static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
2893 struct regulatory_request *user_request)
2894{
2895 const struct ieee80211_regdomain *intersected_rd = NULL;
2896
84721d44
LR
2897 if (!regdom_changes(rd->alpha2))
2898 return -EALREADY;
2899
2900 if (!is_valid_rd(rd)) {
94c4fd64
DY
2901 pr_err("Invalid regulatory domain detected: %c%c\n",
2902 rd->alpha2[0], rd->alpha2[1]);
84721d44
LR
2903 print_regdomain_info(rd);
2904 return -EINVAL;
2905 }
2906
2907 if (!user_request->intersect) {
2908 reset_regdomains(false, rd);
2909 return 0;
2910 }
2911
2912 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2913 if (!intersected_rd)
2914 return -EINVAL;
2915
2916 kfree(rd);
2917 rd = NULL;
2918 reset_regdomains(false, intersected_rd);
2919
2920 return 0;
2921}
2922
f5fe3247
LR
2923static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
2924 struct regulatory_request *driver_request)
b2e1b302 2925{
e9763c3c 2926 const struct ieee80211_regdomain *regd;
9c96477d 2927 const struct ieee80211_regdomain *intersected_rd = NULL;
f5fe3247 2928 const struct ieee80211_regdomain *tmp;
806a9e39 2929 struct wiphy *request_wiphy;
6913b49a 2930
f5fe3247 2931 if (is_world_regdom(rd->alpha2))
b2e1b302
LR
2932 return -EINVAL;
2933
f5fe3247
LR
2934 if (!regdom_changes(rd->alpha2))
2935 return -EALREADY;
b2e1b302 2936
8375af3b 2937 if (!is_valid_rd(rd)) {
94c4fd64
DY
2938 pr_err("Invalid regulatory domain detected: %c%c\n",
2939 rd->alpha2[0], rd->alpha2[1]);
8375af3b
LR
2940 print_regdomain_info(rd);
2941 return -EINVAL;
b2e1b302
LR
2942 }
2943
f5fe3247 2944 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
922ec58c 2945 if (!request_wiphy)
de3584bd 2946 return -ENODEV;
806a9e39 2947
f5fe3247 2948 if (!driver_request->intersect) {
558f6d32
LR
2949 if (request_wiphy->regd)
2950 return -EALREADY;
3e0c3ff3 2951
e9763c3c
JB
2952 regd = reg_copy_regd(rd);
2953 if (IS_ERR(regd))
2954 return PTR_ERR(regd);
3e0c3ff3 2955
458f4f9e 2956 rcu_assign_pointer(request_wiphy->regd, regd);
379b82f4 2957 reset_regdomains(false, rd);
b8295acd
LR
2958 return 0;
2959 }
2960
f5fe3247
LR
2961 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2962 if (!intersected_rd)
2963 return -EINVAL;
b8295acd 2964
f5fe3247
LR
2965 /*
2966 * We can trash what CRDA provided now.
2967 * However if a driver requested this specific regulatory
2968 * domain we keep it for its private use
2969 */
2970 tmp = get_wiphy_regdom(request_wiphy);
2971 rcu_assign_pointer(request_wiphy->regd, rd);
2972 rcu_free_regdom(tmp);
b8295acd 2973
f5fe3247 2974 rd = NULL;
b7566fc3 2975
f5fe3247 2976 reset_regdomains(false, intersected_rd);
3e0c3ff3 2977
f5fe3247
LR
2978 return 0;
2979}
2980
01992406
LR
2981static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
2982 struct regulatory_request *country_ie_request)
f5fe3247
LR
2983{
2984 struct wiphy *request_wiphy;
b8295acd 2985
f5fe3247
LR
2986 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2987 !is_unknown_alpha2(rd->alpha2))
2988 return -EINVAL;
b8295acd 2989
f5fe3247
LR
2990 /*
2991 * Lets only bother proceeding on the same alpha2 if the current
2992 * rd is non static (it means CRDA was present and was used last)
2993 * and the pending request came in from a country IE
2994 */
2995
2996 if (!is_valid_rd(rd)) {
94c4fd64
DY
2997 pr_err("Invalid regulatory domain detected: %c%c\n",
2998 rd->alpha2[0], rd->alpha2[1]);
f5fe3247
LR
2999 print_regdomain_info(rd);
3000 return -EINVAL;
9c96477d
LR
3001 }
3002
01992406 3003 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
922ec58c 3004 if (!request_wiphy)
f5fe3247 3005 return -ENODEV;
b2e1b302 3006
01992406 3007 if (country_ie_request->intersect)
f5fe3247
LR
3008 return -EINVAL;
3009
3010 reset_regdomains(false, rd);
3011 return 0;
3012}
b2e1b302 3013
fb1fc7ad
LR
3014/*
3015 * Use this call to set the current regulatory domain. Conflicts with
b2e1b302 3016 * multiple drivers can be ironed out later. Caller must've already
458f4f9e 3017 * kmalloc'd the rd structure.
fb1fc7ad 3018 */
c37722bd
I
3019int set_regdom(const struct ieee80211_regdomain *rd,
3020 enum ieee80211_regd_source regd_src)
b2e1b302 3021{
c492db37 3022 struct regulatory_request *lr;
092008ab 3023 bool user_reset = false;
b2e1b302
LR
3024 int r;
3025
3b9e5aca
LR
3026 if (!reg_is_valid_request(rd->alpha2)) {
3027 kfree(rd);
3028 return -EINVAL;
3029 }
3030
c37722bd 3031 if (regd_src == REGD_SOURCE_CRDA)
b6863036 3032 reset_crda_timeouts();
c37722bd 3033
c492db37 3034 lr = get_last_request();
abc7381b 3035
b2e1b302 3036 /* Note that this doesn't update the wiphys, this is done below */
3b9e5aca
LR
3037 switch (lr->initiator) {
3038 case NL80211_REGDOM_SET_BY_CORE:
3039 r = reg_set_rd_core(rd);
3040 break;
3041 case NL80211_REGDOM_SET_BY_USER:
84721d44 3042 r = reg_set_rd_user(rd, lr);
092008ab 3043 user_reset = true;
84721d44 3044 break;
3b9e5aca 3045 case NL80211_REGDOM_SET_BY_DRIVER:
f5fe3247
LR
3046 r = reg_set_rd_driver(rd, lr);
3047 break;
3b9e5aca 3048 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
01992406 3049 r = reg_set_rd_country_ie(rd, lr);
3b9e5aca
LR
3050 break;
3051 default:
3052 WARN(1, "invalid initiator %d\n", lr->initiator);
09d11800 3053 kfree(rd);
3b9e5aca
LR
3054 return -EINVAL;
3055 }
3056
d2372b31 3057 if (r) {
092008ab
JD
3058 switch (r) {
3059 case -EALREADY:
95908535 3060 reg_set_request_processed();
092008ab
JD
3061 break;
3062 default:
3063 /* Back to world regulatory in case of errors */
3064 restore_regulatory_settings(user_reset);
3065 }
95908535 3066
d2372b31 3067 kfree(rd);
38fd2143 3068 return r;
d2372b31 3069 }
b2e1b302 3070
b2e1b302 3071 /* This would make this whole thing pointless */
38fd2143
JB
3072 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
3073 return -EINVAL;
b2e1b302
LR
3074
3075 /* update all wiphys now with the new established regulatory domain */
c492db37 3076 update_all_wiphy_regulatory(lr->initiator);
b2e1b302 3077
458f4f9e 3078 print_regdomain(get_cfg80211_regdom());
b2e1b302 3079
c492db37 3080 nl80211_send_reg_change_event(lr);
73d54c9e 3081
b2e253cf
LR
3082 reg_set_request_processed();
3083
38fd2143 3084 return 0;
b2e1b302
LR
3085}
3086
2c3e861c
AN
3087static int __regulatory_set_wiphy_regd(struct wiphy *wiphy,
3088 struct ieee80211_regdomain *rd)
b0d7aa59
JD
3089{
3090 const struct ieee80211_regdomain *regd;
3091 const struct ieee80211_regdomain *prev_regd;
3092 struct cfg80211_registered_device *rdev;
3093
3094 if (WARN_ON(!wiphy || !rd))
3095 return -EINVAL;
3096
3097 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED),
3098 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n"))
3099 return -EPERM;
3100
3101 if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) {
3102 print_regdomain_info(rd);
3103 return -EINVAL;
3104 }
3105
3106 regd = reg_copy_regd(rd);
3107 if (IS_ERR(regd))
3108 return PTR_ERR(regd);
3109
3110 rdev = wiphy_to_rdev(wiphy);
3111
3112 spin_lock(&reg_requests_lock);
3113 prev_regd = rdev->requested_regd;
3114 rdev->requested_regd = regd;
3115 spin_unlock(&reg_requests_lock);
3116
3117 kfree(prev_regd);
2c3e861c
AN
3118 return 0;
3119}
3120
3121int regulatory_set_wiphy_regd(struct wiphy *wiphy,
3122 struct ieee80211_regdomain *rd)
3123{
3124 int ret = __regulatory_set_wiphy_regd(wiphy, rd);
3125
3126 if (ret)
3127 return ret;
b0d7aa59
JD
3128
3129 schedule_work(&reg_work);
3130 return 0;
3131}
3132EXPORT_SYMBOL(regulatory_set_wiphy_regd);
3133
2c3e861c
AN
3134int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy,
3135 struct ieee80211_regdomain *rd)
3136{
3137 int ret;
3138
3139 ASSERT_RTNL();
3140
3141 ret = __regulatory_set_wiphy_regd(wiphy, rd);
3142 if (ret)
3143 return ret;
3144
3145 /* process the request immediately */
3146 reg_process_self_managed_hints();
3147 return 0;
3148}
3149EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl);
3150
57b5ce07
LR
3151void wiphy_regulatory_register(struct wiphy *wiphy)
3152{
23df0b73
AN
3153 struct regulatory_request *lr;
3154
b0d7aa59
JD
3155 /* self-managed devices ignore external hints */
3156 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
3157 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
3158 REGULATORY_COUNTRY_IE_IGNORE;
3159
57b5ce07
LR
3160 if (!reg_dev_ignore_cell_hint(wiphy))
3161 reg_num_devs_support_basehint++;
3162
23df0b73
AN
3163 lr = get_last_request();
3164 wiphy_update_regulatory(wiphy, lr->initiator);
89766727 3165 wiphy_all_share_dfs_chan_state(wiphy);
57b5ce07
LR
3166}
3167
bfead080 3168void wiphy_regulatory_deregister(struct wiphy *wiphy)
3f2355cb 3169{
0ad8acaf 3170 struct wiphy *request_wiphy = NULL;
c492db37 3171 struct regulatory_request *lr;
761cf7ec 3172
c492db37 3173 lr = get_last_request();
abc7381b 3174
57b5ce07
LR
3175 if (!reg_dev_ignore_cell_hint(wiphy))
3176 reg_num_devs_support_basehint--;
3177
458f4f9e 3178 rcu_free_regdom(get_wiphy_regdom(wiphy));
34dd886c 3179 RCU_INIT_POINTER(wiphy->regd, NULL);
0ef9ccdd 3180
c492db37
JB
3181 if (lr)
3182 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
806a9e39 3183
0ef9ccdd 3184 if (!request_wiphy || request_wiphy != wiphy)
38fd2143 3185 return;
0ef9ccdd 3186
c492db37
JB
3187 lr->wiphy_idx = WIPHY_IDX_INVALID;
3188 lr->country_ie_env = ENVIRON_ANY;
3f2355cb
LR
3189}
3190
174e0cd2
IP
3191/*
3192 * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
3193 * UNII band definitions
3194 */
3195int cfg80211_get_unii(int freq)
3196{
3197 /* UNII-1 */
3198 if (freq >= 5150 && freq <= 5250)
3199 return 0;
3200
3201 /* UNII-2A */
3202 if (freq > 5250 && freq <= 5350)
3203 return 1;
3204
3205 /* UNII-2B */
3206 if (freq > 5350 && freq <= 5470)
3207 return 2;
3208
3209 /* UNII-2C */
3210 if (freq > 5470 && freq <= 5725)
3211 return 3;
3212
3213 /* UNII-3 */
3214 if (freq > 5725 && freq <= 5825)
3215 return 4;
3216
3217 return -EINVAL;
3218}
3219
c8866e55
IP
3220bool regulatory_indoor_allowed(void)
3221{
3222 return reg_is_indoor;
3223}
3224
b35a51c7
VT
3225bool regulatory_pre_cac_allowed(struct wiphy *wiphy)
3226{
3227 const struct ieee80211_regdomain *regd = NULL;
3228 const struct ieee80211_regdomain *wiphy_regd = NULL;
3229 bool pre_cac_allowed = false;
3230
3231 rcu_read_lock();
3232
3233 regd = rcu_dereference(cfg80211_regdomain);
3234 wiphy_regd = rcu_dereference(wiphy->regd);
3235 if (!wiphy_regd) {
3236 if (regd->dfs_region == NL80211_DFS_ETSI)
3237 pre_cac_allowed = true;
3238
3239 rcu_read_unlock();
3240
3241 return pre_cac_allowed;
3242 }
3243
3244 if (regd->dfs_region == wiphy_regd->dfs_region &&
3245 wiphy_regd->dfs_region == NL80211_DFS_ETSI)
3246 pre_cac_allowed = true;
3247
3248 rcu_read_unlock();
3249
3250 return pre_cac_allowed;
3251}
3252
89766727
VT
3253void regulatory_propagate_dfs_state(struct wiphy *wiphy,
3254 struct cfg80211_chan_def *chandef,
3255 enum nl80211_dfs_state dfs_state,
3256 enum nl80211_radar_event event)
3257{
3258 struct cfg80211_registered_device *rdev;
3259
3260 ASSERT_RTNL();
3261
3262 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3263 return;
3264
89766727
VT
3265 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3266 if (wiphy == &rdev->wiphy)
3267 continue;
3268
3269 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
3270 continue;
3271
3272 if (!ieee80211_get_channel(&rdev->wiphy,
3273 chandef->chan->center_freq))
3274 continue;
3275
3276 cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state);
3277
3278 if (event == NL80211_RADAR_DETECTED ||
3279 event == NL80211_RADAR_CAC_FINISHED)
3280 cfg80211_sched_dfs_chan_update(rdev);
3281
3282 nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL);
3283 }
3284}
3285
2fcc9f73 3286int __init regulatory_init(void)
b2e1b302 3287{
bcf4f99b 3288 int err = 0;
734366de 3289
b2e1b302
LR
3290 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
3291 if (IS_ERR(reg_pdev))
3292 return PTR_ERR(reg_pdev);
734366de 3293
fe33eb39 3294 spin_lock_init(&reg_requests_lock);
e38f8a7a 3295 spin_lock_init(&reg_pending_beacons_lock);
05050753 3296 spin_lock_init(&reg_indoor_lock);
fe33eb39 3297
80007efe
LR
3298 reg_regdb_size_check();
3299
458f4f9e 3300 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
734366de 3301
09d989d1
LR
3302 user_alpha2[0] = '9';
3303 user_alpha2[1] = '7';
3304
ae9e4b0d 3305 /* We always try to get an update for the static regdomain */
458f4f9e 3306 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
ba25c141 3307 if (err) {
09d11800
OO
3308 if (err == -ENOMEM) {
3309 platform_device_unregister(reg_pdev);
bcf4f99b 3310 return err;
09d11800 3311 }
bcf4f99b
LR
3312 /*
3313 * N.B. kobject_uevent_env() can fail mainly for when we're out
3314 * memory which is handled and propagated appropriately above
3315 * but it can also fail during a netlink_broadcast() or during
3316 * early boot for call_usermodehelper(). For now treat these
3317 * errors as non-fatal.
3318 */
e9c0268f 3319 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
bcf4f99b 3320 }
734366de 3321
ae9e4b0d
LR
3322 /*
3323 * Finally, if the user set the module parameter treat it
3324 * as a user hint.
3325 */
3326 if (!is_world_regdom(ieee80211_regdom))
57b5ce07
LR
3327 regulatory_hint_user(ieee80211_regdom,
3328 NL80211_USER_REG_HINT_USER);
ae9e4b0d 3329
b2e1b302
LR
3330 return 0;
3331}
3332
1a919318 3333void regulatory_exit(void)
b2e1b302 3334{
fe33eb39 3335 struct regulatory_request *reg_request, *tmp;
e38f8a7a 3336 struct reg_beacon *reg_beacon, *btmp;
fe33eb39
LR
3337
3338 cancel_work_sync(&reg_work);
b6863036 3339 cancel_crda_timeout_sync();
ad932f04 3340 cancel_delayed_work_sync(&reg_check_chans);
fe33eb39 3341
9027b149 3342 /* Lock to suppress warnings */
38fd2143 3343 rtnl_lock();
379b82f4 3344 reset_regdomains(true, NULL);
38fd2143 3345 rtnl_unlock();
734366de 3346
58ebacc6 3347 dev_set_uevent_suppress(&reg_pdev->dev, true);
f6037d09 3348
b2e1b302 3349 platform_device_unregister(reg_pdev);
734366de 3350
fea9bced
JB
3351 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
3352 list_del(&reg_beacon->list);
3353 kfree(reg_beacon);
e38f8a7a 3354 }
e38f8a7a 3355
fea9bced
JB
3356 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
3357 list_del(&reg_beacon->list);
3358 kfree(reg_beacon);
e38f8a7a
LR
3359 }
3360
fea9bced
JB
3361 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
3362 list_del(&reg_request->list);
3363 kfree(reg_request);
fe33eb39 3364 }
8318d78a 3365}