blob: b0d9a08447c9f7656b03ff6dfcb2971537a74b9d [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 */
35#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070037#include <linux/list.h>
38#include <linux/random.h>
John W. Linvillec61029c2010-08-05 14:26:24 -040039#include <linux/ctype.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070040#include <linux/nl80211.h>
41#include <linux/platform_device.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070042#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010043#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070044#include "reg.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050045#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040046#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010047
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050048#ifdef CONFIG_CFG80211_REG_DEBUG
John W. Linville82711952010-01-05 17:57:20 -050049#define REG_DBG_PRINT(format, args...) \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050050 do { \
John W. Linville82711952010-01-05 17:57:20 -050051 printk(KERN_DEBUG format , ## args); \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050052 } while (0)
53#else
John W. Linville82711952010-01-05 17:57:20 -050054#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050055#endif
56
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070057/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020058static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020059
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070060/* To trigger userspace events */
61static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010062
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050063/*
64 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020065 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050066 * information to give us an alpha2
67 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080068const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020069
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050070/*
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070071 * Protects static reg.c components:
72 * - cfg80211_world_regdom
73 * - cfg80211_regdom
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070074 * - last_request
75 */
John W. Linville670b7f12010-06-24 11:26:31 -040076static DEFINE_MUTEX(reg_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070077#define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
78
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050079/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050080static LIST_HEAD(reg_requests_list);
81static spinlock_t reg_requests_lock;
82
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050083/* Used to queue up beacon hints for review */
84static LIST_HEAD(reg_pending_beacons);
85static spinlock_t reg_pending_beacons_lock;
86
87/* Used to keep track of processed beacon hints */
88static LIST_HEAD(reg_beacon_list);
89
90struct reg_beacon {
91 struct list_head list;
92 struct ieee80211_channel chan;
93};
94
Johannes Berg734366d2008-09-15 10:56:48 +020095/* We keep a static world regulatory domain in case of the absence of CRDA */
96static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080097 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +020098 .alpha2 = "00",
99 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500100 /* IEEE 802.11b/g, channels 1..11 */
101 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800102 /* IEEE 802.11b/g, channels 12..13. No HT40
103 * channel fits here. */
104 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500105 NL80211_RRF_PASSIVE_SCAN |
106 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800107 /* IEEE 802.11 channel 14 - Only JP enables
108 * this and for 802.11b only */
109 REG_RULE(2484-10, 2484+10, 20, 6, 20,
110 NL80211_RRF_PASSIVE_SCAN |
111 NL80211_RRF_NO_IBSS |
112 NL80211_RRF_NO_OFDM),
113 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800114 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800115 NL80211_RRF_PASSIVE_SCAN |
116 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500117
118 /* NB: 5260 MHz - 5700 MHz requies DFS */
119
120 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800121 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500122 NL80211_RRF_PASSIVE_SCAN |
123 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200124 }
125};
126
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200127static const struct ieee80211_regdomain *cfg80211_world_regdom =
128 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200129
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400130static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500131static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400132
Johannes Berg734366d2008-09-15 10:56:48 +0200133module_param(ieee80211_regdom, charp, 0444);
134MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
135
Johannes Berg734366d2008-09-15 10:56:48 +0200136static void reset_regdomains(void)
137{
Johannes Berg942b25c2008-09-15 11:26:47 +0200138 /* avoid freeing static information or freeing something twice */
139 if (cfg80211_regdomain == cfg80211_world_regdom)
140 cfg80211_regdomain = NULL;
141 if (cfg80211_world_regdom == &world_regdom)
142 cfg80211_world_regdom = NULL;
143 if (cfg80211_regdomain == &world_regdom)
144 cfg80211_regdomain = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200145
146 kfree(cfg80211_regdomain);
147 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200148
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200149 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200150 cfg80211_regdomain = NULL;
151}
152
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500153/*
154 * Dynamic world regulatory domain requested by the wireless
155 * core upon initialization
156 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200157static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200158{
Johannes Bergf6037d02008-10-21 11:01:33 +0200159 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200160
161 reset_regdomains();
162
163 cfg80211_world_regdom = rd;
164 cfg80211_regdomain = rd;
165}
Johannes Berg734366d2008-09-15 10:56:48 +0200166
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200167bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100168{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700169 if (!alpha2)
170 return false;
171 if (alpha2[0] == '0' && alpha2[1] == '0')
172 return true;
173 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100174}
175
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200176static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700177{
178 if (!alpha2)
179 return false;
180 if (alpha2[0] != 0 && alpha2[1] != 0)
181 return true;
182 return false;
183}
Johannes Berg8318d782008-01-24 19:38:38 +0100184
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200185static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700186{
187 if (!alpha2)
188 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500189 /*
190 * Special case where regulatory domain was built by driver
191 * but a specific alpha2 cannot be determined
192 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700193 if (alpha2[0] == '9' && alpha2[1] == '9')
194 return true;
195 return false;
196}
197
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800198static bool is_intersected_alpha2(const char *alpha2)
199{
200 if (!alpha2)
201 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500202 /*
203 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800204 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500205 * structures
206 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800207 if (alpha2[0] == '9' && alpha2[1] == '8')
208 return true;
209 return false;
210}
211
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200212static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700213{
214 if (!alpha2)
215 return false;
John W. Linvillec61029c2010-08-05 14:26:24 -0400216 if (isalpha(alpha2[0]) && isalpha(alpha2[1]))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700217 return true;
218 return false;
219}
220
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200221static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700222{
223 if (!alpha2_x || !alpha2_y)
224 return false;
225 if (alpha2_x[0] == alpha2_y[0] &&
226 alpha2_x[1] == alpha2_y[1])
227 return true;
228 return false;
229}
230
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500231static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700232{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500233 assert_cfg80211_lock();
234
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700235 if (!cfg80211_regdomain)
236 return true;
237 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
238 return false;
239 return true;
240}
241
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500242/*
243 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
244 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
245 * has ever been issued.
246 */
247static bool is_user_regdom_saved(void)
248{
249 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
250 return false;
251
252 /* This would indicate a mistake on the design */
253 if (WARN((!is_world_regdom(user_alpha2) &&
254 !is_an_alpha2(user_alpha2)),
255 "Unexpected user alpha2: %c%c\n",
256 user_alpha2[0],
257 user_alpha2[1]))
258 return false;
259
260 return true;
261}
262
John W. Linville3b377ea2009-12-18 17:59:01 -0500263static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
264 const struct ieee80211_regdomain *src_regd)
265{
266 struct ieee80211_regdomain *regd;
267 int size_of_regd = 0;
268 unsigned int i;
269
270 size_of_regd = sizeof(struct ieee80211_regdomain) +
271 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
272
273 regd = kzalloc(size_of_regd, GFP_KERNEL);
274 if (!regd)
275 return -ENOMEM;
276
277 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
278
279 for (i = 0; i < src_regd->n_reg_rules; i++)
280 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
281 sizeof(struct ieee80211_reg_rule));
282
283 *dst_regd = regd;
284 return 0;
285}
286
287#ifdef CONFIG_CFG80211_INTERNAL_REGDB
288struct reg_regdb_search_request {
289 char alpha2[2];
290 struct list_head list;
291};
292
293static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400294static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500295
296static void reg_regdb_search(struct work_struct *work)
297{
298 struct reg_regdb_search_request *request;
299 const struct ieee80211_regdomain *curdom, *regdom;
300 int i, r;
301
John W. Linville368d06f2010-03-16 15:40:59 -0400302 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500303 while (!list_empty(&reg_regdb_search_list)) {
304 request = list_first_entry(&reg_regdb_search_list,
305 struct reg_regdb_search_request,
306 list);
307 list_del(&request->list);
308
309 for (i=0; i<reg_regdb_size; i++) {
310 curdom = reg_regdb[i];
311
312 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
313 r = reg_copy_regd(&regdom, curdom);
314 if (r)
315 break;
John W. Linville3b377ea2009-12-18 17:59:01 -0500316 mutex_lock(&cfg80211_mutex);
317 set_regdom(regdom);
318 mutex_unlock(&cfg80211_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500319 break;
320 }
321 }
322
323 kfree(request);
324 }
John W. Linville368d06f2010-03-16 15:40:59 -0400325 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500326}
327
328static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
329
330static void reg_regdb_query(const char *alpha2)
331{
332 struct reg_regdb_search_request *request;
333
334 if (!alpha2)
335 return;
336
337 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
338 if (!request)
339 return;
340
341 memcpy(request->alpha2, alpha2, 2);
342
John W. Linville368d06f2010-03-16 15:40:59 -0400343 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500344 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400345 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500346
347 schedule_work(&reg_regdb_work);
348}
349#else
350static inline void reg_regdb_query(const char *alpha2) {}
351#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
352
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500353/*
354 * This lets us keep regulatory code which is updated on a regulatory
355 * basis in userspace.
356 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700357static int call_crda(const char *alpha2)
358{
359 char country_env[9 + 2] = "COUNTRY=";
360 char *envp[] = {
361 country_env,
362 NULL
363 };
364
365 if (!is_world_regdom((char *) alpha2))
366 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
367 alpha2[0], alpha2[1]);
368 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700369 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
370 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700371
John W. Linville3b377ea2009-12-18 17:59:01 -0500372 /* query internal regulatory database (if it exists) */
373 reg_regdb_query(alpha2);
374
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700375 country_env[8] = alpha2[0];
376 country_env[9] = alpha2[1];
377
378 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
379}
380
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700381/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200382bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700383{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400384 assert_cfg80211_lock();
385
Johannes Bergf6037d02008-10-21 11:01:33 +0200386 if (!last_request)
387 return false;
388
389 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700390}
391
392/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200393static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700394{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200395 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700396 u32 freq_diff;
397
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800398 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700399 return false;
400
401 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
402 return false;
403
404 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
405
Roel Kluinbd05f282009-03-03 22:55:21 +0100406 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
407 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700408 return false;
409
410 return true;
411}
412
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200413static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700414{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200415 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700416 unsigned int i;
417
418 if (!rd->n_reg_rules)
419 return false;
420
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800421 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
422 return false;
423
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700424 for (i = 0; i < rd->n_reg_rules; i++) {
425 reg_rule = &rd->reg_rules[i];
426 if (!is_valid_reg_rule(reg_rule))
427 return false;
428 }
429
430 return true;
431}
432
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400433static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
434 u32 center_freq_khz,
435 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700436{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400437 u32 start_freq_khz, end_freq_khz;
438
439 start_freq_khz = center_freq_khz - (bw_khz/2);
440 end_freq_khz = center_freq_khz + (bw_khz/2);
441
442 if (start_freq_khz >= freq_range->start_freq_khz &&
443 end_freq_khz <= freq_range->end_freq_khz)
444 return true;
445
446 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700447}
448
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800449/**
450 * freq_in_rule_band - tells us if a frequency is in a frequency band
451 * @freq_range: frequency rule we want to query
452 * @freq_khz: frequency we are inquiring about
453 *
454 * This lets us know if a specific frequency rule is or is not relevant to
455 * a specific frequency's band. Bands are device specific and artificial
456 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
457 * safe for now to assume that a frequency rule should not be part of a
458 * frequency's band if the start freq or end freq are off by more than 2 GHz.
459 * This resolution can be lowered and should be considered as we add
460 * regulatory rule support for other "bands".
461 **/
462static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
463 u32 freq_khz)
464{
465#define ONE_GHZ_IN_KHZ 1000000
466 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
467 return true;
468 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
469 return true;
470 return false;
471#undef ONE_GHZ_IN_KHZ
472}
473
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500474/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500475 * Helper for regdom_intersect(), this does the real
476 * mathematical intersection fun
477 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700478static int reg_rules_intersect(
479 const struct ieee80211_reg_rule *rule1,
480 const struct ieee80211_reg_rule *rule2,
481 struct ieee80211_reg_rule *intersected_rule)
482{
483 const struct ieee80211_freq_range *freq_range1, *freq_range2;
484 struct ieee80211_freq_range *freq_range;
485 const struct ieee80211_power_rule *power_rule1, *power_rule2;
486 struct ieee80211_power_rule *power_rule;
487 u32 freq_diff;
488
489 freq_range1 = &rule1->freq_range;
490 freq_range2 = &rule2->freq_range;
491 freq_range = &intersected_rule->freq_range;
492
493 power_rule1 = &rule1->power_rule;
494 power_rule2 = &rule2->power_rule;
495 power_rule = &intersected_rule->power_rule;
496
497 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
498 freq_range2->start_freq_khz);
499 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
500 freq_range2->end_freq_khz);
501 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
502 freq_range2->max_bandwidth_khz);
503
504 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
505 if (freq_range->max_bandwidth_khz > freq_diff)
506 freq_range->max_bandwidth_khz = freq_diff;
507
508 power_rule->max_eirp = min(power_rule1->max_eirp,
509 power_rule2->max_eirp);
510 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
511 power_rule2->max_antenna_gain);
512
513 intersected_rule->flags = (rule1->flags | rule2->flags);
514
515 if (!is_valid_reg_rule(intersected_rule))
516 return -EINVAL;
517
518 return 0;
519}
520
521/**
522 * regdom_intersect - do the intersection between two regulatory domains
523 * @rd1: first regulatory domain
524 * @rd2: second regulatory domain
525 *
526 * Use this function to get the intersection between two regulatory domains.
527 * Once completed we will mark the alpha2 for the rd as intersected, "98",
528 * as no one single alpha2 can represent this regulatory domain.
529 *
530 * Returns a pointer to the regulatory domain structure which will hold the
531 * resulting intersection of rules between rd1 and rd2. We will
532 * kzalloc() this structure for you.
533 */
534static struct ieee80211_regdomain *regdom_intersect(
535 const struct ieee80211_regdomain *rd1,
536 const struct ieee80211_regdomain *rd2)
537{
538 int r, size_of_regd;
539 unsigned int x, y;
540 unsigned int num_rules = 0, rule_idx = 0;
541 const struct ieee80211_reg_rule *rule1, *rule2;
542 struct ieee80211_reg_rule *intersected_rule;
543 struct ieee80211_regdomain *rd;
544 /* This is just a dummy holder to help us count */
545 struct ieee80211_reg_rule irule;
546
547 /* Uses the stack temporarily for counter arithmetic */
548 intersected_rule = &irule;
549
550 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
551
552 if (!rd1 || !rd2)
553 return NULL;
554
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500555 /*
556 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700557 * build them. This is to so we can malloc() and free() a
558 * regdomain once. The reason we use reg_rules_intersect() here
559 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500560 * All rules that do check out OK are valid.
561 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700562
563 for (x = 0; x < rd1->n_reg_rules; x++) {
564 rule1 = &rd1->reg_rules[x];
565 for (y = 0; y < rd2->n_reg_rules; y++) {
566 rule2 = &rd2->reg_rules[y];
567 if (!reg_rules_intersect(rule1, rule2,
568 intersected_rule))
569 num_rules++;
570 memset(intersected_rule, 0,
571 sizeof(struct ieee80211_reg_rule));
572 }
573 }
574
575 if (!num_rules)
576 return NULL;
577
578 size_of_regd = sizeof(struct ieee80211_regdomain) +
579 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
580
581 rd = kzalloc(size_of_regd, GFP_KERNEL);
582 if (!rd)
583 return NULL;
584
585 for (x = 0; x < rd1->n_reg_rules; x++) {
586 rule1 = &rd1->reg_rules[x];
587 for (y = 0; y < rd2->n_reg_rules; y++) {
588 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500589 /*
590 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700591 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500592 * a memcpy()
593 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700594 intersected_rule = &rd->reg_rules[rule_idx];
595 r = reg_rules_intersect(rule1, rule2,
596 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500597 /*
598 * No need to memset here the intersected rule here as
599 * we're not using the stack anymore
600 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700601 if (r)
602 continue;
603 rule_idx++;
604 }
605 }
606
607 if (rule_idx != num_rules) {
608 kfree(rd);
609 return NULL;
610 }
611
612 rd->n_reg_rules = num_rules;
613 rd->alpha2[0] = '9';
614 rd->alpha2[1] = '8';
615
616 return rd;
617}
618
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500619/*
620 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
621 * want to just have the channel structure use these
622 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700623static u32 map_regdom_flags(u32 rd_flags)
624{
625 u32 channel_flags = 0;
626 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
627 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
628 if (rd_flags & NL80211_RRF_NO_IBSS)
629 channel_flags |= IEEE80211_CHAN_NO_IBSS;
630 if (rd_flags & NL80211_RRF_DFS)
631 channel_flags |= IEEE80211_CHAN_RADAR;
632 return channel_flags;
633}
634
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800635static int freq_reg_info_regd(struct wiphy *wiphy,
636 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400637 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800638 const struct ieee80211_reg_rule **reg_rule,
639 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100640{
641 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800642 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800643 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400644 bool bw_fits = false;
645
646 if (!desired_bw_khz)
647 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +0100648
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800649 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800650
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500651 /*
652 * Follow the driver's regulatory domain, if present, unless a country
653 * IE has been processed or a user wants to help complaince further
654 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400655 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
656 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800657 wiphy->regd)
658 regd = wiphy->regd;
659
660 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700661 return -EINVAL;
662
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800663 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700664 const struct ieee80211_reg_rule *rr;
665 const struct ieee80211_freq_range *fr = NULL;
666 const struct ieee80211_power_rule *pr = NULL;
667
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800668 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700669 fr = &rr->freq_range;
670 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800671
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500672 /*
673 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800674 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500675 * not overwrite it once found
676 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800677 if (!band_rule_found)
678 band_rule_found = freq_in_rule_band(fr, center_freq);
679
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400680 bw_fits = reg_does_bw_fit(fr,
681 center_freq,
682 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800683
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400684 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700685 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400686 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100687 }
688 }
689
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800690 if (!band_rule_found)
691 return -ERANGE;
692
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400693 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700694}
695
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400696int freq_reg_info(struct wiphy *wiphy,
697 u32 center_freq,
698 u32 desired_bw_khz,
699 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800700{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400701 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400702 return freq_reg_info_regd(wiphy,
703 center_freq,
704 desired_bw_khz,
705 reg_rule,
706 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800707}
John W. Linville4f366c52010-07-15 14:57:33 -0400708EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700709
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400710/*
711 * Note that right now we assume the desired channel bandwidth
712 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
713 * per channel, the primary and the extension channel). To support
714 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
715 * new ieee80211_channel.target_bw and re run the regulatory check
716 * on the wiphy with the target_bw specified. Then we can simply use
717 * that below for the desired_bw_khz below.
718 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800719static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
720 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700721{
722 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400723 u32 flags, bw_flags = 0;
724 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700725 const struct ieee80211_reg_rule *reg_rule = NULL;
726 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400727 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800728 struct ieee80211_supported_band *sband;
729 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500730 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800731
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500732 assert_cfg80211_lock();
733
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500734 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
735
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800736 sband = wiphy->bands[band];
737 BUG_ON(chan_idx >= sband->n_channels);
738 chan = &sband->channels[chan_idx];
739
740 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700741
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400742 r = freq_reg_info(wiphy,
743 MHZ_TO_KHZ(chan->center_freq),
744 desired_bw_khz,
745 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700746
John W. Linville4f366c52010-07-15 14:57:33 -0400747 if (r)
Johannes Berg8318d782008-01-24 19:38:38 +0100748 return;
Johannes Berg8318d782008-01-24 19:38:38 +0100749
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700750 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400751 freq_range = &reg_rule->freq_range;
752
753 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
754 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700755
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400756 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500757 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +0100758 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500759 /*
760 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800761 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500762 * settings
763 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800764 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400765 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800766 chan->max_antenna_gain = chan->orig_mag =
767 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800768 chan->max_power = chan->orig_mpwr =
769 (int) MBM_TO_DBM(power_rule->max_eirp);
770 return;
771 }
772
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400773 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100774 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700775 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -0400776 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700777 chan->max_power = min(chan->orig_mpwr,
778 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400779 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700780 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100781}
782
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800783static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +0100784{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800785 unsigned int i;
786 struct ieee80211_supported_band *sband;
787
788 BUG_ON(!wiphy->bands[band]);
789 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +0100790
791 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800792 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +0100793}
794
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400795static bool ignore_reg_update(struct wiphy *wiphy,
796 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800797{
798 if (!last_request)
799 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400800 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Johannes Berg5be83de2009-11-19 00:56:28 +0100801 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800802 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500803 /*
804 * wiphy->regd will be set once the device has its own
805 * desired regulatory domain set
806 */
Johannes Berg5be83de2009-11-19 00:56:28 +0100807 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800808 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800809 return true;
810 return false;
811}
812
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400813static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700814{
Johannes Berg79c97e92009-07-07 03:56:12 +0200815 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700816
Johannes Berg79c97e92009-07-07 03:56:12 +0200817 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
818 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700819}
820
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500821static void handle_reg_beacon(struct wiphy *wiphy,
822 unsigned int chan_idx,
823 struct reg_beacon *reg_beacon)
824{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500825 struct ieee80211_supported_band *sband;
826 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400827 bool channel_changed = false;
828 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500829
830 assert_cfg80211_lock();
831
832 sband = wiphy->bands[reg_beacon->chan.band];
833 chan = &sband->channels[chan_idx];
834
835 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
836 return;
837
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400838 if (chan->beacon_found)
839 return;
840
841 chan->beacon_found = true;
842
Johannes Berg5be83de2009-11-19 00:56:28 +0100843 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -0700844 return;
845
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400846 chan_before.center_freq = chan->center_freq;
847 chan_before.flags = chan->flags;
848
Luis R. Rodriguez37184242009-07-30 17:43:48 -0700849 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500850 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400851 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500852 }
853
Luis R. Rodriguez37184242009-07-30 17:43:48 -0700854 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500855 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400856 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500857 }
858
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -0400859 if (channel_changed)
860 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500861}
862
863/*
864 * Called when a scan on a wiphy finds a beacon on
865 * new channel
866 */
867static void wiphy_update_new_beacon(struct wiphy *wiphy,
868 struct reg_beacon *reg_beacon)
869{
870 unsigned int i;
871 struct ieee80211_supported_band *sband;
872
873 assert_cfg80211_lock();
874
875 if (!wiphy->bands[reg_beacon->chan.band])
876 return;
877
878 sband = wiphy->bands[reg_beacon->chan.band];
879
880 for (i = 0; i < sband->n_channels; i++)
881 handle_reg_beacon(wiphy, i, reg_beacon);
882}
883
884/*
885 * Called upon reg changes or a new wiphy is added
886 */
887static void wiphy_update_beacon_reg(struct wiphy *wiphy)
888{
889 unsigned int i;
890 struct ieee80211_supported_band *sband;
891 struct reg_beacon *reg_beacon;
892
893 assert_cfg80211_lock();
894
895 if (list_empty(&reg_beacon_list))
896 return;
897
898 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
899 if (!wiphy->bands[reg_beacon->chan.band])
900 continue;
901 sband = wiphy->bands[reg_beacon->chan.band];
902 for (i = 0; i < sband->n_channels; i++)
903 handle_reg_beacon(wiphy, i, reg_beacon);
904 }
905}
906
907static bool reg_is_world_roaming(struct wiphy *wiphy)
908{
909 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
910 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
911 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -0400912 if (last_request &&
913 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +0100914 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500915 return true;
916 return false;
917}
918
919/* Reap the advantages of previously found beacons */
920static void reg_process_beacons(struct wiphy *wiphy)
921{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -0400922 /*
923 * Means we are just firing up cfg80211, so no beacons would
924 * have been processed yet.
925 */
926 if (!last_request)
927 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500928 if (!reg_is_world_roaming(wiphy))
929 return;
930 wiphy_update_beacon_reg(wiphy);
931}
932
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400933static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
934{
935 if (!chan)
936 return true;
937 if (chan->flags & IEEE80211_CHAN_DISABLED)
938 return true;
939 /* This would happen when regulatory rules disallow HT40 completely */
940 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
941 return true;
942 return false;
943}
944
945static void reg_process_ht_flags_channel(struct wiphy *wiphy,
946 enum ieee80211_band band,
947 unsigned int chan_idx)
948{
949 struct ieee80211_supported_band *sband;
950 struct ieee80211_channel *channel;
951 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
952 unsigned int i;
953
954 assert_cfg80211_lock();
955
956 sband = wiphy->bands[band];
957 BUG_ON(chan_idx >= sband->n_channels);
958 channel = &sband->channels[chan_idx];
959
960 if (is_ht40_not_allowed(channel)) {
961 channel->flags |= IEEE80211_CHAN_NO_HT40;
962 return;
963 }
964
965 /*
966 * We need to ensure the extension channels exist to
967 * be able to use HT40- or HT40+, this finds them (or not)
968 */
969 for (i = 0; i < sband->n_channels; i++) {
970 struct ieee80211_channel *c = &sband->channels[i];
971 if (c->center_freq == (channel->center_freq - 20))
972 channel_before = c;
973 if (c->center_freq == (channel->center_freq + 20))
974 channel_after = c;
975 }
976
977 /*
978 * Please note that this assumes target bandwidth is 20 MHz,
979 * if that ever changes we also need to change the below logic
980 * to include that as well.
981 */
982 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -0400983 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400984 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -0400985 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400986
987 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -0400988 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400989 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -0400990 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400991}
992
993static void reg_process_ht_flags_band(struct wiphy *wiphy,
994 enum ieee80211_band band)
995{
996 unsigned int i;
997 struct ieee80211_supported_band *sband;
998
999 BUG_ON(!wiphy->bands[band]);
1000 sband = wiphy->bands[band];
1001
1002 for (i = 0; i < sband->n_channels; i++)
1003 reg_process_ht_flags_channel(wiphy, band, i);
1004}
1005
1006static void reg_process_ht_flags(struct wiphy *wiphy)
1007{
1008 enum ieee80211_band band;
1009
1010 if (!wiphy)
1011 return;
1012
1013 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1014 if (wiphy->bands[band])
1015 reg_process_ht_flags_band(wiphy, band);
1016 }
1017
1018}
1019
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001020void wiphy_update_regulatory(struct wiphy *wiphy,
1021 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001022{
1023 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001024
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001025 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001026 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001027 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001028 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001029 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001030 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001031out:
1032 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001033 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001034 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001035 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001036}
1037
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001038static void handle_channel_custom(struct wiphy *wiphy,
1039 enum ieee80211_band band,
1040 unsigned int chan_idx,
1041 const struct ieee80211_regdomain *regd)
1042{
1043 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001044 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1045 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001046 const struct ieee80211_reg_rule *reg_rule = NULL;
1047 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001048 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001049 struct ieee80211_supported_band *sband;
1050 struct ieee80211_channel *chan;
1051
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001052 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001053
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001054 sband = wiphy->bands[band];
1055 BUG_ON(chan_idx >= sband->n_channels);
1056 chan = &sband->channels[chan_idx];
1057
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001058 r = freq_reg_info_regd(wiphy,
1059 MHZ_TO_KHZ(chan->center_freq),
1060 desired_bw_khz,
1061 &reg_rule,
1062 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001063
1064 if (r) {
1065 chan->flags = IEEE80211_CHAN_DISABLED;
1066 return;
1067 }
1068
1069 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001070 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001071
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001072 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1073 bw_flags = IEEE80211_CHAN_NO_HT40;
1074
1075 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001076 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001077 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1078}
1079
1080static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1081 const struct ieee80211_regdomain *regd)
1082{
1083 unsigned int i;
1084 struct ieee80211_supported_band *sband;
1085
1086 BUG_ON(!wiphy->bands[band]);
1087 sband = wiphy->bands[band];
1088
1089 for (i = 0; i < sband->n_channels; i++)
1090 handle_channel_custom(wiphy, band, i, regd);
1091}
1092
1093/* Used by drivers prior to wiphy registration */
1094void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1095 const struct ieee80211_regdomain *regd)
1096{
1097 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001098 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001099
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001100 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001101 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001102 if (!wiphy->bands[band])
1103 continue;
1104 handle_band_custom(wiphy, band, regd);
1105 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001106 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001107 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001108
1109 /*
1110 * no point in calling this if it won't have any effect
1111 * on your device's supportd bands.
1112 */
1113 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001114}
1115EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1116
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001117/*
1118 * Return value which can be used by ignore_request() to indicate
1119 * it has been determined we should intersect two regulatory domains
1120 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001121#define REG_INTERSECT 1
1122
Johannes Berg84fa4f42008-10-24 20:32:23 +02001123/* This has the logic which determines when a new request
1124 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001125static int ignore_request(struct wiphy *wiphy,
1126 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001127{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001128 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001129
1130 assert_cfg80211_lock();
1131
Johannes Berg84fa4f42008-10-24 20:32:23 +02001132 /* All initial requests are respected */
1133 if (!last_request)
1134 return 0;
1135
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001136 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001137 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001138 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001139 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001140
1141 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1142
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001143 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001144 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001145 if (last_request->initiator ==
1146 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001147 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001148 /*
1149 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001150 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001151 * intersect them, but that seems unlikely
1152 * to be correct. Reject second one for now.
1153 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001154 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001155 return -EOPNOTSUPP;
1156 return -EALREADY;
1157 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001158 /*
1159 * Two consecutive Country IE hints on the same wiphy.
1160 * This should be picked up early by the driver/stack
1161 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001162 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001163 return 0;
1164 return -EALREADY;
1165 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001166 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001167 case NL80211_REGDOM_SET_BY_DRIVER:
1168 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001169 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001170 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001171 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001172 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001173
1174 /*
1175 * This would happen if you unplug and plug your card
1176 * back in or if you add a new device for which the previously
1177 * loaded card also agrees on the regulatory domain.
1178 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001179 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001180 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001181 return -EALREADY;
1182
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001183 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001184 case NL80211_REGDOM_SET_BY_USER:
1185 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001186 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001187 /*
1188 * If the user knows better the user should set the regdom
1189 * to their country before the IE is picked up
1190 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001191 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001192 last_request->intersect)
1193 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001194 /*
1195 * Process user requests only after previous user/driver/core
1196 * requests have been processed
1197 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001198 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1199 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1200 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001201 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001202 return -EAGAIN;
1203 }
1204
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001205 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001206 return -EALREADY;
1207
Johannes Berg84fa4f42008-10-24 20:32:23 +02001208 return 0;
1209 }
1210
1211 return -EINVAL;
1212}
1213
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001214/**
1215 * __regulatory_hint - hint to the wireless core a regulatory domain
1216 * @wiphy: if the hint comes from country information from an AP, this
1217 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001218 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001219 *
1220 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001221 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001222 *
1223 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1224 * already been set or other standard error codes.
1225 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001226 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001227 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001228static int __regulatory_hint(struct wiphy *wiphy,
1229 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001230{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001231 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001232 int r = 0;
1233
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001234 assert_cfg80211_lock();
1235
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001236 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001237
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001238 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001239 if (pending_request->initiator ==
1240 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001241 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001242 if (r) {
1243 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001244 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001245 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001246 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001247 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001248 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001249 /*
1250 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001251 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001252 * wiphy
1253 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001254 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001255 pending_request->initiator ==
1256 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001257 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001258 if (r) {
1259 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001260 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001261 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001262 r = -EALREADY;
1263 goto new_request;
1264 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001265 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001266 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001267 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001268
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001269new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001270 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001271
1272 last_request = pending_request;
1273 last_request->intersect = intersect;
1274
1275 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001276
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001277 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1278 user_alpha2[0] = last_request->alpha2[0];
1279 user_alpha2[1] = last_request->alpha2[1];
1280 }
1281
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001282 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001283 if (r < 0) {
1284 /*
1285 * Since CRDA will not be called in this case as we already
1286 * have applied the requested regulatory domain before we just
1287 * inform userspace we have processed the request
1288 */
1289 if (r == -EALREADY)
1290 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001291 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001292 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001293
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001294 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001295}
1296
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001297/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001298static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001299{
1300 int r = 0;
1301 struct wiphy *wiphy = NULL;
Yuri Ershovc4c32292010-06-29 15:08:08 +04001302 enum nl80211_reg_initiator initiator = reg_request->initiator;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001303
1304 BUG_ON(!reg_request->alpha2);
1305
1306 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001307 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001308
1309 if (wiphy_idx_valid(reg_request->wiphy_idx))
1310 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1311
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001312 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001313 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001314 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001315 goto out;
1316 }
1317
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001318 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001319 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001320 if (r == -EALREADY && wiphy &&
1321 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
Yuri Ershovc4c32292010-06-29 15:08:08 +04001322 wiphy_update_regulatory(wiphy, initiator);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001323out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001324 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001325 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001326}
1327
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001328/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001329static void reg_process_pending_hints(void)
1330 {
1331 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001332
1333 spin_lock(&reg_requests_lock);
1334 while (!list_empty(&reg_requests_list)) {
1335 reg_request = list_first_entry(&reg_requests_list,
1336 struct regulatory_request,
1337 list);
1338 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001339
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001340 spin_unlock(&reg_requests_lock);
1341 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001342 spin_lock(&reg_requests_lock);
1343 }
1344 spin_unlock(&reg_requests_lock);
1345}
1346
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001347/* Processes beacon hints -- this has nothing to do with country IEs */
1348static void reg_process_pending_beacon_hints(void)
1349{
Johannes Berg79c97e92009-07-07 03:56:12 +02001350 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001351 struct reg_beacon *pending_beacon, *tmp;
1352
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001353 /*
1354 * No need to hold the reg_mutex here as we just touch wiphys
1355 * and do not read or access regulatory variables.
1356 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001357 mutex_lock(&cfg80211_mutex);
1358
1359 /* This goes through the _pending_ beacon list */
1360 spin_lock_bh(&reg_pending_beacons_lock);
1361
1362 if (list_empty(&reg_pending_beacons)) {
1363 spin_unlock_bh(&reg_pending_beacons_lock);
1364 goto out;
1365 }
1366
1367 list_for_each_entry_safe(pending_beacon, tmp,
1368 &reg_pending_beacons, list) {
1369
1370 list_del_init(&pending_beacon->list);
1371
1372 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001373 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1374 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001375
1376 /* Remembers the beacon hint for new wiphys or reg changes */
1377 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1378 }
1379
1380 spin_unlock_bh(&reg_pending_beacons_lock);
1381out:
1382 mutex_unlock(&cfg80211_mutex);
1383}
1384
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001385static void reg_todo(struct work_struct *work)
1386{
1387 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001388 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001389}
1390
1391static DECLARE_WORK(reg_work, reg_todo);
1392
1393static void queue_regulatory_request(struct regulatory_request *request)
1394{
John W. Linvillec61029c2010-08-05 14:26:24 -04001395 if (isalpha(request->alpha2[0]))
1396 request->alpha2[0] = toupper(request->alpha2[0]);
1397 if (isalpha(request->alpha2[1]))
1398 request->alpha2[1] = toupper(request->alpha2[1]);
1399
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001400 spin_lock(&reg_requests_lock);
1401 list_add_tail(&request->list, &reg_requests_list);
1402 spin_unlock(&reg_requests_lock);
1403
1404 schedule_work(&reg_work);
1405}
1406
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001407/*
1408 * Core regulatory hint -- happens during cfg80211_init()
1409 * and when we restore regulatory settings.
1410 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001411static int regulatory_hint_core(const char *alpha2)
1412{
1413 struct regulatory_request *request;
1414
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001415 kfree(last_request);
1416 last_request = NULL;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001417
1418 request = kzalloc(sizeof(struct regulatory_request),
1419 GFP_KERNEL);
1420 if (!request)
1421 return -ENOMEM;
1422
1423 request->alpha2[0] = alpha2[0];
1424 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001425 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001426
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001427 /*
1428 * This ensures last_request is populated once modules
1429 * come swinging in and calling regulatory hints and
1430 * wiphy_apply_custom_regulatory().
1431 */
Luis R. Rodrigueza2bff262010-01-29 19:58:56 -05001432 reg_process_hint(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001433
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001434 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001435}
1436
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001437/* User hints */
1438int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001439{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001440 struct regulatory_request *request;
1441
Johannes Bergbe3d4812008-10-24 20:32:21 +02001442 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001443
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001444 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1445 if (!request)
1446 return -ENOMEM;
1447
1448 request->wiphy_idx = WIPHY_IDX_STALE;
1449 request->alpha2[0] = alpha2[0];
1450 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001451 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001452
1453 queue_regulatory_request(request);
1454
1455 return 0;
1456}
1457
1458/* Driver hints */
1459int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1460{
1461 struct regulatory_request *request;
1462
1463 BUG_ON(!alpha2);
1464 BUG_ON(!wiphy);
1465
1466 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1467 if (!request)
1468 return -ENOMEM;
1469
1470 request->wiphy_idx = get_wiphy_idx(wiphy);
1471
1472 /* Must have registered wiphy first */
1473 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1474
1475 request->alpha2[0] = alpha2[0];
1476 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001477 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001478
1479 queue_regulatory_request(request);
1480
1481 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001482}
1483EXPORT_SYMBOL(regulatory_hint);
1484
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001485/*
1486 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1487 * therefore cannot iterate over the rdev list here.
1488 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001489void regulatory_hint_11d(struct wiphy *wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05001490 enum ieee80211_band band,
1491 u8 *country_ie,
1492 u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001493{
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001494 char alpha2[2];
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001495 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001496 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001497
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001498 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001499
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001500 if (unlikely(!last_request))
1501 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001502
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001503 /* IE len must be evenly divisible by 2 */
1504 if (country_ie_len & 0x01)
1505 goto out;
1506
1507 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1508 goto out;
1509
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001510 alpha2[0] = country_ie[0];
1511 alpha2[1] = country_ie[1];
1512
1513 if (country_ie[2] == 'I')
1514 env = ENVIRON_INDOOR;
1515 else if (country_ie[2] == 'O')
1516 env = ENVIRON_OUTDOOR;
1517
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001518 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001519 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001520 * We leave conflict resolution to the workqueue, where can hold
1521 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001522 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001523 if (likely(last_request->initiator ==
1524 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001525 wiphy_idx_valid(last_request->wiphy_idx)))
1526 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001527
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001528 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1529 if (!request)
Dan Carpenterf9f9b6e2010-07-22 13:26:50 +02001530 goto out;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001531
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001532 request->wiphy_idx = get_wiphy_idx(wiphy);
John W. Linville4f366c52010-07-15 14:57:33 -04001533 request->alpha2[0] = alpha2[0];
1534 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001535 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001536 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001537
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001538 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001539
1540 queue_regulatory_request(request);
1541
1542 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001543
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001544out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001545 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001546}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001547
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001548static void restore_alpha2(char *alpha2, bool reset_user)
1549{
1550 /* indicates there is no alpha2 to consider for restoration */
1551 alpha2[0] = '9';
1552 alpha2[1] = '7';
1553
1554 /* The user setting has precedence over the module parameter */
1555 if (is_user_regdom_saved()) {
1556 /* Unless we're asked to ignore it and reset it */
1557 if (reset_user) {
1558 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
1559 "including user preference\n");
1560 user_alpha2[0] = '9';
1561 user_alpha2[1] = '7';
1562
1563 /*
1564 * If we're ignoring user settings, we still need to
1565 * check the module parameter to ensure we put things
1566 * back as they were for a full restore.
1567 */
1568 if (!is_world_regdom(ieee80211_regdom)) {
1569 REG_DBG_PRINT("cfg80211: Keeping preference on "
1570 "module parameter ieee80211_regdom: %c%c\n",
1571 ieee80211_regdom[0],
1572 ieee80211_regdom[1]);
1573 alpha2[0] = ieee80211_regdom[0];
1574 alpha2[1] = ieee80211_regdom[1];
1575 }
1576 } else {
1577 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
1578 "while preserving user preference for: %c%c\n",
1579 user_alpha2[0],
1580 user_alpha2[1]);
1581 alpha2[0] = user_alpha2[0];
1582 alpha2[1] = user_alpha2[1];
1583 }
1584 } else if (!is_world_regdom(ieee80211_regdom)) {
1585 REG_DBG_PRINT("cfg80211: Keeping preference on "
1586 "module parameter ieee80211_regdom: %c%c\n",
1587 ieee80211_regdom[0],
1588 ieee80211_regdom[1]);
1589 alpha2[0] = ieee80211_regdom[0];
1590 alpha2[1] = ieee80211_regdom[1];
1591 } else
1592 REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
1593}
1594
1595/*
1596 * Restoring regulatory settings involves ingoring any
1597 * possibly stale country IE information and user regulatory
1598 * settings if so desired, this includes any beacon hints
1599 * learned as we could have traveled outside to another country
1600 * after disconnection. To restore regulatory settings we do
1601 * exactly what we did at bootup:
1602 *
1603 * - send a core regulatory hint
1604 * - send a user regulatory hint if applicable
1605 *
1606 * Device drivers that send a regulatory hint for a specific country
1607 * keep their own regulatory domain on wiphy->regd so that does does
1608 * not need to be remembered.
1609 */
1610static void restore_regulatory_settings(bool reset_user)
1611{
1612 char alpha2[2];
1613 struct reg_beacon *reg_beacon, *btmp;
1614
1615 mutex_lock(&cfg80211_mutex);
1616 mutex_lock(&reg_mutex);
1617
1618 reset_regdomains();
1619 restore_alpha2(alpha2, reset_user);
1620
1621 /* Clear beacon hints */
1622 spin_lock_bh(&reg_pending_beacons_lock);
1623 if (!list_empty(&reg_pending_beacons)) {
1624 list_for_each_entry_safe(reg_beacon, btmp,
1625 &reg_pending_beacons, list) {
1626 list_del(&reg_beacon->list);
1627 kfree(reg_beacon);
1628 }
1629 }
1630 spin_unlock_bh(&reg_pending_beacons_lock);
1631
1632 if (!list_empty(&reg_beacon_list)) {
1633 list_for_each_entry_safe(reg_beacon, btmp,
1634 &reg_beacon_list, list) {
1635 list_del(&reg_beacon->list);
1636 kfree(reg_beacon);
1637 }
1638 }
1639
1640 /* First restore to the basic regulatory settings */
1641 cfg80211_regdomain = cfg80211_world_regdom;
1642
1643 mutex_unlock(&reg_mutex);
1644 mutex_unlock(&cfg80211_mutex);
1645
1646 regulatory_hint_core(cfg80211_regdomain->alpha2);
1647
1648 /*
1649 * This restores the ieee80211_regdom module parameter
1650 * preference or the last user requested regulatory
1651 * settings, user regulatory settings takes precedence.
1652 */
1653 if (is_an_alpha2(alpha2))
1654 regulatory_hint_user(user_alpha2);
1655}
1656
1657
1658void regulatory_hint_disconnect(void)
1659{
1660 REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
1661 "restore regulatory settings\n");
1662 restore_regulatory_settings(false);
1663}
1664
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001665static bool freq_is_chan_12_13_14(u16 freq)
1666{
1667 if (freq == ieee80211_channel_to_frequency(12) ||
1668 freq == ieee80211_channel_to_frequency(13) ||
1669 freq == ieee80211_channel_to_frequency(14))
1670 return true;
1671 return false;
1672}
1673
1674int regulatory_hint_found_beacon(struct wiphy *wiphy,
1675 struct ieee80211_channel *beacon_chan,
1676 gfp_t gfp)
1677{
1678 struct reg_beacon *reg_beacon;
1679
1680 if (likely((beacon_chan->beacon_found ||
1681 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1682 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1683 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1684 return 0;
1685
1686 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1687 if (!reg_beacon)
1688 return -ENOMEM;
1689
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001690 REG_DBG_PRINT("cfg80211: Found new beacon on "
1691 "frequency: %d MHz (Ch %d) on %s\n",
1692 beacon_chan->center_freq,
1693 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1694 wiphy_name(wiphy));
1695
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001696 memcpy(&reg_beacon->chan, beacon_chan,
1697 sizeof(struct ieee80211_channel));
1698
1699
1700 /*
1701 * Since we can be called from BH or and non-BH context
1702 * we must use spin_lock_bh()
1703 */
1704 spin_lock_bh(&reg_pending_beacons_lock);
1705 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1706 spin_unlock_bh(&reg_pending_beacons_lock);
1707
1708 schedule_work(&reg_work);
1709
1710 return 0;
1711}
1712
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001713static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001714{
1715 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001716 const struct ieee80211_reg_rule *reg_rule = NULL;
1717 const struct ieee80211_freq_range *freq_range = NULL;
1718 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001719
Kalle Valo269ac5f2009-12-01 10:47:15 +02001720 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001721 "(max_antenna_gain, max_eirp)\n");
1722
1723 for (i = 0; i < rd->n_reg_rules; i++) {
1724 reg_rule = &rd->reg_rules[i];
1725 freq_range = &reg_rule->freq_range;
1726 power_rule = &reg_rule->power_rule;
1727
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001728 /*
1729 * There may not be documentation for max antenna gain
1730 * in certain regions
1731 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001732 if (power_rule->max_antenna_gain)
Kalle Valo269ac5f2009-12-01 10:47:15 +02001733 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001734 "(%d mBi, %d mBm)\n",
1735 freq_range->start_freq_khz,
1736 freq_range->end_freq_khz,
1737 freq_range->max_bandwidth_khz,
1738 power_rule->max_antenna_gain,
1739 power_rule->max_eirp);
1740 else
Kalle Valo269ac5f2009-12-01 10:47:15 +02001741 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001742 "(N/A, %d mBm)\n",
1743 freq_range->start_freq_khz,
1744 freq_range->end_freq_khz,
1745 freq_range->max_bandwidth_khz,
1746 power_rule->max_eirp);
1747 }
1748}
1749
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001750static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001751{
1752
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001753 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001754
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001755 if (last_request->initiator ==
1756 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001757 struct cfg80211_registered_device *rdev;
1758 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001759 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02001760 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001761 printk(KERN_INFO "cfg80211: Current regulatory "
1762 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02001763 rdev->country_ie_alpha2[0],
1764 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001765 } else
1766 printk(KERN_INFO "cfg80211: Current regulatory "
Frans Pop55f98932010-03-24 19:46:29 +01001767 "domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001768 } else
Frans Pop55f98932010-03-24 19:46:29 +01001769 printk(KERN_INFO "cfg80211: Current regulatory "
1770 "domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001771 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001772 printk(KERN_INFO "cfg80211: World regulatory "
1773 "domain updated:\n");
1774 else {
1775 if (is_unknown_alpha2(rd->alpha2))
1776 printk(KERN_INFO "cfg80211: Regulatory domain "
1777 "changed to driver built-in settings "
1778 "(unknown country)\n");
1779 else
1780 printk(KERN_INFO "cfg80211: Regulatory domain "
1781 "changed to country: %c%c\n",
1782 rd->alpha2[0], rd->alpha2[1]);
1783 }
1784 print_rd_rules(rd);
1785}
1786
Johannes Berg2df78162008-10-28 16:49:41 +01001787static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001788{
1789 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1790 rd->alpha2[0], rd->alpha2[1]);
1791 print_rd_rules(rd);
1792}
1793
Johannes Bergd2372b32008-10-24 20:32:20 +02001794/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001795static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001796{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001797 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02001798 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001799 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001800 /* Some basic sanity checks first */
1801
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001802 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001803 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001804 return -EINVAL;
1805 update_world_regdomain(rd);
1806 return 0;
1807 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001808
1809 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1810 !is_unknown_alpha2(rd->alpha2))
1811 return -EINVAL;
1812
Johannes Bergf6037d02008-10-21 11:01:33 +02001813 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001814 return -EINVAL;
1815
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001816 /*
1817 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001818 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001819 * and the pending request came in from a country IE
1820 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001821 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001822 /*
1823 * If someone else asked us to change the rd lets only bother
1824 * checking if the alpha2 changes if CRDA was already called
1825 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001826 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001827 return -EINVAL;
1828 }
1829
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001830 /*
1831 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001832 * and finally inform them of what we have done, in case they want
1833 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001834 * internal EEPROM data
1835 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001836
Johannes Bergf6037d02008-10-21 11:01:33 +02001837 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001838 return -EINVAL;
1839
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001840 if (!is_valid_rd(rd)) {
1841 printk(KERN_ERR "cfg80211: Invalid "
1842 "regulatory domain detected:\n");
1843 print_regdomain_info(rd);
1844 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001845 }
1846
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001847 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1848
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001849 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001850 int r;
1851
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001852 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001853 reset_regdomains();
1854 cfg80211_regdomain = rd;
1855 return 0;
1856 }
1857
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001858 /*
1859 * For a driver hint, lets copy the regulatory domain the
1860 * driver wanted to the wiphy to deal with conflicts
1861 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001862
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07001863 /*
1864 * Userspace could have sent two replies with only
1865 * one kernel request.
1866 */
1867 if (request_wiphy->regd)
1868 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001869
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001870 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001871 if (r)
1872 return r;
1873
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001874 reset_regdomains();
1875 cfg80211_regdomain = rd;
1876 return 0;
1877 }
1878
1879 /* Intersection requires a bit more work */
1880
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001881 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001882
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001883 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1884 if (!intersected_rd)
1885 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001886
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001887 /*
1888 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001889 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001890 * domain we keep it for its private use
1891 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001892 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001893 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001894 else
1895 kfree(rd);
1896
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001897 rd = NULL;
1898
1899 reset_regdomains();
1900 cfg80211_regdomain = intersected_rd;
1901
1902 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001903 }
1904
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001905 if (!intersected_rd)
1906 return -EINVAL;
1907
Johannes Berg79c97e92009-07-07 03:56:12 +02001908 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001909
Johannes Berg79c97e92009-07-07 03:56:12 +02001910 rdev->country_ie_alpha2[0] = rd->alpha2[0];
1911 rdev->country_ie_alpha2[1] = rd->alpha2[1];
1912 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001913
1914 BUG_ON(intersected_rd == rd);
1915
1916 kfree(rd);
1917 rd = NULL;
1918
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001919 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001920 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001921
1922 return 0;
1923}
1924
1925
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001926/*
1927 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001928 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001929 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
1930 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001931int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001932{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001933 int r;
1934
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001935 assert_cfg80211_lock();
1936
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001937 mutex_lock(&reg_mutex);
1938
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001939 /* Note that this doesn't update the wiphys, this is done below */
1940 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02001941 if (r) {
1942 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001943 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001944 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02001945 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001946
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001947 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001948 if (!last_request->intersect)
1949 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001950
1951 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02001952 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001953
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001954 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001955
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001956 nl80211_send_reg_change_event(last_request);
1957
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001958 mutex_unlock(&reg_mutex);
1959
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001960 return r;
1961}
1962
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001963/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001964void reg_device_remove(struct wiphy *wiphy)
1965{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04001966 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001967
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001968 assert_cfg80211_lock();
1969
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001970 mutex_lock(&reg_mutex);
1971
Chris Wright0ef9ccd2009-04-24 14:09:31 -07001972 kfree(wiphy->regd);
1973
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04001974 if (last_request)
1975 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001976
Chris Wright0ef9ccd2009-04-24 14:09:31 -07001977 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001978 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07001979
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001980 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001981 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001982out:
1983 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001984}
1985
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02001986int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001987{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05001988 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02001989
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001990 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1991 if (IS_ERR(reg_pdev))
1992 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001993
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001994 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001995 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001996
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001997 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02001998
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001999 user_alpha2[0] = '9';
2000 user_alpha2[1] = '7';
2001
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002002 /* We always try to get an update for the static regdomain */
2003 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002004 if (err) {
2005 if (err == -ENOMEM)
2006 return err;
2007 /*
2008 * N.B. kobject_uevent_env() can fail mainly for when we're out
2009 * memory which is handled and propagated appropriately above
2010 * but it can also fail during a netlink_broadcast() or during
2011 * early boot for call_usermodehelper(). For now treat these
2012 * errors as non-fatal.
2013 */
2014 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2015 "to call CRDA during init");
2016#ifdef CONFIG_CFG80211_REG_DEBUG
2017 /* We want to find out exactly why when debugging */
2018 WARN_ON(err);
2019#endif
2020 }
Johannes Berg734366d2008-09-15 10:56:48 +02002021
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002022 /*
2023 * Finally, if the user set the module parameter treat it
2024 * as a user hint.
2025 */
2026 if (!is_world_regdom(ieee80211_regdom))
2027 regulatory_hint_user(ieee80211_regdom);
2028
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002029 return 0;
2030}
2031
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002032void /* __init_or_exit */ regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002033{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002034 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002035 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002036
2037 cancel_work_sync(&reg_work);
2038
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002039 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002040 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002041
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002042 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002043
Johannes Bergf6037d02008-10-21 11:01:33 +02002044 kfree(last_request);
2045
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002046 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002047
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002048 spin_lock_bh(&reg_pending_beacons_lock);
2049 if (!list_empty(&reg_pending_beacons)) {
2050 list_for_each_entry_safe(reg_beacon, btmp,
2051 &reg_pending_beacons, list) {
2052 list_del(&reg_beacon->list);
2053 kfree(reg_beacon);
2054 }
2055 }
2056 spin_unlock_bh(&reg_pending_beacons_lock);
2057
2058 if (!list_empty(&reg_beacon_list)) {
2059 list_for_each_entry_safe(reg_beacon, btmp,
2060 &reg_beacon_list, list) {
2061 list_del(&reg_beacon->list);
2062 kfree(reg_beacon);
2063 }
2064 }
2065
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002066 spin_lock(&reg_requests_lock);
2067 if (!list_empty(&reg_requests_list)) {
2068 list_for_each_entry_safe(reg_request, tmp,
2069 &reg_requests_list, list) {
2070 list_del(&reg_request->list);
2071 kfree(reg_request);
2072 }
2073 }
2074 spin_unlock(&reg_requests_lock);
2075
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002076 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002077 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002078}