blob: bf3b0938891db90ef35c2b8a7ea68dd6ea2c1341 [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
Ilan Peer52616f22014-02-25 16:26:00 +020068/**
69 * enum reg_request_treatment - regulatory request treatment
70 *
71 * @REG_REQ_OK: continue processing the regulatory request
72 * @REG_REQ_IGNORE: ignore the regulatory request
73 * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
74 * be intersected with the current one.
75 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
76 * regulatory settings, and no further processing is required.
77 * @REG_REQ_USER_HINT_HANDLED: a non alpha2 user hint was handled and no
78 * further processing is required, i.e., not need to update last_request
79 * etc. This should be used for user hints that do not provide an alpha2
80 * but some other type of regulatory hint, i.e., indoor operation.
81 */
Johannes Berg2f922122012-12-03 17:54:55 +010082enum reg_request_treatment {
83 REG_REQ_OK,
84 REG_REQ_IGNORE,
85 REG_REQ_INTERSECT,
86 REG_REQ_ALREADY_SET,
Ilan Peer52616f22014-02-25 16:26:00 +020087 REG_REQ_USER_HINT_HANDLED,
Johannes Berg2f922122012-12-03 17:54:55 +010088};
89
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050090static struct regulatory_request core_request_world = {
91 .initiator = NL80211_REGDOM_SET_BY_CORE,
92 .alpha2[0] = '0',
93 .alpha2[1] = '0',
94 .intersect = false,
95 .processed = true,
96 .country_ie_env = ENVIRON_ANY,
97};
98
Johannes Berg38fd2142013-05-10 19:17:17 +020099/*
100 * Receipt of information from last regulatory request,
101 * protected by RTNL (and can be accessed with RCU protection)
102 */
Johannes Bergc492db32012-12-06 16:29:25 +0100103static struct regulatory_request __rcu *last_request =
104 (void __rcu *)&core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +0200105
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700106/* To trigger userspace events */
107static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +0100108
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500109/*
110 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +0200111 * the current one and a world regulatory domain in case we have no
Johannes Berge8da2bb2012-12-03 23:00:08 +0100112 * information to give us an alpha2.
Johannes Berg38fd2142013-05-10 19:17:17 +0200113 * (protected by RTNL, can be read under RCU)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500114 */
Johannes Berg458f4f92012-12-06 15:47:38 +0100115const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +0200116
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500117/*
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700118 * Number of devices that registered to the core
119 * that support cellular base station regulatory hints
Johannes Berg38fd2142013-05-10 19:17:17 +0200120 * (protected by RTNL)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700121 */
122static int reg_num_devs_support_basehint;
123
Ilan Peer52616f22014-02-25 16:26:00 +0200124/*
125 * State variable indicating if the platform on which the devices
126 * are attached is operating in an indoor environment. The state variable
127 * is relevant for all registered devices.
128 * (protected by RTNL)
129 */
130static bool reg_is_indoor;
131
Johannes Berg458f4f92012-12-06 15:47:38 +0100132static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
133{
Johannes Berg38fd2142013-05-10 19:17:17 +0200134 return rtnl_dereference(cfg80211_regdomain);
Johannes Berg458f4f92012-12-06 15:47:38 +0100135}
136
137static const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
138{
Johannes Berg38fd2142013-05-10 19:17:17 +0200139 return rtnl_dereference(wiphy->regd);
Johannes Berg458f4f92012-12-06 15:47:38 +0100140}
141
Luis R. Rodriguez3ef121b2013-11-13 18:54:05 +0100142static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
143{
144 switch (dfs_region) {
145 case NL80211_DFS_UNSET:
146 return "unset";
147 case NL80211_DFS_FCC:
148 return "FCC";
149 case NL80211_DFS_ETSI:
150 return "ETSI";
151 case NL80211_DFS_JP:
152 return "JP";
153 }
154 return "Unknown";
155}
156
Luis R. Rodriguez6c474792013-11-25 20:56:09 +0100157enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
158{
159 const struct ieee80211_regdomain *regd = NULL;
160 const struct ieee80211_regdomain *wiphy_regd = NULL;
161
162 regd = get_cfg80211_regdom();
163 if (!wiphy)
164 goto out;
165
166 wiphy_regd = get_wiphy_regdom(wiphy);
167 if (!wiphy_regd)
168 goto out;
169
170 if (wiphy_regd->dfs_region == regd->dfs_region)
171 goto out;
172
173 REG_DBG_PRINT("%s: device specific dfs_region "
174 "(%s) disagrees with cfg80211's "
175 "central dfs_region (%s)\n",
176 dev_name(&wiphy->dev),
177 reg_dfs_region_str(wiphy_regd->dfs_region),
178 reg_dfs_region_str(regd->dfs_region));
179
180out:
181 return regd->dfs_region;
182}
183
Johannes Berg458f4f92012-12-06 15:47:38 +0100184static void rcu_free_regdom(const struct ieee80211_regdomain *r)
185{
186 if (!r)
187 return;
188 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
189}
190
Johannes Bergc492db32012-12-06 16:29:25 +0100191static struct regulatory_request *get_last_request(void)
192{
Johannes Berg38fd2142013-05-10 19:17:17 +0200193 return rcu_dereference_rtnl(last_request);
Johannes Bergc492db32012-12-06 16:29:25 +0100194}
195
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500196/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500197static LIST_HEAD(reg_requests_list);
198static spinlock_t reg_requests_lock;
199
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500200/* Used to queue up beacon hints for review */
201static LIST_HEAD(reg_pending_beacons);
202static spinlock_t reg_pending_beacons_lock;
203
204/* Used to keep track of processed beacon hints */
205static LIST_HEAD(reg_beacon_list);
206
207struct reg_beacon {
208 struct list_head list;
209 struct ieee80211_channel chan;
210};
211
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800212static void reg_todo(struct work_struct *work);
213static DECLARE_WORK(reg_work, reg_todo);
214
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -0700215static void reg_timeout_work(struct work_struct *work);
216static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
217
Johannes Berg734366d2008-09-15 10:56:48 +0200218/* We keep a static world regulatory domain in case of the absence of CRDA */
219static const struct ieee80211_regdomain world_regdom = {
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300220 .n_reg_rules = 6,
Johannes Berg734366d2008-09-15 10:56:48 +0200221 .alpha2 = "00",
222 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500223 /* IEEE 802.11b/g, channels 1..11 */
224 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Johannes Berg43c771a2012-11-12 10:51:34 +0100225 /* IEEE 802.11b/g, channels 12..13. */
226 REG_RULE(2467-10, 2472+10, 40, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200227 NL80211_RRF_NO_IR),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800228 /* IEEE 802.11 channel 14 - Only JP enables
229 * this and for 802.11b only */
230 REG_RULE(2484-10, 2484+10, 20, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200231 NL80211_RRF_NO_IR |
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800232 NL80211_RRF_NO_OFDM),
233 /* IEEE 802.11a, channel 36..48 */
Johannes Berg131a19b2013-03-04 20:54:46 +0100234 REG_RULE(5180-10, 5240+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200235 NL80211_RRF_NO_IR),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500236
Johannes Berg131a19b2013-03-04 20:54:46 +0100237 /* IEEE 802.11a, channel 52..64 - DFS required */
238 REG_RULE(5260-10, 5320+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200239 NL80211_RRF_NO_IR |
Johannes Berg131a19b2013-03-04 20:54:46 +0100240 NL80211_RRF_DFS),
241
242 /* IEEE 802.11a, channel 100..144 - DFS required */
243 REG_RULE(5500-10, 5720+10, 160, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200244 NL80211_RRF_NO_IR |
Johannes Berg131a19b2013-03-04 20:54:46 +0100245 NL80211_RRF_DFS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500246
247 /* IEEE 802.11a, channel 149..165 */
Johannes Berg8ab9d852012-12-03 22:09:22 +0100248 REG_RULE(5745-10, 5825+10, 80, 6, 20,
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200249 NL80211_RRF_NO_IR),
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300250
251 /* IEEE 802.11ad (60gHz), channels 1..3 */
252 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200253 }
254};
255
Johannes Berg38fd2142013-05-10 19:17:17 +0200256/* protected by RTNL */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200257static const struct ieee80211_regdomain *cfg80211_world_regdom =
258 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200259
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400260static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500261static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400262
Johannes Berg734366d2008-09-15 10:56:48 +0200263module_param(ieee80211_regdom, charp, 0444);
264MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
265
Luis R. Rodriguez255e25b2014-02-25 17:09:40 -0800266static void reg_free_request(struct regulatory_request *lr)
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800267{
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -0800268 if (lr != &core_request_world && lr)
269 kfree_rcu(lr, rcu_head);
270}
271
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800272static void reg_update_last_request(struct regulatory_request *request)
273{
Luis R. Rodriguez255e25b2014-02-25 17:09:40 -0800274 struct regulatory_request *lr;
275
276 lr = get_last_request();
277 if (lr == request)
278 return;
279
280 reg_free_request(lr);
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800281 rcu_assign_pointer(last_request, request);
282}
283
Johannes Berg379b82f2012-12-06 15:44:07 +0100284static void reset_regdomains(bool full_reset,
285 const struct ieee80211_regdomain *new_regdom)
Johannes Berg734366d2008-09-15 10:56:48 +0200286{
Johannes Berg458f4f92012-12-06 15:47:38 +0100287 const struct ieee80211_regdomain *r;
288
Johannes Berg38fd2142013-05-10 19:17:17 +0200289 ASSERT_RTNL();
Johannes Berge8da2bb2012-12-03 23:00:08 +0100290
Johannes Berg458f4f92012-12-06 15:47:38 +0100291 r = get_cfg80211_regdom();
292
Johannes Berg942b25c2008-09-15 11:26:47 +0200293 /* avoid freeing static information or freeing something twice */
Johannes Berg458f4f92012-12-06 15:47:38 +0100294 if (r == cfg80211_world_regdom)
295 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200296 if (cfg80211_world_regdom == &world_regdom)
297 cfg80211_world_regdom = NULL;
Johannes Berg458f4f92012-12-06 15:47:38 +0100298 if (r == &world_regdom)
299 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200300
Johannes Berg458f4f92012-12-06 15:47:38 +0100301 rcu_free_regdom(r);
302 rcu_free_regdom(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200303
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200304 cfg80211_world_regdom = &world_regdom;
Johannes Berg458f4f92012-12-06 15:47:38 +0100305 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500306
307 if (!full_reset)
308 return;
309
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -0800310 reg_update_last_request(&core_request_world);
Johannes Berg734366d2008-09-15 10:56:48 +0200311}
312
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500313/*
314 * Dynamic world regulatory domain requested by the wireless
315 * core upon initialization
316 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200317static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200318{
Johannes Bergc492db32012-12-06 16:29:25 +0100319 struct regulatory_request *lr;
Johannes Berg734366d2008-09-15 10:56:48 +0200320
Johannes Bergc492db32012-12-06 16:29:25 +0100321 lr = get_last_request();
322
323 WARN_ON(!lr);
Johannes Berge8da2bb2012-12-03 23:00:08 +0100324
Johannes Berg379b82f2012-12-06 15:44:07 +0100325 reset_regdomains(false, rd);
Johannes Berg734366d2008-09-15 10:56:48 +0200326
327 cfg80211_world_regdom = rd;
Johannes Berg734366d2008-09-15 10:56:48 +0200328}
Johannes Berg734366d2008-09-15 10:56:48 +0200329
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200330bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100331{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700332 if (!alpha2)
333 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100334 return alpha2[0] == '0' && alpha2[1] == '0';
Johannes Berg8318d782008-01-24 19:38:38 +0100335}
336
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200337static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700338{
339 if (!alpha2)
340 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100341 return alpha2[0] && alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700342}
Johannes Berg8318d782008-01-24 19:38:38 +0100343
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200344static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700345{
346 if (!alpha2)
347 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500348 /*
349 * Special case where regulatory domain was built by driver
350 * but a specific alpha2 cannot be determined
351 */
Johannes Berg1a919312012-12-03 17:21:11 +0100352 return alpha2[0] == '9' && alpha2[1] == '9';
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700353}
354
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800355static bool is_intersected_alpha2(const char *alpha2)
356{
357 if (!alpha2)
358 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500359 /*
360 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800361 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500362 * structures
363 */
Johannes Berg1a919312012-12-03 17:21:11 +0100364 return alpha2[0] == '9' && alpha2[1] == '8';
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800365}
366
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200367static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700368{
369 if (!alpha2)
370 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100371 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700372}
373
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200374static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700375{
376 if (!alpha2_x || !alpha2_y)
377 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100378 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700379}
380
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500381static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700382{
Johannes Berg458f4f92012-12-06 15:47:38 +0100383 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500384
Johannes Berg458f4f92012-12-06 15:47:38 +0100385 if (!r)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700386 return true;
Johannes Berg458f4f92012-12-06 15:47:38 +0100387 return !alpha2_equal(r->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388}
389
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500390/*
391 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
392 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
393 * has ever been issued.
394 */
395static bool is_user_regdom_saved(void)
396{
397 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
398 return false;
399
400 /* This would indicate a mistake on the design */
Johannes Berg1a919312012-12-03 17:21:11 +0100401 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500402 "Unexpected user alpha2: %c%c\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100403 user_alpha2[0], user_alpha2[1]))
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500404 return false;
405
406 return true;
407}
408
Johannes Berge9763c32012-12-03 16:59:46 +0100409static const struct ieee80211_regdomain *
410reg_copy_regd(const struct ieee80211_regdomain *src_regd)
John W. Linville3b377ea2009-12-18 17:59:01 -0500411{
412 struct ieee80211_regdomain *regd;
Johannes Berge9763c32012-12-03 16:59:46 +0100413 int size_of_regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500414 unsigned int i;
415
Johannes Berg82f20852012-12-04 12:49:16 +0100416 size_of_regd =
417 sizeof(struct ieee80211_regdomain) +
418 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
John W. Linville3b377ea2009-12-18 17:59:01 -0500419
420 regd = kzalloc(size_of_regd, GFP_KERNEL);
421 if (!regd)
Johannes Berge9763c32012-12-03 16:59:46 +0100422 return ERR_PTR(-ENOMEM);
John W. Linville3b377ea2009-12-18 17:59:01 -0500423
424 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
425
426 for (i = 0; i < src_regd->n_reg_rules; i++)
427 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
Johannes Berge9763c32012-12-03 16:59:46 +0100428 sizeof(struct ieee80211_reg_rule));
John W. Linville3b377ea2009-12-18 17:59:01 -0500429
Johannes Berge9763c32012-12-03 16:59:46 +0100430 return regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500431}
432
433#ifdef CONFIG_CFG80211_INTERNAL_REGDB
434struct reg_regdb_search_request {
435 char alpha2[2];
436 struct list_head list;
437};
438
439static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400440static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500441
442static void reg_regdb_search(struct work_struct *work)
443{
444 struct reg_regdb_search_request *request;
Johannes Berge9763c32012-12-03 16:59:46 +0100445 const struct ieee80211_regdomain *curdom, *regdom = NULL;
446 int i;
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700447
Johannes Berg5fe231e2013-05-08 21:45:15 +0200448 rtnl_lock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500449
John W. Linville368d06f2010-03-16 15:40:59 -0400450 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500451 while (!list_empty(&reg_regdb_search_list)) {
452 request = list_first_entry(&reg_regdb_search_list,
453 struct reg_regdb_search_request,
454 list);
455 list_del(&request->list);
456
Johannes Berg1a919312012-12-03 17:21:11 +0100457 for (i = 0; i < reg_regdb_size; i++) {
John W. Linville3b377ea2009-12-18 17:59:01 -0500458 curdom = reg_regdb[i];
459
Johannes Berg1a919312012-12-03 17:21:11 +0100460 if (alpha2_equal(request->alpha2, curdom->alpha2)) {
Johannes Berge9763c32012-12-03 16:59:46 +0100461 regdom = reg_copy_regd(curdom);
John W. Linville3b377ea2009-12-18 17:59:01 -0500462 break;
463 }
464 }
465
466 kfree(request);
467 }
John W. Linville368d06f2010-03-16 15:40:59 -0400468 mutex_unlock(&reg_regdb_search_mutex);
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700469
Johannes Berge9763c32012-12-03 16:59:46 +0100470 if (!IS_ERR_OR_NULL(regdom))
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700471 set_regdom(regdom);
472
Johannes Berg5fe231e2013-05-08 21:45:15 +0200473 rtnl_unlock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500474}
475
476static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
477
478static void reg_regdb_query(const char *alpha2)
479{
480 struct reg_regdb_search_request *request;
481
482 if (!alpha2)
483 return;
484
485 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
486 if (!request)
487 return;
488
489 memcpy(request->alpha2, alpha2, 2);
490
John W. Linville368d06f2010-03-16 15:40:59 -0400491 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500492 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400493 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500494
495 schedule_work(&reg_regdb_work);
496}
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700497
498/* Feel free to add any other sanity checks here */
499static void reg_regdb_size_check(void)
500{
501 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
502 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
503}
John W. Linville3b377ea2009-12-18 17:59:01 -0500504#else
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700505static inline void reg_regdb_size_check(void) {}
John W. Linville3b377ea2009-12-18 17:59:01 -0500506static inline void reg_regdb_query(const char *alpha2) {}
507#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
508
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500509/*
510 * This lets us keep regulatory code which is updated on a regulatory
Johannes Berg1226d252014-02-25 15:43:36 +0100511 * basis in userspace.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500512 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700513static int call_crda(const char *alpha2)
514{
Johannes Berg1226d252014-02-25 15:43:36 +0100515 char country[12];
516 char *env[] = { country, NULL };
517
518 snprintf(country, sizeof(country), "COUNTRY=%c%c",
519 alpha2[0], alpha2[1]);
520
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700521 if (!is_world_regdom((char *) alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -0800522 pr_info("Calling CRDA for country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700523 alpha2[0], alpha2[1]);
524 else
Joe Perchese9c02682010-11-16 19:56:49 -0800525 pr_info("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700526
John W. Linville3b377ea2009-12-18 17:59:01 -0500527 /* query internal regulatory database (if it exists) */
528 reg_regdb_query(alpha2);
529
Johannes Berg1226d252014-02-25 15:43:36 +0100530 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700531}
532
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -0800533static enum reg_request_treatment
534reg_call_crda(struct regulatory_request *request)
535{
536 if (call_crda(request->alpha2))
537 return REG_REQ_IGNORE;
538 return REG_REQ_OK;
539}
540
Luis R. Rodrigueze4387682013-11-05 09:18:01 -0800541bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700542{
Johannes Bergc492db32012-12-06 16:29:25 +0100543 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400544
Johannes Bergc492db32012-12-06 16:29:25 +0100545 if (!lr || lr->processed)
Johannes Bergf6037d02008-10-21 11:01:33 +0200546 return false;
547
Johannes Bergc492db32012-12-06 16:29:25 +0100548 return alpha2_equal(lr->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700549}
550
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100551static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
552{
553 struct regulatory_request *lr = get_last_request();
554
555 /*
556 * Follow the driver's regulatory domain, if present, unless a country
557 * IE has been processed or a user wants to help complaince further
558 */
559 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
560 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
561 wiphy->regd)
562 return get_wiphy_regdom(wiphy);
563
564 return get_cfg80211_regdom();
565}
566
Janusz Dziedzic97524822014-01-30 09:52:20 +0100567unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
568 const struct ieee80211_reg_rule *rule)
569{
570 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
571 const struct ieee80211_freq_range *freq_range_tmp;
572 const struct ieee80211_reg_rule *tmp;
573 u32 start_freq, end_freq, idx, no;
574
575 for (idx = 0; idx < rd->n_reg_rules; idx++)
576 if (rule == &rd->reg_rules[idx])
577 break;
578
579 if (idx == rd->n_reg_rules)
580 return 0;
581
582 /* get start_freq */
583 no = idx;
584
585 while (no) {
586 tmp = &rd->reg_rules[--no];
587 freq_range_tmp = &tmp->freq_range;
588
589 if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
590 break;
591
Janusz Dziedzic97524822014-01-30 09:52:20 +0100592 freq_range = freq_range_tmp;
593 }
594
595 start_freq = freq_range->start_freq_khz;
596
597 /* get end_freq */
598 freq_range = &rule->freq_range;
599 no = idx;
600
601 while (no < rd->n_reg_rules - 1) {
602 tmp = &rd->reg_rules[++no];
603 freq_range_tmp = &tmp->freq_range;
604
605 if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
606 break;
607
Janusz Dziedzic97524822014-01-30 09:52:20 +0100608 freq_range = freq_range_tmp;
609 }
610
611 end_freq = freq_range->end_freq_khz;
612
613 return end_freq - start_freq;
614}
615
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700616/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200617static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700618{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200619 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700620 u32 freq_diff;
621
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800622 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700623 return false;
624
625 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
626 return false;
627
628 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
629
Roel Kluinbd05f282009-03-03 22:55:21 +0100630 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg1a919312012-12-03 17:21:11 +0100631 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700632 return false;
633
634 return true;
635}
636
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200637static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700638{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200639 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700640 unsigned int i;
641
642 if (!rd->n_reg_rules)
643 return false;
644
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800645 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
646 return false;
647
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700648 for (i = 0; i < rd->n_reg_rules; i++) {
649 reg_rule = &rd->reg_rules[i];
650 if (!is_valid_reg_rule(reg_rule))
651 return false;
652 }
653
654 return true;
655}
656
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400657static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100658 u32 center_freq_khz, u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700659{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400660 u32 start_freq_khz, end_freq_khz;
661
662 start_freq_khz = center_freq_khz - (bw_khz/2);
663 end_freq_khz = center_freq_khz + (bw_khz/2);
664
665 if (start_freq_khz >= freq_range->start_freq_khz &&
666 end_freq_khz <= freq_range->end_freq_khz)
667 return true;
668
669 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700670}
671
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800672/**
673 * freq_in_rule_band - tells us if a frequency is in a frequency band
674 * @freq_range: frequency rule we want to query
675 * @freq_khz: frequency we are inquiring about
676 *
677 * This lets us know if a specific frequency rule is or is not relevant to
678 * a specific frequency's band. Bands are device specific and artificial
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200679 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
680 * however it is safe for now to assume that a frequency rule should not be
681 * part of a frequency's band if the start freq or end freq are off by more
682 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
683 * 60 GHz band.
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800684 * This resolution can be lowered and should be considered as we add
685 * regulatory rule support for other "bands".
686 **/
687static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg1a919312012-12-03 17:21:11 +0100688 u32 freq_khz)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800689{
690#define ONE_GHZ_IN_KHZ 1000000
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200691 /*
692 * From 802.11ad: directional multi-gigabit (DMG):
693 * Pertaining to operation in a frequency band containing a channel
694 * with the Channel starting frequency above 45 GHz.
695 */
696 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
697 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
698 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800699 return true;
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200700 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800701 return true;
702 return false;
703#undef ONE_GHZ_IN_KHZ
704}
705
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500706/*
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100707 * Later on we can perhaps use the more restrictive DFS
708 * region but we don't have information for that yet so
709 * for now simply disallow conflicts.
710 */
711static enum nl80211_dfs_regions
712reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
713 const enum nl80211_dfs_regions dfs_region2)
714{
715 if (dfs_region1 != dfs_region2)
716 return NL80211_DFS_UNSET;
717 return dfs_region1;
718}
719
720/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500721 * Helper for regdom_intersect(), this does the real
722 * mathematical intersection fun
723 */
Janusz Dziedzic97524822014-01-30 09:52:20 +0100724static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
725 const struct ieee80211_regdomain *rd2,
726 const struct ieee80211_reg_rule *rule1,
Johannes Berg1a919312012-12-03 17:21:11 +0100727 const struct ieee80211_reg_rule *rule2,
728 struct ieee80211_reg_rule *intersected_rule)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700729{
730 const struct ieee80211_freq_range *freq_range1, *freq_range2;
731 struct ieee80211_freq_range *freq_range;
732 const struct ieee80211_power_rule *power_rule1, *power_rule2;
733 struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +0100734 u32 freq_diff, max_bandwidth1, max_bandwidth2;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700735
736 freq_range1 = &rule1->freq_range;
737 freq_range2 = &rule2->freq_range;
738 freq_range = &intersected_rule->freq_range;
739
740 power_rule1 = &rule1->power_rule;
741 power_rule2 = &rule2->power_rule;
742 power_rule = &intersected_rule->power_rule;
743
744 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100745 freq_range2->start_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700746 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100747 freq_range2->end_freq_khz);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100748
749 max_bandwidth1 = freq_range1->max_bandwidth_khz;
750 max_bandwidth2 = freq_range2->max_bandwidth_khz;
751
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100752 if (rule1->flags & NL80211_RRF_AUTO_BW)
753 max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
754 if (rule2->flags & NL80211_RRF_AUTO_BW)
755 max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
Janusz Dziedzic97524822014-01-30 09:52:20 +0100756
757 freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700758
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100759 intersected_rule->flags = rule1->flags | rule2->flags;
760
761 /*
762 * In case NL80211_RRF_AUTO_BW requested for both rules
763 * set AUTO_BW in intersected rule also. Next we will
764 * calculate BW correctly in handle_channel function.
765 * In other case remove AUTO_BW flag while we calculate
766 * maximum bandwidth correctly and auto calculation is
767 * not required.
768 */
769 if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
770 (rule2->flags & NL80211_RRF_AUTO_BW))
771 intersected_rule->flags |= NL80211_RRF_AUTO_BW;
772 else
773 intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
774
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700775 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
776 if (freq_range->max_bandwidth_khz > freq_diff)
777 freq_range->max_bandwidth_khz = freq_diff;
778
779 power_rule->max_eirp = min(power_rule1->max_eirp,
780 power_rule2->max_eirp);
781 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
782 power_rule2->max_antenna_gain);
783
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100784 intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
785 rule2->dfs_cac_ms);
786
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700787 if (!is_valid_reg_rule(intersected_rule))
788 return -EINVAL;
789
790 return 0;
791}
792
793/**
794 * regdom_intersect - do the intersection between two regulatory domains
795 * @rd1: first regulatory domain
796 * @rd2: second regulatory domain
797 *
798 * Use this function to get the intersection between two regulatory domains.
799 * Once completed we will mark the alpha2 for the rd as intersected, "98",
800 * as no one single alpha2 can represent this regulatory domain.
801 *
802 * Returns a pointer to the regulatory domain structure which will hold the
803 * resulting intersection of rules between rd1 and rd2. We will
804 * kzalloc() this structure for you.
805 */
Johannes Berg1a919312012-12-03 17:21:11 +0100806static struct ieee80211_regdomain *
807regdom_intersect(const struct ieee80211_regdomain *rd1,
808 const struct ieee80211_regdomain *rd2)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700809{
810 int r, size_of_regd;
811 unsigned int x, y;
812 unsigned int num_rules = 0, rule_idx = 0;
813 const struct ieee80211_reg_rule *rule1, *rule2;
814 struct ieee80211_reg_rule *intersected_rule;
815 struct ieee80211_regdomain *rd;
816 /* This is just a dummy holder to help us count */
Johannes Berg74f53cd2012-12-06 17:26:17 +0100817 struct ieee80211_reg_rule dummy_rule;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700818
819 if (!rd1 || !rd2)
820 return NULL;
821
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500822 /*
823 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700824 * build them. This is to so we can malloc() and free() a
825 * regdomain once. The reason we use reg_rules_intersect() here
826 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500827 * All rules that do check out OK are valid.
828 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700829
830 for (x = 0; x < rd1->n_reg_rules; x++) {
831 rule1 = &rd1->reg_rules[x];
832 for (y = 0; y < rd2->n_reg_rules; y++) {
833 rule2 = &rd2->reg_rules[y];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100834 if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
835 &dummy_rule))
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700836 num_rules++;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700837 }
838 }
839
840 if (!num_rules)
841 return NULL;
842
843 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg82f20852012-12-04 12:49:16 +0100844 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700845
846 rd = kzalloc(size_of_regd, GFP_KERNEL);
847 if (!rd)
848 return NULL;
849
Johannes Berg8a57fff2012-12-06 17:03:17 +0100850 for (x = 0; x < rd1->n_reg_rules && rule_idx < num_rules; x++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700851 rule1 = &rd1->reg_rules[x];
Johannes Berg8a57fff2012-12-06 17:03:17 +0100852 for (y = 0; y < rd2->n_reg_rules && rule_idx < num_rules; y++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700853 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500854 /*
855 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700856 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500857 * a memcpy()
858 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700859 intersected_rule = &rd->reg_rules[rule_idx];
Janusz Dziedzic97524822014-01-30 09:52:20 +0100860 r = reg_rules_intersect(rd1, rd2, rule1, rule2,
861 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500862 /*
863 * No need to memset here the intersected rule here as
864 * we're not using the stack anymore
865 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700866 if (r)
867 continue;
868 rule_idx++;
869 }
870 }
871
872 if (rule_idx != num_rules) {
873 kfree(rd);
874 return NULL;
875 }
876
877 rd->n_reg_rules = num_rules;
878 rd->alpha2[0] = '9';
879 rd->alpha2[1] = '8';
Luis R. Rodriguezadbfb052013-11-13 18:54:03 +0100880 rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
881 rd2->dfs_region);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700882
883 return rd;
884}
885
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500886/*
887 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
888 * want to just have the channel structure use these
889 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700890static u32 map_regdom_flags(u32 rd_flags)
891{
892 u32 channel_flags = 0;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200893 if (rd_flags & NL80211_RRF_NO_IR_ALL)
894 channel_flags |= IEEE80211_CHAN_NO_IR;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700895 if (rd_flags & NL80211_RRF_DFS)
896 channel_flags |= IEEE80211_CHAN_RADAR;
Seth Forshee03f6b082012-08-01 15:58:42 -0500897 if (rd_flags & NL80211_RRF_NO_OFDM)
898 channel_flags |= IEEE80211_CHAN_NO_OFDM;
David Spinadel570dbde2014-02-23 09:12:59 +0200899 if (rd_flags & NL80211_RRF_NO_OUTDOOR)
900 channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700901 return channel_flags;
902}
903
Johannes Berg361c9c82012-12-06 15:57:14 +0100904static const struct ieee80211_reg_rule *
905freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
906 const struct ieee80211_regdomain *regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100907{
908 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800909 bool band_rule_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400910 bool bw_fits = false;
911
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800912 if (!regd)
Johannes Berg361c9c82012-12-06 15:57:14 +0100913 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700914
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800915 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700916 const struct ieee80211_reg_rule *rr;
917 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700918
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800919 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700920 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800921
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500922 /*
923 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800924 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500925 * not overwrite it once found
926 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800927 if (!band_rule_found)
928 band_rule_found = freq_in_rule_band(fr, center_freq);
929
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100930 bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800931
Johannes Berg361c9c82012-12-06 15:57:14 +0100932 if (band_rule_found && bw_fits)
933 return rr;
Johannes Berg8318d782008-01-24 19:38:38 +0100934 }
935
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800936 if (!band_rule_found)
Johannes Berg361c9c82012-12-06 15:57:14 +0100937 return ERR_PTR(-ERANGE);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800938
Johannes Berg361c9c82012-12-06 15:57:14 +0100939 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700940}
941
Johannes Berg361c9c82012-12-06 15:57:14 +0100942const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
943 u32 center_freq)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800944{
Johannes Berg5d885b92012-12-04 00:14:17 +0100945 const struct ieee80211_regdomain *regd;
Johannes Berg1a919312012-12-03 17:21:11 +0100946
Janusz Dziedzice3961af2014-01-25 11:24:11 +0100947 regd = reg_get_regdomain(wiphy);
Johannes Berg5d885b92012-12-04 00:14:17 +0100948
Johannes Berg361c9c82012-12-06 15:57:14 +0100949 return freq_reg_info_regd(wiphy, center_freq, regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800950}
John W. Linville4f366c52010-07-15 14:57:33 -0400951EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700952
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700953const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530954{
955 switch (initiator) {
956 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700957 return "core";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530958 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700959 return "user";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530960 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700961 return "driver";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530962 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700963 return "country IE";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530964 default:
965 WARN_ON(1);
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700966 return "bug";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530967 }
968}
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700969EXPORT_SYMBOL(reg_initiator_name);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530970
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700971#ifdef CONFIG_CFG80211_REG_DEBUG
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100972static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
973 struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530974 const struct ieee80211_reg_rule *reg_rule)
975{
976 const struct ieee80211_power_rule *power_rule;
977 const struct ieee80211_freq_range *freq_range;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100978 char max_antenna_gain[32], bw[32];
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530979
980 power_rule = &reg_rule->power_rule;
981 freq_range = &reg_rule->freq_range;
982
983 if (!power_rule->max_antenna_gain)
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100984 snprintf(max_antenna_gain, sizeof(max_antenna_gain), "N/A");
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530985 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +0100986 snprintf(max_antenna_gain, sizeof(max_antenna_gain), "%d",
987 power_rule->max_antenna_gain);
988
989 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
990 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
991 freq_range->max_bandwidth_khz,
992 reg_get_max_bandwidth(regd, reg_rule));
993 else
994 snprintf(bw, sizeof(bw), "%d KHz",
995 freq_range->max_bandwidth_khz);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530996
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100997 REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
998 chan->center_freq);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530999
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001000 REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)\n",
Johannes Berg1a919312012-12-03 17:21:11 +01001001 freq_range->start_freq_khz, freq_range->end_freq_khz,
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001002 bw, max_antenna_gain,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301003 power_rule->max_eirp);
1004}
1005#else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001006static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
1007 struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301008 const struct ieee80211_reg_rule *reg_rule)
1009{
1010 return;
1011}
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301012#endif
1013
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001014/*
1015 * Note that right now we assume the desired channel bandwidth
1016 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001017 * per channel, the primary and the extension channel).
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001018 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001019static void handle_channel(struct wiphy *wiphy,
1020 enum nl80211_reg_initiator initiator,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001021 struct ieee80211_channel *chan)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001022{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001023 u32 flags, bw_flags = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001024 const struct ieee80211_reg_rule *reg_rule = NULL;
1025 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001026 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001027 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01001028 struct regulatory_request *lr = get_last_request();
Janusz Dziedzic97524822014-01-30 09:52:20 +01001029 const struct ieee80211_regdomain *regd;
1030 u32 max_bandwidth_khz;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001031
Johannes Bergc492db32012-12-06 16:29:25 +01001032 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001033
1034 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001035
Johannes Berg361c9c82012-12-06 15:57:14 +01001036 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1037 if (IS_ERR(reg_rule)) {
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001038 /*
1039 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001040 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001041 * from a Country IE and the Country IE had no information
1042 * about a band. The IEEE 802.11 spec allows for an AP
1043 * to send only a subset of the regulatory rules allowed,
1044 * so an AP in the US that only supports 2.4 GHz may only send
1045 * a country IE with information for the 2.4 GHz band
1046 * while 5 GHz is still supported.
1047 */
1048 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg361c9c82012-12-06 15:57:14 +01001049 PTR_ERR(reg_rule) == -ERANGE)
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001050 return;
1051
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001052 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1053 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001054 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001055 REG_DBG_PRINT("Disabling freq %d MHz for good\n",
1056 chan->center_freq);
1057 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1058 chan->flags = chan->orig_flags;
1059 } else {
1060 REG_DBG_PRINT("Disabling freq %d MHz\n",
1061 chan->center_freq);
1062 chan->flags |= IEEE80211_CHAN_DISABLED;
1063 }
Johannes Berg8318d782008-01-24 19:38:38 +01001064 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -07001065 }
Johannes Berg8318d782008-01-24 19:38:38 +01001066
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001067 regd = reg_get_regdomain(wiphy);
1068 chan_reg_rule_print_dbg(regd, chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301069
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001070 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001071 freq_range = &reg_rule->freq_range;
1072
Janusz Dziedzic97524822014-01-30 09:52:20 +01001073 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1074 /* Check if auto calculation requested */
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001075 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001076 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
Janusz Dziedzic97524822014-01-30 09:52:20 +01001077
1078 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001079 bw_flags = IEEE80211_CHAN_NO_HT40;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001080 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001081 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001082 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001083 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001084
Johannes Bergc492db32012-12-06 16:29:25 +01001085 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001086 request_wiphy && request_wiphy == wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001087 request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001088 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001089 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001090 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001091 * settings
1092 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001093 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001094 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001095 chan->max_antenna_gain = chan->orig_mag =
1096 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001097 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001098 (int) MBM_TO_DBM(power_rule->max_eirp);
1099 return;
1100 }
1101
Simon Wunderlich04f39042013-02-08 18:16:19 +01001102 chan->dfs_state = NL80211_DFS_USABLE;
1103 chan->dfs_state_entered = jiffies;
1104
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +05301105 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001106 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg1a919312012-12-03 17:21:11 +01001107 chan->max_antenna_gain =
1108 min_t(int, chan->orig_mag,
1109 MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +02001110 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Janusz Dziedzic089027e2014-02-21 19:46:12 +01001111
1112 if (chan->flags & IEEE80211_CHAN_RADAR) {
1113 if (reg_rule->dfs_cac_ms)
1114 chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
1115 else
1116 chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
1117 }
1118
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001119 if (chan->orig_mpwr) {
1120 /*
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001121 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
1122 * will always follow the passed country IE power settings.
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001123 */
1124 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza09a85a2013-11-11 22:15:30 +01001125 wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +02001126 chan->max_power = chan->max_reg_power;
1127 else
1128 chan->max_power = min(chan->orig_mpwr,
1129 chan->max_reg_power);
1130 } else
1131 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +01001132}
1133
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001134static void handle_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001135 enum nl80211_reg_initiator initiator,
1136 struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +01001137{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001138 unsigned int i;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001139
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001140 if (!sband)
1141 return;
Johannes Berg8318d782008-01-24 19:38:38 +01001142
1143 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001144 handle_channel(wiphy, initiator, &sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +01001145}
1146
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001147static bool reg_request_cell_base(struct regulatory_request *request)
1148{
1149 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1150 return false;
Johannes Berg1a919312012-12-03 17:21:11 +01001151 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001152}
1153
Ilan Peer52616f22014-02-25 16:26:00 +02001154static bool reg_request_indoor(struct regulatory_request *request)
1155{
1156 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1157 return false;
1158 return request->user_reg_hint_type == NL80211_USER_REG_HINT_INDOOR;
1159}
1160
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001161bool reg_last_request_cell_base(void)
1162{
Johannes Berg38fd2142013-05-10 19:17:17 +02001163 return reg_request_cell_base(get_last_request());
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001164}
1165
Ilan Peer94fc6612014-02-23 09:13:00 +02001166#ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001167/* Core specific check */
Johannes Berg2f922122012-12-03 17:54:55 +01001168static enum reg_request_treatment
1169reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001170{
Johannes Bergc492db32012-12-06 16:29:25 +01001171 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001172
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001173 if (!reg_num_devs_support_basehint)
Johannes Berg2f922122012-12-03 17:54:55 +01001174 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001175
Johannes Bergc492db32012-12-06 16:29:25 +01001176 if (reg_request_cell_base(lr) &&
Johannes Berg1a919312012-12-03 17:21:11 +01001177 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001178 return REG_REQ_ALREADY_SET;
Johannes Berg1a919312012-12-03 17:21:11 +01001179
Johannes Berg2f922122012-12-03 17:54:55 +01001180 return REG_REQ_OK;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001181}
1182
1183/* Device specific check */
1184static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
1185{
Johannes Berg1a919312012-12-03 17:21:11 +01001186 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001187}
1188#else
1189static int reg_ignore_cell_hint(struct regulatory_request *pending_request)
1190{
Johannes Berg2f922122012-12-03 17:54:55 +01001191 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001192}
Johannes Berg1a919312012-12-03 17:21:11 +01001193
1194static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001195{
1196 return true;
1197}
1198#endif
1199
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001200static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
1201{
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001202 if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
1203 !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001204 return true;
1205 return false;
1206}
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001207
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001208static bool ignore_reg_update(struct wiphy *wiphy,
1209 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001210{
Johannes Bergc492db32012-12-06 16:29:25 +01001211 struct regulatory_request *lr = get_last_request();
1212
1213 if (!lr) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001214 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1215 "since last_request is not set\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301216 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001217 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301218 }
1219
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001220 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001221 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001222 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1223 "since the driver uses its own custom "
1224 "regulatory domain\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301225 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001226 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301227 }
1228
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001229 /*
1230 * wiphy->regd will be set once the device has its own
1231 * desired regulatory domain set
1232 */
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001233 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -07001234 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Bergc492db32012-12-06 16:29:25 +01001235 !is_world_regdom(lr->alpha2)) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001236 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1237 "since the driver requires its own regulatory "
1238 "domain to be set first\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301239 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001240 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301241 }
1242
Johannes Bergc492db32012-12-06 16:29:25 +01001243 if (reg_request_cell_base(lr))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001244 return reg_dev_ignore_cell_hint(wiphy);
1245
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001246 return false;
1247}
1248
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001249static bool reg_is_world_roaming(struct wiphy *wiphy)
1250{
1251 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1252 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1253 struct regulatory_request *lr = get_last_request();
1254
1255 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1256 return true;
1257
1258 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001259 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001260 return true;
1261
1262 return false;
1263}
1264
Johannes Berg1a919312012-12-03 17:21:11 +01001265static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001266 struct reg_beacon *reg_beacon)
1267{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001268 struct ieee80211_supported_band *sband;
1269 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001270 bool channel_changed = false;
1271 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001272
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001273 sband = wiphy->bands[reg_beacon->chan.band];
1274 chan = &sband->channels[chan_idx];
1275
1276 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1277 return;
1278
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001279 if (chan->beacon_found)
1280 return;
1281
1282 chan->beacon_found = true;
1283
Luis R. Rodriguez0f500a52012-12-19 10:53:04 -08001284 if (!reg_is_world_roaming(wiphy))
1285 return;
1286
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001287 if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001288 return;
1289
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001290 chan_before.center_freq = chan->center_freq;
1291 chan_before.flags = chan->flags;
1292
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001293 if (chan->flags & IEEE80211_CHAN_NO_IR) {
1294 chan->flags &= ~IEEE80211_CHAN_NO_IR;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001295 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001296 }
1297
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001298 if (channel_changed)
1299 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001300}
1301
1302/*
1303 * Called when a scan on a wiphy finds a beacon on
1304 * new channel
1305 */
1306static void wiphy_update_new_beacon(struct wiphy *wiphy,
1307 struct reg_beacon *reg_beacon)
1308{
1309 unsigned int i;
1310 struct ieee80211_supported_band *sband;
1311
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001312 if (!wiphy->bands[reg_beacon->chan.band])
1313 return;
1314
1315 sband = wiphy->bands[reg_beacon->chan.band];
1316
1317 for (i = 0; i < sband->n_channels; i++)
1318 handle_reg_beacon(wiphy, i, reg_beacon);
1319}
1320
1321/*
1322 * Called upon reg changes or a new wiphy is added
1323 */
1324static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1325{
1326 unsigned int i;
1327 struct ieee80211_supported_band *sband;
1328 struct reg_beacon *reg_beacon;
1329
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001330 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1331 if (!wiphy->bands[reg_beacon->chan.band])
1332 continue;
1333 sband = wiphy->bands[reg_beacon->chan.band];
1334 for (i = 0; i < sband->n_channels; i++)
1335 handle_reg_beacon(wiphy, i, reg_beacon);
1336 }
1337}
1338
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001339/* Reap the advantages of previously found beacons */
1340static void reg_process_beacons(struct wiphy *wiphy)
1341{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001342 /*
1343 * Means we are just firing up cfg80211, so no beacons would
1344 * have been processed yet.
1345 */
1346 if (!last_request)
1347 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001348 wiphy_update_beacon_reg(wiphy);
1349}
1350
Johannes Berg1a919312012-12-03 17:21:11 +01001351static bool is_ht40_allowed(struct ieee80211_channel *chan)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001352{
1353 if (!chan)
Johannes Berg1a919312012-12-03 17:21:11 +01001354 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001355 if (chan->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg1a919312012-12-03 17:21:11 +01001356 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001357 /* This would happen when regulatory rules disallow HT40 completely */
Felix Fietkau55b183a2013-01-11 14:22:58 +01001358 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1359 return false;
1360 return true;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001361}
1362
1363static void reg_process_ht_flags_channel(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001364 struct ieee80211_channel *channel)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001365{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001366 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001367 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1368 unsigned int i;
1369
Johannes Berg1a919312012-12-03 17:21:11 +01001370 if (!is_ht40_allowed(channel)) {
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001371 channel->flags |= IEEE80211_CHAN_NO_HT40;
1372 return;
1373 }
1374
1375 /*
1376 * We need to ensure the extension channels exist to
1377 * be able to use HT40- or HT40+, this finds them (or not)
1378 */
1379 for (i = 0; i < sband->n_channels; i++) {
1380 struct ieee80211_channel *c = &sband->channels[i];
Johannes Berg1a919312012-12-03 17:21:11 +01001381
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001382 if (c->center_freq == (channel->center_freq - 20))
1383 channel_before = c;
1384 if (c->center_freq == (channel->center_freq + 20))
1385 channel_after = c;
1386 }
1387
1388 /*
1389 * Please note that this assumes target bandwidth is 20 MHz,
1390 * if that ever changes we also need to change the below logic
1391 * to include that as well.
1392 */
Johannes Berg1a919312012-12-03 17:21:11 +01001393 if (!is_ht40_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001394 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001395 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001396 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001397
Johannes Berg1a919312012-12-03 17:21:11 +01001398 if (!is_ht40_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001399 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001400 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001401 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001402}
1403
1404static void reg_process_ht_flags_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001405 struct ieee80211_supported_band *sband)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001406{
1407 unsigned int i;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001408
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001409 if (!sband)
1410 return;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001411
1412 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001413 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001414}
1415
1416static void reg_process_ht_flags(struct wiphy *wiphy)
1417{
1418 enum ieee80211_band band;
1419
1420 if (!wiphy)
1421 return;
1422
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001423 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1424 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001425}
1426
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001427static void reg_call_notifier(struct wiphy *wiphy,
1428 struct regulatory_request *request)
1429{
1430 if (wiphy->reg_notifier)
1431 wiphy->reg_notifier(wiphy, request);
1432}
1433
Sven Neumanneac03e32011-08-30 23:38:53 +02001434static void wiphy_update_regulatory(struct wiphy *wiphy,
1435 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001436{
1437 enum ieee80211_band band;
Johannes Bergc492db32012-12-06 16:29:25 +01001438 struct regulatory_request *lr = get_last_request();
Sven Neumanneac03e32011-08-30 23:38:53 +02001439
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001440 if (ignore_reg_update(wiphy, initiator)) {
1441 /*
1442 * Regulatory updates set by CORE are ignored for custom
1443 * regulatory cards. Let us notify the changes to the driver,
1444 * as some drivers used this to restore its orig_* reg domain.
1445 */
1446 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001447 wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001448 reg_call_notifier(wiphy, lr);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001449 return;
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001450 }
Sven Neumanna203c2a2011-07-12 15:52:07 +02001451
Johannes Bergc492db32012-12-06 16:29:25 +01001452 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07001453
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001454 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1455 handle_band(wiphy, initiator, wiphy->bands[band]);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001456
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001457 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001458 reg_process_ht_flags(wiphy);
Luis R. Rodriguez0e3802d2013-11-05 09:18:12 -08001459 reg_call_notifier(wiphy, lr);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001460}
1461
Sven Neumannd7549cb2011-08-30 23:38:54 +02001462static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1463{
1464 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301465 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02001466
Johannes Berg5fe231e2013-05-08 21:45:15 +02001467 ASSERT_RTNL();
Johannes Berg458f4f92012-12-06 15:47:38 +01001468
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301469 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1470 wiphy = &rdev->wiphy;
1471 wiphy_update_regulatory(wiphy, initiator);
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301472 }
Sven Neumannd7549cb2011-08-30 23:38:54 +02001473}
1474
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001475static void handle_channel_custom(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001476 struct ieee80211_channel *chan,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001477 const struct ieee80211_regdomain *regd)
1478{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001479 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001480 const struct ieee80211_reg_rule *reg_rule = NULL;
1481 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001482 const struct ieee80211_freq_range *freq_range = NULL;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001483 u32 max_bandwidth_khz;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001484
Johannes Berg361c9c82012-12-06 15:57:14 +01001485 reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1486 regd);
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001487
Johannes Berg361c9c82012-12-06 15:57:14 +01001488 if (IS_ERR(reg_rule)) {
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001489 REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1490 chan->center_freq);
Luis R. Rodriguezcc493e4f2013-11-06 17:54:44 +01001491 chan->orig_flags |= IEEE80211_CHAN_DISABLED;
1492 chan->flags = chan->orig_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001493 return;
1494 }
1495
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001496 chan_reg_rule_print_dbg(regd, chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301497
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001498 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001499 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001500
Janusz Dziedzic97524822014-01-30 09:52:20 +01001501 max_bandwidth_khz = freq_range->max_bandwidth_khz;
1502 /* Check if auto calculation requested */
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01001503 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
Janusz Dziedzic97524822014-01-30 09:52:20 +01001504 max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
1505
1506 if (max_bandwidth_khz < MHZ_TO_KHZ(40))
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001507 bw_flags = IEEE80211_CHAN_NO_HT40;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001508 if (max_bandwidth_khz < MHZ_TO_KHZ(80))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001509 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
Janusz Dziedzic97524822014-01-30 09:52:20 +01001510 if (max_bandwidth_khz < MHZ_TO_KHZ(160))
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001511 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001512
1513 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001514 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001515 chan->max_reg_power = chan->max_power =
1516 (int) MBM_TO_DBM(power_rule->max_eirp);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001517}
1518
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001519static void handle_band_custom(struct wiphy *wiphy,
1520 struct ieee80211_supported_band *sband,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001521 const struct ieee80211_regdomain *regd)
1522{
1523 unsigned int i;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001524
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001525 if (!sband)
1526 return;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001527
1528 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001529 handle_channel_custom(wiphy, &sband->channels[i], regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001530}
1531
1532/* Used by drivers prior to wiphy registration */
1533void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1534 const struct ieee80211_regdomain *regd)
1535{
1536 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001537 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001538
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001539 WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
1540 "wiphy should have REGULATORY_CUSTOM_REG\n");
1541 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
Luis R. Rodriguez222ea582013-11-05 09:18:00 -08001542
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001543 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001544 if (!wiphy->bands[band])
1545 continue;
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001546 handle_band_custom(wiphy, wiphy->bands[band], regd);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001547 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001548 }
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001549
1550 /*
1551 * no point in calling this if it won't have any effect
Johannes Berg1a919312012-12-03 17:21:11 +01001552 * on your device's supported bands.
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001553 */
1554 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001555}
1556EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1557
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001558static void reg_set_request_processed(void)
1559{
1560 bool need_more_processing = false;
Johannes Bergc492db32012-12-06 16:29:25 +01001561 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001562
Johannes Bergc492db32012-12-06 16:29:25 +01001563 lr->processed = true;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001564
1565 spin_lock(&reg_requests_lock);
1566 if (!list_empty(&reg_requests_list))
1567 need_more_processing = true;
1568 spin_unlock(&reg_requests_lock);
1569
Johannes Bergc492db32012-12-06 16:29:25 +01001570 if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
Eliad Pellerfe20b392012-06-12 12:53:13 +03001571 cancel_delayed_work(&reg_timeout);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001572
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001573 if (need_more_processing)
1574 schedule_work(&reg_work);
1575}
1576
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001577/**
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001578 * reg_process_hint_core - process core regulatory requests
1579 * @pending_request: a pending core regulatory request
1580 *
1581 * The wireless subsystem can use this function to process
1582 * a regulatory request issued by the regulatory core.
1583 *
1584 * Returns one of the different reg request treatment values.
1585 */
1586static enum reg_request_treatment
1587reg_process_hint_core(struct regulatory_request *core_request)
1588{
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001589
1590 core_request->intersect = false;
1591 core_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001592
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001593 reg_update_last_request(core_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001594
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001595 return reg_call_crda(core_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001596}
1597
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001598static enum reg_request_treatment
1599__reg_process_hint_user(struct regulatory_request *user_request)
1600{
1601 struct regulatory_request *lr = get_last_request();
1602
Ilan Peer52616f22014-02-25 16:26:00 +02001603 if (reg_request_indoor(user_request)) {
1604 reg_is_indoor = true;
1605 return REG_REQ_USER_HINT_HANDLED;
1606 }
1607
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001608 if (reg_request_cell_base(user_request))
1609 return reg_ignore_cell_hint(user_request);
1610
1611 if (reg_request_cell_base(lr))
1612 return REG_REQ_IGNORE;
1613
1614 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1615 return REG_REQ_INTERSECT;
1616 /*
1617 * If the user knows better the user should set the regdom
1618 * to their country before the IE is picked up
1619 */
1620 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
1621 lr->intersect)
1622 return REG_REQ_IGNORE;
1623 /*
1624 * Process user requests only after previous user/driver/core
1625 * requests have been processed
1626 */
1627 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
1628 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1629 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
1630 regdom_changes(lr->alpha2))
1631 return REG_REQ_IGNORE;
1632
1633 if (!regdom_changes(user_request->alpha2))
1634 return REG_REQ_ALREADY_SET;
1635
1636 return REG_REQ_OK;
1637}
1638
1639/**
1640 * reg_process_hint_user - process user regulatory requests
1641 * @user_request: a pending user regulatory request
1642 *
1643 * The wireless subsystem can use this function to process
1644 * a regulatory request initiated by userspace.
1645 *
1646 * Returns one of the different reg request treatment values.
1647 */
1648static enum reg_request_treatment
1649reg_process_hint_user(struct regulatory_request *user_request)
1650{
1651 enum reg_request_treatment treatment;
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001652
1653 treatment = __reg_process_hint_user(user_request);
1654 if (treatment == REG_REQ_IGNORE ||
Ilan Peer52616f22014-02-25 16:26:00 +02001655 treatment == REG_REQ_ALREADY_SET ||
1656 treatment == REG_REQ_USER_HINT_HANDLED) {
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001657 kfree(user_request);
1658 return treatment;
1659 }
1660
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001661 user_request->intersect = treatment == REG_REQ_INTERSECT;
1662 user_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001663
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001664 reg_update_last_request(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001665
1666 user_alpha2[0] = user_request->alpha2[0];
1667 user_alpha2[1] = user_request->alpha2[1];
1668
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001669 return reg_call_crda(user_request);
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001670}
1671
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001672static enum reg_request_treatment
1673__reg_process_hint_driver(struct regulatory_request *driver_request)
1674{
1675 struct regulatory_request *lr = get_last_request();
1676
1677 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
1678 if (regdom_changes(driver_request->alpha2))
1679 return REG_REQ_OK;
1680 return REG_REQ_ALREADY_SET;
1681 }
1682
1683 /*
1684 * This would happen if you unplug and plug your card
1685 * back in or if you add a new device for which the previously
1686 * loaded card also agrees on the regulatory domain.
1687 */
1688 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1689 !regdom_changes(driver_request->alpha2))
1690 return REG_REQ_ALREADY_SET;
1691
1692 return REG_REQ_INTERSECT;
1693}
1694
1695/**
1696 * reg_process_hint_driver - process driver regulatory requests
1697 * @driver_request: a pending driver regulatory request
1698 *
1699 * The wireless subsystem can use this function to process
1700 * a regulatory request issued by an 802.11 driver.
1701 *
1702 * Returns one of the different reg request treatment values.
1703 */
1704static enum reg_request_treatment
1705reg_process_hint_driver(struct wiphy *wiphy,
1706 struct regulatory_request *driver_request)
1707{
1708 const struct ieee80211_regdomain *regd;
1709 enum reg_request_treatment treatment;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001710
1711 treatment = __reg_process_hint_driver(driver_request);
1712
1713 switch (treatment) {
1714 case REG_REQ_OK:
1715 break;
1716 case REG_REQ_IGNORE:
Ilan Peer52616f22014-02-25 16:26:00 +02001717 case REG_REQ_USER_HINT_HANDLED:
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001718 kfree(driver_request);
1719 return treatment;
1720 case REG_REQ_INTERSECT:
1721 /* fall through */
1722 case REG_REQ_ALREADY_SET:
1723 regd = reg_copy_regd(get_cfg80211_regdom());
1724 if (IS_ERR(regd)) {
1725 kfree(driver_request);
1726 return REG_REQ_IGNORE;
1727 }
1728 rcu_assign_pointer(wiphy->regd, regd);
1729 }
1730
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001731
1732 driver_request->intersect = treatment == REG_REQ_INTERSECT;
1733 driver_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001734
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001735 reg_update_last_request(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001736
1737 /*
1738 * Since CRDA will not be called in this case as we already
1739 * have applied the requested regulatory domain before we just
1740 * inform userspace we have processed the request
1741 */
1742 if (treatment == REG_REQ_ALREADY_SET) {
1743 nl80211_send_reg_change_event(driver_request);
1744 reg_set_request_processed();
1745 return treatment;
1746 }
1747
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001748 return reg_call_crda(driver_request);
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001749}
1750
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001751static enum reg_request_treatment
1752__reg_process_hint_country_ie(struct wiphy *wiphy,
1753 struct regulatory_request *country_ie_request)
1754{
1755 struct wiphy *last_wiphy = NULL;
1756 struct regulatory_request *lr = get_last_request();
1757
1758 if (reg_request_cell_base(lr)) {
1759 /* Trust a Cell base station over the AP's country IE */
1760 if (regdom_changes(country_ie_request->alpha2))
1761 return REG_REQ_IGNORE;
1762 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez2a901462013-11-11 22:15:31 +01001763 } else {
1764 if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
1765 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001766 }
1767
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001768 if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
1769 return -EINVAL;
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001770
1771 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
1772 return REG_REQ_OK;
1773
1774 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
1775
1776 if (last_wiphy != wiphy) {
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001777 /*
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001778 * Two cards with two APs claiming different
1779 * Country IE alpha2s. We could
1780 * intersect them, but that seems unlikely
1781 * to be correct. Reject second one for now.
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001782 */
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001783 if (regdom_changes(country_ie_request->alpha2))
1784 return REG_REQ_IGNORE;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001785 return REG_REQ_ALREADY_SET;
1786 }
Luis R. Rodriguez2f1c6c52013-11-05 09:18:07 -08001787 /*
1788 * Two consecutive Country IE hints on the same wiphy.
1789 * This should be picked up early by the driver/stack
1790 */
1791 if (WARN_ON(regdom_changes(country_ie_request->alpha2)))
1792 return REG_REQ_OK;
1793 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001794}
1795
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001796/**
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001797 * reg_process_hint_country_ie - process regulatory requests from country IEs
1798 * @country_ie_request: a regulatory request from a country IE
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001799 *
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001800 * The wireless subsystem can use this function to process
1801 * a regulatory request issued by a country Information Element.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001802 *
Johannes Berg2f922122012-12-03 17:54:55 +01001803 * Returns one of the different reg request treatment values.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001804 */
Johannes Berg2f922122012-12-03 17:54:55 +01001805static enum reg_request_treatment
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001806reg_process_hint_country_ie(struct wiphy *wiphy,
1807 struct regulatory_request *country_ie_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001808{
Johannes Berg2f922122012-12-03 17:54:55 +01001809 enum reg_request_treatment treatment;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001810
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001811 treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001812
Johannes Berg2f922122012-12-03 17:54:55 +01001813 switch (treatment) {
Johannes Berg2f922122012-12-03 17:54:55 +01001814 case REG_REQ_OK:
1815 break;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001816 case REG_REQ_IGNORE:
Ilan Peer52616f22014-02-25 16:26:00 +02001817 case REG_REQ_USER_HINT_HANDLED:
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001818 /* fall through */
1819 case REG_REQ_ALREADY_SET:
1820 kfree(country_ie_request);
Johannes Berg2f922122012-12-03 17:54:55 +01001821 return treatment;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001822 case REG_REQ_INTERSECT:
1823 kfree(country_ie_request);
1824 /*
1825 * This doesn't happen yet, not sure we
1826 * ever want to support it for this case.
1827 */
1828 WARN_ONCE(1, "Unexpected intersection for country IEs");
1829 return REG_REQ_IGNORE;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001830 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001831
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001832 country_ie_request->intersect = false;
1833 country_ie_request->processed = false;
Luis R. Rodriguez5ad6ef52013-11-05 09:18:08 -08001834
Luis R. Rodriguez05f1a3e2013-11-05 09:18:09 -08001835 reg_update_last_request(country_ie_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001836
Luis R. Rodriguezfe6631f2013-11-05 09:18:10 -08001837 return reg_call_crda(country_ie_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001838}
1839
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001840/* This processes *all* regulatory hints */
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08001841static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001842{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001843 struct wiphy *wiphy = NULL;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001844 enum reg_request_treatment treatment;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001845
Johannes Bergf4173762012-12-03 18:23:37 +01001846 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001847 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1848
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001849 switch (reg_request->initiator) {
1850 case NL80211_REGDOM_SET_BY_CORE:
1851 reg_process_hint_core(reg_request);
1852 return;
1853 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001854 treatment = reg_process_hint_user(reg_request);
Inbal Hacohen50c11eb2014-02-12 09:32:27 +02001855 if (treatment == REG_REQ_IGNORE ||
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001856 treatment == REG_REQ_ALREADY_SET)
1857 return;
Shaibal Dutta845f3352014-01-30 15:08:30 -08001858 queue_delayed_work(system_power_efficient_wq,
1859 &reg_timeout, msecs_to_jiffies(3142));
Luis R. Rodriguez0d97a612013-11-05 09:18:04 -08001860 return;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001861 case NL80211_REGDOM_SET_BY_DRIVER:
Ilan Peer772f0382014-01-14 15:17:23 +02001862 if (!wiphy)
1863 goto out_free;
Luis R. Rodriguez21636c72013-11-05 09:18:05 -08001864 treatment = reg_process_hint_driver(wiphy, reg_request);
1865 break;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001866 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Ilan Peer772f0382014-01-14 15:17:23 +02001867 if (!wiphy)
1868 goto out_free;
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001869 treatment = reg_process_hint_country_ie(wiphy, reg_request);
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001870 break;
1871 default:
1872 WARN(1, "invalid initiator %d\n", reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02001873 goto out_free;
Luis R. Rodriguezb3eb7f32013-11-05 09:18:03 -08001874 }
1875
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001876 /* This is required so that the orig_* parameters are saved */
1877 if (treatment == REG_REQ_ALREADY_SET && wiphy &&
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01001878 wiphy->regulatory_flags & REGULATORY_STRICT_REG)
Luis R. Rodriguezb23e7a92013-11-05 09:18:06 -08001879 wiphy_update_regulatory(wiphy, reg_request->initiator);
Ilan Peer772f0382014-01-14 15:17:23 +02001880
1881 return;
1882
1883out_free:
1884 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001885}
1886
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001887/*
1888 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1889 * Regulatory hints come on a first come first serve basis and we
1890 * must process each one atomically.
1891 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001892static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001893{
Johannes Bergc492db32012-12-06 16:29:25 +01001894 struct regulatory_request *reg_request, *lr;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001895
Johannes Bergc492db32012-12-06 16:29:25 +01001896 lr = get_last_request();
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001897
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001898 /* When last_request->processed becomes true this will be rescheduled */
Johannes Bergc492db32012-12-06 16:29:25 +01001899 if (lr && !lr->processed) {
Johannes Berg1a919312012-12-03 17:21:11 +01001900 REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n");
Johannes Berg5fe231e2013-05-08 21:45:15 +02001901 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001902 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001903
1904 spin_lock(&reg_requests_lock);
1905
1906 if (list_empty(&reg_requests_list)) {
1907 spin_unlock(&reg_requests_lock);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001908 return;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001909 }
1910
1911 reg_request = list_first_entry(&reg_requests_list,
1912 struct regulatory_request,
1913 list);
1914 list_del_init(&reg_request->list);
1915
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001916 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001917
Luis R. Rodriguez1daa37c2013-11-05 09:18:02 -08001918 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001919}
1920
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001921/* Processes beacon hints -- this has nothing to do with country IEs */
1922static void reg_process_pending_beacon_hints(void)
1923{
Johannes Berg79c97e92009-07-07 03:56:12 +02001924 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001925 struct reg_beacon *pending_beacon, *tmp;
1926
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001927 /* This goes through the _pending_ beacon list */
1928 spin_lock_bh(&reg_pending_beacons_lock);
1929
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001930 list_for_each_entry_safe(pending_beacon, tmp,
1931 &reg_pending_beacons, list) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001932 list_del_init(&pending_beacon->list);
1933
1934 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001935 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1936 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001937
1938 /* Remembers the beacon hint for new wiphys or reg changes */
1939 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1940 }
1941
1942 spin_unlock_bh(&reg_pending_beacons_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001943}
1944
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001945static void reg_todo(struct work_struct *work)
1946{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001947 rtnl_lock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001948 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001949 reg_process_pending_beacon_hints();
Johannes Berg5fe231e2013-05-08 21:45:15 +02001950 rtnl_unlock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001951}
1952
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001953static void queue_regulatory_request(struct regulatory_request *request)
1954{
Johannes Bergd4f2c882012-12-03 18:59:58 +01001955 request->alpha2[0] = toupper(request->alpha2[0]);
1956 request->alpha2[1] = toupper(request->alpha2[1]);
John W. Linvillec61029c2010-08-05 14:26:24 -04001957
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001958 spin_lock(&reg_requests_lock);
1959 list_add_tail(&request->list, &reg_requests_list);
1960 spin_unlock(&reg_requests_lock);
1961
1962 schedule_work(&reg_work);
1963}
1964
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001965/*
1966 * Core regulatory hint -- happens during cfg80211_init()
1967 * and when we restore regulatory settings.
1968 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001969static int regulatory_hint_core(const char *alpha2)
1970{
1971 struct regulatory_request *request;
1972
Johannes Berg1a919312012-12-03 17:21:11 +01001973 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001974 if (!request)
1975 return -ENOMEM;
1976
1977 request->alpha2[0] = alpha2[0];
1978 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001979 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001980
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08001981 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001982
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001983 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001984}
1985
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001986/* User hints */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001987int regulatory_hint_user(const char *alpha2,
1988 enum nl80211_user_reg_hint_type user_reg_hint_type)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001989{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001990 struct regulatory_request *request;
1991
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001992 if (WARN_ON(!alpha2))
1993 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001994
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001995 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1996 if (!request)
1997 return -ENOMEM;
1998
Johannes Bergf4173762012-12-03 18:23:37 +01001999 request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002000 request->alpha2[0] = alpha2[0];
2001 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05002002 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002003 request->user_reg_hint_type = user_reg_hint_type;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002004
2005 queue_regulatory_request(request);
2006
2007 return 0;
2008}
2009
Ilan Peer52616f22014-02-25 16:26:00 +02002010int regulatory_hint_indoor_user(void)
2011{
2012 struct regulatory_request *request;
2013
2014 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2015 if (!request)
2016 return -ENOMEM;
2017
2018 request->wiphy_idx = WIPHY_IDX_INVALID;
2019 request->initiator = NL80211_REGDOM_SET_BY_USER;
2020 request->user_reg_hint_type = NL80211_USER_REG_HINT_INDOOR;
2021 queue_regulatory_request(request);
2022
2023 return 0;
2024}
2025
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002026/* Driver hints */
2027int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2028{
2029 struct regulatory_request *request;
2030
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002031 if (WARN_ON(!alpha2 || !wiphy))
2032 return -EINVAL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002033
Luis R. Rodriguez4f7b9142013-12-14 20:09:06 +01002034 wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
2035
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002036 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2037 if (!request)
2038 return -ENOMEM;
2039
2040 request->wiphy_idx = get_wiphy_idx(wiphy);
2041
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002042 request->alpha2[0] = alpha2[0];
2043 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002044 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002045
2046 queue_regulatory_request(request);
2047
2048 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002049}
2050EXPORT_SYMBOL(regulatory_hint);
2051
Luis R. Rodriguez789fd032013-10-04 18:07:24 -07002052void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
2053 const u8 *country_ie, u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002054{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002055 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002056 enum environment_cap env = ENVIRON_ANY;
Johannes Bergdb2424c2013-05-10 19:07:52 +02002057 struct regulatory_request *request = NULL, *lr;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002058
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002059 /* IE len must be evenly divisible by 2 */
2060 if (country_ie_len & 0x01)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002061 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002062
2063 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Bergdb2424c2013-05-10 19:07:52 +02002064 return;
2065
2066 request = kzalloc(sizeof(*request), GFP_KERNEL);
2067 if (!request)
2068 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002069
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002070 alpha2[0] = country_ie[0];
2071 alpha2[1] = country_ie[1];
2072
2073 if (country_ie[2] == 'I')
2074 env = ENVIRON_INDOOR;
2075 else if (country_ie[2] == 'O')
2076 env = ENVIRON_OUTDOOR;
2077
Johannes Bergdb2424c2013-05-10 19:07:52 +02002078 rcu_read_lock();
2079 lr = get_last_request();
2080
2081 if (unlikely(!lr))
2082 goto out;
2083
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002084 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002085 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002086 * We leave conflict resolution to the workqueue, where can hold
Johannes Berg5fe231e2013-05-08 21:45:15 +02002087 * the RTNL.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002088 */
Johannes Bergc492db32012-12-06 16:29:25 +01002089 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
2090 lr->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002091 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002092
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002093 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04002094 request->alpha2[0] = alpha2[0];
2095 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002096 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002097 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002098
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002099 queue_regulatory_request(request);
Johannes Bergdb2424c2013-05-10 19:07:52 +02002100 request = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002101out:
Johannes Bergdb2424c2013-05-10 19:07:52 +02002102 kfree(request);
2103 rcu_read_unlock();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002104}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002105
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002106static void restore_alpha2(char *alpha2, bool reset_user)
2107{
2108 /* indicates there is no alpha2 to consider for restoration */
2109 alpha2[0] = '9';
2110 alpha2[1] = '7';
2111
2112 /* The user setting has precedence over the module parameter */
2113 if (is_user_regdom_saved()) {
2114 /* Unless we're asked to ignore it and reset it */
2115 if (reset_user) {
Johannes Berg1a919312012-12-03 17:21:11 +01002116 REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002117 user_alpha2[0] = '9';
2118 user_alpha2[1] = '7';
2119
2120 /*
2121 * If we're ignoring user settings, we still need to
2122 * check the module parameter to ensure we put things
2123 * back as they were for a full restore.
2124 */
2125 if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01002126 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2127 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002128 alpha2[0] = ieee80211_regdom[0];
2129 alpha2[1] = ieee80211_regdom[1];
2130 }
2131 } else {
Johannes Berg1a919312012-12-03 17:21:11 +01002132 REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
2133 user_alpha2[0], user_alpha2[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002134 alpha2[0] = user_alpha2[0];
2135 alpha2[1] = user_alpha2[1];
2136 }
2137 } else if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01002138 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
2139 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002140 alpha2[0] = ieee80211_regdom[0];
2141 alpha2[1] = ieee80211_regdom[1];
2142 } else
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07002143 REG_DBG_PRINT("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002144}
2145
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302146static void restore_custom_reg_settings(struct wiphy *wiphy)
2147{
2148 struct ieee80211_supported_band *sband;
2149 enum ieee80211_band band;
2150 struct ieee80211_channel *chan;
2151 int i;
2152
2153 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2154 sband = wiphy->bands[band];
2155 if (!sband)
2156 continue;
2157 for (i = 0; i < sband->n_channels; i++) {
2158 chan = &sband->channels[i];
2159 chan->flags = chan->orig_flags;
2160 chan->max_antenna_gain = chan->orig_mag;
2161 chan->max_power = chan->orig_mpwr;
Paul Stewart899852a2012-08-01 16:54:42 -07002162 chan->beacon_found = false;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302163 }
2164 }
2165}
2166
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002167/*
2168 * Restoring regulatory settings involves ingoring any
2169 * possibly stale country IE information and user regulatory
2170 * settings if so desired, this includes any beacon hints
2171 * learned as we could have traveled outside to another country
2172 * after disconnection. To restore regulatory settings we do
2173 * exactly what we did at bootup:
2174 *
2175 * - send a core regulatory hint
2176 * - send a user regulatory hint if applicable
2177 *
2178 * Device drivers that send a regulatory hint for a specific country
2179 * keep their own regulatory domain on wiphy->regd so that does does
2180 * not need to be remembered.
2181 */
2182static void restore_regulatory_settings(bool reset_user)
2183{
2184 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002185 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002186 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002187 struct regulatory_request *reg_request, *tmp;
2188 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302189 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002190
Johannes Berg5fe231e2013-05-08 21:45:15 +02002191 ASSERT_RTNL();
2192
Ilan Peer52616f22014-02-25 16:26:00 +02002193 reg_is_indoor = false;
2194
Johannes Berg2d319862013-01-09 12:01:38 +01002195 reset_regdomains(true, &world_regdom);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002196 restore_alpha2(alpha2, reset_user);
2197
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002198 /*
2199 * If there's any pending requests we simply
2200 * stash them to a temporary pending queue and
2201 * add then after we've restored regulatory
2202 * settings.
2203 */
2204 spin_lock(&reg_requests_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002205 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2206 if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
2207 continue;
2208 list_move_tail(&reg_request->list, &tmp_reg_req_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002209 }
2210 spin_unlock(&reg_requests_lock);
2211
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002212 /* Clear beacon hints */
2213 spin_lock_bh(&reg_pending_beacons_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01002214 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2215 list_del(&reg_beacon->list);
2216 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002217 }
2218 spin_unlock_bh(&reg_pending_beacons_lock);
2219
Johannes Bergfea9bce2012-12-03 17:32:01 +01002220 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2221 list_del(&reg_beacon->list);
2222 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002223 }
2224
2225 /* First restore to the basic regulatory settings */
Johannes Berg379b82f2012-12-06 15:44:07 +01002226 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
2227 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002228
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302229 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +01002230 if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05302231 restore_custom_reg_settings(&rdev->wiphy);
2232 }
2233
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08002234 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002235
2236 /*
2237 * This restores the ieee80211_regdom module parameter
2238 * preference or the last user requested regulatory
2239 * settings, user regulatory settings takes precedence.
2240 */
2241 if (is_an_alpha2(alpha2))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002242 regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002243
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002244 spin_lock(&reg_requests_lock);
Johannes Berg11cff962012-12-03 18:56:41 +01002245 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002246 spin_unlock(&reg_requests_lock);
2247
Luis R. Rodriguez14609552011-04-05 10:49:03 -07002248 REG_DBG_PRINT("Kicking the queue\n");
2249
2250 schedule_work(&reg_work);
2251}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002252
2253void regulatory_hint_disconnect(void)
2254{
Johannes Berg1a919312012-12-03 17:21:11 +01002255 REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002256 restore_regulatory_settings(false);
2257}
2258
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002259static bool freq_is_chan_12_13_14(u16 freq)
2260{
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002261 if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
2262 freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
2263 freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002264 return true;
2265 return false;
2266}
2267
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002268static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
2269{
2270 struct reg_beacon *pending_beacon;
2271
2272 list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
2273 if (beacon_chan->center_freq ==
2274 pending_beacon->chan.center_freq)
2275 return true;
2276 return false;
2277}
2278
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002279int regulatory_hint_found_beacon(struct wiphy *wiphy,
2280 struct ieee80211_channel *beacon_chan,
2281 gfp_t gfp)
2282{
2283 struct reg_beacon *reg_beacon;
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002284 bool processing;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002285
Johannes Berg1a919312012-12-03 17:21:11 +01002286 if (beacon_chan->beacon_found ||
2287 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002288 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg1a919312012-12-03 17:21:11 +01002289 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002290 return 0;
2291
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08002292 spin_lock_bh(&reg_pending_beacons_lock);
2293 processing = pending_reg_beacon(beacon_chan);
2294 spin_unlock_bh(&reg_pending_beacons_lock);
2295
2296 if (processing)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002297 return 0;
2298
2299 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2300 if (!reg_beacon)
2301 return -ENOMEM;
2302
Johannes Berg1a919312012-12-03 17:21:11 +01002303 REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002304 beacon_chan->center_freq,
2305 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2306 wiphy_name(wiphy));
2307
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002308 memcpy(&reg_beacon->chan, beacon_chan,
Johannes Berg1a919312012-12-03 17:21:11 +01002309 sizeof(struct ieee80211_channel));
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002310
2311 /*
2312 * Since we can be called from BH or and non-BH context
2313 * we must use spin_lock_bh()
2314 */
2315 spin_lock_bh(&reg_pending_beacons_lock);
2316 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2317 spin_unlock_bh(&reg_pending_beacons_lock);
2318
2319 schedule_work(&reg_work);
2320
2321 return 0;
2322}
2323
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002324static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002325{
2326 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002327 const struct ieee80211_reg_rule *reg_rule = NULL;
2328 const struct ieee80211_freq_range *freq_range = NULL;
2329 const struct ieee80211_power_rule *power_rule = NULL;
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002330 char bw[32], cac_time[32];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002331
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002332 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002333
2334 for (i = 0; i < rd->n_reg_rules; i++) {
2335 reg_rule = &rd->reg_rules[i];
2336 freq_range = &reg_rule->freq_range;
2337 power_rule = &reg_rule->power_rule;
2338
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002339 if (reg_rule->flags & NL80211_RRF_AUTO_BW)
2340 snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
2341 freq_range->max_bandwidth_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002342 reg_get_max_bandwidth(rd, reg_rule));
2343 else
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01002344 snprintf(bw, sizeof(bw), "%d KHz",
Janusz Dziedzic97524822014-01-30 09:52:20 +01002345 freq_range->max_bandwidth_khz);
2346
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002347 if (reg_rule->flags & NL80211_RRF_DFS)
2348 scnprintf(cac_time, sizeof(cac_time), "%u s",
2349 reg_rule->dfs_cac_ms/1000);
2350 else
2351 scnprintf(cac_time, sizeof(cac_time), "N/A");
2352
2353
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002354 /*
2355 * There may not be documentation for max antenna gain
2356 * in certain regions
2357 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002358 if (power_rule->max_antenna_gain)
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002359 pr_info(" (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002360 freq_range->start_freq_khz,
2361 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002362 bw,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002363 power_rule->max_antenna_gain,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002364 power_rule->max_eirp,
2365 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002366 else
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002367 pr_info(" (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002368 freq_range->start_freq_khz,
2369 freq_range->end_freq_khz,
Janusz Dziedzic97524822014-01-30 09:52:20 +01002370 bw,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01002371 power_rule->max_eirp,
2372 cac_time);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002373 }
2374}
2375
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01002376bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002377{
2378 switch (dfs_region) {
2379 case NL80211_DFS_UNSET:
2380 case NL80211_DFS_FCC:
2381 case NL80211_DFS_ETSI:
2382 case NL80211_DFS_JP:
2383 return true;
2384 default:
2385 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
2386 dfs_region);
2387 return false;
2388 }
2389}
2390
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002391static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002392{
Johannes Bergc492db32012-12-06 16:29:25 +01002393 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002394
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002395 if (is_intersected_alpha2(rd->alpha2)) {
Johannes Bergc492db32012-12-06 16:29:25 +01002396 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002397 struct cfg80211_registered_device *rdev;
Johannes Bergc492db32012-12-06 16:29:25 +01002398 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002399 if (rdev) {
Joe Perchese9c02682010-11-16 19:56:49 -08002400 pr_info("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002401 rdev->country_ie_alpha2[0],
2402 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002403 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002404 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002405 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002406 pr_info("Current regulatory domain intersected:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002407 } else if (is_world_regdom(rd->alpha2)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002408 pr_info("World regulatory domain updated:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002409 } else {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002410 if (is_unknown_alpha2(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08002411 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002412 else {
Johannes Bergc492db32012-12-06 16:29:25 +01002413 if (reg_request_cell_base(lr))
Johannes Berg1a919312012-12-03 17:21:11 +01002414 pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002415 rd->alpha2[0], rd->alpha2[1]);
2416 else
Johannes Berg1a919312012-12-03 17:21:11 +01002417 pr_info("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002418 rd->alpha2[0], rd->alpha2[1]);
2419 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002420 }
Johannes Berg1a919312012-12-03 17:21:11 +01002421
Luis R. Rodriguez3ef121b2013-11-13 18:54:05 +01002422 pr_info(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002423 print_rd_rules(rd);
2424}
2425
Johannes Berg2df78162008-10-28 16:49:41 +01002426static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002427{
Joe Perchese9c02682010-11-16 19:56:49 -08002428 pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002429 print_rd_rules(rd);
2430}
2431
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002432static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
2433{
2434 if (!is_world_regdom(rd->alpha2))
2435 return -EINVAL;
2436 update_world_regdomain(rd);
2437 return 0;
2438}
2439
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002440static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
2441 struct regulatory_request *user_request)
2442{
2443 const struct ieee80211_regdomain *intersected_rd = NULL;
2444
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002445 if (!regdom_changes(rd->alpha2))
2446 return -EALREADY;
2447
2448 if (!is_valid_rd(rd)) {
2449 pr_err("Invalid regulatory domain detected:\n");
2450 print_regdomain_info(rd);
2451 return -EINVAL;
2452 }
2453
2454 if (!user_request->intersect) {
2455 reset_regdomains(false, rd);
2456 return 0;
2457 }
2458
2459 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2460 if (!intersected_rd)
2461 return -EINVAL;
2462
2463 kfree(rd);
2464 rd = NULL;
2465 reset_regdomains(false, intersected_rd);
2466
2467 return 0;
2468}
2469
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002470static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
2471 struct regulatory_request *driver_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002472{
Johannes Berge9763c32012-12-03 16:59:46 +01002473 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002474 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002475 const struct ieee80211_regdomain *tmp;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002476 struct wiphy *request_wiphy;
Johannes Berg6913b492012-12-04 00:48:59 +01002477
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002478 if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002479 return -EINVAL;
2480
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002481 if (!regdom_changes(rd->alpha2))
2482 return -EALREADY;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002483
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002484 if (!is_valid_rd(rd)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002485 pr_err("Invalid regulatory domain detected:\n");
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002486 print_regdomain_info(rd);
2487 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002488 }
2489
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002490 request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
2491 if (!request_wiphy) {
Shaibal Dutta845f3352014-01-30 15:08:30 -08002492 queue_delayed_work(system_power_efficient_wq,
2493 &reg_timeout, 0);
Johannes Bergde3584b2011-11-21 10:44:00 +01002494 return -ENODEV;
2495 }
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002496
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002497 if (!driver_request->intersect) {
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002498 if (request_wiphy->regd)
2499 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002500
Johannes Berge9763c32012-12-03 16:59:46 +01002501 regd = reg_copy_regd(rd);
2502 if (IS_ERR(regd))
2503 return PTR_ERR(regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002504
Johannes Berg458f4f92012-12-06 15:47:38 +01002505 rcu_assign_pointer(request_wiphy->regd, regd);
Johannes Berg379b82f2012-12-06 15:44:07 +01002506 reset_regdomains(false, rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002507 return 0;
2508 }
2509
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002510 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
2511 if (!intersected_rd)
2512 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002513
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002514 /*
2515 * We can trash what CRDA provided now.
2516 * However if a driver requested this specific regulatory
2517 * domain we keep it for its private use
2518 */
2519 tmp = get_wiphy_regdom(request_wiphy);
2520 rcu_assign_pointer(request_wiphy->regd, rd);
2521 rcu_free_regdom(tmp);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002522
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002523 rd = NULL;
Larry Fingerb7566fc2013-02-04 15:33:44 -06002524
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002525 reset_regdomains(false, intersected_rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002526
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002527 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002528}
2529
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002530static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
2531 struct regulatory_request *country_ie_request)
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002532{
2533 struct wiphy *request_wiphy;
2534
2535 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2536 !is_unknown_alpha2(rd->alpha2))
2537 return -EINVAL;
2538
2539 /*
2540 * Lets only bother proceeding on the same alpha2 if the current
2541 * rd is non static (it means CRDA was present and was used last)
2542 * and the pending request came in from a country IE
2543 */
2544
2545 if (!is_valid_rd(rd)) {
2546 pr_err("Invalid regulatory domain detected:\n");
2547 print_regdomain_info(rd);
2548 return -EINVAL;
2549 }
2550
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002551 request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002552 if (!request_wiphy) {
Shaibal Dutta845f3352014-01-30 15:08:30 -08002553 queue_delayed_work(system_power_efficient_wq,
2554 &reg_timeout, 0);
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002555 return -ENODEV;
2556 }
2557
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002558 if (country_ie_request->intersect)
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002559 return -EINVAL;
2560
2561 reset_regdomains(false, rd);
2562 return 0;
2563}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002564
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002565/*
2566 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002567 * multiple drivers can be ironed out later. Caller must've already
Johannes Berg458f4f92012-12-06 15:47:38 +01002568 * kmalloc'd the rd structure.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002569 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002570int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002571{
Johannes Bergc492db32012-12-06 16:29:25 +01002572 struct regulatory_request *lr;
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002573 bool user_reset = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002574 int r;
2575
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002576 if (!reg_is_valid_request(rd->alpha2)) {
2577 kfree(rd);
2578 return -EINVAL;
2579 }
2580
Johannes Bergc492db32012-12-06 16:29:25 +01002581 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002582
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002583 /* Note that this doesn't update the wiphys, this is done below */
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002584 switch (lr->initiator) {
2585 case NL80211_REGDOM_SET_BY_CORE:
2586 r = reg_set_rd_core(rd);
2587 break;
2588 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002589 r = reg_set_rd_user(rd, lr);
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002590 user_reset = true;
Luis R. Rodriguez84721d42013-11-05 09:18:15 -08002591 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002592 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguezf5fe32472013-11-05 09:18:16 -08002593 r = reg_set_rd_driver(rd, lr);
2594 break;
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002595 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez01992402013-11-05 09:18:17 -08002596 r = reg_set_rd_country_ie(rd, lr);
Luis R. Rodriguez3b9e5ac2013-11-05 09:18:14 -08002597 break;
2598 default:
2599 WARN(1, "invalid initiator %d\n", lr->initiator);
2600 return -EINVAL;
2601 }
2602
Johannes Bergd2372b32008-10-24 20:32:20 +02002603 if (r) {
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002604 switch (r) {
2605 case -EALREADY:
Kalle Valo95908532012-07-12 15:33:58 +03002606 reg_set_request_processed();
Janusz Dziedzic092008a2014-02-14 08:54:00 +01002607 break;
2608 default:
2609 /* Back to world regulatory in case of errors */
2610 restore_regulatory_settings(user_reset);
2611 }
Kalle Valo95908532012-07-12 15:33:58 +03002612
Johannes Bergd2372b32008-10-24 20:32:20 +02002613 kfree(rd);
Johannes Berg38fd2142013-05-10 19:17:17 +02002614 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002615 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002616
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002617 /* This would make this whole thing pointless */
Johannes Berg38fd2142013-05-10 19:17:17 +02002618 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
2619 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002620
2621 /* update all wiphys now with the new established regulatory domain */
Johannes Bergc492db32012-12-06 16:29:25 +01002622 update_all_wiphy_regulatory(lr->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002623
Johannes Berg458f4f92012-12-06 15:47:38 +01002624 print_regdomain(get_cfg80211_regdom());
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002625
Johannes Bergc492db32012-12-06 16:29:25 +01002626 nl80211_send_reg_change_event(lr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002627
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002628 reg_set_request_processed();
2629
Johannes Berg38fd2142013-05-10 19:17:17 +02002630 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002631}
2632
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002633void wiphy_regulatory_register(struct wiphy *wiphy)
2634{
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002635 struct regulatory_request *lr;
2636
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002637 if (!reg_dev_ignore_cell_hint(wiphy))
2638 reg_num_devs_support_basehint++;
2639
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002640 lr = get_last_request();
2641 wiphy_update_regulatory(wiphy, lr->initiator);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002642}
2643
Luis R. Rodriguezbfead082012-07-12 11:49:19 -07002644void wiphy_regulatory_deregister(struct wiphy *wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002645{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002646 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01002647 struct regulatory_request *lr;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002648
Johannes Bergc492db32012-12-06 16:29:25 +01002649 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002650
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002651 if (!reg_dev_ignore_cell_hint(wiphy))
2652 reg_num_devs_support_basehint--;
2653
Johannes Berg458f4f92012-12-06 15:47:38 +01002654 rcu_free_regdom(get_wiphy_regdom(wiphy));
Monam Agarwal34dd8862014-03-24 00:53:40 +05302655 RCU_INIT_POINTER(wiphy->regd, NULL);
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002656
Johannes Bergc492db32012-12-06 16:29:25 +01002657 if (lr)
2658 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002659
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002660 if (!request_wiphy || request_wiphy != wiphy)
Johannes Berg38fd2142013-05-10 19:17:17 +02002661 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002662
Johannes Bergc492db32012-12-06 16:29:25 +01002663 lr->wiphy_idx = WIPHY_IDX_INVALID;
2664 lr->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002665}
2666
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002667static void reg_timeout_work(struct work_struct *work)
2668{
Johannes Berg1a919312012-12-03 17:21:11 +01002669 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
Johannes Bergf77b86d2013-06-24 15:43:38 +02002670 rtnl_lock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002671 restore_regulatory_settings(true);
Johannes Bergf77b86d2013-06-24 15:43:38 +02002672 rtnl_unlock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002673}
2674
Ilan Peer174e0cd2014-02-23 09:13:01 +02002675/*
2676 * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
2677 * UNII band definitions
2678 */
2679int cfg80211_get_unii(int freq)
2680{
2681 /* UNII-1 */
2682 if (freq >= 5150 && freq <= 5250)
2683 return 0;
2684
2685 /* UNII-2A */
2686 if (freq > 5250 && freq <= 5350)
2687 return 1;
2688
2689 /* UNII-2B */
2690 if (freq > 5350 && freq <= 5470)
2691 return 2;
2692
2693 /* UNII-2C */
2694 if (freq > 5470 && freq <= 5725)
2695 return 3;
2696
2697 /* UNII-3 */
2698 if (freq > 5725 && freq <= 5825)
2699 return 4;
2700
2701 return -EINVAL;
2702}
2703
Ilan Peerc8866e52014-02-23 09:13:03 +02002704bool regulatory_indoor_allowed(void)
2705{
2706 return reg_is_indoor;
2707}
2708
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002709int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002710{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002711 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002712
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002713 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2714 if (IS_ERR(reg_pdev))
2715 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002716
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002717 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002718 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002719
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -07002720 reg_regdb_size_check();
2721
Johannes Berg458f4f92012-12-06 15:47:38 +01002722 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002723
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002724 user_alpha2[0] = '9';
2725 user_alpha2[1] = '7';
2726
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002727 /* We always try to get an update for the static regdomain */
Johannes Berg458f4f92012-12-06 15:47:38 +01002728 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002729 if (err) {
2730 if (err == -ENOMEM)
2731 return err;
2732 /*
2733 * N.B. kobject_uevent_env() can fail mainly for when we're out
2734 * memory which is handled and propagated appropriately above
2735 * but it can also fail during a netlink_broadcast() or during
2736 * early boot for call_usermodehelper(). For now treat these
2737 * errors as non-fatal.
2738 */
Joe Perchese9c02682010-11-16 19:56:49 -08002739 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002740 }
Johannes Berg734366d2008-09-15 10:56:48 +02002741
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002742 /*
2743 * Finally, if the user set the module parameter treat it
2744 * as a user hint.
2745 */
2746 if (!is_world_regdom(ieee80211_regdom))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002747 regulatory_hint_user(ieee80211_regdom,
2748 NL80211_USER_REG_HINT_USER);
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002749
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002750 return 0;
2751}
2752
Johannes Berg1a919312012-12-03 17:21:11 +01002753void regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002754{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002755 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002756 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002757
2758 cancel_work_sync(&reg_work);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002759 cancel_delayed_work_sync(&reg_timeout);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002760
Johannes Berg9027b142012-12-03 17:59:24 +01002761 /* Lock to suppress warnings */
Johannes Berg38fd2142013-05-10 19:17:17 +02002762 rtnl_lock();
Johannes Berg379b82f2012-12-06 15:44:07 +01002763 reset_regdomains(true, NULL);
Johannes Berg38fd2142013-05-10 19:17:17 +02002764 rtnl_unlock();
Johannes Berg734366d2008-09-15 10:56:48 +02002765
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08002766 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02002767
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002768 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002769
Johannes Bergfea9bce2012-12-03 17:32:01 +01002770 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2771 list_del(&reg_beacon->list);
2772 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002773 }
2774
Johannes Bergfea9bce2012-12-03 17:32:01 +01002775 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2776 list_del(&reg_beacon->list);
2777 kfree(reg_beacon);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002778 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002779
Johannes Bergfea9bce2012-12-03 17:32:01 +01002780 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2781 list_del(&reg_request->list);
2782 kfree(reg_request);
Johannes Berg8318d782008-01-24 19:38:38 +01002783 }
Johannes Berg8318d782008-01-24 19:38:38 +01002784}