blob: 7da67fd0b4188a7cf3e3ded0ae2a64d565274947 [file] [log] [blame]
Johannes Berg8318d782008-01-24 19:38:38 +01001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -08005 * Copyright 2008-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com>
Johannes Berg8318d782008-01-24 19:38:38 +01006 *
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -08007 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Johannes Berg8318d782008-01-24 19:38:38 +010018 */
19
Luis R. Rodriguez3b77d5e2011-12-20 12:23:38 -080020
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070021/**
22 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010023 *
24 * The usual implementation is for a driver to read a device EEPROM to
25 * determine which regulatory domain it should be operating under, then
26 * looking up the allowable channels in a driver-local table and finally
27 * registering those channels in the wiphy structure.
28 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070029 * Another set of compliance enforcement is for drivers to use their
30 * own compliance limits which can be stored on the EEPROM. The host
31 * driver or firmware may ensure these are used.
32 *
33 * In addition to all this we provide an extra layer of regulatory
34 * conformance. For drivers which do not have any regulatory
35 * information CRDA provides the complete regulatory solution.
36 * For others it provides a community effort on further restrictions
37 * to enhance compliance.
38 *
39 * Note: When number of rules --> infinity we will not be able to
40 * index on alpha2 any more, instead we'll probably have to
41 * rely on some SHA1 checksum of the regdomain for example.
42 *
Johannes Berg8318d782008-01-24 19:38:38 +010043 */
Joe Perchese9c02682010-11-16 19:56:49 -080044
45#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Johannes Berg8318d782008-01-24 19:38:38 +010047#include <linux/kernel.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040048#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070050#include <linux/list.h>
John W. Linvillec61029c2010-08-05 14:26:24 -040051#include <linux/ctype.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070052#include <linux/nl80211.h>
53#include <linux/platform_device.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040054#include <linux/moduleparam.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070055#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010056#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070057#include "reg.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050058#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040059#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010060
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050061#ifdef CONFIG_CFG80211_REG_DEBUG
Joe Perches12c5ffb2011-07-29 14:51:25 -070062#define REG_DBG_PRINT(format, args...) \
63 printk(KERN_DEBUG pr_fmt(format), ##args)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050064#else
John W. Linville82711952010-01-05 17:57:20 -050065#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050066#endif
67
Johannes Berg2f922122012-12-03 17:54:55 +010068enum reg_request_treatment {
69 REG_REQ_OK,
70 REG_REQ_IGNORE,
71 REG_REQ_INTERSECT,
72 REG_REQ_ALREADY_SET,
73};
74
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050075static struct regulatory_request core_request_world = {
76 .initiator = NL80211_REGDOM_SET_BY_CORE,
77 .alpha2[0] = '0',
78 .alpha2[1] = '0',
79 .intersect = false,
80 .processed = true,
81 .country_ie_env = ENVIRON_ANY,
82};
83
Johannes Berg38fd2142013-05-10 19:17:17 +020084/*
85 * Receipt of information from last regulatory request,
86 * protected by RTNL (and can be accessed with RCU protection)
87 */
Johannes Bergc492db32012-12-06 16:29:25 +010088static struct regulatory_request __rcu *last_request =
89 (void __rcu *)&core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +020090
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070091/* To trigger userspace events */
92static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010093
Scott James Remnant4d9d88d2011-03-08 10:45:30 -080094static struct device_type reg_device_type = {
95 .uevent = reg_device_uevent,
96};
97
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050098/*
99 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +0200100 * the current one and a world regulatory domain in case we have no
Johannes Berge8da2bb2012-12-03 23:00:08 +0100101 * information to give us an alpha2.
Johannes Berg38fd2142013-05-10 19:17:17 +0200102 * (protected by RTNL, can be read under RCU)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500103 */
Johannes Berg458f4f92012-12-06 15:47:38 +0100104const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +0200105
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500106/*
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700107 * Number of devices that registered to the core
108 * that support cellular base station regulatory hints
Johannes Berg38fd2142013-05-10 19:17:17 +0200109 * (protected by RTNL)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700110 */
111static int reg_num_devs_support_basehint;
112
Johannes Berg458f4f92012-12-06 15:47:38 +0100113static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
114{
Johannes Berg38fd2142013-05-10 19:17:17 +0200115 return rtnl_dereference(cfg80211_regdomain);
Johannes Berg458f4f92012-12-06 15:47:38 +0100116}
117
118static const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
119{
Johannes Berg38fd2142013-05-10 19:17:17 +0200120 return rtnl_dereference(wiphy->regd);
Johannes Berg458f4f92012-12-06 15:47:38 +0100121}
122
123static void rcu_free_regdom(const struct ieee80211_regdomain *r)
124{
125 if (!r)
126 return;
127 kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
128}
129
Johannes Bergc492db32012-12-06 16:29:25 +0100130static struct regulatory_request *get_last_request(void)
131{
Johannes Berg38fd2142013-05-10 19:17:17 +0200132 return rcu_dereference_rtnl(last_request);
Johannes Bergc492db32012-12-06 16:29:25 +0100133}
134
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500135/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500136static LIST_HEAD(reg_requests_list);
137static spinlock_t reg_requests_lock;
138
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500139/* Used to queue up beacon hints for review */
140static LIST_HEAD(reg_pending_beacons);
141static spinlock_t reg_pending_beacons_lock;
142
143/* Used to keep track of processed beacon hints */
144static LIST_HEAD(reg_beacon_list);
145
146struct reg_beacon {
147 struct list_head list;
148 struct ieee80211_channel chan;
149};
150
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800151static void reg_todo(struct work_struct *work);
152static DECLARE_WORK(reg_work, reg_todo);
153
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -0700154static void reg_timeout_work(struct work_struct *work);
155static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
156
Johannes Berg734366d2008-09-15 10:56:48 +0200157/* We keep a static world regulatory domain in case of the absence of CRDA */
158static const struct ieee80211_regdomain world_regdom = {
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300159 .n_reg_rules = 6,
Johannes Berg734366d2008-09-15 10:56:48 +0200160 .alpha2 = "00",
161 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500162 /* IEEE 802.11b/g, channels 1..11 */
163 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Johannes Berg43c771a2012-11-12 10:51:34 +0100164 /* IEEE 802.11b/g, channels 12..13. */
165 REG_RULE(2467-10, 2472+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500166 NL80211_RRF_PASSIVE_SCAN |
167 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800168 /* IEEE 802.11 channel 14 - Only JP enables
169 * this and for 802.11b only */
170 REG_RULE(2484-10, 2484+10, 20, 6, 20,
171 NL80211_RRF_PASSIVE_SCAN |
172 NL80211_RRF_NO_IBSS |
173 NL80211_RRF_NO_OFDM),
174 /* IEEE 802.11a, channel 36..48 */
Johannes Berg131a19b2013-03-04 20:54:46 +0100175 REG_RULE(5180-10, 5240+10, 160, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800176 NL80211_RRF_PASSIVE_SCAN |
177 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500178
Johannes Berg131a19b2013-03-04 20:54:46 +0100179 /* IEEE 802.11a, channel 52..64 - DFS required */
180 REG_RULE(5260-10, 5320+10, 160, 6, 20,
181 NL80211_RRF_PASSIVE_SCAN |
182 NL80211_RRF_NO_IBSS |
183 NL80211_RRF_DFS),
184
185 /* IEEE 802.11a, channel 100..144 - DFS required */
186 REG_RULE(5500-10, 5720+10, 160, 6, 20,
187 NL80211_RRF_PASSIVE_SCAN |
188 NL80211_RRF_NO_IBSS |
189 NL80211_RRF_DFS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500190
191 /* IEEE 802.11a, channel 149..165 */
Johannes Berg8ab9d852012-12-03 22:09:22 +0100192 REG_RULE(5745-10, 5825+10, 80, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500193 NL80211_RRF_PASSIVE_SCAN |
194 NL80211_RRF_NO_IBSS),
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300195
196 /* IEEE 802.11ad (60gHz), channels 1..3 */
197 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200198 }
199};
200
Johannes Berg38fd2142013-05-10 19:17:17 +0200201/* protected by RTNL */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200202static const struct ieee80211_regdomain *cfg80211_world_regdom =
203 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200204
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400205static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500206static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400207
Johannes Berg734366d2008-09-15 10:56:48 +0200208module_param(ieee80211_regdom, charp, 0444);
209MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
210
Johannes Berg379b82f2012-12-06 15:44:07 +0100211static void reset_regdomains(bool full_reset,
212 const struct ieee80211_regdomain *new_regdom)
Johannes Berg734366d2008-09-15 10:56:48 +0200213{
Johannes Berg458f4f92012-12-06 15:47:38 +0100214 const struct ieee80211_regdomain *r;
Johannes Bergc492db32012-12-06 16:29:25 +0100215 struct regulatory_request *lr;
Johannes Berg458f4f92012-12-06 15:47:38 +0100216
Johannes Berg38fd2142013-05-10 19:17:17 +0200217 ASSERT_RTNL();
Johannes Berge8da2bb2012-12-03 23:00:08 +0100218
Johannes Berg458f4f92012-12-06 15:47:38 +0100219 r = get_cfg80211_regdom();
220
Johannes Berg942b25c2008-09-15 11:26:47 +0200221 /* avoid freeing static information or freeing something twice */
Johannes Berg458f4f92012-12-06 15:47:38 +0100222 if (r == cfg80211_world_regdom)
223 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200224 if (cfg80211_world_regdom == &world_regdom)
225 cfg80211_world_regdom = NULL;
Johannes Berg458f4f92012-12-06 15:47:38 +0100226 if (r == &world_regdom)
227 r = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200228
Johannes Berg458f4f92012-12-06 15:47:38 +0100229 rcu_free_regdom(r);
230 rcu_free_regdom(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200231
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200232 cfg80211_world_regdom = &world_regdom;
Johannes Berg458f4f92012-12-06 15:47:38 +0100233 rcu_assign_pointer(cfg80211_regdomain, new_regdom);
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500234
235 if (!full_reset)
236 return;
237
Johannes Bergc492db32012-12-06 16:29:25 +0100238 lr = get_last_request();
239 if (lr != &core_request_world && lr)
240 kfree_rcu(lr, rcu_head);
241 rcu_assign_pointer(last_request, &core_request_world);
Johannes Berg734366d2008-09-15 10:56:48 +0200242}
243
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500244/*
245 * Dynamic world regulatory domain requested by the wireless
246 * core upon initialization
247 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200248static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200249{
Johannes Bergc492db32012-12-06 16:29:25 +0100250 struct regulatory_request *lr;
Johannes Berg734366d2008-09-15 10:56:48 +0200251
Johannes Bergc492db32012-12-06 16:29:25 +0100252 lr = get_last_request();
253
254 WARN_ON(!lr);
Johannes Berge8da2bb2012-12-03 23:00:08 +0100255
Johannes Berg379b82f2012-12-06 15:44:07 +0100256 reset_regdomains(false, rd);
Johannes Berg734366d2008-09-15 10:56:48 +0200257
258 cfg80211_world_regdom = rd;
Johannes Berg734366d2008-09-15 10:56:48 +0200259}
Johannes Berg734366d2008-09-15 10:56:48 +0200260
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200261bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100262{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263 if (!alpha2)
264 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100265 return alpha2[0] == '0' && alpha2[1] == '0';
Johannes Berg8318d782008-01-24 19:38:38 +0100266}
267
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200268static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700269{
270 if (!alpha2)
271 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100272 return alpha2[0] && alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700273}
Johannes Berg8318d782008-01-24 19:38:38 +0100274
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200275static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700276{
277 if (!alpha2)
278 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500279 /*
280 * Special case where regulatory domain was built by driver
281 * but a specific alpha2 cannot be determined
282 */
Johannes Berg1a919312012-12-03 17:21:11 +0100283 return alpha2[0] == '9' && alpha2[1] == '9';
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700284}
285
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800286static bool is_intersected_alpha2(const char *alpha2)
287{
288 if (!alpha2)
289 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500290 /*
291 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800292 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500293 * structures
294 */
Johannes Berg1a919312012-12-03 17:21:11 +0100295 return alpha2[0] == '9' && alpha2[1] == '8';
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800296}
297
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200298static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700299{
300 if (!alpha2)
301 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100302 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700303}
304
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200305static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700306{
307 if (!alpha2_x || !alpha2_y)
308 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100309 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700310}
311
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500312static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700313{
Johannes Berg458f4f92012-12-06 15:47:38 +0100314 const struct ieee80211_regdomain *r = get_cfg80211_regdom();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500315
Johannes Berg458f4f92012-12-06 15:47:38 +0100316 if (!r)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700317 return true;
Johannes Berg458f4f92012-12-06 15:47:38 +0100318 return !alpha2_equal(r->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700319}
320
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500321/*
322 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
323 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
324 * has ever been issued.
325 */
326static bool is_user_regdom_saved(void)
327{
328 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
329 return false;
330
331 /* This would indicate a mistake on the design */
Johannes Berg1a919312012-12-03 17:21:11 +0100332 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500333 "Unexpected user alpha2: %c%c\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100334 user_alpha2[0], user_alpha2[1]))
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500335 return false;
336
337 return true;
338}
339
Johannes Berge9763c32012-12-03 16:59:46 +0100340static const struct ieee80211_regdomain *
341reg_copy_regd(const struct ieee80211_regdomain *src_regd)
John W. Linville3b377ea2009-12-18 17:59:01 -0500342{
343 struct ieee80211_regdomain *regd;
Johannes Berge9763c32012-12-03 16:59:46 +0100344 int size_of_regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500345 unsigned int i;
346
Johannes Berg82f20852012-12-04 12:49:16 +0100347 size_of_regd =
348 sizeof(struct ieee80211_regdomain) +
349 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
John W. Linville3b377ea2009-12-18 17:59:01 -0500350
351 regd = kzalloc(size_of_regd, GFP_KERNEL);
352 if (!regd)
Johannes Berge9763c32012-12-03 16:59:46 +0100353 return ERR_PTR(-ENOMEM);
John W. Linville3b377ea2009-12-18 17:59:01 -0500354
355 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
356
357 for (i = 0; i < src_regd->n_reg_rules; i++)
358 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
Johannes Berge9763c32012-12-03 16:59:46 +0100359 sizeof(struct ieee80211_reg_rule));
John W. Linville3b377ea2009-12-18 17:59:01 -0500360
Johannes Berge9763c32012-12-03 16:59:46 +0100361 return regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500362}
363
364#ifdef CONFIG_CFG80211_INTERNAL_REGDB
365struct reg_regdb_search_request {
366 char alpha2[2];
367 struct list_head list;
368};
369
370static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400371static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500372
373static void reg_regdb_search(struct work_struct *work)
374{
375 struct reg_regdb_search_request *request;
Johannes Berge9763c32012-12-03 16:59:46 +0100376 const struct ieee80211_regdomain *curdom, *regdom = NULL;
377 int i;
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700378
Johannes Berg5fe231e2013-05-08 21:45:15 +0200379 rtnl_lock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500380
John W. Linville368d06f2010-03-16 15:40:59 -0400381 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500382 while (!list_empty(&reg_regdb_search_list)) {
383 request = list_first_entry(&reg_regdb_search_list,
384 struct reg_regdb_search_request,
385 list);
386 list_del(&request->list);
387
Johannes Berg1a919312012-12-03 17:21:11 +0100388 for (i = 0; i < reg_regdb_size; i++) {
John W. Linville3b377ea2009-12-18 17:59:01 -0500389 curdom = reg_regdb[i];
390
Johannes Berg1a919312012-12-03 17:21:11 +0100391 if (alpha2_equal(request->alpha2, curdom->alpha2)) {
Johannes Berge9763c32012-12-03 16:59:46 +0100392 regdom = reg_copy_regd(curdom);
John W. Linville3b377ea2009-12-18 17:59:01 -0500393 break;
394 }
395 }
396
397 kfree(request);
398 }
John W. Linville368d06f2010-03-16 15:40:59 -0400399 mutex_unlock(&reg_regdb_search_mutex);
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700400
Johannes Berge9763c32012-12-03 16:59:46 +0100401 if (!IS_ERR_OR_NULL(regdom))
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700402 set_regdom(regdom);
403
Johannes Berg5fe231e2013-05-08 21:45:15 +0200404 rtnl_unlock();
John W. Linville3b377ea2009-12-18 17:59:01 -0500405}
406
407static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
408
409static void reg_regdb_query(const char *alpha2)
410{
411 struct reg_regdb_search_request *request;
412
413 if (!alpha2)
414 return;
415
416 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
417 if (!request)
418 return;
419
420 memcpy(request->alpha2, alpha2, 2);
421
John W. Linville368d06f2010-03-16 15:40:59 -0400422 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500423 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400424 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500425
426 schedule_work(&reg_regdb_work);
427}
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700428
429/* Feel free to add any other sanity checks here */
430static void reg_regdb_size_check(void)
431{
432 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
433 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
434}
John W. Linville3b377ea2009-12-18 17:59:01 -0500435#else
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700436static inline void reg_regdb_size_check(void) {}
John W. Linville3b377ea2009-12-18 17:59:01 -0500437static inline void reg_regdb_query(const char *alpha2) {}
438#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
439
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500440/*
441 * This lets us keep regulatory code which is updated on a regulatory
Scott James Remnant4d9d88d2011-03-08 10:45:30 -0800442 * basis in userspace. Country information is filled in by
443 * reg_device_uevent
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500444 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700445static int call_crda(const char *alpha2)
446{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700447 if (!is_world_regdom((char *) alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -0800448 pr_info("Calling CRDA for country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700449 alpha2[0], alpha2[1]);
450 else
Joe Perchese9c02682010-11-16 19:56:49 -0800451 pr_info("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700452
John W. Linville3b377ea2009-12-18 17:59:01 -0500453 /* query internal regulatory database (if it exists) */
454 reg_regdb_query(alpha2);
455
Scott James Remnant4d9d88d2011-03-08 10:45:30 -0800456 return kobject_uevent(&reg_pdev->dev.kobj, KOBJ_CHANGE);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700457}
458
Johannes Berg6913b492012-12-04 00:48:59 +0100459static bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700460{
Johannes Bergc492db32012-12-06 16:29:25 +0100461 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400462
Johannes Bergc492db32012-12-06 16:29:25 +0100463 if (!lr || lr->processed)
Johannes Bergf6037d02008-10-21 11:01:33 +0200464 return false;
465
Johannes Bergc492db32012-12-06 16:29:25 +0100466 return alpha2_equal(lr->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700467}
468
469/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200470static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700471{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200472 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700473 u32 freq_diff;
474
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800475 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700476 return false;
477
478 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
479 return false;
480
481 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
482
Roel Kluinbd05f282009-03-03 22:55:21 +0100483 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg1a919312012-12-03 17:21:11 +0100484 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700485 return false;
486
487 return true;
488}
489
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200490static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700491{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200492 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700493 unsigned int i;
494
495 if (!rd->n_reg_rules)
496 return false;
497
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800498 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
499 return false;
500
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700501 for (i = 0; i < rd->n_reg_rules; i++) {
502 reg_rule = &rd->reg_rules[i];
503 if (!is_valid_reg_rule(reg_rule))
504 return false;
505 }
506
507 return true;
508}
509
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400510static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100511 u32 center_freq_khz, u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700512{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400513 u32 start_freq_khz, end_freq_khz;
514
515 start_freq_khz = center_freq_khz - (bw_khz/2);
516 end_freq_khz = center_freq_khz + (bw_khz/2);
517
518 if (start_freq_khz >= freq_range->start_freq_khz &&
519 end_freq_khz <= freq_range->end_freq_khz)
520 return true;
521
522 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700523}
524
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800525/**
526 * freq_in_rule_band - tells us if a frequency is in a frequency band
527 * @freq_range: frequency rule we want to query
528 * @freq_khz: frequency we are inquiring about
529 *
530 * This lets us know if a specific frequency rule is or is not relevant to
531 * a specific frequency's band. Bands are device specific and artificial
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200532 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
533 * however it is safe for now to assume that a frequency rule should not be
534 * part of a frequency's band if the start freq or end freq are off by more
535 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
536 * 60 GHz band.
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800537 * This resolution can be lowered and should be considered as we add
538 * regulatory rule support for other "bands".
539 **/
540static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg1a919312012-12-03 17:21:11 +0100541 u32 freq_khz)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800542{
543#define ONE_GHZ_IN_KHZ 1000000
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200544 /*
545 * From 802.11ad: directional multi-gigabit (DMG):
546 * Pertaining to operation in a frequency band containing a channel
547 * with the Channel starting frequency above 45 GHz.
548 */
549 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
550 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
551 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800552 return true;
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200553 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800554 return true;
555 return false;
556#undef ONE_GHZ_IN_KHZ
557}
558
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500559/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500560 * Helper for regdom_intersect(), this does the real
561 * mathematical intersection fun
562 */
Johannes Berg1a919312012-12-03 17:21:11 +0100563static int reg_rules_intersect(const struct ieee80211_reg_rule *rule1,
564 const struct ieee80211_reg_rule *rule2,
565 struct ieee80211_reg_rule *intersected_rule)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700566{
567 const struct ieee80211_freq_range *freq_range1, *freq_range2;
568 struct ieee80211_freq_range *freq_range;
569 const struct ieee80211_power_rule *power_rule1, *power_rule2;
570 struct ieee80211_power_rule *power_rule;
571 u32 freq_diff;
572
573 freq_range1 = &rule1->freq_range;
574 freq_range2 = &rule2->freq_range;
575 freq_range = &intersected_rule->freq_range;
576
577 power_rule1 = &rule1->power_rule;
578 power_rule2 = &rule2->power_rule;
579 power_rule = &intersected_rule->power_rule;
580
581 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100582 freq_range2->start_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700583 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100584 freq_range2->end_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700585 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100586 freq_range2->max_bandwidth_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700587
588 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
589 if (freq_range->max_bandwidth_khz > freq_diff)
590 freq_range->max_bandwidth_khz = freq_diff;
591
592 power_rule->max_eirp = min(power_rule1->max_eirp,
593 power_rule2->max_eirp);
594 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
595 power_rule2->max_antenna_gain);
596
Johannes Berg1a919312012-12-03 17:21:11 +0100597 intersected_rule->flags = rule1->flags | rule2->flags;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700598
599 if (!is_valid_reg_rule(intersected_rule))
600 return -EINVAL;
601
602 return 0;
603}
604
605/**
606 * regdom_intersect - do the intersection between two regulatory domains
607 * @rd1: first regulatory domain
608 * @rd2: second regulatory domain
609 *
610 * Use this function to get the intersection between two regulatory domains.
611 * Once completed we will mark the alpha2 for the rd as intersected, "98",
612 * as no one single alpha2 can represent this regulatory domain.
613 *
614 * Returns a pointer to the regulatory domain structure which will hold the
615 * resulting intersection of rules between rd1 and rd2. We will
616 * kzalloc() this structure for you.
617 */
Johannes Berg1a919312012-12-03 17:21:11 +0100618static struct ieee80211_regdomain *
619regdom_intersect(const struct ieee80211_regdomain *rd1,
620 const struct ieee80211_regdomain *rd2)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700621{
622 int r, size_of_regd;
623 unsigned int x, y;
624 unsigned int num_rules = 0, rule_idx = 0;
625 const struct ieee80211_reg_rule *rule1, *rule2;
626 struct ieee80211_reg_rule *intersected_rule;
627 struct ieee80211_regdomain *rd;
628 /* This is just a dummy holder to help us count */
Johannes Berg74f53cd2012-12-06 17:26:17 +0100629 struct ieee80211_reg_rule dummy_rule;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700630
631 if (!rd1 || !rd2)
632 return NULL;
633
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500634 /*
635 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700636 * build them. This is to so we can malloc() and free() a
637 * regdomain once. The reason we use reg_rules_intersect() here
638 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500639 * All rules that do check out OK are valid.
640 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700641
642 for (x = 0; x < rd1->n_reg_rules; x++) {
643 rule1 = &rd1->reg_rules[x];
644 for (y = 0; y < rd2->n_reg_rules; y++) {
645 rule2 = &rd2->reg_rules[y];
Johannes Berg74f53cd2012-12-06 17:26:17 +0100646 if (!reg_rules_intersect(rule1, rule2, &dummy_rule))
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700647 num_rules++;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700648 }
649 }
650
651 if (!num_rules)
652 return NULL;
653
654 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg82f20852012-12-04 12:49:16 +0100655 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700656
657 rd = kzalloc(size_of_regd, GFP_KERNEL);
658 if (!rd)
659 return NULL;
660
Johannes Berg8a57fff2012-12-06 17:03:17 +0100661 for (x = 0; x < rd1->n_reg_rules && rule_idx < num_rules; x++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700662 rule1 = &rd1->reg_rules[x];
Johannes Berg8a57fff2012-12-06 17:03:17 +0100663 for (y = 0; y < rd2->n_reg_rules && rule_idx < num_rules; y++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700664 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500665 /*
666 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700667 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500668 * a memcpy()
669 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700670 intersected_rule = &rd->reg_rules[rule_idx];
Johannes Berg1a919312012-12-03 17:21:11 +0100671 r = reg_rules_intersect(rule1, rule2, intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500672 /*
673 * No need to memset here the intersected rule here as
674 * we're not using the stack anymore
675 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700676 if (r)
677 continue;
678 rule_idx++;
679 }
680 }
681
682 if (rule_idx != num_rules) {
683 kfree(rd);
684 return NULL;
685 }
686
687 rd->n_reg_rules = num_rules;
688 rd->alpha2[0] = '9';
689 rd->alpha2[1] = '8';
690
691 return rd;
692}
693
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500694/*
695 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
696 * want to just have the channel structure use these
697 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700698static u32 map_regdom_flags(u32 rd_flags)
699{
700 u32 channel_flags = 0;
701 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
702 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
703 if (rd_flags & NL80211_RRF_NO_IBSS)
704 channel_flags |= IEEE80211_CHAN_NO_IBSS;
705 if (rd_flags & NL80211_RRF_DFS)
706 channel_flags |= IEEE80211_CHAN_RADAR;
Seth Forshee03f6b082012-08-01 15:58:42 -0500707 if (rd_flags & NL80211_RRF_NO_OFDM)
708 channel_flags |= IEEE80211_CHAN_NO_OFDM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700709 return channel_flags;
710}
711
Johannes Berg361c9c82012-12-06 15:57:14 +0100712static const struct ieee80211_reg_rule *
713freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
714 const struct ieee80211_regdomain *regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100715{
716 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800717 bool band_rule_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400718 bool bw_fits = false;
719
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800720 if (!regd)
Johannes Berg361c9c82012-12-06 15:57:14 +0100721 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700722
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800723 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700724 const struct ieee80211_reg_rule *rr;
725 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700726
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800727 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700728 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800729
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500730 /*
731 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800732 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500733 * not overwrite it once found
734 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800735 if (!band_rule_found)
736 band_rule_found = freq_in_rule_band(fr, center_freq);
737
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100738 bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800739
Johannes Berg361c9c82012-12-06 15:57:14 +0100740 if (band_rule_found && bw_fits)
741 return rr;
Johannes Berg8318d782008-01-24 19:38:38 +0100742 }
743
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800744 if (!band_rule_found)
Johannes Berg361c9c82012-12-06 15:57:14 +0100745 return ERR_PTR(-ERANGE);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800746
Johannes Berg361c9c82012-12-06 15:57:14 +0100747 return ERR_PTR(-EINVAL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700748}
749
Johannes Berg361c9c82012-12-06 15:57:14 +0100750const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
751 u32 center_freq)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800752{
Johannes Berg5d885b92012-12-04 00:14:17 +0100753 const struct ieee80211_regdomain *regd;
Johannes Bergc492db32012-12-06 16:29:25 +0100754 struct regulatory_request *lr = get_last_request();
Johannes Berg1a919312012-12-03 17:21:11 +0100755
Johannes Berg5d885b92012-12-04 00:14:17 +0100756 /*
757 * Follow the driver's regulatory domain, if present, unless a country
758 * IE has been processed or a user wants to help complaince further
759 */
Johannes Bergc492db32012-12-06 16:29:25 +0100760 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
761 lr->initiator != NL80211_REGDOM_SET_BY_USER &&
Johannes Berg5d885b92012-12-04 00:14:17 +0100762 wiphy->regd)
Johannes Berg458f4f92012-12-06 15:47:38 +0100763 regd = get_wiphy_regdom(wiphy);
Johannes Berg5d885b92012-12-04 00:14:17 +0100764 else
Johannes Berg458f4f92012-12-06 15:47:38 +0100765 regd = get_cfg80211_regdom();
Johannes Berg5d885b92012-12-04 00:14:17 +0100766
Johannes Berg361c9c82012-12-06 15:57:14 +0100767 return freq_reg_info_regd(wiphy, center_freq, regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800768}
John W. Linville4f366c52010-07-15 14:57:33 -0400769EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700770
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700771const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530772{
773 switch (initiator) {
774 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700775 return "core";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530776 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700777 return "user";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530778 case NL80211_REGDOM_SET_BY_DRIVER:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700779 return "driver";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530780 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700781 return "country IE";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530782 default:
783 WARN_ON(1);
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700784 return "bug";
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530785 }
786}
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700787EXPORT_SYMBOL(reg_initiator_name);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530788
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700789#ifdef CONFIG_CFG80211_REG_DEBUG
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530790static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530791 const struct ieee80211_reg_rule *reg_rule)
792{
793 const struct ieee80211_power_rule *power_rule;
794 const struct ieee80211_freq_range *freq_range;
795 char max_antenna_gain[32];
796
797 power_rule = &reg_rule->power_rule;
798 freq_range = &reg_rule->freq_range;
799
800 if (!power_rule->max_antenna_gain)
801 snprintf(max_antenna_gain, 32, "N/A");
802 else
803 snprintf(max_antenna_gain, 32, "%d", power_rule->max_antenna_gain);
804
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100805 REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
806 chan->center_freq);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530807
Pavel Roskin56e67862011-09-01 11:54:12 -0400808 REG_DBG_PRINT("%d KHz - %d KHz @ %d KHz), (%s mBi, %d mBm)\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100809 freq_range->start_freq_khz, freq_range->end_freq_khz,
810 freq_range->max_bandwidth_khz, max_antenna_gain,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530811 power_rule->max_eirp);
812}
813#else
814static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530815 const struct ieee80211_reg_rule *reg_rule)
816{
817 return;
818}
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530819#endif
820
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400821/*
822 * Note that right now we assume the desired channel bandwidth
823 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100824 * per channel, the primary and the extension channel).
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400825 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700826static void handle_channel(struct wiphy *wiphy,
827 enum nl80211_reg_initiator initiator,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100828 struct ieee80211_channel *chan)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700829{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400830 u32 flags, bw_flags = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700831 const struct ieee80211_reg_rule *reg_rule = NULL;
832 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400833 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500834 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +0100835 struct regulatory_request *lr = get_last_request();
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800836
Johannes Bergc492db32012-12-06 16:29:25 +0100837 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800838
839 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700840
Johannes Berg361c9c82012-12-06 15:57:14 +0100841 reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
842 if (IS_ERR(reg_rule)) {
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700843 /*
844 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300845 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700846 * from a Country IE and the Country IE had no information
847 * about a band. The IEEE 802.11 spec allows for an AP
848 * to send only a subset of the regulatory rules allowed,
849 * so an AP in the US that only supports 2.4 GHz may only send
850 * a country IE with information for the 2.4 GHz band
851 * while 5 GHz is still supported.
852 */
853 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg361c9c82012-12-06 15:57:14 +0100854 PTR_ERR(reg_rule) == -ERANGE)
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700855 return;
856
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700857 REG_DBG_PRINT("Disabling freq %d MHz\n", chan->center_freq);
Johannes Berg990de492013-04-16 14:32:26 +0200858 chan->flags |= IEEE80211_CHAN_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +0100859 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700860 }
Johannes Berg8318d782008-01-24 19:38:38 +0100861
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100862 chan_reg_rule_print_dbg(chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530863
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700864 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400865 freq_range = &reg_rule->freq_range;
866
867 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
868 bw_flags = IEEE80211_CHAN_NO_HT40;
Johannes Bergc7a6ee22012-12-12 17:50:39 +0100869 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(80))
870 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
871 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(160))
872 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700873
Johannes Bergc492db32012-12-06 16:29:25 +0100874 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500875 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +0100876 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500877 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300878 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800879 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500880 * settings
881 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800882 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400883 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800884 chan->max_antenna_gain = chan->orig_mag =
885 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +0200886 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800887 (int) MBM_TO_DBM(power_rule->max_eirp);
888 return;
889 }
890
Simon Wunderlich04f39042013-02-08 18:16:19 +0100891 chan->dfs_state = NL80211_DFS_USABLE;
892 chan->dfs_state_entered = jiffies;
893
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +0530894 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400895 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg1a919312012-12-03 17:21:11 +0100896 chan->max_antenna_gain =
897 min_t(int, chan->orig_mag,
898 MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +0200899 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +0200900 if (chan->orig_mpwr) {
901 /*
902 * Devices that have their own custom regulatory domain
903 * but also use WIPHY_FLAG_STRICT_REGULATORY will follow the
904 * passed country IE power settings.
905 */
906 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
907 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
908 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
909 chan->max_power = chan->max_reg_power;
910 else
911 chan->max_power = min(chan->orig_mpwr,
912 chan->max_reg_power);
913 } else
914 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +0100915}
916
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700917static void handle_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100918 enum nl80211_reg_initiator initiator,
919 struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100920{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800921 unsigned int i;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800922
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100923 if (!sband)
924 return;
Johannes Berg8318d782008-01-24 19:38:38 +0100925
926 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100927 handle_channel(wiphy, initiator, &sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +0100928}
929
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700930static bool reg_request_cell_base(struct regulatory_request *request)
931{
932 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
933 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100934 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700935}
936
937bool reg_last_request_cell_base(void)
938{
Johannes Berg38fd2142013-05-10 19:17:17 +0200939 return reg_request_cell_base(get_last_request());
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700940}
941
942#ifdef CONFIG_CFG80211_CERTIFICATION_ONUS
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700943/* Core specific check */
Johannes Berg2f922122012-12-03 17:54:55 +0100944static enum reg_request_treatment
945reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700946{
Johannes Bergc492db32012-12-06 16:29:25 +0100947 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700948
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700949 if (!reg_num_devs_support_basehint)
Johannes Berg2f922122012-12-03 17:54:55 +0100950 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700951
Johannes Bergc492db32012-12-06 16:29:25 +0100952 if (reg_request_cell_base(lr) &&
Johannes Berg1a919312012-12-03 17:21:11 +0100953 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +0100954 return REG_REQ_ALREADY_SET;
Johannes Berg1a919312012-12-03 17:21:11 +0100955
Johannes Berg2f922122012-12-03 17:54:55 +0100956 return REG_REQ_OK;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700957}
958
959/* Device specific check */
960static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
961{
Johannes Berg1a919312012-12-03 17:21:11 +0100962 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700963}
964#else
965static int reg_ignore_cell_hint(struct regulatory_request *pending_request)
966{
Johannes Berg2f922122012-12-03 17:54:55 +0100967 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700968}
Johannes Berg1a919312012-12-03 17:21:11 +0100969
970static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700971{
972 return true;
973}
974#endif
975
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -0700976static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
977{
978 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY &&
979 !(wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY))
980 return true;
981 return false;
982}
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700983
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400984static bool ignore_reg_update(struct wiphy *wiphy,
985 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800986{
Johannes Bergc492db32012-12-06 16:29:25 +0100987 struct regulatory_request *lr = get_last_request();
988
989 if (!lr) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700990 REG_DBG_PRINT("Ignoring regulatory request set by %s "
991 "since last_request is not set\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530992 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800993 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530994 }
995
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400996 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530997 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -0700998 REG_DBG_PRINT("Ignoring regulatory request set by %s "
999 "since the driver uses its own custom "
1000 "regulatory domain\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301001 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001002 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301003 }
1004
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001005 /*
1006 * wiphy->regd will be set once the device has its own
1007 * desired regulatory domain set
1008 */
Luis R. Rodriguezfa1fb9c2013-10-02 18:33:10 -07001009 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -07001010 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Bergc492db32012-12-06 16:29:25 +01001011 !is_world_regdom(lr->alpha2)) {
Luis R. Rodriguez034c6d62013-10-14 17:42:06 -07001012 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1013 "since the driver requires its own regulatory "
1014 "domain to be set first\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301015 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001016 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +05301017 }
1018
Johannes Bergc492db32012-12-06 16:29:25 +01001019 if (reg_request_cell_base(lr))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001020 return reg_dev_ignore_cell_hint(wiphy);
1021
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001022 return false;
1023}
1024
Luis R. Rodriguez3195e482012-12-19 10:53:03 -08001025static bool reg_is_world_roaming(struct wiphy *wiphy)
1026{
1027 const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
1028 const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
1029 struct regulatory_request *lr = get_last_request();
1030
1031 if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
1032 return true;
1033
1034 if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1035 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1036 return true;
1037
1038 return false;
1039}
1040
Johannes Berg1a919312012-12-03 17:21:11 +01001041static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001042 struct reg_beacon *reg_beacon)
1043{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001044 struct ieee80211_supported_band *sband;
1045 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001046 bool channel_changed = false;
1047 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001048
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001049 sband = wiphy->bands[reg_beacon->chan.band];
1050 chan = &sband->channels[chan_idx];
1051
1052 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1053 return;
1054
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001055 if (chan->beacon_found)
1056 return;
1057
1058 chan->beacon_found = true;
1059
Luis R. Rodriguez0f500a52012-12-19 10:53:04 -08001060 if (!reg_is_world_roaming(wiphy))
1061 return;
1062
Johannes Berg5be83de2009-11-19 00:56:28 +01001063 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001064 return;
1065
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001066 chan_before.center_freq = chan->center_freq;
1067 chan_before.flags = chan->flags;
1068
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001069 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001070 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001071 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001072 }
1073
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001074 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001075 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001076 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001077 }
1078
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001079 if (channel_changed)
1080 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001081}
1082
1083/*
1084 * Called when a scan on a wiphy finds a beacon on
1085 * new channel
1086 */
1087static void wiphy_update_new_beacon(struct wiphy *wiphy,
1088 struct reg_beacon *reg_beacon)
1089{
1090 unsigned int i;
1091 struct ieee80211_supported_band *sband;
1092
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001093 if (!wiphy->bands[reg_beacon->chan.band])
1094 return;
1095
1096 sband = wiphy->bands[reg_beacon->chan.band];
1097
1098 for (i = 0; i < sband->n_channels; i++)
1099 handle_reg_beacon(wiphy, i, reg_beacon);
1100}
1101
1102/*
1103 * Called upon reg changes or a new wiphy is added
1104 */
1105static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1106{
1107 unsigned int i;
1108 struct ieee80211_supported_band *sband;
1109 struct reg_beacon *reg_beacon;
1110
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001111 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1112 if (!wiphy->bands[reg_beacon->chan.band])
1113 continue;
1114 sband = wiphy->bands[reg_beacon->chan.band];
1115 for (i = 0; i < sband->n_channels; i++)
1116 handle_reg_beacon(wiphy, i, reg_beacon);
1117 }
1118}
1119
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001120/* Reap the advantages of previously found beacons */
1121static void reg_process_beacons(struct wiphy *wiphy)
1122{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001123 /*
1124 * Means we are just firing up cfg80211, so no beacons would
1125 * have been processed yet.
1126 */
1127 if (!last_request)
1128 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001129 wiphy_update_beacon_reg(wiphy);
1130}
1131
Johannes Berg1a919312012-12-03 17:21:11 +01001132static bool is_ht40_allowed(struct ieee80211_channel *chan)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001133{
1134 if (!chan)
Johannes Berg1a919312012-12-03 17:21:11 +01001135 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001136 if (chan->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg1a919312012-12-03 17:21:11 +01001137 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001138 /* This would happen when regulatory rules disallow HT40 completely */
Felix Fietkau55b183a2013-01-11 14:22:58 +01001139 if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
1140 return false;
1141 return true;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001142}
1143
1144static void reg_process_ht_flags_channel(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001145 struct ieee80211_channel *channel)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001146{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001147 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001148 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1149 unsigned int i;
1150
Johannes Berg1a919312012-12-03 17:21:11 +01001151 if (!is_ht40_allowed(channel)) {
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001152 channel->flags |= IEEE80211_CHAN_NO_HT40;
1153 return;
1154 }
1155
1156 /*
1157 * We need to ensure the extension channels exist to
1158 * be able to use HT40- or HT40+, this finds them (or not)
1159 */
1160 for (i = 0; i < sband->n_channels; i++) {
1161 struct ieee80211_channel *c = &sband->channels[i];
Johannes Berg1a919312012-12-03 17:21:11 +01001162
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001163 if (c->center_freq == (channel->center_freq - 20))
1164 channel_before = c;
1165 if (c->center_freq == (channel->center_freq + 20))
1166 channel_after = c;
1167 }
1168
1169 /*
1170 * Please note that this assumes target bandwidth is 20 MHz,
1171 * if that ever changes we also need to change the below logic
1172 * to include that as well.
1173 */
Johannes Berg1a919312012-12-03 17:21:11 +01001174 if (!is_ht40_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001175 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001176 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001177 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001178
Johannes Berg1a919312012-12-03 17:21:11 +01001179 if (!is_ht40_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001180 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001181 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001182 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001183}
1184
1185static void reg_process_ht_flags_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001186 struct ieee80211_supported_band *sband)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001187{
1188 unsigned int i;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001189
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001190 if (!sband)
1191 return;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001192
1193 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001194 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001195}
1196
1197static void reg_process_ht_flags(struct wiphy *wiphy)
1198{
1199 enum ieee80211_band band;
1200
1201 if (!wiphy)
1202 return;
1203
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001204 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1205 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001206}
1207
Sven Neumanneac03e32011-08-30 23:38:53 +02001208static void wiphy_update_regulatory(struct wiphy *wiphy,
1209 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001210{
1211 enum ieee80211_band band;
Johannes Bergc492db32012-12-06 16:29:25 +01001212 struct regulatory_request *lr = get_last_request();
Sven Neumanneac03e32011-08-30 23:38:53 +02001213
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001214 if (ignore_reg_update(wiphy, initiator))
Sven Neumanna203c2a2011-07-12 15:52:07 +02001215 return;
1216
Johannes Bergc492db32012-12-06 16:29:25 +01001217 lr->dfs_region = get_cfg80211_regdom()->dfs_region;
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07001218
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001219 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1220 handle_band(wiphy, initiator, wiphy->bands[band]);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001221
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001222 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001223 reg_process_ht_flags(wiphy);
Johannes Berg1a919312012-12-03 17:21:11 +01001224
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001225 if (wiphy->reg_notifier)
Johannes Bergc492db32012-12-06 16:29:25 +01001226 wiphy->reg_notifier(wiphy, lr);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001227}
1228
Sven Neumannd7549cb2011-08-30 23:38:54 +02001229static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1230{
1231 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301232 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02001233
Johannes Berg5fe231e2013-05-08 21:45:15 +02001234 ASSERT_RTNL();
Johannes Berg458f4f92012-12-06 15:47:38 +01001235
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301236 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1237 wiphy = &rdev->wiphy;
1238 wiphy_update_regulatory(wiphy, initiator);
1239 /*
1240 * Regulatory updates set by CORE are ignored for custom
1241 * regulatory cards. Let us notify the changes to the driver,
1242 * as some drivers used this to restore its orig_* reg domain.
1243 */
1244 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1245 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
1246 wiphy->reg_notifier)
Johannes Bergc492db32012-12-06 16:29:25 +01001247 wiphy->reg_notifier(wiphy, get_last_request());
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301248 }
Sven Neumannd7549cb2011-08-30 23:38:54 +02001249}
1250
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001251static void handle_channel_custom(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001252 struct ieee80211_channel *chan,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001253 const struct ieee80211_regdomain *regd)
1254{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001255 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001256 const struct ieee80211_reg_rule *reg_rule = NULL;
1257 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001258 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001259
Johannes Berg361c9c82012-12-06 15:57:14 +01001260 reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1261 regd);
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001262
Johannes Berg361c9c82012-12-06 15:57:14 +01001263 if (IS_ERR(reg_rule)) {
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001264 REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1265 chan->center_freq);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001266 chan->flags = IEEE80211_CHAN_DISABLED;
1267 return;
1268 }
1269
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001270 chan_reg_rule_print_dbg(chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301271
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001272 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001273 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001274
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001275 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1276 bw_flags = IEEE80211_CHAN_NO_HT40;
Johannes Bergc7a6ee22012-12-12 17:50:39 +01001277 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(80))
1278 bw_flags |= IEEE80211_CHAN_NO_80MHZ;
1279 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(160))
1280 bw_flags |= IEEE80211_CHAN_NO_160MHZ;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001281
1282 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001283 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001284 chan->max_reg_power = chan->max_power =
1285 (int) MBM_TO_DBM(power_rule->max_eirp);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001286}
1287
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001288static void handle_band_custom(struct wiphy *wiphy,
1289 struct ieee80211_supported_band *sband,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001290 const struct ieee80211_regdomain *regd)
1291{
1292 unsigned int i;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001293
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001294 if (!sband)
1295 return;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001296
1297 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001298 handle_channel_custom(wiphy, &sband->channels[i], regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001299}
1300
1301/* Used by drivers prior to wiphy registration */
1302void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1303 const struct ieee80211_regdomain *regd)
1304{
1305 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001306 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001307
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001308 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001309 if (!wiphy->bands[band])
1310 continue;
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001311 handle_band_custom(wiphy, wiphy->bands[band], regd);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001312 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001313 }
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001314
1315 /*
1316 * no point in calling this if it won't have any effect
Johannes Berg1a919312012-12-03 17:21:11 +01001317 * on your device's supported bands.
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001318 */
1319 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001320}
1321EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1322
Johannes Berg84fa4f42008-10-24 20:32:23 +02001323/* This has the logic which determines when a new request
1324 * should be ignored. */
Johannes Berg2f922122012-12-03 17:54:55 +01001325static enum reg_request_treatment
1326get_reg_request_treatment(struct wiphy *wiphy,
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001327 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001328{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001329 struct wiphy *last_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01001330 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001331
Johannes Berg84fa4f42008-10-24 20:32:23 +02001332 /* All initial requests are respected */
Johannes Bergc492db32012-12-06 16:29:25 +01001333 if (!lr)
Johannes Berg2f922122012-12-03 17:54:55 +01001334 return REG_REQ_OK;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001335
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001336 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001337 case NL80211_REGDOM_SET_BY_CORE:
Johannes Berg2f922122012-12-03 17:54:55 +01001338 return REG_REQ_OK;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001339 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Johannes Bergc492db32012-12-06 16:29:25 +01001340 if (reg_request_cell_base(lr)) {
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001341 /* Trust a Cell base station over the AP's country IE */
1342 if (regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001343 return REG_REQ_IGNORE;
1344 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001345 }
1346
Johannes Bergc492db32012-12-06 16:29:25 +01001347 last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001348
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001349 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001350 return -EINVAL;
Johannes Bergc492db32012-12-06 16:29:25 +01001351 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001352 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001353 /*
1354 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001355 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001356 * intersect them, but that seems unlikely
1357 * to be correct. Reject second one for now.
1358 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001359 if (regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001360 return REG_REQ_IGNORE;
1361 return REG_REQ_ALREADY_SET;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001362 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001363 /*
1364 * Two consecutive Country IE hints on the same wiphy.
1365 * This should be picked up early by the driver/stack
1366 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001367 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg2f922122012-12-03 17:54:55 +01001368 return REG_REQ_OK;
1369 return REG_REQ_ALREADY_SET;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001370 }
Johannes Berg34301402013-06-04 14:35:07 +02001371 return REG_REQ_OK;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001372 case NL80211_REGDOM_SET_BY_DRIVER:
Johannes Bergc492db32012-12-06 16:29:25 +01001373 if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001374 if (regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001375 return REG_REQ_OK;
1376 return REG_REQ_ALREADY_SET;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001377 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001378
1379 /*
1380 * This would happen if you unplug and plug your card
1381 * back in or if you add a new device for which the previously
1382 * loaded card also agrees on the regulatory domain.
1383 */
Johannes Bergc492db32012-12-06 16:29:25 +01001384 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001385 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001386 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001387
Johannes Berg2f922122012-12-03 17:54:55 +01001388 return REG_REQ_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001389 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001390 if (reg_request_cell_base(pending_request))
1391 return reg_ignore_cell_hint(pending_request);
1392
Johannes Bergc492db32012-12-06 16:29:25 +01001393 if (reg_request_cell_base(lr))
Johannes Berg2f922122012-12-03 17:54:55 +01001394 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001395
Johannes Bergc492db32012-12-06 16:29:25 +01001396 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Johannes Berg2f922122012-12-03 17:54:55 +01001397 return REG_REQ_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001398 /*
1399 * If the user knows better the user should set the regdom
1400 * to their country before the IE is picked up
1401 */
Johannes Bergc492db32012-12-06 16:29:25 +01001402 if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
1403 lr->intersect)
Johannes Berg2f922122012-12-03 17:54:55 +01001404 return REG_REQ_IGNORE;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001405 /*
1406 * Process user requests only after previous user/driver/core
1407 * requests have been processed
1408 */
Johannes Bergc492db32012-12-06 16:29:25 +01001409 if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
1410 lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1411 lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
1412 regdom_changes(lr->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001413 return REG_REQ_IGNORE;
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001414
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001415 if (!regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001416 return REG_REQ_ALREADY_SET;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001417
Johannes Berg2f922122012-12-03 17:54:55 +01001418 return REG_REQ_OK;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001419 }
1420
Johannes Berg2f922122012-12-03 17:54:55 +01001421 return REG_REQ_IGNORE;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001422}
1423
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001424static void reg_set_request_processed(void)
1425{
1426 bool need_more_processing = false;
Johannes Bergc492db32012-12-06 16:29:25 +01001427 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001428
Johannes Bergc492db32012-12-06 16:29:25 +01001429 lr->processed = true;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001430
1431 spin_lock(&reg_requests_lock);
1432 if (!list_empty(&reg_requests_list))
1433 need_more_processing = true;
1434 spin_unlock(&reg_requests_lock);
1435
Johannes Bergc492db32012-12-06 16:29:25 +01001436 if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
Eliad Pellerfe20b392012-06-12 12:53:13 +03001437 cancel_delayed_work(&reg_timeout);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001438
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001439 if (need_more_processing)
1440 schedule_work(&reg_work);
1441}
1442
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001443/**
1444 * __regulatory_hint - hint to the wireless core a regulatory domain
1445 * @wiphy: if the hint comes from country information from an AP, this
1446 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001447 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001448 *
1449 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001450 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001451 *
Johannes Berg2f922122012-12-03 17:54:55 +01001452 * Returns one of the different reg request treatment values.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001453 */
Johannes Berg2f922122012-12-03 17:54:55 +01001454static enum reg_request_treatment
1455__regulatory_hint(struct wiphy *wiphy,
1456 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001457{
Johannes Berge9763c32012-12-03 16:59:46 +01001458 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001459 bool intersect = false;
Johannes Berg2f922122012-12-03 17:54:55 +01001460 enum reg_request_treatment treatment;
Johannes Bergc492db32012-12-06 16:29:25 +01001461 struct regulatory_request *lr;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001462
Johannes Berg2f922122012-12-03 17:54:55 +01001463 treatment = get_reg_request_treatment(wiphy, pending_request);
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001464
Johannes Berg2f922122012-12-03 17:54:55 +01001465 switch (treatment) {
1466 case REG_REQ_INTERSECT:
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001467 if (pending_request->initiator ==
1468 NL80211_REGDOM_SET_BY_DRIVER) {
Johannes Berg458f4f92012-12-06 15:47:38 +01001469 regd = reg_copy_regd(get_cfg80211_regdom());
Johannes Berge9763c32012-12-03 16:59:46 +01001470 if (IS_ERR(regd)) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001471 kfree(pending_request);
Johannes Berge9763c32012-12-03 16:59:46 +01001472 return PTR_ERR(regd);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001473 }
Johannes Berg458f4f92012-12-06 15:47:38 +01001474 rcu_assign_pointer(wiphy->regd, regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001475 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001476 intersect = true;
Johannes Berg2f922122012-12-03 17:54:55 +01001477 break;
1478 case REG_REQ_OK:
1479 break;
1480 default:
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001481 /*
1482 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001483 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001484 * wiphy
1485 */
Johannes Berg2f922122012-12-03 17:54:55 +01001486 if (treatment == REG_REQ_ALREADY_SET &&
1487 pending_request->initiator == NL80211_REGDOM_SET_BY_DRIVER) {
Johannes Berg458f4f92012-12-06 15:47:38 +01001488 regd = reg_copy_regd(get_cfg80211_regdom());
Johannes Berge9763c32012-12-03 16:59:46 +01001489 if (IS_ERR(regd)) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001490 kfree(pending_request);
Johannes Berg2f922122012-12-03 17:54:55 +01001491 return REG_REQ_IGNORE;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001492 }
Johannes Berg2f922122012-12-03 17:54:55 +01001493 treatment = REG_REQ_ALREADY_SET;
Johannes Berg458f4f92012-12-06 15:47:38 +01001494 rcu_assign_pointer(wiphy->regd, regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001495 goto new_request;
1496 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001497 kfree(pending_request);
Johannes Berg2f922122012-12-03 17:54:55 +01001498 return treatment;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001499 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001500
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001501new_request:
Johannes Bergc492db32012-12-06 16:29:25 +01001502 lr = get_last_request();
1503 if (lr != &core_request_world && lr)
1504 kfree_rcu(lr, rcu_head);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001505
Johannes Bergc492db32012-12-06 16:29:25 +01001506 pending_request->intersect = intersect;
1507 pending_request->processed = false;
1508 rcu_assign_pointer(last_request, pending_request);
1509 lr = pending_request;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001510
1511 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001512
Johannes Bergc492db32012-12-06 16:29:25 +01001513 if (lr->initiator == NL80211_REGDOM_SET_BY_USER) {
1514 user_alpha2[0] = lr->alpha2[0];
1515 user_alpha2[1] = lr->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001516 }
1517
Johannes Berg2f922122012-12-03 17:54:55 +01001518 /* When r == REG_REQ_INTERSECT we do need to call CRDA */
1519 if (treatment != REG_REQ_OK && treatment != REG_REQ_INTERSECT) {
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001520 /*
1521 * Since CRDA will not be called in this case as we already
1522 * have applied the requested regulatory domain before we just
1523 * inform userspace we have processed the request
1524 */
Johannes Berg2f922122012-12-03 17:54:55 +01001525 if (treatment == REG_REQ_ALREADY_SET) {
Johannes Bergc492db32012-12-06 16:29:25 +01001526 nl80211_send_reg_change_event(lr);
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001527 reg_set_request_processed();
1528 }
Johannes Berg2f922122012-12-03 17:54:55 +01001529 return treatment;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001530 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001531
Johannes Bergc492db32012-12-06 16:29:25 +01001532 if (call_crda(lr->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001533 return REG_REQ_IGNORE;
1534 return REG_REQ_OK;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001535}
1536
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001537/* This processes *all* regulatory hints */
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001538static void reg_process_hint(struct regulatory_request *reg_request,
1539 enum nl80211_reg_initiator reg_initiator)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001540{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001541 struct wiphy *wiphy = NULL;
1542
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001543 if (WARN_ON(!reg_request->alpha2))
1544 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001545
Johannes Bergf4173762012-12-03 18:23:37 +01001546 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001547 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1548
Johannes Berg1a919312012-12-03 17:21:11 +01001549 if (reg_initiator == NL80211_REGDOM_SET_BY_DRIVER && !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001550 kfree(reg_request);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001551 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001552 }
1553
Johannes Berg2f922122012-12-03 17:54:55 +01001554 switch (__regulatory_hint(wiphy, reg_request)) {
1555 case REG_REQ_ALREADY_SET:
1556 /* This is required so that the orig_* parameters are saved */
1557 if (wiphy && wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
1558 wiphy_update_regulatory(wiphy, reg_initiator);
1559 break;
1560 default:
1561 if (reg_initiator == NL80211_REGDOM_SET_BY_USER)
1562 schedule_delayed_work(&reg_timeout,
1563 msecs_to_jiffies(3142));
1564 break;
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001565 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001566}
1567
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001568/*
1569 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1570 * Regulatory hints come on a first come first serve basis and we
1571 * must process each one atomically.
1572 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001573static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001574{
Johannes Bergc492db32012-12-06 16:29:25 +01001575 struct regulatory_request *reg_request, *lr;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001576
Johannes Bergc492db32012-12-06 16:29:25 +01001577 lr = get_last_request();
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001578
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001579 /* When last_request->processed becomes true this will be rescheduled */
Johannes Bergc492db32012-12-06 16:29:25 +01001580 if (lr && !lr->processed) {
Johannes Berg1a919312012-12-03 17:21:11 +01001581 REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n");
Johannes Berg5fe231e2013-05-08 21:45:15 +02001582 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001583 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001584
1585 spin_lock(&reg_requests_lock);
1586
1587 if (list_empty(&reg_requests_list)) {
1588 spin_unlock(&reg_requests_lock);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001589 return;
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001590 }
1591
1592 reg_request = list_first_entry(&reg_requests_list,
1593 struct regulatory_request,
1594 list);
1595 list_del_init(&reg_request->list);
1596
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001597 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001598
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001599 reg_process_hint(reg_request, reg_request->initiator);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001600}
1601
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001602/* Processes beacon hints -- this has nothing to do with country IEs */
1603static void reg_process_pending_beacon_hints(void)
1604{
Johannes Berg79c97e92009-07-07 03:56:12 +02001605 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001606 struct reg_beacon *pending_beacon, *tmp;
1607
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001608 /* This goes through the _pending_ beacon list */
1609 spin_lock_bh(&reg_pending_beacons_lock);
1610
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001611 list_for_each_entry_safe(pending_beacon, tmp,
1612 &reg_pending_beacons, list) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001613 list_del_init(&pending_beacon->list);
1614
1615 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001616 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1617 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001618
1619 /* Remembers the beacon hint for new wiphys or reg changes */
1620 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1621 }
1622
1623 spin_unlock_bh(&reg_pending_beacons_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001624}
1625
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001626static void reg_todo(struct work_struct *work)
1627{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001628 rtnl_lock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001629 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001630 reg_process_pending_beacon_hints();
Johannes Berg5fe231e2013-05-08 21:45:15 +02001631 rtnl_unlock();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001632}
1633
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001634static void queue_regulatory_request(struct regulatory_request *request)
1635{
Johannes Bergd4f2c882012-12-03 18:59:58 +01001636 request->alpha2[0] = toupper(request->alpha2[0]);
1637 request->alpha2[1] = toupper(request->alpha2[1]);
John W. Linvillec61029c2010-08-05 14:26:24 -04001638
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001639 spin_lock(&reg_requests_lock);
1640 list_add_tail(&request->list, &reg_requests_list);
1641 spin_unlock(&reg_requests_lock);
1642
1643 schedule_work(&reg_work);
1644}
1645
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001646/*
1647 * Core regulatory hint -- happens during cfg80211_init()
1648 * and when we restore regulatory settings.
1649 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001650static int regulatory_hint_core(const char *alpha2)
1651{
1652 struct regulatory_request *request;
1653
Johannes Berg1a919312012-12-03 17:21:11 +01001654 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001655 if (!request)
1656 return -ENOMEM;
1657
1658 request->alpha2[0] = alpha2[0];
1659 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001660 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001661
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08001662 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001663
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001664 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001665}
1666
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001667/* User hints */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001668int regulatory_hint_user(const char *alpha2,
1669 enum nl80211_user_reg_hint_type user_reg_hint_type)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001670{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001671 struct regulatory_request *request;
1672
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001673 if (WARN_ON(!alpha2))
1674 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001675
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001676 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1677 if (!request)
1678 return -ENOMEM;
1679
Johannes Bergf4173762012-12-03 18:23:37 +01001680 request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001681 request->alpha2[0] = alpha2[0];
1682 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001683 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001684 request->user_reg_hint_type = user_reg_hint_type;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001685
1686 queue_regulatory_request(request);
1687
1688 return 0;
1689}
1690
1691/* Driver hints */
1692int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1693{
1694 struct regulatory_request *request;
1695
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001696 if (WARN_ON(!alpha2 || !wiphy))
1697 return -EINVAL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001698
1699 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1700 if (!request)
1701 return -ENOMEM;
1702
1703 request->wiphy_idx = get_wiphy_idx(wiphy);
1704
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001705 request->alpha2[0] = alpha2[0];
1706 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001707 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001708
1709 queue_regulatory_request(request);
1710
1711 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001712}
1713EXPORT_SYMBOL(regulatory_hint);
1714
Luis R. Rodriguez789fd032013-10-04 18:07:24 -07001715void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
1716 const u8 *country_ie, u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001717{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001718 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001719 enum environment_cap env = ENVIRON_ANY;
Johannes Bergdb2424c2013-05-10 19:07:52 +02001720 struct regulatory_request *request = NULL, *lr;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001721
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001722 /* IE len must be evenly divisible by 2 */
1723 if (country_ie_len & 0x01)
Johannes Bergdb2424c2013-05-10 19:07:52 +02001724 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001725
1726 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Bergdb2424c2013-05-10 19:07:52 +02001727 return;
1728
1729 request = kzalloc(sizeof(*request), GFP_KERNEL);
1730 if (!request)
1731 return;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001732
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001733 alpha2[0] = country_ie[0];
1734 alpha2[1] = country_ie[1];
1735
1736 if (country_ie[2] == 'I')
1737 env = ENVIRON_INDOOR;
1738 else if (country_ie[2] == 'O')
1739 env = ENVIRON_OUTDOOR;
1740
Johannes Bergdb2424c2013-05-10 19:07:52 +02001741 rcu_read_lock();
1742 lr = get_last_request();
1743
1744 if (unlikely(!lr))
1745 goto out;
1746
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001747 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001748 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001749 * We leave conflict resolution to the workqueue, where can hold
Johannes Berg5fe231e2013-05-08 21:45:15 +02001750 * the RTNL.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001751 */
Johannes Bergc492db32012-12-06 16:29:25 +01001752 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1753 lr->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001754 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001755
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001756 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04001757 request->alpha2[0] = alpha2[0];
1758 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001759 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001760 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001761
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001762 queue_regulatory_request(request);
Johannes Bergdb2424c2013-05-10 19:07:52 +02001763 request = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001764out:
Johannes Bergdb2424c2013-05-10 19:07:52 +02001765 kfree(request);
1766 rcu_read_unlock();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001767}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001768
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001769static void restore_alpha2(char *alpha2, bool reset_user)
1770{
1771 /* indicates there is no alpha2 to consider for restoration */
1772 alpha2[0] = '9';
1773 alpha2[1] = '7';
1774
1775 /* The user setting has precedence over the module parameter */
1776 if (is_user_regdom_saved()) {
1777 /* Unless we're asked to ignore it and reset it */
1778 if (reset_user) {
Johannes Berg1a919312012-12-03 17:21:11 +01001779 REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001780 user_alpha2[0] = '9';
1781 user_alpha2[1] = '7';
1782
1783 /*
1784 * If we're ignoring user settings, we still need to
1785 * check the module parameter to ensure we put things
1786 * back as they were for a full restore.
1787 */
1788 if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01001789 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
1790 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001791 alpha2[0] = ieee80211_regdom[0];
1792 alpha2[1] = ieee80211_regdom[1];
1793 }
1794 } else {
Johannes Berg1a919312012-12-03 17:21:11 +01001795 REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
1796 user_alpha2[0], user_alpha2[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001797 alpha2[0] = user_alpha2[0];
1798 alpha2[1] = user_alpha2[1];
1799 }
1800 } else if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01001801 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
1802 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001803 alpha2[0] = ieee80211_regdom[0];
1804 alpha2[1] = ieee80211_regdom[1];
1805 } else
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001806 REG_DBG_PRINT("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001807}
1808
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301809static void restore_custom_reg_settings(struct wiphy *wiphy)
1810{
1811 struct ieee80211_supported_band *sband;
1812 enum ieee80211_band band;
1813 struct ieee80211_channel *chan;
1814 int i;
1815
1816 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1817 sband = wiphy->bands[band];
1818 if (!sband)
1819 continue;
1820 for (i = 0; i < sband->n_channels; i++) {
1821 chan = &sband->channels[i];
1822 chan->flags = chan->orig_flags;
1823 chan->max_antenna_gain = chan->orig_mag;
1824 chan->max_power = chan->orig_mpwr;
Paul Stewart899852a2012-08-01 16:54:42 -07001825 chan->beacon_found = false;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301826 }
1827 }
1828}
1829
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001830/*
1831 * Restoring regulatory settings involves ingoring any
1832 * possibly stale country IE information and user regulatory
1833 * settings if so desired, this includes any beacon hints
1834 * learned as we could have traveled outside to another country
1835 * after disconnection. To restore regulatory settings we do
1836 * exactly what we did at bootup:
1837 *
1838 * - send a core regulatory hint
1839 * - send a user regulatory hint if applicable
1840 *
1841 * Device drivers that send a regulatory hint for a specific country
1842 * keep their own regulatory domain on wiphy->regd so that does does
1843 * not need to be remembered.
1844 */
1845static void restore_regulatory_settings(bool reset_user)
1846{
1847 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08001848 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001849 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001850 struct regulatory_request *reg_request, *tmp;
1851 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301852 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001853
Johannes Berg5fe231e2013-05-08 21:45:15 +02001854 ASSERT_RTNL();
1855
Johannes Berg2d319862013-01-09 12:01:38 +01001856 reset_regdomains(true, &world_regdom);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001857 restore_alpha2(alpha2, reset_user);
1858
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001859 /*
1860 * If there's any pending requests we simply
1861 * stash them to a temporary pending queue and
1862 * add then after we've restored regulatory
1863 * settings.
1864 */
1865 spin_lock(&reg_requests_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01001866 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
1867 if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
1868 continue;
1869 list_move_tail(&reg_request->list, &tmp_reg_req_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001870 }
1871 spin_unlock(&reg_requests_lock);
1872
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001873 /* Clear beacon hints */
1874 spin_lock_bh(&reg_pending_beacons_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01001875 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
1876 list_del(&reg_beacon->list);
1877 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001878 }
1879 spin_unlock_bh(&reg_pending_beacons_lock);
1880
Johannes Bergfea9bce2012-12-03 17:32:01 +01001881 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
1882 list_del(&reg_beacon->list);
1883 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001884 }
1885
1886 /* First restore to the basic regulatory settings */
Johannes Berg379b82f2012-12-06 15:44:07 +01001887 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
1888 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001889
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301890 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1891 if (rdev->wiphy.flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1892 restore_custom_reg_settings(&rdev->wiphy);
1893 }
1894
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08001895 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001896
1897 /*
1898 * This restores the ieee80211_regdom module parameter
1899 * preference or the last user requested regulatory
1900 * settings, user regulatory settings takes precedence.
1901 */
1902 if (is_an_alpha2(alpha2))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001903 regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001904
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001905 spin_lock(&reg_requests_lock);
Johannes Berg11cff962012-12-03 18:56:41 +01001906 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001907 spin_unlock(&reg_requests_lock);
1908
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001909 REG_DBG_PRINT("Kicking the queue\n");
1910
1911 schedule_work(&reg_work);
1912}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001913
1914void regulatory_hint_disconnect(void)
1915{
Johannes Berg1a919312012-12-03 17:21:11 +01001916 REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001917 restore_regulatory_settings(false);
1918}
1919
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001920static bool freq_is_chan_12_13_14(u16 freq)
1921{
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001922 if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
1923 freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
1924 freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001925 return true;
1926 return false;
1927}
1928
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08001929static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
1930{
1931 struct reg_beacon *pending_beacon;
1932
1933 list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
1934 if (beacon_chan->center_freq ==
1935 pending_beacon->chan.center_freq)
1936 return true;
1937 return false;
1938}
1939
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001940int regulatory_hint_found_beacon(struct wiphy *wiphy,
1941 struct ieee80211_channel *beacon_chan,
1942 gfp_t gfp)
1943{
1944 struct reg_beacon *reg_beacon;
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08001945 bool processing;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001946
Johannes Berg1a919312012-12-03 17:21:11 +01001947 if (beacon_chan->beacon_found ||
1948 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001949 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg1a919312012-12-03 17:21:11 +01001950 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001951 return 0;
1952
Luis R. Rodriguez3ebfa6e2012-12-19 10:53:02 -08001953 spin_lock_bh(&reg_pending_beacons_lock);
1954 processing = pending_reg_beacon(beacon_chan);
1955 spin_unlock_bh(&reg_pending_beacons_lock);
1956
1957 if (processing)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001958 return 0;
1959
1960 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1961 if (!reg_beacon)
1962 return -ENOMEM;
1963
Johannes Berg1a919312012-12-03 17:21:11 +01001964 REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001965 beacon_chan->center_freq,
1966 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1967 wiphy_name(wiphy));
1968
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001969 memcpy(&reg_beacon->chan, beacon_chan,
Johannes Berg1a919312012-12-03 17:21:11 +01001970 sizeof(struct ieee80211_channel));
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001971
1972 /*
1973 * Since we can be called from BH or and non-BH context
1974 * we must use spin_lock_bh()
1975 */
1976 spin_lock_bh(&reg_pending_beacons_lock);
1977 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1978 spin_unlock_bh(&reg_pending_beacons_lock);
1979
1980 schedule_work(&reg_work);
1981
1982 return 0;
1983}
1984
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001985static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001986{
1987 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001988 const struct ieee80211_reg_rule *reg_rule = NULL;
1989 const struct ieee80211_freq_range *freq_range = NULL;
1990 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001991
Luis R. Rodriguez66533252011-12-20 12:23:37 -08001992 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001993
1994 for (i = 0; i < rd->n_reg_rules; i++) {
1995 reg_rule = &rd->reg_rules[i];
1996 freq_range = &reg_rule->freq_range;
1997 power_rule = &reg_rule->power_rule;
1998
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001999 /*
2000 * There may not be documentation for max antenna gain
2001 * in certain regions
2002 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002003 if (power_rule->max_antenna_gain)
Luis R. Rodriguez66533252011-12-20 12:23:37 -08002004 pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002005 freq_range->start_freq_khz,
2006 freq_range->end_freq_khz,
2007 freq_range->max_bandwidth_khz,
2008 power_rule->max_antenna_gain,
2009 power_rule->max_eirp);
2010 else
Luis R. Rodriguez66533252011-12-20 12:23:37 -08002011 pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002012 freq_range->start_freq_khz,
2013 freq_range->end_freq_khz,
2014 freq_range->max_bandwidth_khz,
2015 power_rule->max_eirp);
2016 }
2017}
2018
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002019bool reg_supported_dfs_region(u8 dfs_region)
2020{
2021 switch (dfs_region) {
2022 case NL80211_DFS_UNSET:
2023 case NL80211_DFS_FCC:
2024 case NL80211_DFS_ETSI:
2025 case NL80211_DFS_JP:
2026 return true;
2027 default:
2028 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
2029 dfs_region);
2030 return false;
2031 }
2032}
2033
2034static void print_dfs_region(u8 dfs_region)
2035{
2036 if (!dfs_region)
2037 return;
2038
2039 switch (dfs_region) {
2040 case NL80211_DFS_FCC:
2041 pr_info(" DFS Master region FCC");
2042 break;
2043 case NL80211_DFS_ETSI:
2044 pr_info(" DFS Master region ETSI");
2045 break;
2046 case NL80211_DFS_JP:
2047 pr_info(" DFS Master region JP");
2048 break;
2049 default:
Johannes Berg1a919312012-12-03 17:21:11 +01002050 pr_info(" DFS Master region Unknown");
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002051 break;
2052 }
2053}
2054
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002055static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002056{
Johannes Bergc492db32012-12-06 16:29:25 +01002057 struct regulatory_request *lr = get_last_request();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002058
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002059 if (is_intersected_alpha2(rd->alpha2)) {
Johannes Bergc492db32012-12-06 16:29:25 +01002060 if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002061 struct cfg80211_registered_device *rdev;
Johannes Bergc492db32012-12-06 16:29:25 +01002062 rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002063 if (rdev) {
Joe Perchese9c02682010-11-16 19:56:49 -08002064 pr_info("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002065 rdev->country_ie_alpha2[0],
2066 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002067 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002068 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002069 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002070 pr_info("Current regulatory domain intersected:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002071 } else if (is_world_regdom(rd->alpha2)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002072 pr_info("World regulatory domain updated:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002073 } else {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002074 if (is_unknown_alpha2(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08002075 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002076 else {
Johannes Bergc492db32012-12-06 16:29:25 +01002077 if (reg_request_cell_base(lr))
Johannes Berg1a919312012-12-03 17:21:11 +01002078 pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002079 rd->alpha2[0], rd->alpha2[1]);
2080 else
Johannes Berg1a919312012-12-03 17:21:11 +01002081 pr_info("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002082 rd->alpha2[0], rd->alpha2[1]);
2083 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002084 }
Johannes Berg1a919312012-12-03 17:21:11 +01002085
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002086 print_dfs_region(rd->dfs_region);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002087 print_rd_rules(rd);
2088}
2089
Johannes Berg2df78162008-10-28 16:49:41 +01002090static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002091{
Joe Perchese9c02682010-11-16 19:56:49 -08002092 pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002093 print_rd_rules(rd);
2094}
2095
Johannes Bergd2372b32008-10-24 20:32:20 +02002096/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002097static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002098{
Johannes Berge9763c32012-12-03 16:59:46 +01002099 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002100 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002101 struct wiphy *request_wiphy;
Johannes Bergc492db32012-12-06 16:29:25 +01002102 struct regulatory_request *lr = get_last_request();
Johannes Berg6913b492012-12-04 00:48:59 +01002103
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002104 /* Some basic sanity checks first */
2105
Johannes Berg6913b492012-12-04 00:48:59 +01002106 if (!reg_is_valid_request(rd->alpha2))
2107 return -EINVAL;
2108
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002109 if (is_world_regdom(rd->alpha2)) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002110 update_world_regdomain(rd);
2111 return 0;
2112 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002113
2114 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
Johannes Berg1a919312012-12-03 17:21:11 +01002115 !is_unknown_alpha2(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002116 return -EINVAL;
2117
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002118 /*
2119 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002120 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002121 * and the pending request came in from a country IE
2122 */
Johannes Bergc492db32012-12-06 16:29:25 +01002123 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002124 /*
2125 * If someone else asked us to change the rd lets only bother
2126 * checking if the alpha2 changes if CRDA was already called
2127 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002128 if (!regdom_changes(rd->alpha2))
Kalle Valo95908532012-07-12 15:33:58 +03002129 return -EALREADY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002130 }
2131
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002132 /*
2133 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002134 * and finally inform them of what we have done, in case they want
2135 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002136 * internal EEPROM data
2137 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002138
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002139 if (!is_valid_rd(rd)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002140 pr_err("Invalid regulatory domain detected:\n");
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002141 print_regdomain_info(rd);
2142 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002143 }
2144
Johannes Bergc492db32012-12-06 16:29:25 +01002145 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez0bac71a2011-11-28 16:47:16 -05002146 if (!request_wiphy &&
Johannes Bergc492db32012-12-06 16:29:25 +01002147 (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
2148 lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)) {
Luis R. Rodriguez0bac71a2011-11-28 16:47:16 -05002149 schedule_delayed_work(&reg_timeout, 0);
Johannes Bergde3584b2011-11-21 10:44:00 +01002150 return -ENODEV;
2151 }
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002152
Johannes Bergc492db32012-12-06 16:29:25 +01002153 if (!lr->intersect) {
2154 if (lr->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Johannes Berg379b82f2012-12-06 15:44:07 +01002155 reset_regdomains(false, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002156 return 0;
2157 }
2158
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002159 /*
2160 * For a driver hint, lets copy the regulatory domain the
2161 * driver wanted to the wiphy to deal with conflicts
2162 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002163
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002164 /*
2165 * Userspace could have sent two replies with only
2166 * one kernel request.
2167 */
2168 if (request_wiphy->regd)
2169 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002170
Johannes Berge9763c32012-12-03 16:59:46 +01002171 regd = reg_copy_regd(rd);
2172 if (IS_ERR(regd))
2173 return PTR_ERR(regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002174
Johannes Berg458f4f92012-12-06 15:47:38 +01002175 rcu_assign_pointer(request_wiphy->regd, regd);
Johannes Berg379b82f2012-12-06 15:44:07 +01002176 reset_regdomains(false, rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002177 return 0;
2178 }
2179
2180 /* Intersection requires a bit more work */
2181
Johannes Bergc492db32012-12-06 16:29:25 +01002182 if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg458f4f92012-12-06 15:47:38 +01002183 intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002184 if (!intersected_rd)
2185 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002186
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002187 /*
2188 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002189 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002190 * domain we keep it for its private use
2191 */
Larry Fingerb7566fc2013-02-04 15:33:44 -06002192 if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER) {
2193 const struct ieee80211_regdomain *tmp;
2194
2195 tmp = get_wiphy_regdom(request_wiphy);
Johannes Berg458f4f92012-12-06 15:47:38 +01002196 rcu_assign_pointer(request_wiphy->regd, rd);
Larry Fingerb7566fc2013-02-04 15:33:44 -06002197 rcu_free_regdom(tmp);
2198 } else {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002199 kfree(rd);
Larry Fingerb7566fc2013-02-04 15:33:44 -06002200 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002201
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002202 rd = NULL;
2203
Johannes Berg379b82f2012-12-06 15:44:07 +01002204 reset_regdomains(false, intersected_rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002205
2206 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002207 }
2208
Alan Coxf3baed52012-09-17 11:54:15 +01002209 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002210}
2211
2212
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002213/*
2214 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002215 * multiple drivers can be ironed out later. Caller must've already
Johannes Berg458f4f92012-12-06 15:47:38 +01002216 * kmalloc'd the rd structure.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002217 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002218int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002219{
Johannes Bergc492db32012-12-06 16:29:25 +01002220 struct regulatory_request *lr;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002221 int r;
2222
Johannes Bergc492db32012-12-06 16:29:25 +01002223 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002224
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002225 /* Note that this doesn't update the wiphys, this is done below */
2226 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002227 if (r) {
Kalle Valo95908532012-07-12 15:33:58 +03002228 if (r == -EALREADY)
2229 reg_set_request_processed();
2230
Johannes Bergd2372b32008-10-24 20:32:20 +02002231 kfree(rd);
Johannes Berg38fd2142013-05-10 19:17:17 +02002232 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002233 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002234
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002235 /* This would make this whole thing pointless */
Johannes Berg38fd2142013-05-10 19:17:17 +02002236 if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
2237 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002238
2239 /* update all wiphys now with the new established regulatory domain */
Johannes Bergc492db32012-12-06 16:29:25 +01002240 update_all_wiphy_regulatory(lr->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002241
Johannes Berg458f4f92012-12-06 15:47:38 +01002242 print_regdomain(get_cfg80211_regdom());
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002243
Johannes Bergc492db32012-12-06 16:29:25 +01002244 nl80211_send_reg_change_event(lr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002245
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002246 reg_set_request_processed();
2247
Johannes Berg38fd2142013-05-10 19:17:17 +02002248 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002249}
2250
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002251int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
2252{
Johannes Berg4a484cf2013-01-09 10:22:07 +01002253 struct regulatory_request *lr;
2254 u8 alpha2[2];
2255 bool add = false;
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002256
Johannes Berg4a484cf2013-01-09 10:22:07 +01002257 rcu_read_lock();
2258 lr = get_last_request();
Johannes Bergc492db32012-12-06 16:29:25 +01002259 if (lr && !lr->processed) {
Johannes Berg4a484cf2013-01-09 10:22:07 +01002260 memcpy(alpha2, lr->alpha2, 2);
2261 add = true;
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002262 }
Johannes Berg4a484cf2013-01-09 10:22:07 +01002263 rcu_read_unlock();
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002264
Johannes Berg4a484cf2013-01-09 10:22:07 +01002265 if (add)
2266 return add_uevent_var(env, "COUNTRY=%c%c",
2267 alpha2[0], alpha2[1]);
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002268 return 0;
2269}
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002270
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002271void wiphy_regulatory_register(struct wiphy *wiphy)
2272{
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002273 struct regulatory_request *lr;
2274
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002275 if (!reg_dev_ignore_cell_hint(wiphy))
2276 reg_num_devs_support_basehint++;
2277
Arik Nemtsov23df0b72013-07-21 16:36:48 +03002278 lr = get_last_request();
2279 wiphy_update_regulatory(wiphy, lr->initiator);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002280}
2281
Luis R. Rodriguezbfead082012-07-12 11:49:19 -07002282void wiphy_regulatory_deregister(struct wiphy *wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002283{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002284 struct wiphy *request_wiphy = NULL;
Johannes Bergc492db32012-12-06 16:29:25 +01002285 struct regulatory_request *lr;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002286
Johannes Bergc492db32012-12-06 16:29:25 +01002287 lr = get_last_request();
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002288
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002289 if (!reg_dev_ignore_cell_hint(wiphy))
2290 reg_num_devs_support_basehint--;
2291
Johannes Berg458f4f92012-12-06 15:47:38 +01002292 rcu_free_regdom(get_wiphy_regdom(wiphy));
2293 rcu_assign_pointer(wiphy->regd, NULL);
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002294
Johannes Bergc492db32012-12-06 16:29:25 +01002295 if (lr)
2296 request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002297
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002298 if (!request_wiphy || request_wiphy != wiphy)
Johannes Berg38fd2142013-05-10 19:17:17 +02002299 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002300
Johannes Bergc492db32012-12-06 16:29:25 +01002301 lr->wiphy_idx = WIPHY_IDX_INVALID;
2302 lr->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002303}
2304
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002305static void reg_timeout_work(struct work_struct *work)
2306{
Johannes Berg1a919312012-12-03 17:21:11 +01002307 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
Johannes Bergf77b86d2013-06-24 15:43:38 +02002308 rtnl_lock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002309 restore_regulatory_settings(true);
Johannes Bergf77b86d2013-06-24 15:43:38 +02002310 rtnl_unlock();
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002311}
2312
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002313int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002314{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002315 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002316
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002317 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2318 if (IS_ERR(reg_pdev))
2319 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002320
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002321 reg_pdev->dev.type = &reg_device_type;
2322
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002323 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002324 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002325
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -07002326 reg_regdb_size_check();
2327
Johannes Berg458f4f92012-12-06 15:47:38 +01002328 rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002329
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002330 user_alpha2[0] = '9';
2331 user_alpha2[1] = '7';
2332
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002333 /* We always try to get an update for the static regdomain */
Johannes Berg458f4f92012-12-06 15:47:38 +01002334 err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002335 if (err) {
2336 if (err == -ENOMEM)
2337 return err;
2338 /*
2339 * N.B. kobject_uevent_env() can fail mainly for when we're out
2340 * memory which is handled and propagated appropriately above
2341 * but it can also fail during a netlink_broadcast() or during
2342 * early boot for call_usermodehelper(). For now treat these
2343 * errors as non-fatal.
2344 */
Joe Perchese9c02682010-11-16 19:56:49 -08002345 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002346 }
Johannes Berg734366d2008-09-15 10:56:48 +02002347
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002348 /*
2349 * Finally, if the user set the module parameter treat it
2350 * as a user hint.
2351 */
2352 if (!is_world_regdom(ieee80211_regdom))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002353 regulatory_hint_user(ieee80211_regdom,
2354 NL80211_USER_REG_HINT_USER);
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002355
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002356 return 0;
2357}
2358
Johannes Berg1a919312012-12-03 17:21:11 +01002359void regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002360{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002361 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002362 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002363
2364 cancel_work_sync(&reg_work);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002365 cancel_delayed_work_sync(&reg_timeout);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002366
Johannes Berg9027b142012-12-03 17:59:24 +01002367 /* Lock to suppress warnings */
Johannes Berg38fd2142013-05-10 19:17:17 +02002368 rtnl_lock();
Johannes Berg379b82f2012-12-06 15:44:07 +01002369 reset_regdomains(true, NULL);
Johannes Berg38fd2142013-05-10 19:17:17 +02002370 rtnl_unlock();
Johannes Berg734366d2008-09-15 10:56:48 +02002371
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08002372 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02002373
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002374 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002375
Johannes Bergfea9bce2012-12-03 17:32:01 +01002376 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2377 list_del(&reg_beacon->list);
2378 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002379 }
2380
Johannes Bergfea9bce2012-12-03 17:32:01 +01002381 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2382 list_del(&reg_beacon->list);
2383 kfree(reg_beacon);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002384 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002385
Johannes Bergfea9bce2012-12-03 17:32:01 +01002386 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2387 list_del(&reg_request->list);
2388 kfree(reg_request);
Johannes Berg8318d782008-01-24 19:38:38 +01002389 }
Johannes Berg8318d782008-01-24 19:38:38 +01002390}