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