xref: /openbmc/linux/net/wireless/reg.c (revision 729e9c76)
18318d78aSJohannes Berg /*
28318d78aSJohannes Berg  * Copyright 2002-2005, Instant802 Networks, Inc.
38318d78aSJohannes Berg  * Copyright 2005-2006, Devicescape Software, Inc.
48318d78aSJohannes Berg  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
5b2e1b302SLuis R. Rodriguez  * Copyright 2008	Luis R. Rodriguez <lrodriguz@atheros.com>
68318d78aSJohannes Berg  *
78318d78aSJohannes Berg  * This program is free software; you can redistribute it and/or modify
88318d78aSJohannes Berg  * it under the terms of the GNU General Public License version 2 as
98318d78aSJohannes Berg  * published by the Free Software Foundation.
108318d78aSJohannes Berg  */
118318d78aSJohannes Berg 
12b2e1b302SLuis R. Rodriguez /**
13b2e1b302SLuis R. Rodriguez  * DOC: Wireless regulatory infrastructure
148318d78aSJohannes Berg  *
158318d78aSJohannes Berg  * The usual implementation is for a driver to read a device EEPROM to
168318d78aSJohannes Berg  * determine which regulatory domain it should be operating under, then
178318d78aSJohannes Berg  * looking up the allowable channels in a driver-local table and finally
188318d78aSJohannes Berg  * registering those channels in the wiphy structure.
198318d78aSJohannes Berg  *
20b2e1b302SLuis R. Rodriguez  * Another set of compliance enforcement is for drivers to use their
21b2e1b302SLuis R. Rodriguez  * own compliance limits which can be stored on the EEPROM. The host
22b2e1b302SLuis R. Rodriguez  * driver or firmware may ensure these are used.
23b2e1b302SLuis R. Rodriguez  *
24b2e1b302SLuis R. Rodriguez  * In addition to all this we provide an extra layer of regulatory
25b2e1b302SLuis R. Rodriguez  * conformance. For drivers which do not have any regulatory
26b2e1b302SLuis R. Rodriguez  * information CRDA provides the complete regulatory solution.
27b2e1b302SLuis R. Rodriguez  * For others it provides a community effort on further restrictions
28b2e1b302SLuis R. Rodriguez  * to enhance compliance.
29b2e1b302SLuis R. Rodriguez  *
30b2e1b302SLuis R. Rodriguez  * Note: When number of rules --> infinity we will not be able to
31b2e1b302SLuis R. Rodriguez  * index on alpha2 any more, instead we'll probably have to
32b2e1b302SLuis R. Rodriguez  * rely on some SHA1 checksum of the regdomain for example.
33b2e1b302SLuis R. Rodriguez  *
348318d78aSJohannes Berg  */
358318d78aSJohannes Berg #include <linux/kernel.h>
36b2e1b302SLuis R. Rodriguez #include <linux/list.h>
37b2e1b302SLuis R. Rodriguez #include <linux/random.h>
38b2e1b302SLuis R. Rodriguez #include <linux/nl80211.h>
39b2e1b302SLuis R. Rodriguez #include <linux/platform_device.h>
40b2e1b302SLuis R. Rodriguez #include <net/cfg80211.h>
418318d78aSJohannes Berg #include "core.h"
42b2e1b302SLuis R. Rodriguez #include "reg.h"
4373d54c9eSLuis R. Rodriguez #include "nl80211.h"
448318d78aSJohannes Berg 
455166ccd2SLuis R. Rodriguez /* Receipt of information from last regulatory request */
46f6037d09SJohannes Berg static struct regulatory_request *last_request;
47734366deSJohannes Berg 
48b2e1b302SLuis R. Rodriguez /* To trigger userspace events */
49b2e1b302SLuis R. Rodriguez static struct platform_device *reg_pdev;
508318d78aSJohannes Berg 
51fb1fc7adSLuis R. Rodriguez /*
52fb1fc7adSLuis R. Rodriguez  * Central wireless core regulatory domains, we only need two,
53734366deSJohannes Berg  * the current one and a world regulatory domain in case we have no
54fb1fc7adSLuis R. Rodriguez  * information to give us an alpha2
55fb1fc7adSLuis R. Rodriguez  */
56f130347cSLuis R. Rodriguez const struct ieee80211_regdomain *cfg80211_regdomain;
57734366deSJohannes Berg 
58fb1fc7adSLuis R. Rodriguez /*
59fb1fc7adSLuis R. Rodriguez  * We use this as a place for the rd structure built from the
603f2355cbSLuis R. Rodriguez  * last parsed country IE to rest until CRDA gets back to us with
61fb1fc7adSLuis R. Rodriguez  * what it thinks should apply for the same country
62fb1fc7adSLuis R. Rodriguez  */
633f2355cbSLuis R. Rodriguez static const struct ieee80211_regdomain *country_ie_regdomain;
643f2355cbSLuis R. Rodriguez 
65e38f8a7aSLuis R. Rodriguez /* Used to queue up regulatory hints */
66fe33eb39SLuis R. Rodriguez static LIST_HEAD(reg_requests_list);
67fe33eb39SLuis R. Rodriguez static spinlock_t reg_requests_lock;
68fe33eb39SLuis R. Rodriguez 
69e38f8a7aSLuis R. Rodriguez /* Used to queue up beacon hints for review */
70e38f8a7aSLuis R. Rodriguez static LIST_HEAD(reg_pending_beacons);
71e38f8a7aSLuis R. Rodriguez static spinlock_t reg_pending_beacons_lock;
72e38f8a7aSLuis R. Rodriguez 
73e38f8a7aSLuis R. Rodriguez /* Used to keep track of processed beacon hints */
74e38f8a7aSLuis R. Rodriguez static LIST_HEAD(reg_beacon_list);
75e38f8a7aSLuis R. Rodriguez 
76e38f8a7aSLuis R. Rodriguez struct reg_beacon {
77e38f8a7aSLuis R. Rodriguez 	struct list_head list;
78e38f8a7aSLuis R. Rodriguez 	struct ieee80211_channel chan;
79e38f8a7aSLuis R. Rodriguez };
80e38f8a7aSLuis R. Rodriguez 
81734366deSJohannes Berg /* We keep a static world regulatory domain in case of the absence of CRDA */
82734366deSJohannes Berg static const struct ieee80211_regdomain world_regdom = {
83611b6a82SLuis R. Rodriguez 	.n_reg_rules = 5,
84734366deSJohannes Berg 	.alpha2 =  "00",
85734366deSJohannes Berg 	.reg_rules = {
8668798a62SLuis R. Rodriguez 		/* IEEE 802.11b/g, channels 1..11 */
8768798a62SLuis R. Rodriguez 		REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
88611b6a82SLuis R. Rodriguez 		/* IEEE 802.11b/g, channels 12..13. No HT40
89611b6a82SLuis R. Rodriguez 		 * channel fits here. */
90611b6a82SLuis R. Rodriguez 		REG_RULE(2467-10, 2472+10, 20, 6, 20,
91611b6a82SLuis R. Rodriguez 			NL80211_RRF_PASSIVE_SCAN |
92611b6a82SLuis R. Rodriguez 			NL80211_RRF_NO_IBSS),
93611b6a82SLuis R. Rodriguez 		/* IEEE 802.11 channel 14 - Only JP enables
94611b6a82SLuis R. Rodriguez 		 * this and for 802.11b only */
95611b6a82SLuis R. Rodriguez 		REG_RULE(2484-10, 2484+10, 20, 6, 20,
96611b6a82SLuis R. Rodriguez 			NL80211_RRF_PASSIVE_SCAN |
97611b6a82SLuis R. Rodriguez 			NL80211_RRF_NO_IBSS |
98611b6a82SLuis R. Rodriguez 			NL80211_RRF_NO_OFDM),
993fc71f77SLuis R. Rodriguez 		/* IEEE 802.11a, channel 36..48 */
100ec329aceSLuis R. Rodriguez 		REG_RULE(5180-10, 5240+10, 40, 6, 20,
1013fc71f77SLuis R. Rodriguez                         NL80211_RRF_PASSIVE_SCAN |
1023fc71f77SLuis R. Rodriguez                         NL80211_RRF_NO_IBSS),
1033fc71f77SLuis R. Rodriguez 
1043fc71f77SLuis R. Rodriguez 		/* NB: 5260 MHz - 5700 MHz requies DFS */
1053fc71f77SLuis R. Rodriguez 
1063fc71f77SLuis R. Rodriguez 		/* IEEE 802.11a, channel 149..165 */
107ec329aceSLuis R. Rodriguez 		REG_RULE(5745-10, 5825+10, 40, 6, 20,
1083fc71f77SLuis R. Rodriguez 			NL80211_RRF_PASSIVE_SCAN |
1093fc71f77SLuis R. Rodriguez 			NL80211_RRF_NO_IBSS),
110734366deSJohannes Berg 	}
111734366deSJohannes Berg };
112734366deSJohannes Berg 
113a3d2eaf0SJohannes Berg static const struct ieee80211_regdomain *cfg80211_world_regdom =
114a3d2eaf0SJohannes Berg 	&world_regdom;
115734366deSJohannes Berg 
116734366deSJohannes Berg #ifdef CONFIG_WIRELESS_OLD_REGULATORY
117734366deSJohannes Berg static char *ieee80211_regdom = "US";
1186ee7d330SLuis R. Rodriguez #else
1196ee7d330SLuis R. Rodriguez static char *ieee80211_regdom = "00";
1206ee7d330SLuis R. Rodriguez #endif
1216ee7d330SLuis R. Rodriguez 
122734366deSJohannes Berg module_param(ieee80211_regdom, charp, 0444);
123734366deSJohannes Berg MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
124734366deSJohannes Berg 
1256ee7d330SLuis R. Rodriguez #ifdef CONFIG_WIRELESS_OLD_REGULATORY
126fb1fc7adSLuis R. Rodriguez /*
127fb1fc7adSLuis R. Rodriguez  * We assume 40 MHz bandwidth for the old regulatory work.
128734366deSJohannes Berg  * We make emphasis we are using the exact same frequencies
129fb1fc7adSLuis R. Rodriguez  * as before
130fb1fc7adSLuis R. Rodriguez  */
131734366deSJohannes Berg 
132734366deSJohannes Berg static const struct ieee80211_regdomain us_regdom = {
133734366deSJohannes Berg 	.n_reg_rules = 6,
134734366deSJohannes Berg 	.alpha2 =  "US",
135734366deSJohannes Berg 	.reg_rules = {
136734366deSJohannes Berg 		/* IEEE 802.11b/g, channels 1..11 */
137734366deSJohannes Berg 		REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
138734366deSJohannes Berg 		/* IEEE 802.11a, channel 36 */
139734366deSJohannes Berg 		REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
140734366deSJohannes Berg 		/* IEEE 802.11a, channel 40 */
141734366deSJohannes Berg 		REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
142734366deSJohannes Berg 		/* IEEE 802.11a, channel 44 */
143734366deSJohannes Berg 		REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
144734366deSJohannes Berg 		/* IEEE 802.11a, channels 48..64 */
145734366deSJohannes Berg 		REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
146734366deSJohannes Berg 		/* IEEE 802.11a, channels 149..165, outdoor */
147734366deSJohannes Berg 		REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
148734366deSJohannes Berg 	}
149734366deSJohannes Berg };
150734366deSJohannes Berg 
151734366deSJohannes Berg static const struct ieee80211_regdomain jp_regdom = {
152734366deSJohannes Berg 	.n_reg_rules = 3,
153734366deSJohannes Berg 	.alpha2 =  "JP",
154734366deSJohannes Berg 	.reg_rules = {
155734366deSJohannes Berg 		/* IEEE 802.11b/g, channels 1..14 */
156734366deSJohannes Berg 		REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
157734366deSJohannes Berg 		/* IEEE 802.11a, channels 34..48 */
158734366deSJohannes Berg 		REG_RULE(5170-10, 5240+10, 40, 6, 20,
159734366deSJohannes Berg 			NL80211_RRF_PASSIVE_SCAN),
160734366deSJohannes Berg 		/* IEEE 802.11a, channels 52..64 */
161734366deSJohannes Berg 		REG_RULE(5260-10, 5320+10, 40, 6, 20,
162734366deSJohannes Berg 			NL80211_RRF_NO_IBSS |
163734366deSJohannes Berg 			NL80211_RRF_DFS),
164734366deSJohannes Berg 	}
165734366deSJohannes Berg };
166734366deSJohannes Berg 
167734366deSJohannes Berg static const struct ieee80211_regdomain eu_regdom = {
168734366deSJohannes Berg 	.n_reg_rules = 6,
169fb1fc7adSLuis R. Rodriguez 	/*
170fb1fc7adSLuis R. Rodriguez 	 * This alpha2 is bogus, we leave it here just for stupid
171fb1fc7adSLuis R. Rodriguez 	 * backward compatibility
172fb1fc7adSLuis R. Rodriguez 	 */
173734366deSJohannes Berg 	.alpha2 =  "EU",
174734366deSJohannes Berg 	.reg_rules = {
175734366deSJohannes Berg 		/* IEEE 802.11b/g, channels 1..13 */
176734366deSJohannes Berg 		REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
177734366deSJohannes Berg 		/* IEEE 802.11a, channel 36 */
178734366deSJohannes Berg 		REG_RULE(5180-10, 5180+10, 40, 6, 23,
179734366deSJohannes Berg 			NL80211_RRF_PASSIVE_SCAN),
180734366deSJohannes Berg 		/* IEEE 802.11a, channel 40 */
181734366deSJohannes Berg 		REG_RULE(5200-10, 5200+10, 40, 6, 23,
182734366deSJohannes Berg 			NL80211_RRF_PASSIVE_SCAN),
183734366deSJohannes Berg 		/* IEEE 802.11a, channel 44 */
184734366deSJohannes Berg 		REG_RULE(5220-10, 5220+10, 40, 6, 23,
185734366deSJohannes Berg 			NL80211_RRF_PASSIVE_SCAN),
186734366deSJohannes Berg 		/* IEEE 802.11a, channels 48..64 */
187734366deSJohannes Berg 		REG_RULE(5240-10, 5320+10, 40, 6, 20,
188734366deSJohannes Berg 			NL80211_RRF_NO_IBSS |
189734366deSJohannes Berg 			NL80211_RRF_DFS),
190734366deSJohannes Berg 		/* IEEE 802.11a, channels 100..140 */
191734366deSJohannes Berg 		REG_RULE(5500-10, 5700+10, 40, 6, 30,
192734366deSJohannes Berg 			NL80211_RRF_NO_IBSS |
193734366deSJohannes Berg 			NL80211_RRF_DFS),
194734366deSJohannes Berg 	}
195734366deSJohannes Berg };
196734366deSJohannes Berg 
197734366deSJohannes Berg static const struct ieee80211_regdomain *static_regdom(char *alpha2)
198734366deSJohannes Berg {
199734366deSJohannes Berg 	if (alpha2[0] == 'U' && alpha2[1] == 'S')
200734366deSJohannes Berg 		return &us_regdom;
201734366deSJohannes Berg 	if (alpha2[0] == 'J' && alpha2[1] == 'P')
202734366deSJohannes Berg 		return &jp_regdom;
203734366deSJohannes Berg 	if (alpha2[0] == 'E' && alpha2[1] == 'U')
204734366deSJohannes Berg 		return &eu_regdom;
205734366deSJohannes Berg 	/* Default, as per the old rules */
206734366deSJohannes Berg 	return &us_regdom;
207734366deSJohannes Berg }
208734366deSJohannes Berg 
209a3d2eaf0SJohannes Berg static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
210734366deSJohannes Berg {
211734366deSJohannes Berg 	if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
212734366deSJohannes Berg 		return true;
213734366deSJohannes Berg 	return false;
214734366deSJohannes Berg }
215734366deSJohannes Berg #else
216942b25cfSJohannes Berg static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
217942b25cfSJohannes Berg {
218942b25cfSJohannes Berg 	return false;
219942b25cfSJohannes Berg }
220942b25cfSJohannes Berg #endif
221942b25cfSJohannes Berg 
222734366deSJohannes Berg static void reset_regdomains(void)
223734366deSJohannes Berg {
224942b25cfSJohannes Berg 	/* avoid freeing static information or freeing something twice */
225942b25cfSJohannes Berg 	if (cfg80211_regdomain == cfg80211_world_regdom)
226942b25cfSJohannes Berg 		cfg80211_regdomain = NULL;
227942b25cfSJohannes Berg 	if (cfg80211_world_regdom == &world_regdom)
228942b25cfSJohannes Berg 		cfg80211_world_regdom = NULL;
229942b25cfSJohannes Berg 	if (cfg80211_regdomain == &world_regdom)
230942b25cfSJohannes Berg 		cfg80211_regdomain = NULL;
231942b25cfSJohannes Berg 	if (is_old_static_regdom(cfg80211_regdomain))
232942b25cfSJohannes Berg 		cfg80211_regdomain = NULL;
233942b25cfSJohannes Berg 
234734366deSJohannes Berg 	kfree(cfg80211_regdomain);
235734366deSJohannes Berg 	kfree(cfg80211_world_regdom);
236734366deSJohannes Berg 
237a3d2eaf0SJohannes Berg 	cfg80211_world_regdom = &world_regdom;
238734366deSJohannes Berg 	cfg80211_regdomain = NULL;
239734366deSJohannes Berg }
240734366deSJohannes Berg 
241fb1fc7adSLuis R. Rodriguez /*
242fb1fc7adSLuis R. Rodriguez  * Dynamic world regulatory domain requested by the wireless
243fb1fc7adSLuis R. Rodriguez  * core upon initialization
244fb1fc7adSLuis R. Rodriguez  */
245a3d2eaf0SJohannes Berg static void update_world_regdomain(const struct ieee80211_regdomain *rd)
246734366deSJohannes Berg {
247f6037d09SJohannes Berg 	BUG_ON(!last_request);
248734366deSJohannes Berg 
249734366deSJohannes Berg 	reset_regdomains();
250734366deSJohannes Berg 
251734366deSJohannes Berg 	cfg80211_world_regdom = rd;
252734366deSJohannes Berg 	cfg80211_regdomain = rd;
253734366deSJohannes Berg }
254734366deSJohannes Berg 
255a3d2eaf0SJohannes Berg bool is_world_regdom(const char *alpha2)
256b2e1b302SLuis R. Rodriguez {
257b2e1b302SLuis R. Rodriguez 	if (!alpha2)
258b2e1b302SLuis R. Rodriguez 		return false;
259b2e1b302SLuis R. Rodriguez 	if (alpha2[0] == '0' && alpha2[1] == '0')
260b2e1b302SLuis R. Rodriguez 		return true;
261b2e1b302SLuis R. Rodriguez 	return false;
262b2e1b302SLuis R. Rodriguez }
263b2e1b302SLuis R. Rodriguez 
264a3d2eaf0SJohannes Berg static bool is_alpha2_set(const char *alpha2)
265b2e1b302SLuis R. Rodriguez {
266b2e1b302SLuis R. Rodriguez 	if (!alpha2)
267b2e1b302SLuis R. Rodriguez 		return false;
268b2e1b302SLuis R. Rodriguez 	if (alpha2[0] != 0 && alpha2[1] != 0)
269b2e1b302SLuis R. Rodriguez 		return true;
270b2e1b302SLuis R. Rodriguez 	return false;
271b2e1b302SLuis R. Rodriguez }
272b2e1b302SLuis R. Rodriguez 
273b2e1b302SLuis R. Rodriguez static bool is_alpha_upper(char letter)
274b2e1b302SLuis R. Rodriguez {
275b2e1b302SLuis R. Rodriguez 	/* ASCII A - Z */
276b2e1b302SLuis R. Rodriguez 	if (letter >= 65 && letter <= 90)
277b2e1b302SLuis R. Rodriguez 		return true;
278b2e1b302SLuis R. Rodriguez 	return false;
279b2e1b302SLuis R. Rodriguez }
280b2e1b302SLuis R. Rodriguez 
281a3d2eaf0SJohannes Berg static bool is_unknown_alpha2(const char *alpha2)
282b2e1b302SLuis R. Rodriguez {
283b2e1b302SLuis R. Rodriguez 	if (!alpha2)
284b2e1b302SLuis R. Rodriguez 		return false;
285fb1fc7adSLuis R. Rodriguez 	/*
286fb1fc7adSLuis R. Rodriguez 	 * Special case where regulatory domain was built by driver
287fb1fc7adSLuis R. Rodriguez 	 * but a specific alpha2 cannot be determined
288fb1fc7adSLuis R. Rodriguez 	 */
289b2e1b302SLuis R. Rodriguez 	if (alpha2[0] == '9' && alpha2[1] == '9')
290b2e1b302SLuis R. Rodriguez 		return true;
291b2e1b302SLuis R. Rodriguez 	return false;
292b2e1b302SLuis R. Rodriguez }
293b2e1b302SLuis R. Rodriguez 
2943f2355cbSLuis R. Rodriguez static bool is_intersected_alpha2(const char *alpha2)
2953f2355cbSLuis R. Rodriguez {
2963f2355cbSLuis R. Rodriguez 	if (!alpha2)
2973f2355cbSLuis R. Rodriguez 		return false;
298fb1fc7adSLuis R. Rodriguez 	/*
299fb1fc7adSLuis R. Rodriguez 	 * Special case where regulatory domain is the
3003f2355cbSLuis R. Rodriguez 	 * result of an intersection between two regulatory domain
301fb1fc7adSLuis R. Rodriguez 	 * structures
302fb1fc7adSLuis R. Rodriguez 	 */
3033f2355cbSLuis R. Rodriguez 	if (alpha2[0] == '9' && alpha2[1] == '8')
3043f2355cbSLuis R. Rodriguez 		return true;
3053f2355cbSLuis R. Rodriguez 	return false;
3063f2355cbSLuis R. Rodriguez }
3073f2355cbSLuis R. Rodriguez 
308a3d2eaf0SJohannes Berg static bool is_an_alpha2(const char *alpha2)
309b2e1b302SLuis R. Rodriguez {
310b2e1b302SLuis R. Rodriguez 	if (!alpha2)
311b2e1b302SLuis R. Rodriguez 		return false;
312b2e1b302SLuis R. Rodriguez 	if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
313b2e1b302SLuis R. Rodriguez 		return true;
314b2e1b302SLuis R. Rodriguez 	return false;
315b2e1b302SLuis R. Rodriguez }
316b2e1b302SLuis R. Rodriguez 
317a3d2eaf0SJohannes Berg static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
318b2e1b302SLuis R. Rodriguez {
319b2e1b302SLuis R. Rodriguez 	if (!alpha2_x || !alpha2_y)
320b2e1b302SLuis R. Rodriguez 		return false;
321b2e1b302SLuis R. Rodriguez 	if (alpha2_x[0] == alpha2_y[0] &&
322b2e1b302SLuis R. Rodriguez 		alpha2_x[1] == alpha2_y[1])
323b2e1b302SLuis R. Rodriguez 		return true;
324b2e1b302SLuis R. Rodriguez 	return false;
325b2e1b302SLuis R. Rodriguez }
326b2e1b302SLuis R. Rodriguez 
32769b1572bSLuis R. Rodriguez static bool regdom_changes(const char *alpha2)
328b2e1b302SLuis R. Rodriguez {
329761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
330761cf7ecSLuis R. Rodriguez 
331b2e1b302SLuis R. Rodriguez 	if (!cfg80211_regdomain)
332b2e1b302SLuis R. Rodriguez 		return true;
333b2e1b302SLuis R. Rodriguez 	if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
334b2e1b302SLuis R. Rodriguez 		return false;
335b2e1b302SLuis R. Rodriguez 	return true;
336b2e1b302SLuis R. Rodriguez }
337b2e1b302SLuis R. Rodriguez 
3383f2355cbSLuis R. Rodriguez /**
3393f2355cbSLuis R. Rodriguez  * country_ie_integrity_changes - tells us if the country IE has changed
3403f2355cbSLuis R. Rodriguez  * @checksum: checksum of country IE of fields we are interested in
3413f2355cbSLuis R. Rodriguez  *
3423f2355cbSLuis R. Rodriguez  * If the country IE has not changed you can ignore it safely. This is
3433f2355cbSLuis R. Rodriguez  * useful to determine if two devices are seeing two different country IEs
3443f2355cbSLuis R. Rodriguez  * even on the same alpha2. Note that this will return false if no IE has
3453f2355cbSLuis R. Rodriguez  * been set on the wireless core yet.
3463f2355cbSLuis R. Rodriguez  */
3473f2355cbSLuis R. Rodriguez static bool country_ie_integrity_changes(u32 checksum)
3483f2355cbSLuis R. Rodriguez {
3493f2355cbSLuis R. Rodriguez 	/* If no IE has been set then the checksum doesn't change */
3503f2355cbSLuis R. Rodriguez 	if (unlikely(!last_request->country_ie_checksum))
3513f2355cbSLuis R. Rodriguez 		return false;
3523f2355cbSLuis R. Rodriguez 	if (unlikely(last_request->country_ie_checksum != checksum))
3533f2355cbSLuis R. Rodriguez 		return true;
3543f2355cbSLuis R. Rodriguez 	return false;
3553f2355cbSLuis R. Rodriguez }
3563f2355cbSLuis R. Rodriguez 
357fb1fc7adSLuis R. Rodriguez /*
358fb1fc7adSLuis R. Rodriguez  * This lets us keep regulatory code which is updated on a regulatory
359fb1fc7adSLuis R. Rodriguez  * basis in userspace.
360fb1fc7adSLuis R. Rodriguez  */
361b2e1b302SLuis R. Rodriguez static int call_crda(const char *alpha2)
362b2e1b302SLuis R. Rodriguez {
363b2e1b302SLuis R. Rodriguez 	char country_env[9 + 2] = "COUNTRY=";
364b2e1b302SLuis R. Rodriguez 	char *envp[] = {
365b2e1b302SLuis R. Rodriguez 		country_env,
366b2e1b302SLuis R. Rodriguez 		NULL
3678318d78aSJohannes Berg 	};
3688318d78aSJohannes Berg 
369b2e1b302SLuis R. Rodriguez 	if (!is_world_regdom((char *) alpha2))
370b2e1b302SLuis R. Rodriguez 		printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
371b2e1b302SLuis R. Rodriguez 			alpha2[0], alpha2[1]);
372b2e1b302SLuis R. Rodriguez 	else
373b2e1b302SLuis R. Rodriguez 		printk(KERN_INFO "cfg80211: Calling CRDA to update world "
374b2e1b302SLuis R. Rodriguez 			"regulatory domain\n");
3758318d78aSJohannes Berg 
376b2e1b302SLuis R. Rodriguez 	country_env[8] = alpha2[0];
377b2e1b302SLuis R. Rodriguez 	country_env[9] = alpha2[1];
3788318d78aSJohannes Berg 
379b2e1b302SLuis R. Rodriguez 	return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
380b2e1b302SLuis R. Rodriguez }
381b2e1b302SLuis R. Rodriguez 
382b2e1b302SLuis R. Rodriguez /* Used by nl80211 before kmalloc'ing our regulatory domain */
383a3d2eaf0SJohannes Berg bool reg_is_valid_request(const char *alpha2)
384b2e1b302SLuis R. Rodriguez {
38561405e97SLuis R. Rodriguez 	assert_cfg80211_lock();
38661405e97SLuis R. Rodriguez 
387f6037d09SJohannes Berg 	if (!last_request)
388f6037d09SJohannes Berg 		return false;
389f6037d09SJohannes Berg 
390f6037d09SJohannes Berg 	return alpha2_equal(last_request->alpha2, alpha2);
391b2e1b302SLuis R. Rodriguez }
392b2e1b302SLuis R. Rodriguez 
393b2e1b302SLuis R. Rodriguez /* Sanity check on a regulatory rule */
394a3d2eaf0SJohannes Berg static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
395b2e1b302SLuis R. Rodriguez {
396a3d2eaf0SJohannes Berg 	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
397b2e1b302SLuis R. Rodriguez 	u32 freq_diff;
398b2e1b302SLuis R. Rodriguez 
39991e99004SLuis R. Rodriguez 	if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
400b2e1b302SLuis R. Rodriguez 		return false;
401b2e1b302SLuis R. Rodriguez 
402b2e1b302SLuis R. Rodriguez 	if (freq_range->start_freq_khz > freq_range->end_freq_khz)
403b2e1b302SLuis R. Rodriguez 		return false;
404b2e1b302SLuis R. Rodriguez 
405b2e1b302SLuis R. Rodriguez 	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
406b2e1b302SLuis R. Rodriguez 
407bd05f28eSRoel Kluin 	if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
408bd05f28eSRoel Kluin 			freq_range->max_bandwidth_khz > freq_diff)
409b2e1b302SLuis R. Rodriguez 		return false;
410b2e1b302SLuis R. Rodriguez 
411b2e1b302SLuis R. Rodriguez 	return true;
412b2e1b302SLuis R. Rodriguez }
413b2e1b302SLuis R. Rodriguez 
414a3d2eaf0SJohannes Berg static bool is_valid_rd(const struct ieee80211_regdomain *rd)
415b2e1b302SLuis R. Rodriguez {
416a3d2eaf0SJohannes Berg 	const struct ieee80211_reg_rule *reg_rule = NULL;
417b2e1b302SLuis R. Rodriguez 	unsigned int i;
418b2e1b302SLuis R. Rodriguez 
419b2e1b302SLuis R. Rodriguez 	if (!rd->n_reg_rules)
420b2e1b302SLuis R. Rodriguez 		return false;
421b2e1b302SLuis R. Rodriguez 
42288dc1c3fSLuis R. Rodriguez 	if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
42388dc1c3fSLuis R. Rodriguez 		return false;
42488dc1c3fSLuis R. Rodriguez 
425b2e1b302SLuis R. Rodriguez 	for (i = 0; i < rd->n_reg_rules; i++) {
426b2e1b302SLuis R. Rodriguez 		reg_rule = &rd->reg_rules[i];
427b2e1b302SLuis R. Rodriguez 		if (!is_valid_reg_rule(reg_rule))
428b2e1b302SLuis R. Rodriguez 			return false;
429b2e1b302SLuis R. Rodriguez 	}
430b2e1b302SLuis R. Rodriguez 
431b2e1b302SLuis R. Rodriguez 	return true;
432b2e1b302SLuis R. Rodriguez }
433b2e1b302SLuis R. Rodriguez 
434038659e7SLuis R. Rodriguez static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
435038659e7SLuis R. Rodriguez 			    u32 center_freq_khz,
436038659e7SLuis R. Rodriguez 			    u32 bw_khz)
437b2e1b302SLuis R. Rodriguez {
438038659e7SLuis R. Rodriguez 	u32 start_freq_khz, end_freq_khz;
439038659e7SLuis R. Rodriguez 
440038659e7SLuis R. Rodriguez 	start_freq_khz = center_freq_khz - (bw_khz/2);
441038659e7SLuis R. Rodriguez 	end_freq_khz = center_freq_khz + (bw_khz/2);
442038659e7SLuis R. Rodriguez 
443b2e1b302SLuis R. Rodriguez 	if (start_freq_khz >= freq_range->start_freq_khz &&
444b2e1b302SLuis R. Rodriguez 	    end_freq_khz <= freq_range->end_freq_khz)
445038659e7SLuis R. Rodriguez 		return true;
446038659e7SLuis R. Rodriguez 
447038659e7SLuis R. Rodriguez 	return false;
448b2e1b302SLuis R. Rodriguez }
449b2e1b302SLuis R. Rodriguez 
4500c7dc45dSLuis R. Rodriguez /**
4510c7dc45dSLuis R. Rodriguez  * freq_in_rule_band - tells us if a frequency is in a frequency band
4520c7dc45dSLuis R. Rodriguez  * @freq_range: frequency rule we want to query
4530c7dc45dSLuis R. Rodriguez  * @freq_khz: frequency we are inquiring about
4540c7dc45dSLuis R. Rodriguez  *
4550c7dc45dSLuis R. Rodriguez  * This lets us know if a specific frequency rule is or is not relevant to
4560c7dc45dSLuis R. Rodriguez  * a specific frequency's band. Bands are device specific and artificial
4570c7dc45dSLuis R. Rodriguez  * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
4580c7dc45dSLuis R. Rodriguez  * safe for now to assume that a frequency rule should not be part of a
4590c7dc45dSLuis R. Rodriguez  * frequency's band if the start freq or end freq are off by more than 2 GHz.
4600c7dc45dSLuis R. Rodriguez  * This resolution can be lowered and should be considered as we add
4610c7dc45dSLuis R. Rodriguez  * regulatory rule support for other "bands".
4620c7dc45dSLuis R. Rodriguez  **/
4630c7dc45dSLuis R. Rodriguez static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
4640c7dc45dSLuis R. Rodriguez 	u32 freq_khz)
4650c7dc45dSLuis R. Rodriguez {
4660c7dc45dSLuis R. Rodriguez #define ONE_GHZ_IN_KHZ	1000000
4670c7dc45dSLuis R. Rodriguez 	if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
4680c7dc45dSLuis R. Rodriguez 		return true;
4690c7dc45dSLuis R. Rodriguez 	if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
4700c7dc45dSLuis R. Rodriguez 		return true;
4710c7dc45dSLuis R. Rodriguez 	return false;
4720c7dc45dSLuis R. Rodriguez #undef ONE_GHZ_IN_KHZ
4730c7dc45dSLuis R. Rodriguez }
4740c7dc45dSLuis R. Rodriguez 
475fb1fc7adSLuis R. Rodriguez /*
476fb1fc7adSLuis R. Rodriguez  * Converts a country IE to a regulatory domain. A regulatory domain
4773f2355cbSLuis R. Rodriguez  * structure has a lot of information which the IE doesn't yet have,
4783f2355cbSLuis R. Rodriguez  * so for the other values we use upper max values as we will intersect
479fb1fc7adSLuis R. Rodriguez  * with our userspace regulatory agent to get lower bounds.
480fb1fc7adSLuis R. Rodriguez  */
4813f2355cbSLuis R. Rodriguez static struct ieee80211_regdomain *country_ie_2_rd(
4823f2355cbSLuis R. Rodriguez 				u8 *country_ie,
4833f2355cbSLuis R. Rodriguez 				u8 country_ie_len,
4843f2355cbSLuis R. Rodriguez 				u32 *checksum)
4853f2355cbSLuis R. Rodriguez {
4863f2355cbSLuis R. Rodriguez 	struct ieee80211_regdomain *rd = NULL;
4873f2355cbSLuis R. Rodriguez 	unsigned int i = 0;
4883f2355cbSLuis R. Rodriguez 	char alpha2[2];
4893f2355cbSLuis R. Rodriguez 	u32 flags = 0;
4903f2355cbSLuis R. Rodriguez 	u32 num_rules = 0, size_of_regd = 0;
4913f2355cbSLuis R. Rodriguez 	u8 *triplets_start = NULL;
4923f2355cbSLuis R. Rodriguez 	u8 len_at_triplet = 0;
4933f2355cbSLuis R. Rodriguez 	/* the last channel we have registered in a subband (triplet) */
4943f2355cbSLuis R. Rodriguez 	int last_sub_max_channel = 0;
4953f2355cbSLuis R. Rodriguez 
4963f2355cbSLuis R. Rodriguez 	*checksum = 0xDEADBEEF;
4973f2355cbSLuis R. Rodriguez 
4983f2355cbSLuis R. Rodriguez 	/* Country IE requirements */
4993f2355cbSLuis R. Rodriguez 	BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
5003f2355cbSLuis R. Rodriguez 		country_ie_len & 0x01);
5013f2355cbSLuis R. Rodriguez 
5023f2355cbSLuis R. Rodriguez 	alpha2[0] = country_ie[0];
5033f2355cbSLuis R. Rodriguez 	alpha2[1] = country_ie[1];
5043f2355cbSLuis R. Rodriguez 
5053f2355cbSLuis R. Rodriguez 	/*
5063f2355cbSLuis R. Rodriguez 	 * Third octet can be:
5073f2355cbSLuis R. Rodriguez 	 *    'I' - Indoor
5083f2355cbSLuis R. Rodriguez 	 *    'O' - Outdoor
5093f2355cbSLuis R. Rodriguez 	 *
5103f2355cbSLuis R. Rodriguez 	 *  anything else we assume is no restrictions
5113f2355cbSLuis R. Rodriguez 	 */
5123f2355cbSLuis R. Rodriguez 	if (country_ie[2] == 'I')
5133f2355cbSLuis R. Rodriguez 		flags = NL80211_RRF_NO_OUTDOOR;
5143f2355cbSLuis R. Rodriguez 	else if (country_ie[2] == 'O')
5153f2355cbSLuis R. Rodriguez 		flags = NL80211_RRF_NO_INDOOR;
5163f2355cbSLuis R. Rodriguez 
5173f2355cbSLuis R. Rodriguez 	country_ie += 3;
5183f2355cbSLuis R. Rodriguez 	country_ie_len -= 3;
5193f2355cbSLuis R. Rodriguez 
5203f2355cbSLuis R. Rodriguez 	triplets_start = country_ie;
5213f2355cbSLuis R. Rodriguez 	len_at_triplet = country_ie_len;
5223f2355cbSLuis R. Rodriguez 
5233f2355cbSLuis R. Rodriguez 	*checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
5243f2355cbSLuis R. Rodriguez 
525fb1fc7adSLuis R. Rodriguez 	/*
526fb1fc7adSLuis R. Rodriguez 	 * We need to build a reg rule for each triplet, but first we must
5273f2355cbSLuis R. Rodriguez 	 * calculate the number of reg rules we will need. We will need one
528fb1fc7adSLuis R. Rodriguez 	 * for each channel subband
529fb1fc7adSLuis R. Rodriguez 	 */
5303f2355cbSLuis R. Rodriguez 	while (country_ie_len >= 3) {
531615aab4bSLuis R. Rodriguez 		int end_channel = 0;
5323f2355cbSLuis R. Rodriguez 		struct ieee80211_country_ie_triplet *triplet =
5333f2355cbSLuis R. Rodriguez 			(struct ieee80211_country_ie_triplet *) country_ie;
5343f2355cbSLuis R. Rodriguez 		int cur_sub_max_channel = 0, cur_channel = 0;
5353f2355cbSLuis R. Rodriguez 
5363f2355cbSLuis R. Rodriguez 		if (triplet->ext.reg_extension_id >=
5373f2355cbSLuis R. Rodriguez 				IEEE80211_COUNTRY_EXTENSION_ID) {
5383f2355cbSLuis R. Rodriguez 			country_ie += 3;
5393f2355cbSLuis R. Rodriguez 			country_ie_len -= 3;
5403f2355cbSLuis R. Rodriguez 			continue;
5413f2355cbSLuis R. Rodriguez 		}
5423f2355cbSLuis R. Rodriguez 
543615aab4bSLuis R. Rodriguez 		/* 2 GHz */
544615aab4bSLuis R. Rodriguez 		if (triplet->chans.first_channel <= 14)
545615aab4bSLuis R. Rodriguez 			end_channel = triplet->chans.first_channel +
546615aab4bSLuis R. Rodriguez 				triplet->chans.num_channels;
547615aab4bSLuis R. Rodriguez 		else
548615aab4bSLuis R. Rodriguez 			/*
549615aab4bSLuis R. Rodriguez 			 * 5 GHz -- For example in country IEs if the first
550615aab4bSLuis R. Rodriguez 			 * channel given is 36 and the number of channels is 4
551615aab4bSLuis R. Rodriguez 			 * then the individual channel numbers defined for the
552615aab4bSLuis R. Rodriguez 			 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
553615aab4bSLuis R. Rodriguez 			 * and not 36, 37, 38, 39.
554615aab4bSLuis R. Rodriguez 			 *
555615aab4bSLuis R. Rodriguez 			 * See: http://tinyurl.com/11d-clarification
556615aab4bSLuis R. Rodriguez 			 */
557615aab4bSLuis R. Rodriguez 			end_channel =  triplet->chans.first_channel +
558615aab4bSLuis R. Rodriguez 				(4 * (triplet->chans.num_channels - 1));
559615aab4bSLuis R. Rodriguez 
5603f2355cbSLuis R. Rodriguez 		cur_channel = triplet->chans.first_channel;
561615aab4bSLuis R. Rodriguez 		cur_sub_max_channel = end_channel;
5623f2355cbSLuis R. Rodriguez 
5633f2355cbSLuis R. Rodriguez 		/* Basic sanity check */
5643f2355cbSLuis R. Rodriguez 		if (cur_sub_max_channel < cur_channel)
5653f2355cbSLuis R. Rodriguez 			return NULL;
5663f2355cbSLuis R. Rodriguez 
567fb1fc7adSLuis R. Rodriguez 		/*
568fb1fc7adSLuis R. Rodriguez 		 * Do not allow overlapping channels. Also channels
5693f2355cbSLuis R. Rodriguez 		 * passed in each subband must be monotonically
570fb1fc7adSLuis R. Rodriguez 		 * increasing
571fb1fc7adSLuis R. Rodriguez 		 */
5723f2355cbSLuis R. Rodriguez 		if (last_sub_max_channel) {
5733f2355cbSLuis R. Rodriguez 			if (cur_channel <= last_sub_max_channel)
5743f2355cbSLuis R. Rodriguez 				return NULL;
5753f2355cbSLuis R. Rodriguez 			if (cur_sub_max_channel <= last_sub_max_channel)
5763f2355cbSLuis R. Rodriguez 				return NULL;
5773f2355cbSLuis R. Rodriguez 		}
5783f2355cbSLuis R. Rodriguez 
579fb1fc7adSLuis R. Rodriguez 		/*
580fb1fc7adSLuis R. Rodriguez 		 * When dot11RegulatoryClassesRequired is supported
5813f2355cbSLuis R. Rodriguez 		 * we can throw ext triplets as part of this soup,
5823f2355cbSLuis R. Rodriguez 		 * for now we don't care when those change as we
583fb1fc7adSLuis R. Rodriguez 		 * don't support them
584fb1fc7adSLuis R. Rodriguez 		 */
5853f2355cbSLuis R. Rodriguez 		*checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
5863f2355cbSLuis R. Rodriguez 		  ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
5873f2355cbSLuis R. Rodriguez 		  ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
5883f2355cbSLuis R. Rodriguez 
5893f2355cbSLuis R. Rodriguez 		last_sub_max_channel = cur_sub_max_channel;
5903f2355cbSLuis R. Rodriguez 
5913f2355cbSLuis R. Rodriguez 		country_ie += 3;
5923f2355cbSLuis R. Rodriguez 		country_ie_len -= 3;
5933f2355cbSLuis R. Rodriguez 		num_rules++;
5943f2355cbSLuis R. Rodriguez 
595fb1fc7adSLuis R. Rodriguez 		/*
596fb1fc7adSLuis R. Rodriguez 		 * Note: this is not a IEEE requirement but
597fb1fc7adSLuis R. Rodriguez 		 * simply a memory requirement
598fb1fc7adSLuis R. Rodriguez 		 */
5993f2355cbSLuis R. Rodriguez 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
6003f2355cbSLuis R. Rodriguez 			return NULL;
6013f2355cbSLuis R. Rodriguez 	}
6023f2355cbSLuis R. Rodriguez 
6033f2355cbSLuis R. Rodriguez 	country_ie = triplets_start;
6043f2355cbSLuis R. Rodriguez 	country_ie_len = len_at_triplet;
6053f2355cbSLuis R. Rodriguez 
6063f2355cbSLuis R. Rodriguez 	size_of_regd = sizeof(struct ieee80211_regdomain) +
6073f2355cbSLuis R. Rodriguez 		(num_rules * sizeof(struct ieee80211_reg_rule));
6083f2355cbSLuis R. Rodriguez 
6093f2355cbSLuis R. Rodriguez 	rd = kzalloc(size_of_regd, GFP_KERNEL);
6103f2355cbSLuis R. Rodriguez 	if (!rd)
6113f2355cbSLuis R. Rodriguez 		return NULL;
6123f2355cbSLuis R. Rodriguez 
6133f2355cbSLuis R. Rodriguez 	rd->n_reg_rules = num_rules;
6143f2355cbSLuis R. Rodriguez 	rd->alpha2[0] = alpha2[0];
6153f2355cbSLuis R. Rodriguez 	rd->alpha2[1] = alpha2[1];
6163f2355cbSLuis R. Rodriguez 
6173f2355cbSLuis R. Rodriguez 	/* This time around we fill in the rd */
6183f2355cbSLuis R. Rodriguez 	while (country_ie_len >= 3) {
61902e68a3dSLuis R. Rodriguez 		int end_channel = 0;
6203f2355cbSLuis R. Rodriguez 		struct ieee80211_country_ie_triplet *triplet =
6213f2355cbSLuis R. Rodriguez 			(struct ieee80211_country_ie_triplet *) country_ie;
6223f2355cbSLuis R. Rodriguez 		struct ieee80211_reg_rule *reg_rule = NULL;
6233f2355cbSLuis R. Rodriguez 		struct ieee80211_freq_range *freq_range = NULL;
6243f2355cbSLuis R. Rodriguez 		struct ieee80211_power_rule *power_rule = NULL;
6253f2355cbSLuis R. Rodriguez 
626fb1fc7adSLuis R. Rodriguez 		/*
627fb1fc7adSLuis R. Rodriguez 		 * Must parse if dot11RegulatoryClassesRequired is true,
628fb1fc7adSLuis R. Rodriguez 		 * we don't support this yet
629fb1fc7adSLuis R. Rodriguez 		 */
6303f2355cbSLuis R. Rodriguez 		if (triplet->ext.reg_extension_id >=
6313f2355cbSLuis R. Rodriguez 				IEEE80211_COUNTRY_EXTENSION_ID) {
6323f2355cbSLuis R. Rodriguez 			country_ie += 3;
6333f2355cbSLuis R. Rodriguez 			country_ie_len -= 3;
6343f2355cbSLuis R. Rodriguez 			continue;
6353f2355cbSLuis R. Rodriguez 		}
6363f2355cbSLuis R. Rodriguez 
6373f2355cbSLuis R. Rodriguez 		reg_rule = &rd->reg_rules[i];
6383f2355cbSLuis R. Rodriguez 		freq_range = &reg_rule->freq_range;
6393f2355cbSLuis R. Rodriguez 		power_rule = &reg_rule->power_rule;
6403f2355cbSLuis R. Rodriguez 
6413f2355cbSLuis R. Rodriguez 		reg_rule->flags = flags;
6423f2355cbSLuis R. Rodriguez 
64302e68a3dSLuis R. Rodriguez 		/* 2 GHz */
64402e68a3dSLuis R. Rodriguez 		if (triplet->chans.first_channel <= 14)
64502e68a3dSLuis R. Rodriguez 			end_channel = triplet->chans.first_channel +
64602e68a3dSLuis R. Rodriguez 				triplet->chans.num_channels;
64702e68a3dSLuis R. Rodriguez 		else
64802e68a3dSLuis R. Rodriguez 			end_channel =  triplet->chans.first_channel +
64902e68a3dSLuis R. Rodriguez 				(4 * (triplet->chans.num_channels - 1));
65002e68a3dSLuis R. Rodriguez 
651fb1fc7adSLuis R. Rodriguez 		/*
652fb1fc7adSLuis R. Rodriguez 		 * The +10 is since the regulatory domain expects
6533f2355cbSLuis R. Rodriguez 		 * the actual band edge, not the center of freq for
6543f2355cbSLuis R. Rodriguez 		 * its start and end freqs, assuming 20 MHz bandwidth on
655fb1fc7adSLuis R. Rodriguez 		 * the channels passed
656fb1fc7adSLuis R. Rodriguez 		 */
6573f2355cbSLuis R. Rodriguez 		freq_range->start_freq_khz =
6583f2355cbSLuis R. Rodriguez 			MHZ_TO_KHZ(ieee80211_channel_to_frequency(
6593f2355cbSLuis R. Rodriguez 				triplet->chans.first_channel) - 10);
6603f2355cbSLuis R. Rodriguez 		freq_range->end_freq_khz =
6613f2355cbSLuis R. Rodriguez 			MHZ_TO_KHZ(ieee80211_channel_to_frequency(
66202e68a3dSLuis R. Rodriguez 				end_channel) + 10);
6633f2355cbSLuis R. Rodriguez 
664fb1fc7adSLuis R. Rodriguez 		/*
665fb1fc7adSLuis R. Rodriguez 		 * These are large arbitrary values we use to intersect later.
666fb1fc7adSLuis R. Rodriguez 		 * Increment this if we ever support >= 40 MHz channels
667fb1fc7adSLuis R. Rodriguez 		 * in IEEE 802.11
668fb1fc7adSLuis R. Rodriguez 		 */
6693f2355cbSLuis R. Rodriguez 		freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
6703f2355cbSLuis R. Rodriguez 		power_rule->max_antenna_gain = DBI_TO_MBI(100);
6713f2355cbSLuis R. Rodriguez 		power_rule->max_eirp = DBM_TO_MBM(100);
6723f2355cbSLuis R. Rodriguez 
6733f2355cbSLuis R. Rodriguez 		country_ie += 3;
6743f2355cbSLuis R. Rodriguez 		country_ie_len -= 3;
6753f2355cbSLuis R. Rodriguez 		i++;
6763f2355cbSLuis R. Rodriguez 
6773f2355cbSLuis R. Rodriguez 		BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
6783f2355cbSLuis R. Rodriguez 	}
6793f2355cbSLuis R. Rodriguez 
6803f2355cbSLuis R. Rodriguez 	return rd;
6813f2355cbSLuis R. Rodriguez }
6823f2355cbSLuis R. Rodriguez 
6833f2355cbSLuis R. Rodriguez 
684fb1fc7adSLuis R. Rodriguez /*
685fb1fc7adSLuis R. Rodriguez  * Helper for regdom_intersect(), this does the real
686fb1fc7adSLuis R. Rodriguez  * mathematical intersection fun
687fb1fc7adSLuis R. Rodriguez  */
6889c96477dSLuis R. Rodriguez static int reg_rules_intersect(
6899c96477dSLuis R. Rodriguez 	const struct ieee80211_reg_rule *rule1,
6909c96477dSLuis R. Rodriguez 	const struct ieee80211_reg_rule *rule2,
6919c96477dSLuis R. Rodriguez 	struct ieee80211_reg_rule *intersected_rule)
6929c96477dSLuis R. Rodriguez {
6939c96477dSLuis R. Rodriguez 	const struct ieee80211_freq_range *freq_range1, *freq_range2;
6949c96477dSLuis R. Rodriguez 	struct ieee80211_freq_range *freq_range;
6959c96477dSLuis R. Rodriguez 	const struct ieee80211_power_rule *power_rule1, *power_rule2;
6969c96477dSLuis R. Rodriguez 	struct ieee80211_power_rule *power_rule;
6979c96477dSLuis R. Rodriguez 	u32 freq_diff;
6989c96477dSLuis R. Rodriguez 
6999c96477dSLuis R. Rodriguez 	freq_range1 = &rule1->freq_range;
7009c96477dSLuis R. Rodriguez 	freq_range2 = &rule2->freq_range;
7019c96477dSLuis R. Rodriguez 	freq_range = &intersected_rule->freq_range;
7029c96477dSLuis R. Rodriguez 
7039c96477dSLuis R. Rodriguez 	power_rule1 = &rule1->power_rule;
7049c96477dSLuis R. Rodriguez 	power_rule2 = &rule2->power_rule;
7059c96477dSLuis R. Rodriguez 	power_rule = &intersected_rule->power_rule;
7069c96477dSLuis R. Rodriguez 
7079c96477dSLuis R. Rodriguez 	freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
7089c96477dSLuis R. Rodriguez 		freq_range2->start_freq_khz);
7099c96477dSLuis R. Rodriguez 	freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
7109c96477dSLuis R. Rodriguez 		freq_range2->end_freq_khz);
7119c96477dSLuis R. Rodriguez 	freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
7129c96477dSLuis R. Rodriguez 		freq_range2->max_bandwidth_khz);
7139c96477dSLuis R. Rodriguez 
7149c96477dSLuis R. Rodriguez 	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
7159c96477dSLuis R. Rodriguez 	if (freq_range->max_bandwidth_khz > freq_diff)
7169c96477dSLuis R. Rodriguez 		freq_range->max_bandwidth_khz = freq_diff;
7179c96477dSLuis R. Rodriguez 
7189c96477dSLuis R. Rodriguez 	power_rule->max_eirp = min(power_rule1->max_eirp,
7199c96477dSLuis R. Rodriguez 		power_rule2->max_eirp);
7209c96477dSLuis R. Rodriguez 	power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
7219c96477dSLuis R. Rodriguez 		power_rule2->max_antenna_gain);
7229c96477dSLuis R. Rodriguez 
7239c96477dSLuis R. Rodriguez 	intersected_rule->flags = (rule1->flags | rule2->flags);
7249c96477dSLuis R. Rodriguez 
7259c96477dSLuis R. Rodriguez 	if (!is_valid_reg_rule(intersected_rule))
7269c96477dSLuis R. Rodriguez 		return -EINVAL;
7279c96477dSLuis R. Rodriguez 
7289c96477dSLuis R. Rodriguez 	return 0;
7299c96477dSLuis R. Rodriguez }
7309c96477dSLuis R. Rodriguez 
7319c96477dSLuis R. Rodriguez /**
7329c96477dSLuis R. Rodriguez  * regdom_intersect - do the intersection between two regulatory domains
7339c96477dSLuis R. Rodriguez  * @rd1: first regulatory domain
7349c96477dSLuis R. Rodriguez  * @rd2: second regulatory domain
7359c96477dSLuis R. Rodriguez  *
7369c96477dSLuis R. Rodriguez  * Use this function to get the intersection between two regulatory domains.
7379c96477dSLuis R. Rodriguez  * Once completed we will mark the alpha2 for the rd as intersected, "98",
7389c96477dSLuis R. Rodriguez  * as no one single alpha2 can represent this regulatory domain.
7399c96477dSLuis R. Rodriguez  *
7409c96477dSLuis R. Rodriguez  * Returns a pointer to the regulatory domain structure which will hold the
7419c96477dSLuis R. Rodriguez  * resulting intersection of rules between rd1 and rd2. We will
7429c96477dSLuis R. Rodriguez  * kzalloc() this structure for you.
7439c96477dSLuis R. Rodriguez  */
7449c96477dSLuis R. Rodriguez static struct ieee80211_regdomain *regdom_intersect(
7459c96477dSLuis R. Rodriguez 	const struct ieee80211_regdomain *rd1,
7469c96477dSLuis R. Rodriguez 	const struct ieee80211_regdomain *rd2)
7479c96477dSLuis R. Rodriguez {
7489c96477dSLuis R. Rodriguez 	int r, size_of_regd;
7499c96477dSLuis R. Rodriguez 	unsigned int x, y;
7509c96477dSLuis R. Rodriguez 	unsigned int num_rules = 0, rule_idx = 0;
7519c96477dSLuis R. Rodriguez 	const struct ieee80211_reg_rule *rule1, *rule2;
7529c96477dSLuis R. Rodriguez 	struct ieee80211_reg_rule *intersected_rule;
7539c96477dSLuis R. Rodriguez 	struct ieee80211_regdomain *rd;
7549c96477dSLuis R. Rodriguez 	/* This is just a dummy holder to help us count */
7559c96477dSLuis R. Rodriguez 	struct ieee80211_reg_rule irule;
7569c96477dSLuis R. Rodriguez 
7579c96477dSLuis R. Rodriguez 	/* Uses the stack temporarily for counter arithmetic */
7589c96477dSLuis R. Rodriguez 	intersected_rule = &irule;
7599c96477dSLuis R. Rodriguez 
7609c96477dSLuis R. Rodriguez 	memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
7619c96477dSLuis R. Rodriguez 
7629c96477dSLuis R. Rodriguez 	if (!rd1 || !rd2)
7639c96477dSLuis R. Rodriguez 		return NULL;
7649c96477dSLuis R. Rodriguez 
765fb1fc7adSLuis R. Rodriguez 	/*
766fb1fc7adSLuis R. Rodriguez 	 * First we get a count of the rules we'll need, then we actually
7679c96477dSLuis R. Rodriguez 	 * build them. This is to so we can malloc() and free() a
7689c96477dSLuis R. Rodriguez 	 * regdomain once. The reason we use reg_rules_intersect() here
7699c96477dSLuis R. Rodriguez 	 * is it will return -EINVAL if the rule computed makes no sense.
770fb1fc7adSLuis R. Rodriguez 	 * All rules that do check out OK are valid.
771fb1fc7adSLuis R. Rodriguez 	 */
7729c96477dSLuis R. Rodriguez 
7739c96477dSLuis R. Rodriguez 	for (x = 0; x < rd1->n_reg_rules; x++) {
7749c96477dSLuis R. Rodriguez 		rule1 = &rd1->reg_rules[x];
7759c96477dSLuis R. Rodriguez 		for (y = 0; y < rd2->n_reg_rules; y++) {
7769c96477dSLuis R. Rodriguez 			rule2 = &rd2->reg_rules[y];
7779c96477dSLuis R. Rodriguez 			if (!reg_rules_intersect(rule1, rule2,
7789c96477dSLuis R. Rodriguez 					intersected_rule))
7799c96477dSLuis R. Rodriguez 				num_rules++;
7809c96477dSLuis R. Rodriguez 			memset(intersected_rule, 0,
7819c96477dSLuis R. Rodriguez 					sizeof(struct ieee80211_reg_rule));
7829c96477dSLuis R. Rodriguez 		}
7839c96477dSLuis R. Rodriguez 	}
7849c96477dSLuis R. Rodriguez 
7859c96477dSLuis R. Rodriguez 	if (!num_rules)
7869c96477dSLuis R. Rodriguez 		return NULL;
7879c96477dSLuis R. Rodriguez 
7889c96477dSLuis R. Rodriguez 	size_of_regd = sizeof(struct ieee80211_regdomain) +
7899c96477dSLuis R. Rodriguez 		((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
7909c96477dSLuis R. Rodriguez 
7919c96477dSLuis R. Rodriguez 	rd = kzalloc(size_of_regd, GFP_KERNEL);
7929c96477dSLuis R. Rodriguez 	if (!rd)
7939c96477dSLuis R. Rodriguez 		return NULL;
7949c96477dSLuis R. Rodriguez 
7959c96477dSLuis R. Rodriguez 	for (x = 0; x < rd1->n_reg_rules; x++) {
7969c96477dSLuis R. Rodriguez 		rule1 = &rd1->reg_rules[x];
7979c96477dSLuis R. Rodriguez 		for (y = 0; y < rd2->n_reg_rules; y++) {
7989c96477dSLuis R. Rodriguez 			rule2 = &rd2->reg_rules[y];
799fb1fc7adSLuis R. Rodriguez 			/*
800fb1fc7adSLuis R. Rodriguez 			 * This time around instead of using the stack lets
8019c96477dSLuis R. Rodriguez 			 * write to the target rule directly saving ourselves
802fb1fc7adSLuis R. Rodriguez 			 * a memcpy()
803fb1fc7adSLuis R. Rodriguez 			 */
8049c96477dSLuis R. Rodriguez 			intersected_rule = &rd->reg_rules[rule_idx];
8059c96477dSLuis R. Rodriguez 			r = reg_rules_intersect(rule1, rule2,
8069c96477dSLuis R. Rodriguez 				intersected_rule);
807fb1fc7adSLuis R. Rodriguez 			/*
808fb1fc7adSLuis R. Rodriguez 			 * No need to memset here the intersected rule here as
809fb1fc7adSLuis R. Rodriguez 			 * we're not using the stack anymore
810fb1fc7adSLuis R. Rodriguez 			 */
8119c96477dSLuis R. Rodriguez 			if (r)
8129c96477dSLuis R. Rodriguez 				continue;
8139c96477dSLuis R. Rodriguez 			rule_idx++;
8149c96477dSLuis R. Rodriguez 		}
8159c96477dSLuis R. Rodriguez 	}
8169c96477dSLuis R. Rodriguez 
8179c96477dSLuis R. Rodriguez 	if (rule_idx != num_rules) {
8189c96477dSLuis R. Rodriguez 		kfree(rd);
8199c96477dSLuis R. Rodriguez 		return NULL;
8209c96477dSLuis R. Rodriguez 	}
8219c96477dSLuis R. Rodriguez 
8229c96477dSLuis R. Rodriguez 	rd->n_reg_rules = num_rules;
8239c96477dSLuis R. Rodriguez 	rd->alpha2[0] = '9';
8249c96477dSLuis R. Rodriguez 	rd->alpha2[1] = '8';
8259c96477dSLuis R. Rodriguez 
8269c96477dSLuis R. Rodriguez 	return rd;
8279c96477dSLuis R. Rodriguez }
8289c96477dSLuis R. Rodriguez 
829fb1fc7adSLuis R. Rodriguez /*
830fb1fc7adSLuis R. Rodriguez  * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
831fb1fc7adSLuis R. Rodriguez  * want to just have the channel structure use these
832fb1fc7adSLuis R. Rodriguez  */
833b2e1b302SLuis R. Rodriguez static u32 map_regdom_flags(u32 rd_flags)
834b2e1b302SLuis R. Rodriguez {
835b2e1b302SLuis R. Rodriguez 	u32 channel_flags = 0;
836b2e1b302SLuis R. Rodriguez 	if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
837b2e1b302SLuis R. Rodriguez 		channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
838b2e1b302SLuis R. Rodriguez 	if (rd_flags & NL80211_RRF_NO_IBSS)
839b2e1b302SLuis R. Rodriguez 		channel_flags |= IEEE80211_CHAN_NO_IBSS;
840b2e1b302SLuis R. Rodriguez 	if (rd_flags & NL80211_RRF_DFS)
841b2e1b302SLuis R. Rodriguez 		channel_flags |= IEEE80211_CHAN_RADAR;
842b2e1b302SLuis R. Rodriguez 	return channel_flags;
843b2e1b302SLuis R. Rodriguez }
844b2e1b302SLuis R. Rodriguez 
8451fa25e41SLuis R. Rodriguez static int freq_reg_info_regd(struct wiphy *wiphy,
8461fa25e41SLuis R. Rodriguez 			      u32 center_freq,
847038659e7SLuis R. Rodriguez 			      u32 desired_bw_khz,
8481fa25e41SLuis R. Rodriguez 			      const struct ieee80211_reg_rule **reg_rule,
8491fa25e41SLuis R. Rodriguez 			      const struct ieee80211_regdomain *custom_regd)
8508318d78aSJohannes Berg {
8518318d78aSJohannes Berg 	int i;
8520c7dc45dSLuis R. Rodriguez 	bool band_rule_found = false;
8533e0c3ff3SLuis R. Rodriguez 	const struct ieee80211_regdomain *regd;
854038659e7SLuis R. Rodriguez 	bool bw_fits = false;
855038659e7SLuis R. Rodriguez 
856038659e7SLuis R. Rodriguez 	if (!desired_bw_khz)
857038659e7SLuis R. Rodriguez 		desired_bw_khz = MHZ_TO_KHZ(20);
8588318d78aSJohannes Berg 
8591fa25e41SLuis R. Rodriguez 	regd = custom_regd ? custom_regd : cfg80211_regdomain;
8603e0c3ff3SLuis R. Rodriguez 
861fb1fc7adSLuis R. Rodriguez 	/*
862fb1fc7adSLuis R. Rodriguez 	 * Follow the driver's regulatory domain, if present, unless a country
863fb1fc7adSLuis R. Rodriguez 	 * IE has been processed or a user wants to help complaince further
864fb1fc7adSLuis R. Rodriguez 	 */
8657db90f4aSLuis R. Rodriguez 	if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
8667db90f4aSLuis R. Rodriguez 	    last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
8673e0c3ff3SLuis R. Rodriguez 	    wiphy->regd)
8683e0c3ff3SLuis R. Rodriguez 		regd = wiphy->regd;
8693e0c3ff3SLuis R. Rodriguez 
8703e0c3ff3SLuis R. Rodriguez 	if (!regd)
871b2e1b302SLuis R. Rodriguez 		return -EINVAL;
872b2e1b302SLuis R. Rodriguez 
8733e0c3ff3SLuis R. Rodriguez 	for (i = 0; i < regd->n_reg_rules; i++) {
874b2e1b302SLuis R. Rodriguez 		const struct ieee80211_reg_rule *rr;
875b2e1b302SLuis R. Rodriguez 		const struct ieee80211_freq_range *fr = NULL;
876b2e1b302SLuis R. Rodriguez 		const struct ieee80211_power_rule *pr = NULL;
877b2e1b302SLuis R. Rodriguez 
8783e0c3ff3SLuis R. Rodriguez 		rr = &regd->reg_rules[i];
879b2e1b302SLuis R. Rodriguez 		fr = &rr->freq_range;
880b2e1b302SLuis R. Rodriguez 		pr = &rr->power_rule;
8810c7dc45dSLuis R. Rodriguez 
882fb1fc7adSLuis R. Rodriguez 		/*
883fb1fc7adSLuis R. Rodriguez 		 * We only need to know if one frequency rule was
8840c7dc45dSLuis R. Rodriguez 		 * was in center_freq's band, that's enough, so lets
885fb1fc7adSLuis R. Rodriguez 		 * not overwrite it once found
886fb1fc7adSLuis R. Rodriguez 		 */
8870c7dc45dSLuis R. Rodriguez 		if (!band_rule_found)
8880c7dc45dSLuis R. Rodriguez 			band_rule_found = freq_in_rule_band(fr, center_freq);
8890c7dc45dSLuis R. Rodriguez 
890038659e7SLuis R. Rodriguez 		bw_fits = reg_does_bw_fit(fr,
891038659e7SLuis R. Rodriguez 					  center_freq,
892038659e7SLuis R. Rodriguez 					  desired_bw_khz);
8930c7dc45dSLuis R. Rodriguez 
894038659e7SLuis R. Rodriguez 		if (band_rule_found && bw_fits) {
895b2e1b302SLuis R. Rodriguez 			*reg_rule = rr;
896038659e7SLuis R. Rodriguez 			return 0;
8978318d78aSJohannes Berg 		}
8988318d78aSJohannes Berg 	}
8998318d78aSJohannes Berg 
9000c7dc45dSLuis R. Rodriguez 	if (!band_rule_found)
9010c7dc45dSLuis R. Rodriguez 		return -ERANGE;
9020c7dc45dSLuis R. Rodriguez 
903038659e7SLuis R. Rodriguez 	return -EINVAL;
904b2e1b302SLuis R. Rodriguez }
90534f57347SLuis R. Rodriguez EXPORT_SYMBOL(freq_reg_info);
906b2e1b302SLuis R. Rodriguez 
907038659e7SLuis R. Rodriguez int freq_reg_info(struct wiphy *wiphy,
908038659e7SLuis R. Rodriguez 		  u32 center_freq,
909038659e7SLuis R. Rodriguez 		  u32 desired_bw_khz,
9101fa25e41SLuis R. Rodriguez 		  const struct ieee80211_reg_rule **reg_rule)
9111fa25e41SLuis R. Rodriguez {
912ac46d48eSLuis R. Rodriguez 	assert_cfg80211_lock();
913038659e7SLuis R. Rodriguez 	return freq_reg_info_regd(wiphy,
914038659e7SLuis R. Rodriguez 				  center_freq,
915038659e7SLuis R. Rodriguez 				  desired_bw_khz,
916038659e7SLuis R. Rodriguez 				  reg_rule,
917038659e7SLuis R. Rodriguez 				  NULL);
9181fa25e41SLuis R. Rodriguez }
919b2e1b302SLuis R. Rodriguez 
920038659e7SLuis R. Rodriguez /*
921038659e7SLuis R. Rodriguez  * Note that right now we assume the desired channel bandwidth
922038659e7SLuis R. Rodriguez  * is always 20 MHz for each individual channel (HT40 uses 20 MHz
923038659e7SLuis R. Rodriguez  * per channel, the primary and the extension channel). To support
924038659e7SLuis R. Rodriguez  * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
925038659e7SLuis R. Rodriguez  * new ieee80211_channel.target_bw and re run the regulatory check
926038659e7SLuis R. Rodriguez  * on the wiphy with the target_bw specified. Then we can simply use
927038659e7SLuis R. Rodriguez  * that below for the desired_bw_khz below.
928038659e7SLuis R. Rodriguez  */
929a92a3ce7SLuis R. Rodriguez static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
930a92a3ce7SLuis R. Rodriguez 			   unsigned int chan_idx)
931b2e1b302SLuis R. Rodriguez {
932b2e1b302SLuis R. Rodriguez 	int r;
933038659e7SLuis R. Rodriguez 	u32 flags, bw_flags = 0;
934038659e7SLuis R. Rodriguez 	u32 desired_bw_khz = MHZ_TO_KHZ(20);
935b2e1b302SLuis R. Rodriguez 	const struct ieee80211_reg_rule *reg_rule = NULL;
936b2e1b302SLuis R. Rodriguez 	const struct ieee80211_power_rule *power_rule = NULL;
937038659e7SLuis R. Rodriguez 	const struct ieee80211_freq_range *freq_range = NULL;
938a92a3ce7SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
939a92a3ce7SLuis R. Rodriguez 	struct ieee80211_channel *chan;
940fe33eb39SLuis R. Rodriguez 	struct wiphy *request_wiphy = NULL;
941a92a3ce7SLuis R. Rodriguez 
942761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
943761cf7ecSLuis R. Rodriguez 
944806a9e39SLuis R. Rodriguez 	request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
945806a9e39SLuis R. Rodriguez 
946a92a3ce7SLuis R. Rodriguez 	sband = wiphy->bands[band];
947a92a3ce7SLuis R. Rodriguez 	BUG_ON(chan_idx >= sband->n_channels);
948a92a3ce7SLuis R. Rodriguez 	chan = &sband->channels[chan_idx];
949a92a3ce7SLuis R. Rodriguez 
950a92a3ce7SLuis R. Rodriguez 	flags = chan->orig_flags;
951b2e1b302SLuis R. Rodriguez 
952038659e7SLuis R. Rodriguez 	r = freq_reg_info(wiphy,
953038659e7SLuis R. Rodriguez 			  MHZ_TO_KHZ(chan->center_freq),
954038659e7SLuis R. Rodriguez 			  desired_bw_khz,
955038659e7SLuis R. Rodriguez 			  &reg_rule);
956b2e1b302SLuis R. Rodriguez 
957b2e1b302SLuis R. Rodriguez 	if (r) {
958fb1fc7adSLuis R. Rodriguez 		/*
959fb1fc7adSLuis R. Rodriguez 		 * This means no regulatory rule was found in the country IE
9600c7dc45dSLuis R. Rodriguez 		 * with a frequency range on the center_freq's band, since
9610c7dc45dSLuis R. Rodriguez 		 * IEEE-802.11 allows for a country IE to have a subset of the
9620c7dc45dSLuis R. Rodriguez 		 * regulatory information provided in a country we ignore
9630c7dc45dSLuis R. Rodriguez 		 * disabling the channel unless at least one reg rule was
9640c7dc45dSLuis R. Rodriguez 		 * found on the center_freq's band. For details see this
9650c7dc45dSLuis R. Rodriguez 		 * clarification:
9660c7dc45dSLuis R. Rodriguez 		 *
9670c7dc45dSLuis R. Rodriguez 		 * http://tinyurl.com/11d-clarification
9680c7dc45dSLuis R. Rodriguez 		 */
9690c7dc45dSLuis R. Rodriguez 		if (r == -ERANGE &&
9707db90f4aSLuis R. Rodriguez 		    last_request->initiator ==
9717db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_COUNTRY_IE) {
9720c7dc45dSLuis R. Rodriguez #ifdef CONFIG_CFG80211_REG_DEBUG
9730c7dc45dSLuis R. Rodriguez 			printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
9740c7dc45dSLuis R. Rodriguez 				"intact on %s - no rule found in band on "
9750c7dc45dSLuis R. Rodriguez 				"Country IE\n",
9760c7dc45dSLuis R. Rodriguez 				chan->center_freq, wiphy_name(wiphy));
9770c7dc45dSLuis R. Rodriguez #endif
9780c7dc45dSLuis R. Rodriguez 		} else {
979fb1fc7adSLuis R. Rodriguez 		/*
980fb1fc7adSLuis R. Rodriguez 		 * In this case we know the country IE has at least one reg rule
981fb1fc7adSLuis R. Rodriguez 		 * for the band so we respect its band definitions
982fb1fc7adSLuis R. Rodriguez 		 */
9830c7dc45dSLuis R. Rodriguez #ifdef CONFIG_CFG80211_REG_DEBUG
9847db90f4aSLuis R. Rodriguez 			if (last_request->initiator ==
9857db90f4aSLuis R. Rodriguez 			    NL80211_REGDOM_SET_BY_COUNTRY_IE)
9860c7dc45dSLuis R. Rodriguez 				printk(KERN_DEBUG "cfg80211: Disabling "
9870c7dc45dSLuis R. Rodriguez 					"channel %d MHz on %s due to "
9880c7dc45dSLuis R. Rodriguez 					"Country IE\n",
9890c7dc45dSLuis R. Rodriguez 					chan->center_freq, wiphy_name(wiphy));
9900c7dc45dSLuis R. Rodriguez #endif
9918318d78aSJohannes Berg 			flags |= IEEE80211_CHAN_DISABLED;
9928318d78aSJohannes Berg 			chan->flags = flags;
9930c7dc45dSLuis R. Rodriguez 		}
9948318d78aSJohannes Berg 		return;
9958318d78aSJohannes Berg 	}
9968318d78aSJohannes Berg 
997b2e1b302SLuis R. Rodriguez 	power_rule = &reg_rule->power_rule;
998038659e7SLuis R. Rodriguez 	freq_range = &reg_rule->freq_range;
999038659e7SLuis R. Rodriguez 
1000038659e7SLuis R. Rodriguez 	if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1001038659e7SLuis R. Rodriguez 		bw_flags = IEEE80211_CHAN_NO_HT40;
1002b2e1b302SLuis R. Rodriguez 
10037db90f4aSLuis R. Rodriguez 	if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1004806a9e39SLuis R. Rodriguez 	    request_wiphy && request_wiphy == wiphy &&
1005806a9e39SLuis R. Rodriguez 	    request_wiphy->strict_regulatory) {
1006fb1fc7adSLuis R. Rodriguez 		/*
1007fb1fc7adSLuis R. Rodriguez 		 * This gaurantees the driver's requested regulatory domain
1008f976376dSLuis R. Rodriguez 		 * will always be used as a base for further regulatory
1009fb1fc7adSLuis R. Rodriguez 		 * settings
1010fb1fc7adSLuis R. Rodriguez 		 */
1011f976376dSLuis R. Rodriguez 		chan->flags = chan->orig_flags =
1012038659e7SLuis R. Rodriguez 			map_regdom_flags(reg_rule->flags) | bw_flags;
1013f976376dSLuis R. Rodriguez 		chan->max_antenna_gain = chan->orig_mag =
1014f976376dSLuis R. Rodriguez 			(int) MBI_TO_DBI(power_rule->max_antenna_gain);
1015038659e7SLuis R. Rodriguez 		chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
1016f976376dSLuis R. Rodriguez 		chan->max_power = chan->orig_mpwr =
1017f976376dSLuis R. Rodriguez 			(int) MBM_TO_DBM(power_rule->max_eirp);
1018f976376dSLuis R. Rodriguez 		return;
1019f976376dSLuis R. Rodriguez 	}
1020f976376dSLuis R. Rodriguez 
1021038659e7SLuis R. Rodriguez 	chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
10228318d78aSJohannes Berg 	chan->max_antenna_gain = min(chan->orig_mag,
1023b2e1b302SLuis R. Rodriguez 		(int) MBI_TO_DBI(power_rule->max_antenna_gain));
1024038659e7SLuis R. Rodriguez 	chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
1025253898c4SJohn W. Linville 	if (chan->orig_mpwr)
1026b2e1b302SLuis R. Rodriguez 		chan->max_power = min(chan->orig_mpwr,
1027b2e1b302SLuis R. Rodriguez 			(int) MBM_TO_DBM(power_rule->max_eirp));
1028253898c4SJohn W. Linville 	else
1029b2e1b302SLuis R. Rodriguez 		chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
10308318d78aSJohannes Berg }
10318318d78aSJohannes Berg 
1032a92a3ce7SLuis R. Rodriguez static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
10338318d78aSJohannes Berg {
1034a92a3ce7SLuis R. Rodriguez 	unsigned int i;
1035a92a3ce7SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
1036a92a3ce7SLuis R. Rodriguez 
1037a92a3ce7SLuis R. Rodriguez 	BUG_ON(!wiphy->bands[band]);
1038a92a3ce7SLuis R. Rodriguez 	sband = wiphy->bands[band];
10398318d78aSJohannes Berg 
10408318d78aSJohannes Berg 	for (i = 0; i < sband->n_channels; i++)
1041a92a3ce7SLuis R. Rodriguez 		handle_channel(wiphy, band, i);
10428318d78aSJohannes Berg }
10438318d78aSJohannes Berg 
10447db90f4aSLuis R. Rodriguez static bool ignore_reg_update(struct wiphy *wiphy,
10457db90f4aSLuis R. Rodriguez 			      enum nl80211_reg_initiator initiator)
104614b9815aSLuis R. Rodriguez {
104714b9815aSLuis R. Rodriguez 	if (!last_request)
104814b9815aSLuis R. Rodriguez 		return true;
10497db90f4aSLuis R. Rodriguez 	if (initiator == NL80211_REGDOM_SET_BY_CORE &&
10502a44f911SLuis R. Rodriguez 		  wiphy->custom_regulatory)
105114b9815aSLuis R. Rodriguez 		return true;
1052fb1fc7adSLuis R. Rodriguez 	/*
1053fb1fc7adSLuis R. Rodriguez 	 * wiphy->regd will be set once the device has its own
1054fb1fc7adSLuis R. Rodriguez 	 * desired regulatory domain set
1055fb1fc7adSLuis R. Rodriguez 	 */
1056f976376dSLuis R. Rodriguez 	if (wiphy->strict_regulatory && !wiphy->regd &&
1057f976376dSLuis R. Rodriguez 	    !is_world_regdom(last_request->alpha2))
105814b9815aSLuis R. Rodriguez 		return true;
105914b9815aSLuis R. Rodriguez 	return false;
106014b9815aSLuis R. Rodriguez }
106114b9815aSLuis R. Rodriguez 
10627db90f4aSLuis R. Rodriguez static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1063b2e1b302SLuis R. Rodriguez {
1064b2e1b302SLuis R. Rodriguez 	struct cfg80211_registered_device *drv;
1065b2e1b302SLuis R. Rodriguez 
1066b2e1b302SLuis R. Rodriguez 	list_for_each_entry(drv, &cfg80211_drv_list, list)
10677db90f4aSLuis R. Rodriguez 		wiphy_update_regulatory(&drv->wiphy, initiator);
1068b2e1b302SLuis R. Rodriguez }
1069b2e1b302SLuis R. Rodriguez 
1070e38f8a7aSLuis R. Rodriguez static void handle_reg_beacon(struct wiphy *wiphy,
1071e38f8a7aSLuis R. Rodriguez 			      unsigned int chan_idx,
1072e38f8a7aSLuis R. Rodriguez 			      struct reg_beacon *reg_beacon)
1073e38f8a7aSLuis R. Rodriguez {
1074e38f8a7aSLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
1075e38f8a7aSLuis R. Rodriguez 	struct ieee80211_channel *chan;
10766bad8766SLuis R. Rodriguez 	bool channel_changed = false;
10776bad8766SLuis R. Rodriguez 	struct ieee80211_channel chan_before;
1078e38f8a7aSLuis R. Rodriguez 
1079e38f8a7aSLuis R. Rodriguez 	assert_cfg80211_lock();
1080e38f8a7aSLuis R. Rodriguez 
1081e38f8a7aSLuis R. Rodriguez 	sband = wiphy->bands[reg_beacon->chan.band];
1082e38f8a7aSLuis R. Rodriguez 	chan = &sband->channels[chan_idx];
1083e38f8a7aSLuis R. Rodriguez 
1084e38f8a7aSLuis R. Rodriguez 	if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1085e38f8a7aSLuis R. Rodriguez 		return;
1086e38f8a7aSLuis R. Rodriguez 
10876bad8766SLuis R. Rodriguez 	if (chan->beacon_found)
10886bad8766SLuis R. Rodriguez 		return;
10896bad8766SLuis R. Rodriguez 
10906bad8766SLuis R. Rodriguez 	chan->beacon_found = true;
10916bad8766SLuis R. Rodriguez 
10926bad8766SLuis R. Rodriguez 	chan_before.center_freq = chan->center_freq;
10936bad8766SLuis R. Rodriguez 	chan_before.flags = chan->flags;
10946bad8766SLuis R. Rodriguez 
1095a4ed90d6SLuis R. Rodriguez 	if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
1096a4ed90d6SLuis R. Rodriguez 	    !(chan->orig_flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
1097e38f8a7aSLuis R. Rodriguez 		chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
10986bad8766SLuis R. Rodriguez 		channel_changed = true;
1099e38f8a7aSLuis R. Rodriguez 	}
1100e38f8a7aSLuis R. Rodriguez 
1101a4ed90d6SLuis R. Rodriguez 	if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
1102a4ed90d6SLuis R. Rodriguez 	    !(chan->orig_flags & IEEE80211_CHAN_NO_IBSS)) {
1103e38f8a7aSLuis R. Rodriguez 		chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
11046bad8766SLuis R. Rodriguez 		channel_changed = true;
1105e38f8a7aSLuis R. Rodriguez 	}
1106e38f8a7aSLuis R. Rodriguez 
11076bad8766SLuis R. Rodriguez 	if (channel_changed)
11086bad8766SLuis R. Rodriguez 		nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1109e38f8a7aSLuis R. Rodriguez }
1110e38f8a7aSLuis R. Rodriguez 
1111e38f8a7aSLuis R. Rodriguez /*
1112e38f8a7aSLuis R. Rodriguez  * Called when a scan on a wiphy finds a beacon on
1113e38f8a7aSLuis R. Rodriguez  * new channel
1114e38f8a7aSLuis R. Rodriguez  */
1115e38f8a7aSLuis R. Rodriguez static void wiphy_update_new_beacon(struct wiphy *wiphy,
1116e38f8a7aSLuis R. Rodriguez 				    struct reg_beacon *reg_beacon)
1117e38f8a7aSLuis R. Rodriguez {
1118e38f8a7aSLuis R. Rodriguez 	unsigned int i;
1119e38f8a7aSLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
1120e38f8a7aSLuis R. Rodriguez 
1121e38f8a7aSLuis R. Rodriguez 	assert_cfg80211_lock();
1122e38f8a7aSLuis R. Rodriguez 
1123e38f8a7aSLuis R. Rodriguez 	if (!wiphy->bands[reg_beacon->chan.band])
1124e38f8a7aSLuis R. Rodriguez 		return;
1125e38f8a7aSLuis R. Rodriguez 
1126e38f8a7aSLuis R. Rodriguez 	sband = wiphy->bands[reg_beacon->chan.band];
1127e38f8a7aSLuis R. Rodriguez 
1128e38f8a7aSLuis R. Rodriguez 	for (i = 0; i < sband->n_channels; i++)
1129e38f8a7aSLuis R. Rodriguez 		handle_reg_beacon(wiphy, i, reg_beacon);
1130e38f8a7aSLuis R. Rodriguez }
1131e38f8a7aSLuis R. Rodriguez 
1132e38f8a7aSLuis R. Rodriguez /*
1133e38f8a7aSLuis R. Rodriguez  * Called upon reg changes or a new wiphy is added
1134e38f8a7aSLuis R. Rodriguez  */
1135e38f8a7aSLuis R. Rodriguez static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1136e38f8a7aSLuis R. Rodriguez {
1137e38f8a7aSLuis R. Rodriguez 	unsigned int i;
1138e38f8a7aSLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
1139e38f8a7aSLuis R. Rodriguez 	struct reg_beacon *reg_beacon;
1140e38f8a7aSLuis R. Rodriguez 
1141e38f8a7aSLuis R. Rodriguez 	assert_cfg80211_lock();
1142e38f8a7aSLuis R. Rodriguez 
1143e38f8a7aSLuis R. Rodriguez 	if (list_empty(&reg_beacon_list))
1144e38f8a7aSLuis R. Rodriguez 		return;
1145e38f8a7aSLuis R. Rodriguez 
1146e38f8a7aSLuis R. Rodriguez 	list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1147e38f8a7aSLuis R. Rodriguez 		if (!wiphy->bands[reg_beacon->chan.band])
1148e38f8a7aSLuis R. Rodriguez 			continue;
1149e38f8a7aSLuis R. Rodriguez 		sband = wiphy->bands[reg_beacon->chan.band];
1150e38f8a7aSLuis R. Rodriguez 		for (i = 0; i < sband->n_channels; i++)
1151e38f8a7aSLuis R. Rodriguez 			handle_reg_beacon(wiphy, i, reg_beacon);
1152e38f8a7aSLuis R. Rodriguez 	}
1153e38f8a7aSLuis R. Rodriguez }
1154e38f8a7aSLuis R. Rodriguez 
1155e38f8a7aSLuis R. Rodriguez static bool reg_is_world_roaming(struct wiphy *wiphy)
1156e38f8a7aSLuis R. Rodriguez {
1157e38f8a7aSLuis R. Rodriguez 	if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1158e38f8a7aSLuis R. Rodriguez 	    (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1159e38f8a7aSLuis R. Rodriguez 		return true;
1160b1ed8dddSLuis R. Rodriguez 	if (last_request &&
1161b1ed8dddSLuis R. Rodriguez 	    last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1162e38f8a7aSLuis R. Rodriguez 	    wiphy->custom_regulatory)
1163e38f8a7aSLuis R. Rodriguez 		return true;
1164e38f8a7aSLuis R. Rodriguez 	return false;
1165e38f8a7aSLuis R. Rodriguez }
1166e38f8a7aSLuis R. Rodriguez 
1167e38f8a7aSLuis R. Rodriguez /* Reap the advantages of previously found beacons */
1168e38f8a7aSLuis R. Rodriguez static void reg_process_beacons(struct wiphy *wiphy)
1169e38f8a7aSLuis R. Rodriguez {
1170b1ed8dddSLuis R. Rodriguez 	/*
1171b1ed8dddSLuis R. Rodriguez 	 * Means we are just firing up cfg80211, so no beacons would
1172b1ed8dddSLuis R. Rodriguez 	 * have been processed yet.
1173b1ed8dddSLuis R. Rodriguez 	 */
1174b1ed8dddSLuis R. Rodriguez 	if (!last_request)
1175b1ed8dddSLuis R. Rodriguez 		return;
1176e38f8a7aSLuis R. Rodriguez 	if (!reg_is_world_roaming(wiphy))
1177e38f8a7aSLuis R. Rodriguez 		return;
1178e38f8a7aSLuis R. Rodriguez 	wiphy_update_beacon_reg(wiphy);
1179e38f8a7aSLuis R. Rodriguez }
1180e38f8a7aSLuis R. Rodriguez 
1181038659e7SLuis R. Rodriguez static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1182038659e7SLuis R. Rodriguez {
1183038659e7SLuis R. Rodriguez 	if (!chan)
1184038659e7SLuis R. Rodriguez 		return true;
1185038659e7SLuis R. Rodriguez 	if (chan->flags & IEEE80211_CHAN_DISABLED)
1186038659e7SLuis R. Rodriguez 		return true;
1187038659e7SLuis R. Rodriguez 	/* This would happen when regulatory rules disallow HT40 completely */
1188038659e7SLuis R. Rodriguez 	if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1189038659e7SLuis R. Rodriguez 		return true;
1190038659e7SLuis R. Rodriguez 	return false;
1191038659e7SLuis R. Rodriguez }
1192038659e7SLuis R. Rodriguez 
1193038659e7SLuis R. Rodriguez static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1194038659e7SLuis R. Rodriguez 					 enum ieee80211_band band,
1195038659e7SLuis R. Rodriguez 					 unsigned int chan_idx)
1196038659e7SLuis R. Rodriguez {
1197038659e7SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
1198038659e7SLuis R. Rodriguez 	struct ieee80211_channel *channel;
1199038659e7SLuis R. Rodriguez 	struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1200038659e7SLuis R. Rodriguez 	unsigned int i;
1201038659e7SLuis R. Rodriguez 
1202038659e7SLuis R. Rodriguez 	assert_cfg80211_lock();
1203038659e7SLuis R. Rodriguez 
1204038659e7SLuis R. Rodriguez 	sband = wiphy->bands[band];
1205038659e7SLuis R. Rodriguez 	BUG_ON(chan_idx >= sband->n_channels);
1206038659e7SLuis R. Rodriguez 	channel = &sband->channels[chan_idx];
1207038659e7SLuis R. Rodriguez 
1208038659e7SLuis R. Rodriguez 	if (is_ht40_not_allowed(channel)) {
1209038659e7SLuis R. Rodriguez 		channel->flags |= IEEE80211_CHAN_NO_HT40;
1210038659e7SLuis R. Rodriguez 		return;
1211038659e7SLuis R. Rodriguez 	}
1212038659e7SLuis R. Rodriguez 
1213038659e7SLuis R. Rodriguez 	/*
1214038659e7SLuis R. Rodriguez 	 * We need to ensure the extension channels exist to
1215038659e7SLuis R. Rodriguez 	 * be able to use HT40- or HT40+, this finds them (or not)
1216038659e7SLuis R. Rodriguez 	 */
1217038659e7SLuis R. Rodriguez 	for (i = 0; i < sband->n_channels; i++) {
1218038659e7SLuis R. Rodriguez 		struct ieee80211_channel *c = &sband->channels[i];
1219038659e7SLuis R. Rodriguez 		if (c->center_freq == (channel->center_freq - 20))
1220038659e7SLuis R. Rodriguez 			channel_before = c;
1221038659e7SLuis R. Rodriguez 		if (c->center_freq == (channel->center_freq + 20))
1222038659e7SLuis R. Rodriguez 			channel_after = c;
1223038659e7SLuis R. Rodriguez 	}
1224038659e7SLuis R. Rodriguez 
1225038659e7SLuis R. Rodriguez 	/*
1226038659e7SLuis R. Rodriguez 	 * Please note that this assumes target bandwidth is 20 MHz,
1227038659e7SLuis R. Rodriguez 	 * if that ever changes we also need to change the below logic
1228038659e7SLuis R. Rodriguez 	 * to include that as well.
1229038659e7SLuis R. Rodriguez 	 */
1230038659e7SLuis R. Rodriguez 	if (is_ht40_not_allowed(channel_before))
1231689da1b3SLuis R. Rodriguez 		channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1232038659e7SLuis R. Rodriguez 	else
1233689da1b3SLuis R. Rodriguez 		channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1234038659e7SLuis R. Rodriguez 
1235038659e7SLuis R. Rodriguez 	if (is_ht40_not_allowed(channel_after))
1236689da1b3SLuis R. Rodriguez 		channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1237038659e7SLuis R. Rodriguez 	else
1238689da1b3SLuis R. Rodriguez 		channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1239038659e7SLuis R. Rodriguez }
1240038659e7SLuis R. Rodriguez 
1241038659e7SLuis R. Rodriguez static void reg_process_ht_flags_band(struct wiphy *wiphy,
1242038659e7SLuis R. Rodriguez 				      enum ieee80211_band band)
1243038659e7SLuis R. Rodriguez {
1244038659e7SLuis R. Rodriguez 	unsigned int i;
1245038659e7SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
1246038659e7SLuis R. Rodriguez 
1247038659e7SLuis R. Rodriguez 	BUG_ON(!wiphy->bands[band]);
1248038659e7SLuis R. Rodriguez 	sband = wiphy->bands[band];
1249038659e7SLuis R. Rodriguez 
1250038659e7SLuis R. Rodriguez 	for (i = 0; i < sband->n_channels; i++)
1251038659e7SLuis R. Rodriguez 		reg_process_ht_flags_channel(wiphy, band, i);
1252038659e7SLuis R. Rodriguez }
1253038659e7SLuis R. Rodriguez 
1254038659e7SLuis R. Rodriguez static void reg_process_ht_flags(struct wiphy *wiphy)
1255038659e7SLuis R. Rodriguez {
1256038659e7SLuis R. Rodriguez 	enum ieee80211_band band;
1257038659e7SLuis R. Rodriguez 
1258038659e7SLuis R. Rodriguez 	if (!wiphy)
1259038659e7SLuis R. Rodriguez 		return;
1260038659e7SLuis R. Rodriguez 
1261038659e7SLuis R. Rodriguez 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1262038659e7SLuis R. Rodriguez 		if (wiphy->bands[band])
1263038659e7SLuis R. Rodriguez 			reg_process_ht_flags_band(wiphy, band);
1264038659e7SLuis R. Rodriguez 	}
1265038659e7SLuis R. Rodriguez 
1266038659e7SLuis R. Rodriguez }
1267038659e7SLuis R. Rodriguez 
12687db90f4aSLuis R. Rodriguez void wiphy_update_regulatory(struct wiphy *wiphy,
12697db90f4aSLuis R. Rodriguez 			     enum nl80211_reg_initiator initiator)
12708318d78aSJohannes Berg {
12718318d78aSJohannes Berg 	enum ieee80211_band band;
1272d46e5b1dSLuis R. Rodriguez 
12737db90f4aSLuis R. Rodriguez 	if (ignore_reg_update(wiphy, initiator))
1274e38f8a7aSLuis R. Rodriguez 		goto out;
1275b2e1b302SLuis R. Rodriguez 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
12768318d78aSJohannes Berg 		if (wiphy->bands[band])
1277a92a3ce7SLuis R. Rodriguez 			handle_band(wiphy, band);
1278b2e1b302SLuis R. Rodriguez 	}
1279e38f8a7aSLuis R. Rodriguez out:
1280e38f8a7aSLuis R. Rodriguez 	reg_process_beacons(wiphy);
1281038659e7SLuis R. Rodriguez 	reg_process_ht_flags(wiphy);
1282b2e1b302SLuis R. Rodriguez 	if (wiphy->reg_notifier)
1283716f9392SLuis R. Rodriguez 		wiphy->reg_notifier(wiphy, last_request);
1284b2e1b302SLuis R. Rodriguez }
1285b2e1b302SLuis R. Rodriguez 
12861fa25e41SLuis R. Rodriguez static void handle_channel_custom(struct wiphy *wiphy,
12871fa25e41SLuis R. Rodriguez 				  enum ieee80211_band band,
12881fa25e41SLuis R. Rodriguez 				  unsigned int chan_idx,
12891fa25e41SLuis R. Rodriguez 				  const struct ieee80211_regdomain *regd)
12901fa25e41SLuis R. Rodriguez {
12911fa25e41SLuis R. Rodriguez 	int r;
1292038659e7SLuis R. Rodriguez 	u32 desired_bw_khz = MHZ_TO_KHZ(20);
1293038659e7SLuis R. Rodriguez 	u32 bw_flags = 0;
12941fa25e41SLuis R. Rodriguez 	const struct ieee80211_reg_rule *reg_rule = NULL;
12951fa25e41SLuis R. Rodriguez 	const struct ieee80211_power_rule *power_rule = NULL;
1296038659e7SLuis R. Rodriguez 	const struct ieee80211_freq_range *freq_range = NULL;
12971fa25e41SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
12981fa25e41SLuis R. Rodriguez 	struct ieee80211_channel *chan;
12991fa25e41SLuis R. Rodriguez 
1300ac46d48eSLuis R. Rodriguez 	assert_cfg80211_lock();
1301ac46d48eSLuis R. Rodriguez 
13021fa25e41SLuis R. Rodriguez 	sband = wiphy->bands[band];
13031fa25e41SLuis R. Rodriguez 	BUG_ON(chan_idx >= sband->n_channels);
13041fa25e41SLuis R. Rodriguez 	chan = &sband->channels[chan_idx];
13051fa25e41SLuis R. Rodriguez 
1306038659e7SLuis R. Rodriguez 	r = freq_reg_info_regd(wiphy,
1307038659e7SLuis R. Rodriguez 			       MHZ_TO_KHZ(chan->center_freq),
1308038659e7SLuis R. Rodriguez 			       desired_bw_khz,
1309038659e7SLuis R. Rodriguez 			       &reg_rule,
1310038659e7SLuis R. Rodriguez 			       regd);
13111fa25e41SLuis R. Rodriguez 
13121fa25e41SLuis R. Rodriguez 	if (r) {
13131fa25e41SLuis R. Rodriguez 		chan->flags = IEEE80211_CHAN_DISABLED;
13141fa25e41SLuis R. Rodriguez 		return;
13151fa25e41SLuis R. Rodriguez 	}
13161fa25e41SLuis R. Rodriguez 
13171fa25e41SLuis R. Rodriguez 	power_rule = &reg_rule->power_rule;
1318038659e7SLuis R. Rodriguez 	freq_range = &reg_rule->freq_range;
13191fa25e41SLuis R. Rodriguez 
1320038659e7SLuis R. Rodriguez 	if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1321038659e7SLuis R. Rodriguez 		bw_flags = IEEE80211_CHAN_NO_HT40;
1322038659e7SLuis R. Rodriguez 
1323038659e7SLuis R. Rodriguez 	chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
13241fa25e41SLuis R. Rodriguez 	chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1325038659e7SLuis R. Rodriguez 	chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
13261fa25e41SLuis R. Rodriguez 	chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
13271fa25e41SLuis R. Rodriguez }
13281fa25e41SLuis R. Rodriguez 
13291fa25e41SLuis R. Rodriguez static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
13301fa25e41SLuis R. Rodriguez 			       const struct ieee80211_regdomain *regd)
13311fa25e41SLuis R. Rodriguez {
13321fa25e41SLuis R. Rodriguez 	unsigned int i;
13331fa25e41SLuis R. Rodriguez 	struct ieee80211_supported_band *sband;
13341fa25e41SLuis R. Rodriguez 
13351fa25e41SLuis R. Rodriguez 	BUG_ON(!wiphy->bands[band]);
13361fa25e41SLuis R. Rodriguez 	sband = wiphy->bands[band];
13371fa25e41SLuis R. Rodriguez 
13381fa25e41SLuis R. Rodriguez 	for (i = 0; i < sband->n_channels; i++)
13391fa25e41SLuis R. Rodriguez 		handle_channel_custom(wiphy, band, i, regd);
13401fa25e41SLuis R. Rodriguez }
13411fa25e41SLuis R. Rodriguez 
13421fa25e41SLuis R. Rodriguez /* Used by drivers prior to wiphy registration */
13431fa25e41SLuis R. Rodriguez void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
13441fa25e41SLuis R. Rodriguez 				   const struct ieee80211_regdomain *regd)
13451fa25e41SLuis R. Rodriguez {
13461fa25e41SLuis R. Rodriguez 	enum ieee80211_band band;
1347bbcf3f02SLuis R. Rodriguez 	unsigned int bands_set = 0;
1348ac46d48eSLuis R. Rodriguez 
1349ac46d48eSLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
13501fa25e41SLuis R. Rodriguez 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1351bbcf3f02SLuis R. Rodriguez 		if (!wiphy->bands[band])
1352bbcf3f02SLuis R. Rodriguez 			continue;
13531fa25e41SLuis R. Rodriguez 		handle_band_custom(wiphy, band, regd);
1354bbcf3f02SLuis R. Rodriguez 		bands_set++;
13551fa25e41SLuis R. Rodriguez 	}
1356ac46d48eSLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
1357bbcf3f02SLuis R. Rodriguez 
1358bbcf3f02SLuis R. Rodriguez 	/*
1359bbcf3f02SLuis R. Rodriguez 	 * no point in calling this if it won't have any effect
1360bbcf3f02SLuis R. Rodriguez 	 * on your device's supportd bands.
1361bbcf3f02SLuis R. Rodriguez 	 */
1362bbcf3f02SLuis R. Rodriguez 	WARN_ON(!bands_set);
13631fa25e41SLuis R. Rodriguez }
13641fa25e41SLuis R. Rodriguez EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
13651fa25e41SLuis R. Rodriguez 
13663e0c3ff3SLuis R. Rodriguez static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
13673e0c3ff3SLuis R. Rodriguez 			 const struct ieee80211_regdomain *src_regd)
13683e0c3ff3SLuis R. Rodriguez {
13693e0c3ff3SLuis R. Rodriguez 	struct ieee80211_regdomain *regd;
13703e0c3ff3SLuis R. Rodriguez 	int size_of_regd = 0;
13713e0c3ff3SLuis R. Rodriguez 	unsigned int i;
13723e0c3ff3SLuis R. Rodriguez 
13733e0c3ff3SLuis R. Rodriguez 	size_of_regd = sizeof(struct ieee80211_regdomain) +
13743e0c3ff3SLuis R. Rodriguez 	  ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
13753e0c3ff3SLuis R. Rodriguez 
13763e0c3ff3SLuis R. Rodriguez 	regd = kzalloc(size_of_regd, GFP_KERNEL);
13773e0c3ff3SLuis R. Rodriguez 	if (!regd)
13783e0c3ff3SLuis R. Rodriguez 		return -ENOMEM;
13793e0c3ff3SLuis R. Rodriguez 
13803e0c3ff3SLuis R. Rodriguez 	memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
13813e0c3ff3SLuis R. Rodriguez 
13823e0c3ff3SLuis R. Rodriguez 	for (i = 0; i < src_regd->n_reg_rules; i++)
13833e0c3ff3SLuis R. Rodriguez 		memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
13843e0c3ff3SLuis R. Rodriguez 			sizeof(struct ieee80211_reg_rule));
13853e0c3ff3SLuis R. Rodriguez 
13863e0c3ff3SLuis R. Rodriguez 	*dst_regd = regd;
13873e0c3ff3SLuis R. Rodriguez 	return 0;
1388b2e1b302SLuis R. Rodriguez }
1389b2e1b302SLuis R. Rodriguez 
1390fb1fc7adSLuis R. Rodriguez /*
1391fb1fc7adSLuis R. Rodriguez  * Return value which can be used by ignore_request() to indicate
1392fb1fc7adSLuis R. Rodriguez  * it has been determined we should intersect two regulatory domains
1393fb1fc7adSLuis R. Rodriguez  */
13949c96477dSLuis R. Rodriguez #define REG_INTERSECT	1
13959c96477dSLuis R. Rodriguez 
139684fa4f43SJohannes Berg /* This has the logic which determines when a new request
139784fa4f43SJohannes Berg  * should be ignored. */
13982f92cd2eSLuis R. Rodriguez static int ignore_request(struct wiphy *wiphy,
13992f92cd2eSLuis R. Rodriguez 			  struct regulatory_request *pending_request)
140084fa4f43SJohannes Berg {
1401806a9e39SLuis R. Rodriguez 	struct wiphy *last_wiphy = NULL;
1402761cf7ecSLuis R. Rodriguez 
1403761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
1404761cf7ecSLuis R. Rodriguez 
140584fa4f43SJohannes Berg 	/* All initial requests are respected */
140684fa4f43SJohannes Berg 	if (!last_request)
140784fa4f43SJohannes Berg 		return 0;
140884fa4f43SJohannes Berg 
14092f92cd2eSLuis R. Rodriguez 	switch (pending_request->initiator) {
14107db90f4aSLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_CORE:
1411ba25c141SLuis R. Rodriguez 		return -EINVAL;
14127db90f4aSLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1413806a9e39SLuis R. Rodriguez 
1414806a9e39SLuis R. Rodriguez 		last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1415806a9e39SLuis R. Rodriguez 
14162f92cd2eSLuis R. Rodriguez 		if (unlikely(!is_an_alpha2(pending_request->alpha2)))
141784fa4f43SJohannes Berg 			return -EINVAL;
14187db90f4aSLuis R. Rodriguez 		if (last_request->initiator ==
14197db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1420806a9e39SLuis R. Rodriguez 			if (last_wiphy != wiphy) {
142184fa4f43SJohannes Berg 				/*
142284fa4f43SJohannes Berg 				 * Two cards with two APs claiming different
142384fa4f43SJohannes Berg 				 * different Country IE alpha2s. We could
142484fa4f43SJohannes Berg 				 * intersect them, but that seems unlikely
142584fa4f43SJohannes Berg 				 * to be correct. Reject second one for now.
142684fa4f43SJohannes Berg 				 */
14272f92cd2eSLuis R. Rodriguez 				if (regdom_changes(pending_request->alpha2))
142884fa4f43SJohannes Berg 					return -EOPNOTSUPP;
142984fa4f43SJohannes Berg 				return -EALREADY;
143084fa4f43SJohannes Berg 			}
1431fb1fc7adSLuis R. Rodriguez 			/*
1432fb1fc7adSLuis R. Rodriguez 			 * Two consecutive Country IE hints on the same wiphy.
1433fb1fc7adSLuis R. Rodriguez 			 * This should be picked up early by the driver/stack
1434fb1fc7adSLuis R. Rodriguez 			 */
14352f92cd2eSLuis R. Rodriguez 			if (WARN_ON(regdom_changes(pending_request->alpha2)))
143684fa4f43SJohannes Berg 				return 0;
143784fa4f43SJohannes Berg 			return -EALREADY;
143884fa4f43SJohannes Berg 		}
14393f2355cbSLuis R. Rodriguez 		return REG_INTERSECT;
14407db90f4aSLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_DRIVER:
14417db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1442e74b1e7fSLuis R. Rodriguez 			if (is_old_static_regdom(cfg80211_regdomain))
144384fa4f43SJohannes Berg 				return 0;
14442f92cd2eSLuis R. Rodriguez 			if (regdom_changes(pending_request->alpha2))
1445e74b1e7fSLuis R. Rodriguez 				return 0;
1446e74b1e7fSLuis R. Rodriguez 			return -EALREADY;
1447e74b1e7fSLuis R. Rodriguez 		}
1448fff32c04SLuis R. Rodriguez 
1449fff32c04SLuis R. Rodriguez 		/*
1450fff32c04SLuis R. Rodriguez 		 * This would happen if you unplug and plug your card
1451fff32c04SLuis R. Rodriguez 		 * back in or if you add a new device for which the previously
1452fff32c04SLuis R. Rodriguez 		 * loaded card also agrees on the regulatory domain.
1453fff32c04SLuis R. Rodriguez 		 */
14547db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
14552f92cd2eSLuis R. Rodriguez 		    !regdom_changes(pending_request->alpha2))
1456fff32c04SLuis R. Rodriguez 			return -EALREADY;
1457fff32c04SLuis R. Rodriguez 
14583e0c3ff3SLuis R. Rodriguez 		return REG_INTERSECT;
14597db90f4aSLuis R. Rodriguez 	case NL80211_REGDOM_SET_BY_USER:
14607db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
14619c96477dSLuis R. Rodriguez 			return REG_INTERSECT;
1462fb1fc7adSLuis R. Rodriguez 		/*
1463fb1fc7adSLuis R. Rodriguez 		 * If the user knows better the user should set the regdom
1464fb1fc7adSLuis R. Rodriguez 		 * to their country before the IE is picked up
1465fb1fc7adSLuis R. Rodriguez 		 */
14667db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
14673f2355cbSLuis R. Rodriguez 			  last_request->intersect)
14683f2355cbSLuis R. Rodriguez 			return -EOPNOTSUPP;
1469fb1fc7adSLuis R. Rodriguez 		/*
1470fb1fc7adSLuis R. Rodriguez 		 * Process user requests only after previous user/driver/core
1471fb1fc7adSLuis R. Rodriguez 		 * requests have been processed
1472fb1fc7adSLuis R. Rodriguez 		 */
14737db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
14747db90f4aSLuis R. Rodriguez 		    last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
14757db90f4aSLuis R. Rodriguez 		    last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
147669b1572bSLuis R. Rodriguez 			if (regdom_changes(last_request->alpha2))
14775eebade6SLuis R. Rodriguez 				return -EAGAIN;
14785eebade6SLuis R. Rodriguez 		}
14795eebade6SLuis R. Rodriguez 
1480e74b1e7fSLuis R. Rodriguez 		if (!is_old_static_regdom(cfg80211_regdomain) &&
14812f92cd2eSLuis R. Rodriguez 		    !regdom_changes(pending_request->alpha2))
1482e74b1e7fSLuis R. Rodriguez 			return -EALREADY;
1483e74b1e7fSLuis R. Rodriguez 
148484fa4f43SJohannes Berg 		return 0;
148584fa4f43SJohannes Berg 	}
148684fa4f43SJohannes Berg 
148784fa4f43SJohannes Berg 	return -EINVAL;
148884fa4f43SJohannes Berg }
148984fa4f43SJohannes Berg 
1490d1c96a9aSLuis R. Rodriguez /**
1491d1c96a9aSLuis R. Rodriguez  * __regulatory_hint - hint to the wireless core a regulatory domain
1492d1c96a9aSLuis R. Rodriguez  * @wiphy: if the hint comes from country information from an AP, this
1493d1c96a9aSLuis R. Rodriguez  *	is required to be set to the wiphy that received the information
149428da32d7SLuis R. Rodriguez  * @pending_request: the regulatory request currently being processed
1495d1c96a9aSLuis R. Rodriguez  *
1496d1c96a9aSLuis R. Rodriguez  * The Wireless subsystem can use this function to hint to the wireless core
149728da32d7SLuis R. Rodriguez  * what it believes should be the current regulatory domain.
1498d1c96a9aSLuis R. Rodriguez  *
1499d1c96a9aSLuis R. Rodriguez  * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1500d1c96a9aSLuis R. Rodriguez  * already been set or other standard error codes.
1501d1c96a9aSLuis R. Rodriguez  *
1502d1c96a9aSLuis R. Rodriguez  * Caller must hold &cfg80211_mutex
1503d1c96a9aSLuis R. Rodriguez  */
150428da32d7SLuis R. Rodriguez static int __regulatory_hint(struct wiphy *wiphy,
150528da32d7SLuis R. Rodriguez 			     struct regulatory_request *pending_request)
1506b2e1b302SLuis R. Rodriguez {
15079c96477dSLuis R. Rodriguez 	bool intersect = false;
1508b2e1b302SLuis R. Rodriguez 	int r = 0;
1509b2e1b302SLuis R. Rodriguez 
1510761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
1511761cf7ecSLuis R. Rodriguez 
15122f92cd2eSLuis R. Rodriguez 	r = ignore_request(wiphy, pending_request);
15139c96477dSLuis R. Rodriguez 
15143e0c3ff3SLuis R. Rodriguez 	if (r == REG_INTERSECT) {
15157db90f4aSLuis R. Rodriguez 		if (pending_request->initiator ==
15167db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_DRIVER) {
15173e0c3ff3SLuis R. Rodriguez 			r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1518d951c1ddSLuis R. Rodriguez 			if (r) {
1519d951c1ddSLuis R. Rodriguez 				kfree(pending_request);
1520b2e1b302SLuis R. Rodriguez 				return r;
15213e0c3ff3SLuis R. Rodriguez 			}
1522d951c1ddSLuis R. Rodriguez 		}
15233e0c3ff3SLuis R. Rodriguez 		intersect = true;
15243e0c3ff3SLuis R. Rodriguez 	} else if (r) {
1525fb1fc7adSLuis R. Rodriguez 		/*
1526fb1fc7adSLuis R. Rodriguez 		 * If the regulatory domain being requested by the
15273e0c3ff3SLuis R. Rodriguez 		 * driver has already been set just copy it to the
1528fb1fc7adSLuis R. Rodriguez 		 * wiphy
1529fb1fc7adSLuis R. Rodriguez 		 */
153028da32d7SLuis R. Rodriguez 		if (r == -EALREADY &&
15317db90f4aSLuis R. Rodriguez 		    pending_request->initiator ==
15327db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_DRIVER) {
15333e0c3ff3SLuis R. Rodriguez 			r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1534d951c1ddSLuis R. Rodriguez 			if (r) {
1535d951c1ddSLuis R. Rodriguez 				kfree(pending_request);
15363e0c3ff3SLuis R. Rodriguez 				return r;
1537d951c1ddSLuis R. Rodriguez 			}
15383e0c3ff3SLuis R. Rodriguez 			r = -EALREADY;
15393e0c3ff3SLuis R. Rodriguez 			goto new_request;
15403e0c3ff3SLuis R. Rodriguez 		}
1541d951c1ddSLuis R. Rodriguez 		kfree(pending_request);
15423e0c3ff3SLuis R. Rodriguez 		return r;
15433e0c3ff3SLuis R. Rodriguez 	}
1544b2e1b302SLuis R. Rodriguez 
15453e0c3ff3SLuis R. Rodriguez new_request:
1546f6037d09SJohannes Berg 	kfree(last_request);
1547d951c1ddSLuis R. Rodriguez 
1548d951c1ddSLuis R. Rodriguez 	last_request = pending_request;
1549d951c1ddSLuis R. Rodriguez 	last_request->intersect = intersect;
1550d951c1ddSLuis R. Rodriguez 
1551d951c1ddSLuis R. Rodriguez 	pending_request = NULL;
15523e0c3ff3SLuis R. Rodriguez 
15533e0c3ff3SLuis R. Rodriguez 	/* When r == REG_INTERSECT we do need to call CRDA */
155473d54c9eSLuis R. Rodriguez 	if (r < 0) {
155573d54c9eSLuis R. Rodriguez 		/*
155673d54c9eSLuis R. Rodriguez 		 * Since CRDA will not be called in this case as we already
155773d54c9eSLuis R. Rodriguez 		 * have applied the requested regulatory domain before we just
155873d54c9eSLuis R. Rodriguez 		 * inform userspace we have processed the request
155973d54c9eSLuis R. Rodriguez 		 */
156073d54c9eSLuis R. Rodriguez 		if (r == -EALREADY)
156173d54c9eSLuis R. Rodriguez 			nl80211_send_reg_change_event(last_request);
15623e0c3ff3SLuis R. Rodriguez 		return r;
156373d54c9eSLuis R. Rodriguez 	}
15643e0c3ff3SLuis R. Rodriguez 
1565d951c1ddSLuis R. Rodriguez 	return call_crda(last_request->alpha2);
1566b2e1b302SLuis R. Rodriguez }
1567b2e1b302SLuis R. Rodriguez 
156830a548c7SLuis R. Rodriguez /* This processes *all* regulatory hints */
1569d951c1ddSLuis R. Rodriguez static void reg_process_hint(struct regulatory_request *reg_request)
1570fe33eb39SLuis R. Rodriguez {
1571fe33eb39SLuis R. Rodriguez 	int r = 0;
1572fe33eb39SLuis R. Rodriguez 	struct wiphy *wiphy = NULL;
1573fe33eb39SLuis R. Rodriguez 
1574fe33eb39SLuis R. Rodriguez 	BUG_ON(!reg_request->alpha2);
1575fe33eb39SLuis R. Rodriguez 
1576fe33eb39SLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
1577fe33eb39SLuis R. Rodriguez 
1578fe33eb39SLuis R. Rodriguez 	if (wiphy_idx_valid(reg_request->wiphy_idx))
1579fe33eb39SLuis R. Rodriguez 		wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1580fe33eb39SLuis R. Rodriguez 
15817db90f4aSLuis R. Rodriguez 	if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1582fe33eb39SLuis R. Rodriguez 	    !wiphy) {
1583d951c1ddSLuis R. Rodriguez 		kfree(reg_request);
1584fe33eb39SLuis R. Rodriguez 		goto out;
1585fe33eb39SLuis R. Rodriguez 	}
1586fe33eb39SLuis R. Rodriguez 
158728da32d7SLuis R. Rodriguez 	r = __regulatory_hint(wiphy, reg_request);
1588fe33eb39SLuis R. Rodriguez 	/* This is required so that the orig_* parameters are saved */
1589fe33eb39SLuis R. Rodriguez 	if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1590fe33eb39SLuis R. Rodriguez 		wiphy_update_regulatory(wiphy, reg_request->initiator);
1591fe33eb39SLuis R. Rodriguez out:
1592fe33eb39SLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
1593fe33eb39SLuis R. Rodriguez }
1594fe33eb39SLuis R. Rodriguez 
15957db90f4aSLuis R. Rodriguez /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
1596fe33eb39SLuis R. Rodriguez static void reg_process_pending_hints(void)
1597fe33eb39SLuis R. Rodriguez 	{
1598fe33eb39SLuis R. Rodriguez 	struct regulatory_request *reg_request;
1599fe33eb39SLuis R. Rodriguez 
1600fe33eb39SLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
1601fe33eb39SLuis R. Rodriguez 	while (!list_empty(&reg_requests_list)) {
1602fe33eb39SLuis R. Rodriguez 		reg_request = list_first_entry(&reg_requests_list,
1603fe33eb39SLuis R. Rodriguez 					       struct regulatory_request,
1604fe33eb39SLuis R. Rodriguez 					       list);
1605fe33eb39SLuis R. Rodriguez 		list_del_init(&reg_request->list);
1606fe33eb39SLuis R. Rodriguez 
1607d951c1ddSLuis R. Rodriguez 		spin_unlock(&reg_requests_lock);
1608d951c1ddSLuis R. Rodriguez 		reg_process_hint(reg_request);
1609fe33eb39SLuis R. Rodriguez 		spin_lock(&reg_requests_lock);
1610fe33eb39SLuis R. Rodriguez 	}
1611fe33eb39SLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
1612fe33eb39SLuis R. Rodriguez }
1613fe33eb39SLuis R. Rodriguez 
1614e38f8a7aSLuis R. Rodriguez /* Processes beacon hints -- this has nothing to do with country IEs */
1615e38f8a7aSLuis R. Rodriguez static void reg_process_pending_beacon_hints(void)
1616e38f8a7aSLuis R. Rodriguez {
1617e38f8a7aSLuis R. Rodriguez 	struct cfg80211_registered_device *drv;
1618e38f8a7aSLuis R. Rodriguez 	struct reg_beacon *pending_beacon, *tmp;
1619e38f8a7aSLuis R. Rodriguez 
1620e38f8a7aSLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
1621e38f8a7aSLuis R. Rodriguez 
1622e38f8a7aSLuis R. Rodriguez 	/* This goes through the _pending_ beacon list */
1623e38f8a7aSLuis R. Rodriguez 	spin_lock_bh(&reg_pending_beacons_lock);
1624e38f8a7aSLuis R. Rodriguez 
1625e38f8a7aSLuis R. Rodriguez 	if (list_empty(&reg_pending_beacons)) {
1626e38f8a7aSLuis R. Rodriguez 		spin_unlock_bh(&reg_pending_beacons_lock);
1627e38f8a7aSLuis R. Rodriguez 		goto out;
1628e38f8a7aSLuis R. Rodriguez 	}
1629e38f8a7aSLuis R. Rodriguez 
1630e38f8a7aSLuis R. Rodriguez 	list_for_each_entry_safe(pending_beacon, tmp,
1631e38f8a7aSLuis R. Rodriguez 				 &reg_pending_beacons, list) {
1632e38f8a7aSLuis R. Rodriguez 
1633e38f8a7aSLuis R. Rodriguez 		list_del_init(&pending_beacon->list);
1634e38f8a7aSLuis R. Rodriguez 
1635e38f8a7aSLuis R. Rodriguez 		/* Applies the beacon hint to current wiphys */
1636e38f8a7aSLuis R. Rodriguez 		list_for_each_entry(drv, &cfg80211_drv_list, list)
1637e38f8a7aSLuis R. Rodriguez 			wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1638e38f8a7aSLuis R. Rodriguez 
1639e38f8a7aSLuis R. Rodriguez 		/* Remembers the beacon hint for new wiphys or reg changes */
1640e38f8a7aSLuis R. Rodriguez 		list_add_tail(&pending_beacon->list, &reg_beacon_list);
1641e38f8a7aSLuis R. Rodriguez 	}
1642e38f8a7aSLuis R. Rodriguez 
1643e38f8a7aSLuis R. Rodriguez 	spin_unlock_bh(&reg_pending_beacons_lock);
1644e38f8a7aSLuis R. Rodriguez out:
1645e38f8a7aSLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
1646e38f8a7aSLuis R. Rodriguez }
1647e38f8a7aSLuis R. Rodriguez 
1648fe33eb39SLuis R. Rodriguez static void reg_todo(struct work_struct *work)
1649fe33eb39SLuis R. Rodriguez {
1650fe33eb39SLuis R. Rodriguez 	reg_process_pending_hints();
1651e38f8a7aSLuis R. Rodriguez 	reg_process_pending_beacon_hints();
1652fe33eb39SLuis R. Rodriguez }
1653fe33eb39SLuis R. Rodriguez 
1654fe33eb39SLuis R. Rodriguez static DECLARE_WORK(reg_work, reg_todo);
1655fe33eb39SLuis R. Rodriguez 
1656fe33eb39SLuis R. Rodriguez static void queue_regulatory_request(struct regulatory_request *request)
1657fe33eb39SLuis R. Rodriguez {
1658fe33eb39SLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
1659fe33eb39SLuis R. Rodriguez 	list_add_tail(&request->list, &reg_requests_list);
1660fe33eb39SLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
1661fe33eb39SLuis R. Rodriguez 
1662fe33eb39SLuis R. Rodriguez 	schedule_work(&reg_work);
1663fe33eb39SLuis R. Rodriguez }
1664fe33eb39SLuis R. Rodriguez 
1665fe33eb39SLuis R. Rodriguez /* Core regulatory hint -- happens once during cfg80211_init() */
1666ba25c141SLuis R. Rodriguez static int regulatory_hint_core(const char *alpha2)
1667ba25c141SLuis R. Rodriguez {
1668ba25c141SLuis R. Rodriguez 	struct regulatory_request *request;
1669ba25c141SLuis R. Rodriguez 
1670ba25c141SLuis R. Rodriguez 	BUG_ON(last_request);
1671ba25c141SLuis R. Rodriguez 
1672ba25c141SLuis R. Rodriguez 	request = kzalloc(sizeof(struct regulatory_request),
1673ba25c141SLuis R. Rodriguez 			  GFP_KERNEL);
1674ba25c141SLuis R. Rodriguez 	if (!request)
1675ba25c141SLuis R. Rodriguez 		return -ENOMEM;
1676ba25c141SLuis R. Rodriguez 
1677ba25c141SLuis R. Rodriguez 	request->alpha2[0] = alpha2[0];
1678ba25c141SLuis R. Rodriguez 	request->alpha2[1] = alpha2[1];
16797db90f4aSLuis R. Rodriguez 	request->initiator = NL80211_REGDOM_SET_BY_CORE;
1680ba25c141SLuis R. Rodriguez 
1681fe33eb39SLuis R. Rodriguez 	queue_regulatory_request(request);
1682ba25c141SLuis R. Rodriguez 
16835078b2e3SLuis R. Rodriguez 	/*
16845078b2e3SLuis R. Rodriguez 	 * This ensures last_request is populated once modules
16855078b2e3SLuis R. Rodriguez 	 * come swinging in and calling regulatory hints and
16865078b2e3SLuis R. Rodriguez 	 * wiphy_apply_custom_regulatory().
16875078b2e3SLuis R. Rodriguez 	 */
16885078b2e3SLuis R. Rodriguez 	flush_scheduled_work();
16895078b2e3SLuis R. Rodriguez 
1690fe33eb39SLuis R. Rodriguez 	return 0;
1691ba25c141SLuis R. Rodriguez }
1692ba25c141SLuis R. Rodriguez 
1693fe33eb39SLuis R. Rodriguez /* User hints */
1694fe33eb39SLuis R. Rodriguez int regulatory_hint_user(const char *alpha2)
1695b2e1b302SLuis R. Rodriguez {
1696fe33eb39SLuis R. Rodriguez 	struct regulatory_request *request;
1697fe33eb39SLuis R. Rodriguez 
1698be3d4810SJohannes Berg 	BUG_ON(!alpha2);
1699b2e1b302SLuis R. Rodriguez 
1700fe33eb39SLuis R. Rodriguez 	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1701fe33eb39SLuis R. Rodriguez 	if (!request)
1702fe33eb39SLuis R. Rodriguez 		return -ENOMEM;
1703fe33eb39SLuis R. Rodriguez 
1704fe33eb39SLuis R. Rodriguez 	request->wiphy_idx = WIPHY_IDX_STALE;
1705fe33eb39SLuis R. Rodriguez 	request->alpha2[0] = alpha2[0];
1706fe33eb39SLuis R. Rodriguez 	request->alpha2[1] = alpha2[1];
17077db90f4aSLuis R. Rodriguez 	request->initiator = NL80211_REGDOM_SET_BY_USER,
1708fe33eb39SLuis R. Rodriguez 
1709fe33eb39SLuis R. Rodriguez 	queue_regulatory_request(request);
1710fe33eb39SLuis R. Rodriguez 
1711fe33eb39SLuis R. Rodriguez 	return 0;
1712fe33eb39SLuis R. Rodriguez }
1713fe33eb39SLuis R. Rodriguez 
1714fe33eb39SLuis R. Rodriguez /* Driver hints */
1715fe33eb39SLuis R. Rodriguez int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1716fe33eb39SLuis R. Rodriguez {
1717fe33eb39SLuis R. Rodriguez 	struct regulatory_request *request;
1718fe33eb39SLuis R. Rodriguez 
1719fe33eb39SLuis R. Rodriguez 	BUG_ON(!alpha2);
1720fe33eb39SLuis R. Rodriguez 	BUG_ON(!wiphy);
1721fe33eb39SLuis R. Rodriguez 
1722fe33eb39SLuis R. Rodriguez 	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1723fe33eb39SLuis R. Rodriguez 	if (!request)
1724fe33eb39SLuis R. Rodriguez 		return -ENOMEM;
1725fe33eb39SLuis R. Rodriguez 
1726fe33eb39SLuis R. Rodriguez 	request->wiphy_idx = get_wiphy_idx(wiphy);
1727fe33eb39SLuis R. Rodriguez 
1728fe33eb39SLuis R. Rodriguez 	/* Must have registered wiphy first */
1729fe33eb39SLuis R. Rodriguez 	BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1730fe33eb39SLuis R. Rodriguez 
1731fe33eb39SLuis R. Rodriguez 	request->alpha2[0] = alpha2[0];
1732fe33eb39SLuis R. Rodriguez 	request->alpha2[1] = alpha2[1];
17337db90f4aSLuis R. Rodriguez 	request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1734fe33eb39SLuis R. Rodriguez 
1735fe33eb39SLuis R. Rodriguez 	queue_regulatory_request(request);
1736fe33eb39SLuis R. Rodriguez 
1737fe33eb39SLuis R. Rodriguez 	return 0;
1738b2e1b302SLuis R. Rodriguez }
1739b2e1b302SLuis R. Rodriguez EXPORT_SYMBOL(regulatory_hint);
1740b2e1b302SLuis R. Rodriguez 
17413f2355cbSLuis R. Rodriguez static bool reg_same_country_ie_hint(struct wiphy *wiphy,
17423f2355cbSLuis R. Rodriguez 			u32 country_ie_checksum)
17433f2355cbSLuis R. Rodriguez {
1744806a9e39SLuis R. Rodriguez 	struct wiphy *request_wiphy;
1745806a9e39SLuis R. Rodriguez 
1746761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
1747761cf7ecSLuis R. Rodriguez 
1748cc0b6fe8SLuis R. Rodriguez 	if (unlikely(last_request->initiator !=
1749cc0b6fe8SLuis R. Rodriguez 	    NL80211_REGDOM_SET_BY_COUNTRY_IE))
1750cc0b6fe8SLuis R. Rodriguez 		return false;
1751cc0b6fe8SLuis R. Rodriguez 
1752806a9e39SLuis R. Rodriguez 	request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1753806a9e39SLuis R. Rodriguez 
1754806a9e39SLuis R. Rodriguez 	if (!request_wiphy)
17553f2355cbSLuis R. Rodriguez 		return false;
1756806a9e39SLuis R. Rodriguez 
1757806a9e39SLuis R. Rodriguez 	if (likely(request_wiphy != wiphy))
17583f2355cbSLuis R. Rodriguez 		return !country_ie_integrity_changes(country_ie_checksum);
1759fb1fc7adSLuis R. Rodriguez 	/*
1760fb1fc7adSLuis R. Rodriguez 	 * We should not have let these through at this point, they
17613f2355cbSLuis R. Rodriguez 	 * should have been picked up earlier by the first alpha2 check
1762fb1fc7adSLuis R. Rodriguez 	 * on the device
1763fb1fc7adSLuis R. Rodriguez 	 */
17643f2355cbSLuis R. Rodriguez 	if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
17653f2355cbSLuis R. Rodriguez 		return true;
17663f2355cbSLuis R. Rodriguez 	return false;
17673f2355cbSLuis R. Rodriguez }
17683f2355cbSLuis R. Rodriguez 
17693f2355cbSLuis R. Rodriguez void regulatory_hint_11d(struct wiphy *wiphy,
17703f2355cbSLuis R. Rodriguez 			u8 *country_ie,
17713f2355cbSLuis R. Rodriguez 			u8 country_ie_len)
17723f2355cbSLuis R. Rodriguez {
17733f2355cbSLuis R. Rodriguez 	struct ieee80211_regdomain *rd = NULL;
17743f2355cbSLuis R. Rodriguez 	char alpha2[2];
17753f2355cbSLuis R. Rodriguez 	u32 checksum = 0;
17763f2355cbSLuis R. Rodriguez 	enum environment_cap env = ENVIRON_ANY;
1777fe33eb39SLuis R. Rodriguez 	struct regulatory_request *request;
17783f2355cbSLuis R. Rodriguez 
1779a1794390SLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
17803f2355cbSLuis R. Rodriguez 
1781d335fe63SLuis R. Rodriguez 	if (unlikely(!last_request)) {
1782d335fe63SLuis R. Rodriguez 		mutex_unlock(&cfg80211_mutex);
1783d335fe63SLuis R. Rodriguez 		return;
1784d335fe63SLuis R. Rodriguez 	}
1785d335fe63SLuis R. Rodriguez 
17863f2355cbSLuis R. Rodriguez 	/* IE len must be evenly divisible by 2 */
17873f2355cbSLuis R. Rodriguez 	if (country_ie_len & 0x01)
17883f2355cbSLuis R. Rodriguez 		goto out;
17893f2355cbSLuis R. Rodriguez 
17903f2355cbSLuis R. Rodriguez 	if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
17913f2355cbSLuis R. Rodriguez 		goto out;
17923f2355cbSLuis R. Rodriguez 
1793fb1fc7adSLuis R. Rodriguez 	/*
1794fb1fc7adSLuis R. Rodriguez 	 * Pending country IE processing, this can happen after we
17953f2355cbSLuis R. Rodriguez 	 * call CRDA and wait for a response if a beacon was received before
1796fb1fc7adSLuis R. Rodriguez 	 * we were able to process the last regulatory_hint_11d() call
1797fb1fc7adSLuis R. Rodriguez 	 */
17983f2355cbSLuis R. Rodriguez 	if (country_ie_regdomain)
17993f2355cbSLuis R. Rodriguez 		goto out;
18003f2355cbSLuis R. Rodriguez 
18013f2355cbSLuis R. Rodriguez 	alpha2[0] = country_ie[0];
18023f2355cbSLuis R. Rodriguez 	alpha2[1] = country_ie[1];
18033f2355cbSLuis R. Rodriguez 
18043f2355cbSLuis R. Rodriguez 	if (country_ie[2] == 'I')
18053f2355cbSLuis R. Rodriguez 		env = ENVIRON_INDOOR;
18063f2355cbSLuis R. Rodriguez 	else if (country_ie[2] == 'O')
18073f2355cbSLuis R. Rodriguez 		env = ENVIRON_OUTDOOR;
18083f2355cbSLuis R. Rodriguez 
1809fb1fc7adSLuis R. Rodriguez 	/*
1810fb1fc7adSLuis R. Rodriguez 	 * We will run this for *every* beacon processed for the BSSID, so
18113f2355cbSLuis R. Rodriguez 	 * we optimize an early check to exit out early if we don't have to
1812fb1fc7adSLuis R. Rodriguez 	 * do anything
1813fb1fc7adSLuis R. Rodriguez 	 */
1814cc0b6fe8SLuis R. Rodriguez 	if (likely(last_request->initiator ==
1815cc0b6fe8SLuis R. Rodriguez 	    NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1816cc0b6fe8SLuis R. Rodriguez 	    wiphy_idx_valid(last_request->wiphy_idx))) {
18173f2355cbSLuis R. Rodriguez 		struct cfg80211_registered_device *drv_last_ie;
18183f2355cbSLuis R. Rodriguez 
1819806a9e39SLuis R. Rodriguez 		drv_last_ie =
1820806a9e39SLuis R. Rodriguez 			cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
18213f2355cbSLuis R. Rodriguez 
1822fb1fc7adSLuis R. Rodriguez 		/*
1823fb1fc7adSLuis R. Rodriguez 		 * Lets keep this simple -- we trust the first AP
1824fb1fc7adSLuis R. Rodriguez 		 * after we intersect with CRDA
1825fb1fc7adSLuis R. Rodriguez 		 */
1826806a9e39SLuis R. Rodriguez 		if (likely(&drv_last_ie->wiphy == wiphy)) {
1827fb1fc7adSLuis R. Rodriguez 			/*
1828fb1fc7adSLuis R. Rodriguez 			 * Ignore IEs coming in on this wiphy with
1829fb1fc7adSLuis R. Rodriguez 			 * the same alpha2 and environment cap
1830fb1fc7adSLuis R. Rodriguez 			 */
18313f2355cbSLuis R. Rodriguez 			if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
18323f2355cbSLuis R. Rodriguez 				  alpha2) &&
18333f2355cbSLuis R. Rodriguez 				  env == drv_last_ie->env)) {
18343f2355cbSLuis R. Rodriguez 				goto out;
18353f2355cbSLuis R. Rodriguez 			}
1836fb1fc7adSLuis R. Rodriguez 			/*
1837fb1fc7adSLuis R. Rodriguez 			 * the wiphy moved on to another BSSID or the AP
18383f2355cbSLuis R. Rodriguez 			 * was reconfigured. XXX: We need to deal with the
18393f2355cbSLuis R. Rodriguez 			 * case where the user suspends and goes to goes
18403f2355cbSLuis R. Rodriguez 			 * to another country, and then gets IEs from an
1841fb1fc7adSLuis R. Rodriguez 			 * AP with different settings
1842fb1fc7adSLuis R. Rodriguez 			 */
18433f2355cbSLuis R. Rodriguez 			goto out;
18443f2355cbSLuis R. Rodriguez 		} else {
1845fb1fc7adSLuis R. Rodriguez 			/*
1846fb1fc7adSLuis R. Rodriguez 			 * Ignore IEs coming in on two separate wiphys with
1847fb1fc7adSLuis R. Rodriguez 			 * the same alpha2 and environment cap
1848fb1fc7adSLuis R. Rodriguez 			 */
18493f2355cbSLuis R. Rodriguez 			if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
18503f2355cbSLuis R. Rodriguez 				  alpha2) &&
18513f2355cbSLuis R. Rodriguez 				  env == drv_last_ie->env)) {
18523f2355cbSLuis R. Rodriguez 				goto out;
18533f2355cbSLuis R. Rodriguez 			}
18543f2355cbSLuis R. Rodriguez 			/* We could potentially intersect though */
18553f2355cbSLuis R. Rodriguez 			goto out;
18563f2355cbSLuis R. Rodriguez 		}
18573f2355cbSLuis R. Rodriguez 	}
18583f2355cbSLuis R. Rodriguez 
18593f2355cbSLuis R. Rodriguez 	rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
18603f2355cbSLuis R. Rodriguez 	if (!rd)
18613f2355cbSLuis R. Rodriguez 		goto out;
18623f2355cbSLuis R. Rodriguez 
1863915278e0SLuis R. Rodriguez 	/*
1864915278e0SLuis R. Rodriguez 	 * This will not happen right now but we leave it here for the
18653f2355cbSLuis R. Rodriguez 	 * the future when we want to add suspend/resume support and having
18663f2355cbSLuis R. Rodriguez 	 * the user move to another country after doing so, or having the user
1867915278e0SLuis R. Rodriguez 	 * move to another AP. Right now we just trust the first AP.
1868915278e0SLuis R. Rodriguez 	 *
1869915278e0SLuis R. Rodriguez 	 * If we hit this before we add this support we want to be informed of
1870915278e0SLuis R. Rodriguez 	 * it as it would indicate a mistake in the current design
1871915278e0SLuis R. Rodriguez 	 */
1872915278e0SLuis R. Rodriguez 	if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
18730441d6ffSLuis R. Rodriguez 		goto free_rd_out;
18743f2355cbSLuis R. Rodriguez 
1875fe33eb39SLuis R. Rodriguez 	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1876fe33eb39SLuis R. Rodriguez 	if (!request)
1877fe33eb39SLuis R. Rodriguez 		goto free_rd_out;
1878fe33eb39SLuis R. Rodriguez 
1879fb1fc7adSLuis R. Rodriguez 	/*
1880fb1fc7adSLuis R. Rodriguez 	 * We keep this around for when CRDA comes back with a response so
1881fb1fc7adSLuis R. Rodriguez 	 * we can intersect with that
1882fb1fc7adSLuis R. Rodriguez 	 */
18833f2355cbSLuis R. Rodriguez 	country_ie_regdomain = rd;
18843f2355cbSLuis R. Rodriguez 
1885fe33eb39SLuis R. Rodriguez 	request->wiphy_idx = get_wiphy_idx(wiphy);
1886fe33eb39SLuis R. Rodriguez 	request->alpha2[0] = rd->alpha2[0];
1887fe33eb39SLuis R. Rodriguez 	request->alpha2[1] = rd->alpha2[1];
18887db90f4aSLuis R. Rodriguez 	request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
1889fe33eb39SLuis R. Rodriguez 	request->country_ie_checksum = checksum;
1890fe33eb39SLuis R. Rodriguez 	request->country_ie_env = env;
18913f2355cbSLuis R. Rodriguez 
1892fe33eb39SLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
1893fe33eb39SLuis R. Rodriguez 
1894fe33eb39SLuis R. Rodriguez 	queue_regulatory_request(request);
1895fe33eb39SLuis R. Rodriguez 
1896fe33eb39SLuis R. Rodriguez 	return;
18970441d6ffSLuis R. Rodriguez 
18980441d6ffSLuis R. Rodriguez free_rd_out:
18990441d6ffSLuis R. Rodriguez 	kfree(rd);
19003f2355cbSLuis R. Rodriguez out:
1901a1794390SLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
19023f2355cbSLuis R. Rodriguez }
19033f2355cbSLuis R. Rodriguez EXPORT_SYMBOL(regulatory_hint_11d);
1904b2e1b302SLuis R. Rodriguez 
1905e38f8a7aSLuis R. Rodriguez static bool freq_is_chan_12_13_14(u16 freq)
1906e38f8a7aSLuis R. Rodriguez {
1907e38f8a7aSLuis R. Rodriguez 	if (freq == ieee80211_channel_to_frequency(12) ||
1908e38f8a7aSLuis R. Rodriguez 	    freq == ieee80211_channel_to_frequency(13) ||
1909e38f8a7aSLuis R. Rodriguez 	    freq == ieee80211_channel_to_frequency(14))
1910e38f8a7aSLuis R. Rodriguez 		return true;
1911e38f8a7aSLuis R. Rodriguez 	return false;
1912e38f8a7aSLuis R. Rodriguez }
1913e38f8a7aSLuis R. Rodriguez 
1914e38f8a7aSLuis R. Rodriguez int regulatory_hint_found_beacon(struct wiphy *wiphy,
1915e38f8a7aSLuis R. Rodriguez 				 struct ieee80211_channel *beacon_chan,
1916e38f8a7aSLuis R. Rodriguez 				 gfp_t gfp)
1917e38f8a7aSLuis R. Rodriguez {
1918e38f8a7aSLuis R. Rodriguez 	struct reg_beacon *reg_beacon;
1919e38f8a7aSLuis R. Rodriguez 
1920e38f8a7aSLuis R. Rodriguez 	if (likely((beacon_chan->beacon_found ||
1921e38f8a7aSLuis R. Rodriguez 	    (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1922e38f8a7aSLuis R. Rodriguez 	    (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1923e38f8a7aSLuis R. Rodriguez 	     !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1924e38f8a7aSLuis R. Rodriguez 		return 0;
1925e38f8a7aSLuis R. Rodriguez 
1926e38f8a7aSLuis R. Rodriguez 	reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1927e38f8a7aSLuis R. Rodriguez 	if (!reg_beacon)
1928e38f8a7aSLuis R. Rodriguez 		return -ENOMEM;
1929e38f8a7aSLuis R. Rodriguez 
1930e38f8a7aSLuis R. Rodriguez #ifdef CONFIG_CFG80211_REG_DEBUG
1931e38f8a7aSLuis R. Rodriguez 	printk(KERN_DEBUG "cfg80211: Found new beacon on "
1932e38f8a7aSLuis R. Rodriguez 		"frequency: %d MHz (Ch %d) on %s\n",
1933e38f8a7aSLuis R. Rodriguez 		beacon_chan->center_freq,
1934e38f8a7aSLuis R. Rodriguez 		ieee80211_frequency_to_channel(beacon_chan->center_freq),
1935e38f8a7aSLuis R. Rodriguez 		wiphy_name(wiphy));
1936e38f8a7aSLuis R. Rodriguez #endif
1937e38f8a7aSLuis R. Rodriguez 	memcpy(&reg_beacon->chan, beacon_chan,
1938e38f8a7aSLuis R. Rodriguez 		sizeof(struct ieee80211_channel));
1939e38f8a7aSLuis R. Rodriguez 
1940e38f8a7aSLuis R. Rodriguez 
1941e38f8a7aSLuis R. Rodriguez 	/*
1942e38f8a7aSLuis R. Rodriguez 	 * Since we can be called from BH or and non-BH context
1943e38f8a7aSLuis R. Rodriguez 	 * we must use spin_lock_bh()
1944e38f8a7aSLuis R. Rodriguez 	 */
1945e38f8a7aSLuis R. Rodriguez 	spin_lock_bh(&reg_pending_beacons_lock);
1946e38f8a7aSLuis R. Rodriguez 	list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1947e38f8a7aSLuis R. Rodriguez 	spin_unlock_bh(&reg_pending_beacons_lock);
1948e38f8a7aSLuis R. Rodriguez 
1949e38f8a7aSLuis R. Rodriguez 	schedule_work(&reg_work);
1950e38f8a7aSLuis R. Rodriguez 
1951e38f8a7aSLuis R. Rodriguez 	return 0;
1952e38f8a7aSLuis R. Rodriguez }
1953e38f8a7aSLuis R. Rodriguez 
1954a3d2eaf0SJohannes Berg static void print_rd_rules(const struct ieee80211_regdomain *rd)
1955b2e1b302SLuis R. Rodriguez {
1956b2e1b302SLuis R. Rodriguez 	unsigned int i;
1957a3d2eaf0SJohannes Berg 	const struct ieee80211_reg_rule *reg_rule = NULL;
1958a3d2eaf0SJohannes Berg 	const struct ieee80211_freq_range *freq_range = NULL;
1959a3d2eaf0SJohannes Berg 	const struct ieee80211_power_rule *power_rule = NULL;
1960b2e1b302SLuis R. Rodriguez 
1961b2e1b302SLuis R. Rodriguez 	printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1962b2e1b302SLuis R. Rodriguez 		"(max_antenna_gain, max_eirp)\n");
1963b2e1b302SLuis R. Rodriguez 
1964b2e1b302SLuis R. Rodriguez 	for (i = 0; i < rd->n_reg_rules; i++) {
1965b2e1b302SLuis R. Rodriguez 		reg_rule = &rd->reg_rules[i];
1966b2e1b302SLuis R. Rodriguez 		freq_range = &reg_rule->freq_range;
1967b2e1b302SLuis R. Rodriguez 		power_rule = &reg_rule->power_rule;
1968b2e1b302SLuis R. Rodriguez 
1969fb1fc7adSLuis R. Rodriguez 		/*
1970fb1fc7adSLuis R. Rodriguez 		 * There may not be documentation for max antenna gain
1971fb1fc7adSLuis R. Rodriguez 		 * in certain regions
1972fb1fc7adSLuis R. Rodriguez 		 */
1973b2e1b302SLuis R. Rodriguez 		if (power_rule->max_antenna_gain)
1974b2e1b302SLuis R. Rodriguez 			printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1975b2e1b302SLuis R. Rodriguez 				"(%d mBi, %d mBm)\n",
1976b2e1b302SLuis R. Rodriguez 				freq_range->start_freq_khz,
1977b2e1b302SLuis R. Rodriguez 				freq_range->end_freq_khz,
1978b2e1b302SLuis R. Rodriguez 				freq_range->max_bandwidth_khz,
1979b2e1b302SLuis R. Rodriguez 				power_rule->max_antenna_gain,
1980b2e1b302SLuis R. Rodriguez 				power_rule->max_eirp);
1981b2e1b302SLuis R. Rodriguez 		else
1982b2e1b302SLuis R. Rodriguez 			printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1983b2e1b302SLuis R. Rodriguez 				"(N/A, %d mBm)\n",
1984b2e1b302SLuis R. Rodriguez 				freq_range->start_freq_khz,
1985b2e1b302SLuis R. Rodriguez 				freq_range->end_freq_khz,
1986b2e1b302SLuis R. Rodriguez 				freq_range->max_bandwidth_khz,
1987b2e1b302SLuis R. Rodriguez 				power_rule->max_eirp);
1988b2e1b302SLuis R. Rodriguez 	}
1989b2e1b302SLuis R. Rodriguez }
1990b2e1b302SLuis R. Rodriguez 
1991a3d2eaf0SJohannes Berg static void print_regdomain(const struct ieee80211_regdomain *rd)
1992b2e1b302SLuis R. Rodriguez {
1993b2e1b302SLuis R. Rodriguez 
19943f2355cbSLuis R. Rodriguez 	if (is_intersected_alpha2(rd->alpha2)) {
19953f2355cbSLuis R. Rodriguez 
19967db90f4aSLuis R. Rodriguez 		if (last_request->initiator ==
19977db90f4aSLuis R. Rodriguez 		    NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1998806a9e39SLuis R. Rodriguez 			struct cfg80211_registered_device *drv;
1999806a9e39SLuis R. Rodriguez 			drv = cfg80211_drv_by_wiphy_idx(
2000806a9e39SLuis R. Rodriguez 				last_request->wiphy_idx);
2001806a9e39SLuis R. Rodriguez 			if (drv) {
20023f2355cbSLuis R. Rodriguez 				printk(KERN_INFO "cfg80211: Current regulatory "
20033f2355cbSLuis R. Rodriguez 					"domain updated by AP to: %c%c\n",
20043f2355cbSLuis R. Rodriguez 					drv->country_ie_alpha2[0],
20053f2355cbSLuis R. Rodriguez 					drv->country_ie_alpha2[1]);
20063f2355cbSLuis R. Rodriguez 			} else
20073f2355cbSLuis R. Rodriguez 				printk(KERN_INFO "cfg80211: Current regulatory "
20083f2355cbSLuis R. Rodriguez 					"domain intersected: \n");
20093f2355cbSLuis R. Rodriguez 		} else
20103f2355cbSLuis R. Rodriguez 				printk(KERN_INFO "cfg80211: Current regulatory "
2011039498c6SLuis R. Rodriguez 					"domain intersected: \n");
20123f2355cbSLuis R. Rodriguez 	} else if (is_world_regdom(rd->alpha2))
2013b2e1b302SLuis R. Rodriguez 		printk(KERN_INFO "cfg80211: World regulatory "
2014b2e1b302SLuis R. Rodriguez 			"domain updated:\n");
2015b2e1b302SLuis R. Rodriguez 	else {
2016b2e1b302SLuis R. Rodriguez 		if (is_unknown_alpha2(rd->alpha2))
2017b2e1b302SLuis R. Rodriguez 			printk(KERN_INFO "cfg80211: Regulatory domain "
2018b2e1b302SLuis R. Rodriguez 				"changed to driver built-in settings "
2019b2e1b302SLuis R. Rodriguez 				"(unknown country)\n");
2020b2e1b302SLuis R. Rodriguez 		else
2021b2e1b302SLuis R. Rodriguez 			printk(KERN_INFO "cfg80211: Regulatory domain "
2022b2e1b302SLuis R. Rodriguez 				"changed to country: %c%c\n",
2023b2e1b302SLuis R. Rodriguez 				rd->alpha2[0], rd->alpha2[1]);
2024b2e1b302SLuis R. Rodriguez 	}
2025b2e1b302SLuis R. Rodriguez 	print_rd_rules(rd);
2026b2e1b302SLuis R. Rodriguez }
2027b2e1b302SLuis R. Rodriguez 
20282df78167SJohannes Berg static void print_regdomain_info(const struct ieee80211_regdomain *rd)
2029b2e1b302SLuis R. Rodriguez {
2030b2e1b302SLuis R. Rodriguez 	printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2031b2e1b302SLuis R. Rodriguez 		rd->alpha2[0], rd->alpha2[1]);
2032b2e1b302SLuis R. Rodriguez 	print_rd_rules(rd);
2033b2e1b302SLuis R. Rodriguez }
2034b2e1b302SLuis R. Rodriguez 
20353f2355cbSLuis R. Rodriguez #ifdef CONFIG_CFG80211_REG_DEBUG
20363f2355cbSLuis R. Rodriguez static void reg_country_ie_process_debug(
20373f2355cbSLuis R. Rodriguez 	const struct ieee80211_regdomain *rd,
20383f2355cbSLuis R. Rodriguez 	const struct ieee80211_regdomain *country_ie_regdomain,
20393f2355cbSLuis R. Rodriguez 	const struct ieee80211_regdomain *intersected_rd)
20403f2355cbSLuis R. Rodriguez {
20413f2355cbSLuis R. Rodriguez 	printk(KERN_DEBUG "cfg80211: Received country IE:\n");
20423f2355cbSLuis R. Rodriguez 	print_regdomain_info(country_ie_regdomain);
20433f2355cbSLuis R. Rodriguez 	printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
20443f2355cbSLuis R. Rodriguez 	print_regdomain_info(rd);
20453f2355cbSLuis R. Rodriguez 	if (intersected_rd) {
20463f2355cbSLuis R. Rodriguez 		printk(KERN_DEBUG "cfg80211: We intersect both of these "
20473f2355cbSLuis R. Rodriguez 			"and get:\n");
2048667ecd01SLuis R. Rodriguez 		print_regdomain_info(intersected_rd);
20493f2355cbSLuis R. Rodriguez 		return;
20503f2355cbSLuis R. Rodriguez 	}
20513f2355cbSLuis R. Rodriguez 	printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
20523f2355cbSLuis R. Rodriguez }
20533f2355cbSLuis R. Rodriguez #else
20543f2355cbSLuis R. Rodriguez static inline void reg_country_ie_process_debug(
20553f2355cbSLuis R. Rodriguez 	const struct ieee80211_regdomain *rd,
20563f2355cbSLuis R. Rodriguez 	const struct ieee80211_regdomain *country_ie_regdomain,
20573f2355cbSLuis R. Rodriguez 	const struct ieee80211_regdomain *intersected_rd)
20583f2355cbSLuis R. Rodriguez {
20593f2355cbSLuis R. Rodriguez }
20603f2355cbSLuis R. Rodriguez #endif
20613f2355cbSLuis R. Rodriguez 
2062d2372b31SJohannes Berg /* Takes ownership of rd only if it doesn't fail */
2063a3d2eaf0SJohannes Berg static int __set_regdom(const struct ieee80211_regdomain *rd)
2064b2e1b302SLuis R. Rodriguez {
20659c96477dSLuis R. Rodriguez 	const struct ieee80211_regdomain *intersected_rd = NULL;
20663f2355cbSLuis R. Rodriguez 	struct cfg80211_registered_device *drv = NULL;
2067806a9e39SLuis R. Rodriguez 	struct wiphy *request_wiphy;
2068b2e1b302SLuis R. Rodriguez 	/* Some basic sanity checks first */
2069b2e1b302SLuis R. Rodriguez 
2070b2e1b302SLuis R. Rodriguez 	if (is_world_regdom(rd->alpha2)) {
2071f6037d09SJohannes Berg 		if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2072b2e1b302SLuis R. Rodriguez 			return -EINVAL;
2073b2e1b302SLuis R. Rodriguez 		update_world_regdomain(rd);
2074b2e1b302SLuis R. Rodriguez 		return 0;
2075b2e1b302SLuis R. Rodriguez 	}
2076b2e1b302SLuis R. Rodriguez 
2077b2e1b302SLuis R. Rodriguez 	if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2078b2e1b302SLuis R. Rodriguez 			!is_unknown_alpha2(rd->alpha2))
2079b2e1b302SLuis R. Rodriguez 		return -EINVAL;
2080b2e1b302SLuis R. Rodriguez 
2081f6037d09SJohannes Berg 	if (!last_request)
2082b2e1b302SLuis R. Rodriguez 		return -EINVAL;
2083b2e1b302SLuis R. Rodriguez 
2084fb1fc7adSLuis R. Rodriguez 	/*
2085fb1fc7adSLuis R. Rodriguez 	 * Lets only bother proceeding on the same alpha2 if the current
20863f2355cbSLuis R. Rodriguez 	 * rd is non static (it means CRDA was present and was used last)
2087fb1fc7adSLuis R. Rodriguez 	 * and the pending request came in from a country IE
2088fb1fc7adSLuis R. Rodriguez 	 */
20897db90f4aSLuis R. Rodriguez 	if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2090fb1fc7adSLuis R. Rodriguez 		/*
2091fb1fc7adSLuis R. Rodriguez 		 * If someone else asked us to change the rd lets only bother
2092fb1fc7adSLuis R. Rodriguez 		 * checking if the alpha2 changes if CRDA was already called
2093fb1fc7adSLuis R. Rodriguez 		 */
2094b2e1b302SLuis R. Rodriguez 		if (!is_old_static_regdom(cfg80211_regdomain) &&
209569b1572bSLuis R. Rodriguez 		    !regdom_changes(rd->alpha2))
2096b2e1b302SLuis R. Rodriguez 			return -EINVAL;
20973f2355cbSLuis R. Rodriguez 	}
20983f2355cbSLuis R. Rodriguez 
2099fb1fc7adSLuis R. Rodriguez 	/*
2100fb1fc7adSLuis R. Rodriguez 	 * Now lets set the regulatory domain, update all driver channels
2101b2e1b302SLuis R. Rodriguez 	 * and finally inform them of what we have done, in case they want
2102b2e1b302SLuis R. Rodriguez 	 * to review or adjust their own settings based on their own
2103fb1fc7adSLuis R. Rodriguez 	 * internal EEPROM data
2104fb1fc7adSLuis R. Rodriguez 	 */
2105b2e1b302SLuis R. Rodriguez 
2106f6037d09SJohannes Berg 	if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2107b2e1b302SLuis R. Rodriguez 		return -EINVAL;
2108b2e1b302SLuis R. Rodriguez 
2109b2e1b302SLuis R. Rodriguez 	if (!is_valid_rd(rd)) {
2110b2e1b302SLuis R. Rodriguez 		printk(KERN_ERR "cfg80211: Invalid "
2111b2e1b302SLuis R. Rodriguez 			"regulatory domain detected:\n");
2112b2e1b302SLuis R. Rodriguez 		print_regdomain_info(rd);
2113b2e1b302SLuis R. Rodriguez 		return -EINVAL;
2114b2e1b302SLuis R. Rodriguez 	}
2115b2e1b302SLuis R. Rodriguez 
2116806a9e39SLuis R. Rodriguez 	request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2117806a9e39SLuis R. Rodriguez 
2118b8295acdSLuis R. Rodriguez 	if (!last_request->intersect) {
21193e0c3ff3SLuis R. Rodriguez 		int r;
21203e0c3ff3SLuis R. Rodriguez 
21217db90f4aSLuis R. Rodriguez 		if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
21223e0c3ff3SLuis R. Rodriguez 			reset_regdomains();
21233e0c3ff3SLuis R. Rodriguez 			cfg80211_regdomain = rd;
21243e0c3ff3SLuis R. Rodriguez 			return 0;
21253e0c3ff3SLuis R. Rodriguez 		}
21263e0c3ff3SLuis R. Rodriguez 
2127fb1fc7adSLuis R. Rodriguez 		/*
2128fb1fc7adSLuis R. Rodriguez 		 * For a driver hint, lets copy the regulatory domain the
2129fb1fc7adSLuis R. Rodriguez 		 * driver wanted to the wiphy to deal with conflicts
2130fb1fc7adSLuis R. Rodriguez 		 */
21313e0c3ff3SLuis R. Rodriguez 
2132806a9e39SLuis R. Rodriguez 		BUG_ON(request_wiphy->regd);
21333e0c3ff3SLuis R. Rodriguez 
2134806a9e39SLuis R. Rodriguez 		r = reg_copy_regd(&request_wiphy->regd, rd);
21353e0c3ff3SLuis R. Rodriguez 		if (r)
21363e0c3ff3SLuis R. Rodriguez 			return r;
21373e0c3ff3SLuis R. Rodriguez 
2138b8295acdSLuis R. Rodriguez 		reset_regdomains();
2139b8295acdSLuis R. Rodriguez 		cfg80211_regdomain = rd;
2140b8295acdSLuis R. Rodriguez 		return 0;
2141b8295acdSLuis R. Rodriguez 	}
2142b8295acdSLuis R. Rodriguez 
2143b8295acdSLuis R. Rodriguez 	/* Intersection requires a bit more work */
2144b8295acdSLuis R. Rodriguez 
21457db90f4aSLuis R. Rodriguez 	if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2146b8295acdSLuis R. Rodriguez 
21479c96477dSLuis R. Rodriguez 		intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
21489c96477dSLuis R. Rodriguez 		if (!intersected_rd)
21499c96477dSLuis R. Rodriguez 			return -EINVAL;
2150b8295acdSLuis R. Rodriguez 
2151fb1fc7adSLuis R. Rodriguez 		/*
2152fb1fc7adSLuis R. Rodriguez 		 * We can trash what CRDA provided now.
21533e0c3ff3SLuis R. Rodriguez 		 * However if a driver requested this specific regulatory
2154fb1fc7adSLuis R. Rodriguez 		 * domain we keep it for its private use
2155fb1fc7adSLuis R. Rodriguez 		 */
21567db90f4aSLuis R. Rodriguez 		if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2157806a9e39SLuis R. Rodriguez 			request_wiphy->regd = rd;
21583e0c3ff3SLuis R. Rodriguez 		else
21599c96477dSLuis R. Rodriguez 			kfree(rd);
21603e0c3ff3SLuis R. Rodriguez 
2161b8295acdSLuis R. Rodriguez 		rd = NULL;
2162b8295acdSLuis R. Rodriguez 
2163b8295acdSLuis R. Rodriguez 		reset_regdomains();
2164b8295acdSLuis R. Rodriguez 		cfg80211_regdomain = intersected_rd;
2165b8295acdSLuis R. Rodriguez 
2166b8295acdSLuis R. Rodriguez 		return 0;
21679c96477dSLuis R. Rodriguez 	}
21689c96477dSLuis R. Rodriguez 
21693f2355cbSLuis R. Rodriguez 	/*
21703f2355cbSLuis R. Rodriguez 	 * Country IE requests are handled a bit differently, we intersect
21713f2355cbSLuis R. Rodriguez 	 * the country IE rd with what CRDA believes that country should have
21723f2355cbSLuis R. Rodriguez 	 */
21733f2355cbSLuis R. Rodriguez 
2174729e9c76SLuis R. Rodriguez 	/*
2175729e9c76SLuis R. Rodriguez 	 * Userspace could have sent two replies with only
2176729e9c76SLuis R. Rodriguez 	 * one kernel request. By the second reply we would have
2177729e9c76SLuis R. Rodriguez 	 * already processed and consumed the country_ie_regdomain.
2178729e9c76SLuis R. Rodriguez 	 */
2179729e9c76SLuis R. Rodriguez 	if (!country_ie_regdomain)
2180729e9c76SLuis R. Rodriguez 		return -EALREADY;
218186f04680SLuis R. Rodriguez 	BUG_ON(rd == country_ie_regdomain);
21823f2355cbSLuis R. Rodriguez 
2183fb1fc7adSLuis R. Rodriguez 	/*
2184fb1fc7adSLuis R. Rodriguez 	 * Intersect what CRDA returned and our what we
2185fb1fc7adSLuis R. Rodriguez 	 * had built from the Country IE received
2186fb1fc7adSLuis R. Rodriguez 	 */
21873f2355cbSLuis R. Rodriguez 
21883f2355cbSLuis R. Rodriguez 	intersected_rd = regdom_intersect(rd, country_ie_regdomain);
21893f2355cbSLuis R. Rodriguez 
219086f04680SLuis R. Rodriguez 	reg_country_ie_process_debug(rd,
219186f04680SLuis R. Rodriguez 				     country_ie_regdomain,
21923f2355cbSLuis R. Rodriguez 				     intersected_rd);
21933f2355cbSLuis R. Rodriguez 
21943f2355cbSLuis R. Rodriguez 	kfree(country_ie_regdomain);
21953f2355cbSLuis R. Rodriguez 	country_ie_regdomain = NULL;
21963f2355cbSLuis R. Rodriguez 
21973f2355cbSLuis R. Rodriguez 	if (!intersected_rd)
21983f2355cbSLuis R. Rodriguez 		return -EINVAL;
21993f2355cbSLuis R. Rodriguez 
2200806a9e39SLuis R. Rodriguez 	drv = wiphy_to_dev(request_wiphy);
22013f2355cbSLuis R. Rodriguez 
22023f2355cbSLuis R. Rodriguez 	drv->country_ie_alpha2[0] = rd->alpha2[0];
22033f2355cbSLuis R. Rodriguez 	drv->country_ie_alpha2[1] = rd->alpha2[1];
22043f2355cbSLuis R. Rodriguez 	drv->env = last_request->country_ie_env;
22053f2355cbSLuis R. Rodriguez 
22063f2355cbSLuis R. Rodriguez 	BUG_ON(intersected_rd == rd);
22073f2355cbSLuis R. Rodriguez 
22083f2355cbSLuis R. Rodriguez 	kfree(rd);
22093f2355cbSLuis R. Rodriguez 	rd = NULL;
22103f2355cbSLuis R. Rodriguez 
2211b8295acdSLuis R. Rodriguez 	reset_regdomains();
22123f2355cbSLuis R. Rodriguez 	cfg80211_regdomain = intersected_rd;
2213b2e1b302SLuis R. Rodriguez 
2214b2e1b302SLuis R. Rodriguez 	return 0;
2215b2e1b302SLuis R. Rodriguez }
2216b2e1b302SLuis R. Rodriguez 
2217b2e1b302SLuis R. Rodriguez 
2218fb1fc7adSLuis R. Rodriguez /*
2219fb1fc7adSLuis R. Rodriguez  * Use this call to set the current regulatory domain. Conflicts with
2220b2e1b302SLuis R. Rodriguez  * multiple drivers can be ironed out later. Caller must've already
2221fb1fc7adSLuis R. Rodriguez  * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2222fb1fc7adSLuis R. Rodriguez  */
2223a3d2eaf0SJohannes Berg int set_regdom(const struct ieee80211_regdomain *rd)
2224b2e1b302SLuis R. Rodriguez {
2225b2e1b302SLuis R. Rodriguez 	int r;
2226b2e1b302SLuis R. Rodriguez 
2227761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
2228761cf7ecSLuis R. Rodriguez 
2229b2e1b302SLuis R. Rodriguez 	/* Note that this doesn't update the wiphys, this is done below */
2230b2e1b302SLuis R. Rodriguez 	r = __set_regdom(rd);
2231d2372b31SJohannes Berg 	if (r) {
2232d2372b31SJohannes Berg 		kfree(rd);
2233b2e1b302SLuis R. Rodriguez 		return r;
2234d2372b31SJohannes Berg 	}
2235b2e1b302SLuis R. Rodriguez 
2236b2e1b302SLuis R. Rodriguez 	/* This would make this whole thing pointless */
2237a01ddafdSLuis R. Rodriguez 	if (!last_request->intersect)
2238b2e1b302SLuis R. Rodriguez 		BUG_ON(rd != cfg80211_regdomain);
2239b2e1b302SLuis R. Rodriguez 
2240b2e1b302SLuis R. Rodriguez 	/* update all wiphys now with the new established regulatory domain */
2241f6037d09SJohannes Berg 	update_all_wiphy_regulatory(last_request->initiator);
2242b2e1b302SLuis R. Rodriguez 
2243a01ddafdSLuis R. Rodriguez 	print_regdomain(cfg80211_regdomain);
2244b2e1b302SLuis R. Rodriguez 
224573d54c9eSLuis R. Rodriguez 	nl80211_send_reg_change_event(last_request);
224673d54c9eSLuis R. Rodriguez 
2247b2e1b302SLuis R. Rodriguez 	return r;
2248b2e1b302SLuis R. Rodriguez }
2249b2e1b302SLuis R. Rodriguez 
2250a1794390SLuis R. Rodriguez /* Caller must hold cfg80211_mutex */
22513f2355cbSLuis R. Rodriguez void reg_device_remove(struct wiphy *wiphy)
22523f2355cbSLuis R. Rodriguez {
22530ad8acafSLuis R. Rodriguez 	struct wiphy *request_wiphy = NULL;
2254806a9e39SLuis R. Rodriguez 
2255761cf7ecSLuis R. Rodriguez 	assert_cfg80211_lock();
2256761cf7ecSLuis R. Rodriguez 
22570ef9ccddSChris Wright 	kfree(wiphy->regd);
22580ef9ccddSChris Wright 
22590ad8acafSLuis R. Rodriguez 	if (last_request)
2260806a9e39SLuis R. Rodriguez 		request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2261806a9e39SLuis R. Rodriguez 
22620ef9ccddSChris Wright 	if (!request_wiphy || request_wiphy != wiphy)
22633f2355cbSLuis R. Rodriguez 		return;
22640ef9ccddSChris Wright 
2265806a9e39SLuis R. Rodriguez 	last_request->wiphy_idx = WIPHY_IDX_STALE;
22663f2355cbSLuis R. Rodriguez 	last_request->country_ie_env = ENVIRON_ANY;
22673f2355cbSLuis R. Rodriguez }
22683f2355cbSLuis R. Rodriguez 
2269b2e1b302SLuis R. Rodriguez int regulatory_init(void)
2270b2e1b302SLuis R. Rodriguez {
2271bcf4f99bSLuis R. Rodriguez 	int err = 0;
2272734366deSJohannes Berg 
2273b2e1b302SLuis R. Rodriguez 	reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2274b2e1b302SLuis R. Rodriguez 	if (IS_ERR(reg_pdev))
2275b2e1b302SLuis R. Rodriguez 		return PTR_ERR(reg_pdev);
2276734366deSJohannes Berg 
2277fe33eb39SLuis R. Rodriguez 	spin_lock_init(&reg_requests_lock);
2278e38f8a7aSLuis R. Rodriguez 	spin_lock_init(&reg_pending_beacons_lock);
2279fe33eb39SLuis R. Rodriguez 
2280734366deSJohannes Berg #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2281a3d2eaf0SJohannes Berg 	cfg80211_regdomain = static_regdom(ieee80211_regdom);
2282734366deSJohannes Berg 
2283942b25cfSJohannes Berg 	printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
2284734366deSJohannes Berg 	print_regdomain_info(cfg80211_regdomain);
2285fb1fc7adSLuis R. Rodriguez 	/*
2286fb1fc7adSLuis R. Rodriguez 	 * The old code still requests for a new regdomain and if
2287734366deSJohannes Berg 	 * you have CRDA you get it updated, otherwise you get
22882e097dc6SLuis R. Rodriguez 	 * stuck with the static values. Since "EU" is not a valid
22892e097dc6SLuis R. Rodriguez 	 * ISO / IEC 3166 alpha2 code we can't expect userpace to
22902e097dc6SLuis R. Rodriguez 	 * give us a regulatory domain for it. We need last_request
22912e097dc6SLuis R. Rodriguez 	 * iniitalized though so lets just send a request which we
22922e097dc6SLuis R. Rodriguez 	 * know will be ignored... this crap will be removed once
22932e097dc6SLuis R. Rodriguez 	 * OLD_REG dies.
2294fb1fc7adSLuis R. Rodriguez 	 */
2295ba25c141SLuis R. Rodriguez 	err = regulatory_hint_core(ieee80211_regdom);
2296734366deSJohannes Berg #else
2297a3d2eaf0SJohannes Berg 	cfg80211_regdomain = cfg80211_world_regdom;
2298734366deSJohannes Berg 
22996ee7d330SLuis R. Rodriguez 	err = regulatory_hint_core(ieee80211_regdom);
2300734366deSJohannes Berg #endif
2301bcf4f99bSLuis R. Rodriguez 	if (err) {
2302bcf4f99bSLuis R. Rodriguez 		if (err == -ENOMEM)
2303bcf4f99bSLuis R. Rodriguez 			return err;
2304bcf4f99bSLuis R. Rodriguez 		/*
2305bcf4f99bSLuis R. Rodriguez 		 * N.B. kobject_uevent_env() can fail mainly for when we're out
2306bcf4f99bSLuis R. Rodriguez 		 * memory which is handled and propagated appropriately above
2307bcf4f99bSLuis R. Rodriguez 		 * but it can also fail during a netlink_broadcast() or during
2308bcf4f99bSLuis R. Rodriguez 		 * early boot for call_usermodehelper(). For now treat these
2309bcf4f99bSLuis R. Rodriguez 		 * errors as non-fatal.
2310bcf4f99bSLuis R. Rodriguez 		 */
2311bcf4f99bSLuis R. Rodriguez 		printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2312bcf4f99bSLuis R. Rodriguez 			"to call CRDA during init");
2313bcf4f99bSLuis R. Rodriguez #ifdef CONFIG_CFG80211_REG_DEBUG
2314bcf4f99bSLuis R. Rodriguez 		/* We want to find out exactly why when debugging */
2315bcf4f99bSLuis R. Rodriguez 		WARN_ON(err);
2316bcf4f99bSLuis R. Rodriguez #endif
2317bcf4f99bSLuis R. Rodriguez 	}
2318734366deSJohannes Berg 
2319b2e1b302SLuis R. Rodriguez 	return 0;
2320b2e1b302SLuis R. Rodriguez }
2321b2e1b302SLuis R. Rodriguez 
2322b2e1b302SLuis R. Rodriguez void regulatory_exit(void)
2323b2e1b302SLuis R. Rodriguez {
2324fe33eb39SLuis R. Rodriguez 	struct regulatory_request *reg_request, *tmp;
2325e38f8a7aSLuis R. Rodriguez 	struct reg_beacon *reg_beacon, *btmp;
2326fe33eb39SLuis R. Rodriguez 
2327fe33eb39SLuis R. Rodriguez 	cancel_work_sync(&reg_work);
2328fe33eb39SLuis R. Rodriguez 
2329a1794390SLuis R. Rodriguez 	mutex_lock(&cfg80211_mutex);
2330734366deSJohannes Berg 
2331b2e1b302SLuis R. Rodriguez 	reset_regdomains();
2332734366deSJohannes Berg 
23333f2355cbSLuis R. Rodriguez 	kfree(country_ie_regdomain);
23343f2355cbSLuis R. Rodriguez 	country_ie_regdomain = NULL;
23353f2355cbSLuis R. Rodriguez 
2336f6037d09SJohannes Berg 	kfree(last_request);
2337f6037d09SJohannes Berg 
2338b2e1b302SLuis R. Rodriguez 	platform_device_unregister(reg_pdev);
2339734366deSJohannes Berg 
2340e38f8a7aSLuis R. Rodriguez 	spin_lock_bh(&reg_pending_beacons_lock);
2341e38f8a7aSLuis R. Rodriguez 	if (!list_empty(&reg_pending_beacons)) {
2342e38f8a7aSLuis R. Rodriguez 		list_for_each_entry_safe(reg_beacon, btmp,
2343e38f8a7aSLuis R. Rodriguez 					 &reg_pending_beacons, list) {
2344e38f8a7aSLuis R. Rodriguez 			list_del(&reg_beacon->list);
2345e38f8a7aSLuis R. Rodriguez 			kfree(reg_beacon);
2346e38f8a7aSLuis R. Rodriguez 		}
2347e38f8a7aSLuis R. Rodriguez 	}
2348e38f8a7aSLuis R. Rodriguez 	spin_unlock_bh(&reg_pending_beacons_lock);
2349e38f8a7aSLuis R. Rodriguez 
2350e38f8a7aSLuis R. Rodriguez 	if (!list_empty(&reg_beacon_list)) {
2351e38f8a7aSLuis R. Rodriguez 		list_for_each_entry_safe(reg_beacon, btmp,
2352e38f8a7aSLuis R. Rodriguez 					 &reg_beacon_list, list) {
2353e38f8a7aSLuis R. Rodriguez 			list_del(&reg_beacon->list);
2354e38f8a7aSLuis R. Rodriguez 			kfree(reg_beacon);
2355e38f8a7aSLuis R. Rodriguez 		}
2356e38f8a7aSLuis R. Rodriguez 	}
2357e38f8a7aSLuis R. Rodriguez 
2358fe33eb39SLuis R. Rodriguez 	spin_lock(&reg_requests_lock);
2359fe33eb39SLuis R. Rodriguez 	if (!list_empty(&reg_requests_list)) {
2360fe33eb39SLuis R. Rodriguez 		list_for_each_entry_safe(reg_request, tmp,
2361fe33eb39SLuis R. Rodriguez 					 &reg_requests_list, list) {
2362fe33eb39SLuis R. Rodriguez 			list_del(&reg_request->list);
2363fe33eb39SLuis R. Rodriguez 			kfree(reg_request);
2364fe33eb39SLuis R. Rodriguez 		}
2365fe33eb39SLuis R. Rodriguez 	}
2366fe33eb39SLuis R. Rodriguez 	spin_unlock(&reg_requests_lock);
2367fe33eb39SLuis R. Rodriguez 
2368a1794390SLuis R. Rodriguez 	mutex_unlock(&cfg80211_mutex);
23698318d78aSJohannes Berg }
2370