blob: 35541d6d41450f26c4dfa5ff1d323f771e1f564b [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
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070084/* Receipt of information from last regulatory request */
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050085static struct regulatory_request *last_request = &core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +020086
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070087/* To trigger userspace events */
88static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010089
Scott James Remnant4d9d88d2011-03-08 10:45:30 -080090static struct device_type reg_device_type = {
91 .uevent = reg_device_uevent,
92};
93
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050094/*
95 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020096 * the current one and a world regulatory domain in case we have no
Johannes Berge8da2bb2012-12-03 23:00:08 +010097 * information to give us an alpha2.
98 * Protected by the cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050099 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -0800100const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +0200101
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500102/*
Luis R. Rodriguezabc73812009-07-30 17:38:08 -0700103 * Protects static reg.c components:
104 * - cfg80211_world_regdom
Luis R. Rodriguezabc73812009-07-30 17:38:08 -0700105 * - last_request
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700106 * - reg_num_devs_support_basehint
Luis R. Rodriguezabc73812009-07-30 17:38:08 -0700107 */
John W. Linville670b7f112010-06-24 11:26:31 -0400108static DEFINE_MUTEX(reg_mutex);
Johannes Berg46a5eba2010-09-15 13:28:15 +0200109
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700110/*
111 * Number of devices that registered to the core
112 * that support cellular base station regulatory hints
113 */
114static int reg_num_devs_support_basehint;
115
Johannes Berg46a5eba2010-09-15 13:28:15 +0200116static inline void assert_reg_lock(void)
117{
118 lockdep_assert_held(&reg_mutex);
119}
Luis R. Rodriguezabc73812009-07-30 17:38:08 -0700120
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500121/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500122static LIST_HEAD(reg_requests_list);
123static spinlock_t reg_requests_lock;
124
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500125/* Used to queue up beacon hints for review */
126static LIST_HEAD(reg_pending_beacons);
127static spinlock_t reg_pending_beacons_lock;
128
129/* Used to keep track of processed beacon hints */
130static LIST_HEAD(reg_beacon_list);
131
132struct reg_beacon {
133 struct list_head list;
134 struct ieee80211_channel chan;
135};
136
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800137static void reg_todo(struct work_struct *work);
138static DECLARE_WORK(reg_work, reg_todo);
139
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -0700140static void reg_timeout_work(struct work_struct *work);
141static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
142
Johannes Berg734366d2008-09-15 10:56:48 +0200143/* We keep a static world regulatory domain in case of the absence of CRDA */
144static const struct ieee80211_regdomain world_regdom = {
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300145 .n_reg_rules = 6,
Johannes Berg734366d2008-09-15 10:56:48 +0200146 .alpha2 = "00",
147 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500148 /* IEEE 802.11b/g, channels 1..11 */
149 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Johannes Berg43c771a2012-11-12 10:51:34 +0100150 /* IEEE 802.11b/g, channels 12..13. */
151 REG_RULE(2467-10, 2472+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500152 NL80211_RRF_PASSIVE_SCAN |
153 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800154 /* IEEE 802.11 channel 14 - Only JP enables
155 * this and for 802.11b only */
156 REG_RULE(2484-10, 2484+10, 20, 6, 20,
157 NL80211_RRF_PASSIVE_SCAN |
158 NL80211_RRF_NO_IBSS |
159 NL80211_RRF_NO_OFDM),
160 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800161 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800162 NL80211_RRF_PASSIVE_SCAN |
163 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500164
165 /* NB: 5260 MHz - 5700 MHz requies DFS */
166
167 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800168 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500169 NL80211_RRF_PASSIVE_SCAN |
170 NL80211_RRF_NO_IBSS),
Vladimir Kondratiev90cdc6d2012-07-02 09:32:34 +0300171
172 /* IEEE 802.11ad (60gHz), channels 1..3 */
173 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200174 }
175};
176
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200177static const struct ieee80211_regdomain *cfg80211_world_regdom =
178 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200179
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400180static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500181static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400182
Johannes Berg734366d2008-09-15 10:56:48 +0200183module_param(ieee80211_regdom, charp, 0444);
184MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
185
Johannes Berg379b82f2012-12-06 15:44:07 +0100186static void reset_regdomains(bool full_reset,
187 const struct ieee80211_regdomain *new_regdom)
Johannes Berg734366d2008-09-15 10:56:48 +0200188{
Johannes Berge8da2bb2012-12-03 23:00:08 +0100189 assert_cfg80211_lock();
190 assert_reg_lock();
191
Johannes Berg942b25c2008-09-15 11:26:47 +0200192 /* avoid freeing static information or freeing something twice */
193 if (cfg80211_regdomain == cfg80211_world_regdom)
194 cfg80211_regdomain = NULL;
195 if (cfg80211_world_regdom == &world_regdom)
196 cfg80211_world_regdom = NULL;
197 if (cfg80211_regdomain == &world_regdom)
198 cfg80211_regdomain = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200199
200 kfree(cfg80211_regdomain);
201 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200202
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200203 cfg80211_world_regdom = &world_regdom;
Johannes Berg379b82f2012-12-06 15:44:07 +0100204 cfg80211_regdomain = new_regdom;
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500205
206 if (!full_reset)
207 return;
208
209 if (last_request != &core_request_world)
210 kfree(last_request);
211 last_request = &core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +0200212}
213
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500214/*
215 * Dynamic world regulatory domain requested by the wireless
216 * core upon initialization
217 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200218static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200219{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100220 WARN_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200221
Johannes Berge8da2bb2012-12-03 23:00:08 +0100222 assert_cfg80211_lock();
223 assert_reg_lock();
224
Johannes Berg379b82f2012-12-06 15:44:07 +0100225 reset_regdomains(false, rd);
Johannes Berg734366d2008-09-15 10:56:48 +0200226
227 cfg80211_world_regdom = rd;
Johannes Berg734366d2008-09-15 10:56:48 +0200228}
Johannes Berg734366d2008-09-15 10:56:48 +0200229
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200230bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100231{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700232 if (!alpha2)
233 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100234 return alpha2[0] == '0' && alpha2[1] == '0';
Johannes Berg8318d782008-01-24 19:38:38 +0100235}
236
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200237static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700238{
239 if (!alpha2)
240 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100241 return alpha2[0] && alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700242}
Johannes Berg8318d782008-01-24 19:38:38 +0100243
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200244static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700245{
246 if (!alpha2)
247 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500248 /*
249 * Special case where regulatory domain was built by driver
250 * but a specific alpha2 cannot be determined
251 */
Johannes Berg1a919312012-12-03 17:21:11 +0100252 return alpha2[0] == '9' && alpha2[1] == '9';
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700253}
254
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800255static bool is_intersected_alpha2(const char *alpha2)
256{
257 if (!alpha2)
258 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500259 /*
260 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800261 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500262 * structures
263 */
Johannes Berg1a919312012-12-03 17:21:11 +0100264 return alpha2[0] == '9' && alpha2[1] == '8';
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800265}
266
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200267static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268{
269 if (!alpha2)
270 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100271 return isalpha(alpha2[0]) && isalpha(alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700272}
273
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200274static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700275{
276 if (!alpha2_x || !alpha2_y)
277 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100278 return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700279}
280
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500281static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700282{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500283 assert_cfg80211_lock();
284
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700285 if (!cfg80211_regdomain)
286 return true;
Johannes Berg1a919312012-12-03 17:21:11 +0100287 return !alpha2_equal(cfg80211_regdomain->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700288}
289
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500290/*
291 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
292 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
293 * has ever been issued.
294 */
295static bool is_user_regdom_saved(void)
296{
297 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
298 return false;
299
300 /* This would indicate a mistake on the design */
Johannes Berg1a919312012-12-03 17:21:11 +0100301 if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500302 "Unexpected user alpha2: %c%c\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100303 user_alpha2[0], user_alpha2[1]))
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500304 return false;
305
306 return true;
307}
308
Johannes Berge9763c32012-12-03 16:59:46 +0100309static const struct ieee80211_regdomain *
310reg_copy_regd(const struct ieee80211_regdomain *src_regd)
John W. Linville3b377ea2009-12-18 17:59:01 -0500311{
312 struct ieee80211_regdomain *regd;
Johannes Berge9763c32012-12-03 16:59:46 +0100313 int size_of_regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500314 unsigned int i;
315
Johannes Berg82f20852012-12-04 12:49:16 +0100316 size_of_regd =
317 sizeof(struct ieee80211_regdomain) +
318 src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
John W. Linville3b377ea2009-12-18 17:59:01 -0500319
320 regd = kzalloc(size_of_regd, GFP_KERNEL);
321 if (!regd)
Johannes Berge9763c32012-12-03 16:59:46 +0100322 return ERR_PTR(-ENOMEM);
John W. Linville3b377ea2009-12-18 17:59:01 -0500323
324 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
325
326 for (i = 0; i < src_regd->n_reg_rules; i++)
327 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
Johannes Berge9763c32012-12-03 16:59:46 +0100328 sizeof(struct ieee80211_reg_rule));
John W. Linville3b377ea2009-12-18 17:59:01 -0500329
Johannes Berge9763c32012-12-03 16:59:46 +0100330 return regd;
John W. Linville3b377ea2009-12-18 17:59:01 -0500331}
332
333#ifdef CONFIG_CFG80211_INTERNAL_REGDB
334struct reg_regdb_search_request {
335 char alpha2[2];
336 struct list_head list;
337};
338
339static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400340static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500341
342static void reg_regdb_search(struct work_struct *work)
343{
344 struct reg_regdb_search_request *request;
Johannes Berge9763c32012-12-03 16:59:46 +0100345 const struct ieee80211_regdomain *curdom, *regdom = NULL;
346 int i;
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700347
348 mutex_lock(&cfg80211_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500349
John W. Linville368d06f2010-03-16 15:40:59 -0400350 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500351 while (!list_empty(&reg_regdb_search_list)) {
352 request = list_first_entry(&reg_regdb_search_list,
353 struct reg_regdb_search_request,
354 list);
355 list_del(&request->list);
356
Johannes Berg1a919312012-12-03 17:21:11 +0100357 for (i = 0; i < reg_regdb_size; i++) {
John W. Linville3b377ea2009-12-18 17:59:01 -0500358 curdom = reg_regdb[i];
359
Johannes Berg1a919312012-12-03 17:21:11 +0100360 if (alpha2_equal(request->alpha2, curdom->alpha2)) {
Johannes Berge9763c32012-12-03 16:59:46 +0100361 regdom = reg_copy_regd(curdom);
John W. Linville3b377ea2009-12-18 17:59:01 -0500362 break;
363 }
364 }
365
366 kfree(request);
367 }
John W. Linville368d06f2010-03-16 15:40:59 -0400368 mutex_unlock(&reg_regdb_search_mutex);
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700369
Johannes Berge9763c32012-12-03 16:59:46 +0100370 if (!IS_ERR_OR_NULL(regdom))
Luis R. Rodrigueza85d0d72012-09-14 15:36:57 -0700371 set_regdom(regdom);
372
373 mutex_unlock(&cfg80211_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500374}
375
376static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
377
378static void reg_regdb_query(const char *alpha2)
379{
380 struct reg_regdb_search_request *request;
381
382 if (!alpha2)
383 return;
384
385 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
386 if (!request)
387 return;
388
389 memcpy(request->alpha2, alpha2, 2);
390
John W. Linville368d06f2010-03-16 15:40:59 -0400391 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500392 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400393 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500394
395 schedule_work(&reg_regdb_work);
396}
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700397
398/* Feel free to add any other sanity checks here */
399static void reg_regdb_size_check(void)
400{
401 /* We should ideally BUILD_BUG_ON() but then random builds would fail */
402 WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
403}
John W. Linville3b377ea2009-12-18 17:59:01 -0500404#else
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -0700405static inline void reg_regdb_size_check(void) {}
John W. Linville3b377ea2009-12-18 17:59:01 -0500406static inline void reg_regdb_query(const char *alpha2) {}
407#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
408
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500409/*
410 * This lets us keep regulatory code which is updated on a regulatory
Scott James Remnant4d9d88d2011-03-08 10:45:30 -0800411 * basis in userspace. Country information is filled in by
412 * reg_device_uevent
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500413 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700414static int call_crda(const char *alpha2)
415{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700416 if (!is_world_regdom((char *) alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -0800417 pr_info("Calling CRDA for country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700418 alpha2[0], alpha2[1]);
419 else
Joe Perchese9c02682010-11-16 19:56:49 -0800420 pr_info("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700421
John W. Linville3b377ea2009-12-18 17:59:01 -0500422 /* query internal regulatory database (if it exists) */
423 reg_regdb_query(alpha2);
424
Scott James Remnant4d9d88d2011-03-08 10:45:30 -0800425 return kobject_uevent(&reg_pdev->dev.kobj, KOBJ_CHANGE);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700426}
427
Johannes Berg6913b492012-12-04 00:48:59 +0100428static bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700429{
Johannes Berg6913b492012-12-04 00:48:59 +0100430 assert_reg_lock();
431
Johannes Bergf6037d02008-10-21 11:01:33 +0200432 if (!last_request)
433 return false;
434
Johannes Berg6913b492012-12-04 00:48:59 +0100435 if (last_request->processed)
436 return false;
437
Johannes Bergf6037d02008-10-21 11:01:33 +0200438 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700439}
440
441/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200442static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700443{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200444 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700445 u32 freq_diff;
446
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800447 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700448 return false;
449
450 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
451 return false;
452
453 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
454
Roel Kluinbd05f282009-03-03 22:55:21 +0100455 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
Johannes Berg1a919312012-12-03 17:21:11 +0100456 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700457 return false;
458
459 return true;
460}
461
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200462static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700463{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200464 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700465 unsigned int i;
466
467 if (!rd->n_reg_rules)
468 return false;
469
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800470 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
471 return false;
472
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700473 for (i = 0; i < rd->n_reg_rules; i++) {
474 reg_rule = &rd->reg_rules[i];
475 if (!is_valid_reg_rule(reg_rule))
476 return false;
477 }
478
479 return true;
480}
481
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400482static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100483 u32 center_freq_khz, u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700484{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400485 u32 start_freq_khz, end_freq_khz;
486
487 start_freq_khz = center_freq_khz - (bw_khz/2);
488 end_freq_khz = center_freq_khz + (bw_khz/2);
489
490 if (start_freq_khz >= freq_range->start_freq_khz &&
491 end_freq_khz <= freq_range->end_freq_khz)
492 return true;
493
494 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700495}
496
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800497/**
498 * freq_in_rule_band - tells us if a frequency is in a frequency band
499 * @freq_range: frequency rule we want to query
500 * @freq_khz: frequency we are inquiring about
501 *
502 * This lets us know if a specific frequency rule is or is not relevant to
503 * a specific frequency's band. Bands are device specific and artificial
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200504 * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
505 * however it is safe for now to assume that a frequency rule should not be
506 * part of a frequency's band if the start freq or end freq are off by more
507 * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
508 * 60 GHz band.
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800509 * This resolution can be lowered and should be considered as we add
510 * regulatory rule support for other "bands".
511 **/
512static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
Johannes Berg1a919312012-12-03 17:21:11 +0100513 u32 freq_khz)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800514{
515#define ONE_GHZ_IN_KHZ 1000000
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200516 /*
517 * From 802.11ad: directional multi-gigabit (DMG):
518 * Pertaining to operation in a frequency band containing a channel
519 * with the Channel starting frequency above 45 GHz.
520 */
521 u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
522 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
523 if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800524 return true;
Vladimir Kondratiev64629b92012-09-23 09:49:54 +0200525 if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800526 return true;
527 return false;
528#undef ONE_GHZ_IN_KHZ
529}
530
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500531/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500532 * Helper for regdom_intersect(), this does the real
533 * mathematical intersection fun
534 */
Johannes Berg1a919312012-12-03 17:21:11 +0100535static int reg_rules_intersect(const struct ieee80211_reg_rule *rule1,
536 const struct ieee80211_reg_rule *rule2,
537 struct ieee80211_reg_rule *intersected_rule)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700538{
539 const struct ieee80211_freq_range *freq_range1, *freq_range2;
540 struct ieee80211_freq_range *freq_range;
541 const struct ieee80211_power_rule *power_rule1, *power_rule2;
542 struct ieee80211_power_rule *power_rule;
543 u32 freq_diff;
544
545 freq_range1 = &rule1->freq_range;
546 freq_range2 = &rule2->freq_range;
547 freq_range = &intersected_rule->freq_range;
548
549 power_rule1 = &rule1->power_rule;
550 power_rule2 = &rule2->power_rule;
551 power_rule = &intersected_rule->power_rule;
552
553 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100554 freq_range2->start_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700555 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100556 freq_range2->end_freq_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700557 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
Johannes Berg1a919312012-12-03 17:21:11 +0100558 freq_range2->max_bandwidth_khz);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700559
560 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
561 if (freq_range->max_bandwidth_khz > freq_diff)
562 freq_range->max_bandwidth_khz = freq_diff;
563
564 power_rule->max_eirp = min(power_rule1->max_eirp,
565 power_rule2->max_eirp);
566 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
567 power_rule2->max_antenna_gain);
568
Johannes Berg1a919312012-12-03 17:21:11 +0100569 intersected_rule->flags = rule1->flags | rule2->flags;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700570
571 if (!is_valid_reg_rule(intersected_rule))
572 return -EINVAL;
573
574 return 0;
575}
576
577/**
578 * regdom_intersect - do the intersection between two regulatory domains
579 * @rd1: first regulatory domain
580 * @rd2: second regulatory domain
581 *
582 * Use this function to get the intersection between two regulatory domains.
583 * Once completed we will mark the alpha2 for the rd as intersected, "98",
584 * as no one single alpha2 can represent this regulatory domain.
585 *
586 * Returns a pointer to the regulatory domain structure which will hold the
587 * resulting intersection of rules between rd1 and rd2. We will
588 * kzalloc() this structure for you.
589 */
Johannes Berg1a919312012-12-03 17:21:11 +0100590static struct ieee80211_regdomain *
591regdom_intersect(const struct ieee80211_regdomain *rd1,
592 const struct ieee80211_regdomain *rd2)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700593{
594 int r, size_of_regd;
595 unsigned int x, y;
596 unsigned int num_rules = 0, rule_idx = 0;
597 const struct ieee80211_reg_rule *rule1, *rule2;
598 struct ieee80211_reg_rule *intersected_rule;
599 struct ieee80211_regdomain *rd;
600 /* This is just a dummy holder to help us count */
Johannes Berg74f53cd2012-12-06 17:26:17 +0100601 struct ieee80211_reg_rule dummy_rule;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700602
603 if (!rd1 || !rd2)
604 return NULL;
605
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500606 /*
607 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700608 * build them. This is to so we can malloc() and free() a
609 * regdomain once. The reason we use reg_rules_intersect() here
610 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500611 * All rules that do check out OK are valid.
612 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700613
614 for (x = 0; x < rd1->n_reg_rules; x++) {
615 rule1 = &rd1->reg_rules[x];
616 for (y = 0; y < rd2->n_reg_rules; y++) {
617 rule2 = &rd2->reg_rules[y];
Johannes Berg74f53cd2012-12-06 17:26:17 +0100618 if (!reg_rules_intersect(rule1, rule2, &dummy_rule))
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700619 num_rules++;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700620 }
621 }
622
623 if (!num_rules)
624 return NULL;
625
626 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg82f20852012-12-04 12:49:16 +0100627 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700628
629 rd = kzalloc(size_of_regd, GFP_KERNEL);
630 if (!rd)
631 return NULL;
632
Johannes Berg8a57fff2012-12-06 17:03:17 +0100633 for (x = 0; x < rd1->n_reg_rules && rule_idx < num_rules; x++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700634 rule1 = &rd1->reg_rules[x];
Johannes Berg8a57fff2012-12-06 17:03:17 +0100635 for (y = 0; y < rd2->n_reg_rules && rule_idx < num_rules; y++) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700636 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500637 /*
638 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700639 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500640 * a memcpy()
641 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700642 intersected_rule = &rd->reg_rules[rule_idx];
Johannes Berg1a919312012-12-03 17:21:11 +0100643 r = reg_rules_intersect(rule1, rule2, intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500644 /*
645 * No need to memset here the intersected rule here as
646 * we're not using the stack anymore
647 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700648 if (r)
649 continue;
650 rule_idx++;
651 }
652 }
653
654 if (rule_idx != num_rules) {
655 kfree(rd);
656 return NULL;
657 }
658
659 rd->n_reg_rules = num_rules;
660 rd->alpha2[0] = '9';
661 rd->alpha2[1] = '8';
662
663 return rd;
664}
665
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500666/*
667 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
668 * want to just have the channel structure use these
669 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700670static u32 map_regdom_flags(u32 rd_flags)
671{
672 u32 channel_flags = 0;
673 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
674 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
675 if (rd_flags & NL80211_RRF_NO_IBSS)
676 channel_flags |= IEEE80211_CHAN_NO_IBSS;
677 if (rd_flags & NL80211_RRF_DFS)
678 channel_flags |= IEEE80211_CHAN_RADAR;
Seth Forshee03f6b082012-08-01 15:58:42 -0500679 if (rd_flags & NL80211_RRF_NO_OFDM)
680 channel_flags |= IEEE80211_CHAN_NO_OFDM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700681 return channel_flags;
682}
683
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100684static int freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800685 const struct ieee80211_reg_rule **reg_rule,
Johannes Berg5d885b92012-12-04 00:14:17 +0100686 const struct ieee80211_regdomain *regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100687{
688 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800689 bool band_rule_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400690 bool bw_fits = false;
691
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800692 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700693 return -EINVAL;
694
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800695 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700696 const struct ieee80211_reg_rule *rr;
697 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700698
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800699 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700700 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800701
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500702 /*
703 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800704 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500705 * not overwrite it once found
706 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800707 if (!band_rule_found)
708 band_rule_found = freq_in_rule_band(fr, center_freq);
709
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100710 bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800711
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400712 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700713 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400714 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100715 }
716 }
717
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800718 if (!band_rule_found)
719 return -ERANGE;
720
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400721 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700722}
723
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100724int freq_reg_info(struct wiphy *wiphy, u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400725 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800726{
Johannes Berg5d885b92012-12-04 00:14:17 +0100727 const struct ieee80211_regdomain *regd;
728
729 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400730 assert_cfg80211_lock();
Johannes Berg1a919312012-12-03 17:21:11 +0100731
Johannes Berg5d885b92012-12-04 00:14:17 +0100732 /*
733 * Follow the driver's regulatory domain, if present, unless a country
734 * IE has been processed or a user wants to help complaince further
735 */
736 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
737 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
738 wiphy->regd)
739 regd = wiphy->regd;
740 else
741 regd = cfg80211_regdomain;
742
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100743 return freq_reg_info_regd(wiphy, center_freq, reg_rule, regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800744}
John W. Linville4f366c52010-07-15 14:57:33 -0400745EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700746
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530747#ifdef CONFIG_CFG80211_REG_DEBUG
748static const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
749{
750 switch (initiator) {
751 case NL80211_REGDOM_SET_BY_CORE:
752 return "Set by core";
753 case NL80211_REGDOM_SET_BY_USER:
754 return "Set by user";
755 case NL80211_REGDOM_SET_BY_DRIVER:
756 return "Set by driver";
757 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
758 return "Set by country IE";
759 default:
760 WARN_ON(1);
761 return "Set by bug";
762 }
763}
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530764
765static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530766 const struct ieee80211_reg_rule *reg_rule)
767{
768 const struct ieee80211_power_rule *power_rule;
769 const struct ieee80211_freq_range *freq_range;
770 char max_antenna_gain[32];
771
772 power_rule = &reg_rule->power_rule;
773 freq_range = &reg_rule->freq_range;
774
775 if (!power_rule->max_antenna_gain)
776 snprintf(max_antenna_gain, 32, "N/A");
777 else
778 snprintf(max_antenna_gain, 32, "%d", power_rule->max_antenna_gain);
779
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100780 REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n",
781 chan->center_freq);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530782
Pavel Roskin56e67862011-09-01 11:54:12 -0400783 REG_DBG_PRINT("%d KHz - %d KHz @ %d KHz), (%s mBi, %d mBm)\n",
Johannes Berg1a919312012-12-03 17:21:11 +0100784 freq_range->start_freq_khz, freq_range->end_freq_khz,
785 freq_range->max_bandwidth_khz, max_antenna_gain,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530786 power_rule->max_eirp);
787}
788#else
789static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530790 const struct ieee80211_reg_rule *reg_rule)
791{
792 return;
793}
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530794#endif
795
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400796/*
797 * Note that right now we assume the desired channel bandwidth
798 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100799 * per channel, the primary and the extension channel).
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400800 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700801static void handle_channel(struct wiphy *wiphy,
802 enum nl80211_reg_initiator initiator,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100803 struct ieee80211_channel *chan)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700804{
805 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400806 u32 flags, bw_flags = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700807 const struct ieee80211_reg_rule *reg_rule = NULL;
808 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400809 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500810 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800811
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500812 assert_cfg80211_lock();
813
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500814 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
815
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800816 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700817
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100818 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq), &reg_rule);
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700819 if (r) {
820 /*
821 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300822 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700823 * from a Country IE and the Country IE had no information
824 * about a band. The IEEE 802.11 spec allows for an AP
825 * to send only a subset of the regulatory rules allowed,
826 * so an AP in the US that only supports 2.4 GHz may only send
827 * a country IE with information for the 2.4 GHz band
828 * while 5 GHz is still supported.
829 */
830 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
831 r == -ERANGE)
832 return;
833
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700834 REG_DBG_PRINT("Disabling freq %d MHz\n", chan->center_freq);
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700835 chan->flags = IEEE80211_CHAN_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +0100836 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700837 }
Johannes Berg8318d782008-01-24 19:38:38 +0100838
Johannes Bergfe7ef5e2012-12-04 15:07:34 +0100839 chan_reg_rule_print_dbg(chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530840
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700841 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400842 freq_range = &reg_rule->freq_range;
843
844 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
845 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700846
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400847 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500848 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +0100849 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500850 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300851 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800852 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500853 * settings
854 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800855 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400856 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800857 chan->max_antenna_gain = chan->orig_mag =
858 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +0200859 chan->max_reg_power = chan->max_power = chan->orig_mpwr =
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800860 (int) MBM_TO_DBM(power_rule->max_eirp);
861 return;
862 }
863
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +0530864 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400865 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg1a919312012-12-03 17:21:11 +0100866 chan->max_antenna_gain =
867 min_t(int, chan->orig_mag,
868 MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +0200869 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Stanislaw Gruszka5e31fc02012-07-24 08:35:39 +0200870 if (chan->orig_mpwr) {
871 /*
872 * Devices that have their own custom regulatory domain
873 * but also use WIPHY_FLAG_STRICT_REGULATORY will follow the
874 * passed country IE power settings.
875 */
876 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
877 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
878 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
879 chan->max_power = chan->max_reg_power;
880 else
881 chan->max_power = min(chan->orig_mpwr,
882 chan->max_reg_power);
883 } else
884 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +0100885}
886
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100887static void handle_band(struct wiphy *wiphy,
888 enum nl80211_reg_initiator initiator,
889 struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100890{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800891 unsigned int i;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800892
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100893 if (!sband)
894 return;
Johannes Berg8318d782008-01-24 19:38:38 +0100895
896 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +0100897 handle_channel(wiphy, initiator, &sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +0100898}
899
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700900static bool reg_request_cell_base(struct regulatory_request *request)
901{
902 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
903 return false;
Johannes Berg1a919312012-12-03 17:21:11 +0100904 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700905}
906
907bool reg_last_request_cell_base(void)
908{
Mohammed Shafi Shajakhanebd0fd22012-07-18 18:11:29 +0530909 bool val;
Johannes Berg1a919312012-12-03 17:21:11 +0100910
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700911 mutex_lock(&reg_mutex);
Mohammed Shafi Shajakhanebd0fd22012-07-18 18:11:29 +0530912 val = reg_request_cell_base(last_request);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700913 mutex_unlock(&reg_mutex);
Johannes Berg1a919312012-12-03 17:21:11 +0100914
Mohammed Shafi Shajakhanebd0fd22012-07-18 18:11:29 +0530915 return val;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700916}
917
918#ifdef CONFIG_CFG80211_CERTIFICATION_ONUS
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700919/* Core specific check */
Johannes Berg2f922122012-12-03 17:54:55 +0100920static enum reg_request_treatment
921reg_ignore_cell_hint(struct regulatory_request *pending_request)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700922{
923 if (!reg_num_devs_support_basehint)
Johannes Berg2f922122012-12-03 17:54:55 +0100924 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700925
Johannes Berg1a919312012-12-03 17:21:11 +0100926 if (reg_request_cell_base(last_request) &&
927 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +0100928 return REG_REQ_ALREADY_SET;
Johannes Berg1a919312012-12-03 17:21:11 +0100929
Johannes Berg2f922122012-12-03 17:54:55 +0100930 return REG_REQ_OK;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700931}
932
933/* Device specific check */
934static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
935{
Johannes Berg1a919312012-12-03 17:21:11 +0100936 return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700937}
938#else
939static int reg_ignore_cell_hint(struct regulatory_request *pending_request)
940{
Johannes Berg2f922122012-12-03 17:54:55 +0100941 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700942}
Johannes Berg1a919312012-12-03 17:21:11 +0100943
944static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700945{
946 return true;
947}
948#endif
949
950
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400951static bool ignore_reg_update(struct wiphy *wiphy,
952 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800953{
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530954 if (!last_request) {
Johannes Berg1a919312012-12-03 17:21:11 +0100955 REG_DBG_PRINT("Ignoring regulatory request %s since last_request is not set\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530956 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800957 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530958 }
959
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400960 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530961 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
Johannes Berg1a919312012-12-03 17:21:11 +0100962 REG_DBG_PRINT("Ignoring regulatory request %s since the driver uses its own custom regulatory domain\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530963 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800964 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530965 }
966
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500967 /*
968 * wiphy->regd will be set once the device has its own
969 * desired regulatory domain set
970 */
Johannes Berg5be83de2009-11-19 00:56:28 +0100971 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -0700972 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530973 !is_world_regdom(last_request->alpha2)) {
Johannes Berg1a919312012-12-03 17:21:11 +0100974 REG_DBG_PRINT("Ignoring regulatory request %s since the driver requires its own regulatory domain to be set first\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530975 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800976 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530977 }
978
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700979 if (reg_request_cell_base(last_request))
980 return reg_dev_ignore_cell_hint(wiphy);
981
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800982 return false;
983}
984
Johannes Berg1a919312012-12-03 17:21:11 +0100985static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500986 struct reg_beacon *reg_beacon)
987{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500988 struct ieee80211_supported_band *sband;
989 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400990 bool channel_changed = false;
991 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500992
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500993 sband = wiphy->bands[reg_beacon->chan.band];
994 chan = &sband->channels[chan_idx];
995
996 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
997 return;
998
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400999 if (chan->beacon_found)
1000 return;
1001
1002 chan->beacon_found = true;
1003
Johannes Berg5be83de2009-11-19 00:56:28 +01001004 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001005 return;
1006
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001007 chan_before.center_freq = chan->center_freq;
1008 chan_before.flags = chan->flags;
1009
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001010 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001011 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001012 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001013 }
1014
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001015 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001016 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001017 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001018 }
1019
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001020 if (channel_changed)
1021 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001022}
1023
1024/*
1025 * Called when a scan on a wiphy finds a beacon on
1026 * new channel
1027 */
1028static void wiphy_update_new_beacon(struct wiphy *wiphy,
1029 struct reg_beacon *reg_beacon)
1030{
1031 unsigned int i;
1032 struct ieee80211_supported_band *sband;
1033
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001034 if (!wiphy->bands[reg_beacon->chan.band])
1035 return;
1036
1037 sband = wiphy->bands[reg_beacon->chan.band];
1038
1039 for (i = 0; i < sband->n_channels; i++)
1040 handle_reg_beacon(wiphy, i, reg_beacon);
1041}
1042
1043/*
1044 * Called upon reg changes or a new wiphy is added
1045 */
1046static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1047{
1048 unsigned int i;
1049 struct ieee80211_supported_band *sband;
1050 struct reg_beacon *reg_beacon;
1051
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001052 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1053 if (!wiphy->bands[reg_beacon->chan.band])
1054 continue;
1055 sband = wiphy->bands[reg_beacon->chan.band];
1056 for (i = 0; i < sband->n_channels; i++)
1057 handle_reg_beacon(wiphy, i, reg_beacon);
1058 }
1059}
1060
1061static bool reg_is_world_roaming(struct wiphy *wiphy)
1062{
Johannes Berge8da2bb2012-12-03 23:00:08 +01001063 assert_cfg80211_lock();
1064
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001065 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1066 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1067 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001068 if (last_request &&
1069 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001070 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001071 return true;
1072 return false;
1073}
1074
1075/* Reap the advantages of previously found beacons */
1076static void reg_process_beacons(struct wiphy *wiphy)
1077{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001078 /*
1079 * Means we are just firing up cfg80211, so no beacons would
1080 * have been processed yet.
1081 */
1082 if (!last_request)
1083 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001084 if (!reg_is_world_roaming(wiphy))
1085 return;
1086 wiphy_update_beacon_reg(wiphy);
1087}
1088
Johannes Berg1a919312012-12-03 17:21:11 +01001089static bool is_ht40_allowed(struct ieee80211_channel *chan)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001090{
1091 if (!chan)
Johannes Berg1a919312012-12-03 17:21:11 +01001092 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001093 if (chan->flags & IEEE80211_CHAN_DISABLED)
Johannes Berg1a919312012-12-03 17:21:11 +01001094 return false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001095 /* This would happen when regulatory rules disallow HT40 completely */
Johannes Berg1a919312012-12-03 17:21:11 +01001096 return !(chan->flags & IEEE80211_CHAN_NO_HT40);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001097}
1098
1099static void reg_process_ht_flags_channel(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001100 struct ieee80211_channel *channel)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001101{
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001102 struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001103 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1104 unsigned int i;
1105
Johannes Berg1a919312012-12-03 17:21:11 +01001106 if (!is_ht40_allowed(channel)) {
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001107 channel->flags |= IEEE80211_CHAN_NO_HT40;
1108 return;
1109 }
1110
1111 /*
1112 * We need to ensure the extension channels exist to
1113 * be able to use HT40- or HT40+, this finds them (or not)
1114 */
1115 for (i = 0; i < sband->n_channels; i++) {
1116 struct ieee80211_channel *c = &sband->channels[i];
Johannes Berg1a919312012-12-03 17:21:11 +01001117
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001118 if (c->center_freq == (channel->center_freq - 20))
1119 channel_before = c;
1120 if (c->center_freq == (channel->center_freq + 20))
1121 channel_after = c;
1122 }
1123
1124 /*
1125 * Please note that this assumes target bandwidth is 20 MHz,
1126 * if that ever changes we also need to change the below logic
1127 * to include that as well.
1128 */
Johannes Berg1a919312012-12-03 17:21:11 +01001129 if (!is_ht40_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001130 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001131 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001132 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001133
Johannes Berg1a919312012-12-03 17:21:11 +01001134 if (!is_ht40_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001135 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001136 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001137 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001138}
1139
1140static void reg_process_ht_flags_band(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001141 struct ieee80211_supported_band *sband)
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001142{
1143 unsigned int i;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001144
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001145 if (!sband)
1146 return;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001147
1148 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001149 reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001150}
1151
1152static void reg_process_ht_flags(struct wiphy *wiphy)
1153{
1154 enum ieee80211_band band;
1155
1156 if (!wiphy)
1157 return;
1158
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001159 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1160 reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001161}
1162
Sven Neumanneac03e32011-08-30 23:38:53 +02001163static void wiphy_update_regulatory(struct wiphy *wiphy,
1164 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001165{
1166 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001167
Johannes Berge8da2bb2012-12-03 23:00:08 +01001168 assert_cfg80211_lock();
Sven Neumanneac03e32011-08-30 23:38:53 +02001169 assert_reg_lock();
1170
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001171 if (ignore_reg_update(wiphy, initiator))
Sven Neumanna203c2a2011-07-12 15:52:07 +02001172 return;
1173
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07001174 last_request->dfs_region = cfg80211_regdomain->dfs_region;
1175
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001176 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1177 handle_band(wiphy, initiator, wiphy->bands[band]);
Sven Neumanna203c2a2011-07-12 15:52:07 +02001178
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001179 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001180 reg_process_ht_flags(wiphy);
Johannes Berg1a919312012-12-03 17:21:11 +01001181
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001182 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001183 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001184}
1185
Sven Neumannd7549cb2011-08-30 23:38:54 +02001186static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1187{
1188 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301189 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02001190
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301191 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1192 wiphy = &rdev->wiphy;
1193 wiphy_update_regulatory(wiphy, initiator);
1194 /*
1195 * Regulatory updates set by CORE are ignored for custom
1196 * regulatory cards. Let us notify the changes to the driver,
1197 * as some drivers used this to restore its orig_* reg domain.
1198 */
1199 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1200 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
1201 wiphy->reg_notifier)
1202 wiphy->reg_notifier(wiphy, last_request);
1203 }
Sven Neumannd7549cb2011-08-30 23:38:54 +02001204}
1205
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001206static void handle_channel_custom(struct wiphy *wiphy,
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001207 struct ieee80211_channel *chan,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001208 const struct ieee80211_regdomain *regd)
1209{
1210 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001211 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001212 const struct ieee80211_reg_rule *reg_rule = NULL;
1213 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001214 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001215
Johannes Berg1a919312012-12-03 17:21:11 +01001216 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001217 &reg_rule, regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001218
1219 if (r) {
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001220 REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n",
1221 chan->center_freq);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001222 chan->flags = IEEE80211_CHAN_DISABLED;
1223 return;
1224 }
1225
Johannes Bergfe7ef5e2012-12-04 15:07:34 +01001226 chan_reg_rule_print_dbg(chan, reg_rule);
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301227
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001228 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001229 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001230
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001231 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1232 bw_flags = IEEE80211_CHAN_NO_HT40;
1233
1234 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001235 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Felix Fietkau279f0f52012-10-17 13:56:20 +02001236 chan->max_reg_power = chan->max_power =
1237 (int) MBM_TO_DBM(power_rule->max_eirp);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001238}
1239
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001240static void handle_band_custom(struct wiphy *wiphy,
1241 struct ieee80211_supported_band *sband,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001242 const struct ieee80211_regdomain *regd)
1243{
1244 unsigned int i;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001245
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001246 if (!sband)
1247 return;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001248
1249 for (i = 0; i < sband->n_channels; i++)
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001250 handle_channel_custom(wiphy, &sband->channels[i], regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001251}
1252
1253/* Used by drivers prior to wiphy registration */
1254void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1255 const struct ieee80211_regdomain *regd)
1256{
1257 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001258 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001259
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001260 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001261 if (!wiphy->bands[band])
1262 continue;
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001263 handle_band_custom(wiphy, wiphy->bands[band], regd);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001264 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001265 }
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001266
1267 /*
1268 * no point in calling this if it won't have any effect
Johannes Berg1a919312012-12-03 17:21:11 +01001269 * on your device's supported bands.
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001270 */
1271 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001272}
1273EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1274
Johannes Berg84fa4f42008-10-24 20:32:23 +02001275/* This has the logic which determines when a new request
1276 * should be ignored. */
Johannes Berg2f922122012-12-03 17:54:55 +01001277static enum reg_request_treatment
1278get_reg_request_treatment(struct wiphy *wiphy,
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001279 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001280{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001281 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001282
Johannes Berg84fa4f42008-10-24 20:32:23 +02001283 /* All initial requests are respected */
1284 if (!last_request)
Johannes Berg2f922122012-12-03 17:54:55 +01001285 return REG_REQ_OK;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001286
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001287 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001288 case NL80211_REGDOM_SET_BY_CORE:
Johannes Berg2f922122012-12-03 17:54:55 +01001289 return REG_REQ_OK;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001290 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001291 if (reg_request_cell_base(last_request)) {
1292 /* Trust a Cell base station over the AP's country IE */
1293 if (regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001294 return REG_REQ_IGNORE;
1295 return REG_REQ_ALREADY_SET;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001296 }
1297
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001298 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1299
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001300 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001301 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001302 if (last_request->initiator ==
1303 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001304 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001305 /*
1306 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001307 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001308 * intersect them, but that seems unlikely
1309 * to be correct. Reject second one for now.
1310 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001311 if (regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001312 return REG_REQ_IGNORE;
1313 return REG_REQ_ALREADY_SET;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001314 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001315 /*
1316 * Two consecutive Country IE hints on the same wiphy.
1317 * This should be picked up early by the driver/stack
1318 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001319 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg2f922122012-12-03 17:54:55 +01001320 return REG_REQ_OK;
1321 return REG_REQ_ALREADY_SET;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001322 }
Luis R. Rodrigueza171fba2010-10-18 17:44:51 -07001323 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001324 case NL80211_REGDOM_SET_BY_DRIVER:
1325 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001326 if (regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001327 return REG_REQ_OK;
1328 return REG_REQ_ALREADY_SET;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001329 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001330
1331 /*
1332 * This would happen if you unplug and plug your card
1333 * back in or if you add a new device for which the previously
1334 * loaded card also agrees on the regulatory domain.
1335 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001336 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001337 !regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001338 return REG_REQ_ALREADY_SET;
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001339
Johannes Berg2f922122012-12-03 17:54:55 +01001340 return REG_REQ_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001341 case NL80211_REGDOM_SET_BY_USER:
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001342 if (reg_request_cell_base(pending_request))
1343 return reg_ignore_cell_hint(pending_request);
1344
1345 if (reg_request_cell_base(last_request))
Johannes Berg2f922122012-12-03 17:54:55 +01001346 return REG_REQ_IGNORE;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001347
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001348 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Johannes Berg2f922122012-12-03 17:54:55 +01001349 return REG_REQ_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001350 /*
1351 * If the user knows better the user should set the regdom
1352 * to their country before the IE is picked up
1353 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001354 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Johannes Berg1a919312012-12-03 17:21:11 +01001355 last_request->intersect)
Johannes Berg2f922122012-12-03 17:54:55 +01001356 return REG_REQ_IGNORE;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001357 /*
1358 * Process user requests only after previous user/driver/core
1359 * requests have been processed
1360 */
Johannes Berg1a919312012-12-03 17:21:11 +01001361 if ((last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1362 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1363 last_request->initiator == NL80211_REGDOM_SET_BY_USER) &&
1364 regdom_changes(last_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001365 return REG_REQ_IGNORE;
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001366
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001367 if (!regdom_changes(pending_request->alpha2))
Johannes Berg2f922122012-12-03 17:54:55 +01001368 return REG_REQ_ALREADY_SET;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001369
Johannes Berg2f922122012-12-03 17:54:55 +01001370 return REG_REQ_OK;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001371 }
1372
Johannes Berg2f922122012-12-03 17:54:55 +01001373 return REG_REQ_IGNORE;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001374}
1375
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001376static void reg_set_request_processed(void)
1377{
1378 bool need_more_processing = false;
1379
1380 last_request->processed = true;
1381
1382 spin_lock(&reg_requests_lock);
1383 if (!list_empty(&reg_requests_list))
1384 need_more_processing = true;
1385 spin_unlock(&reg_requests_lock);
1386
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001387 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER)
Eliad Pellerfe20b392012-06-12 12:53:13 +03001388 cancel_delayed_work(&reg_timeout);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001389
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001390 if (need_more_processing)
1391 schedule_work(&reg_work);
1392}
1393
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001394/**
1395 * __regulatory_hint - hint to the wireless core a regulatory domain
1396 * @wiphy: if the hint comes from country information from an AP, this
1397 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001398 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001399 *
1400 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001401 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001402 *
Johannes Berg2f922122012-12-03 17:54:55 +01001403 * Returns one of the different reg request treatment values.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001404 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001405 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001406 */
Johannes Berg2f922122012-12-03 17:54:55 +01001407static enum reg_request_treatment
1408__regulatory_hint(struct wiphy *wiphy,
1409 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001410{
Johannes Berge9763c32012-12-03 16:59:46 +01001411 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001412 bool intersect = false;
Johannes Berg2f922122012-12-03 17:54:55 +01001413 enum reg_request_treatment treatment;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001414
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001415 assert_cfg80211_lock();
1416
Johannes Berg2f922122012-12-03 17:54:55 +01001417 treatment = get_reg_request_treatment(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001418
Johannes Berg2f922122012-12-03 17:54:55 +01001419 switch (treatment) {
1420 case REG_REQ_INTERSECT:
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001421 if (pending_request->initiator ==
1422 NL80211_REGDOM_SET_BY_DRIVER) {
Johannes Berge9763c32012-12-03 16:59:46 +01001423 regd = reg_copy_regd(cfg80211_regdomain);
1424 if (IS_ERR(regd)) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001425 kfree(pending_request);
Johannes Berge9763c32012-12-03 16:59:46 +01001426 return PTR_ERR(regd);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001427 }
Johannes Berge9763c32012-12-03 16:59:46 +01001428 wiphy->regd = regd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001429 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001430 intersect = true;
Johannes Berg2f922122012-12-03 17:54:55 +01001431 break;
1432 case REG_REQ_OK:
1433 break;
1434 default:
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001435 /*
1436 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001437 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001438 * wiphy
1439 */
Johannes Berg2f922122012-12-03 17:54:55 +01001440 if (treatment == REG_REQ_ALREADY_SET &&
1441 pending_request->initiator == NL80211_REGDOM_SET_BY_DRIVER) {
Johannes Berge9763c32012-12-03 16:59:46 +01001442 regd = reg_copy_regd(cfg80211_regdomain);
1443 if (IS_ERR(regd)) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001444 kfree(pending_request);
Johannes Berg2f922122012-12-03 17:54:55 +01001445 return REG_REQ_IGNORE;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001446 }
Johannes Berg2f922122012-12-03 17:54:55 +01001447 treatment = REG_REQ_ALREADY_SET;
Johannes Berge9763c32012-12-03 16:59:46 +01001448 wiphy->regd = regd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001449 goto new_request;
1450 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001451 kfree(pending_request);
Johannes Berg2f922122012-12-03 17:54:55 +01001452 return treatment;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001453 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001454
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001455new_request:
Luis R. Rodrigueza0429942011-11-28 16:47:15 -05001456 if (last_request != &core_request_world)
1457 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001458
1459 last_request = pending_request;
1460 last_request->intersect = intersect;
Johannes Berg6913b492012-12-04 00:48:59 +01001461 last_request->processed = false;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001462
1463 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001464
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001465 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1466 user_alpha2[0] = last_request->alpha2[0];
1467 user_alpha2[1] = last_request->alpha2[1];
1468 }
1469
Johannes Berg2f922122012-12-03 17:54:55 +01001470 /* When r == REG_REQ_INTERSECT we do need to call CRDA */
1471 if (treatment != REG_REQ_OK && treatment != REG_REQ_INTERSECT) {
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001472 /*
1473 * Since CRDA will not be called in this case as we already
1474 * have applied the requested regulatory domain before we just
1475 * inform userspace we have processed the request
1476 */
Johannes Berg2f922122012-12-03 17:54:55 +01001477 if (treatment == REG_REQ_ALREADY_SET) {
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001478 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001479 reg_set_request_processed();
1480 }
Johannes Berg2f922122012-12-03 17:54:55 +01001481 return treatment;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001482 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001483
Johannes Berg2f922122012-12-03 17:54:55 +01001484 if (call_crda(last_request->alpha2))
1485 return REG_REQ_IGNORE;
1486 return REG_REQ_OK;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001487}
1488
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001489/* This processes *all* regulatory hints */
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001490static void reg_process_hint(struct regulatory_request *reg_request,
1491 enum nl80211_reg_initiator reg_initiator)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001492{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001493 struct wiphy *wiphy = NULL;
1494
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001495 if (WARN_ON(!reg_request->alpha2))
1496 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001497
Johannes Bergf4173762012-12-03 18:23:37 +01001498 if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001499 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1500
Johannes Berg1a919312012-12-03 17:21:11 +01001501 if (reg_initiator == NL80211_REGDOM_SET_BY_DRIVER && !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001502 kfree(reg_request);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001503 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001504 }
1505
Johannes Berg2f922122012-12-03 17:54:55 +01001506 switch (__regulatory_hint(wiphy, reg_request)) {
1507 case REG_REQ_ALREADY_SET:
1508 /* This is required so that the orig_* parameters are saved */
1509 if (wiphy && wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
1510 wiphy_update_regulatory(wiphy, reg_initiator);
1511 break;
1512 default:
1513 if (reg_initiator == NL80211_REGDOM_SET_BY_USER)
1514 schedule_delayed_work(&reg_timeout,
1515 msecs_to_jiffies(3142));
1516 break;
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001517 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001518}
1519
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001520/*
1521 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1522 * Regulatory hints come on a first come first serve basis and we
1523 * must process each one atomically.
1524 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001525static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001526{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001527 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001528
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001529 mutex_lock(&cfg80211_mutex);
1530 mutex_lock(&reg_mutex);
1531
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001532 /* When last_request->processed becomes true this will be rescheduled */
1533 if (last_request && !last_request->processed) {
Johannes Berg1a919312012-12-03 17:21:11 +01001534 REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n");
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001535 goto out;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001536 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001537
1538 spin_lock(&reg_requests_lock);
1539
1540 if (list_empty(&reg_requests_list)) {
1541 spin_unlock(&reg_requests_lock);
1542 goto out;
1543 }
1544
1545 reg_request = list_first_entry(&reg_requests_list,
1546 struct regulatory_request,
1547 list);
1548 list_del_init(&reg_request->list);
1549
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001550 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001551
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001552 reg_process_hint(reg_request, reg_request->initiator);
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001553
1554out:
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001555 mutex_unlock(&reg_mutex);
1556 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001557}
1558
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001559/* Processes beacon hints -- this has nothing to do with country IEs */
1560static void reg_process_pending_beacon_hints(void)
1561{
Johannes Berg79c97e92009-07-07 03:56:12 +02001562 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001563 struct reg_beacon *pending_beacon, *tmp;
1564
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001565 /*
1566 * No need to hold the reg_mutex here as we just touch wiphys
1567 * and do not read or access regulatory variables.
1568 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001569 mutex_lock(&cfg80211_mutex);
1570
1571 /* This goes through the _pending_ beacon list */
1572 spin_lock_bh(&reg_pending_beacons_lock);
1573
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001574 list_for_each_entry_safe(pending_beacon, tmp,
1575 &reg_pending_beacons, list) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001576 list_del_init(&pending_beacon->list);
1577
1578 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001579 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1580 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001581
1582 /* Remembers the beacon hint for new wiphys or reg changes */
1583 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1584 }
1585
1586 spin_unlock_bh(&reg_pending_beacons_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001587 mutex_unlock(&cfg80211_mutex);
1588}
1589
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001590static void reg_todo(struct work_struct *work)
1591{
1592 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001593 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001594}
1595
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001596static void queue_regulatory_request(struct regulatory_request *request)
1597{
Johannes Bergd4f2c882012-12-03 18:59:58 +01001598 request->alpha2[0] = toupper(request->alpha2[0]);
1599 request->alpha2[1] = toupper(request->alpha2[1]);
John W. Linvillec61029c2010-08-05 14:26:24 -04001600
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001601 spin_lock(&reg_requests_lock);
1602 list_add_tail(&request->list, &reg_requests_list);
1603 spin_unlock(&reg_requests_lock);
1604
1605 schedule_work(&reg_work);
1606}
1607
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001608/*
1609 * Core regulatory hint -- happens during cfg80211_init()
1610 * and when we restore regulatory settings.
1611 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001612static int regulatory_hint_core(const char *alpha2)
1613{
1614 struct regulatory_request *request;
1615
Johannes Berg1a919312012-12-03 17:21:11 +01001616 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001617 if (!request)
1618 return -ENOMEM;
1619
1620 request->alpha2[0] = alpha2[0];
1621 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001622 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001623
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08001624 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001625
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001626 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001627}
1628
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001629/* User hints */
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001630int regulatory_hint_user(const char *alpha2,
1631 enum nl80211_user_reg_hint_type user_reg_hint_type)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001632{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001633 struct regulatory_request *request;
1634
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001635 if (WARN_ON(!alpha2))
1636 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001637
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001638 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1639 if (!request)
1640 return -ENOMEM;
1641
Johannes Bergf4173762012-12-03 18:23:37 +01001642 request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001643 request->alpha2[0] = alpha2[0];
1644 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001645 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001646 request->user_reg_hint_type = user_reg_hint_type;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001647
1648 queue_regulatory_request(request);
1649
1650 return 0;
1651}
1652
1653/* Driver hints */
1654int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1655{
1656 struct regulatory_request *request;
1657
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01001658 if (WARN_ON(!alpha2 || !wiphy))
1659 return -EINVAL;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001660
1661 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1662 if (!request)
1663 return -ENOMEM;
1664
1665 request->wiphy_idx = get_wiphy_idx(wiphy);
1666
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001667 request->alpha2[0] = alpha2[0];
1668 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001669 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001670
1671 queue_regulatory_request(request);
1672
1673 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001674}
1675EXPORT_SYMBOL(regulatory_hint);
1676
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001677/*
1678 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1679 * therefore cannot iterate over the rdev list here.
1680 */
Johannes Berg1a919312012-12-03 17:21:11 +01001681void regulatory_hint_11d(struct wiphy *wiphy, enum ieee80211_band band,
1682 const u8 *country_ie, u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001683{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001684 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001685 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001686 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001687
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001688 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001689
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001690 if (unlikely(!last_request))
1691 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001692
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001693 /* IE len must be evenly divisible by 2 */
1694 if (country_ie_len & 0x01)
1695 goto out;
1696
1697 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1698 goto out;
1699
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001700 alpha2[0] = country_ie[0];
1701 alpha2[1] = country_ie[1];
1702
1703 if (country_ie[2] == 'I')
1704 env = ENVIRON_INDOOR;
1705 else if (country_ie[2] == 'O')
1706 env = ENVIRON_OUTDOOR;
1707
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001708 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001709 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001710 * We leave conflict resolution to the workqueue, where can hold
1711 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001712 */
Johannes Bergf4173762012-12-03 18:23:37 +01001713 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1714 last_request->wiphy_idx != WIPHY_IDX_INVALID)
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001715 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001716
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001717 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1718 if (!request)
Dan Carpenterf9f9b6e2010-07-22 13:26:50 +02001719 goto out;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001720
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001721 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04001722 request->alpha2[0] = alpha2[0];
1723 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001724 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001725 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001726
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001727 queue_regulatory_request(request);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001728out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001729 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001730}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001731
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001732static void restore_alpha2(char *alpha2, bool reset_user)
1733{
1734 /* indicates there is no alpha2 to consider for restoration */
1735 alpha2[0] = '9';
1736 alpha2[1] = '7';
1737
1738 /* The user setting has precedence over the module parameter */
1739 if (is_user_regdom_saved()) {
1740 /* Unless we're asked to ignore it and reset it */
1741 if (reset_user) {
Johannes Berg1a919312012-12-03 17:21:11 +01001742 REG_DBG_PRINT("Restoring regulatory settings including user preference\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001743 user_alpha2[0] = '9';
1744 user_alpha2[1] = '7';
1745
1746 /*
1747 * If we're ignoring user settings, we still need to
1748 * check the module parameter to ensure we put things
1749 * back as they were for a full restore.
1750 */
1751 if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01001752 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
1753 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001754 alpha2[0] = ieee80211_regdom[0];
1755 alpha2[1] = ieee80211_regdom[1];
1756 }
1757 } else {
Johannes Berg1a919312012-12-03 17:21:11 +01001758 REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n",
1759 user_alpha2[0], user_alpha2[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001760 alpha2[0] = user_alpha2[0];
1761 alpha2[1] = user_alpha2[1];
1762 }
1763 } else if (!is_world_regdom(ieee80211_regdom)) {
Johannes Berg1a919312012-12-03 17:21:11 +01001764 REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n",
1765 ieee80211_regdom[0], ieee80211_regdom[1]);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001766 alpha2[0] = ieee80211_regdom[0];
1767 alpha2[1] = ieee80211_regdom[1];
1768 } else
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001769 REG_DBG_PRINT("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001770}
1771
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301772static void restore_custom_reg_settings(struct wiphy *wiphy)
1773{
1774 struct ieee80211_supported_band *sband;
1775 enum ieee80211_band band;
1776 struct ieee80211_channel *chan;
1777 int i;
1778
1779 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1780 sband = wiphy->bands[band];
1781 if (!sband)
1782 continue;
1783 for (i = 0; i < sband->n_channels; i++) {
1784 chan = &sband->channels[i];
1785 chan->flags = chan->orig_flags;
1786 chan->max_antenna_gain = chan->orig_mag;
1787 chan->max_power = chan->orig_mpwr;
Paul Stewart899852a2012-08-01 16:54:42 -07001788 chan->beacon_found = false;
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301789 }
1790 }
1791}
1792
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001793/*
1794 * Restoring regulatory settings involves ingoring any
1795 * possibly stale country IE information and user regulatory
1796 * settings if so desired, this includes any beacon hints
1797 * learned as we could have traveled outside to another country
1798 * after disconnection. To restore regulatory settings we do
1799 * exactly what we did at bootup:
1800 *
1801 * - send a core regulatory hint
1802 * - send a user regulatory hint if applicable
1803 *
1804 * Device drivers that send a regulatory hint for a specific country
1805 * keep their own regulatory domain on wiphy->regd so that does does
1806 * not need to be remembered.
1807 */
1808static void restore_regulatory_settings(bool reset_user)
1809{
1810 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08001811 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001812 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001813 struct regulatory_request *reg_request, *tmp;
1814 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301815 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001816
1817 mutex_lock(&cfg80211_mutex);
1818 mutex_lock(&reg_mutex);
1819
Johannes Berg379b82f2012-12-06 15:44:07 +01001820 reset_regdomains(true, cfg80211_world_regdom);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001821 restore_alpha2(alpha2, reset_user);
1822
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001823 /*
1824 * If there's any pending requests we simply
1825 * stash them to a temporary pending queue and
1826 * add then after we've restored regulatory
1827 * settings.
1828 */
1829 spin_lock(&reg_requests_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01001830 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
1831 if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
1832 continue;
1833 list_move_tail(&reg_request->list, &tmp_reg_req_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001834 }
1835 spin_unlock(&reg_requests_lock);
1836
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001837 /* Clear beacon hints */
1838 spin_lock_bh(&reg_pending_beacons_lock);
Johannes Bergfea9bce2012-12-03 17:32:01 +01001839 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
1840 list_del(&reg_beacon->list);
1841 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001842 }
1843 spin_unlock_bh(&reg_pending_beacons_lock);
1844
Johannes Bergfea9bce2012-12-03 17:32:01 +01001845 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
1846 list_del(&reg_beacon->list);
1847 kfree(reg_beacon);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001848 }
1849
1850 /* First restore to the basic regulatory settings */
Johannes Berg379b82f2012-12-06 15:44:07 +01001851 world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
1852 world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001853
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301854 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1855 if (rdev->wiphy.flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1856 restore_custom_reg_settings(&rdev->wiphy);
1857 }
1858
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08001859 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001860
1861 /*
1862 * This restores the ieee80211_regdom module parameter
1863 * preference or the last user requested regulatory
1864 * settings, user regulatory settings takes precedence.
1865 */
1866 if (is_an_alpha2(alpha2))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07001867 regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001868
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001869 spin_lock(&reg_requests_lock);
Johannes Berg11cff962012-12-03 18:56:41 +01001870 list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001871 spin_unlock(&reg_requests_lock);
1872
1873 mutex_unlock(&reg_mutex);
1874 mutex_unlock(&cfg80211_mutex);
1875
1876 REG_DBG_PRINT("Kicking the queue\n");
1877
1878 schedule_work(&reg_work);
1879}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001880
1881void regulatory_hint_disconnect(void)
1882{
Johannes Berg1a919312012-12-03 17:21:11 +01001883 REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001884 restore_regulatory_settings(false);
1885}
1886
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001887static bool freq_is_chan_12_13_14(u16 freq)
1888{
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001889 if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
1890 freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
1891 freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001892 return true;
1893 return false;
1894}
1895
1896int regulatory_hint_found_beacon(struct wiphy *wiphy,
1897 struct ieee80211_channel *beacon_chan,
1898 gfp_t gfp)
1899{
1900 struct reg_beacon *reg_beacon;
1901
Johannes Berg1a919312012-12-03 17:21:11 +01001902 if (beacon_chan->beacon_found ||
1903 beacon_chan->flags & IEEE80211_CHAN_RADAR ||
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001904 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
Johannes Berg1a919312012-12-03 17:21:11 +01001905 !freq_is_chan_12_13_14(beacon_chan->center_freq)))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001906 return 0;
1907
1908 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1909 if (!reg_beacon)
1910 return -ENOMEM;
1911
Johannes Berg1a919312012-12-03 17:21:11 +01001912 REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001913 beacon_chan->center_freq,
1914 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1915 wiphy_name(wiphy));
1916
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001917 memcpy(&reg_beacon->chan, beacon_chan,
Johannes Berg1a919312012-12-03 17:21:11 +01001918 sizeof(struct ieee80211_channel));
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001919
1920 /*
1921 * Since we can be called from BH or and non-BH context
1922 * we must use spin_lock_bh()
1923 */
1924 spin_lock_bh(&reg_pending_beacons_lock);
1925 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1926 spin_unlock_bh(&reg_pending_beacons_lock);
1927
1928 schedule_work(&reg_work);
1929
1930 return 0;
1931}
1932
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001933static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001934{
1935 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001936 const struct ieee80211_reg_rule *reg_rule = NULL;
1937 const struct ieee80211_freq_range *freq_range = NULL;
1938 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001939
Luis R. Rodriguez66533252011-12-20 12:23:37 -08001940 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001941
1942 for (i = 0; i < rd->n_reg_rules; i++) {
1943 reg_rule = &rd->reg_rules[i];
1944 freq_range = &reg_rule->freq_range;
1945 power_rule = &reg_rule->power_rule;
1946
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001947 /*
1948 * There may not be documentation for max antenna gain
1949 * in certain regions
1950 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001951 if (power_rule->max_antenna_gain)
Luis R. Rodriguez66533252011-12-20 12:23:37 -08001952 pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001953 freq_range->start_freq_khz,
1954 freq_range->end_freq_khz,
1955 freq_range->max_bandwidth_khz,
1956 power_rule->max_antenna_gain,
1957 power_rule->max_eirp);
1958 else
Luis R. Rodriguez66533252011-12-20 12:23:37 -08001959 pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001960 freq_range->start_freq_khz,
1961 freq_range->end_freq_khz,
1962 freq_range->max_bandwidth_khz,
1963 power_rule->max_eirp);
1964 }
1965}
1966
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07001967bool reg_supported_dfs_region(u8 dfs_region)
1968{
1969 switch (dfs_region) {
1970 case NL80211_DFS_UNSET:
1971 case NL80211_DFS_FCC:
1972 case NL80211_DFS_ETSI:
1973 case NL80211_DFS_JP:
1974 return true;
1975 default:
1976 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
1977 dfs_region);
1978 return false;
1979 }
1980}
1981
1982static void print_dfs_region(u8 dfs_region)
1983{
1984 if (!dfs_region)
1985 return;
1986
1987 switch (dfs_region) {
1988 case NL80211_DFS_FCC:
1989 pr_info(" DFS Master region FCC");
1990 break;
1991 case NL80211_DFS_ETSI:
1992 pr_info(" DFS Master region ETSI");
1993 break;
1994 case NL80211_DFS_JP:
1995 pr_info(" DFS Master region JP");
1996 break;
1997 default:
Johannes Berg1a919312012-12-03 17:21:11 +01001998 pr_info(" DFS Master region Unknown");
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07001999 break;
2000 }
2001}
2002
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002003static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002004{
2005
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002006 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002007 if (last_request->initiator ==
2008 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002009 struct cfg80211_registered_device *rdev;
2010 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002011 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002012 if (rdev) {
Joe Perchese9c02682010-11-16 19:56:49 -08002013 pr_info("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002014 rdev->country_ie_alpha2[0],
2015 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002016 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002017 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002018 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002019 pr_info("Current regulatory domain intersected:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002020 } else if (is_world_regdom(rd->alpha2)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002021 pr_info("World regulatory domain updated:\n");
Johannes Berg1a919312012-12-03 17:21:11 +01002022 } else {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002023 if (is_unknown_alpha2(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08002024 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002025 else {
2026 if (reg_request_cell_base(last_request))
Johannes Berg1a919312012-12-03 17:21:11 +01002027 pr_info("Regulatory domain changed to country: %c%c by Cell Station\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002028 rd->alpha2[0], rd->alpha2[1]);
2029 else
Johannes Berg1a919312012-12-03 17:21:11 +01002030 pr_info("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002031 rd->alpha2[0], rd->alpha2[1]);
2032 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002033 }
Johannes Berg1a919312012-12-03 17:21:11 +01002034
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002035 print_dfs_region(rd->dfs_region);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002036 print_rd_rules(rd);
2037}
2038
Johannes Berg2df78162008-10-28 16:49:41 +01002039static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002040{
Joe Perchese9c02682010-11-16 19:56:49 -08002041 pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002042 print_rd_rules(rd);
2043}
2044
Johannes Bergd2372b32008-10-24 20:32:20 +02002045/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002046static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002047{
Johannes Berge9763c32012-12-03 16:59:46 +01002048 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002049 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002050 struct wiphy *request_wiphy;
Johannes Berg6913b492012-12-04 00:48:59 +01002051
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002052 /* Some basic sanity checks first */
2053
Johannes Berg6913b492012-12-04 00:48:59 +01002054 if (!reg_is_valid_request(rd->alpha2))
2055 return -EINVAL;
2056
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002057 if (is_world_regdom(rd->alpha2)) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002058 update_world_regdomain(rd);
2059 return 0;
2060 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002061
2062 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
Johannes Berg1a919312012-12-03 17:21:11 +01002063 !is_unknown_alpha2(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002064 return -EINVAL;
2065
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002066 /*
2067 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002068 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002069 * and the pending request came in from a country IE
2070 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002071 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002072 /*
2073 * If someone else asked us to change the rd lets only bother
2074 * checking if the alpha2 changes if CRDA was already called
2075 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002076 if (!regdom_changes(rd->alpha2))
Kalle Valo95908532012-07-12 15:33:58 +03002077 return -EALREADY;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002078 }
2079
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002080 /*
2081 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002082 * and finally inform them of what we have done, in case they want
2083 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002084 * internal EEPROM data
2085 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002086
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002087 if (!is_valid_rd(rd)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002088 pr_err("Invalid regulatory domain detected:\n");
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002089 print_regdomain_info(rd);
2090 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002091 }
2092
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002093 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez0bac71a2011-11-28 16:47:16 -05002094 if (!request_wiphy &&
2095 (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
2096 last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)) {
2097 schedule_delayed_work(&reg_timeout, 0);
Johannes Bergde3584b2011-11-21 10:44:00 +01002098 return -ENODEV;
2099 }
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002100
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002101 if (!last_request->intersect) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002102 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Johannes Berg379b82f2012-12-06 15:44:07 +01002103 reset_regdomains(false, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002104 return 0;
2105 }
2106
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002107 /*
2108 * For a driver hint, lets copy the regulatory domain the
2109 * driver wanted to the wiphy to deal with conflicts
2110 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002111
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002112 /*
2113 * Userspace could have sent two replies with only
2114 * one kernel request.
2115 */
2116 if (request_wiphy->regd)
2117 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002118
Johannes Berge9763c32012-12-03 16:59:46 +01002119 regd = reg_copy_regd(rd);
2120 if (IS_ERR(regd))
2121 return PTR_ERR(regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002122
Johannes Berge9763c32012-12-03 16:59:46 +01002123 request_wiphy->regd = regd;
Johannes Berg379b82f2012-12-06 15:44:07 +01002124 reset_regdomains(false, rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002125 return 0;
2126 }
2127
2128 /* Intersection requires a bit more work */
2129
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002130 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002131 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2132 if (!intersected_rd)
2133 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002134
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002135 /*
2136 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002137 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002138 * domain we keep it for its private use
2139 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002140 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002141 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002142 else
2143 kfree(rd);
2144
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002145 rd = NULL;
2146
Johannes Berg379b82f2012-12-06 15:44:07 +01002147 reset_regdomains(false, intersected_rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002148
2149 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002150 }
2151
Alan Coxf3baed52012-09-17 11:54:15 +01002152 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002153}
2154
2155
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002156/*
2157 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002158 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002159 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2160 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002161int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002162{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002163 int r;
2164
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002165 assert_cfg80211_lock();
2166
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002167 mutex_lock(&reg_mutex);
2168
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002169 /* Note that this doesn't update the wiphys, this is done below */
2170 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002171 if (r) {
Kalle Valo95908532012-07-12 15:33:58 +03002172 if (r == -EALREADY)
2173 reg_set_request_processed();
2174
Johannes Bergd2372b32008-10-24 20:32:20 +02002175 kfree(rd);
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002176 goto out;
Johannes Bergd2372b32008-10-24 20:32:20 +02002177 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002178
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002179 /* This would make this whole thing pointless */
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002180 if (WARN_ON(!last_request->intersect && rd != cfg80211_regdomain)) {
2181 r = -EINVAL;
2182 goto out;
2183 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002184
2185 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002186 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002187
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002188 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002189
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002190 nl80211_send_reg_change_event(last_request);
2191
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002192 reg_set_request_processed();
2193
Johannes Bergfdc9d7b2012-12-03 18:36:09 +01002194 out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002195 mutex_unlock(&reg_mutex);
2196
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002197 return r;
2198}
2199
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002200#ifdef CONFIG_HOTPLUG
2201int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
2202{
2203 if (last_request && !last_request->processed) {
2204 if (add_uevent_var(env, "COUNTRY=%c%c",
2205 last_request->alpha2[0],
2206 last_request->alpha2[1]))
2207 return -ENOMEM;
2208 }
2209
2210 return 0;
2211}
2212#else
2213int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
2214{
2215 return -ENODEV;
2216}
2217#endif /* CONFIG_HOTPLUG */
2218
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002219void wiphy_regulatory_register(struct wiphy *wiphy)
2220{
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002221 mutex_lock(&reg_mutex);
2222
2223 if (!reg_dev_ignore_cell_hint(wiphy))
2224 reg_num_devs_support_basehint++;
2225
Luis R. Rodriguez14cdf112012-07-12 11:49:21 -07002226 wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
Luis R. Rodriguezf8a1c772012-07-12 11:49:20 -07002227
Luis R. Rodriguez14cdf112012-07-12 11:49:21 -07002228 mutex_unlock(&reg_mutex);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002229}
2230
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002231/* Caller must hold cfg80211_mutex */
Luis R. Rodriguezbfead082012-07-12 11:49:19 -07002232void wiphy_regulatory_deregister(struct wiphy *wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002233{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002234 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002235
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002236 mutex_lock(&reg_mutex);
2237
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002238 if (!reg_dev_ignore_cell_hint(wiphy))
2239 reg_num_devs_support_basehint--;
2240
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002241 kfree(wiphy->regd);
2242
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002243 if (last_request)
2244 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002245
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002246 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002247 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002248
Johannes Bergf4173762012-12-03 18:23:37 +01002249 last_request->wiphy_idx = WIPHY_IDX_INVALID;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002250 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002251out:
2252 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002253}
2254
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002255static void reg_timeout_work(struct work_struct *work)
2256{
Johannes Berg1a919312012-12-03 17:21:11 +01002257 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002258 restore_regulatory_settings(true);
2259}
2260
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002261int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002262{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002263 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002264
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002265 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2266 if (IS_ERR(reg_pdev))
2267 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002268
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002269 reg_pdev->dev.type = &reg_device_type;
2270
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002271 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002272 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002273
Luis R. Rodriguez80007ef2012-03-23 07:23:31 -07002274 reg_regdb_size_check();
2275
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002276 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002277
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002278 user_alpha2[0] = '9';
2279 user_alpha2[1] = '7';
2280
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002281 /* We always try to get an update for the static regdomain */
2282 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002283 if (err) {
2284 if (err == -ENOMEM)
2285 return err;
2286 /*
2287 * N.B. kobject_uevent_env() can fail mainly for when we're out
2288 * memory which is handled and propagated appropriately above
2289 * but it can also fail during a netlink_broadcast() or during
2290 * early boot for call_usermodehelper(). For now treat these
2291 * errors as non-fatal.
2292 */
Joe Perchese9c02682010-11-16 19:56:49 -08002293 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002294 }
Johannes Berg734366d2008-09-15 10:56:48 +02002295
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002296 /*
2297 * Finally, if the user set the module parameter treat it
2298 * as a user hint.
2299 */
2300 if (!is_world_regdom(ieee80211_regdom))
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07002301 regulatory_hint_user(ieee80211_regdom,
2302 NL80211_USER_REG_HINT_USER);
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002303
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002304 return 0;
2305}
2306
Johannes Berg1a919312012-12-03 17:21:11 +01002307void regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002308{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002309 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002310 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002311
2312 cancel_work_sync(&reg_work);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002313 cancel_delayed_work_sync(&reg_timeout);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002314
Johannes Berg9027b142012-12-03 17:59:24 +01002315 /* Lock to suppress warnings */
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002316 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002317 mutex_lock(&reg_mutex);
Johannes Berg379b82f2012-12-06 15:44:07 +01002318 reset_regdomains(true, NULL);
Johannes Berg9027b142012-12-03 17:59:24 +01002319 mutex_unlock(&cfg80211_mutex);
2320 mutex_unlock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002321
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08002322 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02002323
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002324 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002325
Johannes Bergfea9bce2012-12-03 17:32:01 +01002326 list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
2327 list_del(&reg_beacon->list);
2328 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002329 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002330
Johannes Bergfea9bce2012-12-03 17:32:01 +01002331 list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
2332 list_del(&reg_beacon->list);
2333 kfree(reg_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002334 }
2335
Johannes Bergfea9bce2012-12-03 17:32:01 +01002336 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
2337 list_del(&reg_request->list);
2338 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002339 }
Johannes Berg8318d782008-01-24 19:38:38 +01002340}