blob: c24c8bf3c9884cb2316098e2bbf4e11401e3a240 [file] [log] [blame]
Johannes Berg8318d782008-01-24 19:38:38 +01001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -08005 * Copyright 2008-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com>
Johannes Berg2740f0c2014-09-03 15:24:58 +03006 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Berg8318d782008-01-24 19:38:38 +01007 *
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -08008 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Johannes Berg8318d782008-01-24 19:38:38 +010019 */
20
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -080021
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070022/**
23 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010024 *
25 * The usual implementation is for a driver to read a device EEPROM to
26 * determine which regulatory domain it should be operating under, then
27 * looking up the allowable channels in a driver-local table and finally
28 * registering those channels in the wiphy structure.
29 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070030 * Another set of compliance enforcement is for drivers to use their
31 * own compliance limits which can be stored on the EEPROM. The host
32 * driver or firmware may ensure these are used.
33 *
34 * In addition to all this we provide an extra layer of regulatory
35 * conformance. For drivers which do not have any regulatory
36 * information CRDA provides the complete regulatory solution.
37 * For others it provides a community effort on further restrictions
38 * to enhance compliance.
39 *
40 * Note: When number of rules --> infinity we will not be able to
41 * index on alpha2 any more, instead we'll probably have to
42 * rely on some SHA1 checksum of the regdomain for example.
43 *
Johannes Berg8318d782008-01-24 19:38:38 +010044 */
Joe Perchese9c02682010-11-16 19:56:49 -080045
46#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47
Johannes Berg8318d782008-01-24 19:38:38 +010048#include <linux/kernel.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040049#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070051#include <linux/list.h>
John W. Linvillec61029c2010-08-05 14:26:24 -040052#include <linux/ctype.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070053#include <linux/nl80211.h>
54#include <linux/platform_device.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040055#include <linux/moduleparam.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070056#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010057#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070058#include "reg.h"
Arik Nemtsovad932f02014-11-27 09:44:55 +020059#include "rdev-ops.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050060#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040061#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010062
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050063#ifdef CONFIG_CFG80211_REG_DEBUG
Joe Perches12c5ffb2011-07-29 14:51:25 -070064#define REG_DBG_PRINT(format, args...) \
65 printk(KERN_DEBUG pr_fmt(format), ##args)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050066#else
John W. Linville82711952010-01-05 17:57:20 -050067#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050068#endif
69
Arik Nemtsovad932f02014-11-27 09:44:55 +020070/*
71 * Grace period we give before making sure all current interfaces reside on
72 * channels allowed by the current regulatory domain.
73 */
74#define REG_ENFORCE_GRACE_MS 60000
75
Ilan Peer52616f22014-02-25 16:26:00 +020076/**
77 * enum reg_request_treatment - regulatory request treatment
78 *
79 * @REG_REQ_OK: continue processing the regulatory request
80 * @REG_REQ_IGNORE: ignore the regulatory request
81 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
82 * be intersected with the current one.
83 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
84 * regulatory settings, and no further processing is required.
Ilan Peer52616f22014-02-25 16:26:00 +020085 */
Johannes Berg2f922122012-12-03 17:54:55 +010086enum reg_request_treatment {
87 REG_REQ_OK,
88 REG_REQ_IGNORE,
89 REG_REQ_INTERSECT,
90 REG_REQ_ALREADY_SET,
91};
92
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050093static struct regulatory_request core_request_world = {
94 .initiator = NL80211_REGDOM_SET_BY_CORE,
95 .alpha2[0] = '0',
96 .alpha2[1] = '0',
97 .intersect = false,
98 .processed = true,
99 .country_ie_env = ENVIRON_ANY,
100};
101
Johannes Berg38fd2142013-05-10 19:17:17 +0200102/*
103 * Receipt of information from last regulatory request,
104 * protected by RTNL (and can be accessed with RCU protection)
105 */
Johannes Bergc492db32012-12-06 16:29:25 +0100106static struct regulatory_request __rcu *last_request =
Johannes Bergcec3f0e2014-12-12 12:26:25 +0100107 (void __force __rcu *)&core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +0200108
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700109/* To trigger userspace events */
110static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +0100111
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500112/*
113 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +0200114 * the current one and a world regulatory domain in case we have no
Johannes Berge8da2bb2012-12-03 23:00:08 +0100115 * information to give us an alpha2.
Johannes Berg38fd2142013-05-10 19:17:17 +0200116 * (protected by RTNL, can be read under RCU)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500117 */
Johannes Berg458f4f92012-12-06 15:47:38 +0100118const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +0200119
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500120/*
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700121 * Number of devices that registered to the core
122 * that support cellular base station regulatory hints
Johannes Berg38fd2142013-05-10 19:17:17 +0200123 * (protected by RTNL)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700124 */
125static int reg_num_devs_support_basehint;
126
Ilan Peer52616f22014-02-25 16:26:00 +0200127/*
128 * State variable indicating if the platform on which the devices
129 * are attached is operating in an indoor environment. The state variable
130 * is relevant for all registered devices.
131 * (protected by RTNL)
132 */
133static bool reg_is_indoor;
134
Johannes Berg458f4f92012-12-06 15:47:38 +0100135static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
136{
Johannes Berg38fd2142013-05-10 19:17:17 +0200137 return rtnl_dereference(cfg80211_regdomain);
Johannes Berg458f4f92012-12-06 15:47:38 +0100138}
139
Arik Nemtsovad30ca22014-12-15 19:25:59 +0200140const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
Johannes Berg458f4f92012-12-06 15:47:38 +0100141{
Johannes Berg38fd2142013-05-10 19:17:17 +0200142 return rtnl_dereference(wiphy->regd);
Johannes Berg458f4f92012-12-06 15:47:38 +0100143}
144
Luis R. Rodriguez3ef121b2013-11-13 18:54:05 +0100145static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
146{
147 switch (dfs_region) {
148 case NL80211_DFS_UNSET:
149 return "unset";
150 case NL80211_DFS_FCC:
151 return "FCC";
152 case NL80211_DFS_ETSI:
153 return "ETSI";
154 case NL80211_DFS_JP:
155 return "JP";
156 }
157 return "Unknown";
158}
159
Luis R. Rodriguez6c474792013-11-25 20:56:09 +0100160enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
161{
162 const struct ieee80211_regdomain *regd = NULL;
163 const struct ieee80211_regdomain *wiphy_regd = NULL;
164
165 regd = get_cfg80211_regdom();
166 if (!wiphy)
167 goto out;
168
169 wiphy_regd = get_wiphy_regdom(wiphy);
170 if (!wiphy_regd)
171 goto out;
172
173 if (wiphy_regd->dfs_region == regd->dfs_region)
174 goto out;
175
176 REG_DBG_PRINT("%s: device specific dfs_region "
177 "(%s) disagrees with cfg80211's "
178 "central dfs_region (%s)\n",
179 dev_name(&wiphy->dev),
180 reg_dfs_region_str(wiphy_regd->dfs_region),
181 reg_dfs_region_str(regd->dfs_region));
182
183out:
184 return regd->dfs_region;
185}
186
Johannes Berg458f4f92012-12-06 15:47:38 +0100187static void rcu_free_regdom(const struct ieee80211_regdomain *r)
188{
189 if (!r)
190 return;
191 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
192}
193
Johannes Bergc492db32012-12-06 16:29:25 +0100194static struct regulatory_request *get_last_request(void)
195{
Johannes Berg38fd2142013-05-10 19:17:17 +0200196 return rcu_dereference_rtnl(last_request);
Johannes Bergc492db32012-12-06 16:29:25 +0100197}
198
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500199/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500200static LIST_HEAD(reg_requests_list);
201static spinlock_t reg_requests_lock;
202
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500203/* Used to queue up beacon hints for review */
204static LIST_HEAD(reg_pending_beacons);
205static spinlock_t reg_pending_beacons_lock;
206
207/* Used to keep track of processed beacon hints */
208static LIST_HEAD(reg_beacon_list);
209
210struct reg_beacon {
211 struct list_head list;
212 struct ieee80211_channel chan;
213};
214
Arik Nemtsovad932f02014-11-27 09:44:55 +0200215static void reg_check_chans_work(struct work_struct *work);
216static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);
217
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800218static void reg_todo(struct work_struct *work);
219static DECLARE_WORK(reg_work, reg_todo);
220
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -0700221static void reg_timeout_work(struct work_struct *work);
222static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
223
Johannes Berg734366d2008-09-15 10:56:48 +0200224/* We keep a static world regulatory domain in case of the absence of CRDA */
225static const struct ieee80211_regdomain world_regdom = {
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300226 .n_reg_rules = 6,
Johannes Berg734366d2008-09-15 10:56:48 +0200227 .alpha2 = "00",
228 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500229 /* IEEE 802.11b/g, channels 1..11 */
230 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Johannes Berg43c771a2012-11-12 10:51:34 +0100231 /* IEEE 802.11b/g, channels 12..13. */
232 REG_RULE(2467-10, 2472+10, 40, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200233 NL80211_RRF_NO_IR),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800234 /* IEEE 802.11 channel 14 - Only JP enables
235 * this and for 802.11b only */
236 REG_RULE(2484-10, 2484+10, 20, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200237 NL80211_RRF_NO_IR |
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800238 NL80211_RRF_NO_OFDM),
239 /* IEEE 802.11a, channel 36..48 */
Johannes Berg131a19b2013-03-04 20:54:46 +0100240 REG_RULE(5180-10, 5240+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200241 NL80211_RRF_NO_IR),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500242
Johannes Berg131a19b2013-03-04 20:54:46 +0100243 /* IEEE 802.11a, channel 52..64 - DFS required */
244 REG_RULE(5260-10, 5320+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200245 NL80211_RRF_NO_IR |
Johannes Berg131a19b2013-03-04 20:54:46 +0100246 NL80211_RRF_DFS),
247
248 /* IEEE 802.11a, channel 100..144 - DFS required */
249 REG_RULE(5500-10, 5720+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200250 NL80211_RRF_NO_IR |
Johannes Berg131a19b2013-03-04 20:54:46 +0100251 NL80211_RRF_DFS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500252
253 /* IEEE 802.11a, channel 149..165 */
Johannes Berg8ab9d852012-12-03 22:09:22 +0100254 REG_RULE(5745-10, 5825+10, 80, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200255 NL80211_RRF_NO_IR),
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300256
257 /* IEEE 802.11ad (60gHz), channels 1..3 */
258 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200259 }
260};
261
Johannes Berg38fd2142013-05-10 19:17:17 +0200262/* protected by RTNL */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200263static const struct ieee80211_regdomain *cfg80211_world_regdom =
264 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200265
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400266static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500267static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400268
Johannes Berg734366d2008-09-15 10:56:48 +0200269module_param(ieee80211_regdom, charp, 0444);
270MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
271
Arik Nemtsovc8883932014-04-21 20:39:34 -0700272static void reg_free_request(struct regulatory_request *request)
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800273{
Arik Nemtsovc8883932014-04-21 20:39:34 -0700274 if (request != get_last_request())
275 kfree(request);
276}
277
278static void reg_free_last_request(void)
279{
280 struct regulatory_request *lr = get_last_request();
281
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800282 if (lr != &core_request_world && lr)
283 kfree_rcu(lr, rcu_head);
284}
285
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800286static void reg_update_last_request(struct regulatory_request *request)
287{
Luis R. Rodriguez255e25b2014-02-25 17:09:40 -0800288 struct regulatory_request *lr;
289
290 lr = get_last_request();
291 if (lr == request)
292 return;
293
Arik Nemtsovc8883932014-04-21 20:39:34 -0700294 reg_free_last_request();
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800295 rcu_assign_pointer(last_request, request);
296}
297
Johannes Berg379b82f2012-12-06 15:44:07 +0100298static void reset_regdomains(bool full_reset,
299 const struct ieee80211_regdomain *new_regdom)
Johannes Berg734366d2008-09-15 10:56:48 +0200300{
Johannes Berg458f4f92012-12-06 15:47:38 +0100301 const struct ieee80211_regdomain *r;
302
Johannes Berg38fd2142013-05-10 19:17:17 +0200303 ASSERT_RTNL();
Johannes Berge8da2bb2012-12-03 23:00:08 +0100304
Johannes Berg458f4f92012-12-06 15:47:38 +0100305 r = get_cfg80211_regdom();
306
Johannes Berg942b25c2008-09-15 11:26:47 +0200307 /* avoid freeing static information or freeing something twice */
Johannes Berg458f4f92012-12-06 15:47:38 +0100308 if (r == cfg80211_world_regdom)
309 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200310 if (cfg80211_world_regdom == &world_regdom)
311 cfg80211_world_regdom = NULL;
Johannes Berg458f4f92012-12-06 15:47:38 +0100312 if (r == &world_regdom)
313 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200314
Johannes Berg458f4f92012-12-06 15:47:38 +0100315 rcu_free_regdom(r);
316 rcu_free_regdom(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200317
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200318 cfg80211_world_regdom = &world_regdom;
Johannes Berg458f4f92012-12-06 15:47:38 +0100319 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500320
321 if (!full_reset)
322 return;
323
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800324 reg_update_last_request(&core_request_world);
Johannes Berg734366d2008-09-15 10:56:48 +0200325}
326
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500327/*
328 * Dynamic world regulatory domain requested by the wireless
329 * core upon initialization
330 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200331static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200332{
Johannes Bergc492db32012-12-06 16:29:25 +0100333 struct regulatory_request *lr;
Johannes Berg734366d2008-09-15 10:56:48 +0200334
Johannes Bergc492db32012-12-06 16:29:25 +0100335 lr = get_last_request();
336
337 WARN_ON(!lr);
Johannes Berge8da2bb2012-12-03 23:00:08 +0100338
Johannes Berg379b82f2012-12-06 15:44:07 +0100339 reset_regdomains(false, rd);
Johannes Berg734366d2008-09-15 10:56:48 +0200340
341 cfg80211_world_regdom = rd;
Johannes Berg734366d2008-09-15 10:56:48 +0200342}
Johannes Berg734366d2008-09-15 10:56:48 +0200343
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200344bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100345{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700346 if (!alpha2)
347 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100348 return alpha2[0] == '0' && alpha2[1] == '0';
Johannes Berg8318d782008-01-24 19:38:38 +0100349}
350
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200351static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700352{
353 if (!alpha2)
354 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100355 return alpha2[0] && alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700356}
Johannes Berg8318d782008-01-24 19:38:38 +0100357
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200358static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700359{
360 if (!alpha2)
361 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500362 /*
363 * Special case where regulatory domain was built by driver
364 * but a specific alpha2 cannot be determined
365 */
Johannes Berg1a919312012-12-03 17:21:11 +0100366 return alpha2[0] == '9' && alpha2[1] == '9';
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700367}
368
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800369static bool is_intersected_alpha2(const char *alpha2)
370{
371 if (!alpha2)
372 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500373 /*
374 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800375 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500376 * structures
377 */
Johannes Berg1a919312012-12-03 17:21:11 +0100378 return alpha2[0] == '9' && alpha2[1] == '8';
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800379}
380
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200381static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700382{
383 if (!alpha2)
384 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100385 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700386}
387
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200388static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700389{
390 if (!alpha2_x || !alpha2_y)
391 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100392 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700393}
394
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500395static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700396{
Johannes Berg458f4f92012-12-06 15:47:38 +0100397 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500398
Johannes Berg458f4f92012-12-06 15:47:38 +0100399 if (!r)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700400 return true;
Johannes Berg458f4f92012-12-06 15:47:38 +0100401 return !alpha2_equal(r->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700402}
403
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500404/*
405 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
406 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
407 * has ever been issued.
408 */
409static bool is_user_regdom_saved(void)
410{
411 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
412 return false;
413
414 /* This would indicate a mistake on the design */
Johannes Berg1a919312012-12-03 17:21:11 +0100415 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500416 "Unexpected user alpha2: %c%c\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100417 user_alpha2[0], user_alpha2[1]))
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500418 return false;
419
420 return true;
421}
422
Johannes Berge9763c32012-12-03 16:59:46 +0100423static const struct ieee80211_regdomain *
424reg_copy_regd(const struct ieee80211_regdomain *src_regd)
John W. Linville3b377ea2009-12-18 17:59:01 -0500425{
426 struct ieee80211_regdomain *regd;
Johannes Berge9763c32012-12-03 16:59:46 +0100427 int size_of_regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500428 unsigned int i;
429
Johannes Berg82f20852012-12-04 12:49:16 +0100430 size_of_regd =
431 sizeof(struct ieee80211_regdomain) +
432 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
John W. Linville3b377ea2009-12-18 17:59:01 -0500433
434 regd = kzalloc(size_of_regd, GFP_KERNEL);
435 if (!regd)
Johannes Berge9763c32012-12-03 16:59:46 +0100436 return ERR_PTR(-ENOMEM);
John W. Linville3b377ea2009-12-18 17:59:01 -0500437
438 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
439
440 for (i = 0; i < src_regd->n_reg_rules; i++)
441 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
Johannes Berge9763c32012-12-03 16:59:46 +0100442 sizeof(struct ieee80211_reg_rule));
John W. Linville3b377ea2009-12-18 17:59:01 -0500443
Johannes Berge9763c32012-12-03 16:59:46 +0100444 return regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500445}
446
447#ifdef CONFIG_CFG80211_INTERNAL_REGDB
448struct reg_regdb_search_request {
449 char alpha2[2];
450 struct list_head list;
451};
452
453static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400454static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500455
456static void reg_regdb_search(struct work_struct *work)
457{
458 struct reg_regdb_search_request *request;
Johannes Berge9763c32012-12-03 16:59:46 +0100459 const struct ieee80211_regdomain *curdom, *regdom = NULL;
460 int i;
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700461
Johannes Berg5fe231e2013-05-08 21:45:15 +0200462 rtnl_lock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500463
John W. Linville368d06f2010-03-16 15:40:59 -0400464 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500465 while (!list_empty(&reg_regdb_search_list)) {
466 request = list_first_entry(&reg_regdb_search_list,
467 struct reg_regdb_search_request,
468 list);
469 list_del(&request->list);
470
Johannes Berg1a919312012-12-03 17:21:11 +0100471 for (i = 0; i < reg_regdb_size; i++) {
John W. Linville3b377ea2009-12-18 17:59:01 -0500472 curdom = reg_regdb[i];
473
Johannes Berg1a919312012-12-03 17:21:11 +0100474 if (alpha2_equal(request->alpha2, curdom->alpha2)) {
Johannes Berge9763c32012-12-03 16:59:46 +0100475 regdom = reg_copy_regd(curdom);
John W. Linville3b377ea2009-12-18 17:59:01 -0500476 break;
477 }
478 }
479
480 kfree(request);
481 }
John W. Linville368d06f2010-03-16 15:40:59 -0400482 mutex_unlock(&reg_regdb_search_mutex);
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700483
Johannes Berge9763c32012-12-03 16:59:46 +0100484 if (!IS_ERR_OR_NULL(regdom))
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700485 set_regdom(regdom);
486
Johannes Berg5fe231e2013-05-08 21:45:15 +0200487 rtnl_unlock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500488}
489
490static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
491
492static void reg_regdb_query(const char *alpha2)
493{
494 struct reg_regdb_search_request *request;
495
496 if (!alpha2)
497 return;
498
499 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
500 if (!request)
501 return;
502
503 memcpy(request->alpha2, alpha2, 2);
504
John W. Linville368d06f2010-03-16 15:40:59 -0400505 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500506 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400507 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500508
509 schedule_work(&reg_regdb_work);
510}
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700511
512/* Feel free to add any other sanity checks here */
513static void reg_regdb_size_check(void)
514{
515 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
516 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
517}
John W. Linville3b377ea2009-12-18 17:59:01 -0500518#else
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700519static inline void reg_regdb_size_check(void) {}
John W. Linville3b377ea2009-12-18 17:59:01 -0500520static inline void reg_regdb_query(const char *alpha2) {}
521#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
522
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500523/*
524 * This lets us keep regulatory code which is updated on a regulatory
Johannes Berg1226d252014-02-25 15:43:36 +0100525 * basis in userspace.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500526 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700527static int call_crda(const char *alpha2)
528{
Johannes Berg1226d252014-02-25 15:43:36 +0100529 char country[12];
530 char *env[] = { country, NULL };
531
532 snprintf(country, sizeof(country), "COUNTRY=%c%c",
533 alpha2[0], alpha2[1]);
534
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700535 if (!is_world_regdom((char *) alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -0800536 pr_info("Calling CRDA for country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700537 alpha2[0], alpha2[1]);
538 else
Joe Perchese9c02682010-11-16 19:56:49 -0800539 pr_info("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700540
John W. Linville3b377ea2009-12-18 17:59:01 -0500541 /* query internal regulatory database (if it exists) */
542 reg_regdb_query(alpha2);
543
Johannes Berg1226d252014-02-25 15:43:36 +0100544 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700545}
546
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -0800547static enum reg_request_treatment
548reg_call_crda(struct regulatory_request *request)
549{
550 if (call_crda(request->alpha2))
551 return REG_REQ_IGNORE;
552 return REG_REQ_OK;
553}
554
Luis R. Rodrigueze4387682013-11-05 09:18:01 -0800555bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700556{
Johannes Bergc492db32012-12-06 16:29:25 +0100557 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400558
Johannes Bergc492db32012-12-06 16:29:25 +0100559 if (!lr || lr->processed)
Johannes Bergf6037d02008-10-21 11:01:33 +0200560 return false;
561
Johannes Bergc492db32012-12-06 16:29:25 +0100562 return alpha2_equal(lr->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700563}
564
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100565static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
566{
567 struct regulatory_request *lr = get_last_request();
568
569 /*
570 * Follow the driver's regulatory domain, if present, unless a country
571 * IE has been processed or a user wants to help complaince further
572 */
573 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
574 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
575 wiphy->regd)
576 return get_wiphy_regdom(wiphy);
577
578 return get_cfg80211_regdom();
579}
580
Arik Nemtsova6d4a532014-10-23 09:37:33 +0300581static unsigned int
582reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
583 const struct ieee80211_reg_rule *rule)
Janusz Dziedzic97524822014-01-30 09:52:20 +0100584{
585 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
586 const struct ieee80211_freq_range *freq_range_tmp;
587 const struct ieee80211_reg_rule *tmp;
588 u32 start_freq, end_freq, idx, no;
589
590 for (idx = 0; idx < rd->n_reg_rules; idx++)
591 if (rule == &rd->reg_rules[idx])
592 break;
593
594 if (idx == rd->n_reg_rules)
595 return 0;
596
597 /* get start_freq */
598 no = idx;
599
600 while (no) {
601 tmp = &rd->reg_rules[--no];
602 freq_range_tmp = &tmp->freq_range;
603
604 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
605 break;
606
Janusz Dziedzic97524822014-01-30 09:52:20 +0100607 freq_range = freq_range_tmp;
608 }
609
610 start_freq = freq_range->start_freq_khz;
611
612 /* get end_freq */
613 freq_range = &rule->freq_range;
614 no = idx;
615
616 while (no < rd->n_reg_rules - 1) {
617 tmp = &rd->reg_rules[++no];
618 freq_range_tmp = &tmp->freq_range;
619
620 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
621 break;
622
Janusz Dziedzic97524822014-01-30 09:52:20 +0100623 freq_range = freq_range_tmp;
624 }
625
626 end_freq = freq_range->end_freq_khz;
627
628 return end_freq - start_freq;
629}
630
Arik Nemtsova6d4a532014-10-23 09:37:33 +0300631unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
632 const struct ieee80211_reg_rule *rule)
633{
634 unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);
635
636 if (rule->flags & NL80211_RRF_NO_160MHZ)
637 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
638 if (rule->flags & NL80211_RRF_NO_80MHZ)
639 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));
640
641 /*
642 * HT40+/HT40- limits are handled per-channel. Only limit BW if both
643 * are not allowed.
644 */
645 if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
646 rule->flags & NL80211_RRF_NO_HT40PLUS)
647 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));
648
649 return bw;
650}
651
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700652/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200653static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700654{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200655 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700656 u32 freq_diff;
657
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800658 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700659 return false;
660
661 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
662 return false;
663
664 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
665
Roel Kluinbd05f282009-03-03 22:55:21 +0100666 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg1a919312012-12-03 17:21:11 +0100667 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700668 return false;
669
670 return true;
671}
672
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200673static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700674{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200675 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700676 unsigned int i;
677
678 if (!rd->n_reg_rules)
679 return false;
680
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800681 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
682 return false;
683
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700684 for (i = 0; i < rd->n_reg_rules; i++) {
685 reg_rule = &rd->reg_rules[i];
686 if (!is_valid_reg_rule(reg_rule))
687 return false;
688 }
689
690 return true;
691}
692
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400693static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100694 u32 center_freq_khz, u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700695{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400696 u32 start_freq_khz, end_freq_khz;
697
698 start_freq_khz = center_freq_khz - (bw_khz/2);
699 end_freq_khz = center_freq_khz + (bw_khz/2);
700
701 if (start_freq_khz >= freq_range->start_freq_khz &&
702 end_freq_khz <= freq_range->end_freq_khz)
703 return true;
704
705 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700706}
707
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800708/**
709 * freq_in_rule_band - tells us if a frequency is in a frequency band
710 * @freq_range: frequency rule we want to query
711 * @freq_khz: frequency we are inquiring about
712 *
713 * This lets us know if a specific frequency rule is or is not relevant to
714 * a specific frequency's band. Bands are device specific and artificial
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200715 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
716 * however it is safe for now to assume that a frequency rule should not be
717 * part of a frequency's band if the start freq or end freq are off by more
718 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
719 * 60 GHz band.
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800720 * This resolution can be lowered and should be considered as we add
721 * regulatory rule support for other "bands".
722 **/
723static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg1a919312012-12-03 17:21:11 +0100724 u32 freq_khz)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800725{
726#define ONE_GHZ_IN_KHZ 1000000
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200727 /*
728 * From 802.11ad: directional multi-gigabit (DMG):
729 * Pertaining to operation in a frequency band containing a channel
730 * with the Channel starting frequency above 45 GHz.
731 */
732 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
733 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
734 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800735 return true;
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200736 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800737 return true;
738 return false;
739#undef ONE_GHZ_IN_KHZ
740}
741
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500742/*
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100743 * Later on we can perhaps use the more restrictive DFS
744 * region but we don't have information for that yet so
745 * for now simply disallow conflicts.
746 */
747static enum nl80211_dfs_regions
748reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
749 const enum nl80211_dfs_regions dfs_region2)
750{
751 if (dfs_region1 != dfs_region2)
752 return NL80211_DFS_UNSET;
753 return dfs_region1;
754}
755
756/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500757 * Helper for regdom_intersect(), this does the real
758 * mathematical intersection fun
759 */
Janusz Dziedzic97524822014-01-30 09:52:20 +0100760static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
761 const struct ieee80211_regdomain *rd2,
762 const struct ieee80211_reg_rule *rule1,
Johannes Berg1a919312012-12-03 17:21:11 +0100763 const struct ieee80211_reg_rule *rule2,
764 struct ieee80211_reg_rule *intersected_rule)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700765{
766 const struct ieee80211_freq_range *freq_range1, *freq_range2;
767 struct ieee80211_freq_range *freq_range;
768 const struct ieee80211_power_rule *power_rule1, *power_rule2;
769 struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +0100770 u32 freq_diff, max_bandwidth1, max_bandwidth2;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700771
772 freq_range1 = &rule1->freq_range;
773 freq_range2 = &rule2->freq_range;
774 freq_range = &intersected_rule->freq_range;
775
776 power_rule1 = &rule1->power_rule;
777 power_rule2 = &rule2->power_rule;
778 power_rule = &intersected_rule->power_rule;
779
780 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100781 freq_range2->start_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700782 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100783 freq_range2->end_freq_khz);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100784
785 max_bandwidth1 = freq_range1->max_bandwidth_khz;
786 max_bandwidth2 = freq_range2->max_bandwidth_khz;
787
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100788 if (rule1->flags & NL80211_RRF_AUTO_BW)
789 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
790 if (rule2->flags & NL80211_RRF_AUTO_BW)
791 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100792
793 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700794
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100795 intersected_rule->flags = rule1->flags | rule2->flags;
796
797 /*
798 * In case NL80211_RRF_AUTO_BW requested for both rules
799 * set AUTO_BW in intersected rule also. Next we will
800 * calculate BW correctly in handle_channel function.
801 * In other case remove AUTO_BW flag while we calculate
802 * maximum bandwidth correctly and auto calculation is
803 * not required.
804 */
805 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
806 (rule2->flags & NL80211_RRF_AUTO_BW))
807 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
808 else
809 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
810
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700811 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
812 if (freq_range->max_bandwidth_khz > freq_diff)
813 freq_range->max_bandwidth_khz = freq_diff;
814
815 power_rule->max_eirp = min(power_rule1->max_eirp,
816 power_rule2->max_eirp);
817 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
818 power_rule2->max_antenna_gain);
819
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100820 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
821 rule2->dfs_cac_ms);
822
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700823 if (!is_valid_reg_rule(intersected_rule))
824 return -EINVAL;
825
826 return 0;
827}
828
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300829/* check whether old rule contains new rule */
830static bool rule_contains(struct ieee80211_reg_rule *r1,
831 struct ieee80211_reg_rule *r2)
832{
833 /* for simplicity, currently consider only same flags */
834 if (r1->flags != r2->flags)
835 return false;
836
837 /* verify r1 is more restrictive */
838 if ((r1->power_rule.max_antenna_gain >
839 r2->power_rule.max_antenna_gain) ||
840 r1->power_rule.max_eirp > r2->power_rule.max_eirp)
841 return false;
842
843 /* make sure r2's range is contained within r1 */
844 if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
845 r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
846 return false;
847
848 /* and finally verify that r1.max_bw >= r2.max_bw */
849 if (r1->freq_range.max_bandwidth_khz <
850 r2->freq_range.max_bandwidth_khz)
851 return false;
852
853 return true;
854}
855
856/* add or extend current rules. do nothing if rule is already contained */
857static void add_rule(struct ieee80211_reg_rule *rule,
858 struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
859{
860 struct ieee80211_reg_rule *tmp_rule;
861 int i;
862
863 for (i = 0; i < *n_rules; i++) {
864 tmp_rule = &reg_rules[i];
865 /* rule is already contained - do nothing */
866 if (rule_contains(tmp_rule, rule))
867 return;
868
869 /* extend rule if possible */
870 if (rule_contains(rule, tmp_rule)) {
871 memcpy(tmp_rule, rule, sizeof(*rule));
872 return;
873 }
874 }
875
876 memcpy(&reg_rules[*n_rules], rule, sizeof(*rule));
877 (*n_rules)++;
878}
879
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700880/**
881 * regdom_intersect - do the intersection between two regulatory domains
882 * @rd1: first regulatory domain
883 * @rd2: second regulatory domain
884 *
885 * Use this function to get the intersection between two regulatory domains.
886 * Once completed we will mark the alpha2 for the rd as intersected, "98",
887 * as no one single alpha2 can represent this regulatory domain.
888 *
889 * Returns a pointer to the regulatory domain structure which will hold the
890 * resulting intersection of rules between rd1 and rd2. We will
891 * kzalloc() this structure for you.
892 */
Johannes Berg1a919312012-12-03 17:21:11 +0100893static struct ieee80211_regdomain *
894regdom_intersect(const struct ieee80211_regdomain *rd1,
895 const struct ieee80211_regdomain *rd2)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700896{
897 int r, size_of_regd;
898 unsigned int x, y;
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300899 unsigned int num_rules = 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700900 const struct ieee80211_reg_rule *rule1, *rule2;
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300901 struct ieee80211_reg_rule intersected_rule;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700902 struct ieee80211_regdomain *rd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700903
904 if (!rd1 || !rd2)
905 return NULL;
906
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500907 /*
908 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700909 * build them. This is to so we can malloc() and free() a
910 * regdomain once. The reason we use reg_rules_intersect() here
911 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500912 * All rules that do check out OK are valid.
913 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700914
915 for (x = 0; x < rd1->n_reg_rules; x++) {
916 rule1 = &rd1->reg_rules[x];
917 for (y = 0; y < rd2->n_reg_rules; y++) {
918 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100919 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300920 &intersected_rule))
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700921 num_rules++;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700922 }
923 }
924
925 if (!num_rules)
926 return NULL;
927
928 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg82f20852012-12-04 12:49:16 +0100929 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700930
931 rd = kzalloc(size_of_regd, GFP_KERNEL);
932 if (!rd)
933 return NULL;
934
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300935 for (x = 0; x < rd1->n_reg_rules; x++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700936 rule1 = &rd1->reg_rules[x];
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300937 for (y = 0; y < rd2->n_reg_rules; y++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700938 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100939 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300940 &intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500941 /*
942 * No need to memset here the intersected rule here as
943 * we're not using the stack anymore
944 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700945 if (r)
946 continue;
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300947
948 add_rule(&intersected_rule, rd->reg_rules,
949 &rd->n_reg_rules);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700950 }
951 }
952
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700953 rd->alpha2[0] = '9';
954 rd->alpha2[1] = '8';
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100955 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
956 rd2->dfs_region);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700957
958 return rd;
959}
960
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500961/*
962 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
963 * want to just have the channel structure use these
964 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700965static u32 map_regdom_flags(u32 rd_flags)
966{
967 u32 channel_flags = 0;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200968 if (rd_flags & NL80211_RRF_NO_IR_ALL)
969 channel_flags |= IEEE80211_CHAN_NO_IR;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700970 if (rd_flags & NL80211_RRF_DFS)
971 channel_flags |= IEEE80211_CHAN_RADAR;
Seth Forshee03f6b082012-08-01 15:58:42 -0500972 if (rd_flags & NL80211_RRF_NO_OFDM)
973 channel_flags |= IEEE80211_CHAN_NO_OFDM;
David Spinadel570dbde2014-02-23 09:12:59 +0200974 if (rd_flags & NL80211_RRF_NO_OUTDOOR)
975 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
Arik Nemtsova6d4a532014-10-23 09:37:33 +0300976 if (rd_flags & NL80211_RRF_GO_CONCURRENT)
977 channel_flags |= IEEE80211_CHAN_GO_CONCURRENT;
978 if (rd_flags & NL80211_RRF_NO_HT40MINUS)
979 channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
980 if (rd_flags & NL80211_RRF_NO_HT40PLUS)
981 channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
982 if (rd_flags & NL80211_RRF_NO_80MHZ)
983 channel_flags |= IEEE80211_CHAN_NO_80MHZ;
984 if (rd_flags & NL80211_RRF_NO_160MHZ)
985 channel_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700986 return channel_flags;
987}
988
Johannes Berg361c9c82012-12-06 15:57:14 +0100989static const struct ieee80211_reg_rule *
990freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
991 const struct ieee80211_regdomain *regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100992{
993 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800994 bool band_rule_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400995 bool bw_fits = false;
996
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800997 if (!regd)
Johannes Berg361c9c82012-12-06 15:57:14 +0100998 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700999
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001000 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001001 const struct ieee80211_reg_rule *rr;
1002 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001003
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001004 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001005 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001006
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001007 /*
1008 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001009 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001010 * not overwrite it once found
1011 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001012 if (!band_rule_found)
1013 band_rule_found = freq_in_rule_band(fr, center_freq);
1014
Johannes Berge33e2242014-06-23 11:06:16 +02001015 bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001016
Johannes Berg361c9c82012-12-06 15:57:14 +01001017 if (band_rule_found && bw_fits)
1018 return rr;
Johannes Berg8318d782008-01-24 19:38:38 +01001019 }
1020
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001021 if (!band_rule_found)
Johannes Berg361c9c82012-12-06 15:57:14 +01001022 return ERR_PTR(-ERANGE);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001023
Johannes Berg361c9c82012-12-06 15:57:14 +01001024 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001025}
1026
Johannes Berg361c9c82012-12-06 15:57:14 +01001027const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
1028 u32 center_freq)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001029{
Johannes Berg5d885b92012-12-04 00:14:17 +01001030 const struct ieee80211_regdomain *regd;
Johannes Berg1a919312012-12-03 17:21:11 +01001031
Janusz Dziedzice3961af2014-01-25 11:24:11 +01001032 regd = reg_get_regdomain(wiphy);
Johannes Berg5d885b92012-12-04 00:14:17 +01001033
Johannes Berg361c9c82012-12-06 15:57:14 +01001034 return freq_reg_info_regd(wiphy, center_freq, regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001035}
John W. Linville4f366c52010-07-15 14:57:33 -04001036EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001037
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001038const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301039{
1040 switch (initiator) {
1041 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001042 return "core";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301043 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001044 return "user";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301045 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001046 return "driver";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301047 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001048 return "country IE";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301049 default:
1050 WARN_ON(1);
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001051 return "bug";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301052 }
1053}
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001054EXPORT_SYMBOL(reg_initiator_name);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301055
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001056#ifdef CONFIG_CFG80211_REG_DEBUG
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001057static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
1058 struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301059 const struct ieee80211_reg_rule *reg_rule)
1060{
1061 const struct ieee80211_power_rule *power_rule;
1062 const struct ieee80211_freq_range *freq_range;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001063 char max_antenna_gain[32], bw[32];
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301064
1065 power_rule = &reg_rule->power_rule;
1066 freq_range = &reg_rule->freq_range;
1067
1068 if (!power_rule->max_antenna_gain)
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001069 snprintf(max_antenna_gain, sizeof(max_antenna_gain), "N/A");
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301070 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001071 snprintf(max_antenna_gain, sizeof(max_antenna_gain), "%d",
1072 power_rule->max_antenna_gain);
1073
1074 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
1075 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
1076 freq_range->max_bandwidth_khz,
1077 reg_get_max_bandwidth(regd, reg_rule));
1078 else
1079 snprintf(bw, sizeof(bw), "%d KHz",
1080 freq_range->max_bandwidth_khz);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301081
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001082 REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
1083 chan->center_freq);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301084
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001085 REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)\n",
Johannes Berg1a919312012-12-03 17:21:11 +01001086 freq_range->start_freq_khz, freq_range->end_freq_khz,
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001087 bw, max_antenna_gain,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301088 power_rule->max_eirp);
1089}
1090#else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001091static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
1092 struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301093 const struct ieee80211_reg_rule *reg_rule)
1094{
1095 return;
1096}
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301097#endif
1098
Johannes Berge33e2242014-06-23 11:06:16 +02001099/*
1100 * Note that right now we assume the desired channel bandwidth
1101 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1102 * per channel, the primary and the extension channel).
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001103 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001104static void handle_channel(struct wiphy *wiphy,
1105 enum nl80211_reg_initiator initiator,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001106 struct ieee80211_channel *chan)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001107{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001108 u32 flags, bw_flags = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001109 const struct ieee80211_reg_rule *reg_rule = NULL;
1110 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001111 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001112 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01001113 struct regulatory_request *lr = get_last_request();
Janusz Dziedzic97524822014-01-30 09:52:20 +01001114 const struct ieee80211_regdomain *regd;
1115 u32 max_bandwidth_khz;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001116
Johannes Bergc492db32012-12-06 16:29:25 +01001117 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001118
1119 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001120
Johannes Berg361c9c82012-12-06 15:57:14 +01001121 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1122 if (IS_ERR(reg_rule)) {
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001123 /*
1124 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001125 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001126 * from a Country IE and the Country IE had no information
1127 * about a band. The IEEE 802.11 spec allows for an AP
1128 * to send only a subset of the regulatory rules allowed,
1129 * so an AP in the US that only supports 2.4 GHz may only send
1130 * a country IE with information for the 2.4 GHz band
1131 * while 5 GHz is still supported.
1132 */
1133 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg361c9c82012-12-06 15:57:14 +01001134 PTR_ERR(reg_rule) == -ERANGE)
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001135 return;
1136
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001137 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1138 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001139 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001140 REG_DBG_PRINT("Disabling freq %d MHz for good\n",
1141 chan->center_freq);
1142 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1143 chan->flags = chan->orig_flags;
1144 } else {
1145 REG_DBG_PRINT("Disabling freq %d MHz\n",
1146 chan->center_freq);
1147 chan->flags |= IEEE80211_CHAN_DISABLED;
1148 }
Johannes Berg8318d782008-01-24 19:38:38 +01001149 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001150 }
Johannes Berg8318d782008-01-24 19:38:38 +01001151
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001152 regd = reg_get_regdomain(wiphy);
1153 chan_reg_rule_print_dbg(regd, chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301154
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001155 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001156 freq_range = &reg_rule->freq_range;
1157
Janusz Dziedzic97524822014-01-30 09:52:20 +01001158 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1159 /* Check if auto calculation requested */
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001160 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001161 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
Janusz Dziedzic97524822014-01-30 09:52:20 +01001162
1163 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
Johannes Berge33e2242014-06-23 11:06:16 +02001164 bw_flags = IEEE80211_CHAN_NO_HT40;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001165 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001166 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001167 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001168 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001169
Johannes Bergc492db32012-12-06 16:29:25 +01001170 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001171 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001172 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001173 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001174 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001175 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001176 * settings
1177 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001178 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001179 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001180 chan->max_antenna_gain = chan->orig_mag =
1181 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001182 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001183 (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic4f267c12014-04-09 13:47:12 +02001184
1185 if (chan->flags & IEEE80211_CHAN_RADAR) {
1186 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1187 if (reg_rule->dfs_cac_ms)
1188 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1189 }
1190
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001191 return;
1192 }
1193
Simon Wunderlich04f39042013-02-08 18:16:19 +01001194 chan->dfs_state = NL80211_DFS_USABLE;
1195 chan->dfs_state_entered = jiffies;
1196
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +05301197 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001198 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg1a919312012-12-03 17:21:11 +01001199 chan->max_antenna_gain =
1200 min_t(int, chan->orig_mag,
1201 MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +02001202 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic089027e2014-02-21 19:46:12 +01001203
1204 if (chan->flags & IEEE80211_CHAN_RADAR) {
1205 if (reg_rule->dfs_cac_ms)
1206 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1207 else
1208 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1209 }
1210
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001211 if (chan->orig_mpwr) {
1212 /*
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001213 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1214 * will always follow the passed country IE power settings.
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001215 */
1216 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001217 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001218 chan->max_power = chan->max_reg_power;
1219 else
1220 chan->max_power = min(chan->orig_mpwr,
1221 chan->max_reg_power);
1222 } else
1223 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +01001224}
1225
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001226static void handle_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001227 enum nl80211_reg_initiator initiator,
1228 struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +01001229{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001230 unsigned int i;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001231
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001232 if (!sband)
1233 return;
Johannes Berg8318d782008-01-24 19:38:38 +01001234
1235 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001236 handle_channel(wiphy, initiator, &sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +01001237}
1238
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001239static bool reg_request_cell_base(struct regulatory_request *request)
1240{
1241 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1242 return false;
Johannes Berg1a919312012-12-03 17:21:11 +01001243 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001244}
1245
1246bool reg_last_request_cell_base(void)
1247{
Johannes Berg38fd2142013-05-10 19:17:17 +02001248 return reg_request_cell_base(get_last_request());
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001249}
1250
Ilan Peer94fc6612014-02-23 09:13:00 +02001251#ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001252/* Core specific check */
Johannes Berg2f922122012-12-03 17:54:55 +01001253static enum reg_request_treatment
1254reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001255{
Johannes Bergc492db32012-12-06 16:29:25 +01001256 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001257
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001258 if (!reg_num_devs_support_basehint)
Johannes Berg2f922122012-12-03 17:54:55 +01001259 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001260
Johannes Bergc492db32012-12-06 16:29:25 +01001261 if (reg_request_cell_base(lr) &&
Johannes Berg1a919312012-12-03 17:21:11 +01001262 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001263 return REG_REQ_ALREADY_SET;
Johannes Berg1a919312012-12-03 17:21:11 +01001264
Johannes Berg2f922122012-12-03 17:54:55 +01001265 return REG_REQ_OK;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001266}
1267
1268/* Device specific check */
1269static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1270{
Johannes Berg1a919312012-12-03 17:21:11 +01001271 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001272}
1273#else
1274static int reg_ignore_cell_hint(struct regulatory_request *pending_request)
1275{
Johannes Berg2f922122012-12-03 17:54:55 +01001276 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001277}
Johannes Berg1a919312012-12-03 17:21:11 +01001278
1279static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001280{
1281 return true;
1282}
1283#endif
1284
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001285static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1286{
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001287 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1288 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001289 return true;
1290 return false;
1291}
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001292
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001293static bool ignore_reg_update(struct wiphy *wiphy,
1294 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001295{
Johannes Bergc492db32012-12-06 16:29:25 +01001296 struct regulatory_request *lr = get_last_request();
1297
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02001298 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1299 return true;
1300
Johannes Bergc492db32012-12-06 16:29:25 +01001301 if (!lr) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001302 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1303 "since last_request is not set\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301304 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001305 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301306 }
1307
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001308 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001309 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001310 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1311 "since the driver uses its own custom "
1312 "regulatory domain\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301313 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001314 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301315 }
1316
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001317 /*
1318 * wiphy->regd will be set once the device has its own
1319 * desired regulatory domain set
1320 */
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001321 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -07001322 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Bergc492db32012-12-06 16:29:25 +01001323 !is_world_regdom(lr->alpha2)) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001324 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1325 "since the driver requires its own regulatory "
1326 "domain to be set first\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301327 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001328 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301329 }
1330
Johannes Bergc492db32012-12-06 16:29:25 +01001331 if (reg_request_cell_base(lr))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001332 return reg_dev_ignore_cell_hint(wiphy);
1333
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001334 return false;
1335}
1336
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001337static bool reg_is_world_roaming(struct wiphy *wiphy)
1338{
1339 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1340 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1341 struct regulatory_request *lr = get_last_request();
1342
1343 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1344 return true;
1345
1346 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001347 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001348 return true;
1349
1350 return false;
1351}
1352
Johannes Berg1a919312012-12-03 17:21:11 +01001353static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001354 struct reg_beacon *reg_beacon)
1355{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001356 struct ieee80211_supported_band *sband;
1357 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001358 bool channel_changed = false;
1359 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001360
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001361 sband = wiphy->bands[reg_beacon->chan.band];
1362 chan = &sband->channels[chan_idx];
1363
1364 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1365 return;
1366
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001367 if (chan->beacon_found)
1368 return;
1369
1370 chan->beacon_found = true;
1371
Luis R. Rodriguez0f500a52012-12-19 10:53:04 -08001372 if (!reg_is_world_roaming(wiphy))
1373 return;
1374
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001375 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001376 return;
1377
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001378 chan_before.center_freq = chan->center_freq;
1379 chan_before.flags = chan->flags;
1380
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001381 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1382 chan->flags &= ~IEEE80211_CHAN_NO_IR;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001383 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001384 }
1385
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001386 if (channel_changed)
1387 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001388}
1389
1390/*
1391 * Called when a scan on a wiphy finds a beacon on
1392 * new channel
1393 */
1394static void wiphy_update_new_beacon(struct wiphy *wiphy,
1395 struct reg_beacon *reg_beacon)
1396{
1397 unsigned int i;
1398 struct ieee80211_supported_band *sband;
1399
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001400 if (!wiphy->bands[reg_beacon->chan.band])
1401 return;
1402
1403 sband = wiphy->bands[reg_beacon->chan.band];
1404
1405 for (i = 0; i < sband->n_channels; i++)
1406 handle_reg_beacon(wiphy, i, reg_beacon);
1407}
1408
1409/*
1410 * Called upon reg changes or a new wiphy is added
1411 */
1412static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1413{
1414 unsigned int i;
1415 struct ieee80211_supported_band *sband;
1416 struct reg_beacon *reg_beacon;
1417
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001418 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1419 if (!wiphy->bands[reg_beacon->chan.band])
1420 continue;
1421 sband = wiphy->bands[reg_beacon->chan.band];
1422 for (i = 0; i < sband->n_channels; i++)
1423 handle_reg_beacon(wiphy, i, reg_beacon);
1424 }
1425}
1426
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001427/* Reap the advantages of previously found beacons */
1428static void reg_process_beacons(struct wiphy *wiphy)
1429{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001430 /*
1431 * Means we are just firing up cfg80211, so no beacons would
1432 * have been processed yet.
1433 */
1434 if (!last_request)
1435 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001436 wiphy_update_beacon_reg(wiphy);
1437}
1438
Johannes Berg1a919312012-12-03 17:21:11 +01001439static bool is_ht40_allowed(struct ieee80211_channel *chan)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001440{
1441 if (!chan)
Johannes Berg1a919312012-12-03 17:21:11 +01001442 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001443 if (chan->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg1a919312012-12-03 17:21:11 +01001444 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001445 /* This would happen when regulatory rules disallow HT40 completely */
Felix Fietkau55b183a2013-01-11 14:22:58 +01001446 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1447 return false;
1448 return true;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001449}
1450
1451static void reg_process_ht_flags_channel(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001452 struct ieee80211_channel *channel)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001453{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001454 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001455 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1456 unsigned int i;
1457
Johannes Berg1a919312012-12-03 17:21:11 +01001458 if (!is_ht40_allowed(channel)) {
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001459 channel->flags |= IEEE80211_CHAN_NO_HT40;
1460 return;
1461 }
1462
1463 /*
1464 * We need to ensure the extension channels exist to
1465 * be able to use HT40- or HT40+, this finds them (or not)
1466 */
1467 for (i = 0; i < sband->n_channels; i++) {
1468 struct ieee80211_channel *c = &sband->channels[i];
Johannes Berg1a919312012-12-03 17:21:11 +01001469
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001470 if (c->center_freq == (channel->center_freq - 20))
1471 channel_before = c;
1472 if (c->center_freq == (channel->center_freq + 20))
1473 channel_after = c;
1474 }
1475
1476 /*
1477 * Please note that this assumes target bandwidth is 20 MHz,
1478 * if that ever changes we also need to change the below logic
1479 * to include that as well.
1480 */
Johannes Berg1a919312012-12-03 17:21:11 +01001481 if (!is_ht40_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001482 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001483 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001484 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001485
Johannes Berg1a919312012-12-03 17:21:11 +01001486 if (!is_ht40_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001487 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001488 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001489 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001490}
1491
1492static void reg_process_ht_flags_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001493 struct ieee80211_supported_band *sband)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001494{
1495 unsigned int i;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001496
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001497 if (!sband)
1498 return;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001499
1500 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001501 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001502}
1503
1504static void reg_process_ht_flags(struct wiphy *wiphy)
1505{
1506 enum ieee80211_band band;
1507
1508 if (!wiphy)
1509 return;
1510
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001511 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1512 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001513}
1514
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001515static void reg_call_notifier(struct wiphy *wiphy,
1516 struct regulatory_request *request)
1517{
1518 if (wiphy->reg_notifier)
1519 wiphy->reg_notifier(wiphy, request);
1520}
1521
Arik Nemtsovad932f02014-11-27 09:44:55 +02001522static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev)
1523{
Arik Nemtsovad932f02014-11-27 09:44:55 +02001524 struct cfg80211_chan_def chandef;
1525 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Arik Nemtsov20658702014-12-29 11:59:59 +02001526 enum nl80211_iftype iftype;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001527
1528 wdev_lock(wdev);
Arik Nemtsov20658702014-12-29 11:59:59 +02001529 iftype = wdev->iftype;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001530
Arik Nemtsov20658702014-12-29 11:59:59 +02001531 /* make sure the interface is active */
Arik Nemtsovad932f02014-11-27 09:44:55 +02001532 if (!wdev->netdev || !netif_running(wdev->netdev))
Arik Nemtsov20658702014-12-29 11:59:59 +02001533 goto wdev_inactive_unlock;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001534
Arik Nemtsov20658702014-12-29 11:59:59 +02001535 switch (iftype) {
Arik Nemtsovad932f02014-11-27 09:44:55 +02001536 case NL80211_IFTYPE_AP:
1537 case NL80211_IFTYPE_P2P_GO:
1538 if (!wdev->beacon_interval)
Arik Nemtsov20658702014-12-29 11:59:59 +02001539 goto wdev_inactive_unlock;
1540 chandef = wdev->chandef;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001541 break;
Arik Nemtsov185076d2014-12-03 18:08:17 +02001542 case NL80211_IFTYPE_ADHOC:
1543 if (!wdev->ssid_len)
Arik Nemtsov20658702014-12-29 11:59:59 +02001544 goto wdev_inactive_unlock;
1545 chandef = wdev->chandef;
Arik Nemtsov185076d2014-12-03 18:08:17 +02001546 break;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001547 case NL80211_IFTYPE_STATION:
1548 case NL80211_IFTYPE_P2P_CLIENT:
Arik Nemtsovad932f02014-11-27 09:44:55 +02001549 if (!wdev->current_bss ||
1550 !wdev->current_bss->pub.channel)
Arik Nemtsov20658702014-12-29 11:59:59 +02001551 goto wdev_inactive_unlock;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001552
Arik Nemtsov20658702014-12-29 11:59:59 +02001553 if (!rdev->ops->get_channel ||
1554 rdev_get_channel(rdev, wdev, &chandef))
1555 cfg80211_chandef_create(&chandef,
1556 wdev->current_bss->pub.channel,
1557 NL80211_CHAN_NO_HT);
Arik Nemtsovad932f02014-11-27 09:44:55 +02001558 break;
1559 case NL80211_IFTYPE_MONITOR:
1560 case NL80211_IFTYPE_AP_VLAN:
1561 case NL80211_IFTYPE_P2P_DEVICE:
1562 /* no enforcement required */
1563 break;
1564 default:
1565 /* others not implemented for now */
1566 WARN_ON(1);
1567 break;
1568 }
1569
Arik Nemtsovad932f02014-11-27 09:44:55 +02001570 wdev_unlock(wdev);
Arik Nemtsov20658702014-12-29 11:59:59 +02001571
1572 switch (iftype) {
1573 case NL80211_IFTYPE_AP:
1574 case NL80211_IFTYPE_P2P_GO:
1575 case NL80211_IFTYPE_ADHOC:
1576 return cfg80211_reg_can_beacon(wiphy, &chandef, iftype);
1577 case NL80211_IFTYPE_STATION:
1578 case NL80211_IFTYPE_P2P_CLIENT:
1579 return cfg80211_chandef_usable(wiphy, &chandef,
1580 IEEE80211_CHAN_DISABLED);
1581 default:
1582 break;
1583 }
1584
1585 return true;
1586
1587wdev_inactive_unlock:
1588 wdev_unlock(wdev);
1589 return true;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001590}
1591
1592static void reg_leave_invalid_chans(struct wiphy *wiphy)
1593{
1594 struct wireless_dev *wdev;
1595 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1596
1597 ASSERT_RTNL();
1598
1599 list_for_each_entry(wdev, &rdev->wdev_list, list)
1600 if (!reg_wdev_chan_valid(wiphy, wdev))
1601 cfg80211_leave(rdev, wdev);
1602}
1603
1604static void reg_check_chans_work(struct work_struct *work)
1605{
1606 struct cfg80211_registered_device *rdev;
1607
1608 REG_DBG_PRINT("Verifying active interfaces after reg change\n");
1609 rtnl_lock();
1610
1611 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1612 if (!(rdev->wiphy.regulatory_flags &
1613 REGULATORY_IGNORE_STALE_KICKOFF))
1614 reg_leave_invalid_chans(&rdev->wiphy);
1615
1616 rtnl_unlock();
1617}
1618
1619static void reg_check_channels(void)
1620{
1621 /*
1622 * Give usermode a chance to do something nicer (move to another
1623 * channel, orderly disconnection), before forcing a disconnection.
1624 */
1625 mod_delayed_work(system_power_efficient_wq,
1626 &reg_check_chans,
1627 msecs_to_jiffies(REG_ENFORCE_GRACE_MS));
1628}
1629
Sven Neumanneac03e32011-08-30 23:38:53 +02001630static void wiphy_update_regulatory(struct wiphy *wiphy,
1631 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001632{
1633 enum ieee80211_band band;
Johannes Bergc492db32012-12-06 16:29:25 +01001634 struct regulatory_request *lr = get_last_request();
Sven Neumanneac03e32011-08-30 23:38:53 +02001635
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001636 if (ignore_reg_update(wiphy, initiator)) {
1637 /*
1638 * Regulatory updates set by CORE are ignored for custom
1639 * regulatory cards. Let us notify the changes to the driver,
1640 * as some drivers used this to restore its orig_* reg domain.
1641 */
1642 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001643 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001644 reg_call_notifier(wiphy, lr);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001645 return;
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001646 }
Sven Neumanna203c2a2011-07-12 15:52:07 +02001647
Johannes Bergc492db32012-12-06 16:29:25 +01001648 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07001649
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001650 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1651 handle_band(wiphy, initiator, wiphy->bands[band]);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001652
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001653 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001654 reg_process_ht_flags(wiphy);
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001655 reg_call_notifier(wiphy, lr);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001656}
1657
Sven Neumannd7549cb2011-08-30 23:38:54 +02001658static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1659{
1660 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301661 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02001662
Johannes Berg5fe231e2013-05-08 21:45:15 +02001663 ASSERT_RTNL();
Johannes Berg458f4f92012-12-06 15:47:38 +01001664
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301665 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1666 wiphy = &rdev->wiphy;
1667 wiphy_update_regulatory(wiphy, initiator);
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301668 }
Arik Nemtsovad932f02014-11-27 09:44:55 +02001669
1670 reg_check_channels();
Sven Neumannd7549cb2011-08-30 23:38:54 +02001671}
1672
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001673static void handle_channel_custom(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001674 struct ieee80211_channel *chan,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001675 const struct ieee80211_regdomain *regd)
1676{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001677 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001678 const struct ieee80211_reg_rule *reg_rule = NULL;
1679 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001680 const struct ieee80211_freq_range *freq_range = NULL;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001681 u32 max_bandwidth_khz;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001682
Johannes Berg361c9c82012-12-06 15:57:14 +01001683 reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1684 regd);
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001685
Johannes Berg361c9c82012-12-06 15:57:14 +01001686 if (IS_ERR(reg_rule)) {
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001687 REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1688 chan->center_freq);
Arik Nemtsovdb8dfee2014-12-15 19:26:02 +02001689 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
1690 chan->flags |= IEEE80211_CHAN_DISABLED;
1691 } else {
1692 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1693 chan->flags = chan->orig_flags;
1694 }
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001695 return;
1696 }
1697
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001698 chan_reg_rule_print_dbg(regd, chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301699
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001700 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001701 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001702
Janusz Dziedzic97524822014-01-30 09:52:20 +01001703 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1704 /* Check if auto calculation requested */
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001705 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001706 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1707
1708 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
Johannes Berge33e2242014-06-23 11:06:16 +02001709 bw_flags = IEEE80211_CHAN_NO_HT40;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001710 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001711 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001712 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001713 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001714
Arik Nemtsov2e18b382014-11-16 16:37:46 +02001715 chan->dfs_state_entered = jiffies;
Arik Nemtsovc7ab5082014-11-16 16:37:47 +02001716 chan->dfs_state = NL80211_DFS_USABLE;
1717
1718 chan->beacon_found = false;
Arik Nemtsovdb8dfee2014-12-15 19:26:02 +02001719
1720 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1721 chan->flags = chan->orig_flags | bw_flags |
1722 map_regdom_flags(reg_rule->flags);
1723 else
1724 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1725
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001726 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001727 chan->max_reg_power = chan->max_power =
1728 (int) MBM_TO_DBM(power_rule->max_eirp);
Arik Nemtsov2e18b382014-11-16 16:37:46 +02001729
1730 if (chan->flags & IEEE80211_CHAN_RADAR) {
1731 if (reg_rule->dfs_cac_ms)
1732 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1733 else
1734 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1735 }
1736
1737 chan->max_power = chan->max_reg_power;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001738}
1739
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001740static void handle_band_custom(struct wiphy *wiphy,
1741 struct ieee80211_supported_band *sband,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001742 const struct ieee80211_regdomain *regd)
1743{
1744 unsigned int i;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001745
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001746 if (!sband)
1747 return;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001748
1749 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001750 handle_channel_custom(wiphy, &sband->channels[i], regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001751}
1752
1753/* Used by drivers prior to wiphy registration */
1754void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1755 const struct ieee80211_regdomain *regd)
1756{
1757 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001758 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001759
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001760 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
1761 "wiphy should have REGULATORY_CUSTOM_REG\n");
1762 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Luis R. Rodriguez222ea582013-11-05 09:18:00 -08001763
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001764 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001765 if (!wiphy->bands[band])
1766 continue;
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001767 handle_band_custom(wiphy, wiphy->bands[band], regd);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001768 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001769 }
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001770
1771 /*
1772 * no point in calling this if it won't have any effect
Johannes Berg1a919312012-12-03 17:21:11 +01001773 * on your device's supported bands.
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001774 */
1775 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001776}
1777EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1778
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001779static void reg_set_request_processed(void)
1780{
1781 bool need_more_processing = false;
Johannes Bergc492db32012-12-06 16:29:25 +01001782 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001783
Johannes Bergc492db32012-12-06 16:29:25 +01001784 lr->processed = true;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001785
1786 spin_lock(&reg_requests_lock);
1787 if (!list_empty(&reg_requests_list))
1788 need_more_processing = true;
1789 spin_unlock(&reg_requests_lock);
1790
Johannes Bergc492db32012-12-06 16:29:25 +01001791 if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
Eliad Pellerfe20b392012-06-12 12:53:13 +03001792 cancel_delayed_work(&reg_timeout);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001793
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001794 if (need_more_processing)
1795 schedule_work(&reg_work);
1796}
1797
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001798/**
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001799 * reg_process_hint_core - process core regulatory requests
1800 * @pending_request: a pending core regulatory request
1801 *
1802 * The wireless subsystem can use this function to process
1803 * a regulatory request issued by the regulatory core.
1804 *
1805 * Returns one of the different reg request treatment values.
1806 */
1807static enum reg_request_treatment
1808reg_process_hint_core(struct regulatory_request *core_request)
1809{
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001810
1811 core_request->intersect = false;
1812 core_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001813
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001814 reg_update_last_request(core_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001815
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001816 return reg_call_crda(core_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001817}
1818
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001819static enum reg_request_treatment
1820__reg_process_hint_user(struct regulatory_request *user_request)
1821{
1822 struct regulatory_request *lr = get_last_request();
1823
1824 if (reg_request_cell_base(user_request))
1825 return reg_ignore_cell_hint(user_request);
1826
1827 if (reg_request_cell_base(lr))
1828 return REG_REQ_IGNORE;
1829
1830 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1831 return REG_REQ_INTERSECT;
1832 /*
1833 * If the user knows better the user should set the regdom
1834 * to their country before the IE is picked up
1835 */
1836 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
1837 lr->intersect)
1838 return REG_REQ_IGNORE;
1839 /*
1840 * Process user requests only after previous user/driver/core
1841 * requests have been processed
1842 */
1843 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
1844 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1845 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
1846 regdom_changes(lr->alpha2))
1847 return REG_REQ_IGNORE;
1848
1849 if (!regdom_changes(user_request->alpha2))
1850 return REG_REQ_ALREADY_SET;
1851
1852 return REG_REQ_OK;
1853}
1854
1855/**
1856 * reg_process_hint_user - process user regulatory requests
1857 * @user_request: a pending user regulatory request
1858 *
1859 * The wireless subsystem can use this function to process
1860 * a regulatory request initiated by userspace.
1861 *
1862 * Returns one of the different reg request treatment values.
1863 */
1864static enum reg_request_treatment
1865reg_process_hint_user(struct regulatory_request *user_request)
1866{
1867 enum reg_request_treatment treatment;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001868
1869 treatment = __reg_process_hint_user(user_request);
1870 if (treatment == REG_REQ_IGNORE ||
Ilan peer0c4ddcd2015-03-04 00:32:05 -05001871 treatment == REG_REQ_ALREADY_SET) {
Arik Nemtsovc8883932014-04-21 20:39:34 -07001872 reg_free_request(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001873 return treatment;
1874 }
1875
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001876 user_request->intersect = treatment == REG_REQ_INTERSECT;
1877 user_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001878
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001879 reg_update_last_request(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001880
1881 user_alpha2[0] = user_request->alpha2[0];
1882 user_alpha2[1] = user_request->alpha2[1];
1883
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001884 return reg_call_crda(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001885}
1886
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001887static enum reg_request_treatment
1888__reg_process_hint_driver(struct regulatory_request *driver_request)
1889{
1890 struct regulatory_request *lr = get_last_request();
1891
1892 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
1893 if (regdom_changes(driver_request->alpha2))
1894 return REG_REQ_OK;
1895 return REG_REQ_ALREADY_SET;
1896 }
1897
1898 /*
1899 * This would happen if you unplug and plug your card
1900 * back in or if you add a new device for which the previously
1901 * loaded card also agrees on the regulatory domain.
1902 */
1903 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1904 !regdom_changes(driver_request->alpha2))
1905 return REG_REQ_ALREADY_SET;
1906
1907 return REG_REQ_INTERSECT;
1908}
1909
1910/**
1911 * reg_process_hint_driver - process driver regulatory requests
1912 * @driver_request: a pending driver regulatory request
1913 *
1914 * The wireless subsystem can use this function to process
1915 * a regulatory request issued by an 802.11 driver.
1916 *
1917 * Returns one of the different reg request treatment values.
1918 */
1919static enum reg_request_treatment
1920reg_process_hint_driver(struct wiphy *wiphy,
1921 struct regulatory_request *driver_request)
1922{
Arik Nemtsov34f05f52014-12-04 12:22:16 +02001923 const struct ieee80211_regdomain *regd, *tmp;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001924 enum reg_request_treatment treatment;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001925
1926 treatment = __reg_process_hint_driver(driver_request);
1927
1928 switch (treatment) {
1929 case REG_REQ_OK:
1930 break;
1931 case REG_REQ_IGNORE:
Arik Nemtsovc8883932014-04-21 20:39:34 -07001932 reg_free_request(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001933 return treatment;
1934 case REG_REQ_INTERSECT:
1935 /* fall through */
1936 case REG_REQ_ALREADY_SET:
1937 regd = reg_copy_regd(get_cfg80211_regdom());
1938 if (IS_ERR(regd)) {
Arik Nemtsovc8883932014-04-21 20:39:34 -07001939 reg_free_request(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001940 return REG_REQ_IGNORE;
1941 }
Arik Nemtsov34f05f52014-12-04 12:22:16 +02001942
1943 tmp = get_wiphy_regdom(wiphy);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001944 rcu_assign_pointer(wiphy->regd, regd);
Arik Nemtsov34f05f52014-12-04 12:22:16 +02001945 rcu_free_regdom(tmp);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001946 }
1947
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001948
1949 driver_request->intersect = treatment == REG_REQ_INTERSECT;
1950 driver_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001951
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001952 reg_update_last_request(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001953
1954 /*
1955 * Since CRDA will not be called in this case as we already
1956 * have applied the requested regulatory domain before we just
1957 * inform userspace we have processed the request
1958 */
1959 if (treatment == REG_REQ_ALREADY_SET) {
1960 nl80211_send_reg_change_event(driver_request);
1961 reg_set_request_processed();
1962 return treatment;
1963 }
1964
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001965 return reg_call_crda(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001966}
1967
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001968static enum reg_request_treatment
1969__reg_process_hint_country_ie(struct wiphy *wiphy,
1970 struct regulatory_request *country_ie_request)
1971{
1972 struct wiphy *last_wiphy = NULL;
1973 struct regulatory_request *lr = get_last_request();
1974
1975 if (reg_request_cell_base(lr)) {
1976 /* Trust a Cell base station over the AP's country IE */
1977 if (regdom_changes(country_ie_request->alpha2))
1978 return REG_REQ_IGNORE;
1979 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez2a901462013-11-11 22:15:31 +01001980 } else {
1981 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
1982 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001983 }
1984
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001985 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
1986 return -EINVAL;
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001987
1988 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
1989 return REG_REQ_OK;
1990
1991 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
1992
1993 if (last_wiphy != wiphy) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001994 /*
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001995 * Two cards with two APs claiming different
1996 * Country IE alpha2s. We could
1997 * intersect them, but that seems unlikely
1998 * to be correct. Reject second one for now.
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001999 */
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002000 if (regdom_changes(country_ie_request->alpha2))
2001 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002002 return REG_REQ_ALREADY_SET;
2003 }
Emmanuel Grumbach70dcec52014-12-02 09:53:25 +02002004
2005 if (regdom_changes(country_ie_request->alpha2))
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002006 return REG_REQ_OK;
2007 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002008}
2009
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002010/**
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002011 * reg_process_hint_country_ie - process regulatory requests from country IEs
2012 * @country_ie_request: a regulatory request from a country IE
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002013 *
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002014 * The wireless subsystem can use this function to process
2015 * a regulatory request issued by a country Information Element.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002016 *
Johannes Berg2f922122012-12-03 17:54:55 +01002017 * Returns one of the different reg request treatment values.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002018 */
Johannes Berg2f922122012-12-03 17:54:55 +01002019static enum reg_request_treatment
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002020reg_process_hint_country_ie(struct wiphy *wiphy,
2021 struct regulatory_request *country_ie_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002022{
Johannes Berg2f922122012-12-03 17:54:55 +01002023 enum reg_request_treatment treatment;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002024
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002025 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002026
Johannes Berg2f922122012-12-03 17:54:55 +01002027 switch (treatment) {
Johannes Berg2f922122012-12-03 17:54:55 +01002028 case REG_REQ_OK:
2029 break;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002030 case REG_REQ_IGNORE:
2031 /* fall through */
2032 case REG_REQ_ALREADY_SET:
Arik Nemtsovc8883932014-04-21 20:39:34 -07002033 reg_free_request(country_ie_request);
Johannes Berg2f922122012-12-03 17:54:55 +01002034 return treatment;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002035 case REG_REQ_INTERSECT:
Arik Nemtsovc8883932014-04-21 20:39:34 -07002036 reg_free_request(country_ie_request);
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002037 /*
2038 * This doesn't happen yet, not sure we
2039 * ever want to support it for this case.
2040 */
2041 WARN_ONCE(1, "Unexpected intersection for country IEs");
2042 return REG_REQ_IGNORE;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002043 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002044
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002045 country_ie_request->intersect = false;
2046 country_ie_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08002047
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08002048 reg_update_last_request(country_ie_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05002049
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08002050 return reg_call_crda(country_ie_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002051}
2052
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04002053/* This processes *all* regulatory hints */
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08002054static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002055{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002056 struct wiphy *wiphy = NULL;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002057 enum reg_request_treatment treatment;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002058
Johannes Bergf4173762012-12-03 18:23:37 +01002059 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002060 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
2061
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002062 switch (reg_request->initiator) {
2063 case NL80211_REGDOM_SET_BY_CORE:
2064 reg_process_hint_core(reg_request);
2065 return;
2066 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002067 treatment = reg_process_hint_user(reg_request);
Inbal Hacohen50c11eb2014-02-12 09:32:27 +02002068 if (treatment == REG_REQ_IGNORE ||
Ilan peer0c4ddcd2015-03-04 00:32:05 -05002069 treatment == REG_REQ_ALREADY_SET)
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002070 return;
Shaibal Dutta845f3352014-01-30 15:08:30 -08002071 queue_delayed_work(system_power_efficient_wq,
2072 &reg_timeout, msecs_to_jiffies(3142));
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002073 return;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002074 case NL80211_REGDOM_SET_BY_DRIVER:
Ilan Peer772f0382014-01-14 15:17:23 +02002075 if (!wiphy)
2076 goto out_free;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002077 treatment = reg_process_hint_driver(wiphy, reg_request);
2078 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002079 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Ilan Peer772f0382014-01-14 15:17:23 +02002080 if (!wiphy)
2081 goto out_free;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002082 treatment = reg_process_hint_country_ie(wiphy, reg_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002083 break;
2084 default:
2085 WARN(1, "invalid initiator %d\n", reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02002086 goto out_free;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002087 }
2088
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002089 /* This is required so that the orig_* parameters are saved */
2090 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
Arik Nemtsovad932f02014-11-27 09:44:55 +02002091 wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002092 wiphy_update_regulatory(wiphy, reg_request->initiator);
Arik Nemtsovad932f02014-11-27 09:44:55 +02002093 reg_check_channels();
2094 }
Ilan Peer772f0382014-01-14 15:17:23 +02002095
2096 return;
2097
2098out_free:
Arik Nemtsovc8883932014-04-21 20:39:34 -07002099 reg_free_request(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002100}
2101
Arik Nemtsovef51fb12015-01-07 16:47:20 +02002102static bool reg_only_self_managed_wiphys(void)
2103{
2104 struct cfg80211_registered_device *rdev;
2105 struct wiphy *wiphy;
2106 bool self_managed_found = false;
2107
2108 ASSERT_RTNL();
2109
2110 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2111 wiphy = &rdev->wiphy;
2112 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2113 self_managed_found = true;
2114 else
2115 return false;
2116 }
2117
2118 /* make sure at least one self-managed wiphy exists */
2119 return self_managed_found;
2120}
2121
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002122/*
2123 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
2124 * Regulatory hints come on a first come first serve basis and we
2125 * must process each one atomically.
2126 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002127static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002128{
Johannes Bergc492db32012-12-06 16:29:25 +01002129 struct regulatory_request *reg_request, *lr;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002130
Johannes Bergc492db32012-12-06 16:29:25 +01002131 lr = get_last_request();
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002132
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002133 /* When last_request->processed becomes true this will be rescheduled */
Johannes Bergc492db32012-12-06 16:29:25 +01002134 if (lr && !lr->processed) {
Luis R. Rodriguez96cce122014-04-21 20:39:35 -07002135 reg_process_hint(lr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002136 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002137 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002138
2139 spin_lock(&reg_requests_lock);
2140
2141 if (list_empty(&reg_requests_list)) {
2142 spin_unlock(&reg_requests_lock);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002143 return;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002144 }
2145
2146 reg_request = list_first_entry(&reg_requests_list,
2147 struct regulatory_request,
2148 list);
2149 list_del_init(&reg_request->list);
2150
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002151 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002152
Arik Nemtsovef51fb12015-01-07 16:47:20 +02002153 if (reg_only_self_managed_wiphys()) {
2154 reg_free_request(reg_request);
2155 return;
2156 }
2157
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08002158 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002159}
2160
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002161/* Processes beacon hints -- this has nothing to do with country IEs */
2162static void reg_process_pending_beacon_hints(void)
2163{
Johannes Berg79c97e92009-07-07 03:56:12 +02002164 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002165 struct reg_beacon *pending_beacon, *tmp;
2166
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002167 /* This goes through the _pending_ beacon list */
2168 spin_lock_bh(&reg_pending_beacons_lock);
2169
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002170 list_for_each_entry_safe(pending_beacon, tmp,
2171 &reg_pending_beacons, list) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002172 list_del_init(&pending_beacon->list);
2173
2174 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02002175 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2176 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002177
2178 /* Remembers the beacon hint for new wiphys or reg changes */
2179 list_add_tail(&pending_beacon->list, &reg_beacon_list);
2180 }
2181
2182 spin_unlock_bh(&reg_pending_beacons_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002183}
2184
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002185static void reg_process_self_managed_hints(void)
2186{
2187 struct cfg80211_registered_device *rdev;
2188 struct wiphy *wiphy;
2189 const struct ieee80211_regdomain *tmp;
2190 const struct ieee80211_regdomain *regd;
2191 enum ieee80211_band band;
2192 struct regulatory_request request = {};
2193
2194 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2195 wiphy = &rdev->wiphy;
2196
2197 spin_lock(&reg_requests_lock);
2198 regd = rdev->requested_regd;
2199 rdev->requested_regd = NULL;
2200 spin_unlock(&reg_requests_lock);
2201
2202 if (regd == NULL)
2203 continue;
2204
2205 tmp = get_wiphy_regdom(wiphy);
2206 rcu_assign_pointer(wiphy->regd, regd);
2207 rcu_free_regdom(tmp);
2208
2209 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2210 handle_band_custom(wiphy, wiphy->bands[band], regd);
2211
2212 reg_process_ht_flags(wiphy);
2213
2214 request.wiphy_idx = get_wiphy_idx(wiphy);
2215 request.alpha2[0] = regd->alpha2[0];
2216 request.alpha2[1] = regd->alpha2[1];
2217 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
2218
2219 nl80211_send_wiphy_reg_change_event(&request);
2220 }
2221
2222 reg_check_channels();
2223}
2224
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002225static void reg_todo(struct work_struct *work)
2226{
Johannes Berg5fe231e2013-05-08 21:45:15 +02002227 rtnl_lock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002228 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002229 reg_process_pending_beacon_hints();
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002230 reg_process_self_managed_hints();
Johannes Berg5fe231e2013-05-08 21:45:15 +02002231 rtnl_unlock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002232}
2233
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002234static void queue_regulatory_request(struct regulatory_request *request)
2235{
Johannes Bergd4f2c882012-12-03 18:59:58 +01002236 request->alpha2[0] = toupper(request->alpha2[0]);
2237 request->alpha2[1] = toupper(request->alpha2[1]);
John W. Linvillec61029c2010-08-05 14:26:24 -04002238
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002239 spin_lock(&reg_requests_lock);
2240 list_add_tail(&request->list, &reg_requests_list);
2241 spin_unlock(&reg_requests_lock);
2242
2243 schedule_work(&reg_work);
2244}
2245
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002246/*
2247 * Core regulatory hint -- happens during cfg80211_init()
2248 * and when we restore regulatory settings.
2249 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002250static int regulatory_hint_core(const char *alpha2)
2251{
2252 struct regulatory_request *request;
2253
Johannes Berg1a919312012-12-03 17:21:11 +01002254 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002255 if (!request)
2256 return -ENOMEM;
2257
2258 request->alpha2[0] = alpha2[0];
2259 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002260 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002261
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08002262 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04002263
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002264 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002265}
2266
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002267/* User hints */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002268int regulatory_hint_user(const char *alpha2,
2269 enum nl80211_user_reg_hint_type user_reg_hint_type)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002270{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002271 struct regulatory_request *request;
2272
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002273 if (WARN_ON(!alpha2))
2274 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002275
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002276 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2277 if (!request)
2278 return -ENOMEM;
2279
Johannes Bergf4173762012-12-03 18:23:37 +01002280 request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002281 request->alpha2[0] = alpha2[0];
2282 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05002283 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002284 request->user_reg_hint_type = user_reg_hint_type;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002285
2286 queue_regulatory_request(request);
2287
2288 return 0;
2289}
2290
Ilan Peer52616f22014-02-25 16:26:00 +02002291int regulatory_hint_indoor_user(void)
2292{
Ilan Peer52616f22014-02-25 16:26:00 +02002293
Ilan Peer52616f22014-02-25 16:26:00 +02002294
Ilan peer0c4ddcd2015-03-04 00:32:05 -05002295 reg_is_indoor = true;
Ilan Peer52616f22014-02-25 16:26:00 +02002296
2297 return 0;
2298}
2299
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002300/* Driver hints */
2301int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2302{
2303 struct regulatory_request *request;
2304
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002305 if (WARN_ON(!alpha2 || !wiphy))
2306 return -EINVAL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002307
Luis R. Rodriguez4f7b9142013-12-14 20:09:06 +01002308 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
2309
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002310 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2311 if (!request)
2312 return -ENOMEM;
2313
2314 request->wiphy_idx = get_wiphy_idx(wiphy);
2315
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002316 request->alpha2[0] = alpha2[0];
2317 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002318 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002319
2320 queue_regulatory_request(request);
2321
2322 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002323}
2324EXPORT_SYMBOL(regulatory_hint);
2325
Luis R. Rodriguez789fd032013-10-04 18:07:24 -07002326void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
2327 const u8 *country_ie, u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002328{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002329 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002330 enum environment_cap env = ENVIRON_ANY;
Johannes Bergdb2424c2013-05-10 19:07:52 +02002331 struct regulatory_request *request = NULL, *lr;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002332
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002333 /* IE len must be evenly divisible by 2 */
2334 if (country_ie_len & 0x01)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002335 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002336
2337 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002338 return;
2339
2340 request = kzalloc(sizeof(*request), GFP_KERNEL);
2341 if (!request)
2342 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002343
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002344 alpha2[0] = country_ie[0];
2345 alpha2[1] = country_ie[1];
2346
2347 if (country_ie[2] == 'I')
2348 env = ENVIRON_INDOOR;
2349 else if (country_ie[2] == 'O')
2350 env = ENVIRON_OUTDOOR;
2351
Johannes Bergdb2424c2013-05-10 19:07:52 +02002352 rcu_read_lock();
2353 lr = get_last_request();
2354
2355 if (unlikely(!lr))
2356 goto out;
2357
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002358 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002359 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002360 * We leave conflict resolution to the workqueue, where can hold
Johannes Berg5fe231e2013-05-08 21:45:15 +02002361 * the RTNL.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002362 */
Johannes Bergc492db32012-12-06 16:29:25 +01002363 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2364 lr->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002365 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002366
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002367 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04002368 request->alpha2[0] = alpha2[0];
2369 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002370 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002371 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002372
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002373 queue_regulatory_request(request);
Johannes Bergdb2424c2013-05-10 19:07:52 +02002374 request = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002375out:
Johannes Bergdb2424c2013-05-10 19:07:52 +02002376 kfree(request);
2377 rcu_read_unlock();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002378}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002379
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002380static void restore_alpha2(char *alpha2, bool reset_user)
2381{
2382 /* indicates there is no alpha2 to consider for restoration */
2383 alpha2[0] = '9';
2384 alpha2[1] = '7';
2385
2386 /* The user setting has precedence over the module parameter */
2387 if (is_user_regdom_saved()) {
2388 /* Unless we're asked to ignore it and reset it */
2389 if (reset_user) {
Johannes Berg1a919312012-12-03 17:21:11 +01002390 REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002391 user_alpha2[0] = '9';
2392 user_alpha2[1] = '7';
2393
2394 /*
2395 * If we're ignoring user settings, we still need to
2396 * check the module parameter to ensure we put things
2397 * back as they were for a full restore.
2398 */
2399 if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01002400 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2401 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002402 alpha2[0] = ieee80211_regdom[0];
2403 alpha2[1] = ieee80211_regdom[1];
2404 }
2405 } else {
Johannes Berg1a919312012-12-03 17:21:11 +01002406 REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
2407 user_alpha2[0], user_alpha2[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002408 alpha2[0] = user_alpha2[0];
2409 alpha2[1] = user_alpha2[1];
2410 }
2411 } else if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01002412 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2413 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002414 alpha2[0] = ieee80211_regdom[0];
2415 alpha2[1] = ieee80211_regdom[1];
2416 } else
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07002417 REG_DBG_PRINT("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002418}
2419
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302420static void restore_custom_reg_settings(struct wiphy *wiphy)
2421{
2422 struct ieee80211_supported_band *sband;
2423 enum ieee80211_band band;
2424 struct ieee80211_channel *chan;
2425 int i;
2426
2427 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2428 sband = wiphy->bands[band];
2429 if (!sband)
2430 continue;
2431 for (i = 0; i < sband->n_channels; i++) {
2432 chan = &sband->channels[i];
2433 chan->flags = chan->orig_flags;
2434 chan->max_antenna_gain = chan->orig_mag;
2435 chan->max_power = chan->orig_mpwr;
Paul Stewart899852a2012-08-01 16:54:42 -07002436 chan->beacon_found = false;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302437 }
2438 }
2439}
2440
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002441/*
2442 * Restoring regulatory settings involves ingoring any
2443 * possibly stale country IE information and user regulatory
2444 * settings if so desired, this includes any beacon hints
2445 * learned as we could have traveled outside to another country
2446 * after disconnection. To restore regulatory settings we do
2447 * exactly what we did at bootup:
2448 *
2449 * - send a core regulatory hint
2450 * - send a user regulatory hint if applicable
2451 *
2452 * Device drivers that send a regulatory hint for a specific country
2453 * keep their own regulatory domain on wiphy->regd so that does does
2454 * not need to be remembered.
2455 */
2456static void restore_regulatory_settings(bool reset_user)
2457{
2458 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002459 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002460 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002461 struct regulatory_request *reg_request, *tmp;
2462 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302463 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002464
Johannes Berg5fe231e2013-05-08 21:45:15 +02002465 ASSERT_RTNL();
2466
Ilan Peer52616f22014-02-25 16:26:00 +02002467 reg_is_indoor = false;
2468
Johannes Berg2d319862013-01-09 12:01:38 +01002469 reset_regdomains(true, &world_regdom);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002470 restore_alpha2(alpha2, reset_user);
2471
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002472 /*
2473 * If there's any pending requests we simply
2474 * stash them to a temporary pending queue and
2475 * add then after we've restored regulatory
2476 * settings.
2477 */
2478 spin_lock(&reg_requests_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002479 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2480 if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
2481 continue;
2482 list_move_tail(&reg_request->list, &tmp_reg_req_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002483 }
2484 spin_unlock(&reg_requests_lock);
2485
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002486 /* Clear beacon hints */
2487 spin_lock_bh(&reg_pending_beacons_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002488 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2489 list_del(&reg_beacon->list);
2490 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002491 }
2492 spin_unlock_bh(&reg_pending_beacons_lock);
2493
Johannes Bergfea9bce2012-12-03 17:32:01 +01002494 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2495 list_del(&reg_beacon->list);
2496 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002497 }
2498
2499 /* First restore to the basic regulatory settings */
Johannes Berg379b82f2012-12-06 15:44:07 +01002500 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
2501 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002502
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302503 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002504 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2505 continue;
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01002506 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302507 restore_custom_reg_settings(&rdev->wiphy);
2508 }
2509
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002510 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002511
2512 /*
2513 * This restores the ieee80211_regdom module parameter
2514 * preference or the last user requested regulatory
2515 * settings, user regulatory settings takes precedence.
2516 */
2517 if (is_an_alpha2(alpha2))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002518 regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002519
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002520 spin_lock(&reg_requests_lock);
Johannes Berg11cff962012-12-03 18:56:41 +01002521 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002522 spin_unlock(&reg_requests_lock);
2523
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002524 REG_DBG_PRINT("Kicking the queue\n");
2525
2526 schedule_work(&reg_work);
2527}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002528
2529void regulatory_hint_disconnect(void)
2530{
Johannes Berg1a919312012-12-03 17:21:11 +01002531 REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002532 restore_regulatory_settings(false);
2533}
2534
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002535static bool freq_is_chan_12_13_14(u16 freq)
2536{
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002537 if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
2538 freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
2539 freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002540 return true;
2541 return false;
2542}
2543
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002544static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
2545{
2546 struct reg_beacon *pending_beacon;
2547
2548 list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
2549 if (beacon_chan->center_freq ==
2550 pending_beacon->chan.center_freq)
2551 return true;
2552 return false;
2553}
2554
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002555int regulatory_hint_found_beacon(struct wiphy *wiphy,
2556 struct ieee80211_channel *beacon_chan,
2557 gfp_t gfp)
2558{
2559 struct reg_beacon *reg_beacon;
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002560 bool processing;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002561
Johannes Berg1a919312012-12-03 17:21:11 +01002562 if (beacon_chan->beacon_found ||
2563 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002564 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg1a919312012-12-03 17:21:11 +01002565 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002566 return 0;
2567
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002568 spin_lock_bh(&reg_pending_beacons_lock);
2569 processing = pending_reg_beacon(beacon_chan);
2570 spin_unlock_bh(&reg_pending_beacons_lock);
2571
2572 if (processing)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002573 return 0;
2574
2575 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2576 if (!reg_beacon)
2577 return -ENOMEM;
2578
Johannes Berg1a919312012-12-03 17:21:11 +01002579 REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002580 beacon_chan->center_freq,
2581 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2582 wiphy_name(wiphy));
2583
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002584 memcpy(&reg_beacon->chan, beacon_chan,
Johannes Berg1a919312012-12-03 17:21:11 +01002585 sizeof(struct ieee80211_channel));
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002586
2587 /*
2588 * Since we can be called from BH or and non-BH context
2589 * we must use spin_lock_bh()
2590 */
2591 spin_lock_bh(&reg_pending_beacons_lock);
2592 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2593 spin_unlock_bh(&reg_pending_beacons_lock);
2594
2595 schedule_work(&reg_work);
2596
2597 return 0;
2598}
2599
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002600static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002601{
2602 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002603 const struct ieee80211_reg_rule *reg_rule = NULL;
2604 const struct ieee80211_freq_range *freq_range = NULL;
2605 const struct ieee80211_power_rule *power_rule = NULL;
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002606 char bw[32], cac_time[32];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002607
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002608 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002609
2610 for (i = 0; i < rd->n_reg_rules; i++) {
2611 reg_rule = &rd->reg_rules[i];
2612 freq_range = &reg_rule->freq_range;
2613 power_rule = &reg_rule->power_rule;
2614
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002615 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
2616 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
2617 freq_range->max_bandwidth_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002618 reg_get_max_bandwidth(rd, reg_rule));
2619 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002620 snprintf(bw, sizeof(bw), "%d KHz",
Janusz Dziedzic97524822014-01-30 09:52:20 +01002621 freq_range->max_bandwidth_khz);
2622
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002623 if (reg_rule->flags & NL80211_RRF_DFS)
2624 scnprintf(cac_time, sizeof(cac_time), "%u s",
2625 reg_rule->dfs_cac_ms/1000);
2626 else
2627 scnprintf(cac_time, sizeof(cac_time), "N/A");
2628
2629
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002630 /*
2631 * There may not be documentation for max antenna gain
2632 * in certain regions
2633 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002634 if (power_rule->max_antenna_gain)
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002635 pr_info(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002636 freq_range->start_freq_khz,
2637 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002638 bw,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002639 power_rule->max_antenna_gain,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002640 power_rule->max_eirp,
2641 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002642 else
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002643 pr_info(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002644 freq_range->start_freq_khz,
2645 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002646 bw,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002647 power_rule->max_eirp,
2648 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002649 }
2650}
2651
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01002652bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002653{
2654 switch (dfs_region) {
2655 case NL80211_DFS_UNSET:
2656 case NL80211_DFS_FCC:
2657 case NL80211_DFS_ETSI:
2658 case NL80211_DFS_JP:
2659 return true;
2660 default:
2661 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
2662 dfs_region);
2663 return false;
2664 }
2665}
2666
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002667static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002668{
Johannes Bergc492db32012-12-06 16:29:25 +01002669 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002670
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002671 if (is_intersected_alpha2(rd->alpha2)) {
Johannes Bergc492db32012-12-06 16:29:25 +01002672 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002673 struct cfg80211_registered_device *rdev;
Johannes Bergc492db32012-12-06 16:29:25 +01002674 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002675 if (rdev) {
Joe Perchese9c02682010-11-16 19:56:49 -08002676 pr_info("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002677 rdev->country_ie_alpha2[0],
2678 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002679 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002680 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002681 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002682 pr_info("Current regulatory domain intersected:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002683 } else if (is_world_regdom(rd->alpha2)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002684 pr_info("World regulatory domain updated:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002685 } else {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002686 if (is_unknown_alpha2(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08002687 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002688 else {
Johannes Bergc492db32012-12-06 16:29:25 +01002689 if (reg_request_cell_base(lr))
Johannes Berg1a919312012-12-03 17:21:11 +01002690 pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002691 rd->alpha2[0], rd->alpha2[1]);
2692 else
Johannes Berg1a919312012-12-03 17:21:11 +01002693 pr_info("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002694 rd->alpha2[0], rd->alpha2[1]);
2695 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002696 }
Johannes Berg1a919312012-12-03 17:21:11 +01002697
Luis R. Rodriguez3ef121b2013-11-13 18:54:05 +01002698 pr_info(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002699 print_rd_rules(rd);
2700}
2701
Johannes Berg2df78162008-10-28 16:49:41 +01002702static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002703{
Joe Perchese9c02682010-11-16 19:56:49 -08002704 pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002705 print_rd_rules(rd);
2706}
2707
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002708static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
2709{
2710 if (!is_world_regdom(rd->alpha2))
2711 return -EINVAL;
2712 update_world_regdomain(rd);
2713 return 0;
2714}
2715
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002716static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
2717 struct regulatory_request *user_request)
2718{
2719 const struct ieee80211_regdomain *intersected_rd = NULL;
2720
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002721 if (!regdom_changes(rd->alpha2))
2722 return -EALREADY;
2723
2724 if (!is_valid_rd(rd)) {
2725 pr_err("Invalid regulatory domain detected:\n");
2726 print_regdomain_info(rd);
2727 return -EINVAL;
2728 }
2729
2730 if (!user_request->intersect) {
2731 reset_regdomains(false, rd);
2732 return 0;
2733 }
2734
2735 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2736 if (!intersected_rd)
2737 return -EINVAL;
2738
2739 kfree(rd);
2740 rd = NULL;
2741 reset_regdomains(false, intersected_rd);
2742
2743 return 0;
2744}
2745
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002746static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
2747 struct regulatory_request *driver_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002748{
Johannes Berge9763c32012-12-03 16:59:46 +01002749 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002750 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002751 const struct ieee80211_regdomain *tmp;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002752 struct wiphy *request_wiphy;
Johannes Berg6913b492012-12-04 00:48:59 +01002753
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002754 if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002755 return -EINVAL;
2756
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002757 if (!regdom_changes(rd->alpha2))
2758 return -EALREADY;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002759
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002760 if (!is_valid_rd(rd)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002761 pr_err("Invalid regulatory domain detected:\n");
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002762 print_regdomain_info(rd);
2763 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002764 }
2765
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002766 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
2767 if (!request_wiphy) {
Shaibal Dutta845f3352014-01-30 15:08:30 -08002768 queue_delayed_work(system_power_efficient_wq,
2769 &reg_timeout, 0);
Johannes Bergde3584b2011-11-21 10:44:00 +01002770 return -ENODEV;
2771 }
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002772
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002773 if (!driver_request->intersect) {
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002774 if (request_wiphy->regd)
2775 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002776
Johannes Berge9763c32012-12-03 16:59:46 +01002777 regd = reg_copy_regd(rd);
2778 if (IS_ERR(regd))
2779 return PTR_ERR(regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002780
Johannes Berg458f4f92012-12-06 15:47:38 +01002781 rcu_assign_pointer(request_wiphy->regd, regd);
Johannes Berg379b82f2012-12-06 15:44:07 +01002782 reset_regdomains(false, rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002783 return 0;
2784 }
2785
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002786 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2787 if (!intersected_rd)
2788 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002789
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002790 /*
2791 * We can trash what CRDA provided now.
2792 * However if a driver requested this specific regulatory
2793 * domain we keep it for its private use
2794 */
2795 tmp = get_wiphy_regdom(request_wiphy);
2796 rcu_assign_pointer(request_wiphy->regd, rd);
2797 rcu_free_regdom(tmp);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002798
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002799 rd = NULL;
Larry Fingerb7566fc2013-02-04 15:33:44 -06002800
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002801 reset_regdomains(false, intersected_rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002802
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002803 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002804}
2805
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002806static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
2807 struct regulatory_request *country_ie_request)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002808{
2809 struct wiphy *request_wiphy;
2810
2811 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2812 !is_unknown_alpha2(rd->alpha2))
2813 return -EINVAL;
2814
2815 /*
2816 * Lets only bother proceeding on the same alpha2 if the current
2817 * rd is non static (it means CRDA was present and was used last)
2818 * and the pending request came in from a country IE
2819 */
2820
2821 if (!is_valid_rd(rd)) {
2822 pr_err("Invalid regulatory domain detected:\n");
2823 print_regdomain_info(rd);
2824 return -EINVAL;
2825 }
2826
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002827 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002828 if (!request_wiphy) {
Shaibal Dutta845f3352014-01-30 15:08:30 -08002829 queue_delayed_work(system_power_efficient_wq,
2830 &reg_timeout, 0);
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002831 return -ENODEV;
2832 }
2833
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002834 if (country_ie_request->intersect)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002835 return -EINVAL;
2836
2837 reset_regdomains(false, rd);
2838 return 0;
2839}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002840
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002841/*
2842 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002843 * multiple drivers can be ironed out later. Caller must've already
Johannes Berg458f4f92012-12-06 15:47:38 +01002844 * kmalloc'd the rd structure.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002845 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002846int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002847{
Johannes Bergc492db32012-12-06 16:29:25 +01002848 struct regulatory_request *lr;
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002849 bool user_reset = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002850 int r;
2851
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002852 if (!reg_is_valid_request(rd->alpha2)) {
2853 kfree(rd);
2854 return -EINVAL;
2855 }
2856
Johannes Bergc492db32012-12-06 16:29:25 +01002857 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002858
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002859 /* Note that this doesn't update the wiphys, this is done below */
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002860 switch (lr->initiator) {
2861 case NL80211_REGDOM_SET_BY_CORE:
2862 r = reg_set_rd_core(rd);
2863 break;
2864 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002865 r = reg_set_rd_user(rd, lr);
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002866 user_reset = true;
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002867 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002868 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002869 r = reg_set_rd_driver(rd, lr);
2870 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002871 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002872 r = reg_set_rd_country_ie(rd, lr);
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002873 break;
2874 default:
2875 WARN(1, "invalid initiator %d\n", lr->initiator);
2876 return -EINVAL;
2877 }
2878
Johannes Bergd2372b32008-10-24 20:32:20 +02002879 if (r) {
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002880 switch (r) {
2881 case -EALREADY:
Kalle Valo95908532012-07-12 15:33:58 +03002882 reg_set_request_processed();
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002883 break;
2884 default:
2885 /* Back to world regulatory in case of errors */
2886 restore_regulatory_settings(user_reset);
2887 }
Kalle Valo95908532012-07-12 15:33:58 +03002888
Johannes Bergd2372b32008-10-24 20:32:20 +02002889 kfree(rd);
Johannes Berg38fd2142013-05-10 19:17:17 +02002890 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002891 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002892
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002893 /* This would make this whole thing pointless */
Johannes Berg38fd2142013-05-10 19:17:17 +02002894 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
2895 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002896
2897 /* update all wiphys now with the new established regulatory domain */
Johannes Bergc492db32012-12-06 16:29:25 +01002898 update_all_wiphy_regulatory(lr->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002899
Johannes Berg458f4f92012-12-06 15:47:38 +01002900 print_regdomain(get_cfg80211_regdom());
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002901
Johannes Bergc492db32012-12-06 16:29:25 +01002902 nl80211_send_reg_change_event(lr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002903
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002904 reg_set_request_processed();
2905
Johannes Berg38fd2142013-05-10 19:17:17 +02002906 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002907}
2908
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02002909static int __regulatory_set_wiphy_regd(struct wiphy *wiphy,
2910 struct ieee80211_regdomain *rd)
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002911{
2912 const struct ieee80211_regdomain *regd;
2913 const struct ieee80211_regdomain *prev_regd;
2914 struct cfg80211_registered_device *rdev;
2915
2916 if (WARN_ON(!wiphy || !rd))
2917 return -EINVAL;
2918
2919 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED),
2920 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n"))
2921 return -EPERM;
2922
2923 if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) {
2924 print_regdomain_info(rd);
2925 return -EINVAL;
2926 }
2927
2928 regd = reg_copy_regd(rd);
2929 if (IS_ERR(regd))
2930 return PTR_ERR(regd);
2931
2932 rdev = wiphy_to_rdev(wiphy);
2933
2934 spin_lock(&reg_requests_lock);
2935 prev_regd = rdev->requested_regd;
2936 rdev->requested_regd = regd;
2937 spin_unlock(&reg_requests_lock);
2938
2939 kfree(prev_regd);
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02002940 return 0;
2941}
2942
2943int regulatory_set_wiphy_regd(struct wiphy *wiphy,
2944 struct ieee80211_regdomain *rd)
2945{
2946 int ret = __regulatory_set_wiphy_regd(wiphy, rd);
2947
2948 if (ret)
2949 return ret;
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002950
2951 schedule_work(&reg_work);
2952 return 0;
2953}
2954EXPORT_SYMBOL(regulatory_set_wiphy_regd);
2955
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02002956int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy,
2957 struct ieee80211_regdomain *rd)
2958{
2959 int ret;
2960
2961 ASSERT_RTNL();
2962
2963 ret = __regulatory_set_wiphy_regd(wiphy, rd);
2964 if (ret)
2965 return ret;
2966
2967 /* process the request immediately */
2968 reg_process_self_managed_hints();
2969 return 0;
2970}
2971EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl);
2972
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002973void wiphy_regulatory_register(struct wiphy *wiphy)
2974{
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002975 struct regulatory_request *lr;
2976
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002977 /* self-managed devices ignore external hints */
2978 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2979 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
2980 REGULATORY_COUNTRY_IE_IGNORE;
2981
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002982 if (!reg_dev_ignore_cell_hint(wiphy))
2983 reg_num_devs_support_basehint++;
2984
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002985 lr = get_last_request();
2986 wiphy_update_regulatory(wiphy, lr->initiator);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002987}
2988
Luis R. Rodriguezbfead082012-07-12 11:49:19 -07002989void wiphy_regulatory_deregister(struct wiphy *wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002990{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002991 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01002992 struct regulatory_request *lr;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002993
Johannes Bergc492db32012-12-06 16:29:25 +01002994 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002995
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002996 if (!reg_dev_ignore_cell_hint(wiphy))
2997 reg_num_devs_support_basehint--;
2998
Johannes Berg458f4f92012-12-06 15:47:38 +01002999 rcu_free_regdom(get_wiphy_regdom(wiphy));
Monam Agarwal34dd8862014-03-24 00:53:40 +05303000 RCU_INIT_POINTER(wiphy->regd, NULL);
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003001
Johannes Bergc492db32012-12-06 16:29:25 +01003002 if (lr)
3003 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05003004
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003005 if (!request_wiphy || request_wiphy != wiphy)
Johannes Berg38fd2142013-05-10 19:17:17 +02003006 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003007
Johannes Bergc492db32012-12-06 16:29:25 +01003008 lr->wiphy_idx = WIPHY_IDX_INVALID;
3009 lr->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003010}
3011
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07003012static void reg_timeout_work(struct work_struct *work)
3013{
Johannes Berg1a919312012-12-03 17:21:11 +01003014 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
Johannes Bergf77b86d2013-06-24 15:43:38 +02003015 rtnl_lock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07003016 restore_regulatory_settings(true);
Johannes Bergf77b86d2013-06-24 15:43:38 +02003017 rtnl_unlock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07003018}
3019
Ilan Peer174e0cd2014-02-23 09:13:01 +02003020/*
3021 * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
3022 * UNII band definitions
3023 */
3024int cfg80211_get_unii(int freq)
3025{
3026 /* UNII-1 */
3027 if (freq >= 5150 && freq <= 5250)
3028 return 0;
3029
3030 /* UNII-2A */
3031 if (freq > 5250 && freq <= 5350)
3032 return 1;
3033
3034 /* UNII-2B */
3035 if (freq > 5350 && freq <= 5470)
3036 return 2;
3037
3038 /* UNII-2C */
3039 if (freq > 5470 && freq <= 5725)
3040 return 3;
3041
3042 /* UNII-3 */
3043 if (freq > 5725 && freq <= 5825)
3044 return 4;
3045
3046 return -EINVAL;
3047}
3048
Ilan Peerc8866e52014-02-23 09:13:03 +02003049bool regulatory_indoor_allowed(void)
3050{
3051 return reg_is_indoor;
3052}
3053
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02003054int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003055{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003056 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02003057
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003058 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
3059 if (IS_ERR(reg_pdev))
3060 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02003061
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003062 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003063 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003064
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -07003065 reg_regdb_size_check();
3066
Johannes Berg458f4f92012-12-06 15:47:38 +01003067 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02003068
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003069 user_alpha2[0] = '9';
3070 user_alpha2[1] = '7';
3071
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003072 /* We always try to get an update for the static regdomain */
Johannes Berg458f4f92012-12-06 15:47:38 +01003073 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003074 if (err) {
3075 if (err == -ENOMEM)
3076 return err;
3077 /*
3078 * N.B. kobject_uevent_env() can fail mainly for when we're out
3079 * memory which is handled and propagated appropriately above
3080 * but it can also fail during a netlink_broadcast() or during
3081 * early boot for call_usermodehelper(). For now treat these
3082 * errors as non-fatal.
3083 */
Joe Perchese9c02682010-11-16 19:56:49 -08003084 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003085 }
Johannes Berg734366d2008-09-15 10:56:48 +02003086
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003087 /*
3088 * Finally, if the user set the module parameter treat it
3089 * as a user hint.
3090 */
3091 if (!is_world_regdom(ieee80211_regdom))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003092 regulatory_hint_user(ieee80211_regdom,
3093 NL80211_USER_REG_HINT_USER);
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003094
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003095 return 0;
3096}
3097
Johannes Berg1a919312012-12-03 17:21:11 +01003098void regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003099{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003100 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003101 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003102
3103 cancel_work_sync(&reg_work);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07003104 cancel_delayed_work_sync(&reg_timeout);
Arik Nemtsovad932f02014-11-27 09:44:55 +02003105 cancel_delayed_work_sync(&reg_check_chans);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003106
Johannes Berg9027b142012-12-03 17:59:24 +01003107 /* Lock to suppress warnings */
Johannes Berg38fd2142013-05-10 19:17:17 +02003108 rtnl_lock();
Johannes Berg379b82f2012-12-06 15:44:07 +01003109 reset_regdomains(true, NULL);
Johannes Berg38fd2142013-05-10 19:17:17 +02003110 rtnl_unlock();
Johannes Berg734366d2008-09-15 10:56:48 +02003111
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08003112 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02003113
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003114 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02003115
Johannes Bergfea9bce2012-12-03 17:32:01 +01003116 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
3117 list_del(&reg_beacon->list);
3118 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003119 }
3120
Johannes Bergfea9bce2012-12-03 17:32:01 +01003121 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
3122 list_del(&reg_beacon->list);
3123 kfree(reg_beacon);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003124 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003125
Johannes Bergfea9bce2012-12-03 17:32:01 +01003126 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
3127 list_del(&reg_request->list);
3128 kfree(reg_request);
Johannes Berg8318d782008-01-24 19:38:38 +01003129 }
Johannes Berg8318d782008-01-24 19:38:38 +01003130}