blob: 3871998059de7beb9a2bff9a5321a8eb81bfcff4 [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
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03007 * Copyright 2017 Intel Deutschland GmbH
Johannes Berg8318d782008-01-24 19:38:38 +01008 *
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -08009 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Johannes Berg8318d782008-01-24 19:38:38 +010020 */
21
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -080022
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070023/**
24 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010025 *
26 * The usual implementation is for a driver to read a device EEPROM to
27 * determine which regulatory domain it should be operating under, then
28 * looking up the allowable channels in a driver-local table and finally
29 * registering those channels in the wiphy structure.
30 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070031 * Another set of compliance enforcement is for drivers to use their
32 * own compliance limits which can be stored on the EEPROM. The host
33 * driver or firmware may ensure these are used.
34 *
35 * In addition to all this we provide an extra layer of regulatory
36 * conformance. For drivers which do not have any regulatory
37 * information CRDA provides the complete regulatory solution.
38 * For others it provides a community effort on further restrictions
39 * to enhance compliance.
40 *
41 * Note: When number of rules --> infinity we will not be able to
42 * index on alpha2 any more, instead we'll probably have to
43 * rely on some SHA1 checksum of the regdomain for example.
44 *
Johannes Berg8318d782008-01-24 19:38:38 +010045 */
Joe Perchese9c02682010-11-16 19:56:49 -080046
47#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48
Johannes Berg8318d782008-01-24 19:38:38 +010049#include <linux/kernel.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040050#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070052#include <linux/list.h>
John W. Linvillec61029c2010-08-05 14:26:24 -040053#include <linux/ctype.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070054#include <linux/nl80211.h>
55#include <linux/platform_device.h>
Johannes Berg90a53e42017-09-13 22:21:08 +020056#include <linux/verification.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040057#include <linux/moduleparam.h>
Johannes Berg007f6c52015-10-15 11:22:58 +020058#include <linux/firmware.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070059#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010060#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070061#include "reg.h"
Arik Nemtsovad932f02014-11-27 09:44:55 +020062#include "rdev-ops.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040063#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010064
Arik Nemtsovad932f02014-11-27 09:44:55 +020065/*
66 * Grace period we give before making sure all current interfaces reside on
67 * channels allowed by the current regulatory domain.
68 */
69#define REG_ENFORCE_GRACE_MS 60000
70
Ilan Peer52616f22014-02-25 16:26:00 +020071/**
72 * enum reg_request_treatment - regulatory request treatment
73 *
74 * @REG_REQ_OK: continue processing the regulatory request
75 * @REG_REQ_IGNORE: ignore the regulatory request
76 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
77 * be intersected with the current one.
78 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
79 * regulatory settings, and no further processing is required.
Ilan Peer52616f22014-02-25 16:26:00 +020080 */
Johannes Berg2f922122012-12-03 17:54:55 +010081enum reg_request_treatment {
82 REG_REQ_OK,
83 REG_REQ_IGNORE,
84 REG_REQ_INTERSECT,
85 REG_REQ_ALREADY_SET,
86};
87
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050088static struct regulatory_request core_request_world = {
89 .initiator = NL80211_REGDOM_SET_BY_CORE,
90 .alpha2[0] = '0',
91 .alpha2[1] = '0',
92 .intersect = false,
93 .processed = true,
94 .country_ie_env = ENVIRON_ANY,
95};
96
Johannes Berg38fd2142013-05-10 19:17:17 +020097/*
98 * Receipt of information from last regulatory request,
99 * protected by RTNL (and can be accessed with RCU protection)
100 */
Johannes Bergc492db32012-12-06 16:29:25 +0100101static struct regulatory_request __rcu *last_request =
Johannes Bergcec3f0e2014-12-12 12:26:25 +0100102 (void __force __rcu *)&core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +0200103
Johannes Berg007f6c52015-10-15 11:22:58 +0200104/* To trigger userspace events and load firmware */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700105static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +0100106
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500107/*
108 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +0200109 * the current one and a world regulatory domain in case we have no
Johannes Berge8da2bb2012-12-03 23:00:08 +0100110 * information to give us an alpha2.
Johannes Berg38fd2142013-05-10 19:17:17 +0200111 * (protected by RTNL, can be read under RCU)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500112 */
Johannes Berg458f4f92012-12-06 15:47:38 +0100113const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +0200114
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500115/*
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700116 * Number of devices that registered to the core
117 * that support cellular base station regulatory hints
Johannes Berg38fd2142013-05-10 19:17:17 +0200118 * (protected by RTNL)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700119 */
120static int reg_num_devs_support_basehint;
121
Ilan Peer52616f22014-02-25 16:26:00 +0200122/*
123 * State variable indicating if the platform on which the devices
124 * are attached is operating in an indoor environment. The state variable
125 * is relevant for all registered devices.
Ilan Peer52616f22014-02-25 16:26:00 +0200126 */
127static bool reg_is_indoor;
Ilan peer05050752015-03-04 00:32:06 -0500128static spinlock_t reg_indoor_lock;
129
130/* Used to track the userspace process controlling the indoor setting */
131static u32 reg_is_indoor_portid;
Ilan Peer52616f22014-02-25 16:26:00 +0200132
Johannes Bergb6863032015-10-15 09:25:18 +0200133static void restore_regulatory_settings(bool reset_user);
Ilan peerc37722b2015-03-30 15:15:49 +0300134
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
Johannes Bergc799ba62015-12-11 15:31:10 +0100176 pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n",
177 dev_name(&wiphy->dev),
178 reg_dfs_region_str(wiphy_regd->dfs_region),
179 reg_dfs_region_str(regd->dfs_region));
Luis R. Rodriguez6c474792013-11-25 20:56:09 +0100180
181out:
182 return regd->dfs_region;
183}
184
Johannes Berg458f4f92012-12-06 15:47:38 +0100185static void rcu_free_regdom(const struct ieee80211_regdomain *r)
186{
187 if (!r)
188 return;
189 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
190}
191
Johannes Bergc492db32012-12-06 16:29:25 +0100192static struct regulatory_request *get_last_request(void)
193{
Johannes Berg38fd2142013-05-10 19:17:17 +0200194 return rcu_dereference_rtnl(last_request);
Johannes Bergc492db32012-12-06 16:29:25 +0100195}
196
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500197/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500198static LIST_HEAD(reg_requests_list);
199static spinlock_t reg_requests_lock;
200
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500201/* Used to queue up beacon hints for review */
202static LIST_HEAD(reg_pending_beacons);
203static spinlock_t reg_pending_beacons_lock;
204
205/* Used to keep track of processed beacon hints */
206static LIST_HEAD(reg_beacon_list);
207
208struct reg_beacon {
209 struct list_head list;
210 struct ieee80211_channel chan;
211};
212
Arik Nemtsovad932f02014-11-27 09:44:55 +0200213static void reg_check_chans_work(struct work_struct *work);
214static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);
215
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800216static void reg_todo(struct work_struct *work);
217static DECLARE_WORK(reg_work, reg_todo);
218
Johannes Berg734366d2008-09-15 10:56:48 +0200219/* We keep a static world regulatory domain in case of the absence of CRDA */
220static const struct ieee80211_regdomain world_regdom = {
Jason Abele28981e52015-02-17 16:10:41 -0800221 .n_reg_rules = 8,
Johannes Berg734366d2008-09-15 10:56:48 +0200222 .alpha2 = "00",
223 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500224 /* IEEE 802.11b/g, channels 1..11 */
225 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Johannes Berg43c771a2012-11-12 10:51:34 +0100226 /* IEEE 802.11b/g, channels 12..13. */
Johannes Bergc3826802015-12-11 17:35:50 +0100227 REG_RULE(2467-10, 2472+10, 20, 6, 20,
228 NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800229 /* IEEE 802.11 channel 14 - Only JP enables
230 * this and for 802.11b only */
231 REG_RULE(2484-10, 2484+10, 20, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200232 NL80211_RRF_NO_IR |
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800233 NL80211_RRF_NO_OFDM),
234 /* IEEE 802.11a, channel 36..48 */
Johannes Bergc3826802015-12-11 17:35:50 +0100235 REG_RULE(5180-10, 5240+10, 80, 6, 20,
236 NL80211_RRF_NO_IR |
237 NL80211_RRF_AUTO_BW),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500238
Johannes Berg131a19b2013-03-04 20:54:46 +0100239 /* IEEE 802.11a, channel 52..64 - DFS required */
Johannes Bergc3826802015-12-11 17:35:50 +0100240 REG_RULE(5260-10, 5320+10, 80, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200241 NL80211_RRF_NO_IR |
Johannes Bergc3826802015-12-11 17:35:50 +0100242 NL80211_RRF_AUTO_BW |
Johannes Berg131a19b2013-03-04 20:54:46 +0100243 NL80211_RRF_DFS),
244
245 /* IEEE 802.11a, channel 100..144 - DFS required */
246 REG_RULE(5500-10, 5720+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200247 NL80211_RRF_NO_IR |
Johannes Berg131a19b2013-03-04 20:54:46 +0100248 NL80211_RRF_DFS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500249
250 /* IEEE 802.11a, channel 149..165 */
Johannes Berg8ab9d852012-12-03 22:09:22 +0100251 REG_RULE(5745-10, 5825+10, 80, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200252 NL80211_RRF_NO_IR),
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300253
Johannes Berg8047d262015-10-15 16:16:09 +0200254 /* IEEE 802.11ad (60GHz), channels 1..3 */
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300255 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200256 }
257};
258
Johannes Berg38fd2142013-05-10 19:17:17 +0200259/* protected by RTNL */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200260static const struct ieee80211_regdomain *cfg80211_world_regdom =
261 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200262
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400263static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500264static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400265
Johannes Berg734366d2008-09-15 10:56:48 +0200266module_param(ieee80211_regdom, charp, 0444);
267MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
268
Arik Nemtsovc8883932014-04-21 20:39:34 -0700269static void reg_free_request(struct regulatory_request *request)
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800270{
Johannes Bergd34265a2015-10-15 13:05:55 +0200271 if (request == &core_request_world)
272 return;
273
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
Johannes Bergc7d319e52015-10-15 09:03:05 +0200447struct reg_regdb_apply_request {
John W. Linville3b377ea2009-12-18 17:59:01 -0500448 struct list_head list;
Johannes Bergc7d319e52015-10-15 09:03:05 +0200449 const struct ieee80211_regdomain *regdom;
John W. Linville3b377ea2009-12-18 17:59:01 -0500450};
451
Johannes Bergc7d319e52015-10-15 09:03:05 +0200452static LIST_HEAD(reg_regdb_apply_list);
453static DEFINE_MUTEX(reg_regdb_apply_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500454
Johannes Bergc7d319e52015-10-15 09:03:05 +0200455static void reg_regdb_apply(struct work_struct *work)
John W. Linville3b377ea2009-12-18 17:59:01 -0500456{
Johannes Bergc7d319e52015-10-15 09:03:05 +0200457 struct reg_regdb_apply_request *request;
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700458
Johannes Berg5fe231e2013-05-08 21:45:15 +0200459 rtnl_lock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500460
Johannes Bergc7d319e52015-10-15 09:03:05 +0200461 mutex_lock(&reg_regdb_apply_mutex);
462 while (!list_empty(&reg_regdb_apply_list)) {
463 request = list_first_entry(&reg_regdb_apply_list,
464 struct reg_regdb_apply_request,
John W. Linville3b377ea2009-12-18 17:59:01 -0500465 list);
466 list_del(&request->list);
467
Johannes Bergc7d319e52015-10-15 09:03:05 +0200468 set_regdom(request->regdom, REGD_SOURCE_INTERNAL_DB);
John W. Linville3b377ea2009-12-18 17:59:01 -0500469 kfree(request);
470 }
Johannes Bergc7d319e52015-10-15 09:03:05 +0200471 mutex_unlock(&reg_regdb_apply_mutex);
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700472
Johannes Berg5fe231e2013-05-08 21:45:15 +0200473 rtnl_unlock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500474}
475
Johannes Bergc7d319e52015-10-15 09:03:05 +0200476static DECLARE_WORK(reg_regdb_work, reg_regdb_apply);
John W. Linville3b377ea2009-12-18 17:59:01 -0500477
Johannes Berg007f6c52015-10-15 11:22:58 +0200478static int reg_schedule_apply(const struct ieee80211_regdomain *regdom)
John W. Linville3b377ea2009-12-18 17:59:01 -0500479{
Johannes Bergc7d319e52015-10-15 09:03:05 +0200480 struct reg_regdb_apply_request *request;
Johannes Bergc7d319e52015-10-15 09:03:05 +0200481
482 request = kzalloc(sizeof(struct reg_regdb_apply_request), GFP_KERNEL);
Johannes Berg007f6c52015-10-15 11:22:58 +0200483 if (!request) {
484 kfree(regdom);
Johannes Bergc7d319e52015-10-15 09:03:05 +0200485 return -ENOMEM;
486 }
John W. Linville3b377ea2009-12-18 17:59:01 -0500487
Johannes Berg007f6c52015-10-15 11:22:58 +0200488 request->regdom = regdom;
489
Johannes Bergc7d319e52015-10-15 09:03:05 +0200490 mutex_lock(&reg_regdb_apply_mutex);
491 list_add_tail(&request->list, &reg_regdb_apply_list);
492 mutex_unlock(&reg_regdb_apply_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500493
494 schedule_work(&reg_regdb_work);
Johannes Bergc7d319e52015-10-15 09:03:05 +0200495 return 0;
John W. Linville3b377ea2009-12-18 17:59:01 -0500496}
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700497
Johannes Bergb6863032015-10-15 09:25:18 +0200498#ifdef CONFIG_CFG80211_CRDA_SUPPORT
499/* Max number of consecutive attempts to communicate with CRDA */
500#define REG_MAX_CRDA_TIMEOUTS 10
501
502static u32 reg_crda_timeouts;
503
504static void crda_timeout_work(struct work_struct *work);
505static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work);
506
507static void crda_timeout_work(struct work_struct *work)
508{
Johannes Bergc799ba62015-12-11 15:31:10 +0100509 pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
Johannes Bergb6863032015-10-15 09:25:18 +0200510 rtnl_lock();
511 reg_crda_timeouts++;
512 restore_regulatory_settings(true);
513 rtnl_unlock();
514}
515
516static void cancel_crda_timeout(void)
517{
518 cancel_delayed_work(&crda_timeout);
519}
520
521static void cancel_crda_timeout_sync(void)
522{
523 cancel_delayed_work_sync(&crda_timeout);
524}
525
526static void reset_crda_timeouts(void)
527{
528 reg_crda_timeouts = 0;
529}
530
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500531/*
532 * This lets us keep regulatory code which is updated on a regulatory
Johannes Berg1226d252014-02-25 15:43:36 +0100533 * basis in userspace.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500534 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700535static int call_crda(const char *alpha2)
536{
Johannes Berg1226d252014-02-25 15:43:36 +0100537 char country[12];
538 char *env[] = { country, NULL };
Johannes Bergc7d319e52015-10-15 09:03:05 +0200539 int ret;
Johannes Berg1226d252014-02-25 15:43:36 +0100540
541 snprintf(country, sizeof(country), "COUNTRY=%c%c",
542 alpha2[0], alpha2[1]);
543
Ilan peerc37722b2015-03-30 15:15:49 +0300544 if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) {
Thomas Petazzoni042ab5f2015-07-09 15:35:15 +0200545 pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n");
Ilan peerc37722b2015-03-30 15:15:49 +0300546 return -EINVAL;
547 }
548
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700549 if (!is_world_regdom((char *) alpha2))
Thomas Petazzoni042ab5f2015-07-09 15:35:15 +0200550 pr_debug("Calling CRDA for country: %c%c\n",
Johannes Bergc799ba62015-12-11 15:31:10 +0100551 alpha2[0], alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700552 else
Thomas Petazzoni042ab5f2015-07-09 15:35:15 +0200553 pr_debug("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700554
Johannes Bergc7d319e52015-10-15 09:03:05 +0200555 ret = kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
556 if (ret)
557 return ret;
558
559 queue_delayed_work(system_power_efficient_wq,
Johannes Bergb6863032015-10-15 09:25:18 +0200560 &crda_timeout, msecs_to_jiffies(3142));
Johannes Bergc7d319e52015-10-15 09:03:05 +0200561 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700562}
Johannes Bergb6863032015-10-15 09:25:18 +0200563#else
564static inline void cancel_crda_timeout(void) {}
565static inline void cancel_crda_timeout_sync(void) {}
566static inline void reset_crda_timeouts(void) {}
567static inline int call_crda(const char *alpha2)
568{
569 return -ENODATA;
570}
571#endif /* CONFIG_CFG80211_CRDA_SUPPORT */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700572
Johannes Berg007f6c52015-10-15 11:22:58 +0200573/* code to directly load a firmware database through request_firmware */
574static const struct fwdb_header *regdb;
575
576struct fwdb_country {
577 u8 alpha2[2];
578 __be16 coll_ptr;
579 /* this struct cannot be extended */
580} __packed __aligned(4);
581
582struct fwdb_collection {
583 u8 len;
584 u8 n_rules;
585 u8 dfs_region;
586 /* no optional data yet */
587 /* aligned to 2, then followed by __be16 array of rule pointers */
588} __packed __aligned(4);
589
590enum fwdb_flags {
591 FWDB_FLAG_NO_OFDM = BIT(0),
592 FWDB_FLAG_NO_OUTDOOR = BIT(1),
593 FWDB_FLAG_DFS = BIT(2),
594 FWDB_FLAG_NO_IR = BIT(3),
595 FWDB_FLAG_AUTO_BW = BIT(4),
596};
597
598struct fwdb_rule {
599 u8 len;
600 u8 flags;
601 __be16 max_eirp;
602 __be32 start, end, max_bw;
603 /* start of optional data */
604 __be16 cac_timeout;
605} __packed __aligned(4);
606
607#define FWDB_MAGIC 0x52474442
608#define FWDB_VERSION 20
609
610struct fwdb_header {
611 __be32 magic;
612 __be32 version;
613 struct fwdb_country country[];
614} __packed __aligned(4);
615
616static bool valid_rule(const u8 *data, unsigned int size, u16 rule_ptr)
617{
618 struct fwdb_rule *rule = (void *)(data + (rule_ptr << 2));
619
620 if ((u8 *)rule + sizeof(rule->len) > data + size)
621 return false;
622
623 /* mandatory fields */
624 if (rule->len < offsetofend(struct fwdb_rule, max_bw))
625 return false;
626
627 return true;
628}
629
630static bool valid_country(const u8 *data, unsigned int size,
631 const struct fwdb_country *country)
632{
633 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
634 struct fwdb_collection *coll = (void *)(data + ptr);
635 __be16 *rules_ptr;
636 unsigned int i;
637
638 /* make sure we can read len/n_rules */
639 if ((u8 *)coll + offsetofend(typeof(*coll), n_rules) > data + size)
640 return false;
641
642 /* make sure base struct and all rules fit */
643 if ((u8 *)coll + ALIGN(coll->len, 2) +
644 (coll->n_rules * 2) > data + size)
645 return false;
646
647 /* mandatory fields must exist */
648 if (coll->len < offsetofend(struct fwdb_collection, dfs_region))
649 return false;
650
651 rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
652
653 for (i = 0; i < coll->n_rules; i++) {
654 u16 rule_ptr = be16_to_cpu(rules_ptr[i]);
655
656 if (!valid_rule(data, size, rule_ptr))
657 return false;
658 }
659
660 return true;
661}
662
Johannes Berg90a53e42017-09-13 22:21:08 +0200663#ifdef CONFIG_CFG80211_REQUIRE_SIGNED_REGDB
664static struct key *builtin_regdb_keys;
665
666static void __init load_keys_from_buffer(const u8 *p, unsigned int buflen)
667{
668 const u8 *end = p + buflen;
669 size_t plen;
670 key_ref_t key;
671
672 while (p < end) {
673 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
674 * than 256 bytes in size.
675 */
676 if (end - p < 4)
677 goto dodgy_cert;
678 if (p[0] != 0x30 &&
679 p[1] != 0x82)
680 goto dodgy_cert;
681 plen = (p[2] << 8) | p[3];
682 plen += 4;
683 if (plen > end - p)
684 goto dodgy_cert;
685
686 key = key_create_or_update(make_key_ref(builtin_regdb_keys, 1),
687 "asymmetric", NULL, p, plen,
688 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
689 KEY_USR_VIEW | KEY_USR_READ),
690 KEY_ALLOC_NOT_IN_QUOTA |
691 KEY_ALLOC_BUILT_IN |
692 KEY_ALLOC_BYPASS_RESTRICTION);
693 if (IS_ERR(key)) {
694 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
695 PTR_ERR(key));
696 } else {
697 pr_notice("Loaded X.509 cert '%s'\n",
698 key_ref_to_ptr(key)->description);
699 key_ref_put(key);
700 }
701 p += plen;
702 }
703
704 return;
705
706dodgy_cert:
707 pr_err("Problem parsing in-kernel X.509 certificate list\n");
708}
709
710static int __init load_builtin_regdb_keys(void)
711{
712 builtin_regdb_keys =
713 keyring_alloc(".builtin_regdb_keys",
714 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
715 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
716 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
717 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
718 if (IS_ERR(builtin_regdb_keys))
719 return PTR_ERR(builtin_regdb_keys);
720
721 pr_notice("Loading compiled-in X.509 certificates for regulatory database\n");
722
723#ifdef CONFIG_CFG80211_USE_KERNEL_REGDB_KEYS
724 load_keys_from_buffer(shipped_regdb_certs, shipped_regdb_certs_len);
725#endif
Arnd Bergmann88230ef2017-10-13 14:04:31 +0200726#ifdef CONFIG_CFG80211_EXTRA_REGDB_KEYDIR
Johannes Berg90a53e42017-09-13 22:21:08 +0200727 if (CONFIG_CFG80211_EXTRA_REGDB_KEYDIR[0] != '\0')
728 load_keys_from_buffer(extra_regdb_certs, extra_regdb_certs_len);
729#endif
730
731 return 0;
732}
733
734static bool regdb_has_valid_signature(const u8 *data, unsigned int size)
735{
736 const struct firmware *sig;
737 bool result;
738
739 if (request_firmware(&sig, "regulatory.db.p7s", &reg_pdev->dev))
740 return false;
741
742 result = verify_pkcs7_signature(data, size, sig->data, sig->size,
743 builtin_regdb_keys,
744 VERIFYING_UNSPECIFIED_SIGNATURE,
745 NULL, NULL) == 0;
746
747 release_firmware(sig);
748
749 return result;
750}
751
752static void free_regdb_keyring(void)
753{
754 key_put(builtin_regdb_keys);
755}
756#else
757static int load_builtin_regdb_keys(void)
758{
759 return 0;
760}
761
762static bool regdb_has_valid_signature(const u8 *data, unsigned int size)
763{
764 return true;
765}
766
767static void free_regdb_keyring(void)
768{
769}
770#endif /* CONFIG_CFG80211_REQUIRE_SIGNED_REGDB */
771
Johannes Berg007f6c52015-10-15 11:22:58 +0200772static bool valid_regdb(const u8 *data, unsigned int size)
773{
774 const struct fwdb_header *hdr = (void *)data;
775 const struct fwdb_country *country;
776
777 if (size < sizeof(*hdr))
778 return false;
779
780 if (hdr->magic != cpu_to_be32(FWDB_MAGIC))
781 return false;
782
783 if (hdr->version != cpu_to_be32(FWDB_VERSION))
784 return false;
785
Johannes Berg90a53e42017-09-13 22:21:08 +0200786 if (!regdb_has_valid_signature(data, size))
787 return false;
788
Johannes Berg007f6c52015-10-15 11:22:58 +0200789 country = &hdr->country[0];
790 while ((u8 *)(country + 1) <= data + size) {
791 if (!country->coll_ptr)
792 break;
793 if (!valid_country(data, size, country))
794 return false;
795 country++;
796 }
797
798 return true;
799}
800
801static int regdb_query_country(const struct fwdb_header *db,
802 const struct fwdb_country *country)
803{
804 unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
805 struct fwdb_collection *coll = (void *)((u8 *)db + ptr);
806 struct ieee80211_regdomain *regdom;
807 unsigned int size_of_regd;
808 unsigned int i;
809
810 size_of_regd =
811 sizeof(struct ieee80211_regdomain) +
812 coll->n_rules * sizeof(struct ieee80211_reg_rule);
813
814 regdom = kzalloc(size_of_regd, GFP_KERNEL);
815 if (!regdom)
816 return -ENOMEM;
817
818 regdom->n_reg_rules = coll->n_rules;
819 regdom->alpha2[0] = country->alpha2[0];
820 regdom->alpha2[1] = country->alpha2[1];
821 regdom->dfs_region = coll->dfs_region;
822
823 for (i = 0; i < regdom->n_reg_rules; i++) {
824 __be16 *rules_ptr = (void *)((u8 *)coll + ALIGN(coll->len, 2));
825 unsigned int rule_ptr = be16_to_cpu(rules_ptr[i]) << 2;
826 struct fwdb_rule *rule = (void *)((u8 *)db + rule_ptr);
827 struct ieee80211_reg_rule *rrule = &regdom->reg_rules[i];
828
829 rrule->freq_range.start_freq_khz = be32_to_cpu(rule->start);
830 rrule->freq_range.end_freq_khz = be32_to_cpu(rule->end);
831 rrule->freq_range.max_bandwidth_khz = be32_to_cpu(rule->max_bw);
832
833 rrule->power_rule.max_antenna_gain = 0;
834 rrule->power_rule.max_eirp = be16_to_cpu(rule->max_eirp);
835
836 rrule->flags = 0;
837 if (rule->flags & FWDB_FLAG_NO_OFDM)
838 rrule->flags |= NL80211_RRF_NO_OFDM;
839 if (rule->flags & FWDB_FLAG_NO_OUTDOOR)
840 rrule->flags |= NL80211_RRF_NO_OUTDOOR;
841 if (rule->flags & FWDB_FLAG_DFS)
842 rrule->flags |= NL80211_RRF_DFS;
843 if (rule->flags & FWDB_FLAG_NO_IR)
844 rrule->flags |= NL80211_RRF_NO_IR;
845 if (rule->flags & FWDB_FLAG_AUTO_BW)
846 rrule->flags |= NL80211_RRF_AUTO_BW;
847
848 rrule->dfs_cac_ms = 0;
849
850 /* handle optional data */
851 if (rule->len >= offsetofend(struct fwdb_rule, cac_timeout))
852 rrule->dfs_cac_ms =
853 1000 * be16_to_cpu(rule->cac_timeout);
854 }
855
856 return reg_schedule_apply(regdom);
857}
858
859static int query_regdb(const char *alpha2)
860{
861 const struct fwdb_header *hdr = regdb;
862 const struct fwdb_country *country;
863
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200864 ASSERT_RTNL();
865
Johannes Berg007f6c52015-10-15 11:22:58 +0200866 if (IS_ERR(regdb))
867 return PTR_ERR(regdb);
868
869 country = &hdr->country[0];
870 while (country->coll_ptr) {
871 if (alpha2_equal(alpha2, country->alpha2))
872 return regdb_query_country(regdb, country);
873 country++;
874 }
875
876 return -ENODATA;
877}
878
879static void regdb_fw_cb(const struct firmware *fw, void *context)
880{
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200881 int set_error = 0;
882 bool restore = true;
Johannes Berg007f6c52015-10-15 11:22:58 +0200883 void *db;
884
885 if (!fw) {
886 pr_info("failed to load regulatory.db\n");
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200887 set_error = -ENODATA;
888 } else if (!valid_regdb(fw->data, fw->size)) {
Johannes Berg90a53e42017-09-13 22:21:08 +0200889 pr_info("loaded regulatory.db is malformed or signature is missing/invalid\n");
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200890 set_error = -EINVAL;
Johannes Berg007f6c52015-10-15 11:22:58 +0200891 }
892
Johannes Berg007f6c52015-10-15 11:22:58 +0200893 rtnl_lock();
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200894 if (WARN_ON(regdb && !IS_ERR(regdb))) {
895 /* just restore and free new db */
896 } else if (set_error) {
897 regdb = ERR_PTR(set_error);
898 } else if (fw) {
899 db = kmemdup(fw->data, fw->size, GFP_KERNEL);
900 if (db) {
901 regdb = db;
902 restore = context && query_regdb(context);
903 } else {
904 restore = true;
905 }
906 }
907
908 if (restore)
909 restore_regulatory_settings(true);
910
Johannes Berg007f6c52015-10-15 11:22:58 +0200911 rtnl_unlock();
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200912
Johannes Berg007f6c52015-10-15 11:22:58 +0200913 kfree(context);
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200914
915 release_firmware(fw);
Johannes Berg007f6c52015-10-15 11:22:58 +0200916}
917
918static int query_regdb_file(const char *alpha2)
919{
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200920 ASSERT_RTNL();
921
Johannes Berg007f6c52015-10-15 11:22:58 +0200922 if (regdb)
923 return query_regdb(alpha2);
924
925 alpha2 = kmemdup(alpha2, 2, GFP_KERNEL);
926 if (!alpha2)
927 return -ENOMEM;
928
929 return request_firmware_nowait(THIS_MODULE, true, "regulatory.db",
930 &reg_pdev->dev, GFP_KERNEL,
931 (void *)alpha2, regdb_fw_cb);
932}
933
Johannes Berg1ea4ff32017-09-13 16:07:22 +0200934int reg_reload_regdb(void)
935{
936 const struct firmware *fw;
937 void *db;
938 int err;
939
940 err = request_firmware(&fw, "regulatory.db", &reg_pdev->dev);
941 if (err)
942 return err;
943
944 if (!valid_regdb(fw->data, fw->size)) {
945 err = -ENODATA;
946 goto out;
947 }
948
949 db = kmemdup(fw->data, fw->size, GFP_KERNEL);
950 if (!db) {
951 err = -ENOMEM;
952 goto out;
953 }
954
955 rtnl_lock();
956 if (!IS_ERR_OR_NULL(regdb))
957 kfree(regdb);
958 regdb = db;
959 rtnl_unlock();
960
961 out:
962 release_firmware(fw);
963 return err;
964}
965
Johannes Bergcecbb062015-10-15 08:47:34 +0200966static bool reg_query_database(struct regulatory_request *request)
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -0800967{
Johannes Berg007f6c52015-10-15 11:22:58 +0200968 if (query_regdb_file(request->alpha2) == 0)
969 return true;
970
Johannes Bergc7d319e52015-10-15 09:03:05 +0200971 if (call_crda(request->alpha2) == 0)
972 return true;
973
974 return false;
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -0800975}
976
Luis R. Rodrigueze4387682013-11-05 09:18:01 -0800977bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700978{
Johannes Bergc492db32012-12-06 16:29:25 +0100979 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400980
Johannes Bergc492db32012-12-06 16:29:25 +0100981 if (!lr || lr->processed)
Johannes Bergf6037d02008-10-21 11:01:33 +0200982 return false;
983
Johannes Bergc492db32012-12-06 16:29:25 +0100984 return alpha2_equal(lr->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700985}
986
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100987static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
988{
989 struct regulatory_request *lr = get_last_request();
990
991 /*
992 * Follow the driver's regulatory domain, if present, unless a country
993 * IE has been processed or a user wants to help complaince further
994 */
995 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
996 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
997 wiphy->regd)
998 return get_wiphy_regdom(wiphy);
999
1000 return get_cfg80211_regdom();
1001}
1002
Arik Nemtsova6d4a532014-10-23 09:37:33 +03001003static unsigned int
1004reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
1005 const struct ieee80211_reg_rule *rule)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001006{
1007 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
1008 const struct ieee80211_freq_range *freq_range_tmp;
1009 const struct ieee80211_reg_rule *tmp;
1010 u32 start_freq, end_freq, idx, no;
1011
1012 for (idx = 0; idx < rd->n_reg_rules; idx++)
1013 if (rule == &rd->reg_rules[idx])
1014 break;
1015
1016 if (idx == rd->n_reg_rules)
1017 return 0;
1018
1019 /* get start_freq */
1020 no = idx;
1021
1022 while (no) {
1023 tmp = &rd->reg_rules[--no];
1024 freq_range_tmp = &tmp->freq_range;
1025
1026 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
1027 break;
1028
Janusz Dziedzic97524822014-01-30 09:52:20 +01001029 freq_range = freq_range_tmp;
1030 }
1031
1032 start_freq = freq_range->start_freq_khz;
1033
1034 /* get end_freq */
1035 freq_range = &rule->freq_range;
1036 no = idx;
1037
1038 while (no < rd->n_reg_rules - 1) {
1039 tmp = &rd->reg_rules[++no];
1040 freq_range_tmp = &tmp->freq_range;
1041
1042 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
1043 break;
1044
Janusz Dziedzic97524822014-01-30 09:52:20 +01001045 freq_range = freq_range_tmp;
1046 }
1047
1048 end_freq = freq_range->end_freq_khz;
1049
1050 return end_freq - start_freq;
1051}
1052
Arik Nemtsova6d4a532014-10-23 09:37:33 +03001053unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
1054 const struct ieee80211_reg_rule *rule)
1055{
1056 unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);
1057
1058 if (rule->flags & NL80211_RRF_NO_160MHZ)
1059 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
1060 if (rule->flags & NL80211_RRF_NO_80MHZ)
1061 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));
1062
1063 /*
1064 * HT40+/HT40- limits are handled per-channel. Only limit BW if both
1065 * are not allowed.
1066 */
1067 if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
1068 rule->flags & NL80211_RRF_NO_HT40PLUS)
1069 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));
1070
1071 return bw;
1072}
1073
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001074/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001075static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001076{
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001077 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001078 u32 freq_diff;
1079
Luis R. Rodriguez91e99002008-11-12 14:21:55 -08001080 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001081 return false;
1082
1083 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
1084 return false;
1085
1086 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1087
Roel Kluinbd05f282009-03-03 22:55:21 +01001088 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg1a919312012-12-03 17:21:11 +01001089 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001090 return false;
1091
1092 return true;
1093}
1094
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001095static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001096{
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001097 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001098 unsigned int i;
1099
1100 if (!rd->n_reg_rules)
1101 return false;
1102
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -08001103 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
1104 return false;
1105
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001106 for (i = 0; i < rd->n_reg_rules; i++) {
1107 reg_rule = &rd->reg_rules[i];
1108 if (!is_valid_reg_rule(reg_rule))
1109 return false;
1110 }
1111
1112 return true;
1113}
1114
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001115/**
1116 * freq_in_rule_band - tells us if a frequency is in a frequency band
1117 * @freq_range: frequency rule we want to query
1118 * @freq_khz: frequency we are inquiring about
1119 *
1120 * This lets us know if a specific frequency rule is or is not relevant to
1121 * a specific frequency's band. Bands are device specific and artificial
Vladimir Kondratiev64629b92012-09-23 09:49:54 +02001122 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
1123 * however it is safe for now to assume that a frequency rule should not be
1124 * part of a frequency's band if the start freq or end freq are off by more
1125 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
1126 * 60 GHz band.
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001127 * This resolution can be lowered and should be considered as we add
1128 * regulatory rule support for other "bands".
1129 **/
1130static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg1a919312012-12-03 17:21:11 +01001131 u32 freq_khz)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001132{
1133#define ONE_GHZ_IN_KHZ 1000000
Vladimir Kondratiev64629b92012-09-23 09:49:54 +02001134 /*
1135 * From 802.11ad: directional multi-gigabit (DMG):
1136 * Pertaining to operation in a frequency band containing a channel
1137 * with the Channel starting frequency above 45 GHz.
1138 */
1139 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
1140 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
1141 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001142 return true;
Vladimir Kondratiev64629b92012-09-23 09:49:54 +02001143 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001144 return true;
1145 return false;
1146#undef ONE_GHZ_IN_KHZ
1147}
1148
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001149/*
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +01001150 * Later on we can perhaps use the more restrictive DFS
1151 * region but we don't have information for that yet so
1152 * for now simply disallow conflicts.
1153 */
1154static enum nl80211_dfs_regions
1155reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
1156 const enum nl80211_dfs_regions dfs_region2)
1157{
1158 if (dfs_region1 != dfs_region2)
1159 return NL80211_DFS_UNSET;
1160 return dfs_region1;
1161}
1162
1163/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001164 * Helper for regdom_intersect(), this does the real
1165 * mathematical intersection fun
1166 */
Janusz Dziedzic97524822014-01-30 09:52:20 +01001167static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
1168 const struct ieee80211_regdomain *rd2,
1169 const struct ieee80211_reg_rule *rule1,
Johannes Berg1a919312012-12-03 17:21:11 +01001170 const struct ieee80211_reg_rule *rule2,
1171 struct ieee80211_reg_rule *intersected_rule)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001172{
1173 const struct ieee80211_freq_range *freq_range1, *freq_range2;
1174 struct ieee80211_freq_range *freq_range;
1175 const struct ieee80211_power_rule *power_rule1, *power_rule2;
1176 struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001177 u32 freq_diff, max_bandwidth1, max_bandwidth2;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001178
1179 freq_range1 = &rule1->freq_range;
1180 freq_range2 = &rule2->freq_range;
1181 freq_range = &intersected_rule->freq_range;
1182
1183 power_rule1 = &rule1->power_rule;
1184 power_rule2 = &rule2->power_rule;
1185 power_rule = &intersected_rule->power_rule;
1186
1187 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +01001188 freq_range2->start_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001189 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +01001190 freq_range2->end_freq_khz);
Janusz Dziedzic97524822014-01-30 09:52:20 +01001191
1192 max_bandwidth1 = freq_range1->max_bandwidth_khz;
1193 max_bandwidth2 = freq_range2->max_bandwidth_khz;
1194
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001195 if (rule1->flags & NL80211_RRF_AUTO_BW)
1196 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
1197 if (rule2->flags & NL80211_RRF_AUTO_BW)
1198 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
Janusz Dziedzic97524822014-01-30 09:52:20 +01001199
1200 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001201
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001202 intersected_rule->flags = rule1->flags | rule2->flags;
1203
1204 /*
1205 * In case NL80211_RRF_AUTO_BW requested for both rules
1206 * set AUTO_BW in intersected rule also. Next we will
1207 * calculate BW correctly in handle_channel function.
1208 * In other case remove AUTO_BW flag while we calculate
1209 * maximum bandwidth correctly and auto calculation is
1210 * not required.
1211 */
1212 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
1213 (rule2->flags & NL80211_RRF_AUTO_BW))
1214 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
1215 else
1216 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
1217
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001218 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1219 if (freq_range->max_bandwidth_khz > freq_diff)
1220 freq_range->max_bandwidth_khz = freq_diff;
1221
1222 power_rule->max_eirp = min(power_rule1->max_eirp,
1223 power_rule2->max_eirp);
1224 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
1225 power_rule2->max_antenna_gain);
1226
Janusz Dziedzic089027e2014-02-21 19:46:12 +01001227 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
1228 rule2->dfs_cac_ms);
1229
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001230 if (!is_valid_reg_rule(intersected_rule))
1231 return -EINVAL;
1232
1233 return 0;
1234}
1235
Eliad Pellera62a1ae2014-09-03 15:25:03 +03001236/* check whether old rule contains new rule */
1237static bool rule_contains(struct ieee80211_reg_rule *r1,
1238 struct ieee80211_reg_rule *r2)
1239{
1240 /* for simplicity, currently consider only same flags */
1241 if (r1->flags != r2->flags)
1242 return false;
1243
1244 /* verify r1 is more restrictive */
1245 if ((r1->power_rule.max_antenna_gain >
1246 r2->power_rule.max_antenna_gain) ||
1247 r1->power_rule.max_eirp > r2->power_rule.max_eirp)
1248 return false;
1249
1250 /* make sure r2's range is contained within r1 */
1251 if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
1252 r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
1253 return false;
1254
1255 /* and finally verify that r1.max_bw >= r2.max_bw */
1256 if (r1->freq_range.max_bandwidth_khz <
1257 r2->freq_range.max_bandwidth_khz)
1258 return false;
1259
1260 return true;
1261}
1262
1263/* add or extend current rules. do nothing if rule is already contained */
1264static void add_rule(struct ieee80211_reg_rule *rule,
1265 struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
1266{
1267 struct ieee80211_reg_rule *tmp_rule;
1268 int i;
1269
1270 for (i = 0; i < *n_rules; i++) {
1271 tmp_rule = &reg_rules[i];
1272 /* rule is already contained - do nothing */
1273 if (rule_contains(tmp_rule, rule))
1274 return;
1275
1276 /* extend rule if possible */
1277 if (rule_contains(rule, tmp_rule)) {
1278 memcpy(tmp_rule, rule, sizeof(*rule));
1279 return;
1280 }
1281 }
1282
1283 memcpy(&reg_rules[*n_rules], rule, sizeof(*rule));
1284 (*n_rules)++;
1285}
1286
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001287/**
1288 * regdom_intersect - do the intersection between two regulatory domains
1289 * @rd1: first regulatory domain
1290 * @rd2: second regulatory domain
1291 *
1292 * Use this function to get the intersection between two regulatory domains.
1293 * Once completed we will mark the alpha2 for the rd as intersected, "98",
1294 * as no one single alpha2 can represent this regulatory domain.
1295 *
1296 * Returns a pointer to the regulatory domain structure which will hold the
1297 * resulting intersection of rules between rd1 and rd2. We will
1298 * kzalloc() this structure for you.
1299 */
Johannes Berg1a919312012-12-03 17:21:11 +01001300static struct ieee80211_regdomain *
1301regdom_intersect(const struct ieee80211_regdomain *rd1,
1302 const struct ieee80211_regdomain *rd2)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001303{
1304 int r, size_of_regd;
1305 unsigned int x, y;
Eliad Pellera62a1ae2014-09-03 15:25:03 +03001306 unsigned int num_rules = 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001307 const struct ieee80211_reg_rule *rule1, *rule2;
Eliad Pellera62a1ae2014-09-03 15:25:03 +03001308 struct ieee80211_reg_rule intersected_rule;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001309 struct ieee80211_regdomain *rd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001310
1311 if (!rd1 || !rd2)
1312 return NULL;
1313
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001314 /*
1315 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001316 * build them. This is to so we can malloc() and free() a
1317 * regdomain once. The reason we use reg_rules_intersect() here
1318 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001319 * All rules that do check out OK are valid.
1320 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001321
1322 for (x = 0; x < rd1->n_reg_rules; x++) {
1323 rule1 = &rd1->reg_rules[x];
1324 for (y = 0; y < rd2->n_reg_rules; y++) {
1325 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +01001326 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
Eliad Pellera62a1ae2014-09-03 15:25:03 +03001327 &intersected_rule))
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001328 num_rules++;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001329 }
1330 }
1331
1332 if (!num_rules)
1333 return NULL;
1334
1335 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg82f20852012-12-04 12:49:16 +01001336 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001337
1338 rd = kzalloc(size_of_regd, GFP_KERNEL);
1339 if (!rd)
1340 return NULL;
1341
Eliad Pellera62a1ae2014-09-03 15:25:03 +03001342 for (x = 0; x < rd1->n_reg_rules; x++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001343 rule1 = &rd1->reg_rules[x];
Eliad Pellera62a1ae2014-09-03 15:25:03 +03001344 for (y = 0; y < rd2->n_reg_rules; y++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001345 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +01001346 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
Eliad Pellera62a1ae2014-09-03 15:25:03 +03001347 &intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001348 /*
1349 * No need to memset here the intersected rule here as
1350 * we're not using the stack anymore
1351 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001352 if (r)
1353 continue;
Eliad Pellera62a1ae2014-09-03 15:25:03 +03001354
1355 add_rule(&intersected_rule, rd->reg_rules,
1356 &rd->n_reg_rules);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001357 }
1358 }
1359
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001360 rd->alpha2[0] = '9';
1361 rd->alpha2[1] = '8';
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +01001362 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
1363 rd2->dfs_region);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001364
1365 return rd;
1366}
1367
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001368/*
1369 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1370 * want to just have the channel structure use these
1371 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001372static u32 map_regdom_flags(u32 rd_flags)
1373{
1374 u32 channel_flags = 0;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001375 if (rd_flags & NL80211_RRF_NO_IR_ALL)
1376 channel_flags |= IEEE80211_CHAN_NO_IR;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001377 if (rd_flags & NL80211_RRF_DFS)
1378 channel_flags |= IEEE80211_CHAN_RADAR;
Seth Forshee03f6b082012-08-01 15:58:42 -05001379 if (rd_flags & NL80211_RRF_NO_OFDM)
1380 channel_flags |= IEEE80211_CHAN_NO_OFDM;
David Spinadel570dbde2014-02-23 09:12:59 +02001381 if (rd_flags & NL80211_RRF_NO_OUTDOOR)
1382 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
Arik Nemtsov06f207f2015-05-06 16:28:31 +03001383 if (rd_flags & NL80211_RRF_IR_CONCURRENT)
1384 channel_flags |= IEEE80211_CHAN_IR_CONCURRENT;
Arik Nemtsova6d4a532014-10-23 09:37:33 +03001385 if (rd_flags & NL80211_RRF_NO_HT40MINUS)
1386 channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
1387 if (rd_flags & NL80211_RRF_NO_HT40PLUS)
1388 channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
1389 if (rd_flags & NL80211_RRF_NO_80MHZ)
1390 channel_flags |= IEEE80211_CHAN_NO_80MHZ;
1391 if (rd_flags & NL80211_RRF_NO_160MHZ)
1392 channel_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001393 return channel_flags;
1394}
1395
Johannes Berg361c9c82012-12-06 15:57:14 +01001396static const struct ieee80211_reg_rule *
Michal Sojka49172872015-11-23 19:27:14 +01001397freq_reg_info_regd(u32 center_freq,
Matthias May4edd5692015-07-17 15:28:39 +02001398 const struct ieee80211_regdomain *regd, u32 bw)
Johannes Berg8318d782008-01-24 19:38:38 +01001399{
1400 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001401 bool band_rule_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001402 bool bw_fits = false;
1403
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001404 if (!regd)
Johannes Berg361c9c82012-12-06 15:57:14 +01001405 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001406
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001407 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001408 const struct ieee80211_reg_rule *rr;
1409 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001410
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001411 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001412 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001413
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001414 /*
1415 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001416 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001417 * not overwrite it once found
1418 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001419 if (!band_rule_found)
1420 band_rule_found = freq_in_rule_band(fr, center_freq);
1421
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001422 bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001423
Johannes Berg361c9c82012-12-06 15:57:14 +01001424 if (band_rule_found && bw_fits)
1425 return rr;
Johannes Berg8318d782008-01-24 19:38:38 +01001426 }
1427
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001428 if (!band_rule_found)
Johannes Berg361c9c82012-12-06 15:57:14 +01001429 return ERR_PTR(-ERANGE);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001430
Johannes Berg361c9c82012-12-06 15:57:14 +01001431 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001432}
1433
Johannes Berg8de1c632015-08-21 14:13:06 +02001434static const struct ieee80211_reg_rule *
1435__freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw)
Matthias May4edd5692015-07-17 15:28:39 +02001436{
1437 const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy);
1438 const struct ieee80211_reg_rule *reg_rule = NULL;
1439 u32 bw;
1440
1441 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
Michal Sojka49172872015-11-23 19:27:14 +01001442 reg_rule = freq_reg_info_regd(center_freq, regd, bw);
Matthias May4edd5692015-07-17 15:28:39 +02001443 if (!IS_ERR(reg_rule))
1444 return reg_rule;
1445 }
1446
1447 return reg_rule;
1448}
1449
Johannes Berg361c9c82012-12-06 15:57:14 +01001450const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
1451 u32 center_freq)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001452{
Matthias May4edd5692015-07-17 15:28:39 +02001453 return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(20));
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001454}
John W. Linville4f366c52010-07-15 14:57:33 -04001455EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001456
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001457const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301458{
1459 switch (initiator) {
1460 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001461 return "core";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301462 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001463 return "user";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301464 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001465 return "driver";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301466 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001467 return "country IE";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301468 default:
1469 WARN_ON(1);
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001470 return "bug";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301471 }
1472}
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001473EXPORT_SYMBOL(reg_initiator_name);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301474
Michal Sojka1aeb1352015-11-23 19:27:16 +01001475static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd,
1476 const struct ieee80211_reg_rule *reg_rule,
1477 const struct ieee80211_channel *chan)
1478{
1479 const struct ieee80211_freq_range *freq_range = NULL;
1480 u32 max_bandwidth_khz, bw_flags = 0;
1481
1482 freq_range = &reg_rule->freq_range;
1483
1484 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1485 /* Check if auto calculation requested */
1486 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
1487 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1488
1489 /* If we get a reg_rule we can assume that at least 5Mhz fit */
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001490 if (!cfg80211_does_bw_fit_range(freq_range,
1491 MHZ_TO_KHZ(chan->center_freq),
1492 MHZ_TO_KHZ(10)))
Michal Sojka1aeb1352015-11-23 19:27:16 +01001493 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001494 if (!cfg80211_does_bw_fit_range(freq_range,
1495 MHZ_TO_KHZ(chan->center_freq),
1496 MHZ_TO_KHZ(20)))
Michal Sojka1aeb1352015-11-23 19:27:16 +01001497 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1498
1499 if (max_bandwidth_khz < MHZ_TO_KHZ(10))
1500 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1501 if (max_bandwidth_khz < MHZ_TO_KHZ(20))
1502 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1503 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
1504 bw_flags |= IEEE80211_CHAN_NO_HT40;
1505 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
1506 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
1507 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
1508 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
1509 return bw_flags;
1510}
1511
Johannes Berge33e2242014-06-23 11:06:16 +02001512/*
1513 * Note that right now we assume the desired channel bandwidth
1514 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1515 * per channel, the primary and the extension channel).
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001516 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001517static void handle_channel(struct wiphy *wiphy,
1518 enum nl80211_reg_initiator initiator,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001519 struct ieee80211_channel *chan)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001520{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001521 u32 flags, bw_flags = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001522 const struct ieee80211_reg_rule *reg_rule = NULL;
1523 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001524 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01001525 struct regulatory_request *lr = get_last_request();
Janusz Dziedzic97524822014-01-30 09:52:20 +01001526 const struct ieee80211_regdomain *regd;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001527
Johannes Bergc492db32012-12-06 16:29:25 +01001528 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001529
1530 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001531
Johannes Berg361c9c82012-12-06 15:57:14 +01001532 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1533 if (IS_ERR(reg_rule)) {
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001534 /*
1535 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001536 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001537 * from a Country IE and the Country IE had no information
1538 * about a band. The IEEE 802.11 spec allows for an AP
1539 * to send only a subset of the regulatory rules allowed,
1540 * so an AP in the US that only supports 2.4 GHz may only send
1541 * a country IE with information for the 2.4 GHz band
1542 * while 5 GHz is still supported.
1543 */
1544 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg361c9c82012-12-06 15:57:14 +01001545 PTR_ERR(reg_rule) == -ERANGE)
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001546 return;
1547
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001548 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1549 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001550 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001551 pr_debug("Disabling freq %d MHz for good\n",
1552 chan->center_freq);
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001553 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1554 chan->flags = chan->orig_flags;
1555 } else {
Johannes Bergc799ba62015-12-11 15:31:10 +01001556 pr_debug("Disabling freq %d MHz\n",
1557 chan->center_freq);
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001558 chan->flags |= IEEE80211_CHAN_DISABLED;
1559 }
Johannes Berg8318d782008-01-24 19:38:38 +01001560 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001561 }
Johannes Berg8318d782008-01-24 19:38:38 +01001562
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001563 regd = reg_get_regdomain(wiphy);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301564
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001565 power_rule = &reg_rule->power_rule;
Michal Sojka1aeb1352015-11-23 19:27:16 +01001566 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001567
Johannes Bergc492db32012-12-06 16:29:25 +01001568 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001569 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001570 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001571 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001572 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001573 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001574 * settings
1575 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001576 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001577 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001578 chan->max_antenna_gain = chan->orig_mag =
1579 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001580 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001581 (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic4f267c12014-04-09 13:47:12 +02001582
1583 if (chan->flags & IEEE80211_CHAN_RADAR) {
1584 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1585 if (reg_rule->dfs_cac_ms)
1586 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1587 }
1588
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001589 return;
1590 }
1591
Simon Wunderlich04f39042013-02-08 18:16:19 +01001592 chan->dfs_state = NL80211_DFS_USABLE;
1593 chan->dfs_state_entered = jiffies;
1594
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +05301595 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001596 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg1a919312012-12-03 17:21:11 +01001597 chan->max_antenna_gain =
1598 min_t(int, chan->orig_mag,
1599 MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +02001600 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic089027e2014-02-21 19:46:12 +01001601
1602 if (chan->flags & IEEE80211_CHAN_RADAR) {
1603 if (reg_rule->dfs_cac_ms)
1604 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1605 else
1606 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1607 }
1608
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001609 if (chan->orig_mpwr) {
1610 /*
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001611 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1612 * will always follow the passed country IE power settings.
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001613 */
1614 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001615 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001616 chan->max_power = chan->max_reg_power;
1617 else
1618 chan->max_power = min(chan->orig_mpwr,
1619 chan->max_reg_power);
1620 } else
1621 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +01001622}
1623
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001624static void handle_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001625 enum nl80211_reg_initiator initiator,
1626 struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +01001627{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001628 unsigned int i;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001629
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001630 if (!sband)
1631 return;
Johannes Berg8318d782008-01-24 19:38:38 +01001632
1633 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001634 handle_channel(wiphy, initiator, &sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +01001635}
1636
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001637static bool reg_request_cell_base(struct regulatory_request *request)
1638{
1639 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1640 return false;
Johannes Berg1a919312012-12-03 17:21:11 +01001641 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001642}
1643
1644bool reg_last_request_cell_base(void)
1645{
Johannes Berg38fd2142013-05-10 19:17:17 +02001646 return reg_request_cell_base(get_last_request());
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001647}
1648
Ilan Peer94fc6612014-02-23 09:13:00 +02001649#ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001650/* Core specific check */
Johannes Berg2f922122012-12-03 17:54:55 +01001651static enum reg_request_treatment
1652reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001653{
Johannes Bergc492db32012-12-06 16:29:25 +01001654 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001655
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001656 if (!reg_num_devs_support_basehint)
Johannes Berg2f922122012-12-03 17:54:55 +01001657 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001658
Johannes Bergc492db32012-12-06 16:29:25 +01001659 if (reg_request_cell_base(lr) &&
Johannes Berg1a919312012-12-03 17:21:11 +01001660 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001661 return REG_REQ_ALREADY_SET;
Johannes Berg1a919312012-12-03 17:21:11 +01001662
Johannes Berg2f922122012-12-03 17:54:55 +01001663 return REG_REQ_OK;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001664}
1665
1666/* Device specific check */
1667static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1668{
Johannes Berg1a919312012-12-03 17:21:11 +01001669 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001670}
1671#else
Johannes Berga515de62015-10-15 14:28:56 +02001672static enum reg_request_treatment
1673reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001674{
Johannes Berg2f922122012-12-03 17:54:55 +01001675 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001676}
Johannes Berg1a919312012-12-03 17:21:11 +01001677
1678static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001679{
1680 return true;
1681}
1682#endif
1683
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001684static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1685{
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001686 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1687 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001688 return true;
1689 return false;
1690}
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001691
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001692static bool ignore_reg_update(struct wiphy *wiphy,
1693 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001694{
Johannes Bergc492db32012-12-06 16:29:25 +01001695 struct regulatory_request *lr = get_last_request();
1696
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02001697 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1698 return true;
1699
Johannes Bergc492db32012-12-06 16:29:25 +01001700 if (!lr) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001701 pr_debug("Ignoring regulatory request set by %s since last_request is not set\n",
1702 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001703 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301704 }
1705
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001706 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001707 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001708 pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n",
1709 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001710 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301711 }
1712
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001713 /*
1714 * wiphy->regd will be set once the device has its own
1715 * desired regulatory domain set
1716 */
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001717 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -07001718 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Bergc492db32012-12-06 16:29:25 +01001719 !is_world_regdom(lr->alpha2)) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001720 pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n",
1721 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001722 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301723 }
1724
Johannes Bergc492db32012-12-06 16:29:25 +01001725 if (reg_request_cell_base(lr))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001726 return reg_dev_ignore_cell_hint(wiphy);
1727
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001728 return false;
1729}
1730
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001731static bool reg_is_world_roaming(struct wiphy *wiphy)
1732{
1733 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1734 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1735 struct regulatory_request *lr = get_last_request();
1736
1737 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1738 return true;
1739
1740 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001741 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001742 return true;
1743
1744 return false;
1745}
1746
Johannes Berg1a919312012-12-03 17:21:11 +01001747static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001748 struct reg_beacon *reg_beacon)
1749{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001750 struct ieee80211_supported_band *sband;
1751 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001752 bool channel_changed = false;
1753 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001754
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001755 sband = wiphy->bands[reg_beacon->chan.band];
1756 chan = &sband->channels[chan_idx];
1757
1758 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1759 return;
1760
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001761 if (chan->beacon_found)
1762 return;
1763
1764 chan->beacon_found = true;
1765
Luis R. Rodriguez0f500a52012-12-19 10:53:04 -08001766 if (!reg_is_world_roaming(wiphy))
1767 return;
1768
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001769 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001770 return;
1771
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001772 chan_before.center_freq = chan->center_freq;
1773 chan_before.flags = chan->flags;
1774
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001775 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1776 chan->flags &= ~IEEE80211_CHAN_NO_IR;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001777 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001778 }
1779
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001780 if (channel_changed)
1781 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001782}
1783
1784/*
1785 * Called when a scan on a wiphy finds a beacon on
1786 * new channel
1787 */
1788static void wiphy_update_new_beacon(struct wiphy *wiphy,
1789 struct reg_beacon *reg_beacon)
1790{
1791 unsigned int i;
1792 struct ieee80211_supported_band *sband;
1793
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001794 if (!wiphy->bands[reg_beacon->chan.band])
1795 return;
1796
1797 sband = wiphy->bands[reg_beacon->chan.band];
1798
1799 for (i = 0; i < sband->n_channels; i++)
1800 handle_reg_beacon(wiphy, i, reg_beacon);
1801}
1802
1803/*
1804 * Called upon reg changes or a new wiphy is added
1805 */
1806static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1807{
1808 unsigned int i;
1809 struct ieee80211_supported_band *sband;
1810 struct reg_beacon *reg_beacon;
1811
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001812 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1813 if (!wiphy->bands[reg_beacon->chan.band])
1814 continue;
1815 sband = wiphy->bands[reg_beacon->chan.band];
1816 for (i = 0; i < sband->n_channels; i++)
1817 handle_reg_beacon(wiphy, i, reg_beacon);
1818 }
1819}
1820
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001821/* Reap the advantages of previously found beacons */
1822static void reg_process_beacons(struct wiphy *wiphy)
1823{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001824 /*
1825 * Means we are just firing up cfg80211, so no beacons would
1826 * have been processed yet.
1827 */
1828 if (!last_request)
1829 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001830 wiphy_update_beacon_reg(wiphy);
1831}
1832
Johannes Berg1a919312012-12-03 17:21:11 +01001833static bool is_ht40_allowed(struct ieee80211_channel *chan)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001834{
1835 if (!chan)
Johannes Berg1a919312012-12-03 17:21:11 +01001836 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001837 if (chan->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg1a919312012-12-03 17:21:11 +01001838 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001839 /* This would happen when regulatory rules disallow HT40 completely */
Felix Fietkau55b183a2013-01-11 14:22:58 +01001840 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1841 return false;
1842 return true;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001843}
1844
1845static void reg_process_ht_flags_channel(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001846 struct ieee80211_channel *channel)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001847{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001848 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001849 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001850 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001851 unsigned int i;
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001852 u32 flags;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001853
Johannes Berg1a919312012-12-03 17:21:11 +01001854 if (!is_ht40_allowed(channel)) {
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001855 channel->flags |= IEEE80211_CHAN_NO_HT40;
1856 return;
1857 }
1858
1859 /*
1860 * We need to ensure the extension channels exist to
1861 * be able to use HT40- or HT40+, this finds them (or not)
1862 */
1863 for (i = 0; i < sband->n_channels; i++) {
1864 struct ieee80211_channel *c = &sband->channels[i];
Johannes Berg1a919312012-12-03 17:21:11 +01001865
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001866 if (c->center_freq == (channel->center_freq - 20))
1867 channel_before = c;
1868 if (c->center_freq == (channel->center_freq + 20))
1869 channel_after = c;
1870 }
1871
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001872 flags = 0;
1873 regd = get_wiphy_regdom(wiphy);
1874 if (regd) {
1875 const struct ieee80211_reg_rule *reg_rule =
1876 freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq),
1877 regd, MHZ_TO_KHZ(20));
1878
1879 if (!IS_ERR(reg_rule))
1880 flags = reg_rule->flags;
1881 }
1882
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001883 /*
1884 * Please note that this assumes target bandwidth is 20 MHz,
1885 * if that ever changes we also need to change the below logic
1886 * to include that as well.
1887 */
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001888 if (!is_ht40_allowed(channel_before) ||
1889 flags & NL80211_RRF_NO_HT40MINUS)
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001890 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001891 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001892 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001893
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001894 if (!is_ht40_allowed(channel_after) ||
1895 flags & NL80211_RRF_NO_HT40PLUS)
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001896 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001897 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001898 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001899}
1900
1901static void reg_process_ht_flags_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001902 struct ieee80211_supported_band *sband)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001903{
1904 unsigned int i;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001905
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001906 if (!sband)
1907 return;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001908
1909 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001910 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001911}
1912
1913static void reg_process_ht_flags(struct wiphy *wiphy)
1914{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001915 enum nl80211_band band;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001916
1917 if (!wiphy)
1918 return;
1919
Johannes Berg57fbcce2016-04-12 15:56:15 +02001920 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001921 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001922}
1923
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001924static void reg_call_notifier(struct wiphy *wiphy,
1925 struct regulatory_request *request)
1926{
1927 if (wiphy->reg_notifier)
1928 wiphy->reg_notifier(wiphy, request);
1929}
1930
Arik Nemtsovad932f02014-11-27 09:44:55 +02001931static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev)
1932{
Arik Nemtsovad932f02014-11-27 09:44:55 +02001933 struct cfg80211_chan_def chandef;
1934 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Arik Nemtsov20658702014-12-29 11:59:59 +02001935 enum nl80211_iftype iftype;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001936
1937 wdev_lock(wdev);
Arik Nemtsov20658702014-12-29 11:59:59 +02001938 iftype = wdev->iftype;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001939
Arik Nemtsov20658702014-12-29 11:59:59 +02001940 /* make sure the interface is active */
Arik Nemtsovad932f02014-11-27 09:44:55 +02001941 if (!wdev->netdev || !netif_running(wdev->netdev))
Arik Nemtsov20658702014-12-29 11:59:59 +02001942 goto wdev_inactive_unlock;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001943
Arik Nemtsov20658702014-12-29 11:59:59 +02001944 switch (iftype) {
Arik Nemtsovad932f02014-11-27 09:44:55 +02001945 case NL80211_IFTYPE_AP:
1946 case NL80211_IFTYPE_P2P_GO:
1947 if (!wdev->beacon_interval)
Arik Nemtsov20658702014-12-29 11:59:59 +02001948 goto wdev_inactive_unlock;
1949 chandef = wdev->chandef;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001950 break;
Arik Nemtsov185076d2014-12-03 18:08:17 +02001951 case NL80211_IFTYPE_ADHOC:
1952 if (!wdev->ssid_len)
Arik Nemtsov20658702014-12-29 11:59:59 +02001953 goto wdev_inactive_unlock;
1954 chandef = wdev->chandef;
Arik Nemtsov185076d2014-12-03 18:08:17 +02001955 break;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001956 case NL80211_IFTYPE_STATION:
1957 case NL80211_IFTYPE_P2P_CLIENT:
Arik Nemtsovad932f02014-11-27 09:44:55 +02001958 if (!wdev->current_bss ||
1959 !wdev->current_bss->pub.channel)
Arik Nemtsov20658702014-12-29 11:59:59 +02001960 goto wdev_inactive_unlock;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001961
Arik Nemtsov20658702014-12-29 11:59:59 +02001962 if (!rdev->ops->get_channel ||
1963 rdev_get_channel(rdev, wdev, &chandef))
1964 cfg80211_chandef_create(&chandef,
1965 wdev->current_bss->pub.channel,
1966 NL80211_CHAN_NO_HT);
Arik Nemtsovad932f02014-11-27 09:44:55 +02001967 break;
1968 case NL80211_IFTYPE_MONITOR:
1969 case NL80211_IFTYPE_AP_VLAN:
1970 case NL80211_IFTYPE_P2P_DEVICE:
1971 /* no enforcement required */
1972 break;
1973 default:
1974 /* others not implemented for now */
1975 WARN_ON(1);
1976 break;
1977 }
1978
Arik Nemtsovad932f02014-11-27 09:44:55 +02001979 wdev_unlock(wdev);
Arik Nemtsov20658702014-12-29 11:59:59 +02001980
1981 switch (iftype) {
1982 case NL80211_IFTYPE_AP:
1983 case NL80211_IFTYPE_P2P_GO:
1984 case NL80211_IFTYPE_ADHOC:
Arik Nemtsov923b3522015-07-08 15:41:44 +03001985 return cfg80211_reg_can_beacon_relax(wiphy, &chandef, iftype);
Arik Nemtsov20658702014-12-29 11:59:59 +02001986 case NL80211_IFTYPE_STATION:
1987 case NL80211_IFTYPE_P2P_CLIENT:
1988 return cfg80211_chandef_usable(wiphy, &chandef,
1989 IEEE80211_CHAN_DISABLED);
1990 default:
1991 break;
1992 }
1993
1994 return true;
1995
1996wdev_inactive_unlock:
1997 wdev_unlock(wdev);
1998 return true;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001999}
2000
2001static void reg_leave_invalid_chans(struct wiphy *wiphy)
2002{
2003 struct wireless_dev *wdev;
2004 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
2005
2006 ASSERT_RTNL();
2007
Johannes Berg53873f12016-05-03 16:52:04 +03002008 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
Arik Nemtsovad932f02014-11-27 09:44:55 +02002009 if (!reg_wdev_chan_valid(wiphy, wdev))
2010 cfg80211_leave(rdev, wdev);
2011}
2012
2013static void reg_check_chans_work(struct work_struct *work)
2014{
2015 struct cfg80211_registered_device *rdev;
2016
Johannes Bergc799ba62015-12-11 15:31:10 +01002017 pr_debug("Verifying active interfaces after reg change\n");
Arik Nemtsovad932f02014-11-27 09:44:55 +02002018 rtnl_lock();
2019
2020 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2021 if (!(rdev->wiphy.regulatory_flags &
2022 REGULATORY_IGNORE_STALE_KICKOFF))
2023 reg_leave_invalid_chans(&rdev->wiphy);
2024
2025 rtnl_unlock();
2026}
2027
2028static void reg_check_channels(void)
2029{
2030 /*
2031 * Give usermode a chance to do something nicer (move to another
2032 * channel, orderly disconnection), before forcing a disconnection.
2033 */
2034 mod_delayed_work(system_power_efficient_wq,
2035 &reg_check_chans,
2036 msecs_to_jiffies(REG_ENFORCE_GRACE_MS));
2037}
2038
Sven Neumanneac03e32011-08-30 23:38:53 +02002039static void wiphy_update_regulatory(struct wiphy *wiphy,
2040 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01002041{
Johannes Berg57fbcce2016-04-12 15:56:15 +02002042 enum nl80211_band band;
Johannes Bergc492db32012-12-06 16:29:25 +01002043 struct regulatory_request *lr = get_last_request();
Sven Neumanneac03e32011-08-30 23:38:53 +02002044
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08002045 if (ignore_reg_update(wiphy, initiator)) {
2046 /*
2047 * Regulatory updates set by CORE are ignored for custom
2048 * regulatory cards. Let us notify the changes to the driver,
2049 * as some drivers used this to restore its orig_* reg domain.
2050 */
2051 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01002052 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08002053 reg_call_notifier(wiphy, lr);
Sven Neumanna203c2a2011-07-12 15:52:07 +02002054 return;
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08002055 }
Sven Neumanna203c2a2011-07-12 15:52:07 +02002056
Johannes Bergc492db32012-12-06 16:29:25 +01002057 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07002058
Johannes Berg57fbcce2016-04-12 15:56:15 +02002059 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002060 handle_band(wiphy, initiator, wiphy->bands[band]);
Sven Neumanna203c2a2011-07-12 15:52:07 +02002061
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002062 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04002063 reg_process_ht_flags(wiphy);
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08002064 reg_call_notifier(wiphy, lr);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002065}
2066
Sven Neumannd7549cb2011-08-30 23:38:54 +02002067static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
2068{
2069 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05302070 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02002071
Johannes Berg5fe231e2013-05-08 21:45:15 +02002072 ASSERT_RTNL();
Johannes Berg458f4f92012-12-06 15:47:38 +01002073
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05302074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2075 wiphy = &rdev->wiphy;
2076 wiphy_update_regulatory(wiphy, initiator);
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05302077 }
Arik Nemtsovad932f02014-11-27 09:44:55 +02002078
2079 reg_check_channels();
Sven Neumannd7549cb2011-08-30 23:38:54 +02002080}
2081
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002082static void handle_channel_custom(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002083 struct ieee80211_channel *chan,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002084 const struct ieee80211_regdomain *regd)
2085{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04002086 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002087 const struct ieee80211_reg_rule *reg_rule = NULL;
2088 const struct ieee80211_power_rule *power_rule = NULL;
Matthias May4edd5692015-07-17 15:28:39 +02002089 u32 bw;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002090
Matthias May4edd5692015-07-17 15:28:39 +02002091 for (bw = MHZ_TO_KHZ(20); bw >= MHZ_TO_KHZ(5); bw = bw / 2) {
Michal Sojka49172872015-11-23 19:27:14 +01002092 reg_rule = freq_reg_info_regd(MHZ_TO_KHZ(chan->center_freq),
Matthias May4edd5692015-07-17 15:28:39 +02002093 regd, bw);
2094 if (!IS_ERR(reg_rule))
2095 break;
2096 }
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04002097
Johannes Berg361c9c82012-12-06 15:57:14 +01002098 if (IS_ERR(reg_rule)) {
Johannes Bergc799ba62015-12-11 15:31:10 +01002099 pr_debug("Disabling freq %d MHz as custom regd has no rule that fits it\n",
2100 chan->center_freq);
Arik Nemtsovdb8dfee2014-12-15 19:26:02 +02002101 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
2102 chan->flags |= IEEE80211_CHAN_DISABLED;
2103 } else {
2104 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
2105 chan->flags = chan->orig_flags;
2106 }
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002107 return;
2108 }
2109
2110 power_rule = &reg_rule->power_rule;
Michal Sojka1aeb1352015-11-23 19:27:16 +01002111 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04002112
Arik Nemtsov2e18b382014-11-16 16:37:46 +02002113 chan->dfs_state_entered = jiffies;
Arik Nemtsovc7ab5082014-11-16 16:37:47 +02002114 chan->dfs_state = NL80211_DFS_USABLE;
2115
2116 chan->beacon_found = false;
Arik Nemtsovdb8dfee2014-12-15 19:26:02 +02002117
2118 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2119 chan->flags = chan->orig_flags | bw_flags |
2120 map_regdom_flags(reg_rule->flags);
2121 else
2122 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
2123
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002124 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02002125 chan->max_reg_power = chan->max_power =
2126 (int) MBM_TO_DBM(power_rule->max_eirp);
Arik Nemtsov2e18b382014-11-16 16:37:46 +02002127
2128 if (chan->flags & IEEE80211_CHAN_RADAR) {
2129 if (reg_rule->dfs_cac_ms)
2130 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
2131 else
2132 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
2133 }
2134
2135 chan->max_power = chan->max_reg_power;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002136}
2137
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002138static void handle_band_custom(struct wiphy *wiphy,
2139 struct ieee80211_supported_band *sband,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002140 const struct ieee80211_regdomain *regd)
2141{
2142 unsigned int i;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002143
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002144 if (!sband)
2145 return;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002146
2147 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002148 handle_channel_custom(wiphy, &sband->channels[i], regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002149}
2150
2151/* Used by drivers prior to wiphy registration */
2152void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
2153 const struct ieee80211_regdomain *regd)
2154{
Johannes Berg57fbcce2016-04-12 15:56:15 +02002155 enum nl80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04002156 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04002157
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01002158 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
2159 "wiphy should have REGULATORY_CUSTOM_REG\n");
2160 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Luis R. Rodriguez222ea582013-11-05 09:18:00 -08002161
Johannes Berg57fbcce2016-04-12 15:56:15 +02002162 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04002163 if (!wiphy->bands[band])
2164 continue;
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002165 handle_band_custom(wiphy, wiphy->bands[band], regd);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04002166 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002167 }
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04002168
2169 /*
2170 * no point in calling this if it won't have any effect
Johannes Berg1a919312012-12-03 17:21:11 +01002171 * on your device's supported bands.
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04002172 */
2173 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08002174}
2175EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
2176
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002177static void reg_set_request_processed(void)
2178{
2179 bool need_more_processing = false;
Johannes Bergc492db32012-12-06 16:29:25 +01002180 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002181
Johannes Bergc492db32012-12-06 16:29:25 +01002182 lr->processed = true;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002183
2184 spin_lock(&reg_requests_lock);
2185 if (!list_empty(&reg_requests_list))
2186 need_more_processing = true;
2187 spin_unlock(&reg_requests_lock);
2188
Johannes Bergb6863032015-10-15 09:25:18 +02002189 cancel_crda_timeout();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002190
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002191 if (need_more_processing)
2192 schedule_work(&reg_work);
2193}
2194
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002195/**
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002196 * reg_process_hint_core - process core regulatory requests
2197 * @pending_request: a pending core regulatory request
2198 *
2199 * The wireless subsystem can use this function to process
2200 * a regulatory request issued by the regulatory core.
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002201 */
Johannes Bergd34265a2015-10-15 13:05:55 +02002202static enum reg_request_treatment
2203reg_process_hint_core(struct regulatory_request *core_request)
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002204{
Johannes Bergcecbb062015-10-15 08:47:34 +02002205 if (reg_query_database(core_request)) {
Johannes Berg25b20db2015-10-15 12:05:05 +02002206 core_request->intersect = false;
2207 core_request->processed = false;
2208 reg_update_last_request(core_request);
Johannes Bergd34265a2015-10-15 13:05:55 +02002209 return REG_REQ_OK;
Johannes Berg25b20db2015-10-15 12:05:05 +02002210 }
Johannes Bergd34265a2015-10-15 13:05:55 +02002211
2212 return REG_REQ_IGNORE;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002213}
2214
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002215static enum reg_request_treatment
2216__reg_process_hint_user(struct regulatory_request *user_request)
2217{
2218 struct regulatory_request *lr = get_last_request();
2219
2220 if (reg_request_cell_base(user_request))
2221 return reg_ignore_cell_hint(user_request);
2222
2223 if (reg_request_cell_base(lr))
2224 return REG_REQ_IGNORE;
2225
2226 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
2227 return REG_REQ_INTERSECT;
2228 /*
2229 * If the user knows better the user should set the regdom
2230 * to their country before the IE is picked up
2231 */
2232 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
2233 lr->intersect)
2234 return REG_REQ_IGNORE;
2235 /*
2236 * Process user requests only after previous user/driver/core
2237 * requests have been processed
2238 */
2239 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
2240 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
2241 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
2242 regdom_changes(lr->alpha2))
2243 return REG_REQ_IGNORE;
2244
2245 if (!regdom_changes(user_request->alpha2))
2246 return REG_REQ_ALREADY_SET;
2247
2248 return REG_REQ_OK;
2249}
2250
2251/**
2252 * reg_process_hint_user - process user regulatory requests
2253 * @user_request: a pending user regulatory request
2254 *
2255 * The wireless subsystem can use this function to process
2256 * a regulatory request initiated by userspace.
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002257 */
Johannes Bergd34265a2015-10-15 13:05:55 +02002258static enum reg_request_treatment
2259reg_process_hint_user(struct regulatory_request *user_request)
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002260{
2261 enum reg_request_treatment treatment;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002262
2263 treatment = __reg_process_hint_user(user_request);
2264 if (treatment == REG_REQ_IGNORE ||
Johannes Bergd34265a2015-10-15 13:05:55 +02002265 treatment == REG_REQ_ALREADY_SET)
2266 return REG_REQ_IGNORE;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002267
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002268 user_request->intersect = treatment == REG_REQ_INTERSECT;
2269 user_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08002270
Johannes Bergcecbb062015-10-15 08:47:34 +02002271 if (reg_query_database(user_request)) {
Johannes Berg25b20db2015-10-15 12:05:05 +02002272 reg_update_last_request(user_request);
2273 user_alpha2[0] = user_request->alpha2[0];
2274 user_alpha2[1] = user_request->alpha2[1];
Johannes Bergd34265a2015-10-15 13:05:55 +02002275 return REG_REQ_OK;
Johannes Berg25b20db2015-10-15 12:05:05 +02002276 }
Johannes Bergd34265a2015-10-15 13:05:55 +02002277
2278 return REG_REQ_IGNORE;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08002279}
2280
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002281static enum reg_request_treatment
2282__reg_process_hint_driver(struct regulatory_request *driver_request)
2283{
2284 struct regulatory_request *lr = get_last_request();
2285
2286 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
2287 if (regdom_changes(driver_request->alpha2))
2288 return REG_REQ_OK;
2289 return REG_REQ_ALREADY_SET;
2290 }
2291
2292 /*
2293 * This would happen if you unplug and plug your card
2294 * back in or if you add a new device for which the previously
2295 * loaded card also agrees on the regulatory domain.
2296 */
2297 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
2298 !regdom_changes(driver_request->alpha2))
2299 return REG_REQ_ALREADY_SET;
2300
2301 return REG_REQ_INTERSECT;
2302}
2303
2304/**
2305 * reg_process_hint_driver - process driver regulatory requests
2306 * @driver_request: a pending driver regulatory request
2307 *
2308 * The wireless subsystem can use this function to process
2309 * a regulatory request issued by an 802.11 driver.
2310 *
2311 * Returns one of the different reg request treatment values.
2312 */
2313static enum reg_request_treatment
2314reg_process_hint_driver(struct wiphy *wiphy,
2315 struct regulatory_request *driver_request)
2316{
Arik Nemtsov34f05f52014-12-04 12:22:16 +02002317 const struct ieee80211_regdomain *regd, *tmp;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002318 enum reg_request_treatment treatment;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002319
2320 treatment = __reg_process_hint_driver(driver_request);
2321
2322 switch (treatment) {
2323 case REG_REQ_OK:
2324 break;
2325 case REG_REQ_IGNORE:
Johannes Bergd34265a2015-10-15 13:05:55 +02002326 return REG_REQ_IGNORE;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002327 case REG_REQ_INTERSECT:
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002328 case REG_REQ_ALREADY_SET:
2329 regd = reg_copy_regd(get_cfg80211_regdom());
Johannes Bergd34265a2015-10-15 13:05:55 +02002330 if (IS_ERR(regd))
2331 return REG_REQ_IGNORE;
Arik Nemtsov34f05f52014-12-04 12:22:16 +02002332
2333 tmp = get_wiphy_regdom(wiphy);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002334 rcu_assign_pointer(wiphy->regd, regd);
Arik Nemtsov34f05f52014-12-04 12:22:16 +02002335 rcu_free_regdom(tmp);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002336 }
2337
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002338
2339 driver_request->intersect = treatment == REG_REQ_INTERSECT;
2340 driver_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08002341
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002342 /*
2343 * Since CRDA will not be called in this case as we already
2344 * have applied the requested regulatory domain before we just
2345 * inform userspace we have processed the request
2346 */
2347 if (treatment == REG_REQ_ALREADY_SET) {
2348 nl80211_send_reg_change_event(driver_request);
Johannes Berg25b20db2015-10-15 12:05:05 +02002349 reg_update_last_request(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002350 reg_set_request_processed();
Johannes Berg480908a2015-10-15 12:58:58 +02002351 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002352 }
2353
Johannes Bergd34265a2015-10-15 13:05:55 +02002354 if (reg_query_database(driver_request)) {
Johannes Berg25b20db2015-10-15 12:05:05 +02002355 reg_update_last_request(driver_request);
Johannes Bergd34265a2015-10-15 13:05:55 +02002356 return REG_REQ_OK;
2357 }
Johannes Berg25b20db2015-10-15 12:05:05 +02002358
Johannes Bergd34265a2015-10-15 13:05:55 +02002359 return REG_REQ_IGNORE;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002360}
2361
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002362static enum reg_request_treatment
2363__reg_process_hint_country_ie(struct wiphy *wiphy,
2364 struct regulatory_request *country_ie_request)
2365{
2366 struct wiphy *last_wiphy = NULL;
2367 struct regulatory_request *lr = get_last_request();
2368
2369 if (reg_request_cell_base(lr)) {
2370 /* Trust a Cell base station over the AP's country IE */
2371 if (regdom_changes(country_ie_request->alpha2))
2372 return REG_REQ_IGNORE;
2373 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez2a901462013-11-11 22:15:31 +01002374 } else {
2375 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
2376 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002377 }
2378
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002379 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
2380 return -EINVAL;
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002381
2382 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
2383 return REG_REQ_OK;
2384
2385 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
2386
2387 if (last_wiphy != wiphy) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002388 /*
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002389 * Two cards with two APs claiming different
2390 * Country IE alpha2s. We could
2391 * intersect them, but that seems unlikely
2392 * to be correct. Reject second one for now.
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002393 */
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002394 if (regdom_changes(country_ie_request->alpha2))
2395 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002396 return REG_REQ_ALREADY_SET;
2397 }
Emmanuel Grumbach70dcec52014-12-02 09:53:25 +02002398
2399 if (regdom_changes(country_ie_request->alpha2))
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002400 return REG_REQ_OK;
2401 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002402}
2403
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002404/**
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002405 * reg_process_hint_country_ie - process regulatory requests from country IEs
2406 * @country_ie_request: a regulatory request from a country IE
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002407 *
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002408 * The wireless subsystem can use this function to process
2409 * a regulatory request issued by a country Information Element.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002410 *
Johannes Berg2f922122012-12-03 17:54:55 +01002411 * Returns one of the different reg request treatment values.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002412 */
Johannes Berg2f922122012-12-03 17:54:55 +01002413static enum reg_request_treatment
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002414reg_process_hint_country_ie(struct wiphy *wiphy,
2415 struct regulatory_request *country_ie_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002416{
Johannes Berg2f922122012-12-03 17:54:55 +01002417 enum reg_request_treatment treatment;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002418
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002419 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002420
Johannes Berg2f922122012-12-03 17:54:55 +01002421 switch (treatment) {
Johannes Berg2f922122012-12-03 17:54:55 +01002422 case REG_REQ_OK:
2423 break;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002424 case REG_REQ_IGNORE:
Johannes Bergd34265a2015-10-15 13:05:55 +02002425 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002426 case REG_REQ_ALREADY_SET:
Arik Nemtsovc8883932014-04-21 20:39:34 -07002427 reg_free_request(country_ie_request);
Johannes Berg480908a2015-10-15 12:58:58 +02002428 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002429 case REG_REQ_INTERSECT:
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002430 /*
2431 * This doesn't happen yet, not sure we
2432 * ever want to support it for this case.
2433 */
2434 WARN_ONCE(1, "Unexpected intersection for country IEs");
Johannes Bergd34265a2015-10-15 13:05:55 +02002435 return REG_REQ_IGNORE;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002436 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002437
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002438 country_ie_request->intersect = false;
2439 country_ie_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08002440
Johannes Bergd34265a2015-10-15 13:05:55 +02002441 if (reg_query_database(country_ie_request)) {
Johannes Berg25b20db2015-10-15 12:05:05 +02002442 reg_update_last_request(country_ie_request);
Johannes Bergd34265a2015-10-15 13:05:55 +02002443 return REG_REQ_OK;
2444 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05002445
Johannes Bergd34265a2015-10-15 13:05:55 +02002446 return REG_REQ_IGNORE;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002447}
2448
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05302449bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2)
2450{
2451 const struct ieee80211_regdomain *wiphy1_regd = NULL;
2452 const struct ieee80211_regdomain *wiphy2_regd = NULL;
2453 const struct ieee80211_regdomain *cfg80211_regd = NULL;
2454 bool dfs_domain_same;
2455
2456 rcu_read_lock();
2457
2458 cfg80211_regd = rcu_dereference(cfg80211_regdomain);
2459 wiphy1_regd = rcu_dereference(wiphy1->regd);
2460 if (!wiphy1_regd)
2461 wiphy1_regd = cfg80211_regd;
2462
2463 wiphy2_regd = rcu_dereference(wiphy2->regd);
2464 if (!wiphy2_regd)
2465 wiphy2_regd = cfg80211_regd;
2466
2467 dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region;
2468
2469 rcu_read_unlock();
2470
2471 return dfs_domain_same;
2472}
2473
2474static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan,
2475 struct ieee80211_channel *src_chan)
2476{
2477 if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) ||
2478 !(src_chan->flags & IEEE80211_CHAN_RADAR))
2479 return;
2480
2481 if (dst_chan->flags & IEEE80211_CHAN_DISABLED ||
2482 src_chan->flags & IEEE80211_CHAN_DISABLED)
2483 return;
2484
2485 if (src_chan->center_freq == dst_chan->center_freq &&
2486 dst_chan->dfs_state == NL80211_DFS_USABLE) {
2487 dst_chan->dfs_state = src_chan->dfs_state;
2488 dst_chan->dfs_state_entered = src_chan->dfs_state_entered;
2489 }
2490}
2491
2492static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy,
2493 struct wiphy *src_wiphy)
2494{
2495 struct ieee80211_supported_band *src_sband, *dst_sband;
2496 struct ieee80211_channel *src_chan, *dst_chan;
2497 int i, j, band;
2498
2499 if (!reg_dfs_domain_same(dst_wiphy, src_wiphy))
2500 return;
2501
2502 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2503 dst_sband = dst_wiphy->bands[band];
2504 src_sband = src_wiphy->bands[band];
2505 if (!dst_sband || !src_sband)
2506 continue;
2507
2508 for (i = 0; i < dst_sband->n_channels; i++) {
2509 dst_chan = &dst_sband->channels[i];
2510 for (j = 0; j < src_sband->n_channels; j++) {
2511 src_chan = &src_sband->channels[j];
2512 reg_copy_dfs_chan_state(dst_chan, src_chan);
2513 }
2514 }
2515 }
2516}
2517
2518static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy)
2519{
2520 struct cfg80211_registered_device *rdev;
2521
2522 ASSERT_RTNL();
2523
2524 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2525 if (wiphy == &rdev->wiphy)
2526 continue;
2527 wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy);
2528 }
2529}
2530
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04002531/* This processes *all* regulatory hints */
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08002532static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002533{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002534 struct wiphy *wiphy = NULL;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002535 enum reg_request_treatment treatment;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002536
Johannes Bergf4173762012-12-03 18:23:37 +01002537 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002538 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
2539
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002540 switch (reg_request->initiator) {
2541 case NL80211_REGDOM_SET_BY_CORE:
Johannes Bergd34265a2015-10-15 13:05:55 +02002542 treatment = reg_process_hint_core(reg_request);
2543 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002544 case NL80211_REGDOM_SET_BY_USER:
Johannes Bergd34265a2015-10-15 13:05:55 +02002545 treatment = reg_process_hint_user(reg_request);
2546 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002547 case NL80211_REGDOM_SET_BY_DRIVER:
Ilan Peer772f0382014-01-14 15:17:23 +02002548 if (!wiphy)
2549 goto out_free;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002550 treatment = reg_process_hint_driver(wiphy, reg_request);
2551 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002552 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Ilan Peer772f0382014-01-14 15:17:23 +02002553 if (!wiphy)
2554 goto out_free;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002555 treatment = reg_process_hint_country_ie(wiphy, reg_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002556 break;
2557 default:
2558 WARN(1, "invalid initiator %d\n", reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02002559 goto out_free;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002560 }
2561
Johannes Bergd34265a2015-10-15 13:05:55 +02002562 if (treatment == REG_REQ_IGNORE)
2563 goto out_free;
2564
Johannes Berg480908a2015-10-15 12:58:58 +02002565 WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET,
2566 "unexpected treatment value %d\n", treatment);
2567
John Linville841b3512015-06-24 11:42:25 -04002568 /* This is required so that the orig_* parameters are saved.
2569 * NOTE: treatment must be set for any case that reaches here!
2570 */
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002571 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
Arik Nemtsovad932f02014-11-27 09:44:55 +02002572 wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002573 wiphy_update_regulatory(wiphy, reg_request->initiator);
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05302574 wiphy_all_share_dfs_chan_state(wiphy);
Arik Nemtsovad932f02014-11-27 09:44:55 +02002575 reg_check_channels();
2576 }
Ilan Peer772f0382014-01-14 15:17:23 +02002577
2578 return;
2579
2580out_free:
Arik Nemtsovc8883932014-04-21 20:39:34 -07002581 reg_free_request(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002582}
2583
Arik Nemtsovef51fb12015-01-07 16:47:20 +02002584static bool reg_only_self_managed_wiphys(void)
2585{
2586 struct cfg80211_registered_device *rdev;
2587 struct wiphy *wiphy;
2588 bool self_managed_found = false;
2589
2590 ASSERT_RTNL();
2591
2592 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2593 wiphy = &rdev->wiphy;
2594 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2595 self_managed_found = true;
2596 else
2597 return false;
2598 }
2599
2600 /* make sure at least one self-managed wiphy exists */
2601 return self_managed_found;
2602}
2603
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002604/*
2605 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
2606 * Regulatory hints come on a first come first serve basis and we
2607 * must process each one atomically.
2608 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002609static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002610{
Johannes Bergc492db32012-12-06 16:29:25 +01002611 struct regulatory_request *reg_request, *lr;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002612
Johannes Bergc492db32012-12-06 16:29:25 +01002613 lr = get_last_request();
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002614
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002615 /* When last_request->processed becomes true this will be rescheduled */
Johannes Bergc492db32012-12-06 16:29:25 +01002616 if (lr && !lr->processed) {
Luis R. Rodriguez96cce122014-04-21 20:39:35 -07002617 reg_process_hint(lr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002618 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002619 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002620
2621 spin_lock(&reg_requests_lock);
2622
2623 if (list_empty(&reg_requests_list)) {
2624 spin_unlock(&reg_requests_lock);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002625 return;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002626 }
2627
2628 reg_request = list_first_entry(&reg_requests_list,
2629 struct regulatory_request,
2630 list);
2631 list_del_init(&reg_request->list);
2632
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002633 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002634
Arik Nemtsovef51fb12015-01-07 16:47:20 +02002635 if (reg_only_self_managed_wiphys()) {
2636 reg_free_request(reg_request);
2637 return;
2638 }
2639
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08002640 reg_process_hint(reg_request);
Ben2e54a682015-03-12 09:37:34 -04002641
2642 lr = get_last_request();
2643
2644 spin_lock(&reg_requests_lock);
2645 if (!list_empty(&reg_requests_list) && lr && lr->processed)
2646 schedule_work(&reg_work);
2647 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002648}
2649
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002650/* Processes beacon hints -- this has nothing to do with country IEs */
2651static void reg_process_pending_beacon_hints(void)
2652{
Johannes Berg79c97e92009-07-07 03:56:12 +02002653 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002654 struct reg_beacon *pending_beacon, *tmp;
2655
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002656 /* This goes through the _pending_ beacon list */
2657 spin_lock_bh(&reg_pending_beacons_lock);
2658
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002659 list_for_each_entry_safe(pending_beacon, tmp,
2660 &reg_pending_beacons, list) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002661 list_del_init(&pending_beacon->list);
2662
2663 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02002664 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2665 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002666
2667 /* Remembers the beacon hint for new wiphys or reg changes */
2668 list_add_tail(&pending_beacon->list, &reg_beacon_list);
2669 }
2670
2671 spin_unlock_bh(&reg_pending_beacons_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002672}
2673
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002674static void reg_process_self_managed_hints(void)
2675{
2676 struct cfg80211_registered_device *rdev;
2677 struct wiphy *wiphy;
2678 const struct ieee80211_regdomain *tmp;
2679 const struct ieee80211_regdomain *regd;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002680 enum nl80211_band band;
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002681 struct regulatory_request request = {};
2682
2683 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2684 wiphy = &rdev->wiphy;
2685
2686 spin_lock(&reg_requests_lock);
2687 regd = rdev->requested_regd;
2688 rdev->requested_regd = NULL;
2689 spin_unlock(&reg_requests_lock);
2690
2691 if (regd == NULL)
2692 continue;
2693
2694 tmp = get_wiphy_regdom(wiphy);
2695 rcu_assign_pointer(wiphy->regd, regd);
2696 rcu_free_regdom(tmp);
2697
Johannes Berg57fbcce2016-04-12 15:56:15 +02002698 for (band = 0; band < NUM_NL80211_BANDS; band++)
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002699 handle_band_custom(wiphy, wiphy->bands[band], regd);
2700
2701 reg_process_ht_flags(wiphy);
2702
2703 request.wiphy_idx = get_wiphy_idx(wiphy);
2704 request.alpha2[0] = regd->alpha2[0];
2705 request.alpha2[1] = regd->alpha2[1];
2706 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
2707
2708 nl80211_send_wiphy_reg_change_event(&request);
2709 }
2710
2711 reg_check_channels();
2712}
2713
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002714static void reg_todo(struct work_struct *work)
2715{
Johannes Berg5fe231e2013-05-08 21:45:15 +02002716 rtnl_lock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002717 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002718 reg_process_pending_beacon_hints();
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002719 reg_process_self_managed_hints();
Johannes Berg5fe231e2013-05-08 21:45:15 +02002720 rtnl_unlock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002721}
2722
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002723static void queue_regulatory_request(struct regulatory_request *request)
2724{
Johannes Bergd4f2c882012-12-03 18:59:58 +01002725 request->alpha2[0] = toupper(request->alpha2[0]);
2726 request->alpha2[1] = toupper(request->alpha2[1]);
John W. Linvillec61029c2010-08-05 14:26:24 -04002727
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002728 spin_lock(&reg_requests_lock);
2729 list_add_tail(&request->list, &reg_requests_list);
2730 spin_unlock(&reg_requests_lock);
2731
2732 schedule_work(&reg_work);
2733}
2734
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002735/*
2736 * Core regulatory hint -- happens during cfg80211_init()
2737 * and when we restore regulatory settings.
2738 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002739static int regulatory_hint_core(const char *alpha2)
2740{
2741 struct regulatory_request *request;
2742
Johannes Berg1a919312012-12-03 17:21:11 +01002743 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002744 if (!request)
2745 return -ENOMEM;
2746
2747 request->alpha2[0] = alpha2[0];
2748 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002749 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002750
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08002751 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04002752
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002753 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002754}
2755
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002756/* User hints */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002757int regulatory_hint_user(const char *alpha2,
2758 enum nl80211_user_reg_hint_type user_reg_hint_type)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002759{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002760 struct regulatory_request *request;
2761
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002762 if (WARN_ON(!alpha2))
2763 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002764
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002765 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2766 if (!request)
2767 return -ENOMEM;
2768
Johannes Bergf4173762012-12-03 18:23:37 +01002769 request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002770 request->alpha2[0] = alpha2[0];
2771 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05002772 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002773 request->user_reg_hint_type = user_reg_hint_type;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002774
Ilan peerc37722b2015-03-30 15:15:49 +03002775 /* Allow calling CRDA again */
Johannes Bergb6863032015-10-15 09:25:18 +02002776 reset_crda_timeouts();
Ilan peerc37722b2015-03-30 15:15:49 +03002777
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002778 queue_regulatory_request(request);
2779
2780 return 0;
2781}
2782
Ilan peer05050752015-03-04 00:32:06 -05002783int regulatory_hint_indoor(bool is_indoor, u32 portid)
Ilan Peer52616f22014-02-25 16:26:00 +02002784{
Ilan peer05050752015-03-04 00:32:06 -05002785 spin_lock(&reg_indoor_lock);
Ilan Peer52616f22014-02-25 16:26:00 +02002786
Ilan peer05050752015-03-04 00:32:06 -05002787 /* It is possible that more than one user space process is trying to
2788 * configure the indoor setting. To handle such cases, clear the indoor
2789 * setting in case that some process does not think that the device
2790 * is operating in an indoor environment. In addition, if a user space
2791 * process indicates that it is controlling the indoor setting, save its
2792 * portid, i.e., make it the owner.
2793 */
2794 reg_is_indoor = is_indoor;
2795 if (reg_is_indoor) {
2796 if (!reg_is_indoor_portid)
2797 reg_is_indoor_portid = portid;
2798 } else {
2799 reg_is_indoor_portid = 0;
2800 }
Ilan Peer52616f22014-02-25 16:26:00 +02002801
Ilan peer05050752015-03-04 00:32:06 -05002802 spin_unlock(&reg_indoor_lock);
2803
2804 if (!is_indoor)
2805 reg_check_channels();
Ilan Peer52616f22014-02-25 16:26:00 +02002806
2807 return 0;
2808}
2809
Ilan peer05050752015-03-04 00:32:06 -05002810void regulatory_netlink_notify(u32 portid)
2811{
2812 spin_lock(&reg_indoor_lock);
2813
2814 if (reg_is_indoor_portid != portid) {
2815 spin_unlock(&reg_indoor_lock);
2816 return;
2817 }
2818
2819 reg_is_indoor = false;
2820 reg_is_indoor_portid = 0;
2821
2822 spin_unlock(&reg_indoor_lock);
2823
2824 reg_check_channels();
2825}
2826
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002827/* Driver hints */
2828int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2829{
2830 struct regulatory_request *request;
2831
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002832 if (WARN_ON(!alpha2 || !wiphy))
2833 return -EINVAL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002834
Luis R. Rodriguez4f7b9142013-12-14 20:09:06 +01002835 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
2836
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002837 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2838 if (!request)
2839 return -ENOMEM;
2840
2841 request->wiphy_idx = get_wiphy_idx(wiphy);
2842
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002843 request->alpha2[0] = alpha2[0];
2844 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002845 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002846
Ilan peerc37722b2015-03-30 15:15:49 +03002847 /* Allow calling CRDA again */
Johannes Bergb6863032015-10-15 09:25:18 +02002848 reset_crda_timeouts();
Ilan peerc37722b2015-03-30 15:15:49 +03002849
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002850 queue_regulatory_request(request);
2851
2852 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002853}
2854EXPORT_SYMBOL(regulatory_hint);
2855
Johannes Berg57fbcce2016-04-12 15:56:15 +02002856void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band,
Luis R. Rodriguez789fd032013-10-04 18:07:24 -07002857 const u8 *country_ie, u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002858{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002859 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002860 enum environment_cap env = ENVIRON_ANY;
Johannes Bergdb2424c2013-05-10 19:07:52 +02002861 struct regulatory_request *request = NULL, *lr;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002862
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002863 /* IE len must be evenly divisible by 2 */
2864 if (country_ie_len & 0x01)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002865 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002866
2867 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002868 return;
2869
2870 request = kzalloc(sizeof(*request), GFP_KERNEL);
2871 if (!request)
2872 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002873
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002874 alpha2[0] = country_ie[0];
2875 alpha2[1] = country_ie[1];
2876
2877 if (country_ie[2] == 'I')
2878 env = ENVIRON_INDOOR;
2879 else if (country_ie[2] == 'O')
2880 env = ENVIRON_OUTDOOR;
2881
Johannes Bergdb2424c2013-05-10 19:07:52 +02002882 rcu_read_lock();
2883 lr = get_last_request();
2884
2885 if (unlikely(!lr))
2886 goto out;
2887
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002888 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002889 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002890 * We leave conflict resolution to the workqueue, where can hold
Johannes Berg5fe231e2013-05-08 21:45:15 +02002891 * the RTNL.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002892 */
Johannes Bergc492db32012-12-06 16:29:25 +01002893 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2894 lr->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002895 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002896
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002897 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04002898 request->alpha2[0] = alpha2[0];
2899 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002900 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002901 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002902
Ilan peerc37722b2015-03-30 15:15:49 +03002903 /* Allow calling CRDA again */
Johannes Bergb6863032015-10-15 09:25:18 +02002904 reset_crda_timeouts();
Ilan peerc37722b2015-03-30 15:15:49 +03002905
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002906 queue_regulatory_request(request);
Johannes Bergdb2424c2013-05-10 19:07:52 +02002907 request = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002908out:
Johannes Bergdb2424c2013-05-10 19:07:52 +02002909 kfree(request);
2910 rcu_read_unlock();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002911}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002912
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002913static void restore_alpha2(char *alpha2, bool reset_user)
2914{
2915 /* indicates there is no alpha2 to consider for restoration */
2916 alpha2[0] = '9';
2917 alpha2[1] = '7';
2918
2919 /* The user setting has precedence over the module parameter */
2920 if (is_user_regdom_saved()) {
2921 /* Unless we're asked to ignore it and reset it */
2922 if (reset_user) {
Johannes Bergc799ba62015-12-11 15:31:10 +01002923 pr_debug("Restoring regulatory settings including user preference\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002924 user_alpha2[0] = '9';
2925 user_alpha2[1] = '7';
2926
2927 /*
2928 * If we're ignoring user settings, we still need to
2929 * check the module parameter to ensure we put things
2930 * back as they were for a full restore.
2931 */
2932 if (!is_world_regdom(ieee80211_regdom)) {
Johannes Bergc799ba62015-12-11 15:31:10 +01002933 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2934 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002935 alpha2[0] = ieee80211_regdom[0];
2936 alpha2[1] = ieee80211_regdom[1];
2937 }
2938 } else {
Johannes Bergc799ba62015-12-11 15:31:10 +01002939 pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n",
2940 user_alpha2[0], user_alpha2[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002941 alpha2[0] = user_alpha2[0];
2942 alpha2[1] = user_alpha2[1];
2943 }
2944 } else if (!is_world_regdom(ieee80211_regdom)) {
Johannes Bergc799ba62015-12-11 15:31:10 +01002945 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2946 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002947 alpha2[0] = ieee80211_regdom[0];
2948 alpha2[1] = ieee80211_regdom[1];
2949 } else
Johannes Bergc799ba62015-12-11 15:31:10 +01002950 pr_debug("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002951}
2952
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302953static void restore_custom_reg_settings(struct wiphy *wiphy)
2954{
2955 struct ieee80211_supported_band *sband;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002956 enum nl80211_band band;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302957 struct ieee80211_channel *chan;
2958 int i;
2959
Johannes Berg57fbcce2016-04-12 15:56:15 +02002960 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302961 sband = wiphy->bands[band];
2962 if (!sband)
2963 continue;
2964 for (i = 0; i < sband->n_channels; i++) {
2965 chan = &sband->channels[i];
2966 chan->flags = chan->orig_flags;
2967 chan->max_antenna_gain = chan->orig_mag;
2968 chan->max_power = chan->orig_mpwr;
Paul Stewart899852a2012-08-01 16:54:42 -07002969 chan->beacon_found = false;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302970 }
2971 }
2972}
2973
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002974/*
2975 * Restoring regulatory settings involves ingoring any
2976 * possibly stale country IE information and user regulatory
2977 * settings if so desired, this includes any beacon hints
2978 * learned as we could have traveled outside to another country
2979 * after disconnection. To restore regulatory settings we do
2980 * exactly what we did at bootup:
2981 *
2982 * - send a core regulatory hint
2983 * - send a user regulatory hint if applicable
2984 *
2985 * Device drivers that send a regulatory hint for a specific country
2986 * keep their own regulatory domain on wiphy->regd so that does does
2987 * not need to be remembered.
2988 */
2989static void restore_regulatory_settings(bool reset_user)
2990{
2991 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002992 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002993 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002994 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302995 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002996
Johannes Berg5fe231e2013-05-08 21:45:15 +02002997 ASSERT_RTNL();
2998
Ilan peer05050752015-03-04 00:32:06 -05002999 /*
3000 * Clear the indoor setting in case that it is not controlled by user
3001 * space, as otherwise there is no guarantee that the device is still
3002 * operating in an indoor environment.
3003 */
3004 spin_lock(&reg_indoor_lock);
3005 if (reg_is_indoor && !reg_is_indoor_portid) {
3006 reg_is_indoor = false;
3007 reg_check_channels();
3008 }
3009 spin_unlock(&reg_indoor_lock);
Ilan Peer52616f22014-02-25 16:26:00 +02003010
Johannes Berg2d319862013-01-09 12:01:38 +01003011 reset_regdomains(true, &world_regdom);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003012 restore_alpha2(alpha2, reset_user);
3013
Luis R. Rodriguez14609552011-04-05 10:49:03 -07003014 /*
3015 * If there's any pending requests we simply
3016 * stash them to a temporary pending queue and
3017 * add then after we've restored regulatory
3018 * settings.
3019 */
3020 spin_lock(&reg_requests_lock);
Ilan peereeca9fc2015-03-04 00:32:07 -05003021 list_splice_tail_init(&reg_requests_list, &tmp_reg_req_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07003022 spin_unlock(&reg_requests_lock);
3023
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003024 /* Clear beacon hints */
3025 spin_lock_bh(&reg_pending_beacons_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01003026 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
3027 list_del(&reg_beacon->list);
3028 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003029 }
3030 spin_unlock_bh(&reg_pending_beacons_lock);
3031
Johannes Bergfea9bce2012-12-03 17:32:01 +01003032 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
3033 list_del(&reg_beacon->list);
3034 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003035 }
3036
3037 /* First restore to the basic regulatory settings */
Johannes Berg379b82f2012-12-06 15:44:07 +01003038 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
3039 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003040
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05303041 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02003042 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
3043 continue;
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01003044 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05303045 restore_custom_reg_settings(&rdev->wiphy);
3046 }
3047
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08003048 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003049
3050 /*
3051 * This restores the ieee80211_regdom module parameter
3052 * preference or the last user requested regulatory
3053 * settings, user regulatory settings takes precedence.
3054 */
3055 if (is_an_alpha2(alpha2))
Maciej S. Szmigiero549cc1c2015-09-02 19:00:31 +02003056 regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003057
Luis R. Rodriguez14609552011-04-05 10:49:03 -07003058 spin_lock(&reg_requests_lock);
Johannes Berg11cff962012-12-03 18:56:41 +01003059 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07003060 spin_unlock(&reg_requests_lock);
3061
Johannes Bergc799ba62015-12-11 15:31:10 +01003062 pr_debug("Kicking the queue\n");
Luis R. Rodriguez14609552011-04-05 10:49:03 -07003063
3064 schedule_work(&reg_work);
3065}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003066
3067void regulatory_hint_disconnect(void)
3068{
Johannes Bergc799ba62015-12-11 15:31:10 +01003069 pr_debug("All devices are disconnected, going to restore regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003070 restore_regulatory_settings(false);
3071}
3072
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003073static bool freq_is_chan_12_13_14(u16 freq)
3074{
Johannes Berg57fbcce2016-04-12 15:56:15 +02003075 if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) ||
3076 freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) ||
3077 freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003078 return true;
3079 return false;
3080}
3081
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08003082static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
3083{
3084 struct reg_beacon *pending_beacon;
3085
3086 list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
3087 if (beacon_chan->center_freq ==
3088 pending_beacon->chan.center_freq)
3089 return true;
3090 return false;
3091}
3092
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003093int regulatory_hint_found_beacon(struct wiphy *wiphy,
3094 struct ieee80211_channel *beacon_chan,
3095 gfp_t gfp)
3096{
3097 struct reg_beacon *reg_beacon;
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08003098 bool processing;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003099
Johannes Berg1a919312012-12-03 17:21:11 +01003100 if (beacon_chan->beacon_found ||
3101 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
Johannes Berg57fbcce2016-04-12 15:56:15 +02003102 (beacon_chan->band == NL80211_BAND_2GHZ &&
Johannes Berg1a919312012-12-03 17:21:11 +01003103 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003104 return 0;
3105
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08003106 spin_lock_bh(&reg_pending_beacons_lock);
3107 processing = pending_reg_beacon(beacon_chan);
3108 spin_unlock_bh(&reg_pending_beacons_lock);
3109
3110 if (processing)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003111 return 0;
3112
3113 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
3114 if (!reg_beacon)
3115 return -ENOMEM;
3116
Johannes Bergc799ba62015-12-11 15:31:10 +01003117 pr_debug("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
3118 beacon_chan->center_freq,
3119 ieee80211_frequency_to_channel(beacon_chan->center_freq),
3120 wiphy_name(wiphy));
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05003121
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003122 memcpy(&reg_beacon->chan, beacon_chan,
Johannes Berg1a919312012-12-03 17:21:11 +01003123 sizeof(struct ieee80211_channel));
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003124
3125 /*
3126 * Since we can be called from BH or and non-BH context
3127 * we must use spin_lock_bh()
3128 */
3129 spin_lock_bh(&reg_pending_beacons_lock);
3130 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
3131 spin_unlock_bh(&reg_pending_beacons_lock);
3132
3133 schedule_work(&reg_work);
3134
3135 return 0;
3136}
3137
Johannes Berga3d2eaf2008-09-15 11:10:52 +02003138static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003139{
3140 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02003141 const struct ieee80211_reg_rule *reg_rule = NULL;
3142 const struct ieee80211_freq_range *freq_range = NULL;
3143 const struct ieee80211_power_rule *power_rule = NULL;
Janusz Dziedzic089027e2014-02-21 19:46:12 +01003144 char bw[32], cac_time[32];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003145
Dave Young94c4fd62015-11-15 15:31:05 +08003146 pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003147
3148 for (i = 0; i < rd->n_reg_rules; i++) {
3149 reg_rule = &rd->reg_rules[i];
3150 freq_range = &reg_rule->freq_range;
3151 power_rule = &reg_rule->power_rule;
3152
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01003153 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
3154 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
3155 freq_range->max_bandwidth_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01003156 reg_get_max_bandwidth(rd, reg_rule));
3157 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01003158 snprintf(bw, sizeof(bw), "%d KHz",
Janusz Dziedzic97524822014-01-30 09:52:20 +01003159 freq_range->max_bandwidth_khz);
3160
Janusz Dziedzic089027e2014-02-21 19:46:12 +01003161 if (reg_rule->flags & NL80211_RRF_DFS)
3162 scnprintf(cac_time, sizeof(cac_time), "%u s",
3163 reg_rule->dfs_cac_ms/1000);
3164 else
3165 scnprintf(cac_time, sizeof(cac_time), "N/A");
3166
3167
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05003168 /*
3169 * There may not be documentation for max antenna gain
3170 * in certain regions
3171 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003172 if (power_rule->max_antenna_gain)
Dave Young94c4fd62015-11-15 15:31:05 +08003173 pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003174 freq_range->start_freq_khz,
3175 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01003176 bw,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003177 power_rule->max_antenna_gain,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01003178 power_rule->max_eirp,
3179 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003180 else
Dave Young94c4fd62015-11-15 15:31:05 +08003181 pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003182 freq_range->start_freq_khz,
3183 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01003184 bw,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01003185 power_rule->max_eirp,
3186 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003187 }
3188}
3189
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01003190bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003191{
3192 switch (dfs_region) {
3193 case NL80211_DFS_UNSET:
3194 case NL80211_DFS_FCC:
3195 case NL80211_DFS_ETSI:
3196 case NL80211_DFS_JP:
3197 return true;
3198 default:
Johannes Bergc799ba62015-12-11 15:31:10 +01003199 pr_debug("Ignoring uknown DFS master region: %d\n", dfs_region);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003200 return false;
3201 }
3202}
3203
Johannes Berga3d2eaf2008-09-15 11:10:52 +02003204static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003205{
Johannes Bergc492db32012-12-06 16:29:25 +01003206 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003207
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003208 if (is_intersected_alpha2(rd->alpha2)) {
Johannes Bergc492db32012-12-06 16:29:25 +01003209 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003210 struct cfg80211_registered_device *rdev;
Johannes Bergc492db32012-12-06 16:29:25 +01003211 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02003212 if (rdev) {
Dave Young94c4fd62015-11-15 15:31:05 +08003213 pr_debug("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02003214 rdev->country_ie_alpha2[0],
3215 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003216 } else
Dave Young94c4fd62015-11-15 15:31:05 +08003217 pr_debug("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003218 } else
Dave Young94c4fd62015-11-15 15:31:05 +08003219 pr_debug("Current regulatory domain intersected:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01003220 } else if (is_world_regdom(rd->alpha2)) {
Dave Young94c4fd62015-11-15 15:31:05 +08003221 pr_debug("World regulatory domain updated:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01003222 } else {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003223 if (is_unknown_alpha2(rd->alpha2))
Dave Young94c4fd62015-11-15 15:31:05 +08003224 pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003225 else {
Johannes Bergc492db32012-12-06 16:29:25 +01003226 if (reg_request_cell_base(lr))
Dave Young94c4fd62015-11-15 15:31:05 +08003227 pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003228 rd->alpha2[0], rd->alpha2[1]);
3229 else
Dave Young94c4fd62015-11-15 15:31:05 +08003230 pr_debug("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003231 rd->alpha2[0], rd->alpha2[1]);
3232 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003233 }
Johannes Berg1a919312012-12-03 17:21:11 +01003234
Dave Young94c4fd62015-11-15 15:31:05 +08003235 pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003236 print_rd_rules(rd);
3237}
3238
Johannes Berg2df78162008-10-28 16:49:41 +01003239static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003240{
Dave Young94c4fd62015-11-15 15:31:05 +08003241 pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003242 print_rd_rules(rd);
3243}
3244
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003245static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
3246{
3247 if (!is_world_regdom(rd->alpha2))
3248 return -EINVAL;
3249 update_world_regdomain(rd);
3250 return 0;
3251}
3252
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08003253static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
3254 struct regulatory_request *user_request)
3255{
3256 const struct ieee80211_regdomain *intersected_rd = NULL;
3257
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08003258 if (!regdom_changes(rd->alpha2))
3259 return -EALREADY;
3260
3261 if (!is_valid_rd(rd)) {
Dave Young94c4fd62015-11-15 15:31:05 +08003262 pr_err("Invalid regulatory domain detected: %c%c\n",
3263 rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08003264 print_regdomain_info(rd);
3265 return -EINVAL;
3266 }
3267
3268 if (!user_request->intersect) {
3269 reset_regdomains(false, rd);
3270 return 0;
3271 }
3272
3273 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
3274 if (!intersected_rd)
3275 return -EINVAL;
3276
3277 kfree(rd);
3278 rd = NULL;
3279 reset_regdomains(false, intersected_rd);
3280
3281 return 0;
3282}
3283
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003284static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
3285 struct regulatory_request *driver_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003286{
Johannes Berge9763c32012-12-03 16:59:46 +01003287 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07003288 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003289 const struct ieee80211_regdomain *tmp;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05003290 struct wiphy *request_wiphy;
Johannes Berg6913b492012-12-04 00:48:59 +01003291
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003292 if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003293 return -EINVAL;
3294
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003295 if (!regdom_changes(rd->alpha2))
3296 return -EALREADY;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003297
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08003298 if (!is_valid_rd(rd)) {
Dave Young94c4fd62015-11-15 15:31:05 +08003299 pr_err("Invalid regulatory domain detected: %c%c\n",
3300 rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08003301 print_regdomain_info(rd);
3302 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003303 }
3304
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003305 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
Johannes Berg922ec582015-10-15 09:12:49 +02003306 if (!request_wiphy)
Johannes Bergde3584b2011-11-21 10:44:00 +01003307 return -ENODEV;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05003308
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003309 if (!driver_request->intersect) {
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07003310 if (request_wiphy->regd)
3311 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08003312
Johannes Berge9763c32012-12-03 16:59:46 +01003313 regd = reg_copy_regd(rd);
3314 if (IS_ERR(regd))
3315 return PTR_ERR(regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08003316
Johannes Berg458f4f92012-12-06 15:47:38 +01003317 rcu_assign_pointer(request_wiphy->regd, regd);
Johannes Berg379b82f2012-12-06 15:44:07 +01003318 reset_regdomains(false, rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08003319 return 0;
3320 }
3321
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003322 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
3323 if (!intersected_rd)
3324 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08003325
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003326 /*
3327 * We can trash what CRDA provided now.
3328 * However if a driver requested this specific regulatory
3329 * domain we keep it for its private use
3330 */
3331 tmp = get_wiphy_regdom(request_wiphy);
3332 rcu_assign_pointer(request_wiphy->regd, rd);
3333 rcu_free_regdom(tmp);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08003334
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003335 rd = NULL;
Larry Fingerb7566fc2013-02-04 15:33:44 -06003336
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003337 reset_regdomains(false, intersected_rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08003338
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003339 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003340}
3341
Luis R. Rodriguez01992402013-11-05 09:18:17 -08003342static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
3343 struct regulatory_request *country_ie_request)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003344{
3345 struct wiphy *request_wiphy;
3346
3347 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
3348 !is_unknown_alpha2(rd->alpha2))
3349 return -EINVAL;
3350
3351 /*
3352 * Lets only bother proceeding on the same alpha2 if the current
3353 * rd is non static (it means CRDA was present and was used last)
3354 * and the pending request came in from a country IE
3355 */
3356
3357 if (!is_valid_rd(rd)) {
Dave Young94c4fd62015-11-15 15:31:05 +08003358 pr_err("Invalid regulatory domain detected: %c%c\n",
3359 rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003360 print_regdomain_info(rd);
3361 return -EINVAL;
3362 }
3363
Luis R. Rodriguez01992402013-11-05 09:18:17 -08003364 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
Johannes Berg922ec582015-10-15 09:12:49 +02003365 if (!request_wiphy)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003366 return -ENODEV;
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003367
Luis R. Rodriguez01992402013-11-05 09:18:17 -08003368 if (country_ie_request->intersect)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003369 return -EINVAL;
3370
3371 reset_regdomains(false, rd);
3372 return 0;
3373}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003374
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05003375/*
3376 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003377 * multiple drivers can be ironed out later. Caller must've already
Johannes Berg458f4f92012-12-06 15:47:38 +01003378 * kmalloc'd the rd structure.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05003379 */
Ilan peerc37722b2015-03-30 15:15:49 +03003380int set_regdom(const struct ieee80211_regdomain *rd,
3381 enum ieee80211_regd_source regd_src)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003382{
Johannes Bergc492db32012-12-06 16:29:25 +01003383 struct regulatory_request *lr;
Janusz Dziedzic092008a2014-02-14 08:54:00 +01003384 bool user_reset = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003385 int r;
3386
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003387 if (!reg_is_valid_request(rd->alpha2)) {
3388 kfree(rd);
3389 return -EINVAL;
3390 }
3391
Ilan peerc37722b2015-03-30 15:15:49 +03003392 if (regd_src == REGD_SOURCE_CRDA)
Johannes Bergb6863032015-10-15 09:25:18 +02003393 reset_crda_timeouts();
Ilan peerc37722b2015-03-30 15:15:49 +03003394
Johannes Bergc492db32012-12-06 16:29:25 +01003395 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07003396
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003397 /* Note that this doesn't update the wiphys, this is done below */
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003398 switch (lr->initiator) {
3399 case NL80211_REGDOM_SET_BY_CORE:
3400 r = reg_set_rd_core(rd);
3401 break;
3402 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08003403 r = reg_set_rd_user(rd, lr);
Janusz Dziedzic092008a2014-02-14 08:54:00 +01003404 user_reset = true;
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08003405 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003406 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003407 r = reg_set_rd_driver(rd, lr);
3408 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003409 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez01992402013-11-05 09:18:17 -08003410 r = reg_set_rd_country_ie(rd, lr);
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003411 break;
3412 default:
3413 WARN(1, "invalid initiator %d\n", lr->initiator);
Ola Olsson09d11802015-12-13 19:12:03 +01003414 kfree(rd);
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003415 return -EINVAL;
3416 }
3417
Johannes Bergd2372b32008-10-24 20:32:20 +02003418 if (r) {
Janusz Dziedzic092008a2014-02-14 08:54:00 +01003419 switch (r) {
3420 case -EALREADY:
Kalle Valo95908532012-07-12 15:33:58 +03003421 reg_set_request_processed();
Janusz Dziedzic092008a2014-02-14 08:54:00 +01003422 break;
3423 default:
3424 /* Back to world regulatory in case of errors */
3425 restore_regulatory_settings(user_reset);
3426 }
Kalle Valo95908532012-07-12 15:33:58 +03003427
Johannes Bergd2372b32008-10-24 20:32:20 +02003428 kfree(rd);
Johannes Berg38fd2142013-05-10 19:17:17 +02003429 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02003430 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003431
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003432 /* This would make this whole thing pointless */
Johannes Berg38fd2142013-05-10 19:17:17 +02003433 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
3434 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003435
3436 /* update all wiphys now with the new established regulatory domain */
Johannes Bergc492db32012-12-06 16:29:25 +01003437 update_all_wiphy_regulatory(lr->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003438
Johannes Berg458f4f92012-12-06 15:47:38 +01003439 print_regdomain(get_cfg80211_regdom());
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003440
Johannes Bergc492db32012-12-06 16:29:25 +01003441 nl80211_send_reg_change_event(lr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003442
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08003443 reg_set_request_processed();
3444
Johannes Berg38fd2142013-05-10 19:17:17 +02003445 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003446}
3447
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02003448static int __regulatory_set_wiphy_regd(struct wiphy *wiphy,
3449 struct ieee80211_regdomain *rd)
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02003450{
3451 const struct ieee80211_regdomain *regd;
3452 const struct ieee80211_regdomain *prev_regd;
3453 struct cfg80211_registered_device *rdev;
3454
3455 if (WARN_ON(!wiphy || !rd))
3456 return -EINVAL;
3457
3458 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED),
3459 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n"))
3460 return -EPERM;
3461
3462 if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) {
3463 print_regdomain_info(rd);
3464 return -EINVAL;
3465 }
3466
3467 regd = reg_copy_regd(rd);
3468 if (IS_ERR(regd))
3469 return PTR_ERR(regd);
3470
3471 rdev = wiphy_to_rdev(wiphy);
3472
3473 spin_lock(&reg_requests_lock);
3474 prev_regd = rdev->requested_regd;
3475 rdev->requested_regd = regd;
3476 spin_unlock(&reg_requests_lock);
3477
3478 kfree(prev_regd);
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02003479 return 0;
3480}
3481
3482int regulatory_set_wiphy_regd(struct wiphy *wiphy,
3483 struct ieee80211_regdomain *rd)
3484{
3485 int ret = __regulatory_set_wiphy_regd(wiphy, rd);
3486
3487 if (ret)
3488 return ret;
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02003489
3490 schedule_work(&reg_work);
3491 return 0;
3492}
3493EXPORT_SYMBOL(regulatory_set_wiphy_regd);
3494
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02003495int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy,
3496 struct ieee80211_regdomain *rd)
3497{
3498 int ret;
3499
3500 ASSERT_RTNL();
3501
3502 ret = __regulatory_set_wiphy_regd(wiphy, rd);
3503 if (ret)
3504 return ret;
3505
3506 /* process the request immediately */
3507 reg_process_self_managed_hints();
3508 return 0;
3509}
3510EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl);
3511
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003512void wiphy_regulatory_register(struct wiphy *wiphy)
3513{
Arik Nemtsov23df0b72013-07-21 16:36:48 +03003514 struct regulatory_request *lr;
3515
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02003516 /* self-managed devices ignore external hints */
3517 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
3518 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
3519 REGULATORY_COUNTRY_IE_IGNORE;
3520
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003521 if (!reg_dev_ignore_cell_hint(wiphy))
3522 reg_num_devs_support_basehint++;
3523
Arik Nemtsov23df0b72013-07-21 16:36:48 +03003524 lr = get_last_request();
3525 wiphy_update_regulatory(wiphy, lr->initiator);
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05303526 wiphy_all_share_dfs_chan_state(wiphy);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003527}
3528
Luis R. Rodriguezbfead082012-07-12 11:49:19 -07003529void wiphy_regulatory_deregister(struct wiphy *wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003530{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04003531 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01003532 struct regulatory_request *lr;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05003533
Johannes Bergc492db32012-12-06 16:29:25 +01003534 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07003535
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003536 if (!reg_dev_ignore_cell_hint(wiphy))
3537 reg_num_devs_support_basehint--;
3538
Johannes Berg458f4f92012-12-06 15:47:38 +01003539 rcu_free_regdom(get_wiphy_regdom(wiphy));
Monam Agarwal34dd8862014-03-24 00:53:40 +05303540 RCU_INIT_POINTER(wiphy->regd, NULL);
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003541
Johannes Bergc492db32012-12-06 16:29:25 +01003542 if (lr)
3543 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05003544
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003545 if (!request_wiphy || request_wiphy != wiphy)
Johannes Berg38fd2142013-05-10 19:17:17 +02003546 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003547
Johannes Bergc492db32012-12-06 16:29:25 +01003548 lr->wiphy_idx = WIPHY_IDX_INVALID;
3549 lr->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003550}
3551
Ilan Peer174e0cd2014-02-23 09:13:01 +02003552/*
3553 * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
3554 * UNII band definitions
3555 */
3556int cfg80211_get_unii(int freq)
3557{
3558 /* UNII-1 */
3559 if (freq >= 5150 && freq <= 5250)
3560 return 0;
3561
3562 /* UNII-2A */
3563 if (freq > 5250 && freq <= 5350)
3564 return 1;
3565
3566 /* UNII-2B */
3567 if (freq > 5350 && freq <= 5470)
3568 return 2;
3569
3570 /* UNII-2C */
3571 if (freq > 5470 && freq <= 5725)
3572 return 3;
3573
3574 /* UNII-3 */
3575 if (freq > 5725 && freq <= 5825)
3576 return 4;
3577
3578 return -EINVAL;
3579}
3580
Ilan Peerc8866e52014-02-23 09:13:03 +02003581bool regulatory_indoor_allowed(void)
3582{
3583 return reg_is_indoor;
3584}
3585
Vasanthakumar Thiagarajanb35a51c2017-02-27 17:04:33 +05303586bool regulatory_pre_cac_allowed(struct wiphy *wiphy)
3587{
3588 const struct ieee80211_regdomain *regd = NULL;
3589 const struct ieee80211_regdomain *wiphy_regd = NULL;
3590 bool pre_cac_allowed = false;
3591
3592 rcu_read_lock();
3593
3594 regd = rcu_dereference(cfg80211_regdomain);
3595 wiphy_regd = rcu_dereference(wiphy->regd);
3596 if (!wiphy_regd) {
3597 if (regd->dfs_region == NL80211_DFS_ETSI)
3598 pre_cac_allowed = true;
3599
3600 rcu_read_unlock();
3601
3602 return pre_cac_allowed;
3603 }
3604
3605 if (regd->dfs_region == wiphy_regd->dfs_region &&
3606 wiphy_regd->dfs_region == NL80211_DFS_ETSI)
3607 pre_cac_allowed = true;
3608
3609 rcu_read_unlock();
3610
3611 return pre_cac_allowed;
3612}
3613
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05303614void regulatory_propagate_dfs_state(struct wiphy *wiphy,
3615 struct cfg80211_chan_def *chandef,
3616 enum nl80211_dfs_state dfs_state,
3617 enum nl80211_radar_event event)
3618{
3619 struct cfg80211_registered_device *rdev;
3620
3621 ASSERT_RTNL();
3622
3623 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3624 return;
3625
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05303626 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3627 if (wiphy == &rdev->wiphy)
3628 continue;
3629
3630 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
3631 continue;
3632
3633 if (!ieee80211_get_channel(&rdev->wiphy,
3634 chandef->chan->center_freq))
3635 continue;
3636
3637 cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state);
3638
3639 if (event == NL80211_RADAR_DETECTED ||
3640 event == NL80211_RADAR_CAC_FINISHED)
3641 cfg80211_sched_dfs_chan_update(rdev);
3642
3643 nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL);
3644 }
3645}
3646
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02003647int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003648{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003649 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02003650
Johannes Berg90a53e42017-09-13 22:21:08 +02003651 err = load_builtin_regdb_keys();
3652 if (err)
3653 return err;
3654
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003655 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
3656 if (IS_ERR(reg_pdev))
3657 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02003658
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003659 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003660 spin_lock_init(&reg_pending_beacons_lock);
Ilan peer05050752015-03-04 00:32:06 -05003661 spin_lock_init(&reg_indoor_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003662
Johannes Berg458f4f92012-12-06 15:47:38 +01003663 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02003664
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003665 user_alpha2[0] = '9';
3666 user_alpha2[1] = '7';
3667
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003668 /* We always try to get an update for the static regdomain */
Johannes Berg458f4f92012-12-06 15:47:38 +01003669 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003670 if (err) {
Ola Olsson09d11802015-12-13 19:12:03 +01003671 if (err == -ENOMEM) {
3672 platform_device_unregister(reg_pdev);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003673 return err;
Ola Olsson09d11802015-12-13 19:12:03 +01003674 }
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003675 /*
3676 * N.B. kobject_uevent_env() can fail mainly for when we're out
3677 * memory which is handled and propagated appropriately above
3678 * but it can also fail during a netlink_broadcast() or during
3679 * early boot for call_usermodehelper(). For now treat these
3680 * errors as non-fatal.
3681 */
Joe Perchese9c02682010-11-16 19:56:49 -08003682 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003683 }
Johannes Berg734366d2008-09-15 10:56:48 +02003684
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003685 /*
3686 * Finally, if the user set the module parameter treat it
3687 * as a user hint.
3688 */
3689 if (!is_world_regdom(ieee80211_regdom))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003690 regulatory_hint_user(ieee80211_regdom,
3691 NL80211_USER_REG_HINT_USER);
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003692
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003693 return 0;
3694}
3695
Johannes Berg1a919312012-12-03 17:21:11 +01003696void regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003697{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003698 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003699 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003700
3701 cancel_work_sync(&reg_work);
Johannes Bergb6863032015-10-15 09:25:18 +02003702 cancel_crda_timeout_sync();
Arik Nemtsovad932f02014-11-27 09:44:55 +02003703 cancel_delayed_work_sync(&reg_check_chans);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003704
Johannes Berg9027b142012-12-03 17:59:24 +01003705 /* Lock to suppress warnings */
Johannes Berg38fd2142013-05-10 19:17:17 +02003706 rtnl_lock();
Johannes Berg379b82f2012-12-06 15:44:07 +01003707 reset_regdomains(true, NULL);
Johannes Berg38fd2142013-05-10 19:17:17 +02003708 rtnl_unlock();
Johannes Berg734366d2008-09-15 10:56:48 +02003709
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08003710 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02003711
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003712 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02003713
Johannes Bergfea9bce2012-12-03 17:32:01 +01003714 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
3715 list_del(&reg_beacon->list);
3716 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003717 }
3718
Johannes Bergfea9bce2012-12-03 17:32:01 +01003719 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
3720 list_del(&reg_beacon->list);
3721 kfree(reg_beacon);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003722 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003723
Johannes Bergfea9bce2012-12-03 17:32:01 +01003724 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
3725 list_del(&reg_request->list);
3726 kfree(reg_request);
Johannes Berg8318d782008-01-24 19:38:38 +01003727 }
Johannes Berg007f6c52015-10-15 11:22:58 +02003728
3729 if (!IS_ERR_OR_NULL(regdb))
3730 kfree(regdb);
Johannes Berg90a53e42017-09-13 22:21:08 +02003731
3732 free_regdb_keyring();
Johannes Berg8318d782008-01-24 19:38:38 +01003733}