blob: 994c7c5f071b53b969506c8962626d6597f1faa3 [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>
39#include <linux/nl80211.h>
40#include <linux/platform_device.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070041#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010042#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070043#include "reg.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050044#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040045#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010046
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050047#ifdef CONFIG_CFG80211_REG_DEBUG
John W. Linville82711952010-01-05 17:57:20 -050048#define REG_DBG_PRINT(format, args...) \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050049 do { \
John W. Linville82711952010-01-05 17:57:20 -050050 printk(KERN_DEBUG format , ## args); \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050051 } while (0)
52#else
John W. Linville82711952010-01-05 17:57:20 -050053#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050054#endif
55
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070056/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020057static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020058
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070059/* To trigger userspace events */
60static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010061
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050062/*
63 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020064 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050065 * information to give us an alpha2
66 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080067const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020068
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050069/*
70 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080071 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050072 * what it thinks should apply for the same country
73 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080074static const struct ieee80211_regdomain *country_ie_regdomain;
75
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070076/*
77 * Protects static reg.c components:
78 * - cfg80211_world_regdom
79 * - cfg80211_regdom
80 * - country_ie_regdomain
81 * - last_request
82 */
83DEFINE_MUTEX(reg_mutex);
84#define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
85
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050086/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050087static LIST_HEAD(reg_requests_list);
88static spinlock_t reg_requests_lock;
89
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050090/* Used to queue up beacon hints for review */
91static LIST_HEAD(reg_pending_beacons);
92static spinlock_t reg_pending_beacons_lock;
93
94/* Used to keep track of processed beacon hints */
95static LIST_HEAD(reg_beacon_list);
96
97struct reg_beacon {
98 struct list_head list;
99 struct ieee80211_channel chan;
100};
101
Johannes Berg734366d2008-09-15 10:56:48 +0200102/* We keep a static world regulatory domain in case of the absence of CRDA */
103static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800104 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +0200105 .alpha2 = "00",
106 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500107 /* IEEE 802.11b/g, channels 1..11 */
108 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800109 /* IEEE 802.11b/g, channels 12..13. No HT40
110 * channel fits here. */
111 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500112 NL80211_RRF_PASSIVE_SCAN |
113 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800114 /* IEEE 802.11 channel 14 - Only JP enables
115 * this and for 802.11b only */
116 REG_RULE(2484-10, 2484+10, 20, 6, 20,
117 NL80211_RRF_PASSIVE_SCAN |
118 NL80211_RRF_NO_IBSS |
119 NL80211_RRF_NO_OFDM),
120 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800121 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800122 NL80211_RRF_PASSIVE_SCAN |
123 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500124
125 /* NB: 5260 MHz - 5700 MHz requies DFS */
126
127 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800128 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500129 NL80211_RRF_PASSIVE_SCAN |
130 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200131 }
132};
133
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200134static const struct ieee80211_regdomain *cfg80211_world_regdom =
135 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200136
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400137static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500138static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400139
Johannes Berg734366d2008-09-15 10:56:48 +0200140module_param(ieee80211_regdom, charp, 0444);
141MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
142
Johannes Berg734366d2008-09-15 10:56:48 +0200143static void reset_regdomains(void)
144{
Johannes Berg942b25c2008-09-15 11:26:47 +0200145 /* avoid freeing static information or freeing something twice */
146 if (cfg80211_regdomain == cfg80211_world_regdom)
147 cfg80211_regdomain = NULL;
148 if (cfg80211_world_regdom == &world_regdom)
149 cfg80211_world_regdom = NULL;
150 if (cfg80211_regdomain == &world_regdom)
151 cfg80211_regdomain = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200152
153 kfree(cfg80211_regdomain);
154 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200155
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200156 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200157 cfg80211_regdomain = NULL;
158}
159
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500160/*
161 * Dynamic world regulatory domain requested by the wireless
162 * core upon initialization
163 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200164static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200165{
Johannes Bergf6037d02008-10-21 11:01:33 +0200166 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200167
168 reset_regdomains();
169
170 cfg80211_world_regdom = rd;
171 cfg80211_regdomain = rd;
172}
Johannes Berg734366d2008-09-15 10:56:48 +0200173
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200174bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100175{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700176 if (!alpha2)
177 return false;
178 if (alpha2[0] == '0' && alpha2[1] == '0')
179 return true;
180 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100181}
182
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200183static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700184{
185 if (!alpha2)
186 return false;
187 if (alpha2[0] != 0 && alpha2[1] != 0)
188 return true;
189 return false;
190}
Johannes Berg8318d782008-01-24 19:38:38 +0100191
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700192static bool is_alpha_upper(char letter)
193{
194 /* ASCII A - Z */
195 if (letter >= 65 && letter <= 90)
196 return true;
197 return false;
198}
199
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200200static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700201{
202 if (!alpha2)
203 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500204 /*
205 * Special case where regulatory domain was built by driver
206 * but a specific alpha2 cannot be determined
207 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700208 if (alpha2[0] == '9' && alpha2[1] == '9')
209 return true;
210 return false;
211}
212
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800213static bool is_intersected_alpha2(const char *alpha2)
214{
215 if (!alpha2)
216 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500217 /*
218 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800219 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500220 * structures
221 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800222 if (alpha2[0] == '9' && alpha2[1] == '8')
223 return true;
224 return false;
225}
226
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200227static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700228{
229 if (!alpha2)
230 return false;
231 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
232 return true;
233 return false;
234}
235
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200236static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700237{
238 if (!alpha2_x || !alpha2_y)
239 return false;
240 if (alpha2_x[0] == alpha2_y[0] &&
241 alpha2_x[1] == alpha2_y[1])
242 return true;
243 return false;
244}
245
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500246static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700247{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500248 assert_cfg80211_lock();
249
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700250 if (!cfg80211_regdomain)
251 return true;
252 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
253 return false;
254 return true;
255}
256
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500257/*
258 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
259 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
260 * has ever been issued.
261 */
262static bool is_user_regdom_saved(void)
263{
264 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
265 return false;
266
267 /* This would indicate a mistake on the design */
268 if (WARN((!is_world_regdom(user_alpha2) &&
269 !is_an_alpha2(user_alpha2)),
270 "Unexpected user alpha2: %c%c\n",
271 user_alpha2[0],
272 user_alpha2[1]))
273 return false;
274
275 return true;
276}
277
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800278/**
279 * country_ie_integrity_changes - tells us if the country IE has changed
280 * @checksum: checksum of country IE of fields we are interested in
281 *
282 * If the country IE has not changed you can ignore it safely. This is
283 * useful to determine if two devices are seeing two different country IEs
284 * even on the same alpha2. Note that this will return false if no IE has
285 * been set on the wireless core yet.
286 */
287static bool country_ie_integrity_changes(u32 checksum)
288{
289 /* If no IE has been set then the checksum doesn't change */
290 if (unlikely(!last_request->country_ie_checksum))
291 return false;
292 if (unlikely(last_request->country_ie_checksum != checksum))
293 return true;
294 return false;
295}
296
John W. Linville3b377ea2009-12-18 17:59:01 -0500297static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
298 const struct ieee80211_regdomain *src_regd)
299{
300 struct ieee80211_regdomain *regd;
301 int size_of_regd = 0;
302 unsigned int i;
303
304 size_of_regd = sizeof(struct ieee80211_regdomain) +
305 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
306
307 regd = kzalloc(size_of_regd, GFP_KERNEL);
308 if (!regd)
309 return -ENOMEM;
310
311 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
312
313 for (i = 0; i < src_regd->n_reg_rules; i++)
314 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
315 sizeof(struct ieee80211_reg_rule));
316
317 *dst_regd = regd;
318 return 0;
319}
320
321#ifdef CONFIG_CFG80211_INTERNAL_REGDB
322struct reg_regdb_search_request {
323 char alpha2[2];
324 struct list_head list;
325};
326
327static LIST_HEAD(reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400328static DEFINE_MUTEX(reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500329
330static void reg_regdb_search(struct work_struct *work)
331{
332 struct reg_regdb_search_request *request;
333 const struct ieee80211_regdomain *curdom, *regdom;
334 int i, r;
335
John W. Linville368d06f2010-03-16 15:40:59 -0400336 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500337 while (!list_empty(&reg_regdb_search_list)) {
338 request = list_first_entry(&reg_regdb_search_list,
339 struct reg_regdb_search_request,
340 list);
341 list_del(&request->list);
342
343 for (i=0; i<reg_regdb_size; i++) {
344 curdom = reg_regdb[i];
345
346 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
347 r = reg_copy_regd(&regdom, curdom);
348 if (r)
349 break;
John W. Linville3b377ea2009-12-18 17:59:01 -0500350 mutex_lock(&cfg80211_mutex);
351 set_regdom(regdom);
352 mutex_unlock(&cfg80211_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500353 break;
354 }
355 }
356
357 kfree(request);
358 }
John W. Linville368d06f2010-03-16 15:40:59 -0400359 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500360}
361
362static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
363
364static void reg_regdb_query(const char *alpha2)
365{
366 struct reg_regdb_search_request *request;
367
368 if (!alpha2)
369 return;
370
371 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
372 if (!request)
373 return;
374
375 memcpy(request->alpha2, alpha2, 2);
376
John W. Linville368d06f2010-03-16 15:40:59 -0400377 mutex_lock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500378 list_add_tail(&request->list, &reg_regdb_search_list);
John W. Linville368d06f2010-03-16 15:40:59 -0400379 mutex_unlock(&reg_regdb_search_mutex);
John W. Linville3b377ea2009-12-18 17:59:01 -0500380
381 schedule_work(&reg_regdb_work);
382}
383#else
384static inline void reg_regdb_query(const char *alpha2) {}
385#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
386
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500387/*
388 * This lets us keep regulatory code which is updated on a regulatory
389 * basis in userspace.
390 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700391static int call_crda(const char *alpha2)
392{
393 char country_env[9 + 2] = "COUNTRY=";
394 char *envp[] = {
395 country_env,
396 NULL
397 };
398
399 if (!is_world_regdom((char *) alpha2))
400 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
401 alpha2[0], alpha2[1]);
402 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700403 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
404 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700405
John W. Linville3b377ea2009-12-18 17:59:01 -0500406 /* query internal regulatory database (if it exists) */
407 reg_regdb_query(alpha2);
408
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700409 country_env[8] = alpha2[0];
410 country_env[9] = alpha2[1];
411
412 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
413}
414
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700415/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200416bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700417{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400418 assert_cfg80211_lock();
419
Johannes Bergf6037d02008-10-21 11:01:33 +0200420 if (!last_request)
421 return false;
422
423 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700424}
425
426/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200427static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700428{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200429 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700430 u32 freq_diff;
431
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800432 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700433 return false;
434
435 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
436 return false;
437
438 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
439
Roel Kluinbd05f282009-03-03 22:55:21 +0100440 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
441 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700442 return false;
443
444 return true;
445}
446
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200447static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700448{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200449 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700450 unsigned int i;
451
452 if (!rd->n_reg_rules)
453 return false;
454
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800455 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
456 return false;
457
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700458 for (i = 0; i < rd->n_reg_rules; i++) {
459 reg_rule = &rd->reg_rules[i];
460 if (!is_valid_reg_rule(reg_rule))
461 return false;
462 }
463
464 return true;
465}
466
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400467static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
468 u32 center_freq_khz,
469 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700470{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400471 u32 start_freq_khz, end_freq_khz;
472
473 start_freq_khz = center_freq_khz - (bw_khz/2);
474 end_freq_khz = center_freq_khz + (bw_khz/2);
475
476 if (start_freq_khz >= freq_range->start_freq_khz &&
477 end_freq_khz <= freq_range->end_freq_khz)
478 return true;
479
480 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700481}
482
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800483/**
484 * freq_in_rule_band - tells us if a frequency is in a frequency band
485 * @freq_range: frequency rule we want to query
486 * @freq_khz: frequency we are inquiring about
487 *
488 * This lets us know if a specific frequency rule is or is not relevant to
489 * a specific frequency's band. Bands are device specific and artificial
490 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
491 * safe for now to assume that a frequency rule should not be part of a
492 * frequency's band if the start freq or end freq are off by more than 2 GHz.
493 * This resolution can be lowered and should be considered as we add
494 * regulatory rule support for other "bands".
495 **/
496static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
497 u32 freq_khz)
498{
499#define ONE_GHZ_IN_KHZ 1000000
500 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
501 return true;
502 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
503 return true;
504 return false;
505#undef ONE_GHZ_IN_KHZ
506}
507
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500508/*
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500509 * This is a work around for sanity checking ieee80211_channel_to_frequency()'s
510 * work. ieee80211_channel_to_frequency() can for example currently provide a
511 * 2 GHz channel when in fact a 5 GHz channel was desired. An example would be
512 * an AP providing channel 8 on a country IE triplet when it sent this on the
513 * 5 GHz band, that channel is designed to be channel 8 on 5 GHz, not a 2 GHz
514 * channel.
515 *
516 * This can be removed once ieee80211_channel_to_frequency() takes in a band.
517 */
518static bool chan_in_band(int chan, enum ieee80211_band band)
519{
520 int center_freq = ieee80211_channel_to_frequency(chan);
521
522 switch (band) {
523 case IEEE80211_BAND_2GHZ:
524 if (center_freq <= 2484)
525 return true;
526 return false;
527 case IEEE80211_BAND_5GHZ:
528 if (center_freq >= 5005)
529 return true;
530 return false;
531 default:
532 return false;
533 }
534}
535
536/*
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500537 * Some APs may send a country IE triplet for each channel they
538 * support and while this is completely overkill and silly we still
539 * need to support it. We avoid making a single rule for each channel
540 * though and to help us with this we use this helper to find the
541 * actual subband end channel. These type of country IE triplet
542 * scenerios are handled then, all yielding two regulaotry rules from
543 * parsing a country IE:
544 *
545 * [1]
546 * [2]
547 * [36]
548 * [40]
549 *
550 * [1]
551 * [2-4]
552 * [5-12]
553 * [36]
554 * [40-44]
555 *
556 * [1-4]
557 * [5-7]
558 * [36-44]
559 * [48-64]
560 *
561 * [36-36]
562 * [40-40]
563 * [44-44]
564 * [48-48]
565 * [52-52]
566 * [56-56]
567 * [60-60]
568 * [64-64]
569 * [100-100]
570 * [104-104]
571 * [108-108]
572 * [112-112]
573 * [116-116]
574 * [120-120]
575 * [124-124]
576 * [128-128]
577 * [132-132]
578 * [136-136]
579 * [140-140]
580 *
581 * Returns 0 if the IE has been found to be invalid in the middle
582 * somewhere.
583 */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500584static int max_subband_chan(enum ieee80211_band band,
585 int orig_cur_chan,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500586 int orig_end_channel,
587 s8 orig_max_power,
588 u8 **country_ie,
589 u8 *country_ie_len)
590{
591 u8 *triplets_start = *country_ie;
592 u8 len_at_triplet = *country_ie_len;
593 int end_subband_chan = orig_end_channel;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500594
595 /*
596 * We'll deal with padding for the caller unless
597 * its not immediate and we don't process any channels
598 */
599 if (*country_ie_len == 1) {
600 *country_ie += 1;
601 *country_ie_len -= 1;
602 return orig_end_channel;
603 }
604
605 /* Move to the next triplet and then start search */
606 *country_ie += 3;
607 *country_ie_len -= 3;
608
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500609 if (!chan_in_band(orig_cur_chan, band))
610 return 0;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500611
612 while (*country_ie_len >= 3) {
613 int end_channel = 0;
614 struct ieee80211_country_ie_triplet *triplet =
615 (struct ieee80211_country_ie_triplet *) *country_ie;
616 int cur_channel = 0, next_expected_chan;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500617
618 /* means last triplet is completely unrelated to this one */
619 if (triplet->ext.reg_extension_id >=
620 IEEE80211_COUNTRY_EXTENSION_ID) {
621 *country_ie -= 3;
622 *country_ie_len += 3;
623 break;
624 }
625
626 if (triplet->chans.first_channel == 0) {
627 *country_ie += 1;
628 *country_ie_len -= 1;
629 if (*country_ie_len != 0)
630 return 0;
631 break;
632 }
633
Luis R. Rodrigueza0f2e0f2010-01-14 13:27:46 -0500634 if (triplet->chans.num_channels == 0)
635 return 0;
636
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500637 /* Monitonically increasing channel order */
638 if (triplet->chans.first_channel <= end_subband_chan)
639 return 0;
640
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500641 if (!chan_in_band(triplet->chans.first_channel, band))
642 return 0;
643
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500644 /* 2 GHz */
645 if (triplet->chans.first_channel <= 14) {
646 end_channel = triplet->chans.first_channel +
647 triplet->chans.num_channels - 1;
648 }
649 else {
650 end_channel = triplet->chans.first_channel +
651 (4 * (triplet->chans.num_channels - 1));
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500652 }
653
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500654 if (!chan_in_band(end_channel, band))
655 return 0;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500656
657 if (orig_max_power != triplet->chans.max_power) {
658 *country_ie -= 3;
659 *country_ie_len += 3;
660 break;
661 }
662
663 cur_channel = triplet->chans.first_channel;
664
665 /* The key is finding the right next expected channel */
666 if (band == IEEE80211_BAND_2GHZ)
667 next_expected_chan = end_subband_chan + 1;
668 else
669 next_expected_chan = end_subband_chan + 4;
670
671 if (cur_channel != next_expected_chan) {
672 *country_ie -= 3;
673 *country_ie_len += 3;
674 break;
675 }
676
677 end_subband_chan = end_channel;
678
679 /* Move to the next one */
680 *country_ie += 3;
681 *country_ie_len -= 3;
682
683 /*
684 * Padding needs to be dealt with if we processed
685 * some channels.
686 */
687 if (*country_ie_len == 1) {
688 *country_ie += 1;
689 *country_ie_len -= 1;
690 break;
691 }
692
693 /* If seen, the IE is invalid */
694 if (*country_ie_len == 2)
695 return 0;
696 }
697
698 if (end_subband_chan == orig_end_channel) {
699 *country_ie = triplets_start;
700 *country_ie_len = len_at_triplet;
701 return orig_end_channel;
702 }
703
704 return end_subband_chan;
705}
706
707/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500708 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800709 * structure has a lot of information which the IE doesn't yet have,
710 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500711 * with our userspace regulatory agent to get lower bounds.
712 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800713static struct ieee80211_regdomain *country_ie_2_rd(
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500714 enum ieee80211_band band,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800715 u8 *country_ie,
716 u8 country_ie_len,
717 u32 *checksum)
718{
719 struct ieee80211_regdomain *rd = NULL;
720 unsigned int i = 0;
721 char alpha2[2];
722 u32 flags = 0;
723 u32 num_rules = 0, size_of_regd = 0;
724 u8 *triplets_start = NULL;
725 u8 len_at_triplet = 0;
726 /* the last channel we have registered in a subband (triplet) */
727 int last_sub_max_channel = 0;
728
729 *checksum = 0xDEADBEEF;
730
731 /* Country IE requirements */
732 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
733 country_ie_len & 0x01);
734
735 alpha2[0] = country_ie[0];
736 alpha2[1] = country_ie[1];
737
738 /*
739 * Third octet can be:
740 * 'I' - Indoor
741 * 'O' - Outdoor
742 *
743 * anything else we assume is no restrictions
744 */
745 if (country_ie[2] == 'I')
746 flags = NL80211_RRF_NO_OUTDOOR;
747 else if (country_ie[2] == 'O')
748 flags = NL80211_RRF_NO_INDOOR;
749
750 country_ie += 3;
751 country_ie_len -= 3;
752
753 triplets_start = country_ie;
754 len_at_triplet = country_ie_len;
755
756 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
757
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500758 /*
759 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800760 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500761 * for each channel subband
762 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800763 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800764 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800765 struct ieee80211_country_ie_triplet *triplet =
766 (struct ieee80211_country_ie_triplet *) country_ie;
767 int cur_sub_max_channel = 0, cur_channel = 0;
768
769 if (triplet->ext.reg_extension_id >=
770 IEEE80211_COUNTRY_EXTENSION_ID) {
771 country_ie += 3;
772 country_ie_len -= 3;
773 continue;
774 }
775
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500776 /*
777 * APs can add padding to make length divisible
778 * by two, required by the spec.
779 */
780 if (triplet->chans.first_channel == 0) {
781 country_ie++;
782 country_ie_len--;
783 /* This is expected to be at the very end only */
784 if (country_ie_len != 0)
785 return NULL;
786 break;
787 }
788
Luis R. Rodrigueza0f2e0f2010-01-14 13:27:46 -0500789 if (triplet->chans.num_channels == 0)
790 return NULL;
791
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500792 if (!chan_in_band(triplet->chans.first_channel, band))
793 return NULL;
794
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800795 /* 2 GHz */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500796 if (band == IEEE80211_BAND_2GHZ)
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800797 end_channel = triplet->chans.first_channel +
Luis R. Rodrigueze99c7cd2010-01-07 17:24:55 -0500798 triplet->chans.num_channels - 1;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800799 else
800 /*
801 * 5 GHz -- For example in country IEs if the first
802 * channel given is 36 and the number of channels is 4
803 * then the individual channel numbers defined for the
804 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
805 * and not 36, 37, 38, 39.
806 *
807 * See: http://tinyurl.com/11d-clarification
808 */
809 end_channel = triplet->chans.first_channel +
810 (4 * (triplet->chans.num_channels - 1));
811
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800812 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500813
814 /*
815 * Enhancement for APs that send a triplet for every channel
816 * or for whatever reason sends triplets with multiple channels
817 * separated when in fact they should be together.
818 */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500819 end_channel = max_subband_chan(band,
820 cur_channel,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500821 end_channel,
822 triplet->chans.max_power,
823 &country_ie,
824 &country_ie_len);
825 if (!end_channel)
826 return NULL;
827
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500828 if (!chan_in_band(end_channel, band))
829 return NULL;
830
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800831 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800832
833 /* Basic sanity check */
834 if (cur_sub_max_channel < cur_channel)
835 return NULL;
836
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500837 /*
838 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800839 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500840 * increasing
841 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800842 if (last_sub_max_channel) {
843 if (cur_channel <= last_sub_max_channel)
844 return NULL;
845 if (cur_sub_max_channel <= last_sub_max_channel)
846 return NULL;
847 }
848
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500849 /*
850 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800851 * we can throw ext triplets as part of this soup,
852 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500853 * don't support them
854 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800855 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
856 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
857 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
858
859 last_sub_max_channel = cur_sub_max_channel;
860
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800861 num_rules++;
862
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500863 if (country_ie_len >= 3) {
864 country_ie += 3;
865 country_ie_len -= 3;
866 }
867
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500868 /*
869 * Note: this is not a IEEE requirement but
870 * simply a memory requirement
871 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800872 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
873 return NULL;
874 }
875
876 country_ie = triplets_start;
877 country_ie_len = len_at_triplet;
878
879 size_of_regd = sizeof(struct ieee80211_regdomain) +
880 (num_rules * sizeof(struct ieee80211_reg_rule));
881
882 rd = kzalloc(size_of_regd, GFP_KERNEL);
883 if (!rd)
884 return NULL;
885
886 rd->n_reg_rules = num_rules;
887 rd->alpha2[0] = alpha2[0];
888 rd->alpha2[1] = alpha2[1];
889
890 /* This time around we fill in the rd */
891 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800892 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800893 struct ieee80211_country_ie_triplet *triplet =
894 (struct ieee80211_country_ie_triplet *) country_ie;
895 struct ieee80211_reg_rule *reg_rule = NULL;
896 struct ieee80211_freq_range *freq_range = NULL;
897 struct ieee80211_power_rule *power_rule = NULL;
898
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500899 /*
900 * Must parse if dot11RegulatoryClassesRequired is true,
901 * we don't support this yet
902 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800903 if (triplet->ext.reg_extension_id >=
904 IEEE80211_COUNTRY_EXTENSION_ID) {
905 country_ie += 3;
906 country_ie_len -= 3;
907 continue;
908 }
909
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500910 if (triplet->chans.first_channel == 0) {
911 country_ie++;
912 country_ie_len--;
913 break;
914 }
915
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800916 reg_rule = &rd->reg_rules[i];
917 freq_range = &reg_rule->freq_range;
918 power_rule = &reg_rule->power_rule;
919
920 reg_rule->flags = flags;
921
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800922 /* 2 GHz */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500923 if (band == IEEE80211_BAND_2GHZ)
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800924 end_channel = triplet->chans.first_channel +
Luis R. Rodrigueze99c7cd2010-01-07 17:24:55 -0500925 triplet->chans.num_channels -1;
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800926 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800927 end_channel = triplet->chans.first_channel +
928 (4 * (triplet->chans.num_channels - 1));
929
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500930 end_channel = max_subband_chan(band,
931 triplet->chans.first_channel,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500932 end_channel,
933 triplet->chans.max_power,
934 &country_ie,
935 &country_ie_len);
936
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500937 /*
938 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800939 * the actual band edge, not the center of freq for
940 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500941 * the channels passed
942 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800943 freq_range->start_freq_khz =
944 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
945 triplet->chans.first_channel) - 10);
946 freq_range->end_freq_khz =
947 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800948 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800949
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500950 /*
951 * These are large arbitrary values we use to intersect later.
952 * Increment this if we ever support >= 40 MHz channels
953 * in IEEE 802.11
954 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800955 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
956 power_rule->max_antenna_gain = DBI_TO_MBI(100);
Luis R. Rodriguez08030db2010-01-07 17:24:56 -0500957 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800958
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800959 i++;
960
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500961 if (country_ie_len >= 3) {
962 country_ie += 3;
963 country_ie_len -= 3;
964 }
965
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800966 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
967 }
968
969 return rd;
970}
971
972
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500973/*
974 * Helper for regdom_intersect(), this does the real
975 * mathematical intersection fun
976 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700977static int reg_rules_intersect(
978 const struct ieee80211_reg_rule *rule1,
979 const struct ieee80211_reg_rule *rule2,
980 struct ieee80211_reg_rule *intersected_rule)
981{
982 const struct ieee80211_freq_range *freq_range1, *freq_range2;
983 struct ieee80211_freq_range *freq_range;
984 const struct ieee80211_power_rule *power_rule1, *power_rule2;
985 struct ieee80211_power_rule *power_rule;
986 u32 freq_diff;
987
988 freq_range1 = &rule1->freq_range;
989 freq_range2 = &rule2->freq_range;
990 freq_range = &intersected_rule->freq_range;
991
992 power_rule1 = &rule1->power_rule;
993 power_rule2 = &rule2->power_rule;
994 power_rule = &intersected_rule->power_rule;
995
996 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
997 freq_range2->start_freq_khz);
998 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
999 freq_range2->end_freq_khz);
1000 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
1001 freq_range2->max_bandwidth_khz);
1002
1003 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1004 if (freq_range->max_bandwidth_khz > freq_diff)
1005 freq_range->max_bandwidth_khz = freq_diff;
1006
1007 power_rule->max_eirp = min(power_rule1->max_eirp,
1008 power_rule2->max_eirp);
1009 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
1010 power_rule2->max_antenna_gain);
1011
1012 intersected_rule->flags = (rule1->flags | rule2->flags);
1013
1014 if (!is_valid_reg_rule(intersected_rule))
1015 return -EINVAL;
1016
1017 return 0;
1018}
1019
1020/**
1021 * regdom_intersect - do the intersection between two regulatory domains
1022 * @rd1: first regulatory domain
1023 * @rd2: second regulatory domain
1024 *
1025 * Use this function to get the intersection between two regulatory domains.
1026 * Once completed we will mark the alpha2 for the rd as intersected, "98",
1027 * as no one single alpha2 can represent this regulatory domain.
1028 *
1029 * Returns a pointer to the regulatory domain structure which will hold the
1030 * resulting intersection of rules between rd1 and rd2. We will
1031 * kzalloc() this structure for you.
1032 */
1033static struct ieee80211_regdomain *regdom_intersect(
1034 const struct ieee80211_regdomain *rd1,
1035 const struct ieee80211_regdomain *rd2)
1036{
1037 int r, size_of_regd;
1038 unsigned int x, y;
1039 unsigned int num_rules = 0, rule_idx = 0;
1040 const struct ieee80211_reg_rule *rule1, *rule2;
1041 struct ieee80211_reg_rule *intersected_rule;
1042 struct ieee80211_regdomain *rd;
1043 /* This is just a dummy holder to help us count */
1044 struct ieee80211_reg_rule irule;
1045
1046 /* Uses the stack temporarily for counter arithmetic */
1047 intersected_rule = &irule;
1048
1049 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
1050
1051 if (!rd1 || !rd2)
1052 return NULL;
1053
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001054 /*
1055 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001056 * build them. This is to so we can malloc() and free() a
1057 * regdomain once. The reason we use reg_rules_intersect() here
1058 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001059 * All rules that do check out OK are valid.
1060 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001061
1062 for (x = 0; x < rd1->n_reg_rules; x++) {
1063 rule1 = &rd1->reg_rules[x];
1064 for (y = 0; y < rd2->n_reg_rules; y++) {
1065 rule2 = &rd2->reg_rules[y];
1066 if (!reg_rules_intersect(rule1, rule2,
1067 intersected_rule))
1068 num_rules++;
1069 memset(intersected_rule, 0,
1070 sizeof(struct ieee80211_reg_rule));
1071 }
1072 }
1073
1074 if (!num_rules)
1075 return NULL;
1076
1077 size_of_regd = sizeof(struct ieee80211_regdomain) +
1078 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
1079
1080 rd = kzalloc(size_of_regd, GFP_KERNEL);
1081 if (!rd)
1082 return NULL;
1083
1084 for (x = 0; x < rd1->n_reg_rules; x++) {
1085 rule1 = &rd1->reg_rules[x];
1086 for (y = 0; y < rd2->n_reg_rules; y++) {
1087 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001088 /*
1089 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001090 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001091 * a memcpy()
1092 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001093 intersected_rule = &rd->reg_rules[rule_idx];
1094 r = reg_rules_intersect(rule1, rule2,
1095 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001096 /*
1097 * No need to memset here the intersected rule here as
1098 * we're not using the stack anymore
1099 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001100 if (r)
1101 continue;
1102 rule_idx++;
1103 }
1104 }
1105
1106 if (rule_idx != num_rules) {
1107 kfree(rd);
1108 return NULL;
1109 }
1110
1111 rd->n_reg_rules = num_rules;
1112 rd->alpha2[0] = '9';
1113 rd->alpha2[1] = '8';
1114
1115 return rd;
1116}
1117
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001118/*
1119 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1120 * want to just have the channel structure use these
1121 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001122static u32 map_regdom_flags(u32 rd_flags)
1123{
1124 u32 channel_flags = 0;
1125 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
1126 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1127 if (rd_flags & NL80211_RRF_NO_IBSS)
1128 channel_flags |= IEEE80211_CHAN_NO_IBSS;
1129 if (rd_flags & NL80211_RRF_DFS)
1130 channel_flags |= IEEE80211_CHAN_RADAR;
1131 return channel_flags;
1132}
1133
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001134static int freq_reg_info_regd(struct wiphy *wiphy,
1135 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001136 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001137 const struct ieee80211_reg_rule **reg_rule,
1138 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +01001139{
1140 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001141 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001142 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001143 bool bw_fits = false;
1144
1145 if (!desired_bw_khz)
1146 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +01001147
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001148 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001149
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001150 /*
1151 * Follow the driver's regulatory domain, if present, unless a country
1152 * IE has been processed or a user wants to help complaince further
1153 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001154 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1155 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001156 wiphy->regd)
1157 regd = wiphy->regd;
1158
1159 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001160 return -EINVAL;
1161
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001162 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001163 const struct ieee80211_reg_rule *rr;
1164 const struct ieee80211_freq_range *fr = NULL;
1165 const struct ieee80211_power_rule *pr = NULL;
1166
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001167 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001168 fr = &rr->freq_range;
1169 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001170
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001171 /*
1172 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001173 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001174 * not overwrite it once found
1175 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001176 if (!band_rule_found)
1177 band_rule_found = freq_in_rule_band(fr, center_freq);
1178
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001179 bw_fits = reg_does_bw_fit(fr,
1180 center_freq,
1181 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001182
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001183 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001184 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001185 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001186 }
1187 }
1188
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001189 if (!band_rule_found)
1190 return -ERANGE;
1191
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001192 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001193}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -08001194EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001195
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001196int freq_reg_info(struct wiphy *wiphy,
1197 u32 center_freq,
1198 u32 desired_bw_khz,
1199 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001200{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001201 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001202 return freq_reg_info_regd(wiphy,
1203 center_freq,
1204 desired_bw_khz,
1205 reg_rule,
1206 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001207}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001208
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001209/*
1210 * Note that right now we assume the desired channel bandwidth
1211 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1212 * per channel, the primary and the extension channel). To support
1213 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1214 * new ieee80211_channel.target_bw and re run the regulatory check
1215 * on the wiphy with the target_bw specified. Then we can simply use
1216 * that below for the desired_bw_khz below.
1217 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001218static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1219 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001220{
1221 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001222 u32 flags, bw_flags = 0;
1223 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001224 const struct ieee80211_reg_rule *reg_rule = NULL;
1225 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001226 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001227 struct ieee80211_supported_band *sband;
1228 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001229 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001230
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001231 assert_cfg80211_lock();
1232
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001233 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1234
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001235 sband = wiphy->bands[band];
1236 BUG_ON(chan_idx >= sband->n_channels);
1237 chan = &sband->channels[chan_idx];
1238
1239 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001240
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001241 r = freq_reg_info(wiphy,
1242 MHZ_TO_KHZ(chan->center_freq),
1243 desired_bw_khz,
1244 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001245
1246 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001247 /*
1248 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001249 * with a frequency range on the center_freq's band, since
1250 * IEEE-802.11 allows for a country IE to have a subset of the
1251 * regulatory information provided in a country we ignore
1252 * disabling the channel unless at least one reg rule was
1253 * found on the center_freq's band. For details see this
1254 * clarification:
1255 *
1256 * http://tinyurl.com/11d-clarification
1257 */
1258 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001259 last_request->initiator ==
1260 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001261 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001262 "intact on %s - no rule found in band on "
1263 "Country IE\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001264 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001265 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001266 /*
1267 * In this case we know the country IE has at least one reg rule
1268 * for the band so we respect its band definitions
1269 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001270 if (last_request->initiator ==
1271 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001272 REG_DBG_PRINT("cfg80211: Disabling "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001273 "channel %d MHz on %s due to "
1274 "Country IE\n",
1275 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001276 flags |= IEEE80211_CHAN_DISABLED;
1277 chan->flags = flags;
1278 }
Johannes Berg8318d782008-01-24 19:38:38 +01001279 return;
1280 }
1281
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001282 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001283 freq_range = &reg_rule->freq_range;
1284
1285 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1286 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001287
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001288 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001289 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001290 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001291 /*
1292 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001293 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001294 * settings
1295 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001296 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001297 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001298 chan->max_antenna_gain = chan->orig_mag =
1299 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001300 chan->max_power = chan->orig_mpwr =
1301 (int) MBM_TO_DBM(power_rule->max_eirp);
1302 return;
1303 }
1304
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001305 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001306 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001307 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -04001308 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001309 chan->max_power = min(chan->orig_mpwr,
1310 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001311 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001312 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001313}
1314
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001315static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001316{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001317 unsigned int i;
1318 struct ieee80211_supported_band *sband;
1319
1320 BUG_ON(!wiphy->bands[band]);
1321 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001322
1323 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001324 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001325}
1326
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001327static bool ignore_reg_update(struct wiphy *wiphy,
1328 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001329{
1330 if (!last_request)
1331 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001332 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001333 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001334 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001335 /*
1336 * wiphy->regd will be set once the device has its own
1337 * desired regulatory domain set
1338 */
Johannes Berg5be83de2009-11-19 00:56:28 +01001339 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001340 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001341 return true;
1342 return false;
1343}
1344
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001345static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001346{
Johannes Berg79c97e92009-07-07 03:56:12 +02001347 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001348
Johannes Berg79c97e92009-07-07 03:56:12 +02001349 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1350 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001351}
1352
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001353static void handle_reg_beacon(struct wiphy *wiphy,
1354 unsigned int chan_idx,
1355 struct reg_beacon *reg_beacon)
1356{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001357 struct ieee80211_supported_band *sband;
1358 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001359 bool channel_changed = false;
1360 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001361
1362 assert_cfg80211_lock();
1363
1364 sband = wiphy->bands[reg_beacon->chan.band];
1365 chan = &sband->channels[chan_idx];
1366
1367 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1368 return;
1369
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001370 if (chan->beacon_found)
1371 return;
1372
1373 chan->beacon_found = true;
1374
Johannes Berg5be83de2009-11-19 00:56:28 +01001375 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001376 return;
1377
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001378 chan_before.center_freq = chan->center_freq;
1379 chan_before.flags = chan->flags;
1380
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001381 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001382 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001383 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001384 }
1385
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001386 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001387 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001388 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001389 }
1390
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001391 if (channel_changed)
1392 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001393}
1394
1395/*
1396 * Called when a scan on a wiphy finds a beacon on
1397 * new channel
1398 */
1399static void wiphy_update_new_beacon(struct wiphy *wiphy,
1400 struct reg_beacon *reg_beacon)
1401{
1402 unsigned int i;
1403 struct ieee80211_supported_band *sband;
1404
1405 assert_cfg80211_lock();
1406
1407 if (!wiphy->bands[reg_beacon->chan.band])
1408 return;
1409
1410 sband = wiphy->bands[reg_beacon->chan.band];
1411
1412 for (i = 0; i < sband->n_channels; i++)
1413 handle_reg_beacon(wiphy, i, reg_beacon);
1414}
1415
1416/*
1417 * Called upon reg changes or a new wiphy is added
1418 */
1419static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1420{
1421 unsigned int i;
1422 struct ieee80211_supported_band *sband;
1423 struct reg_beacon *reg_beacon;
1424
1425 assert_cfg80211_lock();
1426
1427 if (list_empty(&reg_beacon_list))
1428 return;
1429
1430 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1431 if (!wiphy->bands[reg_beacon->chan.band])
1432 continue;
1433 sband = wiphy->bands[reg_beacon->chan.band];
1434 for (i = 0; i < sband->n_channels; i++)
1435 handle_reg_beacon(wiphy, i, reg_beacon);
1436 }
1437}
1438
1439static bool reg_is_world_roaming(struct wiphy *wiphy)
1440{
1441 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1442 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1443 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001444 if (last_request &&
1445 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001446 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001447 return true;
1448 return false;
1449}
1450
1451/* Reap the advantages of previously found beacons */
1452static void reg_process_beacons(struct wiphy *wiphy)
1453{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001454 /*
1455 * Means we are just firing up cfg80211, so no beacons would
1456 * have been processed yet.
1457 */
1458 if (!last_request)
1459 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001460 if (!reg_is_world_roaming(wiphy))
1461 return;
1462 wiphy_update_beacon_reg(wiphy);
1463}
1464
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001465static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1466{
1467 if (!chan)
1468 return true;
1469 if (chan->flags & IEEE80211_CHAN_DISABLED)
1470 return true;
1471 /* This would happen when regulatory rules disallow HT40 completely */
1472 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1473 return true;
1474 return false;
1475}
1476
1477static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1478 enum ieee80211_band band,
1479 unsigned int chan_idx)
1480{
1481 struct ieee80211_supported_band *sband;
1482 struct ieee80211_channel *channel;
1483 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1484 unsigned int i;
1485
1486 assert_cfg80211_lock();
1487
1488 sband = wiphy->bands[band];
1489 BUG_ON(chan_idx >= sband->n_channels);
1490 channel = &sband->channels[chan_idx];
1491
1492 if (is_ht40_not_allowed(channel)) {
1493 channel->flags |= IEEE80211_CHAN_NO_HT40;
1494 return;
1495 }
1496
1497 /*
1498 * We need to ensure the extension channels exist to
1499 * be able to use HT40- or HT40+, this finds them (or not)
1500 */
1501 for (i = 0; i < sband->n_channels; i++) {
1502 struct ieee80211_channel *c = &sband->channels[i];
1503 if (c->center_freq == (channel->center_freq - 20))
1504 channel_before = c;
1505 if (c->center_freq == (channel->center_freq + 20))
1506 channel_after = c;
1507 }
1508
1509 /*
1510 * Please note that this assumes target bandwidth is 20 MHz,
1511 * if that ever changes we also need to change the below logic
1512 * to include that as well.
1513 */
1514 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001515 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001516 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001517 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001518
1519 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001520 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001521 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001522 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001523}
1524
1525static void reg_process_ht_flags_band(struct wiphy *wiphy,
1526 enum ieee80211_band band)
1527{
1528 unsigned int i;
1529 struct ieee80211_supported_band *sband;
1530
1531 BUG_ON(!wiphy->bands[band]);
1532 sband = wiphy->bands[band];
1533
1534 for (i = 0; i < sband->n_channels; i++)
1535 reg_process_ht_flags_channel(wiphy, band, i);
1536}
1537
1538static void reg_process_ht_flags(struct wiphy *wiphy)
1539{
1540 enum ieee80211_band band;
1541
1542 if (!wiphy)
1543 return;
1544
1545 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1546 if (wiphy->bands[band])
1547 reg_process_ht_flags_band(wiphy, band);
1548 }
1549
1550}
1551
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001552void wiphy_update_regulatory(struct wiphy *wiphy,
1553 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001554{
1555 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001556
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001557 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001558 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001559 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001560 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001561 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001562 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001563out:
1564 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001565 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001566 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001567 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001568}
1569
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001570static void handle_channel_custom(struct wiphy *wiphy,
1571 enum ieee80211_band band,
1572 unsigned int chan_idx,
1573 const struct ieee80211_regdomain *regd)
1574{
1575 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001576 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1577 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001578 const struct ieee80211_reg_rule *reg_rule = NULL;
1579 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001580 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001581 struct ieee80211_supported_band *sband;
1582 struct ieee80211_channel *chan;
1583
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001584 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001585
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001586 sband = wiphy->bands[band];
1587 BUG_ON(chan_idx >= sband->n_channels);
1588 chan = &sband->channels[chan_idx];
1589
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001590 r = freq_reg_info_regd(wiphy,
1591 MHZ_TO_KHZ(chan->center_freq),
1592 desired_bw_khz,
1593 &reg_rule,
1594 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001595
1596 if (r) {
1597 chan->flags = IEEE80211_CHAN_DISABLED;
1598 return;
1599 }
1600
1601 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001602 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001603
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001604 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1605 bw_flags = IEEE80211_CHAN_NO_HT40;
1606
1607 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001608 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001609 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1610}
1611
1612static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1613 const struct ieee80211_regdomain *regd)
1614{
1615 unsigned int i;
1616 struct ieee80211_supported_band *sband;
1617
1618 BUG_ON(!wiphy->bands[band]);
1619 sband = wiphy->bands[band];
1620
1621 for (i = 0; i < sband->n_channels; i++)
1622 handle_channel_custom(wiphy, band, i, regd);
1623}
1624
1625/* Used by drivers prior to wiphy registration */
1626void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1627 const struct ieee80211_regdomain *regd)
1628{
1629 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001630 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001631
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001632 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001633 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001634 if (!wiphy->bands[band])
1635 continue;
1636 handle_band_custom(wiphy, band, regd);
1637 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001638 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001639 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001640
1641 /*
1642 * no point in calling this if it won't have any effect
1643 * on your device's supportd bands.
1644 */
1645 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001646}
1647EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1648
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001649/*
1650 * Return value which can be used by ignore_request() to indicate
1651 * it has been determined we should intersect two regulatory domains
1652 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001653#define REG_INTERSECT 1
1654
Johannes Berg84fa4f42008-10-24 20:32:23 +02001655/* This has the logic which determines when a new request
1656 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001657static int ignore_request(struct wiphy *wiphy,
1658 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001659{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001660 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001661
1662 assert_cfg80211_lock();
1663
Johannes Berg84fa4f42008-10-24 20:32:23 +02001664 /* All initial requests are respected */
1665 if (!last_request)
1666 return 0;
1667
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001668 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001669 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001670 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001671 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001672
1673 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1674
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001675 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001676 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001677 if (last_request->initiator ==
1678 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001679 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001680 /*
1681 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001682 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001683 * intersect them, but that seems unlikely
1684 * to be correct. Reject second one for now.
1685 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001686 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001687 return -EOPNOTSUPP;
1688 return -EALREADY;
1689 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001690 /*
1691 * Two consecutive Country IE hints on the same wiphy.
1692 * This should be picked up early by the driver/stack
1693 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001694 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001695 return 0;
1696 return -EALREADY;
1697 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001698 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001699 case NL80211_REGDOM_SET_BY_DRIVER:
1700 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001701 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001702 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001703 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001704 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001705
1706 /*
1707 * This would happen if you unplug and plug your card
1708 * back in or if you add a new device for which the previously
1709 * loaded card also agrees on the regulatory domain.
1710 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001711 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001712 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001713 return -EALREADY;
1714
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001715 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001716 case NL80211_REGDOM_SET_BY_USER:
1717 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001718 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001719 /*
1720 * If the user knows better the user should set the regdom
1721 * to their country before the IE is picked up
1722 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001723 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001724 last_request->intersect)
1725 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001726 /*
1727 * Process user requests only after previous user/driver/core
1728 * requests have been processed
1729 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001730 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1731 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1732 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001733 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001734 return -EAGAIN;
1735 }
1736
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001737 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001738 return -EALREADY;
1739
Johannes Berg84fa4f42008-10-24 20:32:23 +02001740 return 0;
1741 }
1742
1743 return -EINVAL;
1744}
1745
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001746/**
1747 * __regulatory_hint - hint to the wireless core a regulatory domain
1748 * @wiphy: if the hint comes from country information from an AP, this
1749 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001750 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001751 *
1752 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001753 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001754 *
1755 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1756 * already been set or other standard error codes.
1757 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001758 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001759 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001760static int __regulatory_hint(struct wiphy *wiphy,
1761 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001762{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001763 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001764 int r = 0;
1765
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001766 assert_cfg80211_lock();
1767
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001768 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001769
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001770 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001771 if (pending_request->initiator ==
1772 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001773 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001774 if (r) {
1775 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001776 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001777 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001778 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001779 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001780 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001781 /*
1782 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001783 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001784 * wiphy
1785 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001786 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001787 pending_request->initiator ==
1788 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001789 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001790 if (r) {
1791 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001792 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001793 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001794 r = -EALREADY;
1795 goto new_request;
1796 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001797 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001798 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001799 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001800
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001801new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001802 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001803
1804 last_request = pending_request;
1805 last_request->intersect = intersect;
1806
1807 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001808
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001809 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1810 user_alpha2[0] = last_request->alpha2[0];
1811 user_alpha2[1] = last_request->alpha2[1];
1812 }
1813
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001814 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001815 if (r < 0) {
1816 /*
1817 * Since CRDA will not be called in this case as we already
1818 * have applied the requested regulatory domain before we just
1819 * inform userspace we have processed the request
1820 */
1821 if (r == -EALREADY)
1822 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001823 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001824 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001825
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001826 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001827}
1828
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001829/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001830static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001831{
1832 int r = 0;
1833 struct wiphy *wiphy = NULL;
1834
1835 BUG_ON(!reg_request->alpha2);
1836
1837 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001838 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001839
1840 if (wiphy_idx_valid(reg_request->wiphy_idx))
1841 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1842
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001843 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001844 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001845 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001846 goto out;
1847 }
1848
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001849 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001850 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001851 if (r == -EALREADY && wiphy &&
1852 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001853 wiphy_update_regulatory(wiphy, reg_request->initiator);
1854out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001855 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001856 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001857}
1858
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001859/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001860static void reg_process_pending_hints(void)
1861 {
1862 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001863
1864 spin_lock(&reg_requests_lock);
1865 while (!list_empty(&reg_requests_list)) {
1866 reg_request = list_first_entry(&reg_requests_list,
1867 struct regulatory_request,
1868 list);
1869 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001870
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001871 spin_unlock(&reg_requests_lock);
1872 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001873 spin_lock(&reg_requests_lock);
1874 }
1875 spin_unlock(&reg_requests_lock);
1876}
1877
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001878/* Processes beacon hints -- this has nothing to do with country IEs */
1879static void reg_process_pending_beacon_hints(void)
1880{
Johannes Berg79c97e92009-07-07 03:56:12 +02001881 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001882 struct reg_beacon *pending_beacon, *tmp;
1883
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001884 /*
1885 * No need to hold the reg_mutex here as we just touch wiphys
1886 * and do not read or access regulatory variables.
1887 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001888 mutex_lock(&cfg80211_mutex);
1889
1890 /* This goes through the _pending_ beacon list */
1891 spin_lock_bh(&reg_pending_beacons_lock);
1892
1893 if (list_empty(&reg_pending_beacons)) {
1894 spin_unlock_bh(&reg_pending_beacons_lock);
1895 goto out;
1896 }
1897
1898 list_for_each_entry_safe(pending_beacon, tmp,
1899 &reg_pending_beacons, list) {
1900
1901 list_del_init(&pending_beacon->list);
1902
1903 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001904 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1905 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001906
1907 /* Remembers the beacon hint for new wiphys or reg changes */
1908 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1909 }
1910
1911 spin_unlock_bh(&reg_pending_beacons_lock);
1912out:
1913 mutex_unlock(&cfg80211_mutex);
1914}
1915
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001916static void reg_todo(struct work_struct *work)
1917{
1918 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001919 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001920}
1921
1922static DECLARE_WORK(reg_work, reg_todo);
1923
1924static void queue_regulatory_request(struct regulatory_request *request)
1925{
1926 spin_lock(&reg_requests_lock);
1927 list_add_tail(&request->list, &reg_requests_list);
1928 spin_unlock(&reg_requests_lock);
1929
1930 schedule_work(&reg_work);
1931}
1932
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001933/*
1934 * Core regulatory hint -- happens during cfg80211_init()
1935 * and when we restore regulatory settings.
1936 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001937static int regulatory_hint_core(const char *alpha2)
1938{
1939 struct regulatory_request *request;
1940
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001941 kfree(last_request);
1942 last_request = NULL;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001943
1944 request = kzalloc(sizeof(struct regulatory_request),
1945 GFP_KERNEL);
1946 if (!request)
1947 return -ENOMEM;
1948
1949 request->alpha2[0] = alpha2[0];
1950 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001951 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001952
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001953 /*
1954 * This ensures last_request is populated once modules
1955 * come swinging in and calling regulatory hints and
1956 * wiphy_apply_custom_regulatory().
1957 */
Luis R. Rodrigueza2bff262010-01-29 19:58:56 -05001958 reg_process_hint(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001959
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001960 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001961}
1962
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001963/* User hints */
1964int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001965{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001966 struct regulatory_request *request;
1967
Johannes Bergbe3d4812008-10-24 20:32:21 +02001968 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001969
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001970 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1971 if (!request)
1972 return -ENOMEM;
1973
1974 request->wiphy_idx = WIPHY_IDX_STALE;
1975 request->alpha2[0] = alpha2[0];
1976 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001977 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001978
1979 queue_regulatory_request(request);
1980
1981 return 0;
1982}
1983
1984/* Driver hints */
1985int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1986{
1987 struct regulatory_request *request;
1988
1989 BUG_ON(!alpha2);
1990 BUG_ON(!wiphy);
1991
1992 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1993 if (!request)
1994 return -ENOMEM;
1995
1996 request->wiphy_idx = get_wiphy_idx(wiphy);
1997
1998 /* Must have registered wiphy first */
1999 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
2000
2001 request->alpha2[0] = alpha2[0];
2002 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002003 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002004
2005 queue_regulatory_request(request);
2006
2007 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002008}
2009EXPORT_SYMBOL(regulatory_hint);
2010
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002011/* Caller must hold reg_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002012static bool reg_same_country_ie_hint(struct wiphy *wiphy,
2013 u32 country_ie_checksum)
2014{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002015 struct wiphy *request_wiphy;
2016
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002017 assert_reg_lock();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002018
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04002019 if (unlikely(last_request->initiator !=
2020 NL80211_REGDOM_SET_BY_COUNTRY_IE))
2021 return false;
2022
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002023 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2024
2025 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002026 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002027
2028 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002029 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002030 /*
2031 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002032 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002033 * on the device
2034 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002035 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
2036 return true;
2037 return false;
2038}
2039
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002040/*
2041 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
2042 * therefore cannot iterate over the rdev list here.
2043 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002044void regulatory_hint_11d(struct wiphy *wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05002045 enum ieee80211_band band,
2046 u8 *country_ie,
2047 u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002048{
2049 struct ieee80211_regdomain *rd = NULL;
2050 char alpha2[2];
2051 u32 checksum = 0;
2052 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002053 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002054
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002055 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002056
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07002057 if (unlikely(!last_request))
2058 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002059
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002060 /* IE len must be evenly divisible by 2 */
2061 if (country_ie_len & 0x01)
2062 goto out;
2063
2064 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2065 goto out;
2066
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002067 /*
2068 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002069 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002070 * we were able to process the last regulatory_hint_11d() call
2071 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002072 if (country_ie_regdomain)
2073 goto out;
2074
2075 alpha2[0] = country_ie[0];
2076 alpha2[1] = country_ie[1];
2077
2078 if (country_ie[2] == 'I')
2079 env = ENVIRON_INDOOR;
2080 else if (country_ie[2] == 'O')
2081 env = ENVIRON_OUTDOOR;
2082
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002083 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002084 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002085 * We leave conflict resolution to the workqueue, where can hold
2086 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002087 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04002088 if (likely(last_request->initiator ==
2089 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002090 wiphy_idx_valid(last_request->wiphy_idx)))
2091 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002092
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05002093 rd = country_ie_2_rd(band, country_ie, country_ie_len, &checksum);
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002094 if (!rd) {
2095 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002096 goto out;
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002097 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002098
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002099 /*
2100 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002101 * the future when we want to add suspend/resume support and having
2102 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002103 * move to another AP. Right now we just trust the first AP.
2104 *
2105 * If we hit this before we add this support we want to be informed of
2106 * it as it would indicate a mistake in the current design
2107 */
2108 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002109 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002110
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002111 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2112 if (!request)
2113 goto free_rd_out;
2114
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002115 /*
2116 * We keep this around for when CRDA comes back with a response so
2117 * we can intersect with that
2118 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002119 country_ie_regdomain = rd;
2120
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002121 request->wiphy_idx = get_wiphy_idx(wiphy);
2122 request->alpha2[0] = rd->alpha2[0];
2123 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002124 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002125 request->country_ie_checksum = checksum;
2126 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002127
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002128 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002129
2130 queue_regulatory_request(request);
2131
2132 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002133
2134free_rd_out:
2135 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002136out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002137 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002138}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002139
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002140static void restore_alpha2(char *alpha2, bool reset_user)
2141{
2142 /* indicates there is no alpha2 to consider for restoration */
2143 alpha2[0] = '9';
2144 alpha2[1] = '7';
2145
2146 /* The user setting has precedence over the module parameter */
2147 if (is_user_regdom_saved()) {
2148 /* Unless we're asked to ignore it and reset it */
2149 if (reset_user) {
2150 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2151 "including user preference\n");
2152 user_alpha2[0] = '9';
2153 user_alpha2[1] = '7';
2154
2155 /*
2156 * If we're ignoring user settings, we still need to
2157 * check the module parameter to ensure we put things
2158 * back as they were for a full restore.
2159 */
2160 if (!is_world_regdom(ieee80211_regdom)) {
2161 REG_DBG_PRINT("cfg80211: Keeping preference on "
2162 "module parameter ieee80211_regdom: %c%c\n",
2163 ieee80211_regdom[0],
2164 ieee80211_regdom[1]);
2165 alpha2[0] = ieee80211_regdom[0];
2166 alpha2[1] = ieee80211_regdom[1];
2167 }
2168 } else {
2169 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2170 "while preserving user preference for: %c%c\n",
2171 user_alpha2[0],
2172 user_alpha2[1]);
2173 alpha2[0] = user_alpha2[0];
2174 alpha2[1] = user_alpha2[1];
2175 }
2176 } else if (!is_world_regdom(ieee80211_regdom)) {
2177 REG_DBG_PRINT("cfg80211: Keeping preference on "
2178 "module parameter ieee80211_regdom: %c%c\n",
2179 ieee80211_regdom[0],
2180 ieee80211_regdom[1]);
2181 alpha2[0] = ieee80211_regdom[0];
2182 alpha2[1] = ieee80211_regdom[1];
2183 } else
2184 REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
2185}
2186
2187/*
2188 * Restoring regulatory settings involves ingoring any
2189 * possibly stale country IE information and user regulatory
2190 * settings if so desired, this includes any beacon hints
2191 * learned as we could have traveled outside to another country
2192 * after disconnection. To restore regulatory settings we do
2193 * exactly what we did at bootup:
2194 *
2195 * - send a core regulatory hint
2196 * - send a user regulatory hint if applicable
2197 *
2198 * Device drivers that send a regulatory hint for a specific country
2199 * keep their own regulatory domain on wiphy->regd so that does does
2200 * not need to be remembered.
2201 */
2202static void restore_regulatory_settings(bool reset_user)
2203{
2204 char alpha2[2];
2205 struct reg_beacon *reg_beacon, *btmp;
2206
2207 mutex_lock(&cfg80211_mutex);
2208 mutex_lock(&reg_mutex);
2209
2210 reset_regdomains();
2211 restore_alpha2(alpha2, reset_user);
2212
2213 /* Clear beacon hints */
2214 spin_lock_bh(&reg_pending_beacons_lock);
2215 if (!list_empty(&reg_pending_beacons)) {
2216 list_for_each_entry_safe(reg_beacon, btmp,
2217 &reg_pending_beacons, list) {
2218 list_del(&reg_beacon->list);
2219 kfree(reg_beacon);
2220 }
2221 }
2222 spin_unlock_bh(&reg_pending_beacons_lock);
2223
2224 if (!list_empty(&reg_beacon_list)) {
2225 list_for_each_entry_safe(reg_beacon, btmp,
2226 &reg_beacon_list, list) {
2227 list_del(&reg_beacon->list);
2228 kfree(reg_beacon);
2229 }
2230 }
2231
2232 /* First restore to the basic regulatory settings */
2233 cfg80211_regdomain = cfg80211_world_regdom;
2234
2235 mutex_unlock(&reg_mutex);
2236 mutex_unlock(&cfg80211_mutex);
2237
2238 regulatory_hint_core(cfg80211_regdomain->alpha2);
2239
2240 /*
2241 * This restores the ieee80211_regdom module parameter
2242 * preference or the last user requested regulatory
2243 * settings, user regulatory settings takes precedence.
2244 */
2245 if (is_an_alpha2(alpha2))
2246 regulatory_hint_user(user_alpha2);
2247}
2248
2249
2250void regulatory_hint_disconnect(void)
2251{
2252 REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
2253 "restore regulatory settings\n");
2254 restore_regulatory_settings(false);
2255}
2256
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002257static bool freq_is_chan_12_13_14(u16 freq)
2258{
2259 if (freq == ieee80211_channel_to_frequency(12) ||
2260 freq == ieee80211_channel_to_frequency(13) ||
2261 freq == ieee80211_channel_to_frequency(14))
2262 return true;
2263 return false;
2264}
2265
2266int regulatory_hint_found_beacon(struct wiphy *wiphy,
2267 struct ieee80211_channel *beacon_chan,
2268 gfp_t gfp)
2269{
2270 struct reg_beacon *reg_beacon;
2271
2272 if (likely((beacon_chan->beacon_found ||
2273 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
2274 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
2275 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
2276 return 0;
2277
2278 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2279 if (!reg_beacon)
2280 return -ENOMEM;
2281
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002282 REG_DBG_PRINT("cfg80211: Found new beacon on "
2283 "frequency: %d MHz (Ch %d) on %s\n",
2284 beacon_chan->center_freq,
2285 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2286 wiphy_name(wiphy));
2287
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002288 memcpy(&reg_beacon->chan, beacon_chan,
2289 sizeof(struct ieee80211_channel));
2290
2291
2292 /*
2293 * Since we can be called from BH or and non-BH context
2294 * we must use spin_lock_bh()
2295 */
2296 spin_lock_bh(&reg_pending_beacons_lock);
2297 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2298 spin_unlock_bh(&reg_pending_beacons_lock);
2299
2300 schedule_work(&reg_work);
2301
2302 return 0;
2303}
2304
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002305static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002306{
2307 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002308 const struct ieee80211_reg_rule *reg_rule = NULL;
2309 const struct ieee80211_freq_range *freq_range = NULL;
2310 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002311
Kalle Valo269ac5f2009-12-01 10:47:15 +02002312 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002313 "(max_antenna_gain, max_eirp)\n");
2314
2315 for (i = 0; i < rd->n_reg_rules; i++) {
2316 reg_rule = &rd->reg_rules[i];
2317 freq_range = &reg_rule->freq_range;
2318 power_rule = &reg_rule->power_rule;
2319
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002320 /*
2321 * There may not be documentation for max antenna gain
2322 * in certain regions
2323 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002324 if (power_rule->max_antenna_gain)
Kalle Valo269ac5f2009-12-01 10:47:15 +02002325 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002326 "(%d mBi, %d mBm)\n",
2327 freq_range->start_freq_khz,
2328 freq_range->end_freq_khz,
2329 freq_range->max_bandwidth_khz,
2330 power_rule->max_antenna_gain,
2331 power_rule->max_eirp);
2332 else
Kalle Valo269ac5f2009-12-01 10:47:15 +02002333 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002334 "(N/A, %d mBm)\n",
2335 freq_range->start_freq_khz,
2336 freq_range->end_freq_khz,
2337 freq_range->max_bandwidth_khz,
2338 power_rule->max_eirp);
2339 }
2340}
2341
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002342static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002343{
2344
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002345 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002346
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002347 if (last_request->initiator ==
2348 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002349 struct cfg80211_registered_device *rdev;
2350 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002351 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002352 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002353 printk(KERN_INFO "cfg80211: Current regulatory "
2354 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002355 rdev->country_ie_alpha2[0],
2356 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002357 } else
2358 printk(KERN_INFO "cfg80211: Current regulatory "
Frans Pop55f98932010-03-24 19:46:29 +01002359 "domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002360 } else
Frans Pop55f98932010-03-24 19:46:29 +01002361 printk(KERN_INFO "cfg80211: Current regulatory "
2362 "domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002363 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002364 printk(KERN_INFO "cfg80211: World regulatory "
2365 "domain updated:\n");
2366 else {
2367 if (is_unknown_alpha2(rd->alpha2))
2368 printk(KERN_INFO "cfg80211: Regulatory domain "
2369 "changed to driver built-in settings "
2370 "(unknown country)\n");
2371 else
2372 printk(KERN_INFO "cfg80211: Regulatory domain "
2373 "changed to country: %c%c\n",
2374 rd->alpha2[0], rd->alpha2[1]);
2375 }
2376 print_rd_rules(rd);
2377}
2378
Johannes Berg2df78162008-10-28 16:49:41 +01002379static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002380{
2381 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2382 rd->alpha2[0], rd->alpha2[1]);
2383 print_rd_rules(rd);
2384}
2385
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002386#ifdef CONFIG_CFG80211_REG_DEBUG
2387static void reg_country_ie_process_debug(
2388 const struct ieee80211_regdomain *rd,
2389 const struct ieee80211_regdomain *country_ie_regdomain,
2390 const struct ieee80211_regdomain *intersected_rd)
2391{
2392 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2393 print_regdomain_info(country_ie_regdomain);
2394 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2395 print_regdomain_info(rd);
2396 if (intersected_rd) {
2397 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2398 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08002399 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002400 return;
2401 }
2402 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2403}
2404#else
2405static inline void reg_country_ie_process_debug(
2406 const struct ieee80211_regdomain *rd,
2407 const struct ieee80211_regdomain *country_ie_regdomain,
2408 const struct ieee80211_regdomain *intersected_rd)
2409{
2410}
2411#endif
2412
Johannes Bergd2372b32008-10-24 20:32:20 +02002413/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002414static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002415{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002416 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002417 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002418 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002419 /* Some basic sanity checks first */
2420
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002421 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002422 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002423 return -EINVAL;
2424 update_world_regdomain(rd);
2425 return 0;
2426 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002427
2428 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2429 !is_unknown_alpha2(rd->alpha2))
2430 return -EINVAL;
2431
Johannes Bergf6037d02008-10-21 11:01:33 +02002432 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002433 return -EINVAL;
2434
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002435 /*
2436 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002437 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002438 * and the pending request came in from a country IE
2439 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002440 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002441 /*
2442 * If someone else asked us to change the rd lets only bother
2443 * checking if the alpha2 changes if CRDA was already called
2444 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002445 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002446 return -EINVAL;
2447 }
2448
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002449 /*
2450 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002451 * and finally inform them of what we have done, in case they want
2452 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002453 * internal EEPROM data
2454 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002455
Johannes Bergf6037d02008-10-21 11:01:33 +02002456 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002457 return -EINVAL;
2458
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002459 if (!is_valid_rd(rd)) {
2460 printk(KERN_ERR "cfg80211: Invalid "
2461 "regulatory domain detected:\n");
2462 print_regdomain_info(rd);
2463 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002464 }
2465
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002466 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2467
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002468 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002469 int r;
2470
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002471 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002472 reset_regdomains();
2473 cfg80211_regdomain = rd;
2474 return 0;
2475 }
2476
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002477 /*
2478 * For a driver hint, lets copy the regulatory domain the
2479 * driver wanted to the wiphy to deal with conflicts
2480 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002481
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002482 /*
2483 * Userspace could have sent two replies with only
2484 * one kernel request.
2485 */
2486 if (request_wiphy->regd)
2487 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002488
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002489 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002490 if (r)
2491 return r;
2492
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002493 reset_regdomains();
2494 cfg80211_regdomain = rd;
2495 return 0;
2496 }
2497
2498 /* Intersection requires a bit more work */
2499
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002500 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002501
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002502 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2503 if (!intersected_rd)
2504 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002505
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002506 /*
2507 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002508 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002509 * domain we keep it for its private use
2510 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002511 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002512 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002513 else
2514 kfree(rd);
2515
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002516 rd = NULL;
2517
2518 reset_regdomains();
2519 cfg80211_regdomain = intersected_rd;
2520
2521 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002522 }
2523
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002524 /*
2525 * Country IE requests are handled a bit differently, we intersect
2526 * the country IE rd with what CRDA believes that country should have
2527 */
2528
Luis R. Rodriguez729e9c72009-05-31 18:24:34 -04002529 /*
2530 * Userspace could have sent two replies with only
2531 * one kernel request. By the second reply we would have
2532 * already processed and consumed the country_ie_regdomain.
2533 */
2534 if (!country_ie_regdomain)
2535 return -EALREADY;
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002536 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002537
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002538 /*
2539 * Intersect what CRDA returned and our what we
2540 * had built from the Country IE received
2541 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002542
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002543 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002544
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002545 reg_country_ie_process_debug(rd,
2546 country_ie_regdomain,
2547 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002548
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002549 kfree(country_ie_regdomain);
2550 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002551
2552 if (!intersected_rd)
2553 return -EINVAL;
2554
Johannes Berg79c97e92009-07-07 03:56:12 +02002555 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002556
Johannes Berg79c97e92009-07-07 03:56:12 +02002557 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2558 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2559 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002560
2561 BUG_ON(intersected_rd == rd);
2562
2563 kfree(rd);
2564 rd = NULL;
2565
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002566 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002567 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002568
2569 return 0;
2570}
2571
2572
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002573/*
2574 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002575 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002576 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2577 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002578int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002579{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002580 int r;
2581
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002582 assert_cfg80211_lock();
2583
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002584 mutex_lock(&reg_mutex);
2585
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002586 /* Note that this doesn't update the wiphys, this is done below */
2587 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002588 if (r) {
2589 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002590 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002591 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002592 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002593
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002594 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002595 if (!last_request->intersect)
2596 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002597
2598 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002599 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002600
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002601 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002602
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002603 nl80211_send_reg_change_event(last_request);
2604
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002605 mutex_unlock(&reg_mutex);
2606
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002607 return r;
2608}
2609
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002610/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002611void reg_device_remove(struct wiphy *wiphy)
2612{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002613 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002614
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002615 assert_cfg80211_lock();
2616
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002617 mutex_lock(&reg_mutex);
2618
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002619 kfree(wiphy->regd);
2620
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002621 if (last_request)
2622 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002623
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002624 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002625 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002626
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002627 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002628 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002629out:
2630 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002631}
2632
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002633int __init regulatory_init(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002634{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002635 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002636
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002637 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2638 if (IS_ERR(reg_pdev))
2639 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002640
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002641 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002642 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002643
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002644 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002645
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002646 user_alpha2[0] = '9';
2647 user_alpha2[1] = '7';
2648
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002649 /* We always try to get an update for the static regdomain */
2650 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002651 if (err) {
2652 if (err == -ENOMEM)
2653 return err;
2654 /*
2655 * N.B. kobject_uevent_env() can fail mainly for when we're out
2656 * memory which is handled and propagated appropriately above
2657 * but it can also fail during a netlink_broadcast() or during
2658 * early boot for call_usermodehelper(). For now treat these
2659 * errors as non-fatal.
2660 */
2661 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2662 "to call CRDA during init");
2663#ifdef CONFIG_CFG80211_REG_DEBUG
2664 /* We want to find out exactly why when debugging */
2665 WARN_ON(err);
2666#endif
2667 }
Johannes Berg734366d2008-09-15 10:56:48 +02002668
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002669 /*
2670 * Finally, if the user set the module parameter treat it
2671 * as a user hint.
2672 */
2673 if (!is_world_regdom(ieee80211_regdom))
2674 regulatory_hint_user(ieee80211_regdom);
2675
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002676 return 0;
2677}
2678
Uwe Kleine-König2fcc9f72010-06-18 09:38:55 +02002679void /* __init_or_exit */ regulatory_exit(void)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002680{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002681 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002682 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002683
2684 cancel_work_sync(&reg_work);
2685
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002686 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002687 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002688
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002689 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002690
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002691 kfree(country_ie_regdomain);
2692 country_ie_regdomain = NULL;
2693
Johannes Bergf6037d02008-10-21 11:01:33 +02002694 kfree(last_request);
2695
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002696 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002697
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002698 spin_lock_bh(&reg_pending_beacons_lock);
2699 if (!list_empty(&reg_pending_beacons)) {
2700 list_for_each_entry_safe(reg_beacon, btmp,
2701 &reg_pending_beacons, list) {
2702 list_del(&reg_beacon->list);
2703 kfree(reg_beacon);
2704 }
2705 }
2706 spin_unlock_bh(&reg_pending_beacons_lock);
2707
2708 if (!list_empty(&reg_beacon_list)) {
2709 list_for_each_entry_safe(reg_beacon, btmp,
2710 &reg_beacon_list, list) {
2711 list_del(&reg_beacon->list);
2712 kfree(reg_beacon);
2713 }
2714 }
2715
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002716 spin_lock(&reg_requests_lock);
2717 if (!list_empty(&reg_requests_list)) {
2718 list_for_each_entry_safe(reg_request, tmp,
2719 &reg_requests_list, list) {
2720 list_del(&reg_request->list);
2721 kfree(reg_request);
2722 }
2723 }
2724 spin_unlock(&reg_requests_lock);
2725
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002726 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002727 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002728}