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