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