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