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