blob: b01449f01bbd82cb045d8759a69f2ac31dac5cde [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>
51#include <linux/random.h>
John W. Linvillec61029c2010-08-05 14:26:24 -040052#include <linux/ctype.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070053#include <linux/nl80211.h>
54#include <linux/platform_device.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040055#include <linux/moduleparam.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070056#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010057#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070058#include "reg.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050059#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040060#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010061
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050062#ifdef CONFIG_CFG80211_REG_DEBUG
Joe Perches12c5ffb2011-07-29 14:51:25 -070063#define REG_DBG_PRINT(format, args...) \
64 printk(KERN_DEBUG pr_fmt(format), ##args)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050065#else
John W. Linville82711952010-01-05 17:57:20 -050066#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050067#endif
68
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050069static struct regulatory_request core_request_world = {
70 .initiator = NL80211_REGDOM_SET_BY_CORE,
71 .alpha2[0] = '0',
72 .alpha2[1] = '0',
73 .intersect = false,
74 .processed = true,
75 .country_ie_env = ENVIRON_ANY,
76};
77
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070078/* Receipt of information from last regulatory request */
Luis R. Rodrigueza0429942011-11-28 16:47:15 -050079static struct regulatory_request *last_request = &core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +020080
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070081/* To trigger userspace events */
82static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010083
Scott James Remnant4d9d88d2011-03-08 10:45:30 -080084static struct device_type reg_device_type = {
85 .uevent = reg_device_uevent,
86};
87
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050088/*
89 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020090 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050091 * information to give us an alpha2
92 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080093const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020094
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050095/*
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070096 * Protects static reg.c components:
97 * - cfg80211_world_regdom
98 * - cfg80211_regdom
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070099 * - last_request
100 */
John W. Linville670b7f12010-06-24 11:26:31 -0400101static DEFINE_MUTEX(reg_mutex);
Johannes Berg46a5eba2010-09-15 13:28:15 +0200102
103static inline void assert_reg_lock(void)
104{
105 lockdep_assert_held(&reg_mutex);
106}
Luis R. Rodriguezabc73812009-07-30 17:38:08 -0700107
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500108/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500109static LIST_HEAD(reg_requests_list);
110static spinlock_t reg_requests_lock;
111
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500112/* Used to queue up beacon hints for review */
113static LIST_HEAD(reg_pending_beacons);
114static spinlock_t reg_pending_beacons_lock;
115
116/* Used to keep track of processed beacon hints */
117static LIST_HEAD(reg_beacon_list);
118
119struct reg_beacon {
120 struct list_head list;
121 struct ieee80211_channel chan;
122};
123
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800124static void reg_todo(struct work_struct *work);
125static DECLARE_WORK(reg_work, reg_todo);
126
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -0700127static void reg_timeout_work(struct work_struct *work);
128static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
129
Johannes Berg734366d2008-09-15 10:56:48 +0200130/* We keep a static world regulatory domain in case of the absence of CRDA */
131static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800132 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +0200133 .alpha2 = "00",
134 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500135 /* IEEE 802.11b/g, channels 1..11 */
136 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800137 /* IEEE 802.11b/g, channels 12..13. No HT40
138 * channel fits here. */
139 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500140 NL80211_RRF_PASSIVE_SCAN |
141 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800142 /* IEEE 802.11 channel 14 - Only JP enables
143 * this and for 802.11b only */
144 REG_RULE(2484-10, 2484+10, 20, 6, 20,
145 NL80211_RRF_PASSIVE_SCAN |
146 NL80211_RRF_NO_IBSS |
147 NL80211_RRF_NO_OFDM),
148 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800149 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800150 NL80211_RRF_PASSIVE_SCAN |
151 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500152
153 /* NB: 5260 MHz - 5700 MHz requies DFS */
154
155 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800156 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500157 NL80211_RRF_PASSIVE_SCAN |
158 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200159 }
160};
161
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200162static const struct ieee80211_regdomain *cfg80211_world_regdom =
163 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200164
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400165static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500166static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400167
Johannes Berg734366d2008-09-15 10:56:48 +0200168module_param(ieee80211_regdom, charp, 0444);
169MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
170
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500171static void reset_regdomains(bool full_reset)
Johannes Berg734366d2008-09-15 10:56:48 +0200172{
Johannes Berg942b25c2008-09-15 11:26:47 +0200173 /* avoid freeing static information or freeing something twice */
174 if (cfg80211_regdomain == cfg80211_world_regdom)
175 cfg80211_regdomain = NULL;
176 if (cfg80211_world_regdom == &world_regdom)
177 cfg80211_world_regdom = NULL;
178 if (cfg80211_regdomain == &world_regdom)
179 cfg80211_regdomain = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200180
181 kfree(cfg80211_regdomain);
182 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200183
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200184 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200185 cfg80211_regdomain = NULL;
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500186
187 if (!full_reset)
188 return;
189
190 if (last_request != &core_request_world)
191 kfree(last_request);
192 last_request = &core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +0200193}
194
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500195/*
196 * Dynamic world regulatory domain requested by the wireless
197 * core upon initialization
198 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200199static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200200{
Johannes Bergf6037d02008-10-21 11:01:33 +0200201 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200202
Luis R. Rodrigueza0429942011-11-28 16:47:15 -0500203 reset_regdomains(false);
Johannes Berg734366d2008-09-15 10:56:48 +0200204
205 cfg80211_world_regdom = rd;
206 cfg80211_regdomain = rd;
207}
Johannes Berg734366d2008-09-15 10:56:48 +0200208
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200209bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100210{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700211 if (!alpha2)
212 return false;
213 if (alpha2[0] == '0' && alpha2[1] == '0')
214 return true;
215 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100216}
217
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200218static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700219{
220 if (!alpha2)
221 return false;
222 if (alpha2[0] != 0 && alpha2[1] != 0)
223 return true;
224 return false;
225}
Johannes Berg8318d782008-01-24 19:38:38 +0100226
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200227static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700228{
229 if (!alpha2)
230 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500231 /*
232 * Special case where regulatory domain was built by driver
233 * but a specific alpha2 cannot be determined
234 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700235 if (alpha2[0] == '9' && alpha2[1] == '9')
236 return true;
237 return false;
238}
239
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800240static bool is_intersected_alpha2(const char *alpha2)
241{
242 if (!alpha2)
243 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500244 /*
245 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800246 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500247 * structures
248 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800249 if (alpha2[0] == '9' && alpha2[1] == '8')
250 return true;
251 return false;
252}
253
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200254static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700255{
256 if (!alpha2)
257 return false;
John W. Linvillec61029c2010-08-05 14:26:24 -0400258 if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700259 return true;
260 return false;
261}
262
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200263static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700264{
265 if (!alpha2_x || !alpha2_y)
266 return false;
267 if (alpha2_x[0] == alpha2_y[0] &&
268 alpha2_x[1] == alpha2_y[1])
269 return true;
270 return false;
271}
272
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500273static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500275 assert_cfg80211_lock();
276
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700277 if (!cfg80211_regdomain)
278 return true;
279 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
280 return false;
281 return true;
282}
283
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500284/*
285 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
286 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
287 * has ever been issued.
288 */
289static bool is_user_regdom_saved(void)
290{
291 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
292 return false;
293
294 /* This would indicate a mistake on the design */
295 if (WARN((!is_world_regdom(user_alpha2) &&
296 !is_an_alpha2(user_alpha2)),
297 "Unexpected user alpha2: %c%c\n",
298 user_alpha2[0],
299 user_alpha2[1]))
300 return false;
301
302 return true;
303}
304
John W. Linville3b377ea2009-12-18 17:59:01 -0500305static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
306 const struct ieee80211_regdomain *src_regd)
307{
308 struct ieee80211_regdomain *regd;
309 int size_of_regd = 0;
310 unsigned int i;
311
312 size_of_regd = sizeof(struct ieee80211_regdomain) +
313 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
314
315 regd = kzalloc(size_of_regd, GFP_KERNEL);
316 if (!regd)
317 return -ENOMEM;
318
319 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
320
321 for (i = 0; i < src_regd->n_reg_rules; i++)
322 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
323 sizeof(struct ieee80211_reg_rule));
324
325 *dst_regd = regd;
326 return 0;
327}
328
329#ifdef CONFIG_CFG80211_INTERNAL_REGDB
330struct reg_regdb_search_request {
331 char alpha2[2];
332 struct list_head list;
333};
334
335static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400336static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500337
338static void reg_regdb_search(struct work_struct *work)
339{
340 struct reg_regdb_search_request *request;
341 const struct ieee80211_regdomain *curdom, *regdom;
342 int i, r;
Luis R. Rodriguez7334e402012-09-14 15:36:57 -0700343 bool set_reg = false;
344
345 mutex_lock(&cfg80211_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500346
John W. Linville368d06f2010-03-16 15:40:59 -0400347 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500348 while (!list_empty(&reg_regdb_search_list)) {
349 request = list_first_entry(&reg_regdb_search_list,
350 struct reg_regdb_search_request,
351 list);
352 list_del(&request->list);
353
354 for (i=0; i<reg_regdb_size; i++) {
355 curdom = reg_regdb[i];
356
357 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
358 r = reg_copy_regd(&regdom, curdom);
359 if (r)
360 break;
Luis R. Rodriguez7334e402012-09-14 15:36:57 -0700361 set_reg = true;
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. Rodriguez7334e402012-09-14 15:36:57 -0700369
370 if (set_reg)
371 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. Rodriguez4c68b0f2012-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. Rodriguez4c68b0f2012-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
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700428/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200429bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700430{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400431 assert_cfg80211_lock();
432
Johannes Bergf6037d02008-10-21 11:01:33 +0200433 if (!last_request)
434 return false;
435
436 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700437}
438
439/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200440static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700441{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200442 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700443 u32 freq_diff;
444
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800445 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700446 return false;
447
448 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
449 return false;
450
451 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
452
Roel Kluinbd05f282009-03-03 22:55:21 +0100453 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
454 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700455 return false;
456
457 return true;
458}
459
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200460static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700461{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200462 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700463 unsigned int i;
464
465 if (!rd->n_reg_rules)
466 return false;
467
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800468 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
469 return false;
470
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700471 for (i = 0; i < rd->n_reg_rules; i++) {
472 reg_rule = &rd->reg_rules[i];
473 if (!is_valid_reg_rule(reg_rule))
474 return false;
475 }
476
477 return true;
478}
479
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400480static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
481 u32 center_freq_khz,
482 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700483{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400484 u32 start_freq_khz, end_freq_khz;
485
486 start_freq_khz = center_freq_khz - (bw_khz/2);
487 end_freq_khz = center_freq_khz + (bw_khz/2);
488
489 if (start_freq_khz >= freq_range->start_freq_khz &&
490 end_freq_khz <= freq_range->end_freq_khz)
491 return true;
492
493 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700494}
495
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800496/**
497 * freq_in_rule_band - tells us if a frequency is in a frequency band
498 * @freq_range: frequency rule we want to query
499 * @freq_khz: frequency we are inquiring about
500 *
501 * This lets us know if a specific frequency rule is or is not relevant to
502 * a specific frequency's band. Bands are device specific and artificial
503 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
504 * safe for now to assume that a frequency rule should not be part of a
505 * frequency's band if the start freq or end freq are off by more than 2 GHz.
506 * This resolution can be lowered and should be considered as we add
507 * regulatory rule support for other "bands".
508 **/
509static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
510 u32 freq_khz)
511{
512#define ONE_GHZ_IN_KHZ 1000000
513 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
514 return true;
515 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
516 return true;
517 return false;
518#undef ONE_GHZ_IN_KHZ
519}
520
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500521/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500522 * Helper for regdom_intersect(), this does the real
523 * mathematical intersection fun
524 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700525static int reg_rules_intersect(
526 const struct ieee80211_reg_rule *rule1,
527 const struct ieee80211_reg_rule *rule2,
528 struct ieee80211_reg_rule *intersected_rule)
529{
530 const struct ieee80211_freq_range *freq_range1, *freq_range2;
531 struct ieee80211_freq_range *freq_range;
532 const struct ieee80211_power_rule *power_rule1, *power_rule2;
533 struct ieee80211_power_rule *power_rule;
534 u32 freq_diff;
535
536 freq_range1 = &rule1->freq_range;
537 freq_range2 = &rule2->freq_range;
538 freq_range = &intersected_rule->freq_range;
539
540 power_rule1 = &rule1->power_rule;
541 power_rule2 = &rule2->power_rule;
542 power_rule = &intersected_rule->power_rule;
543
544 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
545 freq_range2->start_freq_khz);
546 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
547 freq_range2->end_freq_khz);
548 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
549 freq_range2->max_bandwidth_khz);
550
551 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
552 if (freq_range->max_bandwidth_khz > freq_diff)
553 freq_range->max_bandwidth_khz = freq_diff;
554
555 power_rule->max_eirp = min(power_rule1->max_eirp,
556 power_rule2->max_eirp);
557 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
558 power_rule2->max_antenna_gain);
559
560 intersected_rule->flags = (rule1->flags | rule2->flags);
561
562 if (!is_valid_reg_rule(intersected_rule))
563 return -EINVAL;
564
565 return 0;
566}
567
568/**
569 * regdom_intersect - do the intersection between two regulatory domains
570 * @rd1: first regulatory domain
571 * @rd2: second regulatory domain
572 *
573 * Use this function to get the intersection between two regulatory domains.
574 * Once completed we will mark the alpha2 for the rd as intersected, "98",
575 * as no one single alpha2 can represent this regulatory domain.
576 *
577 * Returns a pointer to the regulatory domain structure which will hold the
578 * resulting intersection of rules between rd1 and rd2. We will
579 * kzalloc() this structure for you.
580 */
581static struct ieee80211_regdomain *regdom_intersect(
582 const struct ieee80211_regdomain *rd1,
583 const struct ieee80211_regdomain *rd2)
584{
585 int r, size_of_regd;
586 unsigned int x, y;
587 unsigned int num_rules = 0, rule_idx = 0;
588 const struct ieee80211_reg_rule *rule1, *rule2;
589 struct ieee80211_reg_rule *intersected_rule;
590 struct ieee80211_regdomain *rd;
591 /* This is just a dummy holder to help us count */
592 struct ieee80211_reg_rule irule;
593
594 /* Uses the stack temporarily for counter arithmetic */
595 intersected_rule = &irule;
596
597 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
598
599 if (!rd1 || !rd2)
600 return NULL;
601
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500602 /*
603 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700604 * build them. This is to so we can malloc() and free() a
605 * regdomain once. The reason we use reg_rules_intersect() here
606 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500607 * All rules that do check out OK are valid.
608 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700609
610 for (x = 0; x < rd1->n_reg_rules; x++) {
611 rule1 = &rd1->reg_rules[x];
612 for (y = 0; y < rd2->n_reg_rules; y++) {
613 rule2 = &rd2->reg_rules[y];
614 if (!reg_rules_intersect(rule1, rule2,
615 intersected_rule))
616 num_rules++;
617 memset(intersected_rule, 0,
618 sizeof(struct ieee80211_reg_rule));
619 }
620 }
621
622 if (!num_rules)
623 return NULL;
624
625 size_of_regd = sizeof(struct ieee80211_regdomain) +
626 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
627
628 rd = kzalloc(size_of_regd, GFP_KERNEL);
629 if (!rd)
630 return NULL;
631
632 for (x = 0; x < rd1->n_reg_rules; x++) {
633 rule1 = &rd1->reg_rules[x];
634 for (y = 0; y < rd2->n_reg_rules; y++) {
635 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500636 /*
637 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700638 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500639 * a memcpy()
640 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700641 intersected_rule = &rd->reg_rules[rule_idx];
642 r = reg_rules_intersect(rule1, rule2,
643 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;
679 return channel_flags;
680}
681
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800682static int freq_reg_info_regd(struct wiphy *wiphy,
683 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400684 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800685 const struct ieee80211_reg_rule **reg_rule,
686 const struct ieee80211_regdomain *custom_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. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800690 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400691 bool bw_fits = false;
692
693 if (!desired_bw_khz)
694 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +0100695
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800696 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800697
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500698 /*
699 * Follow the driver's regulatory domain, if present, unless a country
700 * IE has been processed or a user wants to help complaince further
701 */
Luis R. Rodriguez2784fe92010-12-15 19:24:11 -0800702 if (!custom_regd &&
703 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400704 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800705 wiphy->regd)
706 regd = wiphy->regd;
707
708 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700709 return -EINVAL;
710
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800711 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700712 const struct ieee80211_reg_rule *rr;
713 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700714
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800715 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700716 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800717
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500718 /*
719 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800720 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500721 * not overwrite it once found
722 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800723 if (!band_rule_found)
724 band_rule_found = freq_in_rule_band(fr, center_freq);
725
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400726 bw_fits = reg_does_bw_fit(fr,
727 center_freq,
728 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800729
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400730 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700731 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400732 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100733 }
734 }
735
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800736 if (!band_rule_found)
737 return -ERANGE;
738
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400739 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700740}
741
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400742int freq_reg_info(struct wiphy *wiphy,
743 u32 center_freq,
744 u32 desired_bw_khz,
745 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800746{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400747 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400748 return freq_reg_info_regd(wiphy,
749 center_freq,
750 desired_bw_khz,
751 reg_rule,
752 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800753}
John W. Linville4f366c52010-07-15 14:57:33 -0400754EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700755
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530756#ifdef CONFIG_CFG80211_REG_DEBUG
757static const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
758{
759 switch (initiator) {
760 case NL80211_REGDOM_SET_BY_CORE:
761 return "Set by core";
762 case NL80211_REGDOM_SET_BY_USER:
763 return "Set by user";
764 case NL80211_REGDOM_SET_BY_DRIVER:
765 return "Set by driver";
766 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
767 return "Set by country IE";
768 default:
769 WARN_ON(1);
770 return "Set by bug";
771 }
772}
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530773
774static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
775 u32 desired_bw_khz,
776 const struct ieee80211_reg_rule *reg_rule)
777{
778 const struct ieee80211_power_rule *power_rule;
779 const struct ieee80211_freq_range *freq_range;
780 char max_antenna_gain[32];
781
782 power_rule = &reg_rule->power_rule;
783 freq_range = &reg_rule->freq_range;
784
785 if (!power_rule->max_antenna_gain)
786 snprintf(max_antenna_gain, 32, "N/A");
787 else
788 snprintf(max_antenna_gain, 32, "%d", power_rule->max_antenna_gain);
789
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700790 REG_DBG_PRINT("Updating information on frequency %d MHz "
Bob Copelandff039c62010-12-29 17:09:02 -0500791 "for a %d MHz width channel with regulatory rule:\n",
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530792 chan->center_freq,
793 KHZ_TO_MHZ(desired_bw_khz));
794
Pavel Roskin56e67862011-09-01 11:54:12 -0400795 REG_DBG_PRINT("%d KHz - %d KHz @ %d KHz), (%s mBi, %d mBm)\n",
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530796 freq_range->start_freq_khz,
797 freq_range->end_freq_khz,
Pavel Roskin56e67862011-09-01 11:54:12 -0400798 freq_range->max_bandwidth_khz,
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530799 max_antenna_gain,
800 power_rule->max_eirp);
801}
802#else
803static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
804 u32 desired_bw_khz,
805 const struct ieee80211_reg_rule *reg_rule)
806{
807 return;
808}
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530809#endif
810
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400811/*
812 * Note that right now we assume the desired channel bandwidth
813 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
814 * per channel, the primary and the extension channel). To support
815 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
816 * new ieee80211_channel.target_bw and re run the regulatory check
817 * on the wiphy with the target_bw specified. Then we can simply use
818 * that below for the desired_bw_khz below.
819 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700820static void handle_channel(struct wiphy *wiphy,
821 enum nl80211_reg_initiator initiator,
822 enum ieee80211_band band,
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800823 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700824{
825 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400826 u32 flags, bw_flags = 0;
827 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700828 const struct ieee80211_reg_rule *reg_rule = NULL;
829 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400830 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800831 struct ieee80211_supported_band *sband;
832 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500833 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800834
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500835 assert_cfg80211_lock();
836
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500837 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
838
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800839 sband = wiphy->bands[band];
840 BUG_ON(chan_idx >= sband->n_channels);
841 chan = &sband->channels[chan_idx];
842
843 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700844
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400845 r = freq_reg_info(wiphy,
846 MHZ_TO_KHZ(chan->center_freq),
847 desired_bw_khz,
848 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700849
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700850 if (r) {
851 /*
852 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300853 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700854 * from a Country IE and the Country IE had no information
855 * about a band. The IEEE 802.11 spec allows for an AP
856 * to send only a subset of the regulatory rules allowed,
857 * so an AP in the US that only supports 2.4 GHz may only send
858 * a country IE with information for the 2.4 GHz band
859 * while 5 GHz is still supported.
860 */
861 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
862 r == -ERANGE)
863 return;
864
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700865 REG_DBG_PRINT("Disabling freq %d MHz\n", chan->center_freq);
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700866 chan->flags = IEEE80211_CHAN_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +0100867 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700868 }
Johannes Berg8318d782008-01-24 19:38:38 +0100869
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530870 chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
871
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700872 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400873 freq_range = &reg_rule->freq_range;
874
875 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
876 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700877
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400878 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500879 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +0100880 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500881 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300882 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800883 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500884 * settings
885 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800886 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400887 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800888 chan->max_antenna_gain = chan->orig_mag =
889 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800890 chan->max_power = chan->orig_mpwr =
891 (int) MBM_TO_DBM(power_rule->max_eirp);
892 return;
893 }
894
Rajkumar Manoharanaa3d7ee2011-09-14 14:28:17 +0530895 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400896 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100897 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700898 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
Hong Wueccc0682012-01-11 20:33:39 +0200899 chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Stanislaw Gruszka02954032012-07-24 08:35:39 +0200900 if (chan->orig_mpwr) {
901 /*
902 * Devices that have their own custom regulatory domain
903 * but also use WIPHY_FLAG_STRICT_REGULATORY will follow the
904 * passed country IE power settings.
905 */
906 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
907 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
908 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
909 chan->max_power = chan->max_reg_power;
910 else
911 chan->max_power = min(chan->orig_mpwr,
912 chan->max_reg_power);
913 } else
914 chan->max_power = chan->max_reg_power;
Johannes Berg8318d782008-01-24 19:38:38 +0100915}
916
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700917static void handle_band(struct wiphy *wiphy,
918 enum ieee80211_band band,
919 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +0100920{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800921 unsigned int i;
922 struct ieee80211_supported_band *sband;
923
924 BUG_ON(!wiphy->bands[band]);
925 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +0100926
927 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700928 handle_channel(wiphy, initiator, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +0100929}
930
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400931static bool ignore_reg_update(struct wiphy *wiphy,
932 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800933{
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530934 if (!last_request) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700935 REG_DBG_PRINT("Ignoring regulatory request %s since "
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530936 "last_request is not set\n",
937 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800938 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530939 }
940
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400941 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530942 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700943 REG_DBG_PRINT("Ignoring regulatory request %s "
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530944 "since the driver uses its own custom "
Joe Perches12c5ffb2011-07-29 14:51:25 -0700945 "regulatory domain\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530946 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800947 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530948 }
949
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500950 /*
951 * wiphy->regd will be set once the device has its own
952 * desired regulatory domain set
953 */
Johannes Berg5be83de2009-11-19 00:56:28 +0100954 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -0700955 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530956 !is_world_regdom(last_request->alpha2)) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700957 REG_DBG_PRINT("Ignoring regulatory request %s "
Mihai Moldovan5bc91db2011-07-25 11:18:58 +0200958 "since the driver requires its own regulatory "
Joe Perches12c5ffb2011-07-29 14:51:25 -0700959 "domain to be set first\n",
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530960 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800961 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530962 }
963
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800964 return false;
965}
966
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500967static void handle_reg_beacon(struct wiphy *wiphy,
968 unsigned int chan_idx,
969 struct reg_beacon *reg_beacon)
970{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500971 struct ieee80211_supported_band *sband;
972 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400973 bool channel_changed = false;
974 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500975
976 assert_cfg80211_lock();
977
978 sband = wiphy->bands[reg_beacon->chan.band];
979 chan = &sband->channels[chan_idx];
980
981 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
982 return;
983
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400984 if (chan->beacon_found)
985 return;
986
987 chan->beacon_found = true;
988
Johannes Berg5be83de2009-11-19 00:56:28 +0100989 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -0700990 return;
991
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400992 chan_before.center_freq = chan->center_freq;
993 chan_before.flags = chan->flags;
994
Luis R. Rodriguez37184242009-07-30 17:43:48 -0700995 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500996 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400997 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500998 }
999
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001000 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001001 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001002 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001003 }
1004
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001005 if (channel_changed)
1006 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001007}
1008
1009/*
1010 * Called when a scan on a wiphy finds a beacon on
1011 * new channel
1012 */
1013static void wiphy_update_new_beacon(struct wiphy *wiphy,
1014 struct reg_beacon *reg_beacon)
1015{
1016 unsigned int i;
1017 struct ieee80211_supported_band *sband;
1018
1019 assert_cfg80211_lock();
1020
1021 if (!wiphy->bands[reg_beacon->chan.band])
1022 return;
1023
1024 sband = wiphy->bands[reg_beacon->chan.band];
1025
1026 for (i = 0; i < sband->n_channels; i++)
1027 handle_reg_beacon(wiphy, i, reg_beacon);
1028}
1029
1030/*
1031 * Called upon reg changes or a new wiphy is added
1032 */
1033static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1034{
1035 unsigned int i;
1036 struct ieee80211_supported_band *sband;
1037 struct reg_beacon *reg_beacon;
1038
1039 assert_cfg80211_lock();
1040
1041 if (list_empty(&reg_beacon_list))
1042 return;
1043
1044 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1045 if (!wiphy->bands[reg_beacon->chan.band])
1046 continue;
1047 sband = wiphy->bands[reg_beacon->chan.band];
1048 for (i = 0; i < sband->n_channels; i++)
1049 handle_reg_beacon(wiphy, i, reg_beacon);
1050 }
1051}
1052
1053static bool reg_is_world_roaming(struct wiphy *wiphy)
1054{
1055 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1056 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1057 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001058 if (last_request &&
1059 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001060 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001061 return true;
1062 return false;
1063}
1064
1065/* Reap the advantages of previously found beacons */
1066static void reg_process_beacons(struct wiphy *wiphy)
1067{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001068 /*
1069 * Means we are just firing up cfg80211, so no beacons would
1070 * have been processed yet.
1071 */
1072 if (!last_request)
1073 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001074 if (!reg_is_world_roaming(wiphy))
1075 return;
1076 wiphy_update_beacon_reg(wiphy);
1077}
1078
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001079static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1080{
1081 if (!chan)
1082 return true;
1083 if (chan->flags & IEEE80211_CHAN_DISABLED)
1084 return true;
1085 /* This would happen when regulatory rules disallow HT40 completely */
1086 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1087 return true;
1088 return false;
1089}
1090
1091static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1092 enum ieee80211_band band,
1093 unsigned int chan_idx)
1094{
1095 struct ieee80211_supported_band *sband;
1096 struct ieee80211_channel *channel;
1097 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1098 unsigned int i;
1099
1100 assert_cfg80211_lock();
1101
1102 sband = wiphy->bands[band];
1103 BUG_ON(chan_idx >= sband->n_channels);
1104 channel = &sband->channels[chan_idx];
1105
1106 if (is_ht40_not_allowed(channel)) {
1107 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];
1117 if (c->center_freq == (channel->center_freq - 20))
1118 channel_before = c;
1119 if (c->center_freq == (channel->center_freq + 20))
1120 channel_after = c;
1121 }
1122
1123 /*
1124 * Please note that this assumes target bandwidth is 20 MHz,
1125 * if that ever changes we also need to change the below logic
1126 * to include that as well.
1127 */
1128 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001129 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001130 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001131 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001132
1133 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001134 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001135 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001136 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001137}
1138
1139static void reg_process_ht_flags_band(struct wiphy *wiphy,
1140 enum ieee80211_band band)
1141{
1142 unsigned int i;
1143 struct ieee80211_supported_band *sband;
1144
1145 BUG_ON(!wiphy->bands[band]);
1146 sband = wiphy->bands[band];
1147
1148 for (i = 0; i < sband->n_channels; i++)
1149 reg_process_ht_flags_channel(wiphy, band, i);
1150}
1151
1152static void reg_process_ht_flags(struct wiphy *wiphy)
1153{
1154 enum ieee80211_band band;
1155
1156 if (!wiphy)
1157 return;
1158
1159 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1160 if (wiphy->bands[band])
1161 reg_process_ht_flags_band(wiphy, band);
1162 }
1163
1164}
1165
Sven Neumanneac03e32011-08-30 23:38:53 +02001166static void wiphy_update_regulatory(struct wiphy *wiphy,
1167 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001168{
1169 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001170
Sven Neumanneac03e32011-08-30 23:38:53 +02001171 assert_reg_lock();
1172
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001173 if (ignore_reg_update(wiphy, initiator))
Sven Neumanna203c2a2011-07-12 15:52:07 +02001174 return;
1175
Luis R. Rodriguezb68e6b32011-10-11 10:59:03 -07001176 last_request->dfs_region = cfg80211_regdomain->dfs_region;
1177
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001178 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001179 if (wiphy->bands[band])
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001180 handle_band(wiphy, band, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001181 }
Sven Neumanna203c2a2011-07-12 15:52:07 +02001182
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001183 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001184 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001185 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001186 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001187}
1188
Sven Neumanneac03e32011-08-30 23:38:53 +02001189void regulatory_update(struct wiphy *wiphy,
1190 enum nl80211_reg_initiator setby)
1191{
1192 mutex_lock(&reg_mutex);
1193 wiphy_update_regulatory(wiphy, setby);
1194 mutex_unlock(&reg_mutex);
1195}
1196
Sven Neumannd7549cb2011-08-30 23:38:54 +02001197static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1198{
1199 struct cfg80211_registered_device *rdev;
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301200 struct wiphy *wiphy;
Sven Neumannd7549cb2011-08-30 23:38:54 +02001201
Rajkumar Manoharan4a389942011-12-08 23:59:26 +05301202 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1203 wiphy = &rdev->wiphy;
1204 wiphy_update_regulatory(wiphy, initiator);
1205 /*
1206 * Regulatory updates set by CORE are ignored for custom
1207 * regulatory cards. Let us notify the changes to the driver,
1208 * as some drivers used this to restore its orig_* reg domain.
1209 */
1210 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1211 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY &&
1212 wiphy->reg_notifier)
1213 wiphy->reg_notifier(wiphy, last_request);
1214 }
Sven Neumannd7549cb2011-08-30 23:38:54 +02001215}
1216
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001217static void handle_channel_custom(struct wiphy *wiphy,
1218 enum ieee80211_band band,
1219 unsigned int chan_idx,
1220 const struct ieee80211_regdomain *regd)
1221{
1222 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001223 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1224 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001225 const struct ieee80211_reg_rule *reg_rule = NULL;
1226 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001227 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001228 struct ieee80211_supported_band *sband;
1229 struct ieee80211_channel *chan;
1230
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001231 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001232
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001233 sband = wiphy->bands[band];
1234 BUG_ON(chan_idx >= sband->n_channels);
1235 chan = &sband->channels[chan_idx];
1236
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001237 r = freq_reg_info_regd(wiphy,
1238 MHZ_TO_KHZ(chan->center_freq),
1239 desired_bw_khz,
1240 &reg_rule,
1241 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001242
1243 if (r) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001244 REG_DBG_PRINT("Disabling freq %d MHz as custom "
Luis R. Rodrigueza6518532010-10-20 10:18:57 -07001245 "regd has no rule that fits a %d MHz "
1246 "wide channel\n",
1247 chan->center_freq,
1248 KHZ_TO_MHZ(desired_bw_khz));
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001249 chan->flags = IEEE80211_CHAN_DISABLED;
1250 return;
1251 }
1252
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301253 chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
1254
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001255 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001256 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001257
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001258 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1259 bw_flags = IEEE80211_CHAN_NO_HT40;
1260
1261 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001262 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001263 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1264}
1265
1266static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1267 const struct ieee80211_regdomain *regd)
1268{
1269 unsigned int i;
1270 struct ieee80211_supported_band *sband;
1271
1272 BUG_ON(!wiphy->bands[band]);
1273 sband = wiphy->bands[band];
1274
1275 for (i = 0; i < sband->n_channels; i++)
1276 handle_channel_custom(wiphy, band, i, regd);
1277}
1278
1279/* Used by drivers prior to wiphy registration */
1280void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1281 const struct ieee80211_regdomain *regd)
1282{
1283 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001284 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001285
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001286 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001287 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001288 if (!wiphy->bands[band])
1289 continue;
1290 handle_band_custom(wiphy, band, regd);
1291 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001292 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001293 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001294
1295 /*
1296 * no point in calling this if it won't have any effect
1297 * on your device's supportd bands.
1298 */
1299 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001300}
1301EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1302
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001303/*
1304 * Return value which can be used by ignore_request() to indicate
1305 * it has been determined we should intersect two regulatory domains
1306 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001307#define REG_INTERSECT 1
1308
Johannes Berg84fa4f42008-10-24 20:32:23 +02001309/* This has the logic which determines when a new request
1310 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001311static int ignore_request(struct wiphy *wiphy,
1312 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001313{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001314 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001315
1316 assert_cfg80211_lock();
1317
Johannes Berg84fa4f42008-10-24 20:32:23 +02001318 /* All initial requests are respected */
1319 if (!last_request)
1320 return 0;
1321
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001322 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001323 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001324 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001325 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001326
1327 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1328
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001329 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001330 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001331 if (last_request->initiator ==
1332 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001333 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001334 /*
1335 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001336 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001337 * intersect them, but that seems unlikely
1338 * to be correct. Reject second one for now.
1339 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001340 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001341 return -EOPNOTSUPP;
1342 return -EALREADY;
1343 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001344 /*
1345 * Two consecutive Country IE hints on the same wiphy.
1346 * This should be picked up early by the driver/stack
1347 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001348 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001349 return 0;
1350 return -EALREADY;
1351 }
Luis R. Rodrigueza171fba2010-10-18 17:44:51 -07001352 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001353 case NL80211_REGDOM_SET_BY_DRIVER:
1354 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001355 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001356 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001357 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001358 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001359
1360 /*
1361 * This would happen if you unplug and plug your card
1362 * back in or if you add a new device for which the previously
1363 * loaded card also agrees on the regulatory domain.
1364 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001365 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001366 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001367 return -EALREADY;
1368
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001369 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001370 case NL80211_REGDOM_SET_BY_USER:
1371 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001372 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001373 /*
1374 * If the user knows better the user should set the regdom
1375 * to their country before the IE is picked up
1376 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001377 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001378 last_request->intersect)
1379 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001380 /*
1381 * Process user requests only after previous user/driver/core
1382 * requests have been processed
1383 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001384 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1385 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1386 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001387 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001388 return -EAGAIN;
1389 }
1390
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001391 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001392 return -EALREADY;
1393
Johannes Berg84fa4f42008-10-24 20:32:23 +02001394 return 0;
1395 }
1396
1397 return -EINVAL;
1398}
1399
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001400static void reg_set_request_processed(void)
1401{
1402 bool need_more_processing = false;
1403
1404 last_request->processed = true;
1405
1406 spin_lock(&reg_requests_lock);
1407 if (!list_empty(&reg_requests_list))
1408 need_more_processing = true;
1409 spin_unlock(&reg_requests_lock);
1410
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001411 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER)
Eliad Peller0d84f6e2012-06-12 12:53:13 +03001412 cancel_delayed_work(&reg_timeout);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001413
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001414 if (need_more_processing)
1415 schedule_work(&reg_work);
1416}
1417
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001418/**
1419 * __regulatory_hint - hint to the wireless core a regulatory domain
1420 * @wiphy: if the hint comes from country information from an AP, this
1421 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001422 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001423 *
1424 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001425 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001426 *
1427 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1428 * already been set or other standard error codes.
1429 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001430 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001431 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001432static int __regulatory_hint(struct wiphy *wiphy,
1433 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001434{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001435 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001436 int r = 0;
1437
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001438 assert_cfg80211_lock();
1439
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001440 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001441
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001442 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001443 if (pending_request->initiator ==
1444 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001445 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001446 if (r) {
1447 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001448 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001449 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001450 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001451 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001452 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001453 /*
1454 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001455 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001456 * wiphy
1457 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001458 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001459 pending_request->initiator ==
1460 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001461 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001462 if (r) {
1463 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001464 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001465 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001466 r = -EALREADY;
1467 goto new_request;
1468 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001469 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001470 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001471 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001472
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001473new_request:
Luis R. Rodrigueza0429942011-11-28 16:47:15 -05001474 if (last_request != &core_request_world)
1475 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001476
1477 last_request = pending_request;
1478 last_request->intersect = intersect;
1479
1480 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001481
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001482 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1483 user_alpha2[0] = last_request->alpha2[0];
1484 user_alpha2[1] = last_request->alpha2[1];
1485 }
1486
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001487 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001488 if (r < 0) {
1489 /*
1490 * Since CRDA will not be called in this case as we already
1491 * have applied the requested regulatory domain before we just
1492 * inform userspace we have processed the request
1493 */
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001494 if (r == -EALREADY) {
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001495 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001496 reg_set_request_processed();
1497 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001498 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001499 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001500
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001501 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001502}
1503
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001504/* This processes *all* regulatory hints */
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001505static void reg_process_hint(struct regulatory_request *reg_request,
1506 enum nl80211_reg_initiator reg_initiator)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001507{
1508 int r = 0;
1509 struct wiphy *wiphy = NULL;
1510
1511 BUG_ON(!reg_request->alpha2);
1512
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001513 if (wiphy_idx_valid(reg_request->wiphy_idx))
1514 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1515
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001516 if (reg_initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001517 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001518 kfree(reg_request);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001519 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001520 }
1521
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001522 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001523 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001524 if (r == -EALREADY && wiphy &&
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001525 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001526 wiphy_update_regulatory(wiphy, reg_initiator);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001527 return;
1528 }
1529
1530 /*
1531 * We only time out user hints, given that they should be the only
1532 * source of bogus requests.
1533 */
Luis R. Rodriguezc989bb12011-04-25 18:35:48 -07001534 if (r != -EALREADY &&
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001535 reg_initiator == NL80211_REGDOM_SET_BY_USER)
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001536 schedule_delayed_work(&reg_timeout, msecs_to_jiffies(3142));
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001537}
1538
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001539/*
1540 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1541 * Regulatory hints come on a first come first serve basis and we
1542 * must process each one atomically.
1543 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001544static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001545{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001546 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001547
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001548 mutex_lock(&cfg80211_mutex);
1549 mutex_lock(&reg_mutex);
1550
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001551 /* When last_request->processed becomes true this will be rescheduled */
1552 if (last_request && !last_request->processed) {
1553 REG_DBG_PRINT("Pending regulatory request, waiting "
Joe Perches12c5ffb2011-07-29 14:51:25 -07001554 "for it to be processed...\n");
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001555 goto out;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001556 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001557
1558 spin_lock(&reg_requests_lock);
1559
1560 if (list_empty(&reg_requests_list)) {
1561 spin_unlock(&reg_requests_lock);
1562 goto out;
1563 }
1564
1565 reg_request = list_first_entry(&reg_requests_list,
1566 struct regulatory_request,
1567 list);
1568 list_del_init(&reg_request->list);
1569
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001570 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001571
Luis R. Rodriguez8848bef2011-12-20 12:23:36 -08001572 reg_process_hint(reg_request, reg_request->initiator);
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001573
1574out:
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001575 mutex_unlock(&reg_mutex);
1576 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001577}
1578
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001579/* Processes beacon hints -- this has nothing to do with country IEs */
1580static void reg_process_pending_beacon_hints(void)
1581{
Johannes Berg79c97e92009-07-07 03:56:12 +02001582 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001583 struct reg_beacon *pending_beacon, *tmp;
1584
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001585 /*
1586 * No need to hold the reg_mutex here as we just touch wiphys
1587 * and do not read or access regulatory variables.
1588 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001589 mutex_lock(&cfg80211_mutex);
1590
1591 /* This goes through the _pending_ beacon list */
1592 spin_lock_bh(&reg_pending_beacons_lock);
1593
1594 if (list_empty(&reg_pending_beacons)) {
1595 spin_unlock_bh(&reg_pending_beacons_lock);
1596 goto out;
1597 }
1598
1599 list_for_each_entry_safe(pending_beacon, tmp,
1600 &reg_pending_beacons, list) {
1601
1602 list_del_init(&pending_beacon->list);
1603
1604 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001605 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1606 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001607
1608 /* Remembers the beacon hint for new wiphys or reg changes */
1609 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1610 }
1611
1612 spin_unlock_bh(&reg_pending_beacons_lock);
1613out:
1614 mutex_unlock(&cfg80211_mutex);
1615}
1616
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001617static void reg_todo(struct work_struct *work)
1618{
1619 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001620 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001621}
1622
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001623static void queue_regulatory_request(struct regulatory_request *request)
1624{
John W. Linvillec61029c2010-08-05 14:26:24 -04001625 if (isalpha(request->alpha2[0]))
1626 request->alpha2[0] = toupper(request->alpha2[0]);
1627 if (isalpha(request->alpha2[1]))
1628 request->alpha2[1] = toupper(request->alpha2[1]);
1629
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001630 spin_lock(&reg_requests_lock);
1631 list_add_tail(&request->list, &reg_requests_list);
1632 spin_unlock(&reg_requests_lock);
1633
1634 schedule_work(&reg_work);
1635}
1636
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001637/*
1638 * Core regulatory hint -- happens during cfg80211_init()
1639 * and when we restore regulatory settings.
1640 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001641static int regulatory_hint_core(const char *alpha2)
1642{
1643 struct regulatory_request *request;
1644
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001645 request = kzalloc(sizeof(struct regulatory_request),
1646 GFP_KERNEL);
1647 if (!request)
1648 return -ENOMEM;
1649
1650 request->alpha2[0] = alpha2[0];
1651 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001652 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001653
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08001654 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001655
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001656 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001657}
1658
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001659/* User hints */
1660int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001661{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001662 struct regulatory_request *request;
1663
Johannes Bergbe3d4812008-10-24 20:32:21 +02001664 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001665
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001666 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1667 if (!request)
1668 return -ENOMEM;
1669
1670 request->wiphy_idx = WIPHY_IDX_STALE;
1671 request->alpha2[0] = alpha2[0];
1672 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001673 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001674
1675 queue_regulatory_request(request);
1676
1677 return 0;
1678}
1679
1680/* Driver hints */
1681int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1682{
1683 struct regulatory_request *request;
1684
1685 BUG_ON(!alpha2);
1686 BUG_ON(!wiphy);
1687
1688 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1689 if (!request)
1690 return -ENOMEM;
1691
1692 request->wiphy_idx = get_wiphy_idx(wiphy);
1693
1694 /* Must have registered wiphy first */
1695 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1696
1697 request->alpha2[0] = alpha2[0];
1698 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001699 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001700
1701 queue_regulatory_request(request);
1702
1703 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001704}
1705EXPORT_SYMBOL(regulatory_hint);
1706
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001707/*
1708 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1709 * therefore cannot iterate over the rdev list here.
1710 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001711void regulatory_hint_11d(struct wiphy *wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05001712 enum ieee80211_band band,
1713 u8 *country_ie,
1714 u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001715{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001716 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001717 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001718 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001719
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001720 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001721
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001722 if (unlikely(!last_request))
1723 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001724
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001725 /* IE len must be evenly divisible by 2 */
1726 if (country_ie_len & 0x01)
1727 goto out;
1728
1729 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1730 goto out;
1731
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001732 alpha2[0] = country_ie[0];
1733 alpha2[1] = country_ie[1];
1734
1735 if (country_ie[2] == 'I')
1736 env = ENVIRON_INDOOR;
1737 else if (country_ie[2] == 'O')
1738 env = ENVIRON_OUTDOOR;
1739
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001740 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001741 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001742 * We leave conflict resolution to the workqueue, where can hold
1743 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001744 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001745 if (likely(last_request->initiator ==
1746 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001747 wiphy_idx_valid(last_request->wiphy_idx)))
1748 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001749
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001750 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1751 if (!request)
Dan Carpenterf9f9b6e2010-07-22 13:26:50 +02001752 goto out;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001753
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001754 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04001755 request->alpha2[0] = alpha2[0];
1756 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001757 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001758 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001759
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001760 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001761
1762 queue_regulatory_request(request);
1763
1764 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001765
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001766out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001767 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001768}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001769
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001770static void restore_alpha2(char *alpha2, bool reset_user)
1771{
1772 /* indicates there is no alpha2 to consider for restoration */
1773 alpha2[0] = '9';
1774 alpha2[1] = '7';
1775
1776 /* The user setting has precedence over the module parameter */
1777 if (is_user_regdom_saved()) {
1778 /* Unless we're asked to ignore it and reset it */
1779 if (reset_user) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001780 REG_DBG_PRINT("Restoring regulatory settings "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001781 "including user preference\n");
1782 user_alpha2[0] = '9';
1783 user_alpha2[1] = '7';
1784
1785 /*
1786 * If we're ignoring user settings, we still need to
1787 * check the module parameter to ensure we put things
1788 * back as they were for a full restore.
1789 */
1790 if (!is_world_regdom(ieee80211_regdom)) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001791 REG_DBG_PRINT("Keeping preference on "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001792 "module parameter ieee80211_regdom: %c%c\n",
1793 ieee80211_regdom[0],
1794 ieee80211_regdom[1]);
1795 alpha2[0] = ieee80211_regdom[0];
1796 alpha2[1] = ieee80211_regdom[1];
1797 }
1798 } else {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001799 REG_DBG_PRINT("Restoring regulatory settings "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001800 "while preserving user preference for: %c%c\n",
1801 user_alpha2[0],
1802 user_alpha2[1]);
1803 alpha2[0] = user_alpha2[0];
1804 alpha2[1] = user_alpha2[1];
1805 }
1806 } else if (!is_world_regdom(ieee80211_regdom)) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001807 REG_DBG_PRINT("Keeping preference on "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001808 "module parameter ieee80211_regdom: %c%c\n",
1809 ieee80211_regdom[0],
1810 ieee80211_regdom[1]);
1811 alpha2[0] = ieee80211_regdom[0];
1812 alpha2[1] = ieee80211_regdom[1];
1813 } else
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001814 REG_DBG_PRINT("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001815}
1816
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301817static void restore_custom_reg_settings(struct wiphy *wiphy)
1818{
1819 struct ieee80211_supported_band *sband;
1820 enum ieee80211_band band;
1821 struct ieee80211_channel *chan;
1822 int i;
1823
1824 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1825 sband = wiphy->bands[band];
1826 if (!sband)
1827 continue;
1828 for (i = 0; i < sband->n_channels; i++) {
1829 chan = &sband->channels[i];
1830 chan->flags = chan->orig_flags;
1831 chan->max_antenna_gain = chan->orig_mag;
1832 chan->max_power = chan->orig_mpwr;
1833 }
1834 }
1835}
1836
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001837/*
1838 * Restoring regulatory settings involves ingoring any
1839 * possibly stale country IE information and user regulatory
1840 * settings if so desired, this includes any beacon hints
1841 * learned as we could have traveled outside to another country
1842 * after disconnection. To restore regulatory settings we do
1843 * exactly what we did at bootup:
1844 *
1845 * - send a core regulatory hint
1846 * - send a user regulatory hint if applicable
1847 *
1848 * Device drivers that send a regulatory hint for a specific country
1849 * keep their own regulatory domain on wiphy->regd so that does does
1850 * not need to be remembered.
1851 */
1852static void restore_regulatory_settings(bool reset_user)
1853{
1854 char alpha2[2];
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08001855 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001856 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001857 struct regulatory_request *reg_request, *tmp;
1858 LIST_HEAD(tmp_reg_req_list);
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301859 struct cfg80211_registered_device *rdev;
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001860
1861 mutex_lock(&cfg80211_mutex);
1862 mutex_lock(&reg_mutex);
1863
Luis R. Rodrigueza0429942011-11-28 16:47:15 -05001864 reset_regdomains(true);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001865 restore_alpha2(alpha2, reset_user);
1866
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001867 /*
1868 * If there's any pending requests we simply
1869 * stash them to a temporary pending queue and
1870 * add then after we've restored regulatory
1871 * settings.
1872 */
1873 spin_lock(&reg_requests_lock);
1874 if (!list_empty(&reg_requests_list)) {
1875 list_for_each_entry_safe(reg_request, tmp,
1876 &reg_requests_list, list) {
1877 if (reg_request->initiator !=
1878 NL80211_REGDOM_SET_BY_USER)
1879 continue;
1880 list_del(&reg_request->list);
1881 list_add_tail(&reg_request->list, &tmp_reg_req_list);
1882 }
1883 }
1884 spin_unlock(&reg_requests_lock);
1885
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001886 /* Clear beacon hints */
1887 spin_lock_bh(&reg_pending_beacons_lock);
1888 if (!list_empty(&reg_pending_beacons)) {
1889 list_for_each_entry_safe(reg_beacon, btmp,
1890 &reg_pending_beacons, list) {
1891 list_del(&reg_beacon->list);
1892 kfree(reg_beacon);
1893 }
1894 }
1895 spin_unlock_bh(&reg_pending_beacons_lock);
1896
1897 if (!list_empty(&reg_beacon_list)) {
1898 list_for_each_entry_safe(reg_beacon, btmp,
1899 &reg_beacon_list, list) {
1900 list_del(&reg_beacon->list);
1901 kfree(reg_beacon);
1902 }
1903 }
1904
1905 /* First restore to the basic regulatory settings */
1906 cfg80211_regdomain = cfg80211_world_regdom;
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08001907 world_alpha2[0] = cfg80211_regdomain->alpha2[0];
1908 world_alpha2[1] = cfg80211_regdomain->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001909
Rajkumar Manoharan5ce543d2011-12-07 21:50:08 +05301910 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1911 if (rdev->wiphy.flags & WIPHY_FLAG_CUSTOM_REGULATORY)
1912 restore_custom_reg_settings(&rdev->wiphy);
1913 }
1914
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001915 mutex_unlock(&reg_mutex);
1916 mutex_unlock(&cfg80211_mutex);
1917
Dmitry Shmidtcee0bec2011-12-19 12:32:21 -08001918 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001919
1920 /*
1921 * This restores the ieee80211_regdom module parameter
1922 * preference or the last user requested regulatory
1923 * settings, user regulatory settings takes precedence.
1924 */
1925 if (is_an_alpha2(alpha2))
1926 regulatory_hint_user(user_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001927
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001928 if (list_empty(&tmp_reg_req_list))
1929 return;
1930
1931 mutex_lock(&cfg80211_mutex);
1932 mutex_lock(&reg_mutex);
1933
1934 spin_lock(&reg_requests_lock);
1935 list_for_each_entry_safe(reg_request, tmp, &tmp_reg_req_list, list) {
1936 REG_DBG_PRINT("Adding request for country %c%c back "
1937 "into the queue\n",
1938 reg_request->alpha2[0],
1939 reg_request->alpha2[1]);
1940 list_del(&reg_request->list);
1941 list_add_tail(&reg_request->list, &reg_requests_list);
1942 }
1943 spin_unlock(&reg_requests_lock);
1944
1945 mutex_unlock(&reg_mutex);
1946 mutex_unlock(&cfg80211_mutex);
1947
1948 REG_DBG_PRINT("Kicking the queue\n");
1949
1950 schedule_work(&reg_work);
1951}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001952
1953void regulatory_hint_disconnect(void)
1954{
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001955 REG_DBG_PRINT("All devices are disconnected, going to "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001956 "restore regulatory settings\n");
1957 restore_regulatory_settings(false);
1958}
1959
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001960static bool freq_is_chan_12_13_14(u16 freq)
1961{
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001962 if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
1963 freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
1964 freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001965 return true;
1966 return false;
1967}
1968
1969int regulatory_hint_found_beacon(struct wiphy *wiphy,
1970 struct ieee80211_channel *beacon_chan,
1971 gfp_t gfp)
1972{
1973 struct reg_beacon *reg_beacon;
1974
1975 if (likely((beacon_chan->beacon_found ||
1976 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1977 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1978 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1979 return 0;
1980
1981 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1982 if (!reg_beacon)
1983 return -ENOMEM;
1984
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001985 REG_DBG_PRINT("Found new beacon on "
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001986 "frequency: %d MHz (Ch %d) on %s\n",
1987 beacon_chan->center_freq,
1988 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1989 wiphy_name(wiphy));
1990
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001991 memcpy(&reg_beacon->chan, beacon_chan,
1992 sizeof(struct ieee80211_channel));
1993
1994
1995 /*
1996 * Since we can be called from BH or and non-BH context
1997 * we must use spin_lock_bh()
1998 */
1999 spin_lock_bh(&reg_pending_beacons_lock);
2000 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2001 spin_unlock_bh(&reg_pending_beacons_lock);
2002
2003 schedule_work(&reg_work);
2004
2005 return 0;
2006}
2007
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002008static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002009{
2010 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002011 const struct ieee80211_reg_rule *reg_rule = NULL;
2012 const struct ieee80211_freq_range *freq_range = NULL;
2013 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002014
Luis R. Rodriguez66533252011-12-20 12:23:37 -08002015 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002016
2017 for (i = 0; i < rd->n_reg_rules; i++) {
2018 reg_rule = &rd->reg_rules[i];
2019 freq_range = &reg_rule->freq_range;
2020 power_rule = &reg_rule->power_rule;
2021
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002022 /*
2023 * There may not be documentation for max antenna gain
2024 * in certain regions
2025 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002026 if (power_rule->max_antenna_gain)
Luis R. Rodriguez66533252011-12-20 12:23:37 -08002027 pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002028 freq_range->start_freq_khz,
2029 freq_range->end_freq_khz,
2030 freq_range->max_bandwidth_khz,
2031 power_rule->max_antenna_gain,
2032 power_rule->max_eirp);
2033 else
Luis R. Rodriguez66533252011-12-20 12:23:37 -08002034 pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002035 freq_range->start_freq_khz,
2036 freq_range->end_freq_khz,
2037 freq_range->max_bandwidth_khz,
2038 power_rule->max_eirp);
2039 }
2040}
2041
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002042bool reg_supported_dfs_region(u8 dfs_region)
2043{
2044 switch (dfs_region) {
2045 case NL80211_DFS_UNSET:
2046 case NL80211_DFS_FCC:
2047 case NL80211_DFS_ETSI:
2048 case NL80211_DFS_JP:
2049 return true;
2050 default:
2051 REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n",
2052 dfs_region);
2053 return false;
2054 }
2055}
2056
2057static void print_dfs_region(u8 dfs_region)
2058{
2059 if (!dfs_region)
2060 return;
2061
2062 switch (dfs_region) {
2063 case NL80211_DFS_FCC:
2064 pr_info(" DFS Master region FCC");
2065 break;
2066 case NL80211_DFS_ETSI:
2067 pr_info(" DFS Master region ETSI");
2068 break;
2069 case NL80211_DFS_JP:
2070 pr_info(" DFS Master region JP");
2071 break;
2072 default:
2073 pr_info(" DFS Master region Uknown");
2074 break;
2075 }
2076}
2077
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002078static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002079{
2080
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002081 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002082
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002083 if (last_request->initiator ==
2084 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002085 struct cfg80211_registered_device *rdev;
2086 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002087 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002088 if (rdev) {
Joe Perchese9c02682010-11-16 19:56:49 -08002089 pr_info("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002090 rdev->country_ie_alpha2[0],
2091 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002092 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002093 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002094 } else
Joe Perchese9c02682010-11-16 19:56:49 -08002095 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002096 } else if (is_world_regdom(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08002097 pr_info("World regulatory domain updated:\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002098 else {
2099 if (is_unknown_alpha2(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08002100 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002101 else
Joe Perchese9c02682010-11-16 19:56:49 -08002102 pr_info("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002103 rd->alpha2[0], rd->alpha2[1]);
2104 }
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07002105 print_dfs_region(rd->dfs_region);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002106 print_rd_rules(rd);
2107}
2108
Johannes Berg2df78162008-10-28 16:49:41 +01002109static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002110{
Joe Perchese9c02682010-11-16 19:56:49 -08002111 pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002112 print_rd_rules(rd);
2113}
2114
Johannes Bergd2372b32008-10-24 20:32:20 +02002115/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002116static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002117{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002118 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002119 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002120 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002121 /* Some basic sanity checks first */
2122
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002123 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002124 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002125 return -EINVAL;
2126 update_world_regdomain(rd);
2127 return 0;
2128 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002129
2130 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2131 !is_unknown_alpha2(rd->alpha2))
2132 return -EINVAL;
2133
Johannes Bergf6037d02008-10-21 11:01:33 +02002134 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002135 return -EINVAL;
2136
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002137 /*
2138 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002139 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002140 * and the pending request came in from a country IE
2141 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002142 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002143 /*
2144 * If someone else asked us to change the rd lets only bother
2145 * checking if the alpha2 changes if CRDA was already called
2146 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002147 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002148 return -EINVAL;
2149 }
2150
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002151 /*
2152 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002153 * and finally inform them of what we have done, in case they want
2154 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002155 * internal EEPROM data
2156 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002157
Johannes Bergf6037d02008-10-21 11:01:33 +02002158 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002159 return -EINVAL;
2160
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002161 if (!is_valid_rd(rd)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002162 pr_err("Invalid regulatory domain detected:\n");
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002163 print_regdomain_info(rd);
2164 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002165 }
2166
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002167 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez0bac71a2011-11-28 16:47:16 -05002168 if (!request_wiphy &&
2169 (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
2170 last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)) {
2171 schedule_delayed_work(&reg_timeout, 0);
Johannes Bergde3584b2011-11-21 10:44:00 +01002172 return -ENODEV;
2173 }
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002174
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002175 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002176 int r;
2177
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002178 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodrigueza0429942011-11-28 16:47:15 -05002179 reset_regdomains(false);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002180 cfg80211_regdomain = rd;
2181 return 0;
2182 }
2183
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002184 /*
2185 * For a driver hint, lets copy the regulatory domain the
2186 * driver wanted to the wiphy to deal with conflicts
2187 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002188
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002189 /*
2190 * Userspace could have sent two replies with only
2191 * one kernel request.
2192 */
2193 if (request_wiphy->regd)
2194 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002195
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002196 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002197 if (r)
2198 return r;
2199
Luis R. Rodrigueza0429942011-11-28 16:47:15 -05002200 reset_regdomains(false);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002201 cfg80211_regdomain = rd;
2202 return 0;
2203 }
2204
2205 /* Intersection requires a bit more work */
2206
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002207 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002208
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002209 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2210 if (!intersected_rd)
2211 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002212
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002213 /*
2214 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002215 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002216 * domain we keep it for its private use
2217 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002218 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002219 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002220 else
2221 kfree(rd);
2222
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002223 rd = NULL;
2224
Luis R. Rodrigueza0429942011-11-28 16:47:15 -05002225 reset_regdomains(false);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002226 cfg80211_regdomain = intersected_rd;
2227
2228 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002229 }
2230
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002231 if (!intersected_rd)
2232 return -EINVAL;
2233
Johannes Berg79c97e92009-07-07 03:56:12 +02002234 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002235
Johannes Berg79c97e92009-07-07 03:56:12 +02002236 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2237 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2238 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002239
2240 BUG_ON(intersected_rd == rd);
2241
2242 kfree(rd);
2243 rd = NULL;
2244
Luis R. Rodrigueza0429942011-11-28 16:47:15 -05002245 reset_regdomains(false);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002246 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002247
2248 return 0;
2249}
2250
2251
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002252/*
2253 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002254 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002255 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2256 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002257int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002258{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002259 int r;
2260
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002261 assert_cfg80211_lock();
2262
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002263 mutex_lock(&reg_mutex);
2264
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002265 /* Note that this doesn't update the wiphys, this is done below */
2266 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002267 if (r) {
2268 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002269 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002270 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002271 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002272
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002273 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002274 if (!last_request->intersect)
2275 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002276
2277 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002278 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002279
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002280 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002281
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002282 nl80211_send_reg_change_event(last_request);
2283
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002284 reg_set_request_processed();
2285
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002286 mutex_unlock(&reg_mutex);
2287
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002288 return r;
2289}
2290
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002291#ifdef CONFIG_HOTPLUG
2292int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
2293{
2294 if (last_request && !last_request->processed) {
2295 if (add_uevent_var(env, "COUNTRY=%c%c",
2296 last_request->alpha2[0],
2297 last_request->alpha2[1]))
2298 return -ENOMEM;
2299 }
2300
2301 return 0;
2302}
2303#else
2304int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
2305{
2306 return -ENODEV;
2307}
2308#endif /* CONFIG_HOTPLUG */
2309
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002310/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002311void reg_device_remove(struct wiphy *wiphy)
2312{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002313 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002314
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002315 assert_cfg80211_lock();
2316
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002317 mutex_lock(&reg_mutex);
2318
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002319 kfree(wiphy->regd);
2320
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002321 if (last_request)
2322 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002323
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002324 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002325 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002326
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002327 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002328 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002329out:
2330 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002331}
2332
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002333static void reg_timeout_work(struct work_struct *work)
2334{
2335 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, "
Joe Perches12c5ffb2011-07-29 14:51:25 -07002336 "restoring regulatory settings\n");
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002337 restore_regulatory_settings(true);
2338}
2339
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002340int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002341{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002342 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002343
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002344 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2345 if (IS_ERR(reg_pdev))
2346 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002347
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002348 reg_pdev->dev.type = &reg_device_type;
2349
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002350 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002351 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002352
Luis R. Rodriguez4c68b0f2012-03-23 07:23:31 -07002353 reg_regdb_size_check();
2354
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002355 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002356
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002357 user_alpha2[0] = '9';
2358 user_alpha2[1] = '7';
2359
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002360 /* We always try to get an update for the static regdomain */
2361 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002362 if (err) {
2363 if (err == -ENOMEM)
2364 return err;
2365 /*
2366 * N.B. kobject_uevent_env() can fail mainly for when we're out
2367 * memory which is handled and propagated appropriately above
2368 * but it can also fail during a netlink_broadcast() or during
2369 * early boot for call_usermodehelper(). For now treat these
2370 * errors as non-fatal.
2371 */
Joe Perchese9c02682010-11-16 19:56:49 -08002372 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002373#ifdef CONFIG_CFG80211_REG_DEBUG
2374 /* We want to find out exactly why when debugging */
2375 WARN_ON(err);
2376#endif
2377 }
Johannes Berg734366d2008-09-15 10:56:48 +02002378
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002379 /*
2380 * Finally, if the user set the module parameter treat it
2381 * as a user hint.
2382 */
2383 if (!is_world_regdom(ieee80211_regdom))
2384 regulatory_hint_user(ieee80211_regdom);
2385
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002386 return 0;
2387}
2388
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002389void /* __init_or_exit */ regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002390{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002391 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002392 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002393
2394 cancel_work_sync(&reg_work);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002395 cancel_delayed_work_sync(&reg_timeout);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002396
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002397 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002398 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002399
Luis R. Rodrigueza0429942011-11-28 16:47:15 -05002400 reset_regdomains(true);
Johannes Berg734366d2008-09-15 10:56:48 +02002401
Luis R. Rodriguez58ebacc2011-11-08 14:28:06 -08002402 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02002403
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002404 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002405
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002406 spin_lock_bh(&reg_pending_beacons_lock);
2407 if (!list_empty(&reg_pending_beacons)) {
2408 list_for_each_entry_safe(reg_beacon, btmp,
2409 &reg_pending_beacons, list) {
2410 list_del(&reg_beacon->list);
2411 kfree(reg_beacon);
2412 }
2413 }
2414 spin_unlock_bh(&reg_pending_beacons_lock);
2415
2416 if (!list_empty(&reg_beacon_list)) {
2417 list_for_each_entry_safe(reg_beacon, btmp,
2418 &reg_beacon_list, list) {
2419 list_del(&reg_beacon->list);
2420 kfree(reg_beacon);
2421 }
2422 }
2423
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002424 spin_lock(&reg_requests_lock);
2425 if (!list_empty(&reg_requests_list)) {
2426 list_for_each_entry_safe(reg_request, tmp,
2427 &reg_requests_list, list) {
2428 list_del(&reg_request->list);
2429 kfree(reg_request);
2430 }
2431 }
2432 spin_unlock(&reg_requests_lock);
2433
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002434 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002435 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002436}