blob: 6e94f6934a0e7a47fbac086586701d07c9bb5487 [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>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040056#include <linux/moduleparam.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070057#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010058#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070059#include "reg.h"
Arik Nemtsovad932f02014-11-27 09:44:55 +020060#include "rdev-ops.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050061#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040062#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010063
Arik Nemtsovad932f02014-11-27 09:44:55 +020064/*
65 * Grace period we give before making sure all current interfaces reside on
66 * channels allowed by the current regulatory domain.
67 */
68#define REG_ENFORCE_GRACE_MS 60000
69
Ilan Peer52616f22014-02-25 16:26:00 +020070/**
71 * enum reg_request_treatment - regulatory request treatment
72 *
73 * @REG_REQ_OK: continue processing the regulatory request
74 * @REG_REQ_IGNORE: ignore the regulatory request
75 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
76 * be intersected with the current one.
77 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
78 * regulatory settings, and no further processing is required.
Ilan Peer52616f22014-02-25 16:26:00 +020079 */
Johannes Berg2f922122012-12-03 17:54:55 +010080enum reg_request_treatment {
81 REG_REQ_OK,
82 REG_REQ_IGNORE,
83 REG_REQ_INTERSECT,
84 REG_REQ_ALREADY_SET,
85};
86
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050087static struct regulatory_request core_request_world = {
88 .initiator = NL80211_REGDOM_SET_BY_CORE,
89 .alpha2[0] = '0',
90 .alpha2[1] = '0',
91 .intersect = false,
92 .processed = true,
93 .country_ie_env = ENVIRON_ANY,
94};
95
Johannes Berg38fd2142013-05-10 19:17:17 +020096/*
97 * Receipt of information from last regulatory request,
98 * protected by RTNL (and can be accessed with RCU protection)
99 */
Johannes Bergc492db32012-12-06 16:29:25 +0100100static struct regulatory_request __rcu *last_request =
Johannes Bergcec3f0e2014-12-12 12:26:25 +0100101 (void __force __rcu *)&core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +0200102
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700103/* To trigger userspace events */
104static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +0100105
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500106/*
107 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +0200108 * the current one and a world regulatory domain in case we have no
Johannes Berge8da2bb2012-12-03 23:00:08 +0100109 * information to give us an alpha2.
Johannes Berg38fd2142013-05-10 19:17:17 +0200110 * (protected by RTNL, can be read under RCU)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500111 */
Johannes Berg458f4f92012-12-06 15:47:38 +0100112const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +0200113
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500114/*
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700115 * Number of devices that registered to the core
116 * that support cellular base station regulatory hints
Johannes Berg38fd2142013-05-10 19:17:17 +0200117 * (protected by RTNL)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700118 */
119static int reg_num_devs_support_basehint;
120
Ilan Peer52616f22014-02-25 16:26:00 +0200121/*
122 * State variable indicating if the platform on which the devices
123 * are attached is operating in an indoor environment. The state variable
124 * is relevant for all registered devices.
Ilan Peer52616f22014-02-25 16:26:00 +0200125 */
126static bool reg_is_indoor;
Ilan peer05050752015-03-04 00:32:06 -0500127static spinlock_t reg_indoor_lock;
128
129/* Used to track the userspace process controlling the indoor setting */
130static u32 reg_is_indoor_portid;
Ilan Peer52616f22014-02-25 16:26:00 +0200131
Johannes Bergb6863032015-10-15 09:25:18 +0200132static void restore_regulatory_settings(bool reset_user);
Ilan peerc37722b2015-03-30 15:15:49 +0300133
Johannes Berg458f4f92012-12-06 15:47:38 +0100134static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
135{
Johannes Berg38fd2142013-05-10 19:17:17 +0200136 return rtnl_dereference(cfg80211_regdomain);
Johannes Berg458f4f92012-12-06 15:47:38 +0100137}
138
Arik Nemtsovad30ca22014-12-15 19:25:59 +0200139const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
Johannes Berg458f4f92012-12-06 15:47:38 +0100140{
Johannes Berg38fd2142013-05-10 19:17:17 +0200141 return rtnl_dereference(wiphy->regd);
Johannes Berg458f4f92012-12-06 15:47:38 +0100142}
143
Luis R. Rodriguez3ef121b2013-11-13 18:54:05 +0100144static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
145{
146 switch (dfs_region) {
147 case NL80211_DFS_UNSET:
148 return "unset";
149 case NL80211_DFS_FCC:
150 return "FCC";
151 case NL80211_DFS_ETSI:
152 return "ETSI";
153 case NL80211_DFS_JP:
154 return "JP";
155 }
156 return "Unknown";
157}
158
Luis R. Rodriguez6c474792013-11-25 20:56:09 +0100159enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
160{
161 const struct ieee80211_regdomain *regd = NULL;
162 const struct ieee80211_regdomain *wiphy_regd = NULL;
163
164 regd = get_cfg80211_regdom();
165 if (!wiphy)
166 goto out;
167
168 wiphy_regd = get_wiphy_regdom(wiphy);
169 if (!wiphy_regd)
170 goto out;
171
172 if (wiphy_regd->dfs_region == regd->dfs_region)
173 goto out;
174
Johannes Bergc799ba62015-12-11 15:31:10 +0100175 pr_debug("%s: device specific dfs_region (%s) disagrees with cfg80211's central dfs_region (%s)\n",
176 dev_name(&wiphy->dev),
177 reg_dfs_region_str(wiphy_regd->dfs_region),
178 reg_dfs_region_str(regd->dfs_region));
Luis R. Rodriguez6c474792013-11-25 20:56:09 +0100179
180out:
181 return regd->dfs_region;
182}
183
Johannes Berg458f4f92012-12-06 15:47:38 +0100184static void rcu_free_regdom(const struct ieee80211_regdomain *r)
185{
186 if (!r)
187 return;
188 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
189}
190
Johannes Bergc492db32012-12-06 16:29:25 +0100191static struct regulatory_request *get_last_request(void)
192{
Johannes Berg38fd2142013-05-10 19:17:17 +0200193 return rcu_dereference_rtnl(last_request);
Johannes Bergc492db32012-12-06 16:29:25 +0100194}
195
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500196/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500197static LIST_HEAD(reg_requests_list);
198static spinlock_t reg_requests_lock;
199
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500200/* Used to queue up beacon hints for review */
201static LIST_HEAD(reg_pending_beacons);
202static spinlock_t reg_pending_beacons_lock;
203
204/* Used to keep track of processed beacon hints */
205static LIST_HEAD(reg_beacon_list);
206
207struct reg_beacon {
208 struct list_head list;
209 struct ieee80211_channel chan;
210};
211
Arik Nemtsovad932f02014-11-27 09:44:55 +0200212static void reg_check_chans_work(struct work_struct *work);
213static DECLARE_DELAYED_WORK(reg_check_chans, reg_check_chans_work);
214
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800215static void reg_todo(struct work_struct *work);
216static DECLARE_WORK(reg_work, reg_todo);
217
Johannes Berg734366d2008-09-15 10:56:48 +0200218/* We keep a static world regulatory domain in case of the absence of CRDA */
219static const struct ieee80211_regdomain world_regdom = {
Jason Abele28981e52015-02-17 16:10:41 -0800220 .n_reg_rules = 8,
Johannes Berg734366d2008-09-15 10:56:48 +0200221 .alpha2 = "00",
222 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500223 /* IEEE 802.11b/g, channels 1..11 */
224 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Johannes Berg43c771a2012-11-12 10:51:34 +0100225 /* IEEE 802.11b/g, channels 12..13. */
Johannes Bergc3826802015-12-11 17:35:50 +0100226 REG_RULE(2467-10, 2472+10, 20, 6, 20,
227 NL80211_RRF_NO_IR | NL80211_RRF_AUTO_BW),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800228 /* IEEE 802.11 channel 14 - Only JP enables
229 * this and for 802.11b only */
230 REG_RULE(2484-10, 2484+10, 20, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200231 NL80211_RRF_NO_IR |
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800232 NL80211_RRF_NO_OFDM),
233 /* IEEE 802.11a, channel 36..48 */
Johannes Bergc3826802015-12-11 17:35:50 +0100234 REG_RULE(5180-10, 5240+10, 80, 6, 20,
235 NL80211_RRF_NO_IR |
236 NL80211_RRF_AUTO_BW),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500237
Johannes Berg131a19b2013-03-04 20:54:46 +0100238 /* IEEE 802.11a, channel 52..64 - DFS required */
Johannes Bergc3826802015-12-11 17:35:50 +0100239 REG_RULE(5260-10, 5320+10, 80, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200240 NL80211_RRF_NO_IR |
Johannes Bergc3826802015-12-11 17:35:50 +0100241 NL80211_RRF_AUTO_BW |
Johannes Berg131a19b2013-03-04 20:54:46 +0100242 NL80211_RRF_DFS),
243
244 /* IEEE 802.11a, channel 100..144 - DFS required */
245 REG_RULE(5500-10, 5720+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200246 NL80211_RRF_NO_IR |
Johannes Berg131a19b2013-03-04 20:54:46 +0100247 NL80211_RRF_DFS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500248
249 /* IEEE 802.11a, channel 149..165 */
Johannes Berg8ab9d852012-12-03 22:09:22 +0100250 REG_RULE(5745-10, 5825+10, 80, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200251 NL80211_RRF_NO_IR),
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300252
Johannes Berg8047d262015-10-15 16:16:09 +0200253 /* IEEE 802.11ad (60GHz), channels 1..3 */
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300254 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200255 }
256};
257
Johannes Berg38fd2142013-05-10 19:17:17 +0200258/* protected by RTNL */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200259static const struct ieee80211_regdomain *cfg80211_world_regdom =
260 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200261
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400262static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500263static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400264
Johannes Berg734366d2008-09-15 10:56:48 +0200265module_param(ieee80211_regdom, charp, 0444);
266MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
267
Arik Nemtsovc8883932014-04-21 20:39:34 -0700268static void reg_free_request(struct regulatory_request *request)
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800269{
Johannes Bergd34265a2015-10-15 13:05:55 +0200270 if (request == &core_request_world)
271 return;
272
Arik Nemtsovc8883932014-04-21 20:39:34 -0700273 if (request != get_last_request())
274 kfree(request);
275}
276
277static void reg_free_last_request(void)
278{
279 struct regulatory_request *lr = get_last_request();
280
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800281 if (lr != &core_request_world && lr)
282 kfree_rcu(lr, rcu_head);
283}
284
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800285static void reg_update_last_request(struct regulatory_request *request)
286{
Luis R. Rodriguez255e25b2014-02-25 17:09:40 -0800287 struct regulatory_request *lr;
288
289 lr = get_last_request();
290 if (lr == request)
291 return;
292
Arik Nemtsovc8883932014-04-21 20:39:34 -0700293 reg_free_last_request();
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800294 rcu_assign_pointer(last_request, request);
295}
296
Johannes Berg379b82f2012-12-06 15:44:07 +0100297static void reset_regdomains(bool full_reset,
298 const struct ieee80211_regdomain *new_regdom)
Johannes Berg734366d2008-09-15 10:56:48 +0200299{
Johannes Berg458f4f92012-12-06 15:47:38 +0100300 const struct ieee80211_regdomain *r;
301
Johannes Berg38fd2142013-05-10 19:17:17 +0200302 ASSERT_RTNL();
Johannes Berge8da2bb2012-12-03 23:00:08 +0100303
Johannes Berg458f4f92012-12-06 15:47:38 +0100304 r = get_cfg80211_regdom();
305
Johannes Berg942b25c2008-09-15 11:26:47 +0200306 /* avoid freeing static information or freeing something twice */
Johannes Berg458f4f92012-12-06 15:47:38 +0100307 if (r == cfg80211_world_regdom)
308 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200309 if (cfg80211_world_regdom == &world_regdom)
310 cfg80211_world_regdom = NULL;
Johannes Berg458f4f92012-12-06 15:47:38 +0100311 if (r == &world_regdom)
312 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200313
Johannes Berg458f4f92012-12-06 15:47:38 +0100314 rcu_free_regdom(r);
315 rcu_free_regdom(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200316
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200317 cfg80211_world_regdom = &world_regdom;
Johannes Berg458f4f92012-12-06 15:47:38 +0100318 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500319
320 if (!full_reset)
321 return;
322
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800323 reg_update_last_request(&core_request_world);
Johannes Berg734366d2008-09-15 10:56:48 +0200324}
325
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500326/*
327 * Dynamic world regulatory domain requested by the wireless
328 * core upon initialization
329 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200330static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200331{
Johannes Bergc492db32012-12-06 16:29:25 +0100332 struct regulatory_request *lr;
Johannes Berg734366d2008-09-15 10:56:48 +0200333
Johannes Bergc492db32012-12-06 16:29:25 +0100334 lr = get_last_request();
335
336 WARN_ON(!lr);
Johannes Berge8da2bb2012-12-03 23:00:08 +0100337
Johannes Berg379b82f2012-12-06 15:44:07 +0100338 reset_regdomains(false, rd);
Johannes Berg734366d2008-09-15 10:56:48 +0200339
340 cfg80211_world_regdom = rd;
Johannes Berg734366d2008-09-15 10:56:48 +0200341}
Johannes Berg734366d2008-09-15 10:56:48 +0200342
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200343bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100344{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700345 if (!alpha2)
346 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100347 return alpha2[0] == '0' && alpha2[1] == '0';
Johannes Berg8318d782008-01-24 19:38:38 +0100348}
349
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200350static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700351{
352 if (!alpha2)
353 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100354 return alpha2[0] && alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700355}
Johannes Berg8318d782008-01-24 19:38:38 +0100356
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200357static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700358{
359 if (!alpha2)
360 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500361 /*
362 * Special case where regulatory domain was built by driver
363 * but a specific alpha2 cannot be determined
364 */
Johannes Berg1a919312012-12-03 17:21:11 +0100365 return alpha2[0] == '9' && alpha2[1] == '9';
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700366}
367
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800368static bool is_intersected_alpha2(const char *alpha2)
369{
370 if (!alpha2)
371 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500372 /*
373 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800374 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500375 * structures
376 */
Johannes Berg1a919312012-12-03 17:21:11 +0100377 return alpha2[0] == '9' && alpha2[1] == '8';
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800378}
379
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200380static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700381{
382 if (!alpha2)
383 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100384 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700385}
386
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200387static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388{
389 if (!alpha2_x || !alpha2_y)
390 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100391 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700392}
393
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500394static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700395{
Johannes Berg458f4f92012-12-06 15:47:38 +0100396 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500397
Johannes Berg458f4f92012-12-06 15:47:38 +0100398 if (!r)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700399 return true;
Johannes Berg458f4f92012-12-06 15:47:38 +0100400 return !alpha2_equal(r->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700401}
402
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500403/*
404 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
405 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
406 * has ever been issued.
407 */
408static bool is_user_regdom_saved(void)
409{
410 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
411 return false;
412
413 /* This would indicate a mistake on the design */
Johannes Berg1a919312012-12-03 17:21:11 +0100414 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500415 "Unexpected user alpha2: %c%c\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100416 user_alpha2[0], user_alpha2[1]))
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500417 return false;
418
419 return true;
420}
421
Johannes Berge9763c32012-12-03 16:59:46 +0100422static const struct ieee80211_regdomain *
423reg_copy_regd(const struct ieee80211_regdomain *src_regd)
John W. Linville3b377ea2009-12-18 17:59:01 -0500424{
425 struct ieee80211_regdomain *regd;
Johannes Berge9763c32012-12-03 16:59:46 +0100426 int size_of_regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500427 unsigned int i;
428
Johannes Berg82f20852012-12-04 12:49:16 +0100429 size_of_regd =
430 sizeof(struct ieee80211_regdomain) +
431 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
John W. Linville3b377ea2009-12-18 17:59:01 -0500432
433 regd = kzalloc(size_of_regd, GFP_KERNEL);
434 if (!regd)
Johannes Berge9763c32012-12-03 16:59:46 +0100435 return ERR_PTR(-ENOMEM);
John W. Linville3b377ea2009-12-18 17:59:01 -0500436
437 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
438
439 for (i = 0; i < src_regd->n_reg_rules; i++)
440 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
Johannes Berge9763c32012-12-03 16:59:46 +0100441 sizeof(struct ieee80211_reg_rule));
John W. Linville3b377ea2009-12-18 17:59:01 -0500442
Johannes Berge9763c32012-12-03 16:59:46 +0100443 return regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500444}
445
446#ifdef CONFIG_CFG80211_INTERNAL_REGDB
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 Bergfd453d32015-10-15 10:22:12 +0200478static int reg_query_builtin(const char *alpha2)
John W. Linville3b377ea2009-12-18 17:59:01 -0500479{
Johannes Bergc7d319e52015-10-15 09:03:05 +0200480 const struct ieee80211_regdomain *regdom = NULL;
481 struct reg_regdb_apply_request *request;
482 unsigned int i;
John W. Linville3b377ea2009-12-18 17:59:01 -0500483
Johannes Bergc7d319e52015-10-15 09:03:05 +0200484 for (i = 0; i < reg_regdb_size; i++) {
485 if (alpha2_equal(alpha2, reg_regdb[i]->alpha2)) {
486 regdom = reg_regdb[i];
487 break;
488 }
489 }
490
491 if (!regdom)
492 return -ENODATA;
493
494 request = kzalloc(sizeof(struct reg_regdb_apply_request), GFP_KERNEL);
John W. Linville3b377ea2009-12-18 17:59:01 -0500495 if (!request)
Johannes Bergc7d319e52015-10-15 09:03:05 +0200496 return -ENOMEM;
John W. Linville3b377ea2009-12-18 17:59:01 -0500497
Johannes Bergc7d319e52015-10-15 09:03:05 +0200498 request->regdom = reg_copy_regd(regdom);
499 if (IS_ERR_OR_NULL(request->regdom)) {
500 kfree(request);
501 return -ENOMEM;
502 }
John W. Linville3b377ea2009-12-18 17:59:01 -0500503
Johannes Bergc7d319e52015-10-15 09:03:05 +0200504 mutex_lock(&reg_regdb_apply_mutex);
505 list_add_tail(&request->list, &reg_regdb_apply_list);
506 mutex_unlock(&reg_regdb_apply_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500507
508 schedule_work(&reg_regdb_work);
Johannes Bergc7d319e52015-10-15 09:03:05 +0200509
510 return 0;
John W. Linville3b377ea2009-12-18 17:59:01 -0500511}
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700512
513/* Feel free to add any other sanity checks here */
514static void reg_regdb_size_check(void)
515{
516 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
517 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
518}
John W. Linville3b377ea2009-12-18 17:59:01 -0500519#else
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700520static inline void reg_regdb_size_check(void) {}
Johannes Bergfd453d32015-10-15 10:22:12 +0200521static inline int reg_query_builtin(const char *alpha2)
Johannes Bergc7d319e52015-10-15 09:03:05 +0200522{
523 return -ENODATA;
524}
John W. Linville3b377ea2009-12-18 17:59:01 -0500525#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
526
Johannes Bergb6863032015-10-15 09:25:18 +0200527#ifdef CONFIG_CFG80211_CRDA_SUPPORT
528/* Max number of consecutive attempts to communicate with CRDA */
529#define REG_MAX_CRDA_TIMEOUTS 10
530
531static u32 reg_crda_timeouts;
532
533static void crda_timeout_work(struct work_struct *work);
534static DECLARE_DELAYED_WORK(crda_timeout, crda_timeout_work);
535
536static void crda_timeout_work(struct work_struct *work)
537{
Johannes Bergc799ba62015-12-11 15:31:10 +0100538 pr_debug("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
Johannes Bergb6863032015-10-15 09:25:18 +0200539 rtnl_lock();
540 reg_crda_timeouts++;
541 restore_regulatory_settings(true);
542 rtnl_unlock();
543}
544
545static void cancel_crda_timeout(void)
546{
547 cancel_delayed_work(&crda_timeout);
548}
549
550static void cancel_crda_timeout_sync(void)
551{
552 cancel_delayed_work_sync(&crda_timeout);
553}
554
555static void reset_crda_timeouts(void)
556{
557 reg_crda_timeouts = 0;
558}
559
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500560/*
561 * This lets us keep regulatory code which is updated on a regulatory
Johannes Berg1226d252014-02-25 15:43:36 +0100562 * basis in userspace.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500563 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700564static int call_crda(const char *alpha2)
565{
Johannes Berg1226d252014-02-25 15:43:36 +0100566 char country[12];
567 char *env[] = { country, NULL };
Johannes Bergc7d319e52015-10-15 09:03:05 +0200568 int ret;
Johannes Berg1226d252014-02-25 15:43:36 +0100569
570 snprintf(country, sizeof(country), "COUNTRY=%c%c",
571 alpha2[0], alpha2[1]);
572
Ilan peerc37722b2015-03-30 15:15:49 +0300573 if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) {
Thomas Petazzoni042ab5f2015-07-09 15:35:15 +0200574 pr_debug("Exceeded CRDA call max attempts. Not calling CRDA\n");
Ilan peerc37722b2015-03-30 15:15:49 +0300575 return -EINVAL;
576 }
577
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700578 if (!is_world_regdom((char *) alpha2))
Thomas Petazzoni042ab5f2015-07-09 15:35:15 +0200579 pr_debug("Calling CRDA for country: %c%c\n",
Johannes Bergc799ba62015-12-11 15:31:10 +0100580 alpha2[0], alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700581 else
Thomas Petazzoni042ab5f2015-07-09 15:35:15 +0200582 pr_debug("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700583
Johannes Bergc7d319e52015-10-15 09:03:05 +0200584 ret = kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
585 if (ret)
586 return ret;
587
588 queue_delayed_work(system_power_efficient_wq,
Johannes Bergb6863032015-10-15 09:25:18 +0200589 &crda_timeout, msecs_to_jiffies(3142));
Johannes Bergc7d319e52015-10-15 09:03:05 +0200590 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700591}
Johannes Bergb6863032015-10-15 09:25:18 +0200592#else
593static inline void cancel_crda_timeout(void) {}
594static inline void cancel_crda_timeout_sync(void) {}
595static inline void reset_crda_timeouts(void) {}
596static inline int call_crda(const char *alpha2)
597{
598 return -ENODATA;
599}
600#endif /* CONFIG_CFG80211_CRDA_SUPPORT */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700601
Johannes Bergcecbb062015-10-15 08:47:34 +0200602static bool reg_query_database(struct regulatory_request *request)
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -0800603{
Johannes Bergc7d319e52015-10-15 09:03:05 +0200604 /* query internal regulatory database (if it exists) */
Johannes Bergfd453d32015-10-15 10:22:12 +0200605 if (reg_query_builtin(request->alpha2) == 0)
Johannes Bergc7d319e52015-10-15 09:03:05 +0200606 return true;
Ilan peereeca9fc2015-03-04 00:32:07 -0500607
Johannes Bergc7d319e52015-10-15 09:03:05 +0200608 if (call_crda(request->alpha2) == 0)
609 return true;
610
611 return false;
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -0800612}
613
Luis R. Rodrigueze4387682013-11-05 09:18:01 -0800614bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700615{
Johannes Bergc492db32012-12-06 16:29:25 +0100616 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400617
Johannes Bergc492db32012-12-06 16:29:25 +0100618 if (!lr || lr->processed)
Johannes Bergf6037d02008-10-21 11:01:33 +0200619 return false;
620
Johannes Bergc492db32012-12-06 16:29:25 +0100621 return alpha2_equal(lr->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700622}
623
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100624static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
625{
626 struct regulatory_request *lr = get_last_request();
627
628 /*
629 * Follow the driver's regulatory domain, if present, unless a country
630 * IE has been processed or a user wants to help complaince further
631 */
632 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
633 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
634 wiphy->regd)
635 return get_wiphy_regdom(wiphy);
636
637 return get_cfg80211_regdom();
638}
639
Arik Nemtsova6d4a532014-10-23 09:37:33 +0300640static unsigned int
641reg_get_max_bandwidth_from_range(const struct ieee80211_regdomain *rd,
642 const struct ieee80211_reg_rule *rule)
Janusz Dziedzic97524822014-01-30 09:52:20 +0100643{
644 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
645 const struct ieee80211_freq_range *freq_range_tmp;
646 const struct ieee80211_reg_rule *tmp;
647 u32 start_freq, end_freq, idx, no;
648
649 for (idx = 0; idx < rd->n_reg_rules; idx++)
650 if (rule == &rd->reg_rules[idx])
651 break;
652
653 if (idx == rd->n_reg_rules)
654 return 0;
655
656 /* get start_freq */
657 no = idx;
658
659 while (no) {
660 tmp = &rd->reg_rules[--no];
661 freq_range_tmp = &tmp->freq_range;
662
663 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
664 break;
665
Janusz Dziedzic97524822014-01-30 09:52:20 +0100666 freq_range = freq_range_tmp;
667 }
668
669 start_freq = freq_range->start_freq_khz;
670
671 /* get end_freq */
672 freq_range = &rule->freq_range;
673 no = idx;
674
675 while (no < rd->n_reg_rules - 1) {
676 tmp = &rd->reg_rules[++no];
677 freq_range_tmp = &tmp->freq_range;
678
679 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
680 break;
681
Janusz Dziedzic97524822014-01-30 09:52:20 +0100682 freq_range = freq_range_tmp;
683 }
684
685 end_freq = freq_range->end_freq_khz;
686
687 return end_freq - start_freq;
688}
689
Arik Nemtsova6d4a532014-10-23 09:37:33 +0300690unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
691 const struct ieee80211_reg_rule *rule)
692{
693 unsigned int bw = reg_get_max_bandwidth_from_range(rd, rule);
694
695 if (rule->flags & NL80211_RRF_NO_160MHZ)
696 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(80));
697 if (rule->flags & NL80211_RRF_NO_80MHZ)
698 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(40));
699
700 /*
701 * HT40+/HT40- limits are handled per-channel. Only limit BW if both
702 * are not allowed.
703 */
704 if (rule->flags & NL80211_RRF_NO_HT40MINUS &&
705 rule->flags & NL80211_RRF_NO_HT40PLUS)
706 bw = min_t(unsigned int, bw, MHZ_TO_KHZ(20));
707
708 return bw;
709}
710
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700711/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200712static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700713{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200714 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700715 u32 freq_diff;
716
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800717 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700718 return false;
719
720 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
721 return false;
722
723 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
724
Roel Kluinbd05f282009-03-03 22:55:21 +0100725 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg1a919312012-12-03 17:21:11 +0100726 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700727 return false;
728
729 return true;
730}
731
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200732static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700733{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200734 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700735 unsigned int i;
736
737 if (!rd->n_reg_rules)
738 return false;
739
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800740 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
741 return false;
742
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700743 for (i = 0; i < rd->n_reg_rules; i++) {
744 reg_rule = &rd->reg_rules[i];
745 if (!is_valid_reg_rule(reg_rule))
746 return false;
747 }
748
749 return true;
750}
751
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800752/**
753 * freq_in_rule_band - tells us if a frequency is in a frequency band
754 * @freq_range: frequency rule we want to query
755 * @freq_khz: frequency we are inquiring about
756 *
757 * This lets us know if a specific frequency rule is or is not relevant to
758 * a specific frequency's band. Bands are device specific and artificial
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200759 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
760 * however it is safe for now to assume that a frequency rule should not be
761 * part of a frequency's band if the start freq or end freq are off by more
762 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
763 * 60 GHz band.
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800764 * This resolution can be lowered and should be considered as we add
765 * regulatory rule support for other "bands".
766 **/
767static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg1a919312012-12-03 17:21:11 +0100768 u32 freq_khz)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800769{
770#define ONE_GHZ_IN_KHZ 1000000
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200771 /*
772 * From 802.11ad: directional multi-gigabit (DMG):
773 * Pertaining to operation in a frequency band containing a channel
774 * with the Channel starting frequency above 45 GHz.
775 */
776 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
777 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
778 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800779 return true;
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200780 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800781 return true;
782 return false;
783#undef ONE_GHZ_IN_KHZ
784}
785
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500786/*
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100787 * Later on we can perhaps use the more restrictive DFS
788 * region but we don't have information for that yet so
789 * for now simply disallow conflicts.
790 */
791static enum nl80211_dfs_regions
792reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
793 const enum nl80211_dfs_regions dfs_region2)
794{
795 if (dfs_region1 != dfs_region2)
796 return NL80211_DFS_UNSET;
797 return dfs_region1;
798}
799
800/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500801 * Helper for regdom_intersect(), this does the real
802 * mathematical intersection fun
803 */
Janusz Dziedzic97524822014-01-30 09:52:20 +0100804static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
805 const struct ieee80211_regdomain *rd2,
806 const struct ieee80211_reg_rule *rule1,
Johannes Berg1a919312012-12-03 17:21:11 +0100807 const struct ieee80211_reg_rule *rule2,
808 struct ieee80211_reg_rule *intersected_rule)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700809{
810 const struct ieee80211_freq_range *freq_range1, *freq_range2;
811 struct ieee80211_freq_range *freq_range;
812 const struct ieee80211_power_rule *power_rule1, *power_rule2;
813 struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +0100814 u32 freq_diff, max_bandwidth1, max_bandwidth2;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700815
816 freq_range1 = &rule1->freq_range;
817 freq_range2 = &rule2->freq_range;
818 freq_range = &intersected_rule->freq_range;
819
820 power_rule1 = &rule1->power_rule;
821 power_rule2 = &rule2->power_rule;
822 power_rule = &intersected_rule->power_rule;
823
824 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100825 freq_range2->start_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700826 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100827 freq_range2->end_freq_khz);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100828
829 max_bandwidth1 = freq_range1->max_bandwidth_khz;
830 max_bandwidth2 = freq_range2->max_bandwidth_khz;
831
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100832 if (rule1->flags & NL80211_RRF_AUTO_BW)
833 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
834 if (rule2->flags & NL80211_RRF_AUTO_BW)
835 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100836
837 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700838
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100839 intersected_rule->flags = rule1->flags | rule2->flags;
840
841 /*
842 * In case NL80211_RRF_AUTO_BW requested for both rules
843 * set AUTO_BW in intersected rule also. Next we will
844 * calculate BW correctly in handle_channel function.
845 * In other case remove AUTO_BW flag while we calculate
846 * maximum bandwidth correctly and auto calculation is
847 * not required.
848 */
849 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
850 (rule2->flags & NL80211_RRF_AUTO_BW))
851 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
852 else
853 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
854
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700855 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
856 if (freq_range->max_bandwidth_khz > freq_diff)
857 freq_range->max_bandwidth_khz = freq_diff;
858
859 power_rule->max_eirp = min(power_rule1->max_eirp,
860 power_rule2->max_eirp);
861 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
862 power_rule2->max_antenna_gain);
863
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100864 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
865 rule2->dfs_cac_ms);
866
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700867 if (!is_valid_reg_rule(intersected_rule))
868 return -EINVAL;
869
870 return 0;
871}
872
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300873/* check whether old rule contains new rule */
874static bool rule_contains(struct ieee80211_reg_rule *r1,
875 struct ieee80211_reg_rule *r2)
876{
877 /* for simplicity, currently consider only same flags */
878 if (r1->flags != r2->flags)
879 return false;
880
881 /* verify r1 is more restrictive */
882 if ((r1->power_rule.max_antenna_gain >
883 r2->power_rule.max_antenna_gain) ||
884 r1->power_rule.max_eirp > r2->power_rule.max_eirp)
885 return false;
886
887 /* make sure r2's range is contained within r1 */
888 if (r1->freq_range.start_freq_khz > r2->freq_range.start_freq_khz ||
889 r1->freq_range.end_freq_khz < r2->freq_range.end_freq_khz)
890 return false;
891
892 /* and finally verify that r1.max_bw >= r2.max_bw */
893 if (r1->freq_range.max_bandwidth_khz <
894 r2->freq_range.max_bandwidth_khz)
895 return false;
896
897 return true;
898}
899
900/* add or extend current rules. do nothing if rule is already contained */
901static void add_rule(struct ieee80211_reg_rule *rule,
902 struct ieee80211_reg_rule *reg_rules, u32 *n_rules)
903{
904 struct ieee80211_reg_rule *tmp_rule;
905 int i;
906
907 for (i = 0; i < *n_rules; i++) {
908 tmp_rule = &reg_rules[i];
909 /* rule is already contained - do nothing */
910 if (rule_contains(tmp_rule, rule))
911 return;
912
913 /* extend rule if possible */
914 if (rule_contains(rule, tmp_rule)) {
915 memcpy(tmp_rule, rule, sizeof(*rule));
916 return;
917 }
918 }
919
920 memcpy(&reg_rules[*n_rules], rule, sizeof(*rule));
921 (*n_rules)++;
922}
923
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700924/**
925 * regdom_intersect - do the intersection between two regulatory domains
926 * @rd1: first regulatory domain
927 * @rd2: second regulatory domain
928 *
929 * Use this function to get the intersection between two regulatory domains.
930 * Once completed we will mark the alpha2 for the rd as intersected, "98",
931 * as no one single alpha2 can represent this regulatory domain.
932 *
933 * Returns a pointer to the regulatory domain structure which will hold the
934 * resulting intersection of rules between rd1 and rd2. We will
935 * kzalloc() this structure for you.
936 */
Johannes Berg1a919312012-12-03 17:21:11 +0100937static struct ieee80211_regdomain *
938regdom_intersect(const struct ieee80211_regdomain *rd1,
939 const struct ieee80211_regdomain *rd2)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700940{
941 int r, size_of_regd;
942 unsigned int x, y;
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300943 unsigned int num_rules = 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700944 const struct ieee80211_reg_rule *rule1, *rule2;
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300945 struct ieee80211_reg_rule intersected_rule;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700946 struct ieee80211_regdomain *rd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700947
948 if (!rd1 || !rd2)
949 return NULL;
950
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500951 /*
952 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700953 * build them. This is to so we can malloc() and free() a
954 * regdomain once. The reason we use reg_rules_intersect() here
955 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500956 * All rules that do check out OK are valid.
957 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700958
959 for (x = 0; x < rd1->n_reg_rules; x++) {
960 rule1 = &rd1->reg_rules[x];
961 for (y = 0; y < rd2->n_reg_rules; y++) {
962 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100963 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300964 &intersected_rule))
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700965 num_rules++;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700966 }
967 }
968
969 if (!num_rules)
970 return NULL;
971
972 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg82f20852012-12-04 12:49:16 +0100973 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700974
975 rd = kzalloc(size_of_regd, GFP_KERNEL);
976 if (!rd)
977 return NULL;
978
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300979 for (x = 0; x < rd1->n_reg_rules; x++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700980 rule1 = &rd1->reg_rules[x];
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300981 for (y = 0; y < rd2->n_reg_rules; y++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700982 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100983 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300984 &intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500985 /*
986 * No need to memset here the intersected rule here as
987 * we're not using the stack anymore
988 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700989 if (r)
990 continue;
Eliad Pellera62a1ae2014-09-03 15:25:03 +0300991
992 add_rule(&intersected_rule, rd->reg_rules,
993 &rd->n_reg_rules);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700994 }
995 }
996
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700997 rd->alpha2[0] = '9';
998 rd->alpha2[1] = '8';
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100999 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
1000 rd2->dfs_region);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001001
1002 return rd;
1003}
1004
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001005/*
1006 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1007 * want to just have the channel structure use these
1008 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001009static u32 map_regdom_flags(u32 rd_flags)
1010{
1011 u32 channel_flags = 0;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001012 if (rd_flags & NL80211_RRF_NO_IR_ALL)
1013 channel_flags |= IEEE80211_CHAN_NO_IR;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001014 if (rd_flags & NL80211_RRF_DFS)
1015 channel_flags |= IEEE80211_CHAN_RADAR;
Seth Forshee03f6b082012-08-01 15:58:42 -05001016 if (rd_flags & NL80211_RRF_NO_OFDM)
1017 channel_flags |= IEEE80211_CHAN_NO_OFDM;
David Spinadel570dbde2014-02-23 09:12:59 +02001018 if (rd_flags & NL80211_RRF_NO_OUTDOOR)
1019 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
Arik Nemtsov06f207f2015-05-06 16:28:31 +03001020 if (rd_flags & NL80211_RRF_IR_CONCURRENT)
1021 channel_flags |= IEEE80211_CHAN_IR_CONCURRENT;
Arik Nemtsova6d4a532014-10-23 09:37:33 +03001022 if (rd_flags & NL80211_RRF_NO_HT40MINUS)
1023 channel_flags |= IEEE80211_CHAN_NO_HT40MINUS;
1024 if (rd_flags & NL80211_RRF_NO_HT40PLUS)
1025 channel_flags |= IEEE80211_CHAN_NO_HT40PLUS;
1026 if (rd_flags & NL80211_RRF_NO_80MHZ)
1027 channel_flags |= IEEE80211_CHAN_NO_80MHZ;
1028 if (rd_flags & NL80211_RRF_NO_160MHZ)
1029 channel_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001030 return channel_flags;
1031}
1032
Johannes Berg361c9c82012-12-06 15:57:14 +01001033static const struct ieee80211_reg_rule *
Michal Sojka49172872015-11-23 19:27:14 +01001034freq_reg_info_regd(u32 center_freq,
Matthias May4edd5692015-07-17 15:28:39 +02001035 const struct ieee80211_regdomain *regd, u32 bw)
Johannes Berg8318d782008-01-24 19:38:38 +01001036{
1037 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001038 bool band_rule_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001039 bool bw_fits = false;
1040
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001041 if (!regd)
Johannes Berg361c9c82012-12-06 15:57:14 +01001042 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001043
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001044 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001045 const struct ieee80211_reg_rule *rr;
1046 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001047
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001048 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001049 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001050
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001051 /*
1052 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001053 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001054 * not overwrite it once found
1055 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001056 if (!band_rule_found)
1057 band_rule_found = freq_in_rule_band(fr, center_freq);
1058
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001059 bw_fits = cfg80211_does_bw_fit_range(fr, center_freq, bw);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001060
Johannes Berg361c9c82012-12-06 15:57:14 +01001061 if (band_rule_found && bw_fits)
1062 return rr;
Johannes Berg8318d782008-01-24 19:38:38 +01001063 }
1064
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001065 if (!band_rule_found)
Johannes Berg361c9c82012-12-06 15:57:14 +01001066 return ERR_PTR(-ERANGE);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001067
Johannes Berg361c9c82012-12-06 15:57:14 +01001068 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001069}
1070
Johannes Berg8de1c632015-08-21 14:13:06 +02001071static const struct ieee80211_reg_rule *
1072__freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 min_bw)
Matthias May4edd5692015-07-17 15:28:39 +02001073{
1074 const struct ieee80211_regdomain *regd = reg_get_regdomain(wiphy);
1075 const struct ieee80211_reg_rule *reg_rule = NULL;
1076 u32 bw;
1077
1078 for (bw = MHZ_TO_KHZ(20); bw >= min_bw; bw = bw / 2) {
Michal Sojka49172872015-11-23 19:27:14 +01001079 reg_rule = freq_reg_info_regd(center_freq, regd, bw);
Matthias May4edd5692015-07-17 15:28:39 +02001080 if (!IS_ERR(reg_rule))
1081 return reg_rule;
1082 }
1083
1084 return reg_rule;
1085}
1086
Johannes Berg361c9c82012-12-06 15:57:14 +01001087const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
1088 u32 center_freq)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001089{
Matthias May4edd5692015-07-17 15:28:39 +02001090 return __freq_reg_info(wiphy, center_freq, MHZ_TO_KHZ(20));
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001091}
John W. Linville4f366c52010-07-15 14:57:33 -04001092EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001093
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001094const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301095{
1096 switch (initiator) {
1097 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001098 return "core";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301099 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001100 return "user";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301101 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001102 return "driver";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301103 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001104 return "country IE";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301105 default:
1106 WARN_ON(1);
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001107 return "bug";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301108 }
1109}
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001110EXPORT_SYMBOL(reg_initiator_name);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301111
Michal Sojka1aeb1352015-11-23 19:27:16 +01001112static uint32_t reg_rule_to_chan_bw_flags(const struct ieee80211_regdomain *regd,
1113 const struct ieee80211_reg_rule *reg_rule,
1114 const struct ieee80211_channel *chan)
1115{
1116 const struct ieee80211_freq_range *freq_range = NULL;
1117 u32 max_bandwidth_khz, bw_flags = 0;
1118
1119 freq_range = &reg_rule->freq_range;
1120
1121 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1122 /* Check if auto calculation requested */
1123 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
1124 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1125
1126 /* If we get a reg_rule we can assume that at least 5Mhz fit */
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001127 if (!cfg80211_does_bw_fit_range(freq_range,
1128 MHZ_TO_KHZ(chan->center_freq),
1129 MHZ_TO_KHZ(10)))
Michal Sojka1aeb1352015-11-23 19:27:16 +01001130 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
Rafał Miłecki4787cfa2017-01-04 18:58:30 +01001131 if (!cfg80211_does_bw_fit_range(freq_range,
1132 MHZ_TO_KHZ(chan->center_freq),
1133 MHZ_TO_KHZ(20)))
Michal Sojka1aeb1352015-11-23 19:27:16 +01001134 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1135
1136 if (max_bandwidth_khz < MHZ_TO_KHZ(10))
1137 bw_flags |= IEEE80211_CHAN_NO_10MHZ;
1138 if (max_bandwidth_khz < MHZ_TO_KHZ(20))
1139 bw_flags |= IEEE80211_CHAN_NO_20MHZ;
1140 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
1141 bw_flags |= IEEE80211_CHAN_NO_HT40;
1142 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
1143 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
1144 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
1145 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
1146 return bw_flags;
1147}
1148
Johannes Berge33e2242014-06-23 11:06:16 +02001149/*
1150 * Note that right now we assume the desired channel bandwidth
1151 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1152 * per channel, the primary and the extension channel).
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001153 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001154static void handle_channel(struct wiphy *wiphy,
1155 enum nl80211_reg_initiator initiator,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001156 struct ieee80211_channel *chan)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001157{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001158 u32 flags, bw_flags = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001159 const struct ieee80211_reg_rule *reg_rule = NULL;
1160 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001161 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01001162 struct regulatory_request *lr = get_last_request();
Janusz Dziedzic97524822014-01-30 09:52:20 +01001163 const struct ieee80211_regdomain *regd;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001164
Johannes Bergc492db32012-12-06 16:29:25 +01001165 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001166
1167 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001168
Johannes Berg361c9c82012-12-06 15:57:14 +01001169 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1170 if (IS_ERR(reg_rule)) {
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001171 /*
1172 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001173 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001174 * from a Country IE and the Country IE had no information
1175 * about a band. The IEEE 802.11 spec allows for an AP
1176 * to send only a subset of the regulatory rules allowed,
1177 * so an AP in the US that only supports 2.4 GHz may only send
1178 * a country IE with information for the 2.4 GHz band
1179 * while 5 GHz is still supported.
1180 */
1181 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg361c9c82012-12-06 15:57:14 +01001182 PTR_ERR(reg_rule) == -ERANGE)
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001183 return;
1184
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001185 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1186 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001187 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001188 pr_debug("Disabling freq %d MHz for good\n",
1189 chan->center_freq);
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001190 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1191 chan->flags = chan->orig_flags;
1192 } else {
Johannes Bergc799ba62015-12-11 15:31:10 +01001193 pr_debug("Disabling freq %d MHz\n",
1194 chan->center_freq);
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001195 chan->flags |= IEEE80211_CHAN_DISABLED;
1196 }
Johannes Berg8318d782008-01-24 19:38:38 +01001197 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001198 }
Johannes Berg8318d782008-01-24 19:38:38 +01001199
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001200 regd = reg_get_regdomain(wiphy);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301201
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001202 power_rule = &reg_rule->power_rule;
Michal Sojka1aeb1352015-11-23 19:27:16 +01001203 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001204
Johannes Bergc492db32012-12-06 16:29:25 +01001205 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001206 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001207 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001208 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001209 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001210 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001211 * settings
1212 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001213 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001214 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001215 chan->max_antenna_gain = chan->orig_mag =
1216 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001217 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001218 (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic4f267c12014-04-09 13:47:12 +02001219
1220 if (chan->flags & IEEE80211_CHAN_RADAR) {
1221 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1222 if (reg_rule->dfs_cac_ms)
1223 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1224 }
1225
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001226 return;
1227 }
1228
Simon Wunderlich04f39042013-02-08 18:16:19 +01001229 chan->dfs_state = NL80211_DFS_USABLE;
1230 chan->dfs_state_entered = jiffies;
1231
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +05301232 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001233 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg1a919312012-12-03 17:21:11 +01001234 chan->max_antenna_gain =
1235 min_t(int, chan->orig_mag,
1236 MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +02001237 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic089027e2014-02-21 19:46:12 +01001238
1239 if (chan->flags & IEEE80211_CHAN_RADAR) {
1240 if (reg_rule->dfs_cac_ms)
1241 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1242 else
1243 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1244 }
1245
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001246 if (chan->orig_mpwr) {
1247 /*
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001248 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1249 * will always follow the passed country IE power settings.
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001250 */
1251 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001252 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001253 chan->max_power = chan->max_reg_power;
1254 else
1255 chan->max_power = min(chan->orig_mpwr,
1256 chan->max_reg_power);
1257 } else
1258 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +01001259}
1260
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001261static void handle_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001262 enum nl80211_reg_initiator initiator,
1263 struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +01001264{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001265 unsigned int i;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001266
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001267 if (!sband)
1268 return;
Johannes Berg8318d782008-01-24 19:38:38 +01001269
1270 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001271 handle_channel(wiphy, initiator, &sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +01001272}
1273
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001274static bool reg_request_cell_base(struct regulatory_request *request)
1275{
1276 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1277 return false;
Johannes Berg1a919312012-12-03 17:21:11 +01001278 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001279}
1280
1281bool reg_last_request_cell_base(void)
1282{
Johannes Berg38fd2142013-05-10 19:17:17 +02001283 return reg_request_cell_base(get_last_request());
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001284}
1285
Ilan Peer94fc6612014-02-23 09:13:00 +02001286#ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001287/* Core specific check */
Johannes Berg2f922122012-12-03 17:54:55 +01001288static enum reg_request_treatment
1289reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001290{
Johannes Bergc492db32012-12-06 16:29:25 +01001291 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001292
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001293 if (!reg_num_devs_support_basehint)
Johannes Berg2f922122012-12-03 17:54:55 +01001294 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001295
Johannes Bergc492db32012-12-06 16:29:25 +01001296 if (reg_request_cell_base(lr) &&
Johannes Berg1a919312012-12-03 17:21:11 +01001297 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001298 return REG_REQ_ALREADY_SET;
Johannes Berg1a919312012-12-03 17:21:11 +01001299
Johannes Berg2f922122012-12-03 17:54:55 +01001300 return REG_REQ_OK;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001301}
1302
1303/* Device specific check */
1304static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1305{
Johannes Berg1a919312012-12-03 17:21:11 +01001306 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001307}
1308#else
Johannes Berga515de62015-10-15 14:28:56 +02001309static enum reg_request_treatment
1310reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001311{
Johannes Berg2f922122012-12-03 17:54:55 +01001312 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001313}
Johannes Berg1a919312012-12-03 17:21:11 +01001314
1315static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001316{
1317 return true;
1318}
1319#endif
1320
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001321static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1322{
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001323 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1324 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001325 return true;
1326 return false;
1327}
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001328
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001329static bool ignore_reg_update(struct wiphy *wiphy,
1330 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001331{
Johannes Bergc492db32012-12-06 16:29:25 +01001332 struct regulatory_request *lr = get_last_request();
1333
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02001334 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1335 return true;
1336
Johannes Bergc492db32012-12-06 16:29:25 +01001337 if (!lr) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001338 pr_debug("Ignoring regulatory request set by %s since last_request is not set\n",
1339 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001340 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301341 }
1342
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001343 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001344 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001345 pr_debug("Ignoring regulatory request set by %s since the driver uses its own custom regulatory domain\n",
1346 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001347 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301348 }
1349
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001350 /*
1351 * wiphy->regd will be set once the device has its own
1352 * desired regulatory domain set
1353 */
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001354 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -07001355 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Bergc492db32012-12-06 16:29:25 +01001356 !is_world_regdom(lr->alpha2)) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001357 pr_debug("Ignoring regulatory request set by %s since the driver requires its own regulatory domain to be set first\n",
1358 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001359 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301360 }
1361
Johannes Bergc492db32012-12-06 16:29:25 +01001362 if (reg_request_cell_base(lr))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001363 return reg_dev_ignore_cell_hint(wiphy);
1364
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001365 return false;
1366}
1367
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001368static bool reg_is_world_roaming(struct wiphy *wiphy)
1369{
1370 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1371 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1372 struct regulatory_request *lr = get_last_request();
1373
1374 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1375 return true;
1376
1377 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001378 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001379 return true;
1380
1381 return false;
1382}
1383
Johannes Berg1a919312012-12-03 17:21:11 +01001384static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001385 struct reg_beacon *reg_beacon)
1386{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001387 struct ieee80211_supported_band *sband;
1388 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001389 bool channel_changed = false;
1390 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001391
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001392 sband = wiphy->bands[reg_beacon->chan.band];
1393 chan = &sband->channels[chan_idx];
1394
1395 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1396 return;
1397
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001398 if (chan->beacon_found)
1399 return;
1400
1401 chan->beacon_found = true;
1402
Luis R. Rodriguez0f500a52012-12-19 10:53:04 -08001403 if (!reg_is_world_roaming(wiphy))
1404 return;
1405
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001406 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001407 return;
1408
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001409 chan_before.center_freq = chan->center_freq;
1410 chan_before.flags = chan->flags;
1411
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001412 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1413 chan->flags &= ~IEEE80211_CHAN_NO_IR;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001414 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001415 }
1416
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001417 if (channel_changed)
1418 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001419}
1420
1421/*
1422 * Called when a scan on a wiphy finds a beacon on
1423 * new channel
1424 */
1425static void wiphy_update_new_beacon(struct wiphy *wiphy,
1426 struct reg_beacon *reg_beacon)
1427{
1428 unsigned int i;
1429 struct ieee80211_supported_band *sband;
1430
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001431 if (!wiphy->bands[reg_beacon->chan.band])
1432 return;
1433
1434 sband = wiphy->bands[reg_beacon->chan.band];
1435
1436 for (i = 0; i < sband->n_channels; i++)
1437 handle_reg_beacon(wiphy, i, reg_beacon);
1438}
1439
1440/*
1441 * Called upon reg changes or a new wiphy is added
1442 */
1443static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1444{
1445 unsigned int i;
1446 struct ieee80211_supported_band *sband;
1447 struct reg_beacon *reg_beacon;
1448
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001449 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1450 if (!wiphy->bands[reg_beacon->chan.band])
1451 continue;
1452 sband = wiphy->bands[reg_beacon->chan.band];
1453 for (i = 0; i < sband->n_channels; i++)
1454 handle_reg_beacon(wiphy, i, reg_beacon);
1455 }
1456}
1457
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001458/* Reap the advantages of previously found beacons */
1459static void reg_process_beacons(struct wiphy *wiphy)
1460{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001461 /*
1462 * Means we are just firing up cfg80211, so no beacons would
1463 * have been processed yet.
1464 */
1465 if (!last_request)
1466 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001467 wiphy_update_beacon_reg(wiphy);
1468}
1469
Johannes Berg1a919312012-12-03 17:21:11 +01001470static bool is_ht40_allowed(struct ieee80211_channel *chan)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001471{
1472 if (!chan)
Johannes Berg1a919312012-12-03 17:21:11 +01001473 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001474 if (chan->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg1a919312012-12-03 17:21:11 +01001475 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001476 /* This would happen when regulatory rules disallow HT40 completely */
Felix Fietkau55b183a2013-01-11 14:22:58 +01001477 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1478 return false;
1479 return true;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001480}
1481
1482static void reg_process_ht_flags_channel(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001483 struct ieee80211_channel *channel)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001484{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001485 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001486 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001487 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001488 unsigned int i;
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001489 u32 flags;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001490
Johannes Berg1a919312012-12-03 17:21:11 +01001491 if (!is_ht40_allowed(channel)) {
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001492 channel->flags |= IEEE80211_CHAN_NO_HT40;
1493 return;
1494 }
1495
1496 /*
1497 * We need to ensure the extension channels exist to
1498 * be able to use HT40- or HT40+, this finds them (or not)
1499 */
1500 for (i = 0; i < sband->n_channels; i++) {
1501 struct ieee80211_channel *c = &sband->channels[i];
Johannes Berg1a919312012-12-03 17:21:11 +01001502
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001503 if (c->center_freq == (channel->center_freq - 20))
1504 channel_before = c;
1505 if (c->center_freq == (channel->center_freq + 20))
1506 channel_after = c;
1507 }
1508
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001509 flags = 0;
1510 regd = get_wiphy_regdom(wiphy);
1511 if (regd) {
1512 const struct ieee80211_reg_rule *reg_rule =
1513 freq_reg_info_regd(MHZ_TO_KHZ(channel->center_freq),
1514 regd, MHZ_TO_KHZ(20));
1515
1516 if (!IS_ERR(reg_rule))
1517 flags = reg_rule->flags;
1518 }
1519
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001520 /*
1521 * Please note that this assumes target bandwidth is 20 MHz,
1522 * if that ever changes we also need to change the below logic
1523 * to include that as well.
1524 */
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001525 if (!is_ht40_allowed(channel_before) ||
1526 flags & NL80211_RRF_NO_HT40MINUS)
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001527 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001528 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001529 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001530
Emmanuel Grumbach4e0854a2017-09-06 13:45:40 +03001531 if (!is_ht40_allowed(channel_after) ||
1532 flags & NL80211_RRF_NO_HT40PLUS)
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001533 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001534 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001535 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001536}
1537
1538static void reg_process_ht_flags_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001539 struct ieee80211_supported_band *sband)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001540{
1541 unsigned int i;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001542
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001543 if (!sband)
1544 return;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001545
1546 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001547 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001548}
1549
1550static void reg_process_ht_flags(struct wiphy *wiphy)
1551{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001552 enum nl80211_band band;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001553
1554 if (!wiphy)
1555 return;
1556
Johannes Berg57fbcce2016-04-12 15:56:15 +02001557 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001558 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001559}
1560
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001561static void reg_call_notifier(struct wiphy *wiphy,
1562 struct regulatory_request *request)
1563{
1564 if (wiphy->reg_notifier)
1565 wiphy->reg_notifier(wiphy, request);
1566}
1567
Arik Nemtsovad932f02014-11-27 09:44:55 +02001568static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev)
1569{
Arik Nemtsovad932f02014-11-27 09:44:55 +02001570 struct cfg80211_chan_def chandef;
1571 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Arik Nemtsov20658702014-12-29 11:59:59 +02001572 enum nl80211_iftype iftype;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001573
1574 wdev_lock(wdev);
Arik Nemtsov20658702014-12-29 11:59:59 +02001575 iftype = wdev->iftype;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001576
Arik Nemtsov20658702014-12-29 11:59:59 +02001577 /* make sure the interface is active */
Arik Nemtsovad932f02014-11-27 09:44:55 +02001578 if (!wdev->netdev || !netif_running(wdev->netdev))
Arik Nemtsov20658702014-12-29 11:59:59 +02001579 goto wdev_inactive_unlock;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001580
Arik Nemtsov20658702014-12-29 11:59:59 +02001581 switch (iftype) {
Arik Nemtsovad932f02014-11-27 09:44:55 +02001582 case NL80211_IFTYPE_AP:
1583 case NL80211_IFTYPE_P2P_GO:
1584 if (!wdev->beacon_interval)
Arik Nemtsov20658702014-12-29 11:59:59 +02001585 goto wdev_inactive_unlock;
1586 chandef = wdev->chandef;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001587 break;
Arik Nemtsov185076d2014-12-03 18:08:17 +02001588 case NL80211_IFTYPE_ADHOC:
1589 if (!wdev->ssid_len)
Arik Nemtsov20658702014-12-29 11:59:59 +02001590 goto wdev_inactive_unlock;
1591 chandef = wdev->chandef;
Arik Nemtsov185076d2014-12-03 18:08:17 +02001592 break;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001593 case NL80211_IFTYPE_STATION:
1594 case NL80211_IFTYPE_P2P_CLIENT:
Arik Nemtsovad932f02014-11-27 09:44:55 +02001595 if (!wdev->current_bss ||
1596 !wdev->current_bss->pub.channel)
Arik Nemtsov20658702014-12-29 11:59:59 +02001597 goto wdev_inactive_unlock;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001598
Arik Nemtsov20658702014-12-29 11:59:59 +02001599 if (!rdev->ops->get_channel ||
1600 rdev_get_channel(rdev, wdev, &chandef))
1601 cfg80211_chandef_create(&chandef,
1602 wdev->current_bss->pub.channel,
1603 NL80211_CHAN_NO_HT);
Arik Nemtsovad932f02014-11-27 09:44:55 +02001604 break;
1605 case NL80211_IFTYPE_MONITOR:
1606 case NL80211_IFTYPE_AP_VLAN:
1607 case NL80211_IFTYPE_P2P_DEVICE:
1608 /* no enforcement required */
1609 break;
1610 default:
1611 /* others not implemented for now */
1612 WARN_ON(1);
1613 break;
1614 }
1615
Arik Nemtsovad932f02014-11-27 09:44:55 +02001616 wdev_unlock(wdev);
Arik Nemtsov20658702014-12-29 11:59:59 +02001617
1618 switch (iftype) {
1619 case NL80211_IFTYPE_AP:
1620 case NL80211_IFTYPE_P2P_GO:
1621 case NL80211_IFTYPE_ADHOC:
Arik Nemtsov923b3522015-07-08 15:41:44 +03001622 return cfg80211_reg_can_beacon_relax(wiphy, &chandef, iftype);
Arik Nemtsov20658702014-12-29 11:59:59 +02001623 case NL80211_IFTYPE_STATION:
1624 case NL80211_IFTYPE_P2P_CLIENT:
1625 return cfg80211_chandef_usable(wiphy, &chandef,
1626 IEEE80211_CHAN_DISABLED);
1627 default:
1628 break;
1629 }
1630
1631 return true;
1632
1633wdev_inactive_unlock:
1634 wdev_unlock(wdev);
1635 return true;
Arik Nemtsovad932f02014-11-27 09:44:55 +02001636}
1637
1638static void reg_leave_invalid_chans(struct wiphy *wiphy)
1639{
1640 struct wireless_dev *wdev;
1641 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1642
1643 ASSERT_RTNL();
1644
Johannes Berg53873f12016-05-03 16:52:04 +03001645 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
Arik Nemtsovad932f02014-11-27 09:44:55 +02001646 if (!reg_wdev_chan_valid(wiphy, wdev))
1647 cfg80211_leave(rdev, wdev);
1648}
1649
1650static void reg_check_chans_work(struct work_struct *work)
1651{
1652 struct cfg80211_registered_device *rdev;
1653
Johannes Bergc799ba62015-12-11 15:31:10 +01001654 pr_debug("Verifying active interfaces after reg change\n");
Arik Nemtsovad932f02014-11-27 09:44:55 +02001655 rtnl_lock();
1656
1657 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1658 if (!(rdev->wiphy.regulatory_flags &
1659 REGULATORY_IGNORE_STALE_KICKOFF))
1660 reg_leave_invalid_chans(&rdev->wiphy);
1661
1662 rtnl_unlock();
1663}
1664
1665static void reg_check_channels(void)
1666{
1667 /*
1668 * Give usermode a chance to do something nicer (move to another
1669 * channel, orderly disconnection), before forcing a disconnection.
1670 */
1671 mod_delayed_work(system_power_efficient_wq,
1672 &reg_check_chans,
1673 msecs_to_jiffies(REG_ENFORCE_GRACE_MS));
1674}
1675
Sven Neumanneac03e32011-08-30 23:38:53 +02001676static void wiphy_update_regulatory(struct wiphy *wiphy,
1677 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001678{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001679 enum nl80211_band band;
Johannes Bergc492db32012-12-06 16:29:25 +01001680 struct regulatory_request *lr = get_last_request();
Sven Neumanneac03e32011-08-30 23:38:53 +02001681
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001682 if (ignore_reg_update(wiphy, initiator)) {
1683 /*
1684 * Regulatory updates set by CORE are ignored for custom
1685 * regulatory cards. Let us notify the changes to the driver,
1686 * as some drivers used this to restore its orig_* reg domain.
1687 */
1688 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001689 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001690 reg_call_notifier(wiphy, lr);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001691 return;
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001692 }
Sven Neumanna203c2a2011-07-12 15:52:07 +02001693
Johannes Bergc492db32012-12-06 16:29:25 +01001694 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07001695
Johannes Berg57fbcce2016-04-12 15:56:15 +02001696 for (band = 0; band < NUM_NL80211_BANDS; band++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001697 handle_band(wiphy, initiator, wiphy->bands[band]);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001698
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001699 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001700 reg_process_ht_flags(wiphy);
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001701 reg_call_notifier(wiphy, lr);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001702}
1703
Sven Neumannd7549cb2011-08-30 23:38:54 +02001704static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1705{
1706 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301707 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02001708
Johannes Berg5fe231e2013-05-08 21:45:15 +02001709 ASSERT_RTNL();
Johannes Berg458f4f92012-12-06 15:47:38 +01001710
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301711 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1712 wiphy = &rdev->wiphy;
1713 wiphy_update_regulatory(wiphy, initiator);
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301714 }
Arik Nemtsovad932f02014-11-27 09:44:55 +02001715
1716 reg_check_channels();
Sven Neumannd7549cb2011-08-30 23:38:54 +02001717}
1718
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001719static void handle_channel_custom(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001720 struct ieee80211_channel *chan,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001721 const struct ieee80211_regdomain *regd)
1722{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001723 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001724 const struct ieee80211_reg_rule *reg_rule = NULL;
1725 const struct ieee80211_power_rule *power_rule = NULL;
Matthias May4edd5692015-07-17 15:28:39 +02001726 u32 bw;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001727
Matthias May4edd5692015-07-17 15:28:39 +02001728 for (bw = MHZ_TO_KHZ(20); bw >= MHZ_TO_KHZ(5); bw = bw / 2) {
Michal Sojka49172872015-11-23 19:27:14 +01001729 reg_rule = freq_reg_info_regd(MHZ_TO_KHZ(chan->center_freq),
Matthias May4edd5692015-07-17 15:28:39 +02001730 regd, bw);
1731 if (!IS_ERR(reg_rule))
1732 break;
1733 }
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001734
Johannes Berg361c9c82012-12-06 15:57:14 +01001735 if (IS_ERR(reg_rule)) {
Johannes Bergc799ba62015-12-11 15:31:10 +01001736 pr_debug("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1737 chan->center_freq);
Arik Nemtsovdb8dfee2014-12-15 19:26:02 +02001738 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED) {
1739 chan->flags |= IEEE80211_CHAN_DISABLED;
1740 } else {
1741 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1742 chan->flags = chan->orig_flags;
1743 }
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001744 return;
1745 }
1746
1747 power_rule = &reg_rule->power_rule;
Michal Sojka1aeb1352015-11-23 19:27:16 +01001748 bw_flags = reg_rule_to_chan_bw_flags(regd, reg_rule, chan);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001749
Arik Nemtsov2e18b382014-11-16 16:37:46 +02001750 chan->dfs_state_entered = jiffies;
Arik Nemtsovc7ab5082014-11-16 16:37:47 +02001751 chan->dfs_state = NL80211_DFS_USABLE;
1752
1753 chan->beacon_found = false;
Arik Nemtsovdb8dfee2014-12-15 19:26:02 +02001754
1755 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1756 chan->flags = chan->orig_flags | bw_flags |
1757 map_regdom_flags(reg_rule->flags);
1758 else
1759 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1760
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001761 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001762 chan->max_reg_power = chan->max_power =
1763 (int) MBM_TO_DBM(power_rule->max_eirp);
Arik Nemtsov2e18b382014-11-16 16:37:46 +02001764
1765 if (chan->flags & IEEE80211_CHAN_RADAR) {
1766 if (reg_rule->dfs_cac_ms)
1767 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1768 else
1769 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1770 }
1771
1772 chan->max_power = chan->max_reg_power;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001773}
1774
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001775static void handle_band_custom(struct wiphy *wiphy,
1776 struct ieee80211_supported_band *sband,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001777 const struct ieee80211_regdomain *regd)
1778{
1779 unsigned int i;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001780
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001781 if (!sband)
1782 return;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001783
1784 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001785 handle_channel_custom(wiphy, &sband->channels[i], regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001786}
1787
1788/* Used by drivers prior to wiphy registration */
1789void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1790 const struct ieee80211_regdomain *regd)
1791{
Johannes Berg57fbcce2016-04-12 15:56:15 +02001792 enum nl80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001793 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001794
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001795 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
1796 "wiphy should have REGULATORY_CUSTOM_REG\n");
1797 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Luis R. Rodriguez222ea582013-11-05 09:18:00 -08001798
Johannes Berg57fbcce2016-04-12 15:56:15 +02001799 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001800 if (!wiphy->bands[band])
1801 continue;
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001802 handle_band_custom(wiphy, wiphy->bands[band], regd);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001803 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001804 }
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001805
1806 /*
1807 * no point in calling this if it won't have any effect
Johannes Berg1a919312012-12-03 17:21:11 +01001808 * on your device's supported bands.
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001809 */
1810 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001811}
1812EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1813
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001814static void reg_set_request_processed(void)
1815{
1816 bool need_more_processing = false;
Johannes Bergc492db32012-12-06 16:29:25 +01001817 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001818
Johannes Bergc492db32012-12-06 16:29:25 +01001819 lr->processed = true;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001820
1821 spin_lock(&reg_requests_lock);
1822 if (!list_empty(&reg_requests_list))
1823 need_more_processing = true;
1824 spin_unlock(&reg_requests_lock);
1825
Johannes Bergb6863032015-10-15 09:25:18 +02001826 cancel_crda_timeout();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001827
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001828 if (need_more_processing)
1829 schedule_work(&reg_work);
1830}
1831
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001832/**
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001833 * reg_process_hint_core - process core regulatory requests
1834 * @pending_request: a pending core regulatory request
1835 *
1836 * The wireless subsystem can use this function to process
1837 * a regulatory request issued by the regulatory core.
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001838 */
Johannes Bergd34265a2015-10-15 13:05:55 +02001839static enum reg_request_treatment
1840reg_process_hint_core(struct regulatory_request *core_request)
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001841{
Johannes Bergcecbb062015-10-15 08:47:34 +02001842 if (reg_query_database(core_request)) {
Johannes Berg25b20db2015-10-15 12:05:05 +02001843 core_request->intersect = false;
1844 core_request->processed = false;
1845 reg_update_last_request(core_request);
Johannes Bergd34265a2015-10-15 13:05:55 +02001846 return REG_REQ_OK;
Johannes Berg25b20db2015-10-15 12:05:05 +02001847 }
Johannes Bergd34265a2015-10-15 13:05:55 +02001848
1849 return REG_REQ_IGNORE;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001850}
1851
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001852static enum reg_request_treatment
1853__reg_process_hint_user(struct regulatory_request *user_request)
1854{
1855 struct regulatory_request *lr = get_last_request();
1856
1857 if (reg_request_cell_base(user_request))
1858 return reg_ignore_cell_hint(user_request);
1859
1860 if (reg_request_cell_base(lr))
1861 return REG_REQ_IGNORE;
1862
1863 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1864 return REG_REQ_INTERSECT;
1865 /*
1866 * If the user knows better the user should set the regdom
1867 * to their country before the IE is picked up
1868 */
1869 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
1870 lr->intersect)
1871 return REG_REQ_IGNORE;
1872 /*
1873 * Process user requests only after previous user/driver/core
1874 * requests have been processed
1875 */
1876 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
1877 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1878 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
1879 regdom_changes(lr->alpha2))
1880 return REG_REQ_IGNORE;
1881
1882 if (!regdom_changes(user_request->alpha2))
1883 return REG_REQ_ALREADY_SET;
1884
1885 return REG_REQ_OK;
1886}
1887
1888/**
1889 * reg_process_hint_user - process user regulatory requests
1890 * @user_request: a pending user regulatory request
1891 *
1892 * The wireless subsystem can use this function to process
1893 * a regulatory request initiated by userspace.
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001894 */
Johannes Bergd34265a2015-10-15 13:05:55 +02001895static enum reg_request_treatment
1896reg_process_hint_user(struct regulatory_request *user_request)
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001897{
1898 enum reg_request_treatment treatment;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001899
1900 treatment = __reg_process_hint_user(user_request);
1901 if (treatment == REG_REQ_IGNORE ||
Johannes Bergd34265a2015-10-15 13:05:55 +02001902 treatment == REG_REQ_ALREADY_SET)
1903 return REG_REQ_IGNORE;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001904
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001905 user_request->intersect = treatment == REG_REQ_INTERSECT;
1906 user_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001907
Johannes Bergcecbb062015-10-15 08:47:34 +02001908 if (reg_query_database(user_request)) {
Johannes Berg25b20db2015-10-15 12:05:05 +02001909 reg_update_last_request(user_request);
1910 user_alpha2[0] = user_request->alpha2[0];
1911 user_alpha2[1] = user_request->alpha2[1];
Johannes Bergd34265a2015-10-15 13:05:55 +02001912 return REG_REQ_OK;
Johannes Berg25b20db2015-10-15 12:05:05 +02001913 }
Johannes Bergd34265a2015-10-15 13:05:55 +02001914
1915 return REG_REQ_IGNORE;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001916}
1917
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001918static enum reg_request_treatment
1919__reg_process_hint_driver(struct regulatory_request *driver_request)
1920{
1921 struct regulatory_request *lr = get_last_request();
1922
1923 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
1924 if (regdom_changes(driver_request->alpha2))
1925 return REG_REQ_OK;
1926 return REG_REQ_ALREADY_SET;
1927 }
1928
1929 /*
1930 * This would happen if you unplug and plug your card
1931 * back in or if you add a new device for which the previously
1932 * loaded card also agrees on the regulatory domain.
1933 */
1934 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1935 !regdom_changes(driver_request->alpha2))
1936 return REG_REQ_ALREADY_SET;
1937
1938 return REG_REQ_INTERSECT;
1939}
1940
1941/**
1942 * reg_process_hint_driver - process driver regulatory requests
1943 * @driver_request: a pending driver regulatory request
1944 *
1945 * The wireless subsystem can use this function to process
1946 * a regulatory request issued by an 802.11 driver.
1947 *
1948 * Returns one of the different reg request treatment values.
1949 */
1950static enum reg_request_treatment
1951reg_process_hint_driver(struct wiphy *wiphy,
1952 struct regulatory_request *driver_request)
1953{
Arik Nemtsov34f05f52014-12-04 12:22:16 +02001954 const struct ieee80211_regdomain *regd, *tmp;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001955 enum reg_request_treatment treatment;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001956
1957 treatment = __reg_process_hint_driver(driver_request);
1958
1959 switch (treatment) {
1960 case REG_REQ_OK:
1961 break;
1962 case REG_REQ_IGNORE:
Johannes Bergd34265a2015-10-15 13:05:55 +02001963 return REG_REQ_IGNORE;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001964 case REG_REQ_INTERSECT:
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001965 case REG_REQ_ALREADY_SET:
1966 regd = reg_copy_regd(get_cfg80211_regdom());
Johannes Bergd34265a2015-10-15 13:05:55 +02001967 if (IS_ERR(regd))
1968 return REG_REQ_IGNORE;
Arik Nemtsov34f05f52014-12-04 12:22:16 +02001969
1970 tmp = get_wiphy_regdom(wiphy);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001971 rcu_assign_pointer(wiphy->regd, regd);
Arik Nemtsov34f05f52014-12-04 12:22:16 +02001972 rcu_free_regdom(tmp);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001973 }
1974
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001975
1976 driver_request->intersect = treatment == REG_REQ_INTERSECT;
1977 driver_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001978
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001979 /*
1980 * Since CRDA will not be called in this case as we already
1981 * have applied the requested regulatory domain before we just
1982 * inform userspace we have processed the request
1983 */
1984 if (treatment == REG_REQ_ALREADY_SET) {
1985 nl80211_send_reg_change_event(driver_request);
Johannes Berg25b20db2015-10-15 12:05:05 +02001986 reg_update_last_request(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001987 reg_set_request_processed();
Johannes Berg480908a2015-10-15 12:58:58 +02001988 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001989 }
1990
Johannes Bergd34265a2015-10-15 13:05:55 +02001991 if (reg_query_database(driver_request)) {
Johannes Berg25b20db2015-10-15 12:05:05 +02001992 reg_update_last_request(driver_request);
Johannes Bergd34265a2015-10-15 13:05:55 +02001993 return REG_REQ_OK;
1994 }
Johannes Berg25b20db2015-10-15 12:05:05 +02001995
Johannes Bergd34265a2015-10-15 13:05:55 +02001996 return REG_REQ_IGNORE;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001997}
1998
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001999static enum reg_request_treatment
2000__reg_process_hint_country_ie(struct wiphy *wiphy,
2001 struct regulatory_request *country_ie_request)
2002{
2003 struct wiphy *last_wiphy = NULL;
2004 struct regulatory_request *lr = get_last_request();
2005
2006 if (reg_request_cell_base(lr)) {
2007 /* Trust a Cell base station over the AP's country IE */
2008 if (regdom_changes(country_ie_request->alpha2))
2009 return REG_REQ_IGNORE;
2010 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez2a901462013-11-11 22:15:31 +01002011 } else {
2012 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
2013 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002014 }
2015
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002016 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
2017 return -EINVAL;
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002018
2019 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
2020 return REG_REQ_OK;
2021
2022 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
2023
2024 if (last_wiphy != wiphy) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002025 /*
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002026 * Two cards with two APs claiming different
2027 * Country IE alpha2s. We could
2028 * intersect them, but that seems unlikely
2029 * to be correct. Reject second one for now.
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002030 */
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002031 if (regdom_changes(country_ie_request->alpha2))
2032 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002033 return REG_REQ_ALREADY_SET;
2034 }
Emmanuel Grumbach70dcec52014-12-02 09:53:25 +02002035
2036 if (regdom_changes(country_ie_request->alpha2))
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08002037 return REG_REQ_OK;
2038 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002039}
2040
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002041/**
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002042 * reg_process_hint_country_ie - process regulatory requests from country IEs
2043 * @country_ie_request: a regulatory request from a country IE
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002044 *
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002045 * The wireless subsystem can use this function to process
2046 * a regulatory request issued by a country Information Element.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002047 *
Johannes Berg2f922122012-12-03 17:54:55 +01002048 * Returns one of the different reg request treatment values.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05002049 */
Johannes Berg2f922122012-12-03 17:54:55 +01002050static enum reg_request_treatment
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002051reg_process_hint_country_ie(struct wiphy *wiphy,
2052 struct regulatory_request *country_ie_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002053{
Johannes Berg2f922122012-12-03 17:54:55 +01002054 enum reg_request_treatment treatment;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002055
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002056 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002057
Johannes Berg2f922122012-12-03 17:54:55 +01002058 switch (treatment) {
Johannes Berg2f922122012-12-03 17:54:55 +01002059 case REG_REQ_OK:
2060 break;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002061 case REG_REQ_IGNORE:
Johannes Bergd34265a2015-10-15 13:05:55 +02002062 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002063 case REG_REQ_ALREADY_SET:
Arik Nemtsovc8883932014-04-21 20:39:34 -07002064 reg_free_request(country_ie_request);
Johannes Berg480908a2015-10-15 12:58:58 +02002065 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002066 case REG_REQ_INTERSECT:
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002067 /*
2068 * This doesn't happen yet, not sure we
2069 * ever want to support it for this case.
2070 */
2071 WARN_ONCE(1, "Unexpected intersection for country IEs");
Johannes Bergd34265a2015-10-15 13:05:55 +02002072 return REG_REQ_IGNORE;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002073 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002074
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002075 country_ie_request->intersect = false;
2076 country_ie_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08002077
Johannes Bergd34265a2015-10-15 13:05:55 +02002078 if (reg_query_database(country_ie_request)) {
Johannes Berg25b20db2015-10-15 12:05:05 +02002079 reg_update_last_request(country_ie_request);
Johannes Bergd34265a2015-10-15 13:05:55 +02002080 return REG_REQ_OK;
2081 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05002082
Johannes Bergd34265a2015-10-15 13:05:55 +02002083 return REG_REQ_IGNORE;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002084}
2085
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05302086bool reg_dfs_domain_same(struct wiphy *wiphy1, struct wiphy *wiphy2)
2087{
2088 const struct ieee80211_regdomain *wiphy1_regd = NULL;
2089 const struct ieee80211_regdomain *wiphy2_regd = NULL;
2090 const struct ieee80211_regdomain *cfg80211_regd = NULL;
2091 bool dfs_domain_same;
2092
2093 rcu_read_lock();
2094
2095 cfg80211_regd = rcu_dereference(cfg80211_regdomain);
2096 wiphy1_regd = rcu_dereference(wiphy1->regd);
2097 if (!wiphy1_regd)
2098 wiphy1_regd = cfg80211_regd;
2099
2100 wiphy2_regd = rcu_dereference(wiphy2->regd);
2101 if (!wiphy2_regd)
2102 wiphy2_regd = cfg80211_regd;
2103
2104 dfs_domain_same = wiphy1_regd->dfs_region == wiphy2_regd->dfs_region;
2105
2106 rcu_read_unlock();
2107
2108 return dfs_domain_same;
2109}
2110
2111static void reg_copy_dfs_chan_state(struct ieee80211_channel *dst_chan,
2112 struct ieee80211_channel *src_chan)
2113{
2114 if (!(dst_chan->flags & IEEE80211_CHAN_RADAR) ||
2115 !(src_chan->flags & IEEE80211_CHAN_RADAR))
2116 return;
2117
2118 if (dst_chan->flags & IEEE80211_CHAN_DISABLED ||
2119 src_chan->flags & IEEE80211_CHAN_DISABLED)
2120 return;
2121
2122 if (src_chan->center_freq == dst_chan->center_freq &&
2123 dst_chan->dfs_state == NL80211_DFS_USABLE) {
2124 dst_chan->dfs_state = src_chan->dfs_state;
2125 dst_chan->dfs_state_entered = src_chan->dfs_state_entered;
2126 }
2127}
2128
2129static void wiphy_share_dfs_chan_state(struct wiphy *dst_wiphy,
2130 struct wiphy *src_wiphy)
2131{
2132 struct ieee80211_supported_band *src_sband, *dst_sband;
2133 struct ieee80211_channel *src_chan, *dst_chan;
2134 int i, j, band;
2135
2136 if (!reg_dfs_domain_same(dst_wiphy, src_wiphy))
2137 return;
2138
2139 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2140 dst_sband = dst_wiphy->bands[band];
2141 src_sband = src_wiphy->bands[band];
2142 if (!dst_sband || !src_sband)
2143 continue;
2144
2145 for (i = 0; i < dst_sband->n_channels; i++) {
2146 dst_chan = &dst_sband->channels[i];
2147 for (j = 0; j < src_sband->n_channels; j++) {
2148 src_chan = &src_sband->channels[j];
2149 reg_copy_dfs_chan_state(dst_chan, src_chan);
2150 }
2151 }
2152 }
2153}
2154
2155static void wiphy_all_share_dfs_chan_state(struct wiphy *wiphy)
2156{
2157 struct cfg80211_registered_device *rdev;
2158
2159 ASSERT_RTNL();
2160
2161 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2162 if (wiphy == &rdev->wiphy)
2163 continue;
2164 wiphy_share_dfs_chan_state(wiphy, &rdev->wiphy);
2165 }
2166}
2167
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04002168/* This processes *all* regulatory hints */
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08002169static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002170{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002171 struct wiphy *wiphy = NULL;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002172 enum reg_request_treatment treatment;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002173
Johannes Bergf4173762012-12-03 18:23:37 +01002174 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002175 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
2176
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002177 switch (reg_request->initiator) {
2178 case NL80211_REGDOM_SET_BY_CORE:
Johannes Bergd34265a2015-10-15 13:05:55 +02002179 treatment = reg_process_hint_core(reg_request);
2180 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002181 case NL80211_REGDOM_SET_BY_USER:
Johannes Bergd34265a2015-10-15 13:05:55 +02002182 treatment = reg_process_hint_user(reg_request);
2183 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002184 case NL80211_REGDOM_SET_BY_DRIVER:
Ilan Peer772f0382014-01-14 15:17:23 +02002185 if (!wiphy)
2186 goto out_free;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08002187 treatment = reg_process_hint_driver(wiphy, reg_request);
2188 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002189 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Ilan Peer772f0382014-01-14 15:17:23 +02002190 if (!wiphy)
2191 goto out_free;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002192 treatment = reg_process_hint_country_ie(wiphy, reg_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002193 break;
2194 default:
2195 WARN(1, "invalid initiator %d\n", reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02002196 goto out_free;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08002197 }
2198
Johannes Bergd34265a2015-10-15 13:05:55 +02002199 if (treatment == REG_REQ_IGNORE)
2200 goto out_free;
2201
Johannes Berg480908a2015-10-15 12:58:58 +02002202 WARN(treatment != REG_REQ_OK && treatment != REG_REQ_ALREADY_SET,
2203 "unexpected treatment value %d\n", treatment);
2204
John Linville841b3512015-06-24 11:42:25 -04002205 /* This is required so that the orig_* parameters are saved.
2206 * NOTE: treatment must be set for any case that reaches here!
2207 */
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002208 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
Arik Nemtsovad932f02014-11-27 09:44:55 +02002209 wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08002210 wiphy_update_regulatory(wiphy, reg_request->initiator);
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05302211 wiphy_all_share_dfs_chan_state(wiphy);
Arik Nemtsovad932f02014-11-27 09:44:55 +02002212 reg_check_channels();
2213 }
Ilan Peer772f0382014-01-14 15:17:23 +02002214
2215 return;
2216
2217out_free:
Arik Nemtsovc8883932014-04-21 20:39:34 -07002218 reg_free_request(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002219}
2220
Arik Nemtsovef51fb12015-01-07 16:47:20 +02002221static bool reg_only_self_managed_wiphys(void)
2222{
2223 struct cfg80211_registered_device *rdev;
2224 struct wiphy *wiphy;
2225 bool self_managed_found = false;
2226
2227 ASSERT_RTNL();
2228
2229 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2230 wiphy = &rdev->wiphy;
2231 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2232 self_managed_found = true;
2233 else
2234 return false;
2235 }
2236
2237 /* make sure at least one self-managed wiphy exists */
2238 return self_managed_found;
2239}
2240
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002241/*
2242 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
2243 * Regulatory hints come on a first come first serve basis and we
2244 * must process each one atomically.
2245 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002246static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002247{
Johannes Bergc492db32012-12-06 16:29:25 +01002248 struct regulatory_request *reg_request, *lr;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002249
Johannes Bergc492db32012-12-06 16:29:25 +01002250 lr = get_last_request();
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002251
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002252 /* When last_request->processed becomes true this will be rescheduled */
Johannes Bergc492db32012-12-06 16:29:25 +01002253 if (lr && !lr->processed) {
Luis R. Rodriguez96cce122014-04-21 20:39:35 -07002254 reg_process_hint(lr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002255 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002256 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002257
2258 spin_lock(&reg_requests_lock);
2259
2260 if (list_empty(&reg_requests_list)) {
2261 spin_unlock(&reg_requests_lock);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002262 return;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002263 }
2264
2265 reg_request = list_first_entry(&reg_requests_list,
2266 struct regulatory_request,
2267 list);
2268 list_del_init(&reg_request->list);
2269
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002270 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08002271
Arik Nemtsovef51fb12015-01-07 16:47:20 +02002272 if (reg_only_self_managed_wiphys()) {
2273 reg_free_request(reg_request);
2274 return;
2275 }
2276
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08002277 reg_process_hint(reg_request);
Ben2e54a682015-03-12 09:37:34 -04002278
2279 lr = get_last_request();
2280
2281 spin_lock(&reg_requests_lock);
2282 if (!list_empty(&reg_requests_list) && lr && lr->processed)
2283 schedule_work(&reg_work);
2284 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002285}
2286
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002287/* Processes beacon hints -- this has nothing to do with country IEs */
2288static void reg_process_pending_beacon_hints(void)
2289{
Johannes Berg79c97e92009-07-07 03:56:12 +02002290 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002291 struct reg_beacon *pending_beacon, *tmp;
2292
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002293 /* This goes through the _pending_ beacon list */
2294 spin_lock_bh(&reg_pending_beacons_lock);
2295
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002296 list_for_each_entry_safe(pending_beacon, tmp,
2297 &reg_pending_beacons, list) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002298 list_del_init(&pending_beacon->list);
2299
2300 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02002301 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
2302 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002303
2304 /* Remembers the beacon hint for new wiphys or reg changes */
2305 list_add_tail(&pending_beacon->list, &reg_beacon_list);
2306 }
2307
2308 spin_unlock_bh(&reg_pending_beacons_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002309}
2310
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002311static void reg_process_self_managed_hints(void)
2312{
2313 struct cfg80211_registered_device *rdev;
2314 struct wiphy *wiphy;
2315 const struct ieee80211_regdomain *tmp;
2316 const struct ieee80211_regdomain *regd;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002317 enum nl80211_band band;
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002318 struct regulatory_request request = {};
2319
2320 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2321 wiphy = &rdev->wiphy;
2322
2323 spin_lock(&reg_requests_lock);
2324 regd = rdev->requested_regd;
2325 rdev->requested_regd = NULL;
2326 spin_unlock(&reg_requests_lock);
2327
2328 if (regd == NULL)
2329 continue;
2330
2331 tmp = get_wiphy_regdom(wiphy);
2332 rcu_assign_pointer(wiphy->regd, regd);
2333 rcu_free_regdom(tmp);
2334
Johannes Berg57fbcce2016-04-12 15:56:15 +02002335 for (band = 0; band < NUM_NL80211_BANDS; band++)
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002336 handle_band_custom(wiphy, wiphy->bands[band], regd);
2337
2338 reg_process_ht_flags(wiphy);
2339
2340 request.wiphy_idx = get_wiphy_idx(wiphy);
2341 request.alpha2[0] = regd->alpha2[0];
2342 request.alpha2[1] = regd->alpha2[1];
2343 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
2344
2345 nl80211_send_wiphy_reg_change_event(&request);
2346 }
2347
2348 reg_check_channels();
2349}
2350
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002351static void reg_todo(struct work_struct *work)
2352{
Johannes Berg5fe231e2013-05-08 21:45:15 +02002353 rtnl_lock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002354 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002355 reg_process_pending_beacon_hints();
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002356 reg_process_self_managed_hints();
Johannes Berg5fe231e2013-05-08 21:45:15 +02002357 rtnl_unlock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002358}
2359
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002360static void queue_regulatory_request(struct regulatory_request *request)
2361{
Johannes Bergd4f2c882012-12-03 18:59:58 +01002362 request->alpha2[0] = toupper(request->alpha2[0]);
2363 request->alpha2[1] = toupper(request->alpha2[1]);
John W. Linvillec61029c2010-08-05 14:26:24 -04002364
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002365 spin_lock(&reg_requests_lock);
2366 list_add_tail(&request->list, &reg_requests_list);
2367 spin_unlock(&reg_requests_lock);
2368
2369 schedule_work(&reg_work);
2370}
2371
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002372/*
2373 * Core regulatory hint -- happens during cfg80211_init()
2374 * and when we restore regulatory settings.
2375 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002376static int regulatory_hint_core(const char *alpha2)
2377{
2378 struct regulatory_request *request;
2379
Johannes Berg1a919312012-12-03 17:21:11 +01002380 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002381 if (!request)
2382 return -ENOMEM;
2383
2384 request->alpha2[0] = alpha2[0];
2385 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002386 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002387
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08002388 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04002389
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002390 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002391}
2392
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002393/* User hints */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002394int regulatory_hint_user(const char *alpha2,
2395 enum nl80211_user_reg_hint_type user_reg_hint_type)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002396{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002397 struct regulatory_request *request;
2398
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002399 if (WARN_ON(!alpha2))
2400 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002401
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002402 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2403 if (!request)
2404 return -ENOMEM;
2405
Johannes Bergf4173762012-12-03 18:23:37 +01002406 request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002407 request->alpha2[0] = alpha2[0];
2408 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05002409 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002410 request->user_reg_hint_type = user_reg_hint_type;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002411
Ilan peerc37722b2015-03-30 15:15:49 +03002412 /* Allow calling CRDA again */
Johannes Bergb6863032015-10-15 09:25:18 +02002413 reset_crda_timeouts();
Ilan peerc37722b2015-03-30 15:15:49 +03002414
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002415 queue_regulatory_request(request);
2416
2417 return 0;
2418}
2419
Ilan peer05050752015-03-04 00:32:06 -05002420int regulatory_hint_indoor(bool is_indoor, u32 portid)
Ilan Peer52616f22014-02-25 16:26:00 +02002421{
Ilan peer05050752015-03-04 00:32:06 -05002422 spin_lock(&reg_indoor_lock);
Ilan Peer52616f22014-02-25 16:26:00 +02002423
Ilan peer05050752015-03-04 00:32:06 -05002424 /* It is possible that more than one user space process is trying to
2425 * configure the indoor setting. To handle such cases, clear the indoor
2426 * setting in case that some process does not think that the device
2427 * is operating in an indoor environment. In addition, if a user space
2428 * process indicates that it is controlling the indoor setting, save its
2429 * portid, i.e., make it the owner.
2430 */
2431 reg_is_indoor = is_indoor;
2432 if (reg_is_indoor) {
2433 if (!reg_is_indoor_portid)
2434 reg_is_indoor_portid = portid;
2435 } else {
2436 reg_is_indoor_portid = 0;
2437 }
Ilan Peer52616f22014-02-25 16:26:00 +02002438
Ilan peer05050752015-03-04 00:32:06 -05002439 spin_unlock(&reg_indoor_lock);
2440
2441 if (!is_indoor)
2442 reg_check_channels();
Ilan Peer52616f22014-02-25 16:26:00 +02002443
2444 return 0;
2445}
2446
Ilan peer05050752015-03-04 00:32:06 -05002447void regulatory_netlink_notify(u32 portid)
2448{
2449 spin_lock(&reg_indoor_lock);
2450
2451 if (reg_is_indoor_portid != portid) {
2452 spin_unlock(&reg_indoor_lock);
2453 return;
2454 }
2455
2456 reg_is_indoor = false;
2457 reg_is_indoor_portid = 0;
2458
2459 spin_unlock(&reg_indoor_lock);
2460
2461 reg_check_channels();
2462}
2463
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002464/* Driver hints */
2465int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2466{
2467 struct regulatory_request *request;
2468
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002469 if (WARN_ON(!alpha2 || !wiphy))
2470 return -EINVAL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002471
Luis R. Rodriguez4f7b9142013-12-14 20:09:06 +01002472 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
2473
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002474 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2475 if (!request)
2476 return -ENOMEM;
2477
2478 request->wiphy_idx = get_wiphy_idx(wiphy);
2479
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002480 request->alpha2[0] = alpha2[0];
2481 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002482 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002483
Ilan peerc37722b2015-03-30 15:15:49 +03002484 /* Allow calling CRDA again */
Johannes Bergb6863032015-10-15 09:25:18 +02002485 reset_crda_timeouts();
Ilan peerc37722b2015-03-30 15:15:49 +03002486
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002487 queue_regulatory_request(request);
2488
2489 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002490}
2491EXPORT_SYMBOL(regulatory_hint);
2492
Johannes Berg57fbcce2016-04-12 15:56:15 +02002493void regulatory_hint_country_ie(struct wiphy *wiphy, enum nl80211_band band,
Luis R. Rodriguez789fd032013-10-04 18:07:24 -07002494 const u8 *country_ie, u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002495{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002496 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002497 enum environment_cap env = ENVIRON_ANY;
Johannes Bergdb2424c2013-05-10 19:07:52 +02002498 struct regulatory_request *request = NULL, *lr;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002499
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002500 /* IE len must be evenly divisible by 2 */
2501 if (country_ie_len & 0x01)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002502 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002503
2504 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002505 return;
2506
2507 request = kzalloc(sizeof(*request), GFP_KERNEL);
2508 if (!request)
2509 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002510
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002511 alpha2[0] = country_ie[0];
2512 alpha2[1] = country_ie[1];
2513
2514 if (country_ie[2] == 'I')
2515 env = ENVIRON_INDOOR;
2516 else if (country_ie[2] == 'O')
2517 env = ENVIRON_OUTDOOR;
2518
Johannes Bergdb2424c2013-05-10 19:07:52 +02002519 rcu_read_lock();
2520 lr = get_last_request();
2521
2522 if (unlikely(!lr))
2523 goto out;
2524
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002525 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002526 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002527 * We leave conflict resolution to the workqueue, where can hold
Johannes Berg5fe231e2013-05-08 21:45:15 +02002528 * the RTNL.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002529 */
Johannes Bergc492db32012-12-06 16:29:25 +01002530 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2531 lr->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002532 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002533
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002534 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04002535 request->alpha2[0] = alpha2[0];
2536 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002537 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002538 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002539
Ilan peerc37722b2015-03-30 15:15:49 +03002540 /* Allow calling CRDA again */
Johannes Bergb6863032015-10-15 09:25:18 +02002541 reset_crda_timeouts();
Ilan peerc37722b2015-03-30 15:15:49 +03002542
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002543 queue_regulatory_request(request);
Johannes Bergdb2424c2013-05-10 19:07:52 +02002544 request = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002545out:
Johannes Bergdb2424c2013-05-10 19:07:52 +02002546 kfree(request);
2547 rcu_read_unlock();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002548}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002549
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002550static void restore_alpha2(char *alpha2, bool reset_user)
2551{
2552 /* indicates there is no alpha2 to consider for restoration */
2553 alpha2[0] = '9';
2554 alpha2[1] = '7';
2555
2556 /* The user setting has precedence over the module parameter */
2557 if (is_user_regdom_saved()) {
2558 /* Unless we're asked to ignore it and reset it */
2559 if (reset_user) {
Johannes Bergc799ba62015-12-11 15:31:10 +01002560 pr_debug("Restoring regulatory settings including user preference\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002561 user_alpha2[0] = '9';
2562 user_alpha2[1] = '7';
2563
2564 /*
2565 * If we're ignoring user settings, we still need to
2566 * check the module parameter to ensure we put things
2567 * back as they were for a full restore.
2568 */
2569 if (!is_world_regdom(ieee80211_regdom)) {
Johannes Bergc799ba62015-12-11 15:31:10 +01002570 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2571 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002572 alpha2[0] = ieee80211_regdom[0];
2573 alpha2[1] = ieee80211_regdom[1];
2574 }
2575 } else {
Johannes Bergc799ba62015-12-11 15:31:10 +01002576 pr_debug("Restoring regulatory settings while preserving user preference for: %c%c\n",
2577 user_alpha2[0], user_alpha2[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002578 alpha2[0] = user_alpha2[0];
2579 alpha2[1] = user_alpha2[1];
2580 }
2581 } else if (!is_world_regdom(ieee80211_regdom)) {
Johannes Bergc799ba62015-12-11 15:31:10 +01002582 pr_debug("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2583 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002584 alpha2[0] = ieee80211_regdom[0];
2585 alpha2[1] = ieee80211_regdom[1];
2586 } else
Johannes Bergc799ba62015-12-11 15:31:10 +01002587 pr_debug("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002588}
2589
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302590static void restore_custom_reg_settings(struct wiphy *wiphy)
2591{
2592 struct ieee80211_supported_band *sband;
Johannes Berg57fbcce2016-04-12 15:56:15 +02002593 enum nl80211_band band;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302594 struct ieee80211_channel *chan;
2595 int i;
2596
Johannes Berg57fbcce2016-04-12 15:56:15 +02002597 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302598 sband = wiphy->bands[band];
2599 if (!sband)
2600 continue;
2601 for (i = 0; i < sband->n_channels; i++) {
2602 chan = &sband->channels[i];
2603 chan->flags = chan->orig_flags;
2604 chan->max_antenna_gain = chan->orig_mag;
2605 chan->max_power = chan->orig_mpwr;
Paul Stewart899852a2012-08-01 16:54:42 -07002606 chan->beacon_found = false;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302607 }
2608 }
2609}
2610
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002611/*
2612 * Restoring regulatory settings involves ingoring any
2613 * possibly stale country IE information and user regulatory
2614 * settings if so desired, this includes any beacon hints
2615 * learned as we could have traveled outside to another country
2616 * after disconnection. To restore regulatory settings we do
2617 * exactly what we did at bootup:
2618 *
2619 * - send a core regulatory hint
2620 * - send a user regulatory hint if applicable
2621 *
2622 * Device drivers that send a regulatory hint for a specific country
2623 * keep their own regulatory domain on wiphy->regd so that does does
2624 * not need to be remembered.
2625 */
2626static void restore_regulatory_settings(bool reset_user)
2627{
2628 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002629 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002630 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002631 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302632 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002633
Johannes Berg5fe231e2013-05-08 21:45:15 +02002634 ASSERT_RTNL();
2635
Ilan peer05050752015-03-04 00:32:06 -05002636 /*
2637 * Clear the indoor setting in case that it is not controlled by user
2638 * space, as otherwise there is no guarantee that the device is still
2639 * operating in an indoor environment.
2640 */
2641 spin_lock(&reg_indoor_lock);
2642 if (reg_is_indoor && !reg_is_indoor_portid) {
2643 reg_is_indoor = false;
2644 reg_check_channels();
2645 }
2646 spin_unlock(&reg_indoor_lock);
Ilan Peer52616f22014-02-25 16:26:00 +02002647
Johannes Berg2d319862013-01-09 12:01:38 +01002648 reset_regdomains(true, &world_regdom);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002649 restore_alpha2(alpha2, reset_user);
2650
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002651 /*
2652 * If there's any pending requests we simply
2653 * stash them to a temporary pending queue and
2654 * add then after we've restored regulatory
2655 * settings.
2656 */
2657 spin_lock(&reg_requests_lock);
Ilan peereeca9fc2015-03-04 00:32:07 -05002658 list_splice_tail_init(&reg_requests_list, &tmp_reg_req_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002659 spin_unlock(&reg_requests_lock);
2660
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002661 /* Clear beacon hints */
2662 spin_lock_bh(&reg_pending_beacons_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002663 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2664 list_del(&reg_beacon->list);
2665 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002666 }
2667 spin_unlock_bh(&reg_pending_beacons_lock);
2668
Johannes Bergfea9bce2012-12-03 17:32:01 +01002669 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2670 list_del(&reg_beacon->list);
2671 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002672 }
2673
2674 /* First restore to the basic regulatory settings */
Johannes Berg379b82f2012-12-06 15:44:07 +01002675 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
2676 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002677
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302678 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02002679 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
2680 continue;
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01002681 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302682 restore_custom_reg_settings(&rdev->wiphy);
2683 }
2684
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002685 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002686
2687 /*
2688 * This restores the ieee80211_regdom module parameter
2689 * preference or the last user requested regulatory
2690 * settings, user regulatory settings takes precedence.
2691 */
2692 if (is_an_alpha2(alpha2))
Maciej S. Szmigiero549cc1c2015-09-02 19:00:31 +02002693 regulatory_hint_user(alpha2, NL80211_USER_REG_HINT_USER);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002694
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002695 spin_lock(&reg_requests_lock);
Johannes Berg11cff962012-12-03 18:56:41 +01002696 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002697 spin_unlock(&reg_requests_lock);
2698
Johannes Bergc799ba62015-12-11 15:31:10 +01002699 pr_debug("Kicking the queue\n");
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002700
2701 schedule_work(&reg_work);
2702}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002703
2704void regulatory_hint_disconnect(void)
2705{
Johannes Bergc799ba62015-12-11 15:31:10 +01002706 pr_debug("All devices are disconnected, going to restore regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002707 restore_regulatory_settings(false);
2708}
2709
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002710static bool freq_is_chan_12_13_14(u16 freq)
2711{
Johannes Berg57fbcce2016-04-12 15:56:15 +02002712 if (freq == ieee80211_channel_to_frequency(12, NL80211_BAND_2GHZ) ||
2713 freq == ieee80211_channel_to_frequency(13, NL80211_BAND_2GHZ) ||
2714 freq == ieee80211_channel_to_frequency(14, NL80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002715 return true;
2716 return false;
2717}
2718
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002719static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
2720{
2721 struct reg_beacon *pending_beacon;
2722
2723 list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
2724 if (beacon_chan->center_freq ==
2725 pending_beacon->chan.center_freq)
2726 return true;
2727 return false;
2728}
2729
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002730int regulatory_hint_found_beacon(struct wiphy *wiphy,
2731 struct ieee80211_channel *beacon_chan,
2732 gfp_t gfp)
2733{
2734 struct reg_beacon *reg_beacon;
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002735 bool processing;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002736
Johannes Berg1a919312012-12-03 17:21:11 +01002737 if (beacon_chan->beacon_found ||
2738 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
Johannes Berg57fbcce2016-04-12 15:56:15 +02002739 (beacon_chan->band == NL80211_BAND_2GHZ &&
Johannes Berg1a919312012-12-03 17:21:11 +01002740 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002741 return 0;
2742
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002743 spin_lock_bh(&reg_pending_beacons_lock);
2744 processing = pending_reg_beacon(beacon_chan);
2745 spin_unlock_bh(&reg_pending_beacons_lock);
2746
2747 if (processing)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002748 return 0;
2749
2750 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2751 if (!reg_beacon)
2752 return -ENOMEM;
2753
Johannes Bergc799ba62015-12-11 15:31:10 +01002754 pr_debug("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
2755 beacon_chan->center_freq,
2756 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2757 wiphy_name(wiphy));
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002758
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002759 memcpy(&reg_beacon->chan, beacon_chan,
Johannes Berg1a919312012-12-03 17:21:11 +01002760 sizeof(struct ieee80211_channel));
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002761
2762 /*
2763 * Since we can be called from BH or and non-BH context
2764 * we must use spin_lock_bh()
2765 */
2766 spin_lock_bh(&reg_pending_beacons_lock);
2767 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2768 spin_unlock_bh(&reg_pending_beacons_lock);
2769
2770 schedule_work(&reg_work);
2771
2772 return 0;
2773}
2774
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002775static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002776{
2777 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002778 const struct ieee80211_reg_rule *reg_rule = NULL;
2779 const struct ieee80211_freq_range *freq_range = NULL;
2780 const struct ieee80211_power_rule *power_rule = NULL;
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002781 char bw[32], cac_time[32];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002782
Dave Young94c4fd62015-11-15 15:31:05 +08002783 pr_debug(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002784
2785 for (i = 0; i < rd->n_reg_rules; i++) {
2786 reg_rule = &rd->reg_rules[i];
2787 freq_range = &reg_rule->freq_range;
2788 power_rule = &reg_rule->power_rule;
2789
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002790 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
2791 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
2792 freq_range->max_bandwidth_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002793 reg_get_max_bandwidth(rd, reg_rule));
2794 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002795 snprintf(bw, sizeof(bw), "%d KHz",
Janusz Dziedzic97524822014-01-30 09:52:20 +01002796 freq_range->max_bandwidth_khz);
2797
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002798 if (reg_rule->flags & NL80211_RRF_DFS)
2799 scnprintf(cac_time, sizeof(cac_time), "%u s",
2800 reg_rule->dfs_cac_ms/1000);
2801 else
2802 scnprintf(cac_time, sizeof(cac_time), "N/A");
2803
2804
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002805 /*
2806 * There may not be documentation for max antenna gain
2807 * in certain regions
2808 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002809 if (power_rule->max_antenna_gain)
Dave Young94c4fd62015-11-15 15:31:05 +08002810 pr_debug(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002811 freq_range->start_freq_khz,
2812 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002813 bw,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002814 power_rule->max_antenna_gain,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002815 power_rule->max_eirp,
2816 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002817 else
Dave Young94c4fd62015-11-15 15:31:05 +08002818 pr_debug(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002819 freq_range->start_freq_khz,
2820 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002821 bw,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002822 power_rule->max_eirp,
2823 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002824 }
2825}
2826
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01002827bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002828{
2829 switch (dfs_region) {
2830 case NL80211_DFS_UNSET:
2831 case NL80211_DFS_FCC:
2832 case NL80211_DFS_ETSI:
2833 case NL80211_DFS_JP:
2834 return true;
2835 default:
Johannes Bergc799ba62015-12-11 15:31:10 +01002836 pr_debug("Ignoring uknown DFS master region: %d\n", dfs_region);
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002837 return false;
2838 }
2839}
2840
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002841static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002842{
Johannes Bergc492db32012-12-06 16:29:25 +01002843 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002844
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002845 if (is_intersected_alpha2(rd->alpha2)) {
Johannes Bergc492db32012-12-06 16:29:25 +01002846 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002847 struct cfg80211_registered_device *rdev;
Johannes Bergc492db32012-12-06 16:29:25 +01002848 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002849 if (rdev) {
Dave Young94c4fd62015-11-15 15:31:05 +08002850 pr_debug("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002851 rdev->country_ie_alpha2[0],
2852 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002853 } else
Dave Young94c4fd62015-11-15 15:31:05 +08002854 pr_debug("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002855 } else
Dave Young94c4fd62015-11-15 15:31:05 +08002856 pr_debug("Current regulatory domain intersected:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002857 } else if (is_world_regdom(rd->alpha2)) {
Dave Young94c4fd62015-11-15 15:31:05 +08002858 pr_debug("World regulatory domain updated:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002859 } else {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002860 if (is_unknown_alpha2(rd->alpha2))
Dave Young94c4fd62015-11-15 15:31:05 +08002861 pr_debug("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002862 else {
Johannes Bergc492db32012-12-06 16:29:25 +01002863 if (reg_request_cell_base(lr))
Dave Young94c4fd62015-11-15 15:31:05 +08002864 pr_debug("Regulatory domain changed to country: %c%c by Cell Station\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002865 rd->alpha2[0], rd->alpha2[1]);
2866 else
Dave Young94c4fd62015-11-15 15:31:05 +08002867 pr_debug("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002868 rd->alpha2[0], rd->alpha2[1]);
2869 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002870 }
Johannes Berg1a919312012-12-03 17:21:11 +01002871
Dave Young94c4fd62015-11-15 15:31:05 +08002872 pr_debug(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002873 print_rd_rules(rd);
2874}
2875
Johannes Berg2df78162008-10-28 16:49:41 +01002876static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002877{
Dave Young94c4fd62015-11-15 15:31:05 +08002878 pr_debug("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002879 print_rd_rules(rd);
2880}
2881
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002882static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
2883{
2884 if (!is_world_regdom(rd->alpha2))
2885 return -EINVAL;
2886 update_world_regdomain(rd);
2887 return 0;
2888}
2889
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002890static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
2891 struct regulatory_request *user_request)
2892{
2893 const struct ieee80211_regdomain *intersected_rd = NULL;
2894
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002895 if (!regdom_changes(rd->alpha2))
2896 return -EALREADY;
2897
2898 if (!is_valid_rd(rd)) {
Dave Young94c4fd62015-11-15 15:31:05 +08002899 pr_err("Invalid regulatory domain detected: %c%c\n",
2900 rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002901 print_regdomain_info(rd);
2902 return -EINVAL;
2903 }
2904
2905 if (!user_request->intersect) {
2906 reset_regdomains(false, rd);
2907 return 0;
2908 }
2909
2910 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2911 if (!intersected_rd)
2912 return -EINVAL;
2913
2914 kfree(rd);
2915 rd = NULL;
2916 reset_regdomains(false, intersected_rd);
2917
2918 return 0;
2919}
2920
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002921static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
2922 struct regulatory_request *driver_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002923{
Johannes Berge9763c32012-12-03 16:59:46 +01002924 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002925 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002926 const struct ieee80211_regdomain *tmp;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002927 struct wiphy *request_wiphy;
Johannes Berg6913b492012-12-04 00:48:59 +01002928
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002929 if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002930 return -EINVAL;
2931
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002932 if (!regdom_changes(rd->alpha2))
2933 return -EALREADY;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002934
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002935 if (!is_valid_rd(rd)) {
Dave Young94c4fd62015-11-15 15:31:05 +08002936 pr_err("Invalid regulatory domain detected: %c%c\n",
2937 rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002938 print_regdomain_info(rd);
2939 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002940 }
2941
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002942 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
Johannes Berg922ec582015-10-15 09:12:49 +02002943 if (!request_wiphy)
Johannes Bergde3584b2011-11-21 10:44:00 +01002944 return -ENODEV;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002945
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002946 if (!driver_request->intersect) {
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002947 if (request_wiphy->regd)
2948 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002949
Johannes Berge9763c32012-12-03 16:59:46 +01002950 regd = reg_copy_regd(rd);
2951 if (IS_ERR(regd))
2952 return PTR_ERR(regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002953
Johannes Berg458f4f92012-12-06 15:47:38 +01002954 rcu_assign_pointer(request_wiphy->regd, regd);
Johannes Berg379b82f2012-12-06 15:44:07 +01002955 reset_regdomains(false, rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002956 return 0;
2957 }
2958
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002959 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2960 if (!intersected_rd)
2961 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002962
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002963 /*
2964 * We can trash what CRDA provided now.
2965 * However if a driver requested this specific regulatory
2966 * domain we keep it for its private use
2967 */
2968 tmp = get_wiphy_regdom(request_wiphy);
2969 rcu_assign_pointer(request_wiphy->regd, rd);
2970 rcu_free_regdom(tmp);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002971
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002972 rd = NULL;
Larry Fingerb7566fc2013-02-04 15:33:44 -06002973
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002974 reset_regdomains(false, intersected_rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002975
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002976 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002977}
2978
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002979static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
2980 struct regulatory_request *country_ie_request)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002981{
2982 struct wiphy *request_wiphy;
2983
2984 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2985 !is_unknown_alpha2(rd->alpha2))
2986 return -EINVAL;
2987
2988 /*
2989 * Lets only bother proceeding on the same alpha2 if the current
2990 * rd is non static (it means CRDA was present and was used last)
2991 * and the pending request came in from a country IE
2992 */
2993
2994 if (!is_valid_rd(rd)) {
Dave Young94c4fd62015-11-15 15:31:05 +08002995 pr_err("Invalid regulatory domain detected: %c%c\n",
2996 rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002997 print_regdomain_info(rd);
2998 return -EINVAL;
2999 }
3000
Luis R. Rodriguez01992402013-11-05 09:18:17 -08003001 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
Johannes Berg922ec582015-10-15 09:12:49 +02003002 if (!request_wiphy)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003003 return -ENODEV;
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003004
Luis R. Rodriguez01992402013-11-05 09:18:17 -08003005 if (country_ie_request->intersect)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003006 return -EINVAL;
3007
3008 reset_regdomains(false, rd);
3009 return 0;
3010}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003011
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05003012/*
3013 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003014 * multiple drivers can be ironed out later. Caller must've already
Johannes Berg458f4f92012-12-06 15:47:38 +01003015 * kmalloc'd the rd structure.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05003016 */
Ilan peerc37722b2015-03-30 15:15:49 +03003017int set_regdom(const struct ieee80211_regdomain *rd,
3018 enum ieee80211_regd_source regd_src)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003019{
Johannes Bergc492db32012-12-06 16:29:25 +01003020 struct regulatory_request *lr;
Janusz Dziedzic092008a2014-02-14 08:54:00 +01003021 bool user_reset = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003022 int r;
3023
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003024 if (!reg_is_valid_request(rd->alpha2)) {
3025 kfree(rd);
3026 return -EINVAL;
3027 }
3028
Ilan peerc37722b2015-03-30 15:15:49 +03003029 if (regd_src == REGD_SOURCE_CRDA)
Johannes Bergb6863032015-10-15 09:25:18 +02003030 reset_crda_timeouts();
Ilan peerc37722b2015-03-30 15:15:49 +03003031
Johannes Bergc492db32012-12-06 16:29:25 +01003032 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07003033
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003034 /* Note that this doesn't update the wiphys, this is done below */
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003035 switch (lr->initiator) {
3036 case NL80211_REGDOM_SET_BY_CORE:
3037 r = reg_set_rd_core(rd);
3038 break;
3039 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08003040 r = reg_set_rd_user(rd, lr);
Janusz Dziedzic092008a2014-02-14 08:54:00 +01003041 user_reset = true;
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08003042 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003043 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08003044 r = reg_set_rd_driver(rd, lr);
3045 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003046 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez01992402013-11-05 09:18:17 -08003047 r = reg_set_rd_country_ie(rd, lr);
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003048 break;
3049 default:
3050 WARN(1, "invalid initiator %d\n", lr->initiator);
Ola Olsson09d11802015-12-13 19:12:03 +01003051 kfree(rd);
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08003052 return -EINVAL;
3053 }
3054
Johannes Bergd2372b32008-10-24 20:32:20 +02003055 if (r) {
Janusz Dziedzic092008a2014-02-14 08:54:00 +01003056 switch (r) {
3057 case -EALREADY:
Kalle Valo95908532012-07-12 15:33:58 +03003058 reg_set_request_processed();
Janusz Dziedzic092008a2014-02-14 08:54:00 +01003059 break;
3060 default:
3061 /* Back to world regulatory in case of errors */
3062 restore_regulatory_settings(user_reset);
3063 }
Kalle Valo95908532012-07-12 15:33:58 +03003064
Johannes Bergd2372b32008-10-24 20:32:20 +02003065 kfree(rd);
Johannes Berg38fd2142013-05-10 19:17:17 +02003066 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02003067 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003068
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003069 /* This would make this whole thing pointless */
Johannes Berg38fd2142013-05-10 19:17:17 +02003070 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
3071 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003072
3073 /* update all wiphys now with the new established regulatory domain */
Johannes Bergc492db32012-12-06 16:29:25 +01003074 update_all_wiphy_regulatory(lr->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003075
Johannes Berg458f4f92012-12-06 15:47:38 +01003076 print_regdomain(get_cfg80211_regdom());
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003077
Johannes Bergc492db32012-12-06 16:29:25 +01003078 nl80211_send_reg_change_event(lr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003079
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08003080 reg_set_request_processed();
3081
Johannes Berg38fd2142013-05-10 19:17:17 +02003082 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003083}
3084
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02003085static int __regulatory_set_wiphy_regd(struct wiphy *wiphy,
3086 struct ieee80211_regdomain *rd)
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02003087{
3088 const struct ieee80211_regdomain *regd;
3089 const struct ieee80211_regdomain *prev_regd;
3090 struct cfg80211_registered_device *rdev;
3091
3092 if (WARN_ON(!wiphy || !rd))
3093 return -EINVAL;
3094
3095 if (WARN(!(wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED),
3096 "wiphy should have REGULATORY_WIPHY_SELF_MANAGED\n"))
3097 return -EPERM;
3098
3099 if (WARN(!is_valid_rd(rd), "Invalid regulatory domain detected\n")) {
3100 print_regdomain_info(rd);
3101 return -EINVAL;
3102 }
3103
3104 regd = reg_copy_regd(rd);
3105 if (IS_ERR(regd))
3106 return PTR_ERR(regd);
3107
3108 rdev = wiphy_to_rdev(wiphy);
3109
3110 spin_lock(&reg_requests_lock);
3111 prev_regd = rdev->requested_regd;
3112 rdev->requested_regd = regd;
3113 spin_unlock(&reg_requests_lock);
3114
3115 kfree(prev_regd);
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02003116 return 0;
3117}
3118
3119int regulatory_set_wiphy_regd(struct wiphy *wiphy,
3120 struct ieee80211_regdomain *rd)
3121{
3122 int ret = __regulatory_set_wiphy_regd(wiphy, rd);
3123
3124 if (ret)
3125 return ret;
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02003126
3127 schedule_work(&reg_work);
3128 return 0;
3129}
3130EXPORT_SYMBOL(regulatory_set_wiphy_regd);
3131
Arik Nemtsov2c3e8612015-01-07 16:47:19 +02003132int regulatory_set_wiphy_regd_sync_rtnl(struct wiphy *wiphy,
3133 struct ieee80211_regdomain *rd)
3134{
3135 int ret;
3136
3137 ASSERT_RTNL();
3138
3139 ret = __regulatory_set_wiphy_regd(wiphy, rd);
3140 if (ret)
3141 return ret;
3142
3143 /* process the request immediately */
3144 reg_process_self_managed_hints();
3145 return 0;
3146}
3147EXPORT_SYMBOL(regulatory_set_wiphy_regd_sync_rtnl);
3148
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003149void wiphy_regulatory_register(struct wiphy *wiphy)
3150{
Arik Nemtsov23df0b72013-07-21 16:36:48 +03003151 struct regulatory_request *lr;
3152
Jonathan Doronb0d7aa52014-12-15 19:26:00 +02003153 /* self-managed devices ignore external hints */
3154 if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
3155 wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS |
3156 REGULATORY_COUNTRY_IE_IGNORE;
3157
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003158 if (!reg_dev_ignore_cell_hint(wiphy))
3159 reg_num_devs_support_basehint++;
3160
Arik Nemtsov23df0b72013-07-21 16:36:48 +03003161 lr = get_last_request();
3162 wiphy_update_regulatory(wiphy, lr->initiator);
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05303163 wiphy_all_share_dfs_chan_state(wiphy);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003164}
3165
Luis R. Rodriguezbfead082012-07-12 11:49:19 -07003166void wiphy_regulatory_deregister(struct wiphy *wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003167{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04003168 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01003169 struct regulatory_request *lr;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05003170
Johannes Bergc492db32012-12-06 16:29:25 +01003171 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07003172
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003173 if (!reg_dev_ignore_cell_hint(wiphy))
3174 reg_num_devs_support_basehint--;
3175
Johannes Berg458f4f92012-12-06 15:47:38 +01003176 rcu_free_regdom(get_wiphy_regdom(wiphy));
Monam Agarwal34dd8862014-03-24 00:53:40 +05303177 RCU_INIT_POINTER(wiphy->regd, NULL);
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003178
Johannes Bergc492db32012-12-06 16:29:25 +01003179 if (lr)
3180 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05003181
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003182 if (!request_wiphy || request_wiphy != wiphy)
Johannes Berg38fd2142013-05-10 19:17:17 +02003183 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07003184
Johannes Bergc492db32012-12-06 16:29:25 +01003185 lr->wiphy_idx = WIPHY_IDX_INVALID;
3186 lr->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003187}
3188
Ilan Peer174e0cd2014-02-23 09:13:01 +02003189/*
3190 * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
3191 * UNII band definitions
3192 */
3193int cfg80211_get_unii(int freq)
3194{
3195 /* UNII-1 */
3196 if (freq >= 5150 && freq <= 5250)
3197 return 0;
3198
3199 /* UNII-2A */
3200 if (freq > 5250 && freq <= 5350)
3201 return 1;
3202
3203 /* UNII-2B */
3204 if (freq > 5350 && freq <= 5470)
3205 return 2;
3206
3207 /* UNII-2C */
3208 if (freq > 5470 && freq <= 5725)
3209 return 3;
3210
3211 /* UNII-3 */
3212 if (freq > 5725 && freq <= 5825)
3213 return 4;
3214
3215 return -EINVAL;
3216}
3217
Ilan Peerc8866e52014-02-23 09:13:03 +02003218bool regulatory_indoor_allowed(void)
3219{
3220 return reg_is_indoor;
3221}
3222
Vasanthakumar Thiagarajanb35a51c2017-02-27 17:04:33 +05303223bool regulatory_pre_cac_allowed(struct wiphy *wiphy)
3224{
3225 const struct ieee80211_regdomain *regd = NULL;
3226 const struct ieee80211_regdomain *wiphy_regd = NULL;
3227 bool pre_cac_allowed = false;
3228
3229 rcu_read_lock();
3230
3231 regd = rcu_dereference(cfg80211_regdomain);
3232 wiphy_regd = rcu_dereference(wiphy->regd);
3233 if (!wiphy_regd) {
3234 if (regd->dfs_region == NL80211_DFS_ETSI)
3235 pre_cac_allowed = true;
3236
3237 rcu_read_unlock();
3238
3239 return pre_cac_allowed;
3240 }
3241
3242 if (regd->dfs_region == wiphy_regd->dfs_region &&
3243 wiphy_regd->dfs_region == NL80211_DFS_ETSI)
3244 pre_cac_allowed = true;
3245
3246 rcu_read_unlock();
3247
3248 return pre_cac_allowed;
3249}
3250
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05303251void regulatory_propagate_dfs_state(struct wiphy *wiphy,
3252 struct cfg80211_chan_def *chandef,
3253 enum nl80211_dfs_state dfs_state,
3254 enum nl80211_radar_event event)
3255{
3256 struct cfg80211_registered_device *rdev;
3257
3258 ASSERT_RTNL();
3259
3260 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3261 return;
3262
Vasanthakumar Thiagarajan89766722017-02-27 17:04:35 +05303263 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3264 if (wiphy == &rdev->wiphy)
3265 continue;
3266
3267 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
3268 continue;
3269
3270 if (!ieee80211_get_channel(&rdev->wiphy,
3271 chandef->chan->center_freq))
3272 continue;
3273
3274 cfg80211_set_dfs_state(&rdev->wiphy, chandef, dfs_state);
3275
3276 if (event == NL80211_RADAR_DETECTED ||
3277 event == NL80211_RADAR_CAC_FINISHED)
3278 cfg80211_sched_dfs_chan_update(rdev);
3279
3280 nl80211_radar_notify(rdev, chandef, event, NULL, GFP_KERNEL);
3281 }
3282}
3283
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02003284int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003285{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003286 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02003287
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003288 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
3289 if (IS_ERR(reg_pdev))
3290 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02003291
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003292 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003293 spin_lock_init(&reg_pending_beacons_lock);
Ilan peer05050752015-03-04 00:32:06 -05003294 spin_lock_init(&reg_indoor_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003295
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -07003296 reg_regdb_size_check();
3297
Johannes Berg458f4f92012-12-06 15:47:38 +01003298 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02003299
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05003300 user_alpha2[0] = '9';
3301 user_alpha2[1] = '7';
3302
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003303 /* We always try to get an update for the static regdomain */
Johannes Berg458f4f92012-12-06 15:47:38 +01003304 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003305 if (err) {
Ola Olsson09d11802015-12-13 19:12:03 +01003306 if (err == -ENOMEM) {
3307 platform_device_unregister(reg_pdev);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003308 return err;
Ola Olsson09d11802015-12-13 19:12:03 +01003309 }
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003310 /*
3311 * N.B. kobject_uevent_env() can fail mainly for when we're out
3312 * memory which is handled and propagated appropriately above
3313 * but it can also fail during a netlink_broadcast() or during
3314 * early boot for call_usermodehelper(). For now treat these
3315 * errors as non-fatal.
3316 */
Joe Perchese9c02682010-11-16 19:56:49 -08003317 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05003318 }
Johannes Berg734366d2008-09-15 10:56:48 +02003319
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003320 /*
3321 * Finally, if the user set the module parameter treat it
3322 * as a user hint.
3323 */
3324 if (!is_world_regdom(ieee80211_regdom))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003325 regulatory_hint_user(ieee80211_regdom,
3326 NL80211_USER_REG_HINT_USER);
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04003327
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003328 return 0;
3329}
3330
Johannes Berg1a919312012-12-03 17:21:11 +01003331void regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003332{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003333 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003334 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003335
3336 cancel_work_sync(&reg_work);
Johannes Bergb6863032015-10-15 09:25:18 +02003337 cancel_crda_timeout_sync();
Arik Nemtsovad932f02014-11-27 09:44:55 +02003338 cancel_delayed_work_sync(&reg_check_chans);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003339
Johannes Berg9027b142012-12-03 17:59:24 +01003340 /* Lock to suppress warnings */
Johannes Berg38fd2142013-05-10 19:17:17 +02003341 rtnl_lock();
Johannes Berg379b82f2012-12-06 15:44:07 +01003342 reset_regdomains(true, NULL);
Johannes Berg38fd2142013-05-10 19:17:17 +02003343 rtnl_unlock();
Johannes Berg734366d2008-09-15 10:56:48 +02003344
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08003345 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02003346
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003347 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02003348
Johannes Bergfea9bce2012-12-03 17:32:01 +01003349 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
3350 list_del(&reg_beacon->list);
3351 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05003352 }
3353
Johannes Bergfea9bce2012-12-03 17:32:01 +01003354 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
3355 list_del(&reg_beacon->list);
3356 kfree(reg_beacon);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003357 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003358
Johannes Bergfea9bce2012-12-03 17:32:01 +01003359 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
3360 list_del(&reg_request->list);
3361 kfree(reg_request);
Johannes Berg8318d782008-01-24 19:38:38 +01003362 }
Johannes Berg8318d782008-01-24 19:38:38 +01003363}