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