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