blob: b95e9cf139c0aeb736a578f3f29bd88c5d95bfd7 [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 Berg8318d782008-01-24 19:38:38 +01006 *
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -08007 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Johannes Berg8318d782008-01-24 19:38:38 +010018 */
19
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -080020
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070021/**
22 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010023 *
24 * The usual implementation is for a driver to read a device EEPROM to
25 * determine which regulatory domain it should be operating under, then
26 * looking up the allowable channels in a driver-local table and finally
27 * registering those channels in the wiphy structure.
28 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070029 * Another set of compliance enforcement is for drivers to use their
30 * own compliance limits which can be stored on the EEPROM. The host
31 * driver or firmware may ensure these are used.
32 *
33 * In addition to all this we provide an extra layer of regulatory
34 * conformance. For drivers which do not have any regulatory
35 * information CRDA provides the complete regulatory solution.
36 * For others it provides a community effort on further restrictions
37 * to enhance compliance.
38 *
39 * Note: When number of rules --> infinity we will not be able to
40 * index on alpha2 any more, instead we'll probably have to
41 * rely on some SHA1 checksum of the regdomain for example.
42 *
Johannes Berg8318d782008-01-24 19:38:38 +010043 */
Joe Perchese9c02682010-11-16 19:56:49 -080044
45#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Johannes Berg8318d782008-01-24 19:38:38 +010047#include <linux/kernel.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040048#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070050#include <linux/list.h>
John W. Linvillec61029c2010-08-05 14:26:24 -040051#include <linux/ctype.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070052#include <linux/nl80211.h>
53#include <linux/platform_device.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040054#include <linux/moduleparam.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070055#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010056#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070057#include "reg.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050058#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040059#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010060
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050061#ifdef CONFIG_CFG80211_REG_DEBUG
Joe Perches12c5ffb2011-07-29 14:51:25 -070062#define REG_DBG_PRINT(format, args...) \
63 printk(KERN_DEBUG pr_fmt(format), ##args)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050064#else
John W. Linville82711952010-01-05 17:57:20 -050065#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050066#endif
67
Johannes Berg2f922122012-12-03 17:54:55 +010068enum reg_request_treatment {
69 REG_REQ_OK,
70 REG_REQ_IGNORE,
71 REG_REQ_INTERSECT,
72 REG_REQ_ALREADY_SET,
73};
74
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050075static struct regulatory_request core_request_world = {
76 .initiator = NL80211_REGDOM_SET_BY_CORE,
77 .alpha2[0] = '0',
78 .alpha2[1] = '0',
79 .intersect = false,
80 .processed = true,
81 .country_ie_env = ENVIRON_ANY,
82};
83
Johannes Berg38fd2142013-05-10 19:17:17 +020084/*
85 * Receipt of information from last regulatory request,
86 * protected by RTNL (and can be accessed with RCU protection)
87 */
Johannes Bergc492db32012-12-06 16:29:25 +010088static struct regulatory_request __rcu *last_request =
89 (void __rcu *)&core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +020090
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070091/* To trigger userspace events */
92static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010093
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050094/*
95 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020096 * the current one and a world regulatory domain in case we have no
Johannes Berge8da2bb2012-12-03 23:00:08 +010097 * information to give us an alpha2.
Johannes Berg38fd2142013-05-10 19:17:17 +020098 * (protected by RTNL, can be read under RCU)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050099 */
Johannes Berg458f4f92012-12-06 15:47:38 +0100100const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +0200101
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500102/*
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700103 * Number of devices that registered to the core
104 * that support cellular base station regulatory hints
Johannes Berg38fd2142013-05-10 19:17:17 +0200105 * (protected by RTNL)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700106 */
107static int reg_num_devs_support_basehint;
108
Johannes Berg458f4f92012-12-06 15:47:38 +0100109static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
110{
Johannes Berg38fd2142013-05-10 19:17:17 +0200111 return rtnl_dereference(cfg80211_regdomain);
Johannes Berg458f4f92012-12-06 15:47:38 +0100112}
113
114static const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
115{
Johannes Berg38fd2142013-05-10 19:17:17 +0200116 return rtnl_dereference(wiphy->regd);
Johannes Berg458f4f92012-12-06 15:47:38 +0100117}
118
Luis R. Rodriguez3ef121b2013-11-13 18:54:05 +0100119static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
120{
121 switch (dfs_region) {
122 case NL80211_DFS_UNSET:
123 return "unset";
124 case NL80211_DFS_FCC:
125 return "FCC";
126 case NL80211_DFS_ETSI:
127 return "ETSI";
128 case NL80211_DFS_JP:
129 return "JP";
130 }
131 return "Unknown";
132}
133
Luis R. Rodriguez6c474792013-11-25 20:56:09 +0100134enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
135{
136 const struct ieee80211_regdomain *regd = NULL;
137 const struct ieee80211_regdomain *wiphy_regd = NULL;
138
139 regd = get_cfg80211_regdom();
140 if (!wiphy)
141 goto out;
142
143 wiphy_regd = get_wiphy_regdom(wiphy);
144 if (!wiphy_regd)
145 goto out;
146
147 if (wiphy_regd->dfs_region == regd->dfs_region)
148 goto out;
149
150 REG_DBG_PRINT("%s: device specific dfs_region "
151 "(%s) disagrees with cfg80211's "
152 "central dfs_region (%s)\n",
153 dev_name(&wiphy->dev),
154 reg_dfs_region_str(wiphy_regd->dfs_region),
155 reg_dfs_region_str(regd->dfs_region));
156
157out:
158 return regd->dfs_region;
159}
160
Johannes Berg458f4f92012-12-06 15:47:38 +0100161static void rcu_free_regdom(const struct ieee80211_regdomain *r)
162{
163 if (!r)
164 return;
165 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
166}
167
Johannes Bergc492db32012-12-06 16:29:25 +0100168static struct regulatory_request *get_last_request(void)
169{
Johannes Berg38fd2142013-05-10 19:17:17 +0200170 return rcu_dereference_rtnl(last_request);
Johannes Bergc492db32012-12-06 16:29:25 +0100171}
172
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500173/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500174static LIST_HEAD(reg_requests_list);
175static spinlock_t reg_requests_lock;
176
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500177/* Used to queue up beacon hints for review */
178static LIST_HEAD(reg_pending_beacons);
179static spinlock_t reg_pending_beacons_lock;
180
181/* Used to keep track of processed beacon hints */
182static LIST_HEAD(reg_beacon_list);
183
184struct reg_beacon {
185 struct list_head list;
186 struct ieee80211_channel chan;
187};
188
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800189static void reg_todo(struct work_struct *work);
190static DECLARE_WORK(reg_work, reg_todo);
191
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -0700192static void reg_timeout_work(struct work_struct *work);
193static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
194
Johannes Berg734366d2008-09-15 10:56:48 +0200195/* We keep a static world regulatory domain in case of the absence of CRDA */
196static const struct ieee80211_regdomain world_regdom = {
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300197 .n_reg_rules = 6,
Johannes Berg734366d2008-09-15 10:56:48 +0200198 .alpha2 = "00",
199 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500200 /* IEEE 802.11b/g, channels 1..11 */
201 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Johannes Berg43c771a2012-11-12 10:51:34 +0100202 /* IEEE 802.11b/g, channels 12..13. */
203 REG_RULE(2467-10, 2472+10, 40, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200204 NL80211_RRF_NO_IR),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800205 /* IEEE 802.11 channel 14 - Only JP enables
206 * this and for 802.11b only */
207 REG_RULE(2484-10, 2484+10, 20, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200208 NL80211_RRF_NO_IR |
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800209 NL80211_RRF_NO_OFDM),
210 /* IEEE 802.11a, channel 36..48 */
Johannes Berg131a19b2013-03-04 20:54:46 +0100211 REG_RULE(5180-10, 5240+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200212 NL80211_RRF_NO_IR),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500213
Johannes Berg131a19b2013-03-04 20:54:46 +0100214 /* IEEE 802.11a, channel 52..64 - DFS required */
215 REG_RULE(5260-10, 5320+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200216 NL80211_RRF_NO_IR |
Johannes Berg131a19b2013-03-04 20:54:46 +0100217 NL80211_RRF_DFS),
218
219 /* IEEE 802.11a, channel 100..144 - DFS required */
220 REG_RULE(5500-10, 5720+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200221 NL80211_RRF_NO_IR |
Johannes Berg131a19b2013-03-04 20:54:46 +0100222 NL80211_RRF_DFS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500223
224 /* IEEE 802.11a, channel 149..165 */
Johannes Berg8ab9d852012-12-03 22:09:22 +0100225 REG_RULE(5745-10, 5825+10, 80, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200226 NL80211_RRF_NO_IR),
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300227
228 /* IEEE 802.11ad (60gHz), channels 1..3 */
229 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200230 }
231};
232
Johannes Berg38fd2142013-05-10 19:17:17 +0200233/* protected by RTNL */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200234static const struct ieee80211_regdomain *cfg80211_world_regdom =
235 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200236
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400237static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500238static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400239
Johannes Berg734366d2008-09-15 10:56:48 +0200240module_param(ieee80211_regdom, charp, 0444);
241MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
242
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800243static void reg_kfree_last_request(void)
244{
245 struct regulatory_request *lr;
246
247 lr = get_last_request();
248
249 if (lr != &core_request_world && lr)
250 kfree_rcu(lr, rcu_head);
251}
252
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800253static void reg_update_last_request(struct regulatory_request *request)
254{
255 reg_kfree_last_request();
256 rcu_assign_pointer(last_request, request);
257}
258
Johannes Berg379b82f2012-12-06 15:44:07 +0100259static void reset_regdomains(bool full_reset,
260 const struct ieee80211_regdomain *new_regdom)
Johannes Berg734366d2008-09-15 10:56:48 +0200261{
Johannes Berg458f4f92012-12-06 15:47:38 +0100262 const struct ieee80211_regdomain *r;
263
Johannes Berg38fd2142013-05-10 19:17:17 +0200264 ASSERT_RTNL();
Johannes Berge8da2bb2012-12-03 23:00:08 +0100265
Johannes Berg458f4f92012-12-06 15:47:38 +0100266 r = get_cfg80211_regdom();
267
Johannes Berg942b25c2008-09-15 11:26:47 +0200268 /* avoid freeing static information or freeing something twice */
Johannes Berg458f4f92012-12-06 15:47:38 +0100269 if (r == cfg80211_world_regdom)
270 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200271 if (cfg80211_world_regdom == &world_regdom)
272 cfg80211_world_regdom = NULL;
Johannes Berg458f4f92012-12-06 15:47:38 +0100273 if (r == &world_regdom)
274 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200275
Johannes Berg458f4f92012-12-06 15:47:38 +0100276 rcu_free_regdom(r);
277 rcu_free_regdom(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200278
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200279 cfg80211_world_regdom = &world_regdom;
Johannes Berg458f4f92012-12-06 15:47:38 +0100280 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500281
282 if (!full_reset)
283 return;
284
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800285 reg_update_last_request(&core_request_world);
Johannes Berg734366d2008-09-15 10:56:48 +0200286}
287
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500288/*
289 * Dynamic world regulatory domain requested by the wireless
290 * core upon initialization
291 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200292static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200293{
Johannes Bergc492db32012-12-06 16:29:25 +0100294 struct regulatory_request *lr;
Johannes Berg734366d2008-09-15 10:56:48 +0200295
Johannes Bergc492db32012-12-06 16:29:25 +0100296 lr = get_last_request();
297
298 WARN_ON(!lr);
Johannes Berge8da2bb2012-12-03 23:00:08 +0100299
Johannes Berg379b82f2012-12-06 15:44:07 +0100300 reset_regdomains(false, rd);
Johannes Berg734366d2008-09-15 10:56:48 +0200301
302 cfg80211_world_regdom = rd;
Johannes Berg734366d2008-09-15 10:56:48 +0200303}
Johannes Berg734366d2008-09-15 10:56:48 +0200304
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200305bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100306{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700307 if (!alpha2)
308 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100309 return alpha2[0] == '0' && alpha2[1] == '0';
Johannes Berg8318d782008-01-24 19:38:38 +0100310}
311
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200312static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700313{
314 if (!alpha2)
315 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100316 return alpha2[0] && alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700317}
Johannes Berg8318d782008-01-24 19:38:38 +0100318
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200319static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700320{
321 if (!alpha2)
322 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500323 /*
324 * Special case where regulatory domain was built by driver
325 * but a specific alpha2 cannot be determined
326 */
Johannes Berg1a919312012-12-03 17:21:11 +0100327 return alpha2[0] == '9' && alpha2[1] == '9';
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700328}
329
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800330static bool is_intersected_alpha2(const char *alpha2)
331{
332 if (!alpha2)
333 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500334 /*
335 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800336 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500337 * structures
338 */
Johannes Berg1a919312012-12-03 17:21:11 +0100339 return alpha2[0] == '9' && alpha2[1] == '8';
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800340}
341
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200342static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700343{
344 if (!alpha2)
345 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100346 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700347}
348
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200349static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700350{
351 if (!alpha2_x || !alpha2_y)
352 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100353 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700354}
355
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500356static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700357{
Johannes Berg458f4f92012-12-06 15:47:38 +0100358 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500359
Johannes Berg458f4f92012-12-06 15:47:38 +0100360 if (!r)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700361 return true;
Johannes Berg458f4f92012-12-06 15:47:38 +0100362 return !alpha2_equal(r->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700363}
364
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500365/*
366 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
367 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
368 * has ever been issued.
369 */
370static bool is_user_regdom_saved(void)
371{
372 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
373 return false;
374
375 /* This would indicate a mistake on the design */
Johannes Berg1a919312012-12-03 17:21:11 +0100376 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500377 "Unexpected user alpha2: %c%c\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100378 user_alpha2[0], user_alpha2[1]))
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500379 return false;
380
381 return true;
382}
383
Johannes Berge9763c32012-12-03 16:59:46 +0100384static const struct ieee80211_regdomain *
385reg_copy_regd(const struct ieee80211_regdomain *src_regd)
John W. Linville3b377ea2009-12-18 17:59:01 -0500386{
387 struct ieee80211_regdomain *regd;
Johannes Berge9763c32012-12-03 16:59:46 +0100388 int size_of_regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500389 unsigned int i;
390
Johannes Berg82f20852012-12-04 12:49:16 +0100391 size_of_regd =
392 sizeof(struct ieee80211_regdomain) +
393 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
John W. Linville3b377ea2009-12-18 17:59:01 -0500394
395 regd = kzalloc(size_of_regd, GFP_KERNEL);
396 if (!regd)
Johannes Berge9763c32012-12-03 16:59:46 +0100397 return ERR_PTR(-ENOMEM);
John W. Linville3b377ea2009-12-18 17:59:01 -0500398
399 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
400
401 for (i = 0; i < src_regd->n_reg_rules; i++)
402 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
Johannes Berge9763c32012-12-03 16:59:46 +0100403 sizeof(struct ieee80211_reg_rule));
John W. Linville3b377ea2009-12-18 17:59:01 -0500404
Johannes Berge9763c32012-12-03 16:59:46 +0100405 return regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500406}
407
408#ifdef CONFIG_CFG80211_INTERNAL_REGDB
409struct reg_regdb_search_request {
410 char alpha2[2];
411 struct list_head list;
412};
413
414static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400415static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500416
417static void reg_regdb_search(struct work_struct *work)
418{
419 struct reg_regdb_search_request *request;
Johannes Berge9763c32012-12-03 16:59:46 +0100420 const struct ieee80211_regdomain *curdom, *regdom = NULL;
421 int i;
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700422
Johannes Berg5fe231e2013-05-08 21:45:15 +0200423 rtnl_lock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500424
John W. Linville368d06f2010-03-16 15:40:59 -0400425 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500426 while (!list_empty(&reg_regdb_search_list)) {
427 request = list_first_entry(&reg_regdb_search_list,
428 struct reg_regdb_search_request,
429 list);
430 list_del(&request->list);
431
Johannes Berg1a919312012-12-03 17:21:11 +0100432 for (i = 0; i < reg_regdb_size; i++) {
John W. Linville3b377ea2009-12-18 17:59:01 -0500433 curdom = reg_regdb[i];
434
Johannes Berg1a919312012-12-03 17:21:11 +0100435 if (alpha2_equal(request->alpha2, curdom->alpha2)) {
Johannes Berge9763c32012-12-03 16:59:46 +0100436 regdom = reg_copy_regd(curdom);
John W. Linville3b377ea2009-12-18 17:59:01 -0500437 break;
438 }
439 }
440
441 kfree(request);
442 }
John W. Linville368d06f2010-03-16 15:40:59 -0400443 mutex_unlock(&reg_regdb_search_mutex);
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700444
Johannes Berge9763c32012-12-03 16:59:46 +0100445 if (!IS_ERR_OR_NULL(regdom))
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700446 set_regdom(regdom);
447
Johannes Berg5fe231e2013-05-08 21:45:15 +0200448 rtnl_unlock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500449}
450
451static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
452
453static void reg_regdb_query(const char *alpha2)
454{
455 struct reg_regdb_search_request *request;
456
457 if (!alpha2)
458 return;
459
460 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
461 if (!request)
462 return;
463
464 memcpy(request->alpha2, alpha2, 2);
465
John W. Linville368d06f2010-03-16 15:40:59 -0400466 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500467 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400468 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500469
470 schedule_work(&reg_regdb_work);
471}
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700472
473/* Feel free to add any other sanity checks here */
474static void reg_regdb_size_check(void)
475{
476 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
477 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
478}
John W. Linville3b377ea2009-12-18 17:59:01 -0500479#else
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700480static inline void reg_regdb_size_check(void) {}
John W. Linville3b377ea2009-12-18 17:59:01 -0500481static inline void reg_regdb_query(const char *alpha2) {}
482#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
483
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500484/*
485 * This lets us keep regulatory code which is updated on a regulatory
Johannes Berg1226d252014-02-25 15:43:36 +0100486 * basis in userspace.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500487 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700488static int call_crda(const char *alpha2)
489{
Johannes Berg1226d252014-02-25 15:43:36 +0100490 char country[12];
491 char *env[] = { country, NULL };
492
493 snprintf(country, sizeof(country), "COUNTRY=%c%c",
494 alpha2[0], alpha2[1]);
495
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700496 if (!is_world_regdom((char *) alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -0800497 pr_info("Calling CRDA for country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700498 alpha2[0], alpha2[1]);
499 else
Joe Perchese9c02682010-11-16 19:56:49 -0800500 pr_info("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700501
John W. Linville3b377ea2009-12-18 17:59:01 -0500502 /* query internal regulatory database (if it exists) */
503 reg_regdb_query(alpha2);
504
Johannes Berg1226d252014-02-25 15:43:36 +0100505 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700506}
507
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -0800508static enum reg_request_treatment
509reg_call_crda(struct regulatory_request *request)
510{
511 if (call_crda(request->alpha2))
512 return REG_REQ_IGNORE;
513 return REG_REQ_OK;
514}
515
Luis R. Rodrigueze4387682013-11-05 09:18:01 -0800516bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700517{
Johannes Bergc492db32012-12-06 16:29:25 +0100518 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400519
Johannes Bergc492db32012-12-06 16:29:25 +0100520 if (!lr || lr->processed)
Johannes Bergf6037d02008-10-21 11:01:33 +0200521 return false;
522
Johannes Bergc492db32012-12-06 16:29:25 +0100523 return alpha2_equal(lr->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700524}
525
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100526static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
527{
528 struct regulatory_request *lr = get_last_request();
529
530 /*
531 * Follow the driver's regulatory domain, if present, unless a country
532 * IE has been processed or a user wants to help complaince further
533 */
534 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
535 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
536 wiphy->regd)
537 return get_wiphy_regdom(wiphy);
538
539 return get_cfg80211_regdom();
540}
541
Janusz Dziedzic97524822014-01-30 09:52:20 +0100542unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
543 const struct ieee80211_reg_rule *rule)
544{
545 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
546 const struct ieee80211_freq_range *freq_range_tmp;
547 const struct ieee80211_reg_rule *tmp;
548 u32 start_freq, end_freq, idx, no;
549
550 for (idx = 0; idx < rd->n_reg_rules; idx++)
551 if (rule == &rd->reg_rules[idx])
552 break;
553
554 if (idx == rd->n_reg_rules)
555 return 0;
556
557 /* get start_freq */
558 no = idx;
559
560 while (no) {
561 tmp = &rd->reg_rules[--no];
562 freq_range_tmp = &tmp->freq_range;
563
564 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
565 break;
566
Janusz Dziedzic97524822014-01-30 09:52:20 +0100567 freq_range = freq_range_tmp;
568 }
569
570 start_freq = freq_range->start_freq_khz;
571
572 /* get end_freq */
573 freq_range = &rule->freq_range;
574 no = idx;
575
576 while (no < rd->n_reg_rules - 1) {
577 tmp = &rd->reg_rules[++no];
578 freq_range_tmp = &tmp->freq_range;
579
580 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
581 break;
582
Janusz Dziedzic97524822014-01-30 09:52:20 +0100583 freq_range = freq_range_tmp;
584 }
585
586 end_freq = freq_range->end_freq_khz;
587
588 return end_freq - start_freq;
589}
590
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700591/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200592static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700593{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200594 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700595 u32 freq_diff;
596
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800597 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700598 return false;
599
600 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
601 return false;
602
603 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
604
Roel Kluinbd05f282009-03-03 22:55:21 +0100605 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg1a919312012-12-03 17:21:11 +0100606 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700607 return false;
608
609 return true;
610}
611
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200612static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700613{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200614 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700615 unsigned int i;
616
617 if (!rd->n_reg_rules)
618 return false;
619
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800620 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
621 return false;
622
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700623 for (i = 0; i < rd->n_reg_rules; i++) {
624 reg_rule = &rd->reg_rules[i];
625 if (!is_valid_reg_rule(reg_rule))
626 return false;
627 }
628
629 return true;
630}
631
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400632static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100633 u32 center_freq_khz, u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700634{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400635 u32 start_freq_khz, end_freq_khz;
636
637 start_freq_khz = center_freq_khz - (bw_khz/2);
638 end_freq_khz = center_freq_khz + (bw_khz/2);
639
640 if (start_freq_khz >= freq_range->start_freq_khz &&
641 end_freq_khz <= freq_range->end_freq_khz)
642 return true;
643
644 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700645}
646
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800647/**
648 * freq_in_rule_band - tells us if a frequency is in a frequency band
649 * @freq_range: frequency rule we want to query
650 * @freq_khz: frequency we are inquiring about
651 *
652 * This lets us know if a specific frequency rule is or is not relevant to
653 * a specific frequency's band. Bands are device specific and artificial
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200654 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
655 * however it is safe for now to assume that a frequency rule should not be
656 * part of a frequency's band if the start freq or end freq are off by more
657 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
658 * 60 GHz band.
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800659 * This resolution can be lowered and should be considered as we add
660 * regulatory rule support for other "bands".
661 **/
662static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg1a919312012-12-03 17:21:11 +0100663 u32 freq_khz)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800664{
665#define ONE_GHZ_IN_KHZ 1000000
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200666 /*
667 * From 802.11ad: directional multi-gigabit (DMG):
668 * Pertaining to operation in a frequency band containing a channel
669 * with the Channel starting frequency above 45 GHz.
670 */
671 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
672 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
673 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800674 return true;
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200675 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800676 return true;
677 return false;
678#undef ONE_GHZ_IN_KHZ
679}
680
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500681/*
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100682 * Later on we can perhaps use the more restrictive DFS
683 * region but we don't have information for that yet so
684 * for now simply disallow conflicts.
685 */
686static enum nl80211_dfs_regions
687reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
688 const enum nl80211_dfs_regions dfs_region2)
689{
690 if (dfs_region1 != dfs_region2)
691 return NL80211_DFS_UNSET;
692 return dfs_region1;
693}
694
695/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500696 * Helper for regdom_intersect(), this does the real
697 * mathematical intersection fun
698 */
Janusz Dziedzic97524822014-01-30 09:52:20 +0100699static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
700 const struct ieee80211_regdomain *rd2,
701 const struct ieee80211_reg_rule *rule1,
Johannes Berg1a919312012-12-03 17:21:11 +0100702 const struct ieee80211_reg_rule *rule2,
703 struct ieee80211_reg_rule *intersected_rule)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700704{
705 const struct ieee80211_freq_range *freq_range1, *freq_range2;
706 struct ieee80211_freq_range *freq_range;
707 const struct ieee80211_power_rule *power_rule1, *power_rule2;
708 struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +0100709 u32 freq_diff, max_bandwidth1, max_bandwidth2;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700710
711 freq_range1 = &rule1->freq_range;
712 freq_range2 = &rule2->freq_range;
713 freq_range = &intersected_rule->freq_range;
714
715 power_rule1 = &rule1->power_rule;
716 power_rule2 = &rule2->power_rule;
717 power_rule = &intersected_rule->power_rule;
718
719 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100720 freq_range2->start_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700721 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100722 freq_range2->end_freq_khz);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100723
724 max_bandwidth1 = freq_range1->max_bandwidth_khz;
725 max_bandwidth2 = freq_range2->max_bandwidth_khz;
726
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100727 if (rule1->flags & NL80211_RRF_AUTO_BW)
728 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
729 if (rule2->flags & NL80211_RRF_AUTO_BW)
730 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100731
732 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700733
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100734 intersected_rule->flags = rule1->flags | rule2->flags;
735
736 /*
737 * In case NL80211_RRF_AUTO_BW requested for both rules
738 * set AUTO_BW in intersected rule also. Next we will
739 * calculate BW correctly in handle_channel function.
740 * In other case remove AUTO_BW flag while we calculate
741 * maximum bandwidth correctly and auto calculation is
742 * not required.
743 */
744 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
745 (rule2->flags & NL80211_RRF_AUTO_BW))
746 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
747 else
748 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
749
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700750 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
751 if (freq_range->max_bandwidth_khz > freq_diff)
752 freq_range->max_bandwidth_khz = freq_diff;
753
754 power_rule->max_eirp = min(power_rule1->max_eirp,
755 power_rule2->max_eirp);
756 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
757 power_rule2->max_antenna_gain);
758
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100759 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
760 rule2->dfs_cac_ms);
761
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700762 if (!is_valid_reg_rule(intersected_rule))
763 return -EINVAL;
764
765 return 0;
766}
767
768/**
769 * regdom_intersect - do the intersection between two regulatory domains
770 * @rd1: first regulatory domain
771 * @rd2: second regulatory domain
772 *
773 * Use this function to get the intersection between two regulatory domains.
774 * Once completed we will mark the alpha2 for the rd as intersected, "98",
775 * as no one single alpha2 can represent this regulatory domain.
776 *
777 * Returns a pointer to the regulatory domain structure which will hold the
778 * resulting intersection of rules between rd1 and rd2. We will
779 * kzalloc() this structure for you.
780 */
Johannes Berg1a919312012-12-03 17:21:11 +0100781static struct ieee80211_regdomain *
782regdom_intersect(const struct ieee80211_regdomain *rd1,
783 const struct ieee80211_regdomain *rd2)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700784{
785 int r, size_of_regd;
786 unsigned int x, y;
787 unsigned int num_rules = 0, rule_idx = 0;
788 const struct ieee80211_reg_rule *rule1, *rule2;
789 struct ieee80211_reg_rule *intersected_rule;
790 struct ieee80211_regdomain *rd;
791 /* This is just a dummy holder to help us count */
Johannes Berg74f53cd2012-12-06 17:26:17 +0100792 struct ieee80211_reg_rule dummy_rule;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700793
794 if (!rd1 || !rd2)
795 return NULL;
796
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500797 /*
798 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700799 * build them. This is to so we can malloc() and free() a
800 * regdomain once. The reason we use reg_rules_intersect() here
801 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500802 * All rules that do check out OK are valid.
803 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700804
805 for (x = 0; x < rd1->n_reg_rules; x++) {
806 rule1 = &rd1->reg_rules[x];
807 for (y = 0; y < rd2->n_reg_rules; y++) {
808 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100809 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
810 &dummy_rule))
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700811 num_rules++;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700812 }
813 }
814
815 if (!num_rules)
816 return NULL;
817
818 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg82f20852012-12-04 12:49:16 +0100819 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700820
821 rd = kzalloc(size_of_regd, GFP_KERNEL);
822 if (!rd)
823 return NULL;
824
Johannes Berg8a57fff2012-12-06 17:03:17 +0100825 for (x = 0; x < rd1->n_reg_rules && rule_idx < num_rules; x++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700826 rule1 = &rd1->reg_rules[x];
Johannes Berg8a57fff2012-12-06 17:03:17 +0100827 for (y = 0; y < rd2->n_reg_rules && rule_idx < num_rules; y++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700828 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500829 /*
830 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700831 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500832 * a memcpy()
833 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700834 intersected_rule = &rd->reg_rules[rule_idx];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100835 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
836 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500837 /*
838 * No need to memset here the intersected rule here as
839 * we're not using the stack anymore
840 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700841 if (r)
842 continue;
843 rule_idx++;
844 }
845 }
846
847 if (rule_idx != num_rules) {
848 kfree(rd);
849 return NULL;
850 }
851
852 rd->n_reg_rules = num_rules;
853 rd->alpha2[0] = '9';
854 rd->alpha2[1] = '8';
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100855 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
856 rd2->dfs_region);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700857
858 return rd;
859}
860
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500861/*
862 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
863 * want to just have the channel structure use these
864 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700865static u32 map_regdom_flags(u32 rd_flags)
866{
867 u32 channel_flags = 0;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200868 if (rd_flags & NL80211_RRF_NO_IR_ALL)
869 channel_flags |= IEEE80211_CHAN_NO_IR;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700870 if (rd_flags & NL80211_RRF_DFS)
871 channel_flags |= IEEE80211_CHAN_RADAR;
Seth Forshee03f6b082012-08-01 15:58:42 -0500872 if (rd_flags & NL80211_RRF_NO_OFDM)
873 channel_flags |= IEEE80211_CHAN_NO_OFDM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700874 return channel_flags;
875}
876
Johannes Berg361c9c82012-12-06 15:57:14 +0100877static const struct ieee80211_reg_rule *
878freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
879 const struct ieee80211_regdomain *regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100880{
881 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800882 bool band_rule_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400883 bool bw_fits = false;
884
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800885 if (!regd)
Johannes Berg361c9c82012-12-06 15:57:14 +0100886 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700887
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800888 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700889 const struct ieee80211_reg_rule *rr;
890 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700891
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800892 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700893 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800894
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500895 /*
896 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800897 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500898 * not overwrite it once found
899 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800900 if (!band_rule_found)
901 band_rule_found = freq_in_rule_band(fr, center_freq);
902
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100903 bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800904
Johannes Berg361c9c82012-12-06 15:57:14 +0100905 if (band_rule_found && bw_fits)
906 return rr;
Johannes Berg8318d782008-01-24 19:38:38 +0100907 }
908
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800909 if (!band_rule_found)
Johannes Berg361c9c82012-12-06 15:57:14 +0100910 return ERR_PTR(-ERANGE);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800911
Johannes Berg361c9c82012-12-06 15:57:14 +0100912 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700913}
914
Johannes Berg361c9c82012-12-06 15:57:14 +0100915const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
916 u32 center_freq)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800917{
Johannes Berg5d885b92012-12-04 00:14:17 +0100918 const struct ieee80211_regdomain *regd;
Johannes Berg1a919312012-12-03 17:21:11 +0100919
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100920 regd = reg_get_regdomain(wiphy);
Johannes Berg5d885b92012-12-04 00:14:17 +0100921
Johannes Berg361c9c82012-12-06 15:57:14 +0100922 return freq_reg_info_regd(wiphy, center_freq, regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800923}
John W. Linville4f366c52010-07-15 14:57:33 -0400924EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700925
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700926const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530927{
928 switch (initiator) {
929 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700930 return "core";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530931 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700932 return "user";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530933 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700934 return "driver";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530935 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700936 return "country IE";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530937 default:
938 WARN_ON(1);
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700939 return "bug";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530940 }
941}
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700942EXPORT_SYMBOL(reg_initiator_name);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530943
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700944#ifdef CONFIG_CFG80211_REG_DEBUG
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100945static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
946 struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530947 const struct ieee80211_reg_rule *reg_rule)
948{
949 const struct ieee80211_power_rule *power_rule;
950 const struct ieee80211_freq_range *freq_range;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100951 char max_antenna_gain[32], bw[32];
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530952
953 power_rule = &reg_rule->power_rule;
954 freq_range = &reg_rule->freq_range;
955
956 if (!power_rule->max_antenna_gain)
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100957 snprintf(max_antenna_gain, sizeof(max_antenna_gain), "N/A");
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530958 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100959 snprintf(max_antenna_gain, sizeof(max_antenna_gain), "%d",
960 power_rule->max_antenna_gain);
961
962 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
963 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
964 freq_range->max_bandwidth_khz,
965 reg_get_max_bandwidth(regd, reg_rule));
966 else
967 snprintf(bw, sizeof(bw), "%d KHz",
968 freq_range->max_bandwidth_khz);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530969
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100970 REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
971 chan->center_freq);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530972
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100973 REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100974 freq_range->start_freq_khz, freq_range->end_freq_khz,
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100975 bw, max_antenna_gain,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530976 power_rule->max_eirp);
977}
978#else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100979static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
980 struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530981 const struct ieee80211_reg_rule *reg_rule)
982{
983 return;
984}
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530985#endif
986
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400987/*
988 * Note that right now we assume the desired channel bandwidth
989 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100990 * per channel, the primary and the extension channel).
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400991 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700992static void handle_channel(struct wiphy *wiphy,
993 enum nl80211_reg_initiator initiator,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100994 struct ieee80211_channel *chan)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700995{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400996 u32 flags, bw_flags = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700997 const struct ieee80211_reg_rule *reg_rule = NULL;
998 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400999 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001000 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01001001 struct regulatory_request *lr = get_last_request();
Janusz Dziedzic97524822014-01-30 09:52:20 +01001002 const struct ieee80211_regdomain *regd;
1003 u32 max_bandwidth_khz;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001004
Johannes Bergc492db32012-12-06 16:29:25 +01001005 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001006
1007 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001008
Johannes Berg361c9c82012-12-06 15:57:14 +01001009 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1010 if (IS_ERR(reg_rule)) {
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001011 /*
1012 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001013 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001014 * from a Country IE and the Country IE had no information
1015 * about a band. The IEEE 802.11 spec allows for an AP
1016 * to send only a subset of the regulatory rules allowed,
1017 * so an AP in the US that only supports 2.4 GHz may only send
1018 * a country IE with information for the 2.4 GHz band
1019 * while 5 GHz is still supported.
1020 */
1021 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg361c9c82012-12-06 15:57:14 +01001022 PTR_ERR(reg_rule) == -ERANGE)
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001023 return;
1024
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001025 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1026 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001027 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001028 REG_DBG_PRINT("Disabling freq %d MHz for good\n",
1029 chan->center_freq);
1030 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1031 chan->flags = chan->orig_flags;
1032 } else {
1033 REG_DBG_PRINT("Disabling freq %d MHz\n",
1034 chan->center_freq);
1035 chan->flags |= IEEE80211_CHAN_DISABLED;
1036 }
Johannes Berg8318d782008-01-24 19:38:38 +01001037 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001038 }
Johannes Berg8318d782008-01-24 19:38:38 +01001039
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001040 regd = reg_get_regdomain(wiphy);
1041 chan_reg_rule_print_dbg(regd, chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301042
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001043 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001044 freq_range = &reg_rule->freq_range;
1045
Janusz Dziedzic97524822014-01-30 09:52:20 +01001046 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1047 /* Check if auto calculation requested */
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001048 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001049 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
Janusz Dziedzic97524822014-01-30 09:52:20 +01001050
1051 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001052 bw_flags = IEEE80211_CHAN_NO_HT40;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001053 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001054 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001055 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001056 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001057
Johannes Bergc492db32012-12-06 16:29:25 +01001058 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001059 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001060 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001061 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001062 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001063 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001064 * settings
1065 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001066 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001067 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001068 chan->max_antenna_gain = chan->orig_mag =
1069 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001070 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001071 (int) MBM_TO_DBM(power_rule->max_eirp);
1072 return;
1073 }
1074
Simon Wunderlich04f39042013-02-08 18:16:19 +01001075 chan->dfs_state = NL80211_DFS_USABLE;
1076 chan->dfs_state_entered = jiffies;
1077
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +05301078 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001079 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg1a919312012-12-03 17:21:11 +01001080 chan->max_antenna_gain =
1081 min_t(int, chan->orig_mag,
1082 MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +02001083 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic089027e2014-02-21 19:46:12 +01001084
1085 if (chan->flags & IEEE80211_CHAN_RADAR) {
1086 if (reg_rule->dfs_cac_ms)
1087 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1088 else
1089 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1090 }
1091
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001092 if (chan->orig_mpwr) {
1093 /*
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001094 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1095 * will always follow the passed country IE power settings.
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001096 */
1097 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001098 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001099 chan->max_power = chan->max_reg_power;
1100 else
1101 chan->max_power = min(chan->orig_mpwr,
1102 chan->max_reg_power);
1103 } else
1104 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +01001105}
1106
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001107static void handle_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001108 enum nl80211_reg_initiator initiator,
1109 struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +01001110{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001111 unsigned int i;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001112
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001113 if (!sband)
1114 return;
Johannes Berg8318d782008-01-24 19:38:38 +01001115
1116 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001117 handle_channel(wiphy, initiator, &sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +01001118}
1119
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001120static bool reg_request_cell_base(struct regulatory_request *request)
1121{
1122 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1123 return false;
Johannes Berg1a919312012-12-03 17:21:11 +01001124 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001125}
1126
1127bool reg_last_request_cell_base(void)
1128{
Johannes Berg38fd2142013-05-10 19:17:17 +02001129 return reg_request_cell_base(get_last_request());
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001130}
1131
1132#ifdef CONFIG_CFG80211_CERTIFICATION_ONUS
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001133/* Core specific check */
Johannes Berg2f922122012-12-03 17:54:55 +01001134static enum reg_request_treatment
1135reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001136{
Johannes Bergc492db32012-12-06 16:29:25 +01001137 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001138
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001139 if (!reg_num_devs_support_basehint)
Johannes Berg2f922122012-12-03 17:54:55 +01001140 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001141
Johannes Bergc492db32012-12-06 16:29:25 +01001142 if (reg_request_cell_base(lr) &&
Johannes Berg1a919312012-12-03 17:21:11 +01001143 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001144 return REG_REQ_ALREADY_SET;
Johannes Berg1a919312012-12-03 17:21:11 +01001145
Johannes Berg2f922122012-12-03 17:54:55 +01001146 return REG_REQ_OK;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001147}
1148
1149/* Device specific check */
1150static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1151{
Johannes Berg1a919312012-12-03 17:21:11 +01001152 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001153}
1154#else
1155static int reg_ignore_cell_hint(struct regulatory_request *pending_request)
1156{
Johannes Berg2f922122012-12-03 17:54:55 +01001157 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001158}
Johannes Berg1a919312012-12-03 17:21:11 +01001159
1160static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001161{
1162 return true;
1163}
1164#endif
1165
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001166static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1167{
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001168 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1169 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001170 return true;
1171 return false;
1172}
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001173
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001174static bool ignore_reg_update(struct wiphy *wiphy,
1175 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001176{
Johannes Bergc492db32012-12-06 16:29:25 +01001177 struct regulatory_request *lr = get_last_request();
1178
1179 if (!lr) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001180 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1181 "since last_request is not set\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301182 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001183 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301184 }
1185
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001186 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001187 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001188 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1189 "since the driver uses its own custom "
1190 "regulatory domain\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301191 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001192 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301193 }
1194
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001195 /*
1196 * wiphy->regd will be set once the device has its own
1197 * desired regulatory domain set
1198 */
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001199 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -07001200 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Bergc492db32012-12-06 16:29:25 +01001201 !is_world_regdom(lr->alpha2)) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001202 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1203 "since the driver requires its own regulatory "
1204 "domain to be set first\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301205 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001206 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301207 }
1208
Johannes Bergc492db32012-12-06 16:29:25 +01001209 if (reg_request_cell_base(lr))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001210 return reg_dev_ignore_cell_hint(wiphy);
1211
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001212 return false;
1213}
1214
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001215static bool reg_is_world_roaming(struct wiphy *wiphy)
1216{
1217 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1218 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1219 struct regulatory_request *lr = get_last_request();
1220
1221 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1222 return true;
1223
1224 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001225 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001226 return true;
1227
1228 return false;
1229}
1230
Johannes Berg1a919312012-12-03 17:21:11 +01001231static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001232 struct reg_beacon *reg_beacon)
1233{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001234 struct ieee80211_supported_band *sband;
1235 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001236 bool channel_changed = false;
1237 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001238
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001239 sband = wiphy->bands[reg_beacon->chan.band];
1240 chan = &sband->channels[chan_idx];
1241
1242 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1243 return;
1244
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001245 if (chan->beacon_found)
1246 return;
1247
1248 chan->beacon_found = true;
1249
Luis R. Rodriguez0f500a52012-12-19 10:53:04 -08001250 if (!reg_is_world_roaming(wiphy))
1251 return;
1252
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001253 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001254 return;
1255
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001256 chan_before.center_freq = chan->center_freq;
1257 chan_before.flags = chan->flags;
1258
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001259 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1260 chan->flags &= ~IEEE80211_CHAN_NO_IR;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001261 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001262 }
1263
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001264 if (channel_changed)
1265 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001266}
1267
1268/*
1269 * Called when a scan on a wiphy finds a beacon on
1270 * new channel
1271 */
1272static void wiphy_update_new_beacon(struct wiphy *wiphy,
1273 struct reg_beacon *reg_beacon)
1274{
1275 unsigned int i;
1276 struct ieee80211_supported_band *sband;
1277
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001278 if (!wiphy->bands[reg_beacon->chan.band])
1279 return;
1280
1281 sband = wiphy->bands[reg_beacon->chan.band];
1282
1283 for (i = 0; i < sband->n_channels; i++)
1284 handle_reg_beacon(wiphy, i, reg_beacon);
1285}
1286
1287/*
1288 * Called upon reg changes or a new wiphy is added
1289 */
1290static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1291{
1292 unsigned int i;
1293 struct ieee80211_supported_band *sband;
1294 struct reg_beacon *reg_beacon;
1295
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001296 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1297 if (!wiphy->bands[reg_beacon->chan.band])
1298 continue;
1299 sband = wiphy->bands[reg_beacon->chan.band];
1300 for (i = 0; i < sband->n_channels; i++)
1301 handle_reg_beacon(wiphy, i, reg_beacon);
1302 }
1303}
1304
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001305/* Reap the advantages of previously found beacons */
1306static void reg_process_beacons(struct wiphy *wiphy)
1307{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001308 /*
1309 * Means we are just firing up cfg80211, so no beacons would
1310 * have been processed yet.
1311 */
1312 if (!last_request)
1313 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001314 wiphy_update_beacon_reg(wiphy);
1315}
1316
Johannes Berg1a919312012-12-03 17:21:11 +01001317static bool is_ht40_allowed(struct ieee80211_channel *chan)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001318{
1319 if (!chan)
Johannes Berg1a919312012-12-03 17:21:11 +01001320 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001321 if (chan->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg1a919312012-12-03 17:21:11 +01001322 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001323 /* This would happen when regulatory rules disallow HT40 completely */
Felix Fietkau55b183a2013-01-11 14:22:58 +01001324 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1325 return false;
1326 return true;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001327}
1328
1329static void reg_process_ht_flags_channel(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001330 struct ieee80211_channel *channel)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001331{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001332 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001333 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1334 unsigned int i;
1335
Johannes Berg1a919312012-12-03 17:21:11 +01001336 if (!is_ht40_allowed(channel)) {
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001337 channel->flags |= IEEE80211_CHAN_NO_HT40;
1338 return;
1339 }
1340
1341 /*
1342 * We need to ensure the extension channels exist to
1343 * be able to use HT40- or HT40+, this finds them (or not)
1344 */
1345 for (i = 0; i < sband->n_channels; i++) {
1346 struct ieee80211_channel *c = &sband->channels[i];
Johannes Berg1a919312012-12-03 17:21:11 +01001347
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001348 if (c->center_freq == (channel->center_freq - 20))
1349 channel_before = c;
1350 if (c->center_freq == (channel->center_freq + 20))
1351 channel_after = c;
1352 }
1353
1354 /*
1355 * Please note that this assumes target bandwidth is 20 MHz,
1356 * if that ever changes we also need to change the below logic
1357 * to include that as well.
1358 */
Johannes Berg1a919312012-12-03 17:21:11 +01001359 if (!is_ht40_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001360 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001361 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001362 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001363
Johannes Berg1a919312012-12-03 17:21:11 +01001364 if (!is_ht40_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001365 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001366 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001367 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001368}
1369
1370static void reg_process_ht_flags_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001371 struct ieee80211_supported_band *sband)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001372{
1373 unsigned int i;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001374
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001375 if (!sband)
1376 return;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001377
1378 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001379 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001380}
1381
1382static void reg_process_ht_flags(struct wiphy *wiphy)
1383{
1384 enum ieee80211_band band;
1385
1386 if (!wiphy)
1387 return;
1388
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001389 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1390 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001391}
1392
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001393static void reg_call_notifier(struct wiphy *wiphy,
1394 struct regulatory_request *request)
1395{
1396 if (wiphy->reg_notifier)
1397 wiphy->reg_notifier(wiphy, request);
1398}
1399
Sven Neumanneac03e32011-08-30 23:38:53 +02001400static void wiphy_update_regulatory(struct wiphy *wiphy,
1401 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001402{
1403 enum ieee80211_band band;
Johannes Bergc492db32012-12-06 16:29:25 +01001404 struct regulatory_request *lr = get_last_request();
Sven Neumanneac03e32011-08-30 23:38:53 +02001405
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001406 if (ignore_reg_update(wiphy, initiator)) {
1407 /*
1408 * Regulatory updates set by CORE are ignored for custom
1409 * regulatory cards. Let us notify the changes to the driver,
1410 * as some drivers used this to restore its orig_* reg domain.
1411 */
1412 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001413 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001414 reg_call_notifier(wiphy, lr);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001415 return;
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001416 }
Sven Neumanna203c2a2011-07-12 15:52:07 +02001417
Johannes Bergc492db32012-12-06 16:29:25 +01001418 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07001419
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001420 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1421 handle_band(wiphy, initiator, wiphy->bands[band]);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001422
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001423 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001424 reg_process_ht_flags(wiphy);
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001425 reg_call_notifier(wiphy, lr);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001426}
1427
Sven Neumannd7549cb2011-08-30 23:38:54 +02001428static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1429{
1430 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301431 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02001432
Johannes Berg5fe231e2013-05-08 21:45:15 +02001433 ASSERT_RTNL();
Johannes Berg458f4f92012-12-06 15:47:38 +01001434
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301435 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1436 wiphy = &rdev->wiphy;
1437 wiphy_update_regulatory(wiphy, initiator);
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301438 }
Sven Neumannd7549cb2011-08-30 23:38:54 +02001439}
1440
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001441static void handle_channel_custom(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001442 struct ieee80211_channel *chan,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001443 const struct ieee80211_regdomain *regd)
1444{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001445 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001446 const struct ieee80211_reg_rule *reg_rule = NULL;
1447 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001448 const struct ieee80211_freq_range *freq_range = NULL;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001449 u32 max_bandwidth_khz;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001450
Johannes Berg361c9c82012-12-06 15:57:14 +01001451 reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1452 regd);
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001453
Johannes Berg361c9c82012-12-06 15:57:14 +01001454 if (IS_ERR(reg_rule)) {
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001455 REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1456 chan->center_freq);
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001457 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1458 chan->flags = chan->orig_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001459 return;
1460 }
1461
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001462 chan_reg_rule_print_dbg(regd, chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301463
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001464 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001465 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001466
Janusz Dziedzic97524822014-01-30 09:52:20 +01001467 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1468 /* Check if auto calculation requested */
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001469 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001470 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1471
1472 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001473 bw_flags = IEEE80211_CHAN_NO_HT40;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001474 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001475 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001476 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001477 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001478
1479 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001480 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001481 chan->max_reg_power = chan->max_power =
1482 (int) MBM_TO_DBM(power_rule->max_eirp);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001483}
1484
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001485static void handle_band_custom(struct wiphy *wiphy,
1486 struct ieee80211_supported_band *sband,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001487 const struct ieee80211_regdomain *regd)
1488{
1489 unsigned int i;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001490
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001491 if (!sband)
1492 return;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001493
1494 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001495 handle_channel_custom(wiphy, &sband->channels[i], regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001496}
1497
1498/* Used by drivers prior to wiphy registration */
1499void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1500 const struct ieee80211_regdomain *regd)
1501{
1502 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001503 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001504
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001505 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
1506 "wiphy should have REGULATORY_CUSTOM_REG\n");
1507 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Luis R. Rodriguez222ea582013-11-05 09:18:00 -08001508
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001509 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001510 if (!wiphy->bands[band])
1511 continue;
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001512 handle_band_custom(wiphy, wiphy->bands[band], regd);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001513 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001514 }
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001515
1516 /*
1517 * no point in calling this if it won't have any effect
Johannes Berg1a919312012-12-03 17:21:11 +01001518 * on your device's supported bands.
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001519 */
1520 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001521}
1522EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1523
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001524static void reg_set_request_processed(void)
1525{
1526 bool need_more_processing = false;
Johannes Bergc492db32012-12-06 16:29:25 +01001527 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001528
Johannes Bergc492db32012-12-06 16:29:25 +01001529 lr->processed = true;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001530
1531 spin_lock(&reg_requests_lock);
1532 if (!list_empty(&reg_requests_list))
1533 need_more_processing = true;
1534 spin_unlock(&reg_requests_lock);
1535
Johannes Bergc492db32012-12-06 16:29:25 +01001536 if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
Eliad Pellerfe20b392012-06-12 12:53:13 +03001537 cancel_delayed_work(&reg_timeout);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001538
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001539 if (need_more_processing)
1540 schedule_work(&reg_work);
1541}
1542
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001543/**
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001544 * reg_process_hint_core - process core regulatory requests
1545 * @pending_request: a pending core regulatory request
1546 *
1547 * The wireless subsystem can use this function to process
1548 * a regulatory request issued by the regulatory core.
1549 *
1550 * Returns one of the different reg request treatment values.
1551 */
1552static enum reg_request_treatment
1553reg_process_hint_core(struct regulatory_request *core_request)
1554{
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001555
1556 core_request->intersect = false;
1557 core_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001558
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001559 reg_update_last_request(core_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001560
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001561 return reg_call_crda(core_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001562}
1563
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001564static enum reg_request_treatment
1565__reg_process_hint_user(struct regulatory_request *user_request)
1566{
1567 struct regulatory_request *lr = get_last_request();
1568
1569 if (reg_request_cell_base(user_request))
1570 return reg_ignore_cell_hint(user_request);
1571
1572 if (reg_request_cell_base(lr))
1573 return REG_REQ_IGNORE;
1574
1575 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1576 return REG_REQ_INTERSECT;
1577 /*
1578 * If the user knows better the user should set the regdom
1579 * to their country before the IE is picked up
1580 */
1581 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
1582 lr->intersect)
1583 return REG_REQ_IGNORE;
1584 /*
1585 * Process user requests only after previous user/driver/core
1586 * requests have been processed
1587 */
1588 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
1589 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1590 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
1591 regdom_changes(lr->alpha2))
1592 return REG_REQ_IGNORE;
1593
1594 if (!regdom_changes(user_request->alpha2))
1595 return REG_REQ_ALREADY_SET;
1596
1597 return REG_REQ_OK;
1598}
1599
1600/**
1601 * reg_process_hint_user - process user regulatory requests
1602 * @user_request: a pending user regulatory request
1603 *
1604 * The wireless subsystem can use this function to process
1605 * a regulatory request initiated by userspace.
1606 *
1607 * Returns one of the different reg request treatment values.
1608 */
1609static enum reg_request_treatment
1610reg_process_hint_user(struct regulatory_request *user_request)
1611{
1612 enum reg_request_treatment treatment;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001613
1614 treatment = __reg_process_hint_user(user_request);
1615 if (treatment == REG_REQ_IGNORE ||
1616 treatment == REG_REQ_ALREADY_SET) {
1617 kfree(user_request);
1618 return treatment;
1619 }
1620
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001621 user_request->intersect = treatment == REG_REQ_INTERSECT;
1622 user_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001623
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001624 reg_update_last_request(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001625
1626 user_alpha2[0] = user_request->alpha2[0];
1627 user_alpha2[1] = user_request->alpha2[1];
1628
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001629 return reg_call_crda(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001630}
1631
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001632static enum reg_request_treatment
1633__reg_process_hint_driver(struct regulatory_request *driver_request)
1634{
1635 struct regulatory_request *lr = get_last_request();
1636
1637 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
1638 if (regdom_changes(driver_request->alpha2))
1639 return REG_REQ_OK;
1640 return REG_REQ_ALREADY_SET;
1641 }
1642
1643 /*
1644 * This would happen if you unplug and plug your card
1645 * back in or if you add a new device for which the previously
1646 * loaded card also agrees on the regulatory domain.
1647 */
1648 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1649 !regdom_changes(driver_request->alpha2))
1650 return REG_REQ_ALREADY_SET;
1651
1652 return REG_REQ_INTERSECT;
1653}
1654
1655/**
1656 * reg_process_hint_driver - process driver regulatory requests
1657 * @driver_request: a pending driver regulatory request
1658 *
1659 * The wireless subsystem can use this function to process
1660 * a regulatory request issued by an 802.11 driver.
1661 *
1662 * Returns one of the different reg request treatment values.
1663 */
1664static enum reg_request_treatment
1665reg_process_hint_driver(struct wiphy *wiphy,
1666 struct regulatory_request *driver_request)
1667{
1668 const struct ieee80211_regdomain *regd;
1669 enum reg_request_treatment treatment;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001670
1671 treatment = __reg_process_hint_driver(driver_request);
1672
1673 switch (treatment) {
1674 case REG_REQ_OK:
1675 break;
1676 case REG_REQ_IGNORE:
1677 kfree(driver_request);
1678 return treatment;
1679 case REG_REQ_INTERSECT:
1680 /* fall through */
1681 case REG_REQ_ALREADY_SET:
1682 regd = reg_copy_regd(get_cfg80211_regdom());
1683 if (IS_ERR(regd)) {
1684 kfree(driver_request);
1685 return REG_REQ_IGNORE;
1686 }
1687 rcu_assign_pointer(wiphy->regd, regd);
1688 }
1689
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001690
1691 driver_request->intersect = treatment == REG_REQ_INTERSECT;
1692 driver_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001693
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001694 reg_update_last_request(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001695
1696 /*
1697 * Since CRDA will not be called in this case as we already
1698 * have applied the requested regulatory domain before we just
1699 * inform userspace we have processed the request
1700 */
1701 if (treatment == REG_REQ_ALREADY_SET) {
1702 nl80211_send_reg_change_event(driver_request);
1703 reg_set_request_processed();
1704 return treatment;
1705 }
1706
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001707 return reg_call_crda(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001708}
1709
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001710static enum reg_request_treatment
1711__reg_process_hint_country_ie(struct wiphy *wiphy,
1712 struct regulatory_request *country_ie_request)
1713{
1714 struct wiphy *last_wiphy = NULL;
1715 struct regulatory_request *lr = get_last_request();
1716
1717 if (reg_request_cell_base(lr)) {
1718 /* Trust a Cell base station over the AP's country IE */
1719 if (regdom_changes(country_ie_request->alpha2))
1720 return REG_REQ_IGNORE;
1721 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez2a901462013-11-11 22:15:31 +01001722 } else {
1723 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
1724 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001725 }
1726
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001727 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
1728 return -EINVAL;
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001729
1730 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
1731 return REG_REQ_OK;
1732
1733 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
1734
1735 if (last_wiphy != wiphy) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001736 /*
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001737 * Two cards with two APs claiming different
1738 * Country IE alpha2s. We could
1739 * intersect them, but that seems unlikely
1740 * to be correct. Reject second one for now.
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001741 */
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001742 if (regdom_changes(country_ie_request->alpha2))
1743 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001744 return REG_REQ_ALREADY_SET;
1745 }
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001746 /*
1747 * Two consecutive Country IE hints on the same wiphy.
1748 * This should be picked up early by the driver/stack
1749 */
1750 if (WARN_ON(regdom_changes(country_ie_request->alpha2)))
1751 return REG_REQ_OK;
1752 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001753}
1754
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001755/**
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001756 * reg_process_hint_country_ie - process regulatory requests from country IEs
1757 * @country_ie_request: a regulatory request from a country IE
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001758 *
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001759 * The wireless subsystem can use this function to process
1760 * a regulatory request issued by a country Information Element.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001761 *
Johannes Berg2f922122012-12-03 17:54:55 +01001762 * Returns one of the different reg request treatment values.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001763 */
Johannes Berg2f922122012-12-03 17:54:55 +01001764static enum reg_request_treatment
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001765reg_process_hint_country_ie(struct wiphy *wiphy,
1766 struct regulatory_request *country_ie_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001767{
Johannes Berg2f922122012-12-03 17:54:55 +01001768 enum reg_request_treatment treatment;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001769
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001770 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001771
Johannes Berg2f922122012-12-03 17:54:55 +01001772 switch (treatment) {
Johannes Berg2f922122012-12-03 17:54:55 +01001773 case REG_REQ_OK:
1774 break;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001775 case REG_REQ_IGNORE:
1776 /* fall through */
1777 case REG_REQ_ALREADY_SET:
1778 kfree(country_ie_request);
Johannes Berg2f922122012-12-03 17:54:55 +01001779 return treatment;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001780 case REG_REQ_INTERSECT:
1781 kfree(country_ie_request);
1782 /*
1783 * This doesn't happen yet, not sure we
1784 * ever want to support it for this case.
1785 */
1786 WARN_ONCE(1, "Unexpected intersection for country IEs");
1787 return REG_REQ_IGNORE;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001788 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001789
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001790 country_ie_request->intersect = false;
1791 country_ie_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001792
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001793 reg_update_last_request(country_ie_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001794
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001795 return reg_call_crda(country_ie_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001796}
1797
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001798/* This processes *all* regulatory hints */
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08001799static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001800{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001801 struct wiphy *wiphy = NULL;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001802 enum reg_request_treatment treatment;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001803
Johannes Bergf4173762012-12-03 18:23:37 +01001804 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001805 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1806
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001807 switch (reg_request->initiator) {
1808 case NL80211_REGDOM_SET_BY_CORE:
1809 reg_process_hint_core(reg_request);
1810 return;
1811 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001812 treatment = reg_process_hint_user(reg_request);
1813 if (treatment == REG_REQ_OK ||
1814 treatment == REG_REQ_ALREADY_SET)
1815 return;
Shaibal Dutta845f3352014-01-30 15:08:30 -08001816 queue_delayed_work(system_power_efficient_wq,
1817 &reg_timeout, msecs_to_jiffies(3142));
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001818 return;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001819 case NL80211_REGDOM_SET_BY_DRIVER:
Ilan Peer772f0382014-01-14 15:17:23 +02001820 if (!wiphy)
1821 goto out_free;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001822 treatment = reg_process_hint_driver(wiphy, reg_request);
1823 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001824 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Ilan Peer772f0382014-01-14 15:17:23 +02001825 if (!wiphy)
1826 goto out_free;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001827 treatment = reg_process_hint_country_ie(wiphy, reg_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001828 break;
1829 default:
1830 WARN(1, "invalid initiator %d\n", reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02001831 goto out_free;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001832 }
1833
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001834 /* This is required so that the orig_* parameters are saved */
1835 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001836 wiphy->regulatory_flags & REGULATORY_STRICT_REG)
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001837 wiphy_update_regulatory(wiphy, reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02001838
1839 return;
1840
1841out_free:
1842 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001843}
1844
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001845/*
1846 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1847 * Regulatory hints come on a first come first serve basis and we
1848 * must process each one atomically.
1849 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001850static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001851{
Johannes Bergc492db32012-12-06 16:29:25 +01001852 struct regulatory_request *reg_request, *lr;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001853
Johannes Bergc492db32012-12-06 16:29:25 +01001854 lr = get_last_request();
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001855
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001856 /* When last_request->processed becomes true this will be rescheduled */
Johannes Bergc492db32012-12-06 16:29:25 +01001857 if (lr && !lr->processed) {
Johannes Berg1a919312012-12-03 17:21:11 +01001858 REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n");
Johannes Berg5fe231e2013-05-08 21:45:15 +02001859 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001860 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001861
1862 spin_lock(&reg_requests_lock);
1863
1864 if (list_empty(&reg_requests_list)) {
1865 spin_unlock(&reg_requests_lock);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001866 return;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001867 }
1868
1869 reg_request = list_first_entry(&reg_requests_list,
1870 struct regulatory_request,
1871 list);
1872 list_del_init(&reg_request->list);
1873
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001874 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001875
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08001876 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001877}
1878
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001879/* Processes beacon hints -- this has nothing to do with country IEs */
1880static void reg_process_pending_beacon_hints(void)
1881{
Johannes Berg79c97e92009-07-07 03:56:12 +02001882 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001883 struct reg_beacon *pending_beacon, *tmp;
1884
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001885 /* This goes through the _pending_ beacon list */
1886 spin_lock_bh(&reg_pending_beacons_lock);
1887
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001888 list_for_each_entry_safe(pending_beacon, tmp,
1889 &reg_pending_beacons, list) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001890 list_del_init(&pending_beacon->list);
1891
1892 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001893 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1894 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001895
1896 /* Remembers the beacon hint for new wiphys or reg changes */
1897 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1898 }
1899
1900 spin_unlock_bh(&reg_pending_beacons_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001901}
1902
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001903static void reg_todo(struct work_struct *work)
1904{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001905 rtnl_lock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001906 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001907 reg_process_pending_beacon_hints();
Johannes Berg5fe231e2013-05-08 21:45:15 +02001908 rtnl_unlock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001909}
1910
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001911static void queue_regulatory_request(struct regulatory_request *request)
1912{
Johannes Bergd4f2c882012-12-03 18:59:58 +01001913 request->alpha2[0] = toupper(request->alpha2[0]);
1914 request->alpha2[1] = toupper(request->alpha2[1]);
John W. Linvillec61029c2010-08-05 14:26:24 -04001915
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001916 spin_lock(&reg_requests_lock);
1917 list_add_tail(&request->list, &reg_requests_list);
1918 spin_unlock(&reg_requests_lock);
1919
1920 schedule_work(&reg_work);
1921}
1922
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001923/*
1924 * Core regulatory hint -- happens during cfg80211_init()
1925 * and when we restore regulatory settings.
1926 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001927static int regulatory_hint_core(const char *alpha2)
1928{
1929 struct regulatory_request *request;
1930
Johannes Berg1a919312012-12-03 17:21:11 +01001931 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001932 if (!request)
1933 return -ENOMEM;
1934
1935 request->alpha2[0] = alpha2[0];
1936 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001937 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001938
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08001939 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001940
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001941 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001942}
1943
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001944/* User hints */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001945int regulatory_hint_user(const char *alpha2,
1946 enum nl80211_user_reg_hint_type user_reg_hint_type)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001947{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001948 struct regulatory_request *request;
1949
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001950 if (WARN_ON(!alpha2))
1951 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001952
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001953 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1954 if (!request)
1955 return -ENOMEM;
1956
Johannes Bergf4173762012-12-03 18:23:37 +01001957 request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001958 request->alpha2[0] = alpha2[0];
1959 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001960 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001961 request->user_reg_hint_type = user_reg_hint_type;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001962
1963 queue_regulatory_request(request);
1964
1965 return 0;
1966}
1967
1968/* Driver hints */
1969int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1970{
1971 struct regulatory_request *request;
1972
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001973 if (WARN_ON(!alpha2 || !wiphy))
1974 return -EINVAL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001975
Luis R. Rodriguez4f7b9142013-12-14 20:09:06 +01001976 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
1977
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001978 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1979 if (!request)
1980 return -ENOMEM;
1981
1982 request->wiphy_idx = get_wiphy_idx(wiphy);
1983
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001984 request->alpha2[0] = alpha2[0];
1985 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001986 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001987
1988 queue_regulatory_request(request);
1989
1990 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001991}
1992EXPORT_SYMBOL(regulatory_hint);
1993
Luis R. Rodriguez789fd032013-10-04 18:07:24 -07001994void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
1995 const u8 *country_ie, u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001996{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001997 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001998 enum environment_cap env = ENVIRON_ANY;
Johannes Bergdb2424c2013-05-10 19:07:52 +02001999 struct regulatory_request *request = NULL, *lr;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002000
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002001 /* IE len must be evenly divisible by 2 */
2002 if (country_ie_len & 0x01)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002003 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002004
2005 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002006 return;
2007
2008 request = kzalloc(sizeof(*request), GFP_KERNEL);
2009 if (!request)
2010 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002011
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002012 alpha2[0] = country_ie[0];
2013 alpha2[1] = country_ie[1];
2014
2015 if (country_ie[2] == 'I')
2016 env = ENVIRON_INDOOR;
2017 else if (country_ie[2] == 'O')
2018 env = ENVIRON_OUTDOOR;
2019
Johannes Bergdb2424c2013-05-10 19:07:52 +02002020 rcu_read_lock();
2021 lr = get_last_request();
2022
2023 if (unlikely(!lr))
2024 goto out;
2025
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002026 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002027 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002028 * We leave conflict resolution to the workqueue, where can hold
Johannes Berg5fe231e2013-05-08 21:45:15 +02002029 * the RTNL.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002030 */
Johannes Bergc492db32012-12-06 16:29:25 +01002031 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2032 lr->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002033 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002034
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002035 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04002036 request->alpha2[0] = alpha2[0];
2037 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002038 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002039 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002040
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002041 queue_regulatory_request(request);
Johannes Bergdb2424c2013-05-10 19:07:52 +02002042 request = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002043out:
Johannes Bergdb2424c2013-05-10 19:07:52 +02002044 kfree(request);
2045 rcu_read_unlock();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002046}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002047
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002048static void restore_alpha2(char *alpha2, bool reset_user)
2049{
2050 /* indicates there is no alpha2 to consider for restoration */
2051 alpha2[0] = '9';
2052 alpha2[1] = '7';
2053
2054 /* The user setting has precedence over the module parameter */
2055 if (is_user_regdom_saved()) {
2056 /* Unless we're asked to ignore it and reset it */
2057 if (reset_user) {
Johannes Berg1a919312012-12-03 17:21:11 +01002058 REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002059 user_alpha2[0] = '9';
2060 user_alpha2[1] = '7';
2061
2062 /*
2063 * If we're ignoring user settings, we still need to
2064 * check the module parameter to ensure we put things
2065 * back as they were for a full restore.
2066 */
2067 if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01002068 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2069 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002070 alpha2[0] = ieee80211_regdom[0];
2071 alpha2[1] = ieee80211_regdom[1];
2072 }
2073 } else {
Johannes Berg1a919312012-12-03 17:21:11 +01002074 REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
2075 user_alpha2[0], user_alpha2[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002076 alpha2[0] = user_alpha2[0];
2077 alpha2[1] = user_alpha2[1];
2078 }
2079 } else if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01002080 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2081 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002082 alpha2[0] = ieee80211_regdom[0];
2083 alpha2[1] = ieee80211_regdom[1];
2084 } else
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07002085 REG_DBG_PRINT("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002086}
2087
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302088static void restore_custom_reg_settings(struct wiphy *wiphy)
2089{
2090 struct ieee80211_supported_band *sband;
2091 enum ieee80211_band band;
2092 struct ieee80211_channel *chan;
2093 int i;
2094
2095 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2096 sband = wiphy->bands[band];
2097 if (!sband)
2098 continue;
2099 for (i = 0; i < sband->n_channels; i++) {
2100 chan = &sband->channels[i];
2101 chan->flags = chan->orig_flags;
2102 chan->max_antenna_gain = chan->orig_mag;
2103 chan->max_power = chan->orig_mpwr;
Paul Stewart899852a2012-08-01 16:54:42 -07002104 chan->beacon_found = false;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302105 }
2106 }
2107}
2108
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002109/*
2110 * Restoring regulatory settings involves ingoring any
2111 * possibly stale country IE information and user regulatory
2112 * settings if so desired, this includes any beacon hints
2113 * learned as we could have traveled outside to another country
2114 * after disconnection. To restore regulatory settings we do
2115 * exactly what we did at bootup:
2116 *
2117 * - send a core regulatory hint
2118 * - send a user regulatory hint if applicable
2119 *
2120 * Device drivers that send a regulatory hint for a specific country
2121 * keep their own regulatory domain on wiphy->regd so that does does
2122 * not need to be remembered.
2123 */
2124static void restore_regulatory_settings(bool reset_user)
2125{
2126 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002127 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002128 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002129 struct regulatory_request *reg_request, *tmp;
2130 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302131 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002132
Johannes Berg5fe231e2013-05-08 21:45:15 +02002133 ASSERT_RTNL();
2134
Johannes Berg2d319862013-01-09 12:01:38 +01002135 reset_regdomains(true, &world_regdom);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002136 restore_alpha2(alpha2, reset_user);
2137
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002138 /*
2139 * If there's any pending requests we simply
2140 * stash them to a temporary pending queue and
2141 * add then after we've restored regulatory
2142 * settings.
2143 */
2144 spin_lock(&reg_requests_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002145 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2146 if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
2147 continue;
2148 list_move_tail(&reg_request->list, &tmp_reg_req_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002149 }
2150 spin_unlock(&reg_requests_lock);
2151
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002152 /* Clear beacon hints */
2153 spin_lock_bh(&reg_pending_beacons_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002154 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2155 list_del(&reg_beacon->list);
2156 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002157 }
2158 spin_unlock_bh(&reg_pending_beacons_lock);
2159
Johannes Bergfea9bce2012-12-03 17:32:01 +01002160 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2161 list_del(&reg_beacon->list);
2162 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002163 }
2164
2165 /* First restore to the basic regulatory settings */
Johannes Berg379b82f2012-12-06 15:44:07 +01002166 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
2167 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002168
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302169 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01002170 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302171 restore_custom_reg_settings(&rdev->wiphy);
2172 }
2173
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002174 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002175
2176 /*
2177 * This restores the ieee80211_regdom module parameter
2178 * preference or the last user requested regulatory
2179 * settings, user regulatory settings takes precedence.
2180 */
2181 if (is_an_alpha2(alpha2))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002182 regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002183
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002184 spin_lock(&reg_requests_lock);
Johannes Berg11cff962012-12-03 18:56:41 +01002185 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002186 spin_unlock(&reg_requests_lock);
2187
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002188 REG_DBG_PRINT("Kicking the queue\n");
2189
2190 schedule_work(&reg_work);
2191}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002192
2193void regulatory_hint_disconnect(void)
2194{
Johannes Berg1a919312012-12-03 17:21:11 +01002195 REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002196 restore_regulatory_settings(false);
2197}
2198
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002199static bool freq_is_chan_12_13_14(u16 freq)
2200{
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002201 if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
2202 freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
2203 freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002204 return true;
2205 return false;
2206}
2207
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002208static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
2209{
2210 struct reg_beacon *pending_beacon;
2211
2212 list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
2213 if (beacon_chan->center_freq ==
2214 pending_beacon->chan.center_freq)
2215 return true;
2216 return false;
2217}
2218
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002219int regulatory_hint_found_beacon(struct wiphy *wiphy,
2220 struct ieee80211_channel *beacon_chan,
2221 gfp_t gfp)
2222{
2223 struct reg_beacon *reg_beacon;
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002224 bool processing;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002225
Johannes Berg1a919312012-12-03 17:21:11 +01002226 if (beacon_chan->beacon_found ||
2227 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002228 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg1a919312012-12-03 17:21:11 +01002229 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002230 return 0;
2231
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002232 spin_lock_bh(&reg_pending_beacons_lock);
2233 processing = pending_reg_beacon(beacon_chan);
2234 spin_unlock_bh(&reg_pending_beacons_lock);
2235
2236 if (processing)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002237 return 0;
2238
2239 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2240 if (!reg_beacon)
2241 return -ENOMEM;
2242
Johannes Berg1a919312012-12-03 17:21:11 +01002243 REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002244 beacon_chan->center_freq,
2245 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2246 wiphy_name(wiphy));
2247
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002248 memcpy(&reg_beacon->chan, beacon_chan,
Johannes Berg1a919312012-12-03 17:21:11 +01002249 sizeof(struct ieee80211_channel));
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002250
2251 /*
2252 * Since we can be called from BH or and non-BH context
2253 * we must use spin_lock_bh()
2254 */
2255 spin_lock_bh(&reg_pending_beacons_lock);
2256 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2257 spin_unlock_bh(&reg_pending_beacons_lock);
2258
2259 schedule_work(&reg_work);
2260
2261 return 0;
2262}
2263
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002264static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002265{
2266 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002267 const struct ieee80211_reg_rule *reg_rule = NULL;
2268 const struct ieee80211_freq_range *freq_range = NULL;
2269 const struct ieee80211_power_rule *power_rule = NULL;
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002270 char bw[32], cac_time[32];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002271
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002272 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002273
2274 for (i = 0; i < rd->n_reg_rules; i++) {
2275 reg_rule = &rd->reg_rules[i];
2276 freq_range = &reg_rule->freq_range;
2277 power_rule = &reg_rule->power_rule;
2278
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002279 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
2280 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
2281 freq_range->max_bandwidth_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002282 reg_get_max_bandwidth(rd, reg_rule));
2283 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002284 snprintf(bw, sizeof(bw), "%d KHz",
Janusz Dziedzic97524822014-01-30 09:52:20 +01002285 freq_range->max_bandwidth_khz);
2286
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002287 if (reg_rule->flags & NL80211_RRF_DFS)
2288 scnprintf(cac_time, sizeof(cac_time), "%u s",
2289 reg_rule->dfs_cac_ms/1000);
2290 else
2291 scnprintf(cac_time, sizeof(cac_time), "N/A");
2292
2293
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002294 /*
2295 * There may not be documentation for max antenna gain
2296 * in certain regions
2297 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002298 if (power_rule->max_antenna_gain)
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002299 pr_info(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002300 freq_range->start_freq_khz,
2301 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002302 bw,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002303 power_rule->max_antenna_gain,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002304 power_rule->max_eirp,
2305 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002306 else
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002307 pr_info(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002308 freq_range->start_freq_khz,
2309 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002310 bw,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002311 power_rule->max_eirp,
2312 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002313 }
2314}
2315
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01002316bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002317{
2318 switch (dfs_region) {
2319 case NL80211_DFS_UNSET:
2320 case NL80211_DFS_FCC:
2321 case NL80211_DFS_ETSI:
2322 case NL80211_DFS_JP:
2323 return true;
2324 default:
2325 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
2326 dfs_region);
2327 return false;
2328 }
2329}
2330
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002331static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002332{
Johannes Bergc492db32012-12-06 16:29:25 +01002333 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002334
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002335 if (is_intersected_alpha2(rd->alpha2)) {
Johannes Bergc492db32012-12-06 16:29:25 +01002336 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002337 struct cfg80211_registered_device *rdev;
Johannes Bergc492db32012-12-06 16:29:25 +01002338 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002339 if (rdev) {
Joe Perchese9c02682010-11-16 19:56:49 -08002340 pr_info("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002341 rdev->country_ie_alpha2[0],
2342 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002343 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002344 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002345 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002346 pr_info("Current regulatory domain intersected:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002347 } else if (is_world_regdom(rd->alpha2)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002348 pr_info("World regulatory domain updated:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002349 } else {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002350 if (is_unknown_alpha2(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08002351 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002352 else {
Johannes Bergc492db32012-12-06 16:29:25 +01002353 if (reg_request_cell_base(lr))
Johannes Berg1a919312012-12-03 17:21:11 +01002354 pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002355 rd->alpha2[0], rd->alpha2[1]);
2356 else
Johannes Berg1a919312012-12-03 17:21:11 +01002357 pr_info("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002358 rd->alpha2[0], rd->alpha2[1]);
2359 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002360 }
Johannes Berg1a919312012-12-03 17:21:11 +01002361
Luis R. Rodriguez3ef121b2013-11-13 18:54:05 +01002362 pr_info(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002363 print_rd_rules(rd);
2364}
2365
Johannes Berg2df78162008-10-28 16:49:41 +01002366static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002367{
Joe Perchese9c02682010-11-16 19:56:49 -08002368 pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002369 print_rd_rules(rd);
2370}
2371
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002372static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
2373{
2374 if (!is_world_regdom(rd->alpha2))
2375 return -EINVAL;
2376 update_world_regdomain(rd);
2377 return 0;
2378}
2379
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002380static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
2381 struct regulatory_request *user_request)
2382{
2383 const struct ieee80211_regdomain *intersected_rd = NULL;
2384
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002385 if (!regdom_changes(rd->alpha2))
2386 return -EALREADY;
2387
2388 if (!is_valid_rd(rd)) {
2389 pr_err("Invalid regulatory domain detected:\n");
2390 print_regdomain_info(rd);
2391 return -EINVAL;
2392 }
2393
2394 if (!user_request->intersect) {
2395 reset_regdomains(false, rd);
2396 return 0;
2397 }
2398
2399 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2400 if (!intersected_rd)
2401 return -EINVAL;
2402
2403 kfree(rd);
2404 rd = NULL;
2405 reset_regdomains(false, intersected_rd);
2406
2407 return 0;
2408}
2409
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002410static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
2411 struct regulatory_request *driver_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002412{
Johannes Berge9763c32012-12-03 16:59:46 +01002413 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002414 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002415 const struct ieee80211_regdomain *tmp;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002416 struct wiphy *request_wiphy;
Johannes Berg6913b492012-12-04 00:48:59 +01002417
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002418 if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002419 return -EINVAL;
2420
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002421 if (!regdom_changes(rd->alpha2))
2422 return -EALREADY;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002423
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002424 if (!is_valid_rd(rd)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002425 pr_err("Invalid regulatory domain detected:\n");
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002426 print_regdomain_info(rd);
2427 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002428 }
2429
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002430 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
2431 if (!request_wiphy) {
Shaibal Dutta845f3352014-01-30 15:08:30 -08002432 queue_delayed_work(system_power_efficient_wq,
2433 &reg_timeout, 0);
Johannes Bergde3584b2011-11-21 10:44:00 +01002434 return -ENODEV;
2435 }
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002436
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002437 if (!driver_request->intersect) {
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002438 if (request_wiphy->regd)
2439 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002440
Johannes Berge9763c32012-12-03 16:59:46 +01002441 regd = reg_copy_regd(rd);
2442 if (IS_ERR(regd))
2443 return PTR_ERR(regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002444
Johannes Berg458f4f92012-12-06 15:47:38 +01002445 rcu_assign_pointer(request_wiphy->regd, regd);
Johannes Berg379b82f2012-12-06 15:44:07 +01002446 reset_regdomains(false, rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002447 return 0;
2448 }
2449
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002450 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2451 if (!intersected_rd)
2452 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002453
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002454 /*
2455 * We can trash what CRDA provided now.
2456 * However if a driver requested this specific regulatory
2457 * domain we keep it for its private use
2458 */
2459 tmp = get_wiphy_regdom(request_wiphy);
2460 rcu_assign_pointer(request_wiphy->regd, rd);
2461 rcu_free_regdom(tmp);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002462
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002463 rd = NULL;
Larry Fingerb7566fc2013-02-04 15:33:44 -06002464
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002465 reset_regdomains(false, intersected_rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002466
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002467 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002468}
2469
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002470static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
2471 struct regulatory_request *country_ie_request)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002472{
2473 struct wiphy *request_wiphy;
2474
2475 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2476 !is_unknown_alpha2(rd->alpha2))
2477 return -EINVAL;
2478
2479 /*
2480 * Lets only bother proceeding on the same alpha2 if the current
2481 * rd is non static (it means CRDA was present and was used last)
2482 * and the pending request came in from a country IE
2483 */
2484
2485 if (!is_valid_rd(rd)) {
2486 pr_err("Invalid regulatory domain detected:\n");
2487 print_regdomain_info(rd);
2488 return -EINVAL;
2489 }
2490
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002491 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002492 if (!request_wiphy) {
Shaibal Dutta845f3352014-01-30 15:08:30 -08002493 queue_delayed_work(system_power_efficient_wq,
2494 &reg_timeout, 0);
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002495 return -ENODEV;
2496 }
2497
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002498 if (country_ie_request->intersect)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002499 return -EINVAL;
2500
2501 reset_regdomains(false, rd);
2502 return 0;
2503}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002504
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002505/*
2506 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002507 * multiple drivers can be ironed out later. Caller must've already
Johannes Berg458f4f92012-12-06 15:47:38 +01002508 * kmalloc'd the rd structure.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002509 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002510int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002511{
Johannes Bergc492db32012-12-06 16:29:25 +01002512 struct regulatory_request *lr;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002513 int r;
2514
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002515 if (!reg_is_valid_request(rd->alpha2)) {
2516 kfree(rd);
2517 return -EINVAL;
2518 }
2519
Johannes Bergc492db32012-12-06 16:29:25 +01002520 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002521
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002522 /* Note that this doesn't update the wiphys, this is done below */
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002523 switch (lr->initiator) {
2524 case NL80211_REGDOM_SET_BY_CORE:
2525 r = reg_set_rd_core(rd);
2526 break;
2527 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002528 r = reg_set_rd_user(rd, lr);
2529 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002530 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002531 r = reg_set_rd_driver(rd, lr);
2532 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002533 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002534 r = reg_set_rd_country_ie(rd, lr);
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002535 break;
2536 default:
2537 WARN(1, "invalid initiator %d\n", lr->initiator);
2538 return -EINVAL;
2539 }
2540
Johannes Bergd2372b32008-10-24 20:32:20 +02002541 if (r) {
Kalle Valo95908532012-07-12 15:33:58 +03002542 if (r == -EALREADY)
2543 reg_set_request_processed();
2544
Johannes Bergd2372b32008-10-24 20:32:20 +02002545 kfree(rd);
Johannes Berg38fd2142013-05-10 19:17:17 +02002546 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002547 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002548
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002549 /* This would make this whole thing pointless */
Johannes Berg38fd2142013-05-10 19:17:17 +02002550 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
2551 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002552
2553 /* update all wiphys now with the new established regulatory domain */
Johannes Bergc492db32012-12-06 16:29:25 +01002554 update_all_wiphy_regulatory(lr->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002555
Johannes Berg458f4f92012-12-06 15:47:38 +01002556 print_regdomain(get_cfg80211_regdom());
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002557
Johannes Bergc492db32012-12-06 16:29:25 +01002558 nl80211_send_reg_change_event(lr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002559
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002560 reg_set_request_processed();
2561
Johannes Berg38fd2142013-05-10 19:17:17 +02002562 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002563}
2564
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002565void wiphy_regulatory_register(struct wiphy *wiphy)
2566{
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002567 struct regulatory_request *lr;
2568
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002569 if (!reg_dev_ignore_cell_hint(wiphy))
2570 reg_num_devs_support_basehint++;
2571
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002572 lr = get_last_request();
2573 wiphy_update_regulatory(wiphy, lr->initiator);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002574}
2575
Luis R. Rodriguezbfead082012-07-12 11:49:19 -07002576void wiphy_regulatory_deregister(struct wiphy *wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002577{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002578 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01002579 struct regulatory_request *lr;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002580
Johannes Bergc492db32012-12-06 16:29:25 +01002581 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002582
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002583 if (!reg_dev_ignore_cell_hint(wiphy))
2584 reg_num_devs_support_basehint--;
2585
Johannes Berg458f4f92012-12-06 15:47:38 +01002586 rcu_free_regdom(get_wiphy_regdom(wiphy));
2587 rcu_assign_pointer(wiphy->regd, NULL);
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002588
Johannes Bergc492db32012-12-06 16:29:25 +01002589 if (lr)
2590 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002591
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002592 if (!request_wiphy || request_wiphy != wiphy)
Johannes Berg38fd2142013-05-10 19:17:17 +02002593 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002594
Johannes Bergc492db32012-12-06 16:29:25 +01002595 lr->wiphy_idx = WIPHY_IDX_INVALID;
2596 lr->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002597}
2598
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002599static void reg_timeout_work(struct work_struct *work)
2600{
Johannes Berg1a919312012-12-03 17:21:11 +01002601 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
Johannes Bergf77b86d2013-06-24 15:43:38 +02002602 rtnl_lock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002603 restore_regulatory_settings(true);
Johannes Bergf77b86d2013-06-24 15:43:38 +02002604 rtnl_unlock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002605}
2606
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002607int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002608{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002609 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002610
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002611 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2612 if (IS_ERR(reg_pdev))
2613 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002614
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002615 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002616 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002617
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -07002618 reg_regdb_size_check();
2619
Johannes Berg458f4f92012-12-06 15:47:38 +01002620 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002621
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002622 user_alpha2[0] = '9';
2623 user_alpha2[1] = '7';
2624
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002625 /* We always try to get an update for the static regdomain */
Johannes Berg458f4f92012-12-06 15:47:38 +01002626 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002627 if (err) {
2628 if (err == -ENOMEM)
2629 return err;
2630 /*
2631 * N.B. kobject_uevent_env() can fail mainly for when we're out
2632 * memory which is handled and propagated appropriately above
2633 * but it can also fail during a netlink_broadcast() or during
2634 * early boot for call_usermodehelper(). For now treat these
2635 * errors as non-fatal.
2636 */
Joe Perchese9c02682010-11-16 19:56:49 -08002637 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002638 }
Johannes Berg734366d2008-09-15 10:56:48 +02002639
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002640 /*
2641 * Finally, if the user set the module parameter treat it
2642 * as a user hint.
2643 */
2644 if (!is_world_regdom(ieee80211_regdom))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002645 regulatory_hint_user(ieee80211_regdom,
2646 NL80211_USER_REG_HINT_USER);
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002647
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002648 return 0;
2649}
2650
Johannes Berg1a919312012-12-03 17:21:11 +01002651void regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002652{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002653 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002654 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002655
2656 cancel_work_sync(&reg_work);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002657 cancel_delayed_work_sync(&reg_timeout);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002658
Johannes Berg9027b142012-12-03 17:59:24 +01002659 /* Lock to suppress warnings */
Johannes Berg38fd2142013-05-10 19:17:17 +02002660 rtnl_lock();
Johannes Berg379b82f2012-12-06 15:44:07 +01002661 reset_regdomains(true, NULL);
Johannes Berg38fd2142013-05-10 19:17:17 +02002662 rtnl_unlock();
Johannes Berg734366d2008-09-15 10:56:48 +02002663
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08002664 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02002665
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002666 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002667
Johannes Bergfea9bce2012-12-03 17:32:01 +01002668 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2669 list_del(&reg_beacon->list);
2670 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002671 }
2672
Johannes Bergfea9bce2012-12-03 17:32:01 +01002673 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2674 list_del(&reg_beacon->list);
2675 kfree(reg_beacon);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002676 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002677
Johannes Bergfea9bce2012-12-03 17:32:01 +01002678 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2679 list_del(&reg_request->list);
2680 kfree(reg_request);
Johannes Berg8318d782008-01-24 19:38:38 +01002681 }
Johannes Berg8318d782008-01-24 19:38:38 +01002682}