blob: f59aaac586f8cf10905135324c3913646910a662 [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. Rodriguez255e25b2014-02-25 17:09:40 -0800243static void reg_free_request(struct regulatory_request *lr)
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800244{
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800245 if (lr != &core_request_world && lr)
246 kfree_rcu(lr, rcu_head);
247}
248
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800249static void reg_update_last_request(struct regulatory_request *request)
250{
Luis R. Rodriguez255e25b2014-02-25 17:09:40 -0800251 struct regulatory_request *lr;
252
253 lr = get_last_request();
254 if (lr == request)
255 return;
256
257 reg_free_request(lr);
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800258 rcu_assign_pointer(last_request, request);
259}
260
Johannes Berg379b82f2012-12-06 15:44:07 +0100261static void reset_regdomains(bool full_reset,
262 const struct ieee80211_regdomain *new_regdom)
Johannes Berg734366d2008-09-15 10:56:48 +0200263{
Johannes Berg458f4f92012-12-06 15:47:38 +0100264 const struct ieee80211_regdomain *r;
265
Johannes Berg38fd2142013-05-10 19:17:17 +0200266 ASSERT_RTNL();
Johannes Berge8da2bb2012-12-03 23:00:08 +0100267
Johannes Berg458f4f92012-12-06 15:47:38 +0100268 r = get_cfg80211_regdom();
269
Johannes Berg942b25c2008-09-15 11:26:47 +0200270 /* avoid freeing static information or freeing something twice */
Johannes Berg458f4f92012-12-06 15:47:38 +0100271 if (r == cfg80211_world_regdom)
272 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200273 if (cfg80211_world_regdom == &world_regdom)
274 cfg80211_world_regdom = NULL;
Johannes Berg458f4f92012-12-06 15:47:38 +0100275 if (r == &world_regdom)
276 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200277
Johannes Berg458f4f92012-12-06 15:47:38 +0100278 rcu_free_regdom(r);
279 rcu_free_regdom(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200280
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200281 cfg80211_world_regdom = &world_regdom;
Johannes Berg458f4f92012-12-06 15:47:38 +0100282 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500283
284 if (!full_reset)
285 return;
286
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800287 reg_update_last_request(&core_request_world);
Johannes Berg734366d2008-09-15 10:56:48 +0200288}
289
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500290/*
291 * Dynamic world regulatory domain requested by the wireless
292 * core upon initialization
293 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200294static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200295{
Johannes Bergc492db32012-12-06 16:29:25 +0100296 struct regulatory_request *lr;
Johannes Berg734366d2008-09-15 10:56:48 +0200297
Johannes Bergc492db32012-12-06 16:29:25 +0100298 lr = get_last_request();
299
300 WARN_ON(!lr);
Johannes Berge8da2bb2012-12-03 23:00:08 +0100301
Johannes Berg379b82f2012-12-06 15:44:07 +0100302 reset_regdomains(false, rd);
Johannes Berg734366d2008-09-15 10:56:48 +0200303
304 cfg80211_world_regdom = rd;
Johannes Berg734366d2008-09-15 10:56:48 +0200305}
Johannes Berg734366d2008-09-15 10:56:48 +0200306
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200307bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100308{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700309 if (!alpha2)
310 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100311 return alpha2[0] == '0' && alpha2[1] == '0';
Johannes Berg8318d782008-01-24 19:38:38 +0100312}
313
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200314static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700315{
316 if (!alpha2)
317 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100318 return alpha2[0] && alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700319}
Johannes Berg8318d782008-01-24 19:38:38 +0100320
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200321static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700322{
323 if (!alpha2)
324 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500325 /*
326 * Special case where regulatory domain was built by driver
327 * but a specific alpha2 cannot be determined
328 */
Johannes Berg1a919312012-12-03 17:21:11 +0100329 return alpha2[0] == '9' && alpha2[1] == '9';
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700330}
331
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800332static bool is_intersected_alpha2(const char *alpha2)
333{
334 if (!alpha2)
335 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500336 /*
337 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800338 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500339 * structures
340 */
Johannes Berg1a919312012-12-03 17:21:11 +0100341 return alpha2[0] == '9' && alpha2[1] == '8';
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800342}
343
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200344static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700345{
346 if (!alpha2)
347 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100348 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700349}
350
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200351static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700352{
353 if (!alpha2_x || !alpha2_y)
354 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100355 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700356}
357
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500358static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700359{
Johannes Berg458f4f92012-12-06 15:47:38 +0100360 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500361
Johannes Berg458f4f92012-12-06 15:47:38 +0100362 if (!r)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700363 return true;
Johannes Berg458f4f92012-12-06 15:47:38 +0100364 return !alpha2_equal(r->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700365}
366
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500367/*
368 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
369 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
370 * has ever been issued.
371 */
372static bool is_user_regdom_saved(void)
373{
374 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
375 return false;
376
377 /* This would indicate a mistake on the design */
Johannes Berg1a919312012-12-03 17:21:11 +0100378 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500379 "Unexpected user alpha2: %c%c\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100380 user_alpha2[0], user_alpha2[1]))
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500381 return false;
382
383 return true;
384}
385
Johannes Berge9763c32012-12-03 16:59:46 +0100386static const struct ieee80211_regdomain *
387reg_copy_regd(const struct ieee80211_regdomain *src_regd)
John W. Linville3b377ea2009-12-18 17:59:01 -0500388{
389 struct ieee80211_regdomain *regd;
Johannes Berge9763c32012-12-03 16:59:46 +0100390 int size_of_regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500391 unsigned int i;
392
Johannes Berg82f20852012-12-04 12:49:16 +0100393 size_of_regd =
394 sizeof(struct ieee80211_regdomain) +
395 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
John W. Linville3b377ea2009-12-18 17:59:01 -0500396
397 regd = kzalloc(size_of_regd, GFP_KERNEL);
398 if (!regd)
Johannes Berge9763c32012-12-03 16:59:46 +0100399 return ERR_PTR(-ENOMEM);
John W. Linville3b377ea2009-12-18 17:59:01 -0500400
401 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
402
403 for (i = 0; i < src_regd->n_reg_rules; i++)
404 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
Johannes Berge9763c32012-12-03 16:59:46 +0100405 sizeof(struct ieee80211_reg_rule));
John W. Linville3b377ea2009-12-18 17:59:01 -0500406
Johannes Berge9763c32012-12-03 16:59:46 +0100407 return regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500408}
409
410#ifdef CONFIG_CFG80211_INTERNAL_REGDB
411struct reg_regdb_search_request {
412 char alpha2[2];
413 struct list_head list;
414};
415
416static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400417static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500418
419static void reg_regdb_search(struct work_struct *work)
420{
421 struct reg_regdb_search_request *request;
Johannes Berge9763c32012-12-03 16:59:46 +0100422 const struct ieee80211_regdomain *curdom, *regdom = NULL;
423 int i;
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700424
Johannes Berg5fe231e2013-05-08 21:45:15 +0200425 rtnl_lock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500426
John W. Linville368d06f2010-03-16 15:40:59 -0400427 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500428 while (!list_empty(&reg_regdb_search_list)) {
429 request = list_first_entry(&reg_regdb_search_list,
430 struct reg_regdb_search_request,
431 list);
432 list_del(&request->list);
433
Johannes Berg1a919312012-12-03 17:21:11 +0100434 for (i = 0; i < reg_regdb_size; i++) {
John W. Linville3b377ea2009-12-18 17:59:01 -0500435 curdom = reg_regdb[i];
436
Johannes Berg1a919312012-12-03 17:21:11 +0100437 if (alpha2_equal(request->alpha2, curdom->alpha2)) {
Johannes Berge9763c32012-12-03 16:59:46 +0100438 regdom = reg_copy_regd(curdom);
John W. Linville3b377ea2009-12-18 17:59:01 -0500439 break;
440 }
441 }
442
443 kfree(request);
444 }
John W. Linville368d06f2010-03-16 15:40:59 -0400445 mutex_unlock(&reg_regdb_search_mutex);
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700446
Johannes Berge9763c32012-12-03 16:59:46 +0100447 if (!IS_ERR_OR_NULL(regdom))
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700448 set_regdom(regdom);
449
Johannes Berg5fe231e2013-05-08 21:45:15 +0200450 rtnl_unlock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500451}
452
453static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
454
455static void reg_regdb_query(const char *alpha2)
456{
457 struct reg_regdb_search_request *request;
458
459 if (!alpha2)
460 return;
461
462 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
463 if (!request)
464 return;
465
466 memcpy(request->alpha2, alpha2, 2);
467
John W. Linville368d06f2010-03-16 15:40:59 -0400468 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500469 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400470 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500471
472 schedule_work(&reg_regdb_work);
473}
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700474
475/* Feel free to add any other sanity checks here */
476static void reg_regdb_size_check(void)
477{
478 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
479 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
480}
John W. Linville3b377ea2009-12-18 17:59:01 -0500481#else
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700482static inline void reg_regdb_size_check(void) {}
John W. Linville3b377ea2009-12-18 17:59:01 -0500483static inline void reg_regdb_query(const char *alpha2) {}
484#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
485
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500486/*
487 * This lets us keep regulatory code which is updated on a regulatory
Johannes Berg1226d252014-02-25 15:43:36 +0100488 * basis in userspace.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500489 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700490static int call_crda(const char *alpha2)
491{
Johannes Berg1226d252014-02-25 15:43:36 +0100492 char country[12];
493 char *env[] = { country, NULL };
494
495 snprintf(country, sizeof(country), "COUNTRY=%c%c",
496 alpha2[0], alpha2[1]);
497
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700498 if (!is_world_regdom((char *) alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -0800499 pr_info("Calling CRDA for country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700500 alpha2[0], alpha2[1]);
501 else
Joe Perchese9c02682010-11-16 19:56:49 -0800502 pr_info("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700503
John W. Linville3b377ea2009-12-18 17:59:01 -0500504 /* query internal regulatory database (if it exists) */
505 reg_regdb_query(alpha2);
506
Johannes Berg1226d252014-02-25 15:43:36 +0100507 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700508}
509
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -0800510static enum reg_request_treatment
511reg_call_crda(struct regulatory_request *request)
512{
513 if (call_crda(request->alpha2))
514 return REG_REQ_IGNORE;
515 return REG_REQ_OK;
516}
517
Luis R. Rodrigueze4387682013-11-05 09:18:01 -0800518bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700519{
Johannes Bergc492db32012-12-06 16:29:25 +0100520 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400521
Johannes Bergc492db32012-12-06 16:29:25 +0100522 if (!lr || lr->processed)
Johannes Bergf6037d02008-10-21 11:01:33 +0200523 return false;
524
Johannes Bergc492db32012-12-06 16:29:25 +0100525 return alpha2_equal(lr->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700526}
527
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100528static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
529{
530 struct regulatory_request *lr = get_last_request();
531
532 /*
533 * Follow the driver's regulatory domain, if present, unless a country
534 * IE has been processed or a user wants to help complaince further
535 */
536 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
537 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
538 wiphy->regd)
539 return get_wiphy_regdom(wiphy);
540
541 return get_cfg80211_regdom();
542}
543
Janusz Dziedzic97524822014-01-30 09:52:20 +0100544unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
545 const struct ieee80211_reg_rule *rule)
546{
547 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
548 const struct ieee80211_freq_range *freq_range_tmp;
549 const struct ieee80211_reg_rule *tmp;
550 u32 start_freq, end_freq, idx, no;
551
552 for (idx = 0; idx < rd->n_reg_rules; idx++)
553 if (rule == &rd->reg_rules[idx])
554 break;
555
556 if (idx == rd->n_reg_rules)
557 return 0;
558
559 /* get start_freq */
560 no = idx;
561
562 while (no) {
563 tmp = &rd->reg_rules[--no];
564 freq_range_tmp = &tmp->freq_range;
565
566 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
567 break;
568
Janusz Dziedzic97524822014-01-30 09:52:20 +0100569 freq_range = freq_range_tmp;
570 }
571
572 start_freq = freq_range->start_freq_khz;
573
574 /* get end_freq */
575 freq_range = &rule->freq_range;
576 no = idx;
577
578 while (no < rd->n_reg_rules - 1) {
579 tmp = &rd->reg_rules[++no];
580 freq_range_tmp = &tmp->freq_range;
581
582 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
583 break;
584
Janusz Dziedzic97524822014-01-30 09:52:20 +0100585 freq_range = freq_range_tmp;
586 }
587
588 end_freq = freq_range->end_freq_khz;
589
590 return end_freq - start_freq;
591}
592
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700593/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200594static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700595{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200596 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700597 u32 freq_diff;
598
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800599 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700600 return false;
601
602 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
603 return false;
604
605 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
606
Roel Kluinbd05f282009-03-03 22:55:21 +0100607 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg1a919312012-12-03 17:21:11 +0100608 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700609 return false;
610
611 return true;
612}
613
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200614static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700615{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200616 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700617 unsigned int i;
618
619 if (!rd->n_reg_rules)
620 return false;
621
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800622 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
623 return false;
624
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700625 for (i = 0; i < rd->n_reg_rules; i++) {
626 reg_rule = &rd->reg_rules[i];
627 if (!is_valid_reg_rule(reg_rule))
628 return false;
629 }
630
631 return true;
632}
633
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400634static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100635 u32 center_freq_khz, u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700636{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400637 u32 start_freq_khz, end_freq_khz;
638
639 start_freq_khz = center_freq_khz - (bw_khz/2);
640 end_freq_khz = center_freq_khz + (bw_khz/2);
641
642 if (start_freq_khz >= freq_range->start_freq_khz &&
643 end_freq_khz <= freq_range->end_freq_khz)
644 return true;
645
646 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700647}
648
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800649/**
650 * freq_in_rule_band - tells us if a frequency is in a frequency band
651 * @freq_range: frequency rule we want to query
652 * @freq_khz: frequency we are inquiring about
653 *
654 * This lets us know if a specific frequency rule is or is not relevant to
655 * a specific frequency's band. Bands are device specific and artificial
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200656 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
657 * however it is safe for now to assume that a frequency rule should not be
658 * part of a frequency's band if the start freq or end freq are off by more
659 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
660 * 60 GHz band.
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800661 * This resolution can be lowered and should be considered as we add
662 * regulatory rule support for other "bands".
663 **/
664static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg1a919312012-12-03 17:21:11 +0100665 u32 freq_khz)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800666{
667#define ONE_GHZ_IN_KHZ 1000000
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200668 /*
669 * From 802.11ad: directional multi-gigabit (DMG):
670 * Pertaining to operation in a frequency band containing a channel
671 * with the Channel starting frequency above 45 GHz.
672 */
673 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
674 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
675 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800676 return true;
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200677 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800678 return true;
679 return false;
680#undef ONE_GHZ_IN_KHZ
681}
682
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500683/*
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100684 * Later on we can perhaps use the more restrictive DFS
685 * region but we don't have information for that yet so
686 * for now simply disallow conflicts.
687 */
688static enum nl80211_dfs_regions
689reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
690 const enum nl80211_dfs_regions dfs_region2)
691{
692 if (dfs_region1 != dfs_region2)
693 return NL80211_DFS_UNSET;
694 return dfs_region1;
695}
696
697/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500698 * Helper for regdom_intersect(), this does the real
699 * mathematical intersection fun
700 */
Janusz Dziedzic97524822014-01-30 09:52:20 +0100701static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
702 const struct ieee80211_regdomain *rd2,
703 const struct ieee80211_reg_rule *rule1,
Johannes Berg1a919312012-12-03 17:21:11 +0100704 const struct ieee80211_reg_rule *rule2,
705 struct ieee80211_reg_rule *intersected_rule)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700706{
707 const struct ieee80211_freq_range *freq_range1, *freq_range2;
708 struct ieee80211_freq_range *freq_range;
709 const struct ieee80211_power_rule *power_rule1, *power_rule2;
710 struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +0100711 u32 freq_diff, max_bandwidth1, max_bandwidth2;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700712
713 freq_range1 = &rule1->freq_range;
714 freq_range2 = &rule2->freq_range;
715 freq_range = &intersected_rule->freq_range;
716
717 power_rule1 = &rule1->power_rule;
718 power_rule2 = &rule2->power_rule;
719 power_rule = &intersected_rule->power_rule;
720
721 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100722 freq_range2->start_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700723 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100724 freq_range2->end_freq_khz);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100725
726 max_bandwidth1 = freq_range1->max_bandwidth_khz;
727 max_bandwidth2 = freq_range2->max_bandwidth_khz;
728
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100729 if (rule1->flags & NL80211_RRF_AUTO_BW)
730 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
731 if (rule2->flags & NL80211_RRF_AUTO_BW)
732 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100733
734 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700735
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100736 intersected_rule->flags = rule1->flags | rule2->flags;
737
738 /*
739 * In case NL80211_RRF_AUTO_BW requested for both rules
740 * set AUTO_BW in intersected rule also. Next we will
741 * calculate BW correctly in handle_channel function.
742 * In other case remove AUTO_BW flag while we calculate
743 * maximum bandwidth correctly and auto calculation is
744 * not required.
745 */
746 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
747 (rule2->flags & NL80211_RRF_AUTO_BW))
748 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
749 else
750 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
751
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700752 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
753 if (freq_range->max_bandwidth_khz > freq_diff)
754 freq_range->max_bandwidth_khz = freq_diff;
755
756 power_rule->max_eirp = min(power_rule1->max_eirp,
757 power_rule2->max_eirp);
758 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
759 power_rule2->max_antenna_gain);
760
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100761 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
762 rule2->dfs_cac_ms);
763
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700764 if (!is_valid_reg_rule(intersected_rule))
765 return -EINVAL;
766
767 return 0;
768}
769
770/**
771 * regdom_intersect - do the intersection between two regulatory domains
772 * @rd1: first regulatory domain
773 * @rd2: second regulatory domain
774 *
775 * Use this function to get the intersection between two regulatory domains.
776 * Once completed we will mark the alpha2 for the rd as intersected, "98",
777 * as no one single alpha2 can represent this regulatory domain.
778 *
779 * Returns a pointer to the regulatory domain structure which will hold the
780 * resulting intersection of rules between rd1 and rd2. We will
781 * kzalloc() this structure for you.
782 */
Johannes Berg1a919312012-12-03 17:21:11 +0100783static struct ieee80211_regdomain *
784regdom_intersect(const struct ieee80211_regdomain *rd1,
785 const struct ieee80211_regdomain *rd2)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700786{
787 int r, size_of_regd;
788 unsigned int x, y;
789 unsigned int num_rules = 0, rule_idx = 0;
790 const struct ieee80211_reg_rule *rule1, *rule2;
791 struct ieee80211_reg_rule *intersected_rule;
792 struct ieee80211_regdomain *rd;
793 /* This is just a dummy holder to help us count */
Johannes Berg74f53cd2012-12-06 17:26:17 +0100794 struct ieee80211_reg_rule dummy_rule;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700795
796 if (!rd1 || !rd2)
797 return NULL;
798
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500799 /*
800 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700801 * build them. This is to so we can malloc() and free() a
802 * regdomain once. The reason we use reg_rules_intersect() here
803 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500804 * All rules that do check out OK are valid.
805 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700806
807 for (x = 0; x < rd1->n_reg_rules; x++) {
808 rule1 = &rd1->reg_rules[x];
809 for (y = 0; y < rd2->n_reg_rules; y++) {
810 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100811 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
812 &dummy_rule))
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700813 num_rules++;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700814 }
815 }
816
817 if (!num_rules)
818 return NULL;
819
820 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg82f20852012-12-04 12:49:16 +0100821 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700822
823 rd = kzalloc(size_of_regd, GFP_KERNEL);
824 if (!rd)
825 return NULL;
826
Johannes Berg8a57fff2012-12-06 17:03:17 +0100827 for (x = 0; x < rd1->n_reg_rules && rule_idx < num_rules; x++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700828 rule1 = &rd1->reg_rules[x];
Johannes Berg8a57fff2012-12-06 17:03:17 +0100829 for (y = 0; y < rd2->n_reg_rules && rule_idx < num_rules; y++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700830 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500831 /*
832 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700833 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500834 * a memcpy()
835 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700836 intersected_rule = &rd->reg_rules[rule_idx];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100837 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
838 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500839 /*
840 * No need to memset here the intersected rule here as
841 * we're not using the stack anymore
842 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700843 if (r)
844 continue;
845 rule_idx++;
846 }
847 }
848
849 if (rule_idx != num_rules) {
850 kfree(rd);
851 return NULL;
852 }
853
854 rd->n_reg_rules = num_rules;
855 rd->alpha2[0] = '9';
856 rd->alpha2[1] = '8';
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100857 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
858 rd2->dfs_region);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700859
860 return rd;
861}
862
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500863/*
864 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
865 * want to just have the channel structure use these
866 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700867static u32 map_regdom_flags(u32 rd_flags)
868{
869 u32 channel_flags = 0;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200870 if (rd_flags & NL80211_RRF_NO_IR_ALL)
871 channel_flags |= IEEE80211_CHAN_NO_IR;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700872 if (rd_flags & NL80211_RRF_DFS)
873 channel_flags |= IEEE80211_CHAN_RADAR;
Seth Forshee03f6b082012-08-01 15:58:42 -0500874 if (rd_flags & NL80211_RRF_NO_OFDM)
875 channel_flags |= IEEE80211_CHAN_NO_OFDM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700876 return channel_flags;
877}
878
Johannes Berg361c9c82012-12-06 15:57:14 +0100879static const struct ieee80211_reg_rule *
880freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
881 const struct ieee80211_regdomain *regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100882{
883 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800884 bool band_rule_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400885 bool bw_fits = false;
886
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800887 if (!regd)
Johannes Berg361c9c82012-12-06 15:57:14 +0100888 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700889
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800890 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700891 const struct ieee80211_reg_rule *rr;
892 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700893
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800894 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700895 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800896
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500897 /*
898 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800899 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500900 * not overwrite it once found
901 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800902 if (!band_rule_found)
903 band_rule_found = freq_in_rule_band(fr, center_freq);
904
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100905 bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800906
Johannes Berg361c9c82012-12-06 15:57:14 +0100907 if (band_rule_found && bw_fits)
908 return rr;
Johannes Berg8318d782008-01-24 19:38:38 +0100909 }
910
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800911 if (!band_rule_found)
Johannes Berg361c9c82012-12-06 15:57:14 +0100912 return ERR_PTR(-ERANGE);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800913
Johannes Berg361c9c82012-12-06 15:57:14 +0100914 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700915}
916
Johannes Berg361c9c82012-12-06 15:57:14 +0100917const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
918 u32 center_freq)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800919{
Johannes Berg5d885b92012-12-04 00:14:17 +0100920 const struct ieee80211_regdomain *regd;
Johannes Berg1a919312012-12-03 17:21:11 +0100921
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100922 regd = reg_get_regdomain(wiphy);
Johannes Berg5d885b92012-12-04 00:14:17 +0100923
Johannes Berg361c9c82012-12-06 15:57:14 +0100924 return freq_reg_info_regd(wiphy, center_freq, regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800925}
John W. Linville4f366c52010-07-15 14:57:33 -0400926EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700927
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700928const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530929{
930 switch (initiator) {
931 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700932 return "core";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530933 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700934 return "user";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530935 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700936 return "driver";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530937 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700938 return "country IE";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530939 default:
940 WARN_ON(1);
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700941 return "bug";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530942 }
943}
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700944EXPORT_SYMBOL(reg_initiator_name);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530945
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700946#ifdef CONFIG_CFG80211_REG_DEBUG
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100947static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
948 struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530949 const struct ieee80211_reg_rule *reg_rule)
950{
951 const struct ieee80211_power_rule *power_rule;
952 const struct ieee80211_freq_range *freq_range;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100953 char max_antenna_gain[32], bw[32];
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530954
955 power_rule = &reg_rule->power_rule;
956 freq_range = &reg_rule->freq_range;
957
958 if (!power_rule->max_antenna_gain)
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100959 snprintf(max_antenna_gain, sizeof(max_antenna_gain), "N/A");
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530960 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100961 snprintf(max_antenna_gain, sizeof(max_antenna_gain), "%d",
962 power_rule->max_antenna_gain);
963
964 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
965 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
966 freq_range->max_bandwidth_khz,
967 reg_get_max_bandwidth(regd, reg_rule));
968 else
969 snprintf(bw, sizeof(bw), "%d KHz",
970 freq_range->max_bandwidth_khz);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530971
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100972 REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
973 chan->center_freq);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530974
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100975 REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100976 freq_range->start_freq_khz, freq_range->end_freq_khz,
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100977 bw, max_antenna_gain,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530978 power_rule->max_eirp);
979}
980#else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100981static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
982 struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530983 const struct ieee80211_reg_rule *reg_rule)
984{
985 return;
986}
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530987#endif
988
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400989/*
990 * Note that right now we assume the desired channel bandwidth
991 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100992 * per channel, the primary and the extension channel).
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400993 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700994static void handle_channel(struct wiphy *wiphy,
995 enum nl80211_reg_initiator initiator,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100996 struct ieee80211_channel *chan)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700997{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400998 u32 flags, bw_flags = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700999 const struct ieee80211_reg_rule *reg_rule = NULL;
1000 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001001 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001002 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01001003 struct regulatory_request *lr = get_last_request();
Janusz Dziedzic97524822014-01-30 09:52:20 +01001004 const struct ieee80211_regdomain *regd;
1005 u32 max_bandwidth_khz;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001006
Johannes Bergc492db32012-12-06 16:29:25 +01001007 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001008
1009 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001010
Johannes Berg361c9c82012-12-06 15:57:14 +01001011 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1012 if (IS_ERR(reg_rule)) {
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001013 /*
1014 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001015 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001016 * from a Country IE and the Country IE had no information
1017 * about a band. The IEEE 802.11 spec allows for an AP
1018 * to send only a subset of the regulatory rules allowed,
1019 * so an AP in the US that only supports 2.4 GHz may only send
1020 * a country IE with information for the 2.4 GHz band
1021 * while 5 GHz is still supported.
1022 */
1023 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg361c9c82012-12-06 15:57:14 +01001024 PTR_ERR(reg_rule) == -ERANGE)
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001025 return;
1026
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001027 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1028 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001029 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001030 REG_DBG_PRINT("Disabling freq %d MHz for good\n",
1031 chan->center_freq);
1032 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1033 chan->flags = chan->orig_flags;
1034 } else {
1035 REG_DBG_PRINT("Disabling freq %d MHz\n",
1036 chan->center_freq);
1037 chan->flags |= IEEE80211_CHAN_DISABLED;
1038 }
Johannes Berg8318d782008-01-24 19:38:38 +01001039 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001040 }
Johannes Berg8318d782008-01-24 19:38:38 +01001041
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001042 regd = reg_get_regdomain(wiphy);
1043 chan_reg_rule_print_dbg(regd, chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301044
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001045 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001046 freq_range = &reg_rule->freq_range;
1047
Janusz Dziedzic97524822014-01-30 09:52:20 +01001048 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1049 /* Check if auto calculation requested */
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001050 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001051 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
Janusz Dziedzic97524822014-01-30 09:52:20 +01001052
1053 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001054 bw_flags = IEEE80211_CHAN_NO_HT40;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001055 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001056 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001057 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001058 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001059
Johannes Bergc492db32012-12-06 16:29:25 +01001060 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001061 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001062 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001063 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001064 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001065 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001066 * settings
1067 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001068 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001069 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001070 chan->max_antenna_gain = chan->orig_mag =
1071 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001072 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001073 (int) MBM_TO_DBM(power_rule->max_eirp);
1074 return;
1075 }
1076
Simon Wunderlich04f39042013-02-08 18:16:19 +01001077 chan->dfs_state = NL80211_DFS_USABLE;
1078 chan->dfs_state_entered = jiffies;
1079
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +05301080 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001081 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg1a919312012-12-03 17:21:11 +01001082 chan->max_antenna_gain =
1083 min_t(int, chan->orig_mag,
1084 MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +02001085 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic089027e2014-02-21 19:46:12 +01001086
1087 if (chan->flags & IEEE80211_CHAN_RADAR) {
1088 if (reg_rule->dfs_cac_ms)
1089 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1090 else
1091 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1092 }
1093
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001094 if (chan->orig_mpwr) {
1095 /*
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001096 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1097 * will always follow the passed country IE power settings.
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001098 */
1099 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001100 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001101 chan->max_power = chan->max_reg_power;
1102 else
1103 chan->max_power = min(chan->orig_mpwr,
1104 chan->max_reg_power);
1105 } else
1106 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +01001107}
1108
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001109static void handle_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001110 enum nl80211_reg_initiator initiator,
1111 struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +01001112{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001113 unsigned int i;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001114
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001115 if (!sband)
1116 return;
Johannes Berg8318d782008-01-24 19:38:38 +01001117
1118 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001119 handle_channel(wiphy, initiator, &sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +01001120}
1121
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001122static bool reg_request_cell_base(struct regulatory_request *request)
1123{
1124 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1125 return false;
Johannes Berg1a919312012-12-03 17:21:11 +01001126 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001127}
1128
1129bool reg_last_request_cell_base(void)
1130{
Johannes Berg38fd2142013-05-10 19:17:17 +02001131 return reg_request_cell_base(get_last_request());
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001132}
1133
1134#ifdef CONFIG_CFG80211_CERTIFICATION_ONUS
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001135/* Core specific check */
Johannes Berg2f922122012-12-03 17:54:55 +01001136static enum reg_request_treatment
1137reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001138{
Johannes Bergc492db32012-12-06 16:29:25 +01001139 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001140
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001141 if (!reg_num_devs_support_basehint)
Johannes Berg2f922122012-12-03 17:54:55 +01001142 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001143
Johannes Bergc492db32012-12-06 16:29:25 +01001144 if (reg_request_cell_base(lr) &&
Johannes Berg1a919312012-12-03 17:21:11 +01001145 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001146 return REG_REQ_ALREADY_SET;
Johannes Berg1a919312012-12-03 17:21:11 +01001147
Johannes Berg2f922122012-12-03 17:54:55 +01001148 return REG_REQ_OK;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001149}
1150
1151/* Device specific check */
1152static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1153{
Johannes Berg1a919312012-12-03 17:21:11 +01001154 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001155}
1156#else
1157static int reg_ignore_cell_hint(struct regulatory_request *pending_request)
1158{
Johannes Berg2f922122012-12-03 17:54:55 +01001159 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001160}
Johannes Berg1a919312012-12-03 17:21:11 +01001161
1162static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001163{
1164 return true;
1165}
1166#endif
1167
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001168static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1169{
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001170 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1171 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001172 return true;
1173 return false;
1174}
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001175
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001176static bool ignore_reg_update(struct wiphy *wiphy,
1177 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001178{
Johannes Bergc492db32012-12-06 16:29:25 +01001179 struct regulatory_request *lr = get_last_request();
1180
1181 if (!lr) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001182 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1183 "since last_request is not set\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301184 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001185 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301186 }
1187
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001188 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001189 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001190 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1191 "since the driver uses its own custom "
1192 "regulatory domain\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301193 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001194 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301195 }
1196
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001197 /*
1198 * wiphy->regd will be set once the device has its own
1199 * desired regulatory domain set
1200 */
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001201 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -07001202 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Bergc492db32012-12-06 16:29:25 +01001203 !is_world_regdom(lr->alpha2)) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001204 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1205 "since the driver requires its own regulatory "
1206 "domain to be set first\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301207 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001208 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301209 }
1210
Johannes Bergc492db32012-12-06 16:29:25 +01001211 if (reg_request_cell_base(lr))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001212 return reg_dev_ignore_cell_hint(wiphy);
1213
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001214 return false;
1215}
1216
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001217static bool reg_is_world_roaming(struct wiphy *wiphy)
1218{
1219 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1220 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1221 struct regulatory_request *lr = get_last_request();
1222
1223 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1224 return true;
1225
1226 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001227 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001228 return true;
1229
1230 return false;
1231}
1232
Johannes Berg1a919312012-12-03 17:21:11 +01001233static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001234 struct reg_beacon *reg_beacon)
1235{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001236 struct ieee80211_supported_band *sband;
1237 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001238 bool channel_changed = false;
1239 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001240
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001241 sband = wiphy->bands[reg_beacon->chan.band];
1242 chan = &sband->channels[chan_idx];
1243
1244 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1245 return;
1246
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001247 if (chan->beacon_found)
1248 return;
1249
1250 chan->beacon_found = true;
1251
Luis R. Rodriguez0f500a52012-12-19 10:53:04 -08001252 if (!reg_is_world_roaming(wiphy))
1253 return;
1254
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001255 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001256 return;
1257
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001258 chan_before.center_freq = chan->center_freq;
1259 chan_before.flags = chan->flags;
1260
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001261 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1262 chan->flags &= ~IEEE80211_CHAN_NO_IR;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001263 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001264 }
1265
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001266 if (channel_changed)
1267 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001268}
1269
1270/*
1271 * Called when a scan on a wiphy finds a beacon on
1272 * new channel
1273 */
1274static void wiphy_update_new_beacon(struct wiphy *wiphy,
1275 struct reg_beacon *reg_beacon)
1276{
1277 unsigned int i;
1278 struct ieee80211_supported_band *sband;
1279
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001280 if (!wiphy->bands[reg_beacon->chan.band])
1281 return;
1282
1283 sband = wiphy->bands[reg_beacon->chan.band];
1284
1285 for (i = 0; i < sband->n_channels; i++)
1286 handle_reg_beacon(wiphy, i, reg_beacon);
1287}
1288
1289/*
1290 * Called upon reg changes or a new wiphy is added
1291 */
1292static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1293{
1294 unsigned int i;
1295 struct ieee80211_supported_band *sband;
1296 struct reg_beacon *reg_beacon;
1297
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001298 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1299 if (!wiphy->bands[reg_beacon->chan.band])
1300 continue;
1301 sband = wiphy->bands[reg_beacon->chan.band];
1302 for (i = 0; i < sband->n_channels; i++)
1303 handle_reg_beacon(wiphy, i, reg_beacon);
1304 }
1305}
1306
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001307/* Reap the advantages of previously found beacons */
1308static void reg_process_beacons(struct wiphy *wiphy)
1309{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001310 /*
1311 * Means we are just firing up cfg80211, so no beacons would
1312 * have been processed yet.
1313 */
1314 if (!last_request)
1315 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001316 wiphy_update_beacon_reg(wiphy);
1317}
1318
Johannes Berg1a919312012-12-03 17:21:11 +01001319static bool is_ht40_allowed(struct ieee80211_channel *chan)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001320{
1321 if (!chan)
Johannes Berg1a919312012-12-03 17:21:11 +01001322 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001323 if (chan->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg1a919312012-12-03 17:21:11 +01001324 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001325 /* This would happen when regulatory rules disallow HT40 completely */
Felix Fietkau55b183a2013-01-11 14:22:58 +01001326 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1327 return false;
1328 return true;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001329}
1330
1331static void reg_process_ht_flags_channel(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001332 struct ieee80211_channel *channel)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001333{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001334 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001335 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1336 unsigned int i;
1337
Johannes Berg1a919312012-12-03 17:21:11 +01001338 if (!is_ht40_allowed(channel)) {
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001339 channel->flags |= IEEE80211_CHAN_NO_HT40;
1340 return;
1341 }
1342
1343 /*
1344 * We need to ensure the extension channels exist to
1345 * be able to use HT40- or HT40+, this finds them (or not)
1346 */
1347 for (i = 0; i < sband->n_channels; i++) {
1348 struct ieee80211_channel *c = &sband->channels[i];
Johannes Berg1a919312012-12-03 17:21:11 +01001349
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001350 if (c->center_freq == (channel->center_freq - 20))
1351 channel_before = c;
1352 if (c->center_freq == (channel->center_freq + 20))
1353 channel_after = c;
1354 }
1355
1356 /*
1357 * Please note that this assumes target bandwidth is 20 MHz,
1358 * if that ever changes we also need to change the below logic
1359 * to include that as well.
1360 */
Johannes Berg1a919312012-12-03 17:21:11 +01001361 if (!is_ht40_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001362 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001363 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001364 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001365
Johannes Berg1a919312012-12-03 17:21:11 +01001366 if (!is_ht40_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001367 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001368 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001369 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001370}
1371
1372static void reg_process_ht_flags_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001373 struct ieee80211_supported_band *sband)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001374{
1375 unsigned int i;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001376
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001377 if (!sband)
1378 return;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001379
1380 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001381 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001382}
1383
1384static void reg_process_ht_flags(struct wiphy *wiphy)
1385{
1386 enum ieee80211_band band;
1387
1388 if (!wiphy)
1389 return;
1390
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001391 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1392 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001393}
1394
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001395static void reg_call_notifier(struct wiphy *wiphy,
1396 struct regulatory_request *request)
1397{
1398 if (wiphy->reg_notifier)
1399 wiphy->reg_notifier(wiphy, request);
1400}
1401
Sven Neumanneac03e32011-08-30 23:38:53 +02001402static void wiphy_update_regulatory(struct wiphy *wiphy,
1403 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001404{
1405 enum ieee80211_band band;
Johannes Bergc492db32012-12-06 16:29:25 +01001406 struct regulatory_request *lr = get_last_request();
Sven Neumanneac03e32011-08-30 23:38:53 +02001407
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001408 if (ignore_reg_update(wiphy, initiator)) {
1409 /*
1410 * Regulatory updates set by CORE are ignored for custom
1411 * regulatory cards. Let us notify the changes to the driver,
1412 * as some drivers used this to restore its orig_* reg domain.
1413 */
1414 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001415 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001416 reg_call_notifier(wiphy, lr);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001417 return;
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001418 }
Sven Neumanna203c2a2011-07-12 15:52:07 +02001419
Johannes Bergc492db32012-12-06 16:29:25 +01001420 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07001421
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001422 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1423 handle_band(wiphy, initiator, wiphy->bands[band]);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001424
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001425 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001426 reg_process_ht_flags(wiphy);
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001427 reg_call_notifier(wiphy, lr);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001428}
1429
Sven Neumannd7549cb2011-08-30 23:38:54 +02001430static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1431{
1432 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301433 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02001434
Johannes Berg5fe231e2013-05-08 21:45:15 +02001435 ASSERT_RTNL();
Johannes Berg458f4f92012-12-06 15:47:38 +01001436
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301437 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1438 wiphy = &rdev->wiphy;
1439 wiphy_update_regulatory(wiphy, initiator);
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301440 }
Sven Neumannd7549cb2011-08-30 23:38:54 +02001441}
1442
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001443static void handle_channel_custom(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001444 struct ieee80211_channel *chan,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001445 const struct ieee80211_regdomain *regd)
1446{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001447 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001448 const struct ieee80211_reg_rule *reg_rule = NULL;
1449 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001450 const struct ieee80211_freq_range *freq_range = NULL;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001451 u32 max_bandwidth_khz;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001452
Johannes Berg361c9c82012-12-06 15:57:14 +01001453 reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1454 regd);
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001455
Johannes Berg361c9c82012-12-06 15:57:14 +01001456 if (IS_ERR(reg_rule)) {
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001457 REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1458 chan->center_freq);
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001459 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1460 chan->flags = chan->orig_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001461 return;
1462 }
1463
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001464 chan_reg_rule_print_dbg(regd, chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301465
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001466 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001467 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001468
Janusz Dziedzic97524822014-01-30 09:52:20 +01001469 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1470 /* Check if auto calculation requested */
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001471 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001472 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1473
1474 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001475 bw_flags = IEEE80211_CHAN_NO_HT40;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001476 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001477 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001478 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001479 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001480
1481 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001482 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001483 chan->max_reg_power = chan->max_power =
1484 (int) MBM_TO_DBM(power_rule->max_eirp);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001485}
1486
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001487static void handle_band_custom(struct wiphy *wiphy,
1488 struct ieee80211_supported_band *sband,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001489 const struct ieee80211_regdomain *regd)
1490{
1491 unsigned int i;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001492
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001493 if (!sband)
1494 return;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001495
1496 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001497 handle_channel_custom(wiphy, &sband->channels[i], regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001498}
1499
1500/* Used by drivers prior to wiphy registration */
1501void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1502 const struct ieee80211_regdomain *regd)
1503{
1504 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001505 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001506
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001507 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
1508 "wiphy should have REGULATORY_CUSTOM_REG\n");
1509 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Luis R. Rodriguez222ea582013-11-05 09:18:00 -08001510
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001511 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001512 if (!wiphy->bands[band])
1513 continue;
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001514 handle_band_custom(wiphy, wiphy->bands[band], regd);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001515 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001516 }
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001517
1518 /*
1519 * no point in calling this if it won't have any effect
Johannes Berg1a919312012-12-03 17:21:11 +01001520 * on your device's supported bands.
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001521 */
1522 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001523}
1524EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1525
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001526static void reg_set_request_processed(void)
1527{
1528 bool need_more_processing = false;
Johannes Bergc492db32012-12-06 16:29:25 +01001529 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001530
Johannes Bergc492db32012-12-06 16:29:25 +01001531 lr->processed = true;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001532
1533 spin_lock(&reg_requests_lock);
1534 if (!list_empty(&reg_requests_list))
1535 need_more_processing = true;
1536 spin_unlock(&reg_requests_lock);
1537
Johannes Bergc492db32012-12-06 16:29:25 +01001538 if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
Eliad Pellerfe20b392012-06-12 12:53:13 +03001539 cancel_delayed_work(&reg_timeout);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001540
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001541 if (need_more_processing)
1542 schedule_work(&reg_work);
1543}
1544
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001545/**
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001546 * reg_process_hint_core - process core regulatory requests
1547 * @pending_request: a pending core regulatory request
1548 *
1549 * The wireless subsystem can use this function to process
1550 * a regulatory request issued by the regulatory core.
1551 *
1552 * Returns one of the different reg request treatment values.
1553 */
1554static enum reg_request_treatment
1555reg_process_hint_core(struct regulatory_request *core_request)
1556{
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001557
1558 core_request->intersect = false;
1559 core_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001560
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001561 reg_update_last_request(core_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001562
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001563 return reg_call_crda(core_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001564}
1565
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001566static enum reg_request_treatment
1567__reg_process_hint_user(struct regulatory_request *user_request)
1568{
1569 struct regulatory_request *lr = get_last_request();
1570
1571 if (reg_request_cell_base(user_request))
1572 return reg_ignore_cell_hint(user_request);
1573
1574 if (reg_request_cell_base(lr))
1575 return REG_REQ_IGNORE;
1576
1577 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1578 return REG_REQ_INTERSECT;
1579 /*
1580 * If the user knows better the user should set the regdom
1581 * to their country before the IE is picked up
1582 */
1583 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
1584 lr->intersect)
1585 return REG_REQ_IGNORE;
1586 /*
1587 * Process user requests only after previous user/driver/core
1588 * requests have been processed
1589 */
1590 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
1591 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1592 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
1593 regdom_changes(lr->alpha2))
1594 return REG_REQ_IGNORE;
1595
1596 if (!regdom_changes(user_request->alpha2))
1597 return REG_REQ_ALREADY_SET;
1598
1599 return REG_REQ_OK;
1600}
1601
1602/**
1603 * reg_process_hint_user - process user regulatory requests
1604 * @user_request: a pending user regulatory request
1605 *
1606 * The wireless subsystem can use this function to process
1607 * a regulatory request initiated by userspace.
1608 *
1609 * Returns one of the different reg request treatment values.
1610 */
1611static enum reg_request_treatment
1612reg_process_hint_user(struct regulatory_request *user_request)
1613{
1614 enum reg_request_treatment treatment;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001615
1616 treatment = __reg_process_hint_user(user_request);
1617 if (treatment == REG_REQ_IGNORE ||
1618 treatment == REG_REQ_ALREADY_SET) {
1619 kfree(user_request);
1620 return treatment;
1621 }
1622
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001623 user_request->intersect = treatment == REG_REQ_INTERSECT;
1624 user_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001625
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001626 reg_update_last_request(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001627
1628 user_alpha2[0] = user_request->alpha2[0];
1629 user_alpha2[1] = user_request->alpha2[1];
1630
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001631 return reg_call_crda(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001632}
1633
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001634static enum reg_request_treatment
1635__reg_process_hint_driver(struct regulatory_request *driver_request)
1636{
1637 struct regulatory_request *lr = get_last_request();
1638
1639 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
1640 if (regdom_changes(driver_request->alpha2))
1641 return REG_REQ_OK;
1642 return REG_REQ_ALREADY_SET;
1643 }
1644
1645 /*
1646 * This would happen if you unplug and plug your card
1647 * back in or if you add a new device for which the previously
1648 * loaded card also agrees on the regulatory domain.
1649 */
1650 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1651 !regdom_changes(driver_request->alpha2))
1652 return REG_REQ_ALREADY_SET;
1653
1654 return REG_REQ_INTERSECT;
1655}
1656
1657/**
1658 * reg_process_hint_driver - process driver regulatory requests
1659 * @driver_request: a pending driver regulatory request
1660 *
1661 * The wireless subsystem can use this function to process
1662 * a regulatory request issued by an 802.11 driver.
1663 *
1664 * Returns one of the different reg request treatment values.
1665 */
1666static enum reg_request_treatment
1667reg_process_hint_driver(struct wiphy *wiphy,
1668 struct regulatory_request *driver_request)
1669{
1670 const struct ieee80211_regdomain *regd;
1671 enum reg_request_treatment treatment;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001672
1673 treatment = __reg_process_hint_driver(driver_request);
1674
1675 switch (treatment) {
1676 case REG_REQ_OK:
1677 break;
1678 case REG_REQ_IGNORE:
1679 kfree(driver_request);
1680 return treatment;
1681 case REG_REQ_INTERSECT:
1682 /* fall through */
1683 case REG_REQ_ALREADY_SET:
1684 regd = reg_copy_regd(get_cfg80211_regdom());
1685 if (IS_ERR(regd)) {
1686 kfree(driver_request);
1687 return REG_REQ_IGNORE;
1688 }
1689 rcu_assign_pointer(wiphy->regd, regd);
1690 }
1691
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001692
1693 driver_request->intersect = treatment == REG_REQ_INTERSECT;
1694 driver_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001695
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001696 reg_update_last_request(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001697
1698 /*
1699 * Since CRDA will not be called in this case as we already
1700 * have applied the requested regulatory domain before we just
1701 * inform userspace we have processed the request
1702 */
1703 if (treatment == REG_REQ_ALREADY_SET) {
1704 nl80211_send_reg_change_event(driver_request);
1705 reg_set_request_processed();
1706 return treatment;
1707 }
1708
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001709 return reg_call_crda(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001710}
1711
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001712static enum reg_request_treatment
1713__reg_process_hint_country_ie(struct wiphy *wiphy,
1714 struct regulatory_request *country_ie_request)
1715{
1716 struct wiphy *last_wiphy = NULL;
1717 struct regulatory_request *lr = get_last_request();
1718
1719 if (reg_request_cell_base(lr)) {
1720 /* Trust a Cell base station over the AP's country IE */
1721 if (regdom_changes(country_ie_request->alpha2))
1722 return REG_REQ_IGNORE;
1723 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez2a901462013-11-11 22:15:31 +01001724 } else {
1725 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
1726 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001727 }
1728
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001729 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
1730 return -EINVAL;
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001731
1732 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
1733 return REG_REQ_OK;
1734
1735 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
1736
1737 if (last_wiphy != wiphy) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001738 /*
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001739 * Two cards with two APs claiming different
1740 * Country IE alpha2s. We could
1741 * intersect them, but that seems unlikely
1742 * to be correct. Reject second one for now.
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001743 */
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001744 if (regdom_changes(country_ie_request->alpha2))
1745 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001746 return REG_REQ_ALREADY_SET;
1747 }
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001748 /*
1749 * Two consecutive Country IE hints on the same wiphy.
1750 * This should be picked up early by the driver/stack
1751 */
1752 if (WARN_ON(regdom_changes(country_ie_request->alpha2)))
1753 return REG_REQ_OK;
1754 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001755}
1756
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001757/**
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001758 * reg_process_hint_country_ie - process regulatory requests from country IEs
1759 * @country_ie_request: a regulatory request from a country IE
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001760 *
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001761 * The wireless subsystem can use this function to process
1762 * a regulatory request issued by a country Information Element.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001763 *
Johannes Berg2f922122012-12-03 17:54:55 +01001764 * Returns one of the different reg request treatment values.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001765 */
Johannes Berg2f922122012-12-03 17:54:55 +01001766static enum reg_request_treatment
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001767reg_process_hint_country_ie(struct wiphy *wiphy,
1768 struct regulatory_request *country_ie_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001769{
Johannes Berg2f922122012-12-03 17:54:55 +01001770 enum reg_request_treatment treatment;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001771
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001772 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001773
Johannes Berg2f922122012-12-03 17:54:55 +01001774 switch (treatment) {
Johannes Berg2f922122012-12-03 17:54:55 +01001775 case REG_REQ_OK:
1776 break;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001777 case REG_REQ_IGNORE:
1778 /* fall through */
1779 case REG_REQ_ALREADY_SET:
1780 kfree(country_ie_request);
Johannes Berg2f922122012-12-03 17:54:55 +01001781 return treatment;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001782 case REG_REQ_INTERSECT:
1783 kfree(country_ie_request);
1784 /*
1785 * This doesn't happen yet, not sure we
1786 * ever want to support it for this case.
1787 */
1788 WARN_ONCE(1, "Unexpected intersection for country IEs");
1789 return REG_REQ_IGNORE;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001790 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001791
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001792 country_ie_request->intersect = false;
1793 country_ie_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001794
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001795 reg_update_last_request(country_ie_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001796
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001797 return reg_call_crda(country_ie_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001798}
1799
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001800/* This processes *all* regulatory hints */
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08001801static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001802{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001803 struct wiphy *wiphy = NULL;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001804 enum reg_request_treatment treatment;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001805
Johannes Bergf4173762012-12-03 18:23:37 +01001806 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001807 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1808
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001809 switch (reg_request->initiator) {
1810 case NL80211_REGDOM_SET_BY_CORE:
1811 reg_process_hint_core(reg_request);
1812 return;
1813 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001814 treatment = reg_process_hint_user(reg_request);
Inbal Hacohen50c11eb2014-02-12 09:32:27 +02001815 if (treatment == REG_REQ_IGNORE ||
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001816 treatment == REG_REQ_ALREADY_SET)
1817 return;
Shaibal Dutta845f3352014-01-30 15:08:30 -08001818 queue_delayed_work(system_power_efficient_wq,
1819 &reg_timeout, msecs_to_jiffies(3142));
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001820 return;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001821 case NL80211_REGDOM_SET_BY_DRIVER:
Ilan Peer772f0382014-01-14 15:17:23 +02001822 if (!wiphy)
1823 goto out_free;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001824 treatment = reg_process_hint_driver(wiphy, reg_request);
1825 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001826 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Ilan Peer772f0382014-01-14 15:17:23 +02001827 if (!wiphy)
1828 goto out_free;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001829 treatment = reg_process_hint_country_ie(wiphy, reg_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001830 break;
1831 default:
1832 WARN(1, "invalid initiator %d\n", reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02001833 goto out_free;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001834 }
1835
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001836 /* This is required so that the orig_* parameters are saved */
1837 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001838 wiphy->regulatory_flags & REGULATORY_STRICT_REG)
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001839 wiphy_update_regulatory(wiphy, reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02001840
1841 return;
1842
1843out_free:
1844 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001845}
1846
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001847/*
1848 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1849 * Regulatory hints come on a first come first serve basis and we
1850 * must process each one atomically.
1851 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001852static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001853{
Johannes Bergc492db32012-12-06 16:29:25 +01001854 struct regulatory_request *reg_request, *lr;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001855
Johannes Bergc492db32012-12-06 16:29:25 +01001856 lr = get_last_request();
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001857
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001858 /* When last_request->processed becomes true this will be rescheduled */
Johannes Bergc492db32012-12-06 16:29:25 +01001859 if (lr && !lr->processed) {
Johannes Berg1a919312012-12-03 17:21:11 +01001860 REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n");
Johannes Berg5fe231e2013-05-08 21:45:15 +02001861 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001862 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001863
1864 spin_lock(&reg_requests_lock);
1865
1866 if (list_empty(&reg_requests_list)) {
1867 spin_unlock(&reg_requests_lock);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001868 return;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001869 }
1870
1871 reg_request = list_first_entry(&reg_requests_list,
1872 struct regulatory_request,
1873 list);
1874 list_del_init(&reg_request->list);
1875
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001876 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001877
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08001878 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001879}
1880
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001881/* Processes beacon hints -- this has nothing to do with country IEs */
1882static void reg_process_pending_beacon_hints(void)
1883{
Johannes Berg79c97e92009-07-07 03:56:12 +02001884 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001885 struct reg_beacon *pending_beacon, *tmp;
1886
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001887 /* This goes through the _pending_ beacon list */
1888 spin_lock_bh(&reg_pending_beacons_lock);
1889
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001890 list_for_each_entry_safe(pending_beacon, tmp,
1891 &reg_pending_beacons, list) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001892 list_del_init(&pending_beacon->list);
1893
1894 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001895 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1896 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001897
1898 /* Remembers the beacon hint for new wiphys or reg changes */
1899 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1900 }
1901
1902 spin_unlock_bh(&reg_pending_beacons_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001903}
1904
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001905static void reg_todo(struct work_struct *work)
1906{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001907 rtnl_lock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001908 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001909 reg_process_pending_beacon_hints();
Johannes Berg5fe231e2013-05-08 21:45:15 +02001910 rtnl_unlock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001911}
1912
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001913static void queue_regulatory_request(struct regulatory_request *request)
1914{
Johannes Bergd4f2c882012-12-03 18:59:58 +01001915 request->alpha2[0] = toupper(request->alpha2[0]);
1916 request->alpha2[1] = toupper(request->alpha2[1]);
John W. Linvillec61029c2010-08-05 14:26:24 -04001917
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001918 spin_lock(&reg_requests_lock);
1919 list_add_tail(&request->list, &reg_requests_list);
1920 spin_unlock(&reg_requests_lock);
1921
1922 schedule_work(&reg_work);
1923}
1924
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001925/*
1926 * Core regulatory hint -- happens during cfg80211_init()
1927 * and when we restore regulatory settings.
1928 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001929static int regulatory_hint_core(const char *alpha2)
1930{
1931 struct regulatory_request *request;
1932
Johannes Berg1a919312012-12-03 17:21:11 +01001933 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001934 if (!request)
1935 return -ENOMEM;
1936
1937 request->alpha2[0] = alpha2[0];
1938 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001939 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001940
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08001941 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001942
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001943 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001944}
1945
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001946/* User hints */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001947int regulatory_hint_user(const char *alpha2,
1948 enum nl80211_user_reg_hint_type user_reg_hint_type)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001949{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001950 struct regulatory_request *request;
1951
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001952 if (WARN_ON(!alpha2))
1953 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001954
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001955 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1956 if (!request)
1957 return -ENOMEM;
1958
Johannes Bergf4173762012-12-03 18:23:37 +01001959 request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001960 request->alpha2[0] = alpha2[0];
1961 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001962 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001963 request->user_reg_hint_type = user_reg_hint_type;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001964
1965 queue_regulatory_request(request);
1966
1967 return 0;
1968}
1969
1970/* Driver hints */
1971int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1972{
1973 struct regulatory_request *request;
1974
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001975 if (WARN_ON(!alpha2 || !wiphy))
1976 return -EINVAL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001977
Luis R. Rodriguez4f7b9142013-12-14 20:09:06 +01001978 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
1979
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001980 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1981 if (!request)
1982 return -ENOMEM;
1983
1984 request->wiphy_idx = get_wiphy_idx(wiphy);
1985
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001986 request->alpha2[0] = alpha2[0];
1987 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001988 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001989
1990 queue_regulatory_request(request);
1991
1992 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001993}
1994EXPORT_SYMBOL(regulatory_hint);
1995
Luis R. Rodriguez789fd032013-10-04 18:07:24 -07001996void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
1997 const u8 *country_ie, u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001998{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001999 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002000 enum environment_cap env = ENVIRON_ANY;
Johannes Bergdb2424c2013-05-10 19:07:52 +02002001 struct regulatory_request *request = NULL, *lr;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002002
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002003 /* IE len must be evenly divisible by 2 */
2004 if (country_ie_len & 0x01)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002005 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002006
2007 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002008 return;
2009
2010 request = kzalloc(sizeof(*request), GFP_KERNEL);
2011 if (!request)
2012 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002013
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002014 alpha2[0] = country_ie[0];
2015 alpha2[1] = country_ie[1];
2016
2017 if (country_ie[2] == 'I')
2018 env = ENVIRON_INDOOR;
2019 else if (country_ie[2] == 'O')
2020 env = ENVIRON_OUTDOOR;
2021
Johannes Bergdb2424c2013-05-10 19:07:52 +02002022 rcu_read_lock();
2023 lr = get_last_request();
2024
2025 if (unlikely(!lr))
2026 goto out;
2027
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002028 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002029 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002030 * We leave conflict resolution to the workqueue, where can hold
Johannes Berg5fe231e2013-05-08 21:45:15 +02002031 * the RTNL.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002032 */
Johannes Bergc492db32012-12-06 16:29:25 +01002033 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2034 lr->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002035 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002036
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002037 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04002038 request->alpha2[0] = alpha2[0];
2039 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002040 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002041 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002042
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002043 queue_regulatory_request(request);
Johannes Bergdb2424c2013-05-10 19:07:52 +02002044 request = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002045out:
Johannes Bergdb2424c2013-05-10 19:07:52 +02002046 kfree(request);
2047 rcu_read_unlock();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002048}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002049
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002050static void restore_alpha2(char *alpha2, bool reset_user)
2051{
2052 /* indicates there is no alpha2 to consider for restoration */
2053 alpha2[0] = '9';
2054 alpha2[1] = '7';
2055
2056 /* The user setting has precedence over the module parameter */
2057 if (is_user_regdom_saved()) {
2058 /* Unless we're asked to ignore it and reset it */
2059 if (reset_user) {
Johannes Berg1a919312012-12-03 17:21:11 +01002060 REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002061 user_alpha2[0] = '9';
2062 user_alpha2[1] = '7';
2063
2064 /*
2065 * If we're ignoring user settings, we still need to
2066 * check the module parameter to ensure we put things
2067 * back as they were for a full restore.
2068 */
2069 if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01002070 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2071 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002072 alpha2[0] = ieee80211_regdom[0];
2073 alpha2[1] = ieee80211_regdom[1];
2074 }
2075 } else {
Johannes Berg1a919312012-12-03 17:21:11 +01002076 REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
2077 user_alpha2[0], user_alpha2[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002078 alpha2[0] = user_alpha2[0];
2079 alpha2[1] = user_alpha2[1];
2080 }
2081 } else if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01002082 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2083 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002084 alpha2[0] = ieee80211_regdom[0];
2085 alpha2[1] = ieee80211_regdom[1];
2086 } else
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07002087 REG_DBG_PRINT("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002088}
2089
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302090static void restore_custom_reg_settings(struct wiphy *wiphy)
2091{
2092 struct ieee80211_supported_band *sband;
2093 enum ieee80211_band band;
2094 struct ieee80211_channel *chan;
2095 int i;
2096
2097 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2098 sband = wiphy->bands[band];
2099 if (!sband)
2100 continue;
2101 for (i = 0; i < sband->n_channels; i++) {
2102 chan = &sband->channels[i];
2103 chan->flags = chan->orig_flags;
2104 chan->max_antenna_gain = chan->orig_mag;
2105 chan->max_power = chan->orig_mpwr;
Paul Stewart899852a2012-08-01 16:54:42 -07002106 chan->beacon_found = false;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302107 }
2108 }
2109}
2110
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002111/*
2112 * Restoring regulatory settings involves ingoring any
2113 * possibly stale country IE information and user regulatory
2114 * settings if so desired, this includes any beacon hints
2115 * learned as we could have traveled outside to another country
2116 * after disconnection. To restore regulatory settings we do
2117 * exactly what we did at bootup:
2118 *
2119 * - send a core regulatory hint
2120 * - send a user regulatory hint if applicable
2121 *
2122 * Device drivers that send a regulatory hint for a specific country
2123 * keep their own regulatory domain on wiphy->regd so that does does
2124 * not need to be remembered.
2125 */
2126static void restore_regulatory_settings(bool reset_user)
2127{
2128 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002129 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002130 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002131 struct regulatory_request *reg_request, *tmp;
2132 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302133 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002134
Johannes Berg5fe231e2013-05-08 21:45:15 +02002135 ASSERT_RTNL();
2136
Johannes Berg2d319862013-01-09 12:01:38 +01002137 reset_regdomains(true, &world_regdom);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002138 restore_alpha2(alpha2, reset_user);
2139
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002140 /*
2141 * If there's any pending requests we simply
2142 * stash them to a temporary pending queue and
2143 * add then after we've restored regulatory
2144 * settings.
2145 */
2146 spin_lock(&reg_requests_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002147 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2148 if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
2149 continue;
2150 list_move_tail(&reg_request->list, &tmp_reg_req_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002151 }
2152 spin_unlock(&reg_requests_lock);
2153
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002154 /* Clear beacon hints */
2155 spin_lock_bh(&reg_pending_beacons_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002156 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2157 list_del(&reg_beacon->list);
2158 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002159 }
2160 spin_unlock_bh(&reg_pending_beacons_lock);
2161
Johannes Bergfea9bce2012-12-03 17:32:01 +01002162 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2163 list_del(&reg_beacon->list);
2164 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002165 }
2166
2167 /* First restore to the basic regulatory settings */
Johannes Berg379b82f2012-12-06 15:44:07 +01002168 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
2169 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002170
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302171 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01002172 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302173 restore_custom_reg_settings(&rdev->wiphy);
2174 }
2175
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002176 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002177
2178 /*
2179 * This restores the ieee80211_regdom module parameter
2180 * preference or the last user requested regulatory
2181 * settings, user regulatory settings takes precedence.
2182 */
2183 if (is_an_alpha2(alpha2))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002184 regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002185
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002186 spin_lock(&reg_requests_lock);
Johannes Berg11cff962012-12-03 18:56:41 +01002187 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002188 spin_unlock(&reg_requests_lock);
2189
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002190 REG_DBG_PRINT("Kicking the queue\n");
2191
2192 schedule_work(&reg_work);
2193}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002194
2195void regulatory_hint_disconnect(void)
2196{
Johannes Berg1a919312012-12-03 17:21:11 +01002197 REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002198 restore_regulatory_settings(false);
2199}
2200
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002201static bool freq_is_chan_12_13_14(u16 freq)
2202{
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002203 if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
2204 freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
2205 freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002206 return true;
2207 return false;
2208}
2209
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002210static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
2211{
2212 struct reg_beacon *pending_beacon;
2213
2214 list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
2215 if (beacon_chan->center_freq ==
2216 pending_beacon->chan.center_freq)
2217 return true;
2218 return false;
2219}
2220
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002221int regulatory_hint_found_beacon(struct wiphy *wiphy,
2222 struct ieee80211_channel *beacon_chan,
2223 gfp_t gfp)
2224{
2225 struct reg_beacon *reg_beacon;
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002226 bool processing;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002227
Johannes Berg1a919312012-12-03 17:21:11 +01002228 if (beacon_chan->beacon_found ||
2229 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002230 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg1a919312012-12-03 17:21:11 +01002231 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002232 return 0;
2233
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002234 spin_lock_bh(&reg_pending_beacons_lock);
2235 processing = pending_reg_beacon(beacon_chan);
2236 spin_unlock_bh(&reg_pending_beacons_lock);
2237
2238 if (processing)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002239 return 0;
2240
2241 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2242 if (!reg_beacon)
2243 return -ENOMEM;
2244
Johannes Berg1a919312012-12-03 17:21:11 +01002245 REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002246 beacon_chan->center_freq,
2247 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2248 wiphy_name(wiphy));
2249
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002250 memcpy(&reg_beacon->chan, beacon_chan,
Johannes Berg1a919312012-12-03 17:21:11 +01002251 sizeof(struct ieee80211_channel));
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002252
2253 /*
2254 * Since we can be called from BH or and non-BH context
2255 * we must use spin_lock_bh()
2256 */
2257 spin_lock_bh(&reg_pending_beacons_lock);
2258 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2259 spin_unlock_bh(&reg_pending_beacons_lock);
2260
2261 schedule_work(&reg_work);
2262
2263 return 0;
2264}
2265
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002266static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002267{
2268 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002269 const struct ieee80211_reg_rule *reg_rule = NULL;
2270 const struct ieee80211_freq_range *freq_range = NULL;
2271 const struct ieee80211_power_rule *power_rule = NULL;
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002272 char bw[32], cac_time[32];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002273
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002274 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002275
2276 for (i = 0; i < rd->n_reg_rules; i++) {
2277 reg_rule = &rd->reg_rules[i];
2278 freq_range = &reg_rule->freq_range;
2279 power_rule = &reg_rule->power_rule;
2280
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002281 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
2282 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
2283 freq_range->max_bandwidth_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002284 reg_get_max_bandwidth(rd, reg_rule));
2285 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002286 snprintf(bw, sizeof(bw), "%d KHz",
Janusz Dziedzic97524822014-01-30 09:52:20 +01002287 freq_range->max_bandwidth_khz);
2288
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002289 if (reg_rule->flags & NL80211_RRF_DFS)
2290 scnprintf(cac_time, sizeof(cac_time), "%u s",
2291 reg_rule->dfs_cac_ms/1000);
2292 else
2293 scnprintf(cac_time, sizeof(cac_time), "N/A");
2294
2295
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002296 /*
2297 * There may not be documentation for max antenna gain
2298 * in certain regions
2299 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002300 if (power_rule->max_antenna_gain)
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002301 pr_info(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002302 freq_range->start_freq_khz,
2303 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002304 bw,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002305 power_rule->max_antenna_gain,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002306 power_rule->max_eirp,
2307 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002308 else
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002309 pr_info(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002310 freq_range->start_freq_khz,
2311 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002312 bw,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002313 power_rule->max_eirp,
2314 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002315 }
2316}
2317
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01002318bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002319{
2320 switch (dfs_region) {
2321 case NL80211_DFS_UNSET:
2322 case NL80211_DFS_FCC:
2323 case NL80211_DFS_ETSI:
2324 case NL80211_DFS_JP:
2325 return true;
2326 default:
2327 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
2328 dfs_region);
2329 return false;
2330 }
2331}
2332
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002333static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002334{
Johannes Bergc492db32012-12-06 16:29:25 +01002335 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002336
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002337 if (is_intersected_alpha2(rd->alpha2)) {
Johannes Bergc492db32012-12-06 16:29:25 +01002338 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002339 struct cfg80211_registered_device *rdev;
Johannes Bergc492db32012-12-06 16:29:25 +01002340 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002341 if (rdev) {
Joe Perchese9c02682010-11-16 19:56:49 -08002342 pr_info("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002343 rdev->country_ie_alpha2[0],
2344 rdev->country_ie_alpha2[1]);
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");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002347 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002348 pr_info("Current regulatory domain intersected:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002349 } else if (is_world_regdom(rd->alpha2)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002350 pr_info("World regulatory domain updated:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002351 } else {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002352 if (is_unknown_alpha2(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08002353 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002354 else {
Johannes Bergc492db32012-12-06 16:29:25 +01002355 if (reg_request_cell_base(lr))
Johannes Berg1a919312012-12-03 17:21:11 +01002356 pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002357 rd->alpha2[0], rd->alpha2[1]);
2358 else
Johannes Berg1a919312012-12-03 17:21:11 +01002359 pr_info("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002360 rd->alpha2[0], rd->alpha2[1]);
2361 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002362 }
Johannes Berg1a919312012-12-03 17:21:11 +01002363
Luis R. Rodriguez3ef121b2013-11-13 18:54:05 +01002364 pr_info(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002365 print_rd_rules(rd);
2366}
2367
Johannes Berg2df78162008-10-28 16:49:41 +01002368static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002369{
Joe Perchese9c02682010-11-16 19:56:49 -08002370 pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002371 print_rd_rules(rd);
2372}
2373
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002374static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
2375{
2376 if (!is_world_regdom(rd->alpha2))
2377 return -EINVAL;
2378 update_world_regdomain(rd);
2379 return 0;
2380}
2381
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002382static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
2383 struct regulatory_request *user_request)
2384{
2385 const struct ieee80211_regdomain *intersected_rd = NULL;
2386
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002387 if (!regdom_changes(rd->alpha2))
2388 return -EALREADY;
2389
2390 if (!is_valid_rd(rd)) {
2391 pr_err("Invalid regulatory domain detected:\n");
2392 print_regdomain_info(rd);
2393 return -EINVAL;
2394 }
2395
2396 if (!user_request->intersect) {
2397 reset_regdomains(false, rd);
2398 return 0;
2399 }
2400
2401 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2402 if (!intersected_rd)
2403 return -EINVAL;
2404
2405 kfree(rd);
2406 rd = NULL;
2407 reset_regdomains(false, intersected_rd);
2408
2409 return 0;
2410}
2411
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002412static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
2413 struct regulatory_request *driver_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002414{
Johannes Berge9763c32012-12-03 16:59:46 +01002415 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002416 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002417 const struct ieee80211_regdomain *tmp;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002418 struct wiphy *request_wiphy;
Johannes Berg6913b492012-12-04 00:48:59 +01002419
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002420 if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002421 return -EINVAL;
2422
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002423 if (!regdom_changes(rd->alpha2))
2424 return -EALREADY;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002425
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002426 if (!is_valid_rd(rd)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002427 pr_err("Invalid regulatory domain detected:\n");
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002428 print_regdomain_info(rd);
2429 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002430 }
2431
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002432 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
2433 if (!request_wiphy) {
Shaibal Dutta845f3352014-01-30 15:08:30 -08002434 queue_delayed_work(system_power_efficient_wq,
2435 &reg_timeout, 0);
Johannes Bergde3584b2011-11-21 10:44:00 +01002436 return -ENODEV;
2437 }
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002438
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002439 if (!driver_request->intersect) {
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002440 if (request_wiphy->regd)
2441 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002442
Johannes Berge9763c32012-12-03 16:59:46 +01002443 regd = reg_copy_regd(rd);
2444 if (IS_ERR(regd))
2445 return PTR_ERR(regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002446
Johannes Berg458f4f92012-12-06 15:47:38 +01002447 rcu_assign_pointer(request_wiphy->regd, regd);
Johannes Berg379b82f2012-12-06 15:44:07 +01002448 reset_regdomains(false, rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002449 return 0;
2450 }
2451
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002452 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2453 if (!intersected_rd)
2454 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002455
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002456 /*
2457 * We can trash what CRDA provided now.
2458 * However if a driver requested this specific regulatory
2459 * domain we keep it for its private use
2460 */
2461 tmp = get_wiphy_regdom(request_wiphy);
2462 rcu_assign_pointer(request_wiphy->regd, rd);
2463 rcu_free_regdom(tmp);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002464
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002465 rd = NULL;
Larry Fingerb7566fc2013-02-04 15:33:44 -06002466
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002467 reset_regdomains(false, intersected_rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002468
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002469 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002470}
2471
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002472static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
2473 struct regulatory_request *country_ie_request)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002474{
2475 struct wiphy *request_wiphy;
2476
2477 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2478 !is_unknown_alpha2(rd->alpha2))
2479 return -EINVAL;
2480
2481 /*
2482 * Lets only bother proceeding on the same alpha2 if the current
2483 * rd is non static (it means CRDA was present and was used last)
2484 * and the pending request came in from a country IE
2485 */
2486
2487 if (!is_valid_rd(rd)) {
2488 pr_err("Invalid regulatory domain detected:\n");
2489 print_regdomain_info(rd);
2490 return -EINVAL;
2491 }
2492
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002493 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002494 if (!request_wiphy) {
Shaibal Dutta845f3352014-01-30 15:08:30 -08002495 queue_delayed_work(system_power_efficient_wq,
2496 &reg_timeout, 0);
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002497 return -ENODEV;
2498 }
2499
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002500 if (country_ie_request->intersect)
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002501 return -EINVAL;
2502
2503 reset_regdomains(false, rd);
2504 return 0;
2505}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002506
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002507/*
2508 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002509 * multiple drivers can be ironed out later. Caller must've already
Johannes Berg458f4f92012-12-06 15:47:38 +01002510 * kmalloc'd the rd structure.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002511 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002512int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002513{
Johannes Bergc492db32012-12-06 16:29:25 +01002514 struct regulatory_request *lr;
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002515 bool user_reset = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002516 int r;
2517
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002518 if (!reg_is_valid_request(rd->alpha2)) {
2519 kfree(rd);
2520 return -EINVAL;
2521 }
2522
Johannes Bergc492db32012-12-06 16:29:25 +01002523 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002524
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002525 /* Note that this doesn't update the wiphys, this is done below */
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002526 switch (lr->initiator) {
2527 case NL80211_REGDOM_SET_BY_CORE:
2528 r = reg_set_rd_core(rd);
2529 break;
2530 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002531 r = reg_set_rd_user(rd, lr);
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002532 user_reset = true;
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002533 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002534 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguezf5fe3242013-11-05 09:18:16 -08002535 r = reg_set_rd_driver(rd, lr);
2536 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002537 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002538 r = reg_set_rd_country_ie(rd, lr);
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002539 break;
2540 default:
2541 WARN(1, "invalid initiator %d\n", lr->initiator);
2542 return -EINVAL;
2543 }
2544
Johannes Bergd2372b32008-10-24 20:32:20 +02002545 if (r) {
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002546 switch (r) {
2547 case -EALREADY:
Kalle Valo95908532012-07-12 15:33:58 +03002548 reg_set_request_processed();
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002549 break;
2550 default:
2551 /* Back to world regulatory in case of errors */
2552 restore_regulatory_settings(user_reset);
2553 }
Kalle Valo95908532012-07-12 15:33:58 +03002554
Johannes Bergd2372b32008-10-24 20:32:20 +02002555 kfree(rd);
Johannes Berg38fd2142013-05-10 19:17:17 +02002556 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002557 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002558
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002559 /* This would make this whole thing pointless */
Johannes Berg38fd2142013-05-10 19:17:17 +02002560 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
2561 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002562
2563 /* update all wiphys now with the new established regulatory domain */
Johannes Bergc492db32012-12-06 16:29:25 +01002564 update_all_wiphy_regulatory(lr->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002565
Johannes Berg458f4f92012-12-06 15:47:38 +01002566 print_regdomain(get_cfg80211_regdom());
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002567
Johannes Bergc492db32012-12-06 16:29:25 +01002568 nl80211_send_reg_change_event(lr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002569
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002570 reg_set_request_processed();
2571
Johannes Berg38fd2142013-05-10 19:17:17 +02002572 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002573}
2574
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002575void wiphy_regulatory_register(struct wiphy *wiphy)
2576{
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002577 struct regulatory_request *lr;
2578
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002579 if (!reg_dev_ignore_cell_hint(wiphy))
2580 reg_num_devs_support_basehint++;
2581
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002582 lr = get_last_request();
2583 wiphy_update_regulatory(wiphy, lr->initiator);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002584}
2585
Luis R. Rodriguezbfead082012-07-12 11:49:19 -07002586void wiphy_regulatory_deregister(struct wiphy *wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002587{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002588 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01002589 struct regulatory_request *lr;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002590
Johannes Bergc492db32012-12-06 16:29:25 +01002591 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002592
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002593 if (!reg_dev_ignore_cell_hint(wiphy))
2594 reg_num_devs_support_basehint--;
2595
Johannes Berg458f4f92012-12-06 15:47:38 +01002596 rcu_free_regdom(get_wiphy_regdom(wiphy));
2597 rcu_assign_pointer(wiphy->regd, NULL);
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002598
Johannes Bergc492db32012-12-06 16:29:25 +01002599 if (lr)
2600 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002601
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002602 if (!request_wiphy || request_wiphy != wiphy)
Johannes Berg38fd2142013-05-10 19:17:17 +02002603 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002604
Johannes Bergc492db32012-12-06 16:29:25 +01002605 lr->wiphy_idx = WIPHY_IDX_INVALID;
2606 lr->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002607}
2608
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002609static void reg_timeout_work(struct work_struct *work)
2610{
Johannes Berg1a919312012-12-03 17:21:11 +01002611 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
Johannes Bergf77b86d2013-06-24 15:43:38 +02002612 rtnl_lock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002613 restore_regulatory_settings(true);
Johannes Bergf77b86d2013-06-24 15:43:38 +02002614 rtnl_unlock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002615}
2616
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002617int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002618{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002619 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002620
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002621 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2622 if (IS_ERR(reg_pdev))
2623 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002624
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002625 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002626 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002627
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -07002628 reg_regdb_size_check();
2629
Johannes Berg458f4f92012-12-06 15:47:38 +01002630 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002631
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002632 user_alpha2[0] = '9';
2633 user_alpha2[1] = '7';
2634
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002635 /* We always try to get an update for the static regdomain */
Johannes Berg458f4f92012-12-06 15:47:38 +01002636 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002637 if (err) {
2638 if (err == -ENOMEM)
2639 return err;
2640 /*
2641 * N.B. kobject_uevent_env() can fail mainly for when we're out
2642 * memory which is handled and propagated appropriately above
2643 * but it can also fail during a netlink_broadcast() or during
2644 * early boot for call_usermodehelper(). For now treat these
2645 * errors as non-fatal.
2646 */
Joe Perchese9c02682010-11-16 19:56:49 -08002647 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002648 }
Johannes Berg734366d2008-09-15 10:56:48 +02002649
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002650 /*
2651 * Finally, if the user set the module parameter treat it
2652 * as a user hint.
2653 */
2654 if (!is_world_regdom(ieee80211_regdom))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002655 regulatory_hint_user(ieee80211_regdom,
2656 NL80211_USER_REG_HINT_USER);
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002657
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002658 return 0;
2659}
2660
Johannes Berg1a919312012-12-03 17:21:11 +01002661void regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002662{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002663 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002664 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002665
2666 cancel_work_sync(&reg_work);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002667 cancel_delayed_work_sync(&reg_timeout);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002668
Johannes Berg9027b142012-12-03 17:59:24 +01002669 /* Lock to suppress warnings */
Johannes Berg38fd2142013-05-10 19:17:17 +02002670 rtnl_lock();
Johannes Berg379b82f2012-12-06 15:44:07 +01002671 reset_regdomains(true, NULL);
Johannes Berg38fd2142013-05-10 19:17:17 +02002672 rtnl_unlock();
Johannes Berg734366d2008-09-15 10:56:48 +02002673
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08002674 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02002675
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002676 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002677
Johannes Bergfea9bce2012-12-03 17:32:01 +01002678 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2679 list_del(&reg_beacon->list);
2680 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002681 }
2682
Johannes Bergfea9bce2012-12-03 17:32:01 +01002683 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2684 list_del(&reg_beacon->list);
2685 kfree(reg_beacon);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002686 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002687
Johannes Bergfea9bce2012-12-03 17:32:01 +01002688 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2689 list_del(&reg_request->list);
2690 kfree(reg_request);
Johannes Berg8318d782008-01-24 19:38:38 +01002691 }
Johannes Berg8318d782008-01-24 19:38:38 +01002692}