blob: 72f7feee254ae14a41ef713576319b9b94c353a1 [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. Rodriguezb2e1b302008-09-09 23:19:48 -07005 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
Johannes Berg8318d782008-01-24 19:38:38 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012/**
13 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010014 *
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
19 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070020 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
23 *
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
29 *
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
33 *
Johannes Berg8318d782008-01-24 19:38:38 +010034 */
Joe Perchese9c02682010-11-16 19:56:49 -080035
36#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Johannes Berg8318d782008-01-24 19:38:38 +010038#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070040#include <linux/list.h>
41#include <linux/random.h>
John W. Linvillec61029c2010-08-05 14:26:24 -040042#include <linux/ctype.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070043#include <linux/nl80211.h>
44#include <linux/platform_device.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070045#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010046#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070047#include "reg.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050048#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040049#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010050
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050051#ifdef CONFIG_CFG80211_REG_DEBUG
John W. Linville82711952010-01-05 17:57:20 -050052#define REG_DBG_PRINT(format, args...) \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050053 do { \
Joe Perchese9c02682010-11-16 19:56:49 -080054 printk(KERN_DEBUG pr_fmt(format), ##args); \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050055 } while (0)
56#else
John W. Linville82711952010-01-05 17:57:20 -050057#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050058#endif
59
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -050060static struct regulatory_request core_request_world = {
61 .initiator = NL80211_REGDOM_SET_BY_CORE,
62 .alpha2[0] = '0',
63 .alpha2[1] = '0',
64 .intersect = false,
65 .processed = true,
66 .country_ie_env = ENVIRON_ANY,
67};
68
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070069/* Receipt of information from last regulatory request */
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -050070static struct regulatory_request *last_request = &core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +020071
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070072/* To trigger userspace events */
73static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010074
Scott James Remnant4d9d88d2011-03-08 10:45:30 -080075static struct device_type reg_device_type = {
76 .uevent = reg_device_uevent,
77};
78
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050079/*
80 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020081 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050082 * information to give us an alpha2
83 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080084const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020085
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050086/*
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070087 * Protects static reg.c components:
88 * - cfg80211_world_regdom
89 * - cfg80211_regdom
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070090 * - last_request
91 */
John W. Linville670b7f12010-06-24 11:26:31 -040092static DEFINE_MUTEX(reg_mutex);
Johannes Berg46a5eba2010-09-15 13:28:15 +020093
94static inline void assert_reg_lock(void)
95{
96 lockdep_assert_held(&reg_mutex);
97}
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070098
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050099/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500100static LIST_HEAD(reg_requests_list);
101static spinlock_t reg_requests_lock;
102
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500103/* Used to queue up beacon hints for review */
104static LIST_HEAD(reg_pending_beacons);
105static spinlock_t reg_pending_beacons_lock;
106
107/* Used to keep track of processed beacon hints */
108static LIST_HEAD(reg_beacon_list);
109
110struct reg_beacon {
111 struct list_head list;
112 struct ieee80211_channel chan;
113};
114
Luis R. Rodriguezf333a7a2010-11-17 21:46:07 -0800115static void reg_todo(struct work_struct *work);
116static DECLARE_WORK(reg_work, reg_todo);
117
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -0700118static void reg_timeout_work(struct work_struct *work);
119static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
120
Johannes Berg734366d2008-09-15 10:56:48 +0200121/* We keep a static world regulatory domain in case of the absence of CRDA */
122static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800123 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +0200124 .alpha2 = "00",
125 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500126 /* IEEE 802.11b/g, channels 1..11 */
127 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800128 /* IEEE 802.11b/g, channels 12..13. No HT40
129 * channel fits here. */
130 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500131 NL80211_RRF_PASSIVE_SCAN |
132 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800133 /* IEEE 802.11 channel 14 - Only JP enables
134 * this and for 802.11b only */
135 REG_RULE(2484-10, 2484+10, 20, 6, 20,
136 NL80211_RRF_PASSIVE_SCAN |
137 NL80211_RRF_NO_IBSS |
138 NL80211_RRF_NO_OFDM),
139 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800140 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800141 NL80211_RRF_PASSIVE_SCAN |
142 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500143
144 /* NB: 5260 MHz - 5700 MHz requies DFS */
145
146 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800147 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500148 NL80211_RRF_PASSIVE_SCAN |
149 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200150 }
151};
152
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200153static const struct ieee80211_regdomain *cfg80211_world_regdom =
154 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200155
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400156static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500157static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400158
Johannes Berg734366d2008-09-15 10:56:48 +0200159module_param(ieee80211_regdom, charp, 0444);
160MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
161
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -0500162static void reset_regdomains(bool full_reset)
Johannes Berg734366d2008-09-15 10:56:48 +0200163{
Johannes Berg942b25c2008-09-15 11:26:47 +0200164 /* avoid freeing static information or freeing something twice */
165 if (cfg80211_regdomain == cfg80211_world_regdom)
166 cfg80211_regdomain = NULL;
167 if (cfg80211_world_regdom == &world_regdom)
168 cfg80211_world_regdom = NULL;
169 if (cfg80211_regdomain == &world_regdom)
170 cfg80211_regdomain = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200171
172 kfree(cfg80211_regdomain);
173 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200174
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200175 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200176 cfg80211_regdomain = NULL;
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -0500177
178 if (!full_reset)
179 return;
180
181 if (last_request != &core_request_world)
182 kfree(last_request);
183 last_request = &core_request_world;
Johannes Berg734366d2008-09-15 10:56:48 +0200184}
185
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500186/*
187 * Dynamic world regulatory domain requested by the wireless
188 * core upon initialization
189 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200190static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200191{
Johannes Bergf6037d02008-10-21 11:01:33 +0200192 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200193
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -0500194 reset_regdomains(false);
Johannes Berg734366d2008-09-15 10:56:48 +0200195
196 cfg80211_world_regdom = rd;
197 cfg80211_regdomain = rd;
198}
Johannes Berg734366d2008-09-15 10:56:48 +0200199
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200200bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100201{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700202 if (!alpha2)
203 return false;
204 if (alpha2[0] == '0' && alpha2[1] == '0')
205 return true;
206 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100207}
208
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200209static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700210{
211 if (!alpha2)
212 return false;
213 if (alpha2[0] != 0 && alpha2[1] != 0)
214 return true;
215 return false;
216}
Johannes Berg8318d782008-01-24 19:38:38 +0100217
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200218static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700219{
220 if (!alpha2)
221 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500222 /*
223 * Special case where regulatory domain was built by driver
224 * but a specific alpha2 cannot be determined
225 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700226 if (alpha2[0] == '9' && alpha2[1] == '9')
227 return true;
228 return false;
229}
230
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800231static bool is_intersected_alpha2(const char *alpha2)
232{
233 if (!alpha2)
234 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500235 /*
236 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800237 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500238 * structures
239 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800240 if (alpha2[0] == '9' && alpha2[1] == '8')
241 return true;
242 return false;
243}
244
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200245static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700246{
247 if (!alpha2)
248 return false;
John W. Linvillec61029c2010-08-05 14:26:24 -0400249 if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700250 return true;
251 return false;
252}
253
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200254static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700255{
256 if (!alpha2_x || !alpha2_y)
257 return false;
258 if (alpha2_x[0] == alpha2_y[0] &&
259 alpha2_x[1] == alpha2_y[1])
260 return true;
261 return false;
262}
263
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500264static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700265{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500266 assert_cfg80211_lock();
267
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268 if (!cfg80211_regdomain)
269 return true;
270 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
271 return false;
272 return true;
273}
274
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500275/*
276 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
277 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
278 * has ever been issued.
279 */
280static bool is_user_regdom_saved(void)
281{
282 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
283 return false;
284
285 /* This would indicate a mistake on the design */
286 if (WARN((!is_world_regdom(user_alpha2) &&
287 !is_an_alpha2(user_alpha2)),
288 "Unexpected user alpha2: %c%c\n",
289 user_alpha2[0],
290 user_alpha2[1]))
291 return false;
292
293 return true;
294}
295
John W. Linville3b377ea2009-12-18 17:59:01 -0500296static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
297 const struct ieee80211_regdomain *src_regd)
298{
299 struct ieee80211_regdomain *regd;
300 int size_of_regd = 0;
301 unsigned int i;
302
303 size_of_regd = sizeof(struct ieee80211_regdomain) +
304 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
305
306 regd = kzalloc(size_of_regd, GFP_KERNEL);
307 if (!regd)
308 return -ENOMEM;
309
310 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
311
312 for (i = 0; i < src_regd->n_reg_rules; i++)
313 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
314 sizeof(struct ieee80211_reg_rule));
315
316 *dst_regd = regd;
317 return 0;
318}
319
320#ifdef CONFIG_CFG80211_INTERNAL_REGDB
321struct reg_regdb_search_request {
322 char alpha2[2];
323 struct list_head list;
324};
325
326static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400327static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500328
329static void reg_regdb_search(struct work_struct *work)
330{
331 struct reg_regdb_search_request *request;
332 const struct ieee80211_regdomain *curdom, *regdom;
333 int i, r;
334
John W. Linville368d06f2010-03-16 15:40:59 -0400335 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500336 while (!list_empty(&reg_regdb_search_list)) {
337 request = list_first_entry(&reg_regdb_search_list,
338 struct reg_regdb_search_request,
339 list);
340 list_del(&request->list);
341
342 for (i=0; i<reg_regdb_size; i++) {
343 curdom = reg_regdb[i];
344
345 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
346 r = reg_copy_regd(&regdom, curdom);
347 if (r)
348 break;
John W. Linville3b377ea2009-12-18 17:59:01 -0500349 mutex_lock(&cfg80211_mutex);
350 set_regdom(regdom);
351 mutex_unlock(&cfg80211_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500352 break;
353 }
354 }
355
356 kfree(request);
357 }
John W. Linville368d06f2010-03-16 15:40:59 -0400358 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500359}
360
361static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
362
363static void reg_regdb_query(const char *alpha2)
364{
365 struct reg_regdb_search_request *request;
366
367 if (!alpha2)
368 return;
369
370 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
371 if (!request)
372 return;
373
374 memcpy(request->alpha2, alpha2, 2);
375
John W. Linville368d06f2010-03-16 15:40:59 -0400376 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500377 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400378 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500379
380 schedule_work(&reg_regdb_work);
381}
382#else
383static inline void reg_regdb_query(const char *alpha2) {}
384#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
385
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500386/*
387 * This lets us keep regulatory code which is updated on a regulatory
Scott James Remnant4d9d88d2011-03-08 10:45:30 -0800388 * basis in userspace. Country information is filled in by
389 * reg_device_uevent
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500390 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700391static int call_crda(const char *alpha2)
392{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700393 if (!is_world_regdom((char *) alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -0800394 pr_info("Calling CRDA for country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700395 alpha2[0], alpha2[1]);
396 else
Joe Perchese9c02682010-11-16 19:56:49 -0800397 pr_info("Calling CRDA to update world regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700398
John W. Linville3b377ea2009-12-18 17:59:01 -0500399 /* query internal regulatory database (if it exists) */
400 reg_regdb_query(alpha2);
401
Scott James Remnant4d9d88d2011-03-08 10:45:30 -0800402 return kobject_uevent(&reg_pdev->dev.kobj, KOBJ_CHANGE);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700403}
404
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700405/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200406bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700407{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400408 assert_cfg80211_lock();
409
Johannes Bergf6037d02008-10-21 11:01:33 +0200410 if (!last_request)
411 return false;
412
413 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700414}
415
416/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200417static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700418{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200419 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700420 u32 freq_diff;
421
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800422 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700423 return false;
424
425 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
426 return false;
427
428 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
429
Roel Kluinbd05f282009-03-03 22:55:21 +0100430 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
431 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700432 return false;
433
434 return true;
435}
436
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200437static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700438{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200439 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700440 unsigned int i;
441
442 if (!rd->n_reg_rules)
443 return false;
444
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800445 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
446 return false;
447
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700448 for (i = 0; i < rd->n_reg_rules; i++) {
449 reg_rule = &rd->reg_rules[i];
450 if (!is_valid_reg_rule(reg_rule))
451 return false;
452 }
453
454 return true;
455}
456
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400457static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
458 u32 center_freq_khz,
459 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700460{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400461 u32 start_freq_khz, end_freq_khz;
462
463 start_freq_khz = center_freq_khz - (bw_khz/2);
464 end_freq_khz = center_freq_khz + (bw_khz/2);
465
466 if (start_freq_khz >= freq_range->start_freq_khz &&
467 end_freq_khz <= freq_range->end_freq_khz)
468 return true;
469
470 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700471}
472
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800473/**
474 * freq_in_rule_band - tells us if a frequency is in a frequency band
475 * @freq_range: frequency rule we want to query
476 * @freq_khz: frequency we are inquiring about
477 *
478 * This lets us know if a specific frequency rule is or is not relevant to
479 * a specific frequency's band. Bands are device specific and artificial
480 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
481 * safe for now to assume that a frequency rule should not be part of a
482 * frequency's band if the start freq or end freq are off by more than 2 GHz.
483 * This resolution can be lowered and should be considered as we add
484 * regulatory rule support for other "bands".
485 **/
486static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
487 u32 freq_khz)
488{
489#define ONE_GHZ_IN_KHZ 1000000
490 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
491 return true;
492 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
493 return true;
494 return false;
495#undef ONE_GHZ_IN_KHZ
496}
497
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500498/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500499 * Helper for regdom_intersect(), this does the real
500 * mathematical intersection fun
501 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700502static int reg_rules_intersect(
503 const struct ieee80211_reg_rule *rule1,
504 const struct ieee80211_reg_rule *rule2,
505 struct ieee80211_reg_rule *intersected_rule)
506{
507 const struct ieee80211_freq_range *freq_range1, *freq_range2;
508 struct ieee80211_freq_range *freq_range;
509 const struct ieee80211_power_rule *power_rule1, *power_rule2;
510 struct ieee80211_power_rule *power_rule;
511 u32 freq_diff;
512
513 freq_range1 = &rule1->freq_range;
514 freq_range2 = &rule2->freq_range;
515 freq_range = &intersected_rule->freq_range;
516
517 power_rule1 = &rule1->power_rule;
518 power_rule2 = &rule2->power_rule;
519 power_rule = &intersected_rule->power_rule;
520
521 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
522 freq_range2->start_freq_khz);
523 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
524 freq_range2->end_freq_khz);
525 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
526 freq_range2->max_bandwidth_khz);
527
528 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
529 if (freq_range->max_bandwidth_khz > freq_diff)
530 freq_range->max_bandwidth_khz = freq_diff;
531
532 power_rule->max_eirp = min(power_rule1->max_eirp,
533 power_rule2->max_eirp);
534 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
535 power_rule2->max_antenna_gain);
536
537 intersected_rule->flags = (rule1->flags | rule2->flags);
538
539 if (!is_valid_reg_rule(intersected_rule))
540 return -EINVAL;
541
542 return 0;
543}
544
545/**
546 * regdom_intersect - do the intersection between two regulatory domains
547 * @rd1: first regulatory domain
548 * @rd2: second regulatory domain
549 *
550 * Use this function to get the intersection between two regulatory domains.
551 * Once completed we will mark the alpha2 for the rd as intersected, "98",
552 * as no one single alpha2 can represent this regulatory domain.
553 *
554 * Returns a pointer to the regulatory domain structure which will hold the
555 * resulting intersection of rules between rd1 and rd2. We will
556 * kzalloc() this structure for you.
557 */
558static struct ieee80211_regdomain *regdom_intersect(
559 const struct ieee80211_regdomain *rd1,
560 const struct ieee80211_regdomain *rd2)
561{
562 int r, size_of_regd;
563 unsigned int x, y;
564 unsigned int num_rules = 0, rule_idx = 0;
565 const struct ieee80211_reg_rule *rule1, *rule2;
566 struct ieee80211_reg_rule *intersected_rule;
567 struct ieee80211_regdomain *rd;
568 /* This is just a dummy holder to help us count */
569 struct ieee80211_reg_rule irule;
570
571 /* Uses the stack temporarily for counter arithmetic */
572 intersected_rule = &irule;
573
574 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
575
576 if (!rd1 || !rd2)
577 return NULL;
578
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500579 /*
580 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700581 * build them. This is to so we can malloc() and free() a
582 * regdomain once. The reason we use reg_rules_intersect() here
583 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500584 * All rules that do check out OK are valid.
585 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700586
587 for (x = 0; x < rd1->n_reg_rules; x++) {
588 rule1 = &rd1->reg_rules[x];
589 for (y = 0; y < rd2->n_reg_rules; y++) {
590 rule2 = &rd2->reg_rules[y];
591 if (!reg_rules_intersect(rule1, rule2,
592 intersected_rule))
593 num_rules++;
594 memset(intersected_rule, 0,
595 sizeof(struct ieee80211_reg_rule));
596 }
597 }
598
599 if (!num_rules)
600 return NULL;
601
602 size_of_regd = sizeof(struct ieee80211_regdomain) +
603 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
604
605 rd = kzalloc(size_of_regd, GFP_KERNEL);
606 if (!rd)
607 return NULL;
608
609 for (x = 0; x < rd1->n_reg_rules; x++) {
610 rule1 = &rd1->reg_rules[x];
611 for (y = 0; y < rd2->n_reg_rules; y++) {
612 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500613 /*
614 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700615 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500616 * a memcpy()
617 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700618 intersected_rule = &rd->reg_rules[rule_idx];
619 r = reg_rules_intersect(rule1, rule2,
620 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500621 /*
622 * No need to memset here the intersected rule here as
623 * we're not using the stack anymore
624 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700625 if (r)
626 continue;
627 rule_idx++;
628 }
629 }
630
631 if (rule_idx != num_rules) {
632 kfree(rd);
633 return NULL;
634 }
635
636 rd->n_reg_rules = num_rules;
637 rd->alpha2[0] = '9';
638 rd->alpha2[1] = '8';
639
640 return rd;
641}
642
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500643/*
644 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
645 * want to just have the channel structure use these
646 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700647static u32 map_regdom_flags(u32 rd_flags)
648{
649 u32 channel_flags = 0;
650 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
651 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
652 if (rd_flags & NL80211_RRF_NO_IBSS)
653 channel_flags |= IEEE80211_CHAN_NO_IBSS;
654 if (rd_flags & NL80211_RRF_DFS)
655 channel_flags |= IEEE80211_CHAN_RADAR;
656 return channel_flags;
657}
658
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800659static int freq_reg_info_regd(struct wiphy *wiphy,
660 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400661 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800662 const struct ieee80211_reg_rule **reg_rule,
663 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100664{
665 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800666 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800667 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400668 bool bw_fits = false;
669
670 if (!desired_bw_khz)
671 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +0100672
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800673 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800674
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500675 /*
676 * Follow the driver's regulatory domain, if present, unless a country
677 * IE has been processed or a user wants to help complaince further
678 */
Luis R. Rodriguez2784fe92010-12-15 19:24:11 -0800679 if (!custom_regd &&
680 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400681 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800682 wiphy->regd)
683 regd = wiphy->regd;
684
685 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700686 return -EINVAL;
687
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800688 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700689 const struct ieee80211_reg_rule *rr;
690 const struct ieee80211_freq_range *fr = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700691
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800692 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700693 fr = &rr->freq_range;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800694
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500695 /*
696 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800697 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500698 * not overwrite it once found
699 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800700 if (!band_rule_found)
701 band_rule_found = freq_in_rule_band(fr, center_freq);
702
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400703 bw_fits = reg_does_bw_fit(fr,
704 center_freq,
705 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800706
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400707 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700708 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400709 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100710 }
711 }
712
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800713 if (!band_rule_found)
714 return -ERANGE;
715
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400716 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700717}
718
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400719int freq_reg_info(struct wiphy *wiphy,
720 u32 center_freq,
721 u32 desired_bw_khz,
722 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800723{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400724 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400725 return freq_reg_info_regd(wiphy,
726 center_freq,
727 desired_bw_khz,
728 reg_rule,
729 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800730}
John W. Linville4f366c52010-07-15 14:57:33 -0400731EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700732
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530733#ifdef CONFIG_CFG80211_REG_DEBUG
734static const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
735{
736 switch (initiator) {
737 case NL80211_REGDOM_SET_BY_CORE:
738 return "Set by core";
739 case NL80211_REGDOM_SET_BY_USER:
740 return "Set by user";
741 case NL80211_REGDOM_SET_BY_DRIVER:
742 return "Set by driver";
743 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
744 return "Set by country IE";
745 default:
746 WARN_ON(1);
747 return "Set by bug";
748 }
749}
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530750
751static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
752 u32 desired_bw_khz,
753 const struct ieee80211_reg_rule *reg_rule)
754{
755 const struct ieee80211_power_rule *power_rule;
756 const struct ieee80211_freq_range *freq_range;
757 char max_antenna_gain[32];
758
759 power_rule = &reg_rule->power_rule;
760 freq_range = &reg_rule->freq_range;
761
762 if (!power_rule->max_antenna_gain)
763 snprintf(max_antenna_gain, 32, "N/A");
764 else
765 snprintf(max_antenna_gain, 32, "%d", power_rule->max_antenna_gain);
766
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700767 REG_DBG_PRINT("Updating information on frequency %d MHz "
Bob Copelandff039c62010-12-29 17:09:02 -0500768 "for a %d MHz width channel with regulatory rule:\n",
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530769 chan->center_freq,
770 KHZ_TO_MHZ(desired_bw_khz));
771
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700772 REG_DBG_PRINT("%d KHz - %d KHz @ KHz), (%s mBi, %d mBm)\n",
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530773 freq_range->start_freq_khz,
774 freq_range->end_freq_khz,
775 max_antenna_gain,
776 power_rule->max_eirp);
777}
778#else
779static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
780 u32 desired_bw_khz,
781 const struct ieee80211_reg_rule *reg_rule)
782{
783 return;
784}
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530785#endif
786
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400787/*
788 * Note that right now we assume the desired channel bandwidth
789 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
790 * per channel, the primary and the extension channel). To support
791 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
792 * new ieee80211_channel.target_bw and re run the regulatory check
793 * on the wiphy with the target_bw specified. Then we can simply use
794 * that below for the desired_bw_khz below.
795 */
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700796static void handle_channel(struct wiphy *wiphy,
797 enum nl80211_reg_initiator initiator,
798 enum ieee80211_band band,
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800799 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700800{
801 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400802 u32 flags, bw_flags = 0;
803 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700804 const struct ieee80211_reg_rule *reg_rule = NULL;
805 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400806 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800807 struct ieee80211_supported_band *sband;
808 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500809 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800810
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500811 assert_cfg80211_lock();
812
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500813 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
814
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800815 sband = wiphy->bands[band];
816 BUG_ON(chan_idx >= sband->n_channels);
817 chan = &sband->channels[chan_idx];
818
819 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700820
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400821 r = freq_reg_info(wiphy,
822 MHZ_TO_KHZ(chan->center_freq),
823 desired_bw_khz,
824 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700825
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700826 if (r) {
827 /*
828 * We will disable all channels that do not match our
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300829 * received regulatory rule unless the hint is coming
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700830 * from a Country IE and the Country IE had no information
831 * about a band. The IEEE 802.11 spec allows for an AP
832 * to send only a subset of the regulatory rules allowed,
833 * so an AP in the US that only supports 2.4 GHz may only send
834 * a country IE with information for the 2.4 GHz band
835 * while 5 GHz is still supported.
836 */
837 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
838 r == -ERANGE)
839 return;
840
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700841 REG_DBG_PRINT("Disabling freq %d MHz\n", chan->center_freq);
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700842 chan->flags = IEEE80211_CHAN_DISABLED;
Johannes Berg8318d782008-01-24 19:38:38 +0100843 return;
Luis R. Rodriguezca4ffe82010-10-20 10:18:55 -0700844 }
Johannes Berg8318d782008-01-24 19:38:38 +0100845
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +0530846 chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
847
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700848 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400849 freq_range = &reg_rule->freq_range;
850
851 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
852 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700853
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400854 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500855 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +0100856 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500857 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300858 * This guarantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800859 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500860 * settings
861 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800862 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400863 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800864 chan->max_antenna_gain = chan->orig_mag =
865 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800866 chan->max_power = chan->orig_mpwr =
867 (int) MBM_TO_DBM(power_rule->max_eirp);
868 return;
869 }
870
Rajkumar Manoharancb49a342011-09-14 14:28:17 +0530871 chan->beacon_found = false;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400872 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100873 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700874 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -0400875 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700876 chan->max_power = min(chan->orig_mpwr,
877 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400878 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700879 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100880}
881
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700882static void handle_band(struct wiphy *wiphy,
883 enum ieee80211_band band,
884 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +0100885{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800886 unsigned int i;
887 struct ieee80211_supported_band *sband;
888
889 BUG_ON(!wiphy->bands[band]);
890 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +0100891
892 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -0700893 handle_channel(wiphy, initiator, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +0100894}
895
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400896static bool ignore_reg_update(struct wiphy *wiphy,
897 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800898{
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530899 if (!last_request) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700900 REG_DBG_PRINT("Ignoring regulatory request %s since "
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530901 "last_request is not set\n",
902 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800903 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530904 }
905
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400906 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530907 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700908 REG_DBG_PRINT("Ignoring regulatory request %s "
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530909 "since the driver uses its own custom "
910 "regulatory domain ",
911 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800912 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530913 }
914
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500915 /*
916 * wiphy->regd will be set once the device has its own
917 * desired regulatory domain set
918 */
Johannes Berg5be83de2009-11-19 00:56:28 +0100919 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguez749b5272010-10-20 10:18:54 -0700920 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530921 !is_world_regdom(last_request->alpha2)) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -0700922 REG_DBG_PRINT("Ignoring regulatory request %s "
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530923 "since the driver requires its own regulaotry "
924 "domain to be set first",
925 reg_initiator_name(initiator));
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800926 return true;
Luis R. Rodriguez926a0a02010-10-21 19:17:03 +0530927 }
928
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800929 return false;
930}
931
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400932static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700933{
Johannes Berg79c97e92009-07-07 03:56:12 +0200934 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700935
Johannes Berg79c97e92009-07-07 03:56:12 +0200936 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
937 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700938}
939
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500940static void handle_reg_beacon(struct wiphy *wiphy,
941 unsigned int chan_idx,
942 struct reg_beacon *reg_beacon)
943{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500944 struct ieee80211_supported_band *sband;
945 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400946 bool channel_changed = false;
947 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500948
949 assert_cfg80211_lock();
950
951 sband = wiphy->bands[reg_beacon->chan.band];
952 chan = &sband->channels[chan_idx];
953
954 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
955 return;
956
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400957 if (chan->beacon_found)
958 return;
959
960 chan->beacon_found = true;
961
Johannes Berg5be83de2009-11-19 00:56:28 +0100962 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -0700963 return;
964
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400965 chan_before.center_freq = chan->center_freq;
966 chan_before.flags = chan->flags;
967
Luis R. Rodriguez37184242009-07-30 17:43:48 -0700968 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500969 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400970 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500971 }
972
Luis R. Rodriguez37184242009-07-30 17:43:48 -0700973 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500974 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400975 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500976 }
977
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400978 if (channel_changed)
979 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500980}
981
982/*
983 * Called when a scan on a wiphy finds a beacon on
984 * new channel
985 */
986static void wiphy_update_new_beacon(struct wiphy *wiphy,
987 struct reg_beacon *reg_beacon)
988{
989 unsigned int i;
990 struct ieee80211_supported_band *sband;
991
992 assert_cfg80211_lock();
993
994 if (!wiphy->bands[reg_beacon->chan.band])
995 return;
996
997 sband = wiphy->bands[reg_beacon->chan.band];
998
999 for (i = 0; i < sband->n_channels; i++)
1000 handle_reg_beacon(wiphy, i, reg_beacon);
1001}
1002
1003/*
1004 * Called upon reg changes or a new wiphy is added
1005 */
1006static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1007{
1008 unsigned int i;
1009 struct ieee80211_supported_band *sband;
1010 struct reg_beacon *reg_beacon;
1011
1012 assert_cfg80211_lock();
1013
1014 if (list_empty(&reg_beacon_list))
1015 return;
1016
1017 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1018 if (!wiphy->bands[reg_beacon->chan.band])
1019 continue;
1020 sband = wiphy->bands[reg_beacon->chan.band];
1021 for (i = 0; i < sband->n_channels; i++)
1022 handle_reg_beacon(wiphy, i, reg_beacon);
1023 }
1024}
1025
1026static bool reg_is_world_roaming(struct wiphy *wiphy)
1027{
1028 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1029 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1030 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001031 if (last_request &&
1032 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001033 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001034 return true;
1035 return false;
1036}
1037
1038/* Reap the advantages of previously found beacons */
1039static void reg_process_beacons(struct wiphy *wiphy)
1040{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001041 /*
1042 * Means we are just firing up cfg80211, so no beacons would
1043 * have been processed yet.
1044 */
1045 if (!last_request)
1046 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001047 if (!reg_is_world_roaming(wiphy))
1048 return;
1049 wiphy_update_beacon_reg(wiphy);
1050}
1051
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001052static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1053{
1054 if (!chan)
1055 return true;
1056 if (chan->flags & IEEE80211_CHAN_DISABLED)
1057 return true;
1058 /* This would happen when regulatory rules disallow HT40 completely */
1059 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1060 return true;
1061 return false;
1062}
1063
1064static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1065 enum ieee80211_band band,
1066 unsigned int chan_idx)
1067{
1068 struct ieee80211_supported_band *sband;
1069 struct ieee80211_channel *channel;
1070 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1071 unsigned int i;
1072
1073 assert_cfg80211_lock();
1074
1075 sband = wiphy->bands[band];
1076 BUG_ON(chan_idx >= sband->n_channels);
1077 channel = &sband->channels[chan_idx];
1078
1079 if (is_ht40_not_allowed(channel)) {
1080 channel->flags |= IEEE80211_CHAN_NO_HT40;
1081 return;
1082 }
1083
1084 /*
1085 * We need to ensure the extension channels exist to
1086 * be able to use HT40- or HT40+, this finds them (or not)
1087 */
1088 for (i = 0; i < sband->n_channels; i++) {
1089 struct ieee80211_channel *c = &sband->channels[i];
1090 if (c->center_freq == (channel->center_freq - 20))
1091 channel_before = c;
1092 if (c->center_freq == (channel->center_freq + 20))
1093 channel_after = c;
1094 }
1095
1096 /*
1097 * Please note that this assumes target bandwidth is 20 MHz,
1098 * if that ever changes we also need to change the below logic
1099 * to include that as well.
1100 */
1101 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001102 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001103 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001104 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001105
1106 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001107 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001108 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001109 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001110}
1111
1112static void reg_process_ht_flags_band(struct wiphy *wiphy,
1113 enum ieee80211_band band)
1114{
1115 unsigned int i;
1116 struct ieee80211_supported_band *sband;
1117
1118 BUG_ON(!wiphy->bands[band]);
1119 sband = wiphy->bands[band];
1120
1121 for (i = 0; i < sband->n_channels; i++)
1122 reg_process_ht_flags_channel(wiphy, band, i);
1123}
1124
1125static void reg_process_ht_flags(struct wiphy *wiphy)
1126{
1127 enum ieee80211_band band;
1128
1129 if (!wiphy)
1130 return;
1131
1132 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1133 if (wiphy->bands[band])
1134 reg_process_ht_flags_band(wiphy, band);
1135 }
1136
1137}
1138
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001139void wiphy_update_regulatory(struct wiphy *wiphy,
1140 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001141{
1142 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001143
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001144 if (ignore_reg_update(wiphy, initiator))
Sven Neumannea530db2011-07-12 15:52:07 +02001145 return;
1146
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001147 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001148 if (wiphy->bands[band])
Luis R. Rodriguez7ca43d02010-10-20 10:18:53 -07001149 handle_band(wiphy, band, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001150 }
Sven Neumannea530db2011-07-12 15:52:07 +02001151
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001152 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001153 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001154 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001155 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001156}
1157
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001158static void handle_channel_custom(struct wiphy *wiphy,
1159 enum ieee80211_band band,
1160 unsigned int chan_idx,
1161 const struct ieee80211_regdomain *regd)
1162{
1163 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001164 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1165 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001166 const struct ieee80211_reg_rule *reg_rule = NULL;
1167 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001168 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001169 struct ieee80211_supported_band *sband;
1170 struct ieee80211_channel *chan;
1171
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001172 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001173
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001174 sband = wiphy->bands[band];
1175 BUG_ON(chan_idx >= sband->n_channels);
1176 chan = &sband->channels[chan_idx];
1177
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001178 r = freq_reg_info_regd(wiphy,
1179 MHZ_TO_KHZ(chan->center_freq),
1180 desired_bw_khz,
1181 &reg_rule,
1182 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001183
1184 if (r) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001185 REG_DBG_PRINT("Disabling freq %d MHz as custom "
Luis R. Rodrigueza6518532010-10-20 10:18:57 -07001186 "regd has no rule that fits a %d MHz "
1187 "wide channel\n",
1188 chan->center_freq,
1189 KHZ_TO_MHZ(desired_bw_khz));
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001190 chan->flags = IEEE80211_CHAN_DISABLED;
1191 return;
1192 }
1193
Luis R. Rodrigueze702d3c2010-10-21 19:17:04 +05301194 chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
1195
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001196 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001197 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001198
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001199 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1200 bw_flags = IEEE80211_CHAN_NO_HT40;
1201
1202 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001203 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001204 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1205}
1206
1207static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1208 const struct ieee80211_regdomain *regd)
1209{
1210 unsigned int i;
1211 struct ieee80211_supported_band *sband;
1212
1213 BUG_ON(!wiphy->bands[band]);
1214 sband = wiphy->bands[band];
1215
1216 for (i = 0; i < sband->n_channels; i++)
1217 handle_channel_custom(wiphy, band, i, regd);
1218}
1219
1220/* Used by drivers prior to wiphy registration */
1221void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1222 const struct ieee80211_regdomain *regd)
1223{
1224 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001225 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001226
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001227 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001228 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001229 if (!wiphy->bands[band])
1230 continue;
1231 handle_band_custom(wiphy, band, regd);
1232 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001233 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001234 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001235
1236 /*
1237 * no point in calling this if it won't have any effect
1238 * on your device's supportd bands.
1239 */
1240 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001241}
1242EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1243
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001244/*
1245 * Return value which can be used by ignore_request() to indicate
1246 * it has been determined we should intersect two regulatory domains
1247 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001248#define REG_INTERSECT 1
1249
Johannes Berg84fa4f42008-10-24 20:32:23 +02001250/* This has the logic which determines when a new request
1251 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001252static int ignore_request(struct wiphy *wiphy,
1253 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001254{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001255 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001256
1257 assert_cfg80211_lock();
1258
Johannes Berg84fa4f42008-10-24 20:32:23 +02001259 /* All initial requests are respected */
1260 if (!last_request)
1261 return 0;
1262
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001263 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001264 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001265 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001266 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001267
1268 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1269
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001270 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001271 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001272 if (last_request->initiator ==
1273 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001274 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001275 /*
1276 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001277 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001278 * intersect them, but that seems unlikely
1279 * to be correct. Reject second one for now.
1280 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001281 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001282 return -EOPNOTSUPP;
1283 return -EALREADY;
1284 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001285 /*
1286 * Two consecutive Country IE hints on the same wiphy.
1287 * This should be picked up early by the driver/stack
1288 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001289 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001290 return 0;
1291 return -EALREADY;
1292 }
Luis R. Rodrigueza171fba2010-10-18 17:44:51 -07001293 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001294 case NL80211_REGDOM_SET_BY_DRIVER:
1295 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001296 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001297 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001298 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001299 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001300
1301 /*
1302 * This would happen if you unplug and plug your card
1303 * back in or if you add a new device for which the previously
1304 * loaded card also agrees on the regulatory domain.
1305 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001306 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001307 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001308 return -EALREADY;
1309
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001310 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001311 case NL80211_REGDOM_SET_BY_USER:
1312 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001313 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001314 /*
1315 * If the user knows better the user should set the regdom
1316 * to their country before the IE is picked up
1317 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001318 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001319 last_request->intersect)
1320 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001321 /*
1322 * Process user requests only after previous user/driver/core
1323 * requests have been processed
1324 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001325 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1326 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1327 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001328 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001329 return -EAGAIN;
1330 }
1331
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001332 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001333 return -EALREADY;
1334
Johannes Berg84fa4f42008-10-24 20:32:23 +02001335 return 0;
1336 }
1337
1338 return -EINVAL;
1339}
1340
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001341static void reg_set_request_processed(void)
1342{
1343 bool need_more_processing = false;
1344
1345 last_request->processed = true;
1346
1347 spin_lock(&reg_requests_lock);
1348 if (!list_empty(&reg_requests_list))
1349 need_more_processing = true;
1350 spin_unlock(&reg_requests_lock);
1351
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001352 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER)
1353 cancel_delayed_work_sync(&reg_timeout);
1354
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001355 if (need_more_processing)
1356 schedule_work(&reg_work);
1357}
1358
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001359/**
1360 * __regulatory_hint - hint to the wireless core a regulatory domain
1361 * @wiphy: if the hint comes from country information from an AP, this
1362 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001363 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001364 *
1365 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001366 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001367 *
1368 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1369 * already been set or other standard error codes.
1370 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001371 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001372 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001373static int __regulatory_hint(struct wiphy *wiphy,
1374 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001375{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001376 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001377 int r = 0;
1378
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001379 assert_cfg80211_lock();
1380
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001381 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001382
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001383 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001384 if (pending_request->initiator ==
1385 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001386 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001387 if (r) {
1388 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001389 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001390 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001391 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001392 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001393 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001394 /*
1395 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001396 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001397 * wiphy
1398 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001399 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001400 pending_request->initiator ==
1401 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001402 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001403 if (r) {
1404 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001405 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001406 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001407 r = -EALREADY;
1408 goto new_request;
1409 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001410 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001411 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001412 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001413
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001414new_request:
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -05001415 if (last_request != &core_request_world)
1416 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001417
1418 last_request = pending_request;
1419 last_request->intersect = intersect;
1420
1421 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001422
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001423 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1424 user_alpha2[0] = last_request->alpha2[0];
1425 user_alpha2[1] = last_request->alpha2[1];
1426 }
1427
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001428 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001429 if (r < 0) {
1430 /*
1431 * Since CRDA will not be called in this case as we already
1432 * have applied the requested regulatory domain before we just
1433 * inform userspace we have processed the request
1434 */
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001435 if (r == -EALREADY) {
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001436 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001437 reg_set_request_processed();
1438 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001439 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001440 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001441
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001442 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001443}
1444
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001445/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001446static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001447{
1448 int r = 0;
1449 struct wiphy *wiphy = NULL;
Yuri Ershovc4c32292010-06-29 15:08:08 +04001450 enum nl80211_reg_initiator initiator = reg_request->initiator;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001451
1452 BUG_ON(!reg_request->alpha2);
1453
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001454 if (wiphy_idx_valid(reg_request->wiphy_idx))
1455 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1456
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001457 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001458 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001459 kfree(reg_request);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001460 return;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001461 }
1462
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001463 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001464 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001465 if (r == -EALREADY && wiphy &&
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001466 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Yuri Ershovc4c32292010-06-29 15:08:08 +04001467 wiphy_update_regulatory(wiphy, initiator);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001468 return;
1469 }
1470
1471 /*
1472 * We only time out user hints, given that they should be the only
1473 * source of bogus requests.
1474 */
Luis R. Rodriguezc989bb12011-04-25 18:35:48 -07001475 if (r != -EALREADY &&
1476 reg_request->initiator == NL80211_REGDOM_SET_BY_USER)
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07001477 schedule_delayed_work(&reg_timeout, msecs_to_jiffies(3142));
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001478}
1479
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001480/*
1481 * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
1482 * Regulatory hints come on a first come first serve basis and we
1483 * must process each one atomically.
1484 */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001485static void reg_process_pending_hints(void)
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001486{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001487 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001488
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001489 mutex_lock(&cfg80211_mutex);
1490 mutex_lock(&reg_mutex);
1491
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001492 /* When last_request->processed becomes true this will be rescheduled */
1493 if (last_request && !last_request->processed) {
1494 REG_DBG_PRINT("Pending regulatory request, waiting "
1495 "for it to be processed...");
1496 goto out;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001497 }
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001498
1499 spin_lock(&reg_requests_lock);
1500
1501 if (list_empty(&reg_requests_list)) {
1502 spin_unlock(&reg_requests_lock);
1503 goto out;
1504 }
1505
1506 reg_request = list_first_entry(&reg_requests_list,
1507 struct regulatory_request,
1508 list);
1509 list_del_init(&reg_request->list);
1510
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001511 spin_unlock(&reg_requests_lock);
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001512
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08001513 reg_process_hint(reg_request);
1514
1515out:
Luis R. Rodriguezb0e28802010-11-17 21:46:08 -08001516 mutex_unlock(&reg_mutex);
1517 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001518}
1519
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001520/* Processes beacon hints -- this has nothing to do with country IEs */
1521static void reg_process_pending_beacon_hints(void)
1522{
Johannes Berg79c97e92009-07-07 03:56:12 +02001523 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001524 struct reg_beacon *pending_beacon, *tmp;
1525
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001526 /*
1527 * No need to hold the reg_mutex here as we just touch wiphys
1528 * and do not read or access regulatory variables.
1529 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001530 mutex_lock(&cfg80211_mutex);
1531
1532 /* This goes through the _pending_ beacon list */
1533 spin_lock_bh(&reg_pending_beacons_lock);
1534
1535 if (list_empty(&reg_pending_beacons)) {
1536 spin_unlock_bh(&reg_pending_beacons_lock);
1537 goto out;
1538 }
1539
1540 list_for_each_entry_safe(pending_beacon, tmp,
1541 &reg_pending_beacons, list) {
1542
1543 list_del_init(&pending_beacon->list);
1544
1545 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001546 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1547 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001548
1549 /* Remembers the beacon hint for new wiphys or reg changes */
1550 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1551 }
1552
1553 spin_unlock_bh(&reg_pending_beacons_lock);
1554out:
1555 mutex_unlock(&cfg80211_mutex);
1556}
1557
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001558static void reg_todo(struct work_struct *work)
1559{
1560 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001561 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001562}
1563
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001564static void queue_regulatory_request(struct regulatory_request *request)
1565{
John W. Linvillec61029c2010-08-05 14:26:24 -04001566 if (isalpha(request->alpha2[0]))
1567 request->alpha2[0] = toupper(request->alpha2[0]);
1568 if (isalpha(request->alpha2[1]))
1569 request->alpha2[1] = toupper(request->alpha2[1]);
1570
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001571 spin_lock(&reg_requests_lock);
1572 list_add_tail(&request->list, &reg_requests_list);
1573 spin_unlock(&reg_requests_lock);
1574
1575 schedule_work(&reg_work);
1576}
1577
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001578/*
1579 * Core regulatory hint -- happens during cfg80211_init()
1580 * and when we restore regulatory settings.
1581 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001582static int regulatory_hint_core(const char *alpha2)
1583{
1584 struct regulatory_request *request;
1585
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001586 request = kzalloc(sizeof(struct regulatory_request),
1587 GFP_KERNEL);
1588 if (!request)
1589 return -ENOMEM;
1590
1591 request->alpha2[0] = alpha2[0];
1592 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001593 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001594
Luis R. Rodriguez31e99722010-11-17 21:46:06 -08001595 queue_regulatory_request(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001596
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001597 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001598}
1599
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001600/* User hints */
1601int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001602{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001603 struct regulatory_request *request;
1604
Johannes Bergbe3d4812008-10-24 20:32:21 +02001605 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001606
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001607 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1608 if (!request)
1609 return -ENOMEM;
1610
1611 request->wiphy_idx = WIPHY_IDX_STALE;
1612 request->alpha2[0] = alpha2[0];
1613 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001614 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001615
1616 queue_regulatory_request(request);
1617
1618 return 0;
1619}
1620
1621/* Driver hints */
1622int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1623{
1624 struct regulatory_request *request;
1625
1626 BUG_ON(!alpha2);
1627 BUG_ON(!wiphy);
1628
1629 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1630 if (!request)
1631 return -ENOMEM;
1632
1633 request->wiphy_idx = get_wiphy_idx(wiphy);
1634
1635 /* Must have registered wiphy first */
1636 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1637
1638 request->alpha2[0] = alpha2[0];
1639 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001640 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001641
1642 queue_regulatory_request(request);
1643
1644 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001645}
1646EXPORT_SYMBOL(regulatory_hint);
1647
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001648/*
1649 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1650 * therefore cannot iterate over the rdev list here.
1651 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001652void regulatory_hint_11d(struct wiphy *wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05001653 enum ieee80211_band band,
1654 u8 *country_ie,
1655 u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001656{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001657 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001658 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001659 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001660
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001661 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001662
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001663 if (unlikely(!last_request))
1664 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001665
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001666 /* IE len must be evenly divisible by 2 */
1667 if (country_ie_len & 0x01)
1668 goto out;
1669
1670 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1671 goto out;
1672
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001673 alpha2[0] = country_ie[0];
1674 alpha2[1] = country_ie[1];
1675
1676 if (country_ie[2] == 'I')
1677 env = ENVIRON_INDOOR;
1678 else if (country_ie[2] == 'O')
1679 env = ENVIRON_OUTDOOR;
1680
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001681 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001682 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001683 * We leave conflict resolution to the workqueue, where can hold
1684 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001685 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001686 if (likely(last_request->initiator ==
1687 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001688 wiphy_idx_valid(last_request->wiphy_idx)))
1689 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001690
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001691 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1692 if (!request)
Dan Carpenterf9f9b6e2010-07-22 13:26:50 +02001693 goto out;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001694
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001695 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04001696 request->alpha2[0] = alpha2[0];
1697 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001698 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001699 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001700
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001701 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001702
1703 queue_regulatory_request(request);
1704
1705 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001706
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001707out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001708 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001709}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001710
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001711static void restore_alpha2(char *alpha2, bool reset_user)
1712{
1713 /* indicates there is no alpha2 to consider for restoration */
1714 alpha2[0] = '9';
1715 alpha2[1] = '7';
1716
1717 /* The user setting has precedence over the module parameter */
1718 if (is_user_regdom_saved()) {
1719 /* Unless we're asked to ignore it and reset it */
1720 if (reset_user) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001721 REG_DBG_PRINT("Restoring regulatory settings "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001722 "including user preference\n");
1723 user_alpha2[0] = '9';
1724 user_alpha2[1] = '7';
1725
1726 /*
1727 * If we're ignoring user settings, we still need to
1728 * check the module parameter to ensure we put things
1729 * back as they were for a full restore.
1730 */
1731 if (!is_world_regdom(ieee80211_regdom)) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001732 REG_DBG_PRINT("Keeping preference on "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001733 "module parameter ieee80211_regdom: %c%c\n",
1734 ieee80211_regdom[0],
1735 ieee80211_regdom[1]);
1736 alpha2[0] = ieee80211_regdom[0];
1737 alpha2[1] = ieee80211_regdom[1];
1738 }
1739 } else {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001740 REG_DBG_PRINT("Restoring regulatory settings "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001741 "while preserving user preference for: %c%c\n",
1742 user_alpha2[0],
1743 user_alpha2[1]);
1744 alpha2[0] = user_alpha2[0];
1745 alpha2[1] = user_alpha2[1];
1746 }
1747 } else if (!is_world_regdom(ieee80211_regdom)) {
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001748 REG_DBG_PRINT("Keeping preference on "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001749 "module parameter ieee80211_regdom: %c%c\n",
1750 ieee80211_regdom[0],
1751 ieee80211_regdom[1]);
1752 alpha2[0] = ieee80211_regdom[0];
1753 alpha2[1] = ieee80211_regdom[1];
1754 } else
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001755 REG_DBG_PRINT("Restoring regulatory settings\n");
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001756}
1757
1758/*
1759 * Restoring regulatory settings involves ingoring any
1760 * possibly stale country IE information and user regulatory
1761 * settings if so desired, this includes any beacon hints
1762 * learned as we could have traveled outside to another country
1763 * after disconnection. To restore regulatory settings we do
1764 * exactly what we did at bootup:
1765 *
1766 * - send a core regulatory hint
1767 * - send a user regulatory hint if applicable
1768 *
1769 * Device drivers that send a regulatory hint for a specific country
1770 * keep their own regulatory domain on wiphy->regd so that does does
1771 * not need to be remembered.
1772 */
1773static void restore_regulatory_settings(bool reset_user)
1774{
1775 char alpha2[2];
Dmitry Shmidt35047202011-12-19 12:32:21 -08001776 char world_alpha2[2];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001777 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001778 struct regulatory_request *reg_request, *tmp;
1779 LIST_HEAD(tmp_reg_req_list);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001780
1781 mutex_lock(&cfg80211_mutex);
1782 mutex_lock(&reg_mutex);
1783
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -05001784 reset_regdomains(true);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001785 restore_alpha2(alpha2, reset_user);
1786
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001787 /*
1788 * If there's any pending requests we simply
1789 * stash them to a temporary pending queue and
1790 * add then after we've restored regulatory
1791 * settings.
1792 */
1793 spin_lock(&reg_requests_lock);
1794 if (!list_empty(&reg_requests_list)) {
1795 list_for_each_entry_safe(reg_request, tmp,
1796 &reg_requests_list, list) {
1797 if (reg_request->initiator !=
1798 NL80211_REGDOM_SET_BY_USER)
1799 continue;
1800 list_del(&reg_request->list);
1801 list_add_tail(&reg_request->list, &tmp_reg_req_list);
1802 }
1803 }
1804 spin_unlock(&reg_requests_lock);
1805
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001806 /* Clear beacon hints */
1807 spin_lock_bh(&reg_pending_beacons_lock);
1808 if (!list_empty(&reg_pending_beacons)) {
1809 list_for_each_entry_safe(reg_beacon, btmp,
1810 &reg_pending_beacons, list) {
1811 list_del(&reg_beacon->list);
1812 kfree(reg_beacon);
1813 }
1814 }
1815 spin_unlock_bh(&reg_pending_beacons_lock);
1816
1817 if (!list_empty(&reg_beacon_list)) {
1818 list_for_each_entry_safe(reg_beacon, btmp,
1819 &reg_beacon_list, list) {
1820 list_del(&reg_beacon->list);
1821 kfree(reg_beacon);
1822 }
1823 }
1824
1825 /* First restore to the basic regulatory settings */
1826 cfg80211_regdomain = cfg80211_world_regdom;
Dmitry Shmidt35047202011-12-19 12:32:21 -08001827 world_alpha2[0] = cfg80211_regdomain->alpha2[0];
1828 world_alpha2[1] = cfg80211_regdomain->alpha2[1];
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001829
1830 mutex_unlock(&reg_mutex);
1831 mutex_unlock(&cfg80211_mutex);
1832
Dmitry Shmidt35047202011-12-19 12:32:21 -08001833 regulatory_hint_core(world_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001834
1835 /*
1836 * This restores the ieee80211_regdom module parameter
1837 * preference or the last user requested regulatory
1838 * settings, user regulatory settings takes precedence.
1839 */
1840 if (is_an_alpha2(alpha2))
1841 regulatory_hint_user(user_alpha2);
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001842
Luis R. Rodriguez14609552011-04-05 10:49:03 -07001843 if (list_empty(&tmp_reg_req_list))
1844 return;
1845
1846 mutex_lock(&cfg80211_mutex);
1847 mutex_lock(&reg_mutex);
1848
1849 spin_lock(&reg_requests_lock);
1850 list_for_each_entry_safe(reg_request, tmp, &tmp_reg_req_list, list) {
1851 REG_DBG_PRINT("Adding request for country %c%c back "
1852 "into the queue\n",
1853 reg_request->alpha2[0],
1854 reg_request->alpha2[1]);
1855 list_del(&reg_request->list);
1856 list_add_tail(&reg_request->list, &reg_requests_list);
1857 }
1858 spin_unlock(&reg_requests_lock);
1859
1860 mutex_unlock(&reg_mutex);
1861 mutex_unlock(&cfg80211_mutex);
1862
1863 REG_DBG_PRINT("Kicking the queue\n");
1864
1865 schedule_work(&reg_work);
1866}
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001867
1868void regulatory_hint_disconnect(void)
1869{
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001870 REG_DBG_PRINT("All devices are disconnected, going to "
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001871 "restore regulatory settings\n");
1872 restore_regulatory_settings(false);
1873}
1874
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001875static bool freq_is_chan_12_13_14(u16 freq)
1876{
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001877 if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
1878 freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
1879 freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001880 return true;
1881 return false;
1882}
1883
1884int regulatory_hint_found_beacon(struct wiphy *wiphy,
1885 struct ieee80211_channel *beacon_chan,
1886 gfp_t gfp)
1887{
1888 struct reg_beacon *reg_beacon;
1889
1890 if (likely((beacon_chan->beacon_found ||
1891 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1892 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1893 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1894 return 0;
1895
1896 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1897 if (!reg_beacon)
1898 return -ENOMEM;
1899
Luis R. Rodriguezd91e41b2010-10-20 10:18:59 -07001900 REG_DBG_PRINT("Found new beacon on "
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001901 "frequency: %d MHz (Ch %d) on %s\n",
1902 beacon_chan->center_freq,
1903 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1904 wiphy_name(wiphy));
1905
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001906 memcpy(&reg_beacon->chan, beacon_chan,
1907 sizeof(struct ieee80211_channel));
1908
1909
1910 /*
1911 * Since we can be called from BH or and non-BH context
1912 * we must use spin_lock_bh()
1913 */
1914 spin_lock_bh(&reg_pending_beacons_lock);
1915 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1916 spin_unlock_bh(&reg_pending_beacons_lock);
1917
1918 schedule_work(&reg_work);
1919
1920 return 0;
1921}
1922
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001923static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001924{
1925 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001926 const struct ieee80211_reg_rule *reg_rule = NULL;
1927 const struct ieee80211_freq_range *freq_range = NULL;
1928 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001929
Joe Perchese9c02682010-11-16 19:56:49 -08001930 pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001931
1932 for (i = 0; i < rd->n_reg_rules; i++) {
1933 reg_rule = &rd->reg_rules[i];
1934 freq_range = &reg_rule->freq_range;
1935 power_rule = &reg_rule->power_rule;
1936
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001937 /*
1938 * There may not be documentation for max antenna gain
1939 * in certain regions
1940 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001941 if (power_rule->max_antenna_gain)
Joe Perchese9c02682010-11-16 19:56:49 -08001942 pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001943 freq_range->start_freq_khz,
1944 freq_range->end_freq_khz,
1945 freq_range->max_bandwidth_khz,
1946 power_rule->max_antenna_gain,
1947 power_rule->max_eirp);
1948 else
Joe Perchese9c02682010-11-16 19:56:49 -08001949 pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001950 freq_range->start_freq_khz,
1951 freq_range->end_freq_khz,
1952 freq_range->max_bandwidth_khz,
1953 power_rule->max_eirp);
1954 }
1955}
1956
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001957static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001958{
1959
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001960 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001961
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001962 if (last_request->initiator ==
1963 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001964 struct cfg80211_registered_device *rdev;
1965 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001966 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02001967 if (rdev) {
Joe Perchese9c02682010-11-16 19:56:49 -08001968 pr_info("Current regulatory domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02001969 rdev->country_ie_alpha2[0],
1970 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001971 } else
Joe Perchese9c02682010-11-16 19:56:49 -08001972 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001973 } else
Joe Perchese9c02682010-11-16 19:56:49 -08001974 pr_info("Current regulatory domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001975 } else if (is_world_regdom(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08001976 pr_info("World regulatory domain updated:\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001977 else {
1978 if (is_unknown_alpha2(rd->alpha2))
Joe Perchese9c02682010-11-16 19:56:49 -08001979 pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001980 else
Joe Perchese9c02682010-11-16 19:56:49 -08001981 pr_info("Regulatory domain changed to country: %c%c\n",
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001982 rd->alpha2[0], rd->alpha2[1]);
1983 }
1984 print_rd_rules(rd);
1985}
1986
Johannes Berg2df78162008-10-28 16:49:41 +01001987static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001988{
Joe Perchese9c02682010-11-16 19:56:49 -08001989 pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001990 print_rd_rules(rd);
1991}
1992
Johannes Bergd2372b32008-10-24 20:32:20 +02001993/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001994static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001995{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001996 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02001997 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001998 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001999 /* Some basic sanity checks first */
2000
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002001 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002002 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002003 return -EINVAL;
2004 update_world_regdomain(rd);
2005 return 0;
2006 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002007
2008 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2009 !is_unknown_alpha2(rd->alpha2))
2010 return -EINVAL;
2011
Johannes Bergf6037d02008-10-21 11:01:33 +02002012 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002013 return -EINVAL;
2014
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002015 /*
2016 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002017 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002018 * and the pending request came in from a country IE
2019 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002020 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002021 /*
2022 * If someone else asked us to change the rd lets only bother
2023 * checking if the alpha2 changes if CRDA was already called
2024 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002025 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002026 return -EINVAL;
2027 }
2028
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002029 /*
2030 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002031 * and finally inform them of what we have done, in case they want
2032 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002033 * internal EEPROM data
2034 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002035
Johannes Bergf6037d02008-10-21 11:01:33 +02002036 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002037 return -EINVAL;
2038
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002039 if (!is_valid_rd(rd)) {
Joe Perchese9c02682010-11-16 19:56:49 -08002040 pr_err("Invalid regulatory domain detected:\n");
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002041 print_regdomain_info(rd);
2042 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002043 }
2044
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002045 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez7e06614a2011-11-28 16:47:16 -05002046 if (!request_wiphy &&
2047 (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
2048 last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)) {
2049 schedule_delayed_work(&reg_timeout, 0);
Johannes Berg6cb4e0d2011-11-21 10:44:00 +01002050 return -ENODEV;
2051 }
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002052
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002053 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002054 int r;
2055
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002056 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -05002057 reset_regdomains(false);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002058 cfg80211_regdomain = rd;
2059 return 0;
2060 }
2061
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002062 /*
2063 * For a driver hint, lets copy the regulatory domain the
2064 * driver wanted to the wiphy to deal with conflicts
2065 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002066
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002067 /*
2068 * Userspace could have sent two replies with only
2069 * one kernel request.
2070 */
2071 if (request_wiphy->regd)
2072 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002073
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002074 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002075 if (r)
2076 return r;
2077
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -05002078 reset_regdomains(false);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002079 cfg80211_regdomain = rd;
2080 return 0;
2081 }
2082
2083 /* Intersection requires a bit more work */
2084
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002085 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002086
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002087 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2088 if (!intersected_rd)
2089 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002090
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002091 /*
2092 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002093 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002094 * domain we keep it for its private use
2095 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002096 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002097 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002098 else
2099 kfree(rd);
2100
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002101 rd = NULL;
2102
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -05002103 reset_regdomains(false);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002104 cfg80211_regdomain = intersected_rd;
2105
2106 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002107 }
2108
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002109 if (!intersected_rd)
2110 return -EINVAL;
2111
Johannes Berg79c97e92009-07-07 03:56:12 +02002112 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002113
Johannes Berg79c97e92009-07-07 03:56:12 +02002114 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2115 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2116 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002117
2118 BUG_ON(intersected_rd == rd);
2119
2120 kfree(rd);
2121 rd = NULL;
2122
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -05002123 reset_regdomains(false);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002124 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002125
2126 return 0;
2127}
2128
2129
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002130/*
2131 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002132 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002133 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2134 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002135int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002136{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002137 int r;
2138
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002139 assert_cfg80211_lock();
2140
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002141 mutex_lock(&reg_mutex);
2142
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002143 /* Note that this doesn't update the wiphys, this is done below */
2144 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002145 if (r) {
2146 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002147 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002148 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002149 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002150
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002151 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002152 if (!last_request->intersect)
2153 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002154
2155 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002156 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002157
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002158 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002159
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002160 nl80211_send_reg_change_event(last_request);
2161
Luis R. Rodriguezb2e253c2010-11-17 21:46:09 -08002162 reg_set_request_processed();
2163
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002164 mutex_unlock(&reg_mutex);
2165
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002166 return r;
2167}
2168
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002169#ifdef CONFIG_HOTPLUG
2170int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
2171{
2172 if (last_request && !last_request->processed) {
2173 if (add_uevent_var(env, "COUNTRY=%c%c",
2174 last_request->alpha2[0],
2175 last_request->alpha2[1]))
2176 return -ENOMEM;
2177 }
2178
2179 return 0;
2180}
2181#else
2182int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env)
2183{
2184 return -ENODEV;
2185}
2186#endif /* CONFIG_HOTPLUG */
2187
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002188/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002189void reg_device_remove(struct wiphy *wiphy)
2190{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002191 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002192
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002193 assert_cfg80211_lock();
2194
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002195 mutex_lock(&reg_mutex);
2196
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002197 kfree(wiphy->regd);
2198
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002199 if (last_request)
2200 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002201
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002202 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002203 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002204
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002205 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002206 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002207out:
2208 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002209}
2210
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002211static void reg_timeout_work(struct work_struct *work)
2212{
2213 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, "
2214 "restoring regulatory settings");
2215 restore_regulatory_settings(true);
2216}
2217
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002218int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002219{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002220 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002221
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002222 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2223 if (IS_ERR(reg_pdev))
2224 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002225
Scott James Remnant4d9d88d2011-03-08 10:45:30 -08002226 reg_pdev->dev.type = &reg_device_type;
2227
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002228 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002229 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002230
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002231 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002232
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002233 user_alpha2[0] = '9';
2234 user_alpha2[1] = '7';
2235
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002236 /* We always try to get an update for the static regdomain */
2237 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002238 if (err) {
2239 if (err == -ENOMEM)
2240 return err;
2241 /*
2242 * N.B. kobject_uevent_env() can fail mainly for when we're out
2243 * memory which is handled and propagated appropriately above
2244 * but it can also fail during a netlink_broadcast() or during
2245 * early boot for call_usermodehelper(). For now treat these
2246 * errors as non-fatal.
2247 */
Joe Perchese9c02682010-11-16 19:56:49 -08002248 pr_err("kobject_uevent_env() was unable to call CRDA during init\n");
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002249#ifdef CONFIG_CFG80211_REG_DEBUG
2250 /* We want to find out exactly why when debugging */
2251 WARN_ON(err);
2252#endif
2253 }
Johannes Berg734366d2008-09-15 10:56:48 +02002254
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002255 /*
2256 * Finally, if the user set the module parameter treat it
2257 * as a user hint.
2258 */
2259 if (!is_world_regdom(ieee80211_regdom))
2260 regulatory_hint_user(ieee80211_regdom);
2261
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002262 return 0;
2263}
2264
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002265void /* __init_or_exit */ regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002266{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002267 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002268 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002269
2270 cancel_work_sync(&reg_work);
Luis R. Rodrigueza90c7a32011-04-05 10:49:04 -07002271 cancel_delayed_work_sync(&reg_timeout);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002272
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002273 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002274 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002275
Luis R. Rodriguez3ed26be2011-11-28 16:47:15 -05002276 reset_regdomains(true);
Johannes Berg734366d2008-09-15 10:56:48 +02002277
Luis R. Rodriguez5c02e3a2011-11-08 14:28:06 -08002278 dev_set_uevent_suppress(&reg_pdev->dev, true);
Johannes Bergf6037d02008-10-21 11:01:33 +02002279
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002280 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002281
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002282 spin_lock_bh(&reg_pending_beacons_lock);
2283 if (!list_empty(&reg_pending_beacons)) {
2284 list_for_each_entry_safe(reg_beacon, btmp,
2285 &reg_pending_beacons, list) {
2286 list_del(&reg_beacon->list);
2287 kfree(reg_beacon);
2288 }
2289 }
2290 spin_unlock_bh(&reg_pending_beacons_lock);
2291
2292 if (!list_empty(&reg_beacon_list)) {
2293 list_for_each_entry_safe(reg_beacon, btmp,
2294 &reg_beacon_list, list) {
2295 list_del(&reg_beacon->list);
2296 kfree(reg_beacon);
2297 }
2298 }
2299
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002300 spin_lock(&reg_requests_lock);
2301 if (!list_empty(&reg_requests_list)) {
2302 list_for_each_entry_safe(reg_request, tmp,
2303 &reg_requests_list, list) {
2304 list_del(&reg_request->list);
2305 kfree(reg_request);
2306 }
2307 }
2308 spin_unlock(&reg_requests_lock);
2309
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002310 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002311 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002312}