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