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