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