blob: b7604b823f46c38a7adfed41ad8d612edf2cf82e [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);
328static DEFINE_SPINLOCK(reg_regdb_search_lock);
329
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
336 spin_lock(&reg_regdb_search_lock);
337 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;
350 spin_unlock(&reg_regdb_search_lock);
351 mutex_lock(&cfg80211_mutex);
352 set_regdom(regdom);
353 mutex_unlock(&cfg80211_mutex);
354 spin_lock(&reg_regdb_search_lock);
355 break;
356 }
357 }
358
359 kfree(request);
360 }
361 spin_unlock(&reg_regdb_search_lock);
362}
363
364static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
365
366static void reg_regdb_query(const char *alpha2)
367{
368 struct reg_regdb_search_request *request;
369
370 if (!alpha2)
371 return;
372
373 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
374 if (!request)
375 return;
376
377 memcpy(request->alpha2, alpha2, 2);
378
379 spin_lock(&reg_regdb_search_lock);
380 list_add_tail(&request->list, &reg_regdb_search_list);
381 spin_unlock(&reg_regdb_search_lock);
382
383 schedule_work(&reg_regdb_work);
384}
385#else
386static inline void reg_regdb_query(const char *alpha2) {}
387#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
388
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500389/*
390 * This lets us keep regulatory code which is updated on a regulatory
391 * basis in userspace.
392 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700393static int call_crda(const char *alpha2)
394{
395 char country_env[9 + 2] = "COUNTRY=";
396 char *envp[] = {
397 country_env,
398 NULL
399 };
400
401 if (!is_world_regdom((char *) alpha2))
402 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
403 alpha2[0], alpha2[1]);
404 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700405 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
406 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700407
John W. Linville3b377ea2009-12-18 17:59:01 -0500408 /* query internal regulatory database (if it exists) */
409 reg_regdb_query(alpha2);
410
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700411 country_env[8] = alpha2[0];
412 country_env[9] = alpha2[1];
413
414 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
415}
416
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700417/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200418bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700419{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400420 assert_cfg80211_lock();
421
Johannes Bergf6037d02008-10-21 11:01:33 +0200422 if (!last_request)
423 return false;
424
425 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700426}
427
428/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200429static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700430{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200431 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700432 u32 freq_diff;
433
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800434 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700435 return false;
436
437 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
438 return false;
439
440 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
441
Roel Kluinbd05f282009-03-03 22:55:21 +0100442 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
443 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700444 return false;
445
446 return true;
447}
448
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200449static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700450{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200451 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700452 unsigned int i;
453
454 if (!rd->n_reg_rules)
455 return false;
456
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800457 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
458 return false;
459
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700460 for (i = 0; i < rd->n_reg_rules; i++) {
461 reg_rule = &rd->reg_rules[i];
462 if (!is_valid_reg_rule(reg_rule))
463 return false;
464 }
465
466 return true;
467}
468
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400469static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
470 u32 center_freq_khz,
471 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700472{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400473 u32 start_freq_khz, end_freq_khz;
474
475 start_freq_khz = center_freq_khz - (bw_khz/2);
476 end_freq_khz = center_freq_khz + (bw_khz/2);
477
478 if (start_freq_khz >= freq_range->start_freq_khz &&
479 end_freq_khz <= freq_range->end_freq_khz)
480 return true;
481
482 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700483}
484
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800485/**
486 * freq_in_rule_band - tells us if a frequency is in a frequency band
487 * @freq_range: frequency rule we want to query
488 * @freq_khz: frequency we are inquiring about
489 *
490 * This lets us know if a specific frequency rule is or is not relevant to
491 * a specific frequency's band. Bands are device specific and artificial
492 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
493 * safe for now to assume that a frequency rule should not be part of a
494 * frequency's band if the start freq or end freq are off by more than 2 GHz.
495 * This resolution can be lowered and should be considered as we add
496 * regulatory rule support for other "bands".
497 **/
498static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
499 u32 freq_khz)
500{
501#define ONE_GHZ_IN_KHZ 1000000
502 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
503 return true;
504 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
505 return true;
506 return false;
507#undef ONE_GHZ_IN_KHZ
508}
509
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500510/*
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500511 * This is a work around for sanity checking ieee80211_channel_to_frequency()'s
512 * work. ieee80211_channel_to_frequency() can for example currently provide a
513 * 2 GHz channel when in fact a 5 GHz channel was desired. An example would be
514 * an AP providing channel 8 on a country IE triplet when it sent this on the
515 * 5 GHz band, that channel is designed to be channel 8 on 5 GHz, not a 2 GHz
516 * channel.
517 *
518 * This can be removed once ieee80211_channel_to_frequency() takes in a band.
519 */
520static bool chan_in_band(int chan, enum ieee80211_band band)
521{
522 int center_freq = ieee80211_channel_to_frequency(chan);
523
524 switch (band) {
525 case IEEE80211_BAND_2GHZ:
526 if (center_freq <= 2484)
527 return true;
528 return false;
529 case IEEE80211_BAND_5GHZ:
530 if (center_freq >= 5005)
531 return true;
532 return false;
533 default:
534 return false;
535 }
536}
537
538/*
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500539 * Some APs may send a country IE triplet for each channel they
540 * support and while this is completely overkill and silly we still
541 * need to support it. We avoid making a single rule for each channel
542 * though and to help us with this we use this helper to find the
543 * actual subband end channel. These type of country IE triplet
544 * scenerios are handled then, all yielding two regulaotry rules from
545 * parsing a country IE:
546 *
547 * [1]
548 * [2]
549 * [36]
550 * [40]
551 *
552 * [1]
553 * [2-4]
554 * [5-12]
555 * [36]
556 * [40-44]
557 *
558 * [1-4]
559 * [5-7]
560 * [36-44]
561 * [48-64]
562 *
563 * [36-36]
564 * [40-40]
565 * [44-44]
566 * [48-48]
567 * [52-52]
568 * [56-56]
569 * [60-60]
570 * [64-64]
571 * [100-100]
572 * [104-104]
573 * [108-108]
574 * [112-112]
575 * [116-116]
576 * [120-120]
577 * [124-124]
578 * [128-128]
579 * [132-132]
580 * [136-136]
581 * [140-140]
582 *
583 * Returns 0 if the IE has been found to be invalid in the middle
584 * somewhere.
585 */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500586static int max_subband_chan(enum ieee80211_band band,
587 int orig_cur_chan,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500588 int orig_end_channel,
589 s8 orig_max_power,
590 u8 **country_ie,
591 u8 *country_ie_len)
592{
593 u8 *triplets_start = *country_ie;
594 u8 len_at_triplet = *country_ie_len;
595 int end_subband_chan = orig_end_channel;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500596
597 /*
598 * We'll deal with padding for the caller unless
599 * its not immediate and we don't process any channels
600 */
601 if (*country_ie_len == 1) {
602 *country_ie += 1;
603 *country_ie_len -= 1;
604 return orig_end_channel;
605 }
606
607 /* Move to the next triplet and then start search */
608 *country_ie += 3;
609 *country_ie_len -= 3;
610
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500611 if (!chan_in_band(orig_cur_chan, band))
612 return 0;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500613
614 while (*country_ie_len >= 3) {
615 int end_channel = 0;
616 struct ieee80211_country_ie_triplet *triplet =
617 (struct ieee80211_country_ie_triplet *) *country_ie;
618 int cur_channel = 0, next_expected_chan;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500619
620 /* means last triplet is completely unrelated to this one */
621 if (triplet->ext.reg_extension_id >=
622 IEEE80211_COUNTRY_EXTENSION_ID) {
623 *country_ie -= 3;
624 *country_ie_len += 3;
625 break;
626 }
627
628 if (triplet->chans.first_channel == 0) {
629 *country_ie += 1;
630 *country_ie_len -= 1;
631 if (*country_ie_len != 0)
632 return 0;
633 break;
634 }
635
Luis R. Rodrigueza0f2e0f2010-01-14 13:27:46 -0500636 if (triplet->chans.num_channels == 0)
637 return 0;
638
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500639 /* Monitonically increasing channel order */
640 if (triplet->chans.first_channel <= end_subband_chan)
641 return 0;
642
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500643 if (!chan_in_band(triplet->chans.first_channel, band))
644 return 0;
645
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500646 /* 2 GHz */
647 if (triplet->chans.first_channel <= 14) {
648 end_channel = triplet->chans.first_channel +
649 triplet->chans.num_channels - 1;
650 }
651 else {
652 end_channel = triplet->chans.first_channel +
653 (4 * (triplet->chans.num_channels - 1));
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500654 }
655
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500656 if (!chan_in_band(end_channel, band))
657 return 0;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500658
659 if (orig_max_power != triplet->chans.max_power) {
660 *country_ie -= 3;
661 *country_ie_len += 3;
662 break;
663 }
664
665 cur_channel = triplet->chans.first_channel;
666
667 /* The key is finding the right next expected channel */
668 if (band == IEEE80211_BAND_2GHZ)
669 next_expected_chan = end_subband_chan + 1;
670 else
671 next_expected_chan = end_subband_chan + 4;
672
673 if (cur_channel != next_expected_chan) {
674 *country_ie -= 3;
675 *country_ie_len += 3;
676 break;
677 }
678
679 end_subband_chan = end_channel;
680
681 /* Move to the next one */
682 *country_ie += 3;
683 *country_ie_len -= 3;
684
685 /*
686 * Padding needs to be dealt with if we processed
687 * some channels.
688 */
689 if (*country_ie_len == 1) {
690 *country_ie += 1;
691 *country_ie_len -= 1;
692 break;
693 }
694
695 /* If seen, the IE is invalid */
696 if (*country_ie_len == 2)
697 return 0;
698 }
699
700 if (end_subband_chan == orig_end_channel) {
701 *country_ie = triplets_start;
702 *country_ie_len = len_at_triplet;
703 return orig_end_channel;
704 }
705
706 return end_subband_chan;
707}
708
709/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500710 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800711 * structure has a lot of information which the IE doesn't yet have,
712 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500713 * with our userspace regulatory agent to get lower bounds.
714 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800715static struct ieee80211_regdomain *country_ie_2_rd(
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500716 enum ieee80211_band band,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800717 u8 *country_ie,
718 u8 country_ie_len,
719 u32 *checksum)
720{
721 struct ieee80211_regdomain *rd = NULL;
722 unsigned int i = 0;
723 char alpha2[2];
724 u32 flags = 0;
725 u32 num_rules = 0, size_of_regd = 0;
726 u8 *triplets_start = NULL;
727 u8 len_at_triplet = 0;
728 /* the last channel we have registered in a subband (triplet) */
729 int last_sub_max_channel = 0;
730
731 *checksum = 0xDEADBEEF;
732
733 /* Country IE requirements */
734 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
735 country_ie_len & 0x01);
736
737 alpha2[0] = country_ie[0];
738 alpha2[1] = country_ie[1];
739
740 /*
741 * Third octet can be:
742 * 'I' - Indoor
743 * 'O' - Outdoor
744 *
745 * anything else we assume is no restrictions
746 */
747 if (country_ie[2] == 'I')
748 flags = NL80211_RRF_NO_OUTDOOR;
749 else if (country_ie[2] == 'O')
750 flags = NL80211_RRF_NO_INDOOR;
751
752 country_ie += 3;
753 country_ie_len -= 3;
754
755 triplets_start = country_ie;
756 len_at_triplet = country_ie_len;
757
758 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
759
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500760 /*
761 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800762 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500763 * for each channel subband
764 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800765 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800766 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800767 struct ieee80211_country_ie_triplet *triplet =
768 (struct ieee80211_country_ie_triplet *) country_ie;
769 int cur_sub_max_channel = 0, cur_channel = 0;
770
771 if (triplet->ext.reg_extension_id >=
772 IEEE80211_COUNTRY_EXTENSION_ID) {
773 country_ie += 3;
774 country_ie_len -= 3;
775 continue;
776 }
777
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500778 /*
779 * APs can add padding to make length divisible
780 * by two, required by the spec.
781 */
782 if (triplet->chans.first_channel == 0) {
783 country_ie++;
784 country_ie_len--;
785 /* This is expected to be at the very end only */
786 if (country_ie_len != 0)
787 return NULL;
788 break;
789 }
790
Luis R. Rodrigueza0f2e0f2010-01-14 13:27:46 -0500791 if (triplet->chans.num_channels == 0)
792 return NULL;
793
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500794 if (!chan_in_band(triplet->chans.first_channel, band))
795 return NULL;
796
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800797 /* 2 GHz */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500798 if (band == IEEE80211_BAND_2GHZ)
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800799 end_channel = triplet->chans.first_channel +
Luis R. Rodrigueze99c7cd2010-01-07 17:24:55 -0500800 triplet->chans.num_channels - 1;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800801 else
802 /*
803 * 5 GHz -- For example in country IEs if the first
804 * channel given is 36 and the number of channels is 4
805 * then the individual channel numbers defined for the
806 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
807 * and not 36, 37, 38, 39.
808 *
809 * See: http://tinyurl.com/11d-clarification
810 */
811 end_channel = triplet->chans.first_channel +
812 (4 * (triplet->chans.num_channels - 1));
813
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800814 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500815
816 /*
817 * Enhancement for APs that send a triplet for every channel
818 * or for whatever reason sends triplets with multiple channels
819 * separated when in fact they should be together.
820 */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500821 end_channel = max_subband_chan(band,
822 cur_channel,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500823 end_channel,
824 triplet->chans.max_power,
825 &country_ie,
826 &country_ie_len);
827 if (!end_channel)
828 return NULL;
829
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500830 if (!chan_in_band(end_channel, band))
831 return NULL;
832
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800833 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800834
835 /* Basic sanity check */
836 if (cur_sub_max_channel < cur_channel)
837 return NULL;
838
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500839 /*
840 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800841 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500842 * increasing
843 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800844 if (last_sub_max_channel) {
845 if (cur_channel <= last_sub_max_channel)
846 return NULL;
847 if (cur_sub_max_channel <= last_sub_max_channel)
848 return NULL;
849 }
850
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500851 /*
852 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800853 * we can throw ext triplets as part of this soup,
854 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500855 * don't support them
856 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800857 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
858 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
859 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
860
861 last_sub_max_channel = cur_sub_max_channel;
862
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800863 num_rules++;
864
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500865 if (country_ie_len >= 3) {
866 country_ie += 3;
867 country_ie_len -= 3;
868 }
869
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500870 /*
871 * Note: this is not a IEEE requirement but
872 * simply a memory requirement
873 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800874 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
875 return NULL;
876 }
877
878 country_ie = triplets_start;
879 country_ie_len = len_at_triplet;
880
881 size_of_regd = sizeof(struct ieee80211_regdomain) +
882 (num_rules * sizeof(struct ieee80211_reg_rule));
883
884 rd = kzalloc(size_of_regd, GFP_KERNEL);
885 if (!rd)
886 return NULL;
887
888 rd->n_reg_rules = num_rules;
889 rd->alpha2[0] = alpha2[0];
890 rd->alpha2[1] = alpha2[1];
891
892 /* This time around we fill in the rd */
893 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800894 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800895 struct ieee80211_country_ie_triplet *triplet =
896 (struct ieee80211_country_ie_triplet *) country_ie;
897 struct ieee80211_reg_rule *reg_rule = NULL;
898 struct ieee80211_freq_range *freq_range = NULL;
899 struct ieee80211_power_rule *power_rule = NULL;
900
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500901 /*
902 * Must parse if dot11RegulatoryClassesRequired is true,
903 * we don't support this yet
904 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800905 if (triplet->ext.reg_extension_id >=
906 IEEE80211_COUNTRY_EXTENSION_ID) {
907 country_ie += 3;
908 country_ie_len -= 3;
909 continue;
910 }
911
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500912 if (triplet->chans.first_channel == 0) {
913 country_ie++;
914 country_ie_len--;
915 break;
916 }
917
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800918 reg_rule = &rd->reg_rules[i];
919 freq_range = &reg_rule->freq_range;
920 power_rule = &reg_rule->power_rule;
921
922 reg_rule->flags = flags;
923
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800924 /* 2 GHz */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500925 if (band == IEEE80211_BAND_2GHZ)
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800926 end_channel = triplet->chans.first_channel +
Luis R. Rodrigueze99c7cd2010-01-07 17:24:55 -0500927 triplet->chans.num_channels -1;
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800928 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800929 end_channel = triplet->chans.first_channel +
930 (4 * (triplet->chans.num_channels - 1));
931
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500932 end_channel = max_subband_chan(band,
933 triplet->chans.first_channel,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500934 end_channel,
935 triplet->chans.max_power,
936 &country_ie,
937 &country_ie_len);
938
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500939 /*
940 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800941 * the actual band edge, not the center of freq for
942 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500943 * the channels passed
944 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800945 freq_range->start_freq_khz =
946 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
947 triplet->chans.first_channel) - 10);
948 freq_range->end_freq_khz =
949 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800950 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800951
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500952 /*
953 * These are large arbitrary values we use to intersect later.
954 * Increment this if we ever support >= 40 MHz channels
955 * in IEEE 802.11
956 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800957 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
958 power_rule->max_antenna_gain = DBI_TO_MBI(100);
Luis R. Rodriguez08030db2010-01-07 17:24:56 -0500959 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800960
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800961 i++;
962
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500963 if (country_ie_len >= 3) {
964 country_ie += 3;
965 country_ie_len -= 3;
966 }
967
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800968 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
969 }
970
971 return rd;
972}
973
974
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500975/*
976 * Helper for regdom_intersect(), this does the real
977 * mathematical intersection fun
978 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700979static int reg_rules_intersect(
980 const struct ieee80211_reg_rule *rule1,
981 const struct ieee80211_reg_rule *rule2,
982 struct ieee80211_reg_rule *intersected_rule)
983{
984 const struct ieee80211_freq_range *freq_range1, *freq_range2;
985 struct ieee80211_freq_range *freq_range;
986 const struct ieee80211_power_rule *power_rule1, *power_rule2;
987 struct ieee80211_power_rule *power_rule;
988 u32 freq_diff;
989
990 freq_range1 = &rule1->freq_range;
991 freq_range2 = &rule2->freq_range;
992 freq_range = &intersected_rule->freq_range;
993
994 power_rule1 = &rule1->power_rule;
995 power_rule2 = &rule2->power_rule;
996 power_rule = &intersected_rule->power_rule;
997
998 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
999 freq_range2->start_freq_khz);
1000 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
1001 freq_range2->end_freq_khz);
1002 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
1003 freq_range2->max_bandwidth_khz);
1004
1005 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1006 if (freq_range->max_bandwidth_khz > freq_diff)
1007 freq_range->max_bandwidth_khz = freq_diff;
1008
1009 power_rule->max_eirp = min(power_rule1->max_eirp,
1010 power_rule2->max_eirp);
1011 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
1012 power_rule2->max_antenna_gain);
1013
1014 intersected_rule->flags = (rule1->flags | rule2->flags);
1015
1016 if (!is_valid_reg_rule(intersected_rule))
1017 return -EINVAL;
1018
1019 return 0;
1020}
1021
1022/**
1023 * regdom_intersect - do the intersection between two regulatory domains
1024 * @rd1: first regulatory domain
1025 * @rd2: second regulatory domain
1026 *
1027 * Use this function to get the intersection between two regulatory domains.
1028 * Once completed we will mark the alpha2 for the rd as intersected, "98",
1029 * as no one single alpha2 can represent this regulatory domain.
1030 *
1031 * Returns a pointer to the regulatory domain structure which will hold the
1032 * resulting intersection of rules between rd1 and rd2. We will
1033 * kzalloc() this structure for you.
1034 */
1035static struct ieee80211_regdomain *regdom_intersect(
1036 const struct ieee80211_regdomain *rd1,
1037 const struct ieee80211_regdomain *rd2)
1038{
1039 int r, size_of_regd;
1040 unsigned int x, y;
1041 unsigned int num_rules = 0, rule_idx = 0;
1042 const struct ieee80211_reg_rule *rule1, *rule2;
1043 struct ieee80211_reg_rule *intersected_rule;
1044 struct ieee80211_regdomain *rd;
1045 /* This is just a dummy holder to help us count */
1046 struct ieee80211_reg_rule irule;
1047
1048 /* Uses the stack temporarily for counter arithmetic */
1049 intersected_rule = &irule;
1050
1051 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
1052
1053 if (!rd1 || !rd2)
1054 return NULL;
1055
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001056 /*
1057 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001058 * build them. This is to so we can malloc() and free() a
1059 * regdomain once. The reason we use reg_rules_intersect() here
1060 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001061 * All rules that do check out OK are valid.
1062 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001063
1064 for (x = 0; x < rd1->n_reg_rules; x++) {
1065 rule1 = &rd1->reg_rules[x];
1066 for (y = 0; y < rd2->n_reg_rules; y++) {
1067 rule2 = &rd2->reg_rules[y];
1068 if (!reg_rules_intersect(rule1, rule2,
1069 intersected_rule))
1070 num_rules++;
1071 memset(intersected_rule, 0,
1072 sizeof(struct ieee80211_reg_rule));
1073 }
1074 }
1075
1076 if (!num_rules)
1077 return NULL;
1078
1079 size_of_regd = sizeof(struct ieee80211_regdomain) +
1080 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
1081
1082 rd = kzalloc(size_of_regd, GFP_KERNEL);
1083 if (!rd)
1084 return NULL;
1085
1086 for (x = 0; x < rd1->n_reg_rules; x++) {
1087 rule1 = &rd1->reg_rules[x];
1088 for (y = 0; y < rd2->n_reg_rules; y++) {
1089 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001090 /*
1091 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001092 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001093 * a memcpy()
1094 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001095 intersected_rule = &rd->reg_rules[rule_idx];
1096 r = reg_rules_intersect(rule1, rule2,
1097 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001098 /*
1099 * No need to memset here the intersected rule here as
1100 * we're not using the stack anymore
1101 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001102 if (r)
1103 continue;
1104 rule_idx++;
1105 }
1106 }
1107
1108 if (rule_idx != num_rules) {
1109 kfree(rd);
1110 return NULL;
1111 }
1112
1113 rd->n_reg_rules = num_rules;
1114 rd->alpha2[0] = '9';
1115 rd->alpha2[1] = '8';
1116
1117 return rd;
1118}
1119
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001120/*
1121 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1122 * want to just have the channel structure use these
1123 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001124static u32 map_regdom_flags(u32 rd_flags)
1125{
1126 u32 channel_flags = 0;
1127 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
1128 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1129 if (rd_flags & NL80211_RRF_NO_IBSS)
1130 channel_flags |= IEEE80211_CHAN_NO_IBSS;
1131 if (rd_flags & NL80211_RRF_DFS)
1132 channel_flags |= IEEE80211_CHAN_RADAR;
1133 return channel_flags;
1134}
1135
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001136static int freq_reg_info_regd(struct wiphy *wiphy,
1137 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001138 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001139 const struct ieee80211_reg_rule **reg_rule,
1140 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +01001141{
1142 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001143 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001144 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001145 bool bw_fits = false;
1146
1147 if (!desired_bw_khz)
1148 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +01001149
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001150 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001151
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001152 /*
1153 * Follow the driver's regulatory domain, if present, unless a country
1154 * IE has been processed or a user wants to help complaince further
1155 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001156 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1157 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001158 wiphy->regd)
1159 regd = wiphy->regd;
1160
1161 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001162 return -EINVAL;
1163
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001164 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001165 const struct ieee80211_reg_rule *rr;
1166 const struct ieee80211_freq_range *fr = NULL;
1167 const struct ieee80211_power_rule *pr = NULL;
1168
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001169 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001170 fr = &rr->freq_range;
1171 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001172
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001173 /*
1174 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001175 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001176 * not overwrite it once found
1177 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001178 if (!band_rule_found)
1179 band_rule_found = freq_in_rule_band(fr, center_freq);
1180
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001181 bw_fits = reg_does_bw_fit(fr,
1182 center_freq,
1183 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001184
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001185 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001186 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001187 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001188 }
1189 }
1190
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001191 if (!band_rule_found)
1192 return -ERANGE;
1193
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001194 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001195}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -08001196EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001197
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001198int freq_reg_info(struct wiphy *wiphy,
1199 u32 center_freq,
1200 u32 desired_bw_khz,
1201 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001202{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001203 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001204 return freq_reg_info_regd(wiphy,
1205 center_freq,
1206 desired_bw_khz,
1207 reg_rule,
1208 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001209}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001210
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001211/*
1212 * Note that right now we assume the desired channel bandwidth
1213 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1214 * per channel, the primary and the extension channel). To support
1215 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1216 * new ieee80211_channel.target_bw and re run the regulatory check
1217 * on the wiphy with the target_bw specified. Then we can simply use
1218 * that below for the desired_bw_khz below.
1219 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001220static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1221 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001222{
1223 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001224 u32 flags, bw_flags = 0;
1225 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001226 const struct ieee80211_reg_rule *reg_rule = NULL;
1227 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001228 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001229 struct ieee80211_supported_band *sband;
1230 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001231 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001232
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001233 assert_cfg80211_lock();
1234
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001235 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1236
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001237 sband = wiphy->bands[band];
1238 BUG_ON(chan_idx >= sband->n_channels);
1239 chan = &sband->channels[chan_idx];
1240
1241 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001242
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001243 r = freq_reg_info(wiphy,
1244 MHZ_TO_KHZ(chan->center_freq),
1245 desired_bw_khz,
1246 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001247
1248 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001249 /*
1250 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001251 * with a frequency range on the center_freq's band, since
1252 * IEEE-802.11 allows for a country IE to have a subset of the
1253 * regulatory information provided in a country we ignore
1254 * disabling the channel unless at least one reg rule was
1255 * found on the center_freq's band. For details see this
1256 * clarification:
1257 *
1258 * http://tinyurl.com/11d-clarification
1259 */
1260 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001261 last_request->initiator ==
1262 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001263 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001264 "intact on %s - no rule found in band on "
1265 "Country IE\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001266 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001267 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001268 /*
1269 * In this case we know the country IE has at least one reg rule
1270 * for the band so we respect its band definitions
1271 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001272 if (last_request->initiator ==
1273 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001274 REG_DBG_PRINT("cfg80211: Disabling "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001275 "channel %d MHz on %s due to "
1276 "Country IE\n",
1277 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001278 flags |= IEEE80211_CHAN_DISABLED;
1279 chan->flags = flags;
1280 }
Johannes Berg8318d782008-01-24 19:38:38 +01001281 return;
1282 }
1283
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001284 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001285 freq_range = &reg_rule->freq_range;
1286
1287 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1288 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001289
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001290 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001291 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001292 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001293 /*
1294 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001295 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001296 * settings
1297 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001298 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001299 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001300 chan->max_antenna_gain = chan->orig_mag =
1301 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001302 chan->max_power = chan->orig_mpwr =
1303 (int) MBM_TO_DBM(power_rule->max_eirp);
1304 return;
1305 }
1306
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001307 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001308 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001309 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -04001310 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001311 chan->max_power = min(chan->orig_mpwr,
1312 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001313 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001314 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001315}
1316
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001317static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001318{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001319 unsigned int i;
1320 struct ieee80211_supported_band *sband;
1321
1322 BUG_ON(!wiphy->bands[band]);
1323 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001324
1325 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001326 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001327}
1328
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001329static bool ignore_reg_update(struct wiphy *wiphy,
1330 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001331{
1332 if (!last_request)
1333 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001334 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001335 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001336 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001337 /*
1338 * wiphy->regd will be set once the device has its own
1339 * desired regulatory domain set
1340 */
Johannes Berg5be83de2009-11-19 00:56:28 +01001341 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001342 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001343 return true;
1344 return false;
1345}
1346
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001347static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001348{
Johannes Berg79c97e92009-07-07 03:56:12 +02001349 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001350
Johannes Berg79c97e92009-07-07 03:56:12 +02001351 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1352 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001353}
1354
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001355static void handle_reg_beacon(struct wiphy *wiphy,
1356 unsigned int chan_idx,
1357 struct reg_beacon *reg_beacon)
1358{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001359 struct ieee80211_supported_band *sband;
1360 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001361 bool channel_changed = false;
1362 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001363
1364 assert_cfg80211_lock();
1365
1366 sband = wiphy->bands[reg_beacon->chan.band];
1367 chan = &sband->channels[chan_idx];
1368
1369 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1370 return;
1371
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001372 if (chan->beacon_found)
1373 return;
1374
1375 chan->beacon_found = true;
1376
Johannes Berg5be83de2009-11-19 00:56:28 +01001377 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001378 return;
1379
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001380 chan_before.center_freq = chan->center_freq;
1381 chan_before.flags = chan->flags;
1382
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001383 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001384 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001385 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001386 }
1387
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001388 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001389 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001390 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001391 }
1392
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001393 if (channel_changed)
1394 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001395}
1396
1397/*
1398 * Called when a scan on a wiphy finds a beacon on
1399 * new channel
1400 */
1401static void wiphy_update_new_beacon(struct wiphy *wiphy,
1402 struct reg_beacon *reg_beacon)
1403{
1404 unsigned int i;
1405 struct ieee80211_supported_band *sband;
1406
1407 assert_cfg80211_lock();
1408
1409 if (!wiphy->bands[reg_beacon->chan.band])
1410 return;
1411
1412 sband = wiphy->bands[reg_beacon->chan.band];
1413
1414 for (i = 0; i < sband->n_channels; i++)
1415 handle_reg_beacon(wiphy, i, reg_beacon);
1416}
1417
1418/*
1419 * Called upon reg changes or a new wiphy is added
1420 */
1421static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1422{
1423 unsigned int i;
1424 struct ieee80211_supported_band *sband;
1425 struct reg_beacon *reg_beacon;
1426
1427 assert_cfg80211_lock();
1428
1429 if (list_empty(&reg_beacon_list))
1430 return;
1431
1432 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1433 if (!wiphy->bands[reg_beacon->chan.band])
1434 continue;
1435 sband = wiphy->bands[reg_beacon->chan.band];
1436 for (i = 0; i < sband->n_channels; i++)
1437 handle_reg_beacon(wiphy, i, reg_beacon);
1438 }
1439}
1440
1441static bool reg_is_world_roaming(struct wiphy *wiphy)
1442{
1443 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1444 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1445 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001446 if (last_request &&
1447 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001448 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001449 return true;
1450 return false;
1451}
1452
1453/* Reap the advantages of previously found beacons */
1454static void reg_process_beacons(struct wiphy *wiphy)
1455{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001456 /*
1457 * Means we are just firing up cfg80211, so no beacons would
1458 * have been processed yet.
1459 */
1460 if (!last_request)
1461 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001462 if (!reg_is_world_roaming(wiphy))
1463 return;
1464 wiphy_update_beacon_reg(wiphy);
1465}
1466
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001467static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1468{
1469 if (!chan)
1470 return true;
1471 if (chan->flags & IEEE80211_CHAN_DISABLED)
1472 return true;
1473 /* This would happen when regulatory rules disallow HT40 completely */
1474 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1475 return true;
1476 return false;
1477}
1478
1479static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1480 enum ieee80211_band band,
1481 unsigned int chan_idx)
1482{
1483 struct ieee80211_supported_band *sband;
1484 struct ieee80211_channel *channel;
1485 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1486 unsigned int i;
1487
1488 assert_cfg80211_lock();
1489
1490 sband = wiphy->bands[band];
1491 BUG_ON(chan_idx >= sband->n_channels);
1492 channel = &sband->channels[chan_idx];
1493
1494 if (is_ht40_not_allowed(channel)) {
1495 channel->flags |= IEEE80211_CHAN_NO_HT40;
1496 return;
1497 }
1498
1499 /*
1500 * We need to ensure the extension channels exist to
1501 * be able to use HT40- or HT40+, this finds them (or not)
1502 */
1503 for (i = 0; i < sband->n_channels; i++) {
1504 struct ieee80211_channel *c = &sband->channels[i];
1505 if (c->center_freq == (channel->center_freq - 20))
1506 channel_before = c;
1507 if (c->center_freq == (channel->center_freq + 20))
1508 channel_after = c;
1509 }
1510
1511 /*
1512 * Please note that this assumes target bandwidth is 20 MHz,
1513 * if that ever changes we also need to change the below logic
1514 * to include that as well.
1515 */
1516 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001517 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001518 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001519 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001520
1521 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001522 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001523 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001524 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001525}
1526
1527static void reg_process_ht_flags_band(struct wiphy *wiphy,
1528 enum ieee80211_band band)
1529{
1530 unsigned int i;
1531 struct ieee80211_supported_band *sband;
1532
1533 BUG_ON(!wiphy->bands[band]);
1534 sband = wiphy->bands[band];
1535
1536 for (i = 0; i < sband->n_channels; i++)
1537 reg_process_ht_flags_channel(wiphy, band, i);
1538}
1539
1540static void reg_process_ht_flags(struct wiphy *wiphy)
1541{
1542 enum ieee80211_band band;
1543
1544 if (!wiphy)
1545 return;
1546
1547 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1548 if (wiphy->bands[band])
1549 reg_process_ht_flags_band(wiphy, band);
1550 }
1551
1552}
1553
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001554void wiphy_update_regulatory(struct wiphy *wiphy,
1555 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001556{
1557 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001558
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001559 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001560 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001561 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001562 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001563 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001564 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001565out:
1566 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001567 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001568 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001569 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001570}
1571
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001572static void handle_channel_custom(struct wiphy *wiphy,
1573 enum ieee80211_band band,
1574 unsigned int chan_idx,
1575 const struct ieee80211_regdomain *regd)
1576{
1577 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001578 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1579 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001580 const struct ieee80211_reg_rule *reg_rule = NULL;
1581 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001582 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001583 struct ieee80211_supported_band *sband;
1584 struct ieee80211_channel *chan;
1585
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001586 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001587
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001588 sband = wiphy->bands[band];
1589 BUG_ON(chan_idx >= sband->n_channels);
1590 chan = &sband->channels[chan_idx];
1591
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001592 r = freq_reg_info_regd(wiphy,
1593 MHZ_TO_KHZ(chan->center_freq),
1594 desired_bw_khz,
1595 &reg_rule,
1596 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001597
1598 if (r) {
1599 chan->flags = IEEE80211_CHAN_DISABLED;
1600 return;
1601 }
1602
1603 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001604 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001605
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001606 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1607 bw_flags = IEEE80211_CHAN_NO_HT40;
1608
1609 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001610 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001611 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1612}
1613
1614static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1615 const struct ieee80211_regdomain *regd)
1616{
1617 unsigned int i;
1618 struct ieee80211_supported_band *sband;
1619
1620 BUG_ON(!wiphy->bands[band]);
1621 sband = wiphy->bands[band];
1622
1623 for (i = 0; i < sband->n_channels; i++)
1624 handle_channel_custom(wiphy, band, i, regd);
1625}
1626
1627/* Used by drivers prior to wiphy registration */
1628void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1629 const struct ieee80211_regdomain *regd)
1630{
1631 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001632 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001633
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001634 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001635 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001636 if (!wiphy->bands[band])
1637 continue;
1638 handle_band_custom(wiphy, band, regd);
1639 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001640 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001641 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001642
1643 /*
1644 * no point in calling this if it won't have any effect
1645 * on your device's supportd bands.
1646 */
1647 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001648}
1649EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1650
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001651/*
1652 * Return value which can be used by ignore_request() to indicate
1653 * it has been determined we should intersect two regulatory domains
1654 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001655#define REG_INTERSECT 1
1656
Johannes Berg84fa4f42008-10-24 20:32:23 +02001657/* This has the logic which determines when a new request
1658 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001659static int ignore_request(struct wiphy *wiphy,
1660 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001661{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001662 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001663
1664 assert_cfg80211_lock();
1665
Johannes Berg84fa4f42008-10-24 20:32:23 +02001666 /* All initial requests are respected */
1667 if (!last_request)
1668 return 0;
1669
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001670 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001671 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001672 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001673 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001674
1675 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1676
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001677 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001678 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001679 if (last_request->initiator ==
1680 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001681 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001682 /*
1683 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001684 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001685 * intersect them, but that seems unlikely
1686 * to be correct. Reject second one for now.
1687 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001688 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001689 return -EOPNOTSUPP;
1690 return -EALREADY;
1691 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001692 /*
1693 * Two consecutive Country IE hints on the same wiphy.
1694 * This should be picked up early by the driver/stack
1695 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001696 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001697 return 0;
1698 return -EALREADY;
1699 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001700 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001701 case NL80211_REGDOM_SET_BY_DRIVER:
1702 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001703 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001704 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001705 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001706 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001707
1708 /*
1709 * This would happen if you unplug and plug your card
1710 * back in or if you add a new device for which the previously
1711 * loaded card also agrees on the regulatory domain.
1712 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001713 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001714 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001715 return -EALREADY;
1716
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001717 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001718 case NL80211_REGDOM_SET_BY_USER:
1719 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001720 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001721 /*
1722 * If the user knows better the user should set the regdom
1723 * to their country before the IE is picked up
1724 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001725 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001726 last_request->intersect)
1727 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001728 /*
1729 * Process user requests only after previous user/driver/core
1730 * requests have been processed
1731 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001732 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1733 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1734 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001735 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001736 return -EAGAIN;
1737 }
1738
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001739 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001740 return -EALREADY;
1741
Johannes Berg84fa4f42008-10-24 20:32:23 +02001742 return 0;
1743 }
1744
1745 return -EINVAL;
1746}
1747
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001748/**
1749 * __regulatory_hint - hint to the wireless core a regulatory domain
1750 * @wiphy: if the hint comes from country information from an AP, this
1751 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001752 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001753 *
1754 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001755 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001756 *
1757 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1758 * already been set or other standard error codes.
1759 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001760 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001761 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001762static int __regulatory_hint(struct wiphy *wiphy,
1763 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001764{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001765 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001766 int r = 0;
1767
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001768 assert_cfg80211_lock();
1769
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001770 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001771
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001772 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001773 if (pending_request->initiator ==
1774 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001775 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001776 if (r) {
1777 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001778 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001779 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001780 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001781 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001782 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001783 /*
1784 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001785 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001786 * wiphy
1787 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001788 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001789 pending_request->initiator ==
1790 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001791 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001792 if (r) {
1793 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001794 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001795 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001796 r = -EALREADY;
1797 goto new_request;
1798 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001799 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001800 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001801 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001802
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001803new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001804 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001805
1806 last_request = pending_request;
1807 last_request->intersect = intersect;
1808
1809 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001810
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001811 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1812 user_alpha2[0] = last_request->alpha2[0];
1813 user_alpha2[1] = last_request->alpha2[1];
1814 }
1815
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001816 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001817 if (r < 0) {
1818 /*
1819 * Since CRDA will not be called in this case as we already
1820 * have applied the requested regulatory domain before we just
1821 * inform userspace we have processed the request
1822 */
1823 if (r == -EALREADY)
1824 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001825 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001826 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001827
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001828 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001829}
1830
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001831/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001832static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001833{
1834 int r = 0;
1835 struct wiphy *wiphy = NULL;
1836
1837 BUG_ON(!reg_request->alpha2);
1838
1839 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001840 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001841
1842 if (wiphy_idx_valid(reg_request->wiphy_idx))
1843 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1844
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001845 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001846 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001847 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001848 goto out;
1849 }
1850
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001851 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001852 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001853 if (r == -EALREADY && wiphy &&
1854 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001855 wiphy_update_regulatory(wiphy, reg_request->initiator);
1856out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001857 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001858 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001859}
1860
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001861/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001862static void reg_process_pending_hints(void)
1863 {
1864 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001865
1866 spin_lock(&reg_requests_lock);
1867 while (!list_empty(&reg_requests_list)) {
1868 reg_request = list_first_entry(&reg_requests_list,
1869 struct regulatory_request,
1870 list);
1871 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001872
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001873 spin_unlock(&reg_requests_lock);
1874 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001875 spin_lock(&reg_requests_lock);
1876 }
1877 spin_unlock(&reg_requests_lock);
1878}
1879
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001880/* Processes beacon hints -- this has nothing to do with country IEs */
1881static void reg_process_pending_beacon_hints(void)
1882{
Johannes Berg79c97e92009-07-07 03:56:12 +02001883 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001884 struct reg_beacon *pending_beacon, *tmp;
1885
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001886 /*
1887 * No need to hold the reg_mutex here as we just touch wiphys
1888 * and do not read or access regulatory variables.
1889 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001890 mutex_lock(&cfg80211_mutex);
1891
1892 /* This goes through the _pending_ beacon list */
1893 spin_lock_bh(&reg_pending_beacons_lock);
1894
1895 if (list_empty(&reg_pending_beacons)) {
1896 spin_unlock_bh(&reg_pending_beacons_lock);
1897 goto out;
1898 }
1899
1900 list_for_each_entry_safe(pending_beacon, tmp,
1901 &reg_pending_beacons, list) {
1902
1903 list_del_init(&pending_beacon->list);
1904
1905 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001906 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1907 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001908
1909 /* Remembers the beacon hint for new wiphys or reg changes */
1910 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1911 }
1912
1913 spin_unlock_bh(&reg_pending_beacons_lock);
1914out:
1915 mutex_unlock(&cfg80211_mutex);
1916}
1917
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001918static void reg_todo(struct work_struct *work)
1919{
1920 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001921 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001922}
1923
1924static DECLARE_WORK(reg_work, reg_todo);
1925
1926static void queue_regulatory_request(struct regulatory_request *request)
1927{
1928 spin_lock(&reg_requests_lock);
1929 list_add_tail(&request->list, &reg_requests_list);
1930 spin_unlock(&reg_requests_lock);
1931
1932 schedule_work(&reg_work);
1933}
1934
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001935/*
1936 * Core regulatory hint -- happens during cfg80211_init()
1937 * and when we restore regulatory settings.
1938 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001939static int regulatory_hint_core(const char *alpha2)
1940{
1941 struct regulatory_request *request;
1942
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001943 kfree(last_request);
1944 last_request = NULL;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001945
1946 request = kzalloc(sizeof(struct regulatory_request),
1947 GFP_KERNEL);
1948 if (!request)
1949 return -ENOMEM;
1950
1951 request->alpha2[0] = alpha2[0];
1952 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001953 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001954
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001955 /*
1956 * This ensures last_request is populated once modules
1957 * come swinging in and calling regulatory hints and
1958 * wiphy_apply_custom_regulatory().
1959 */
Luis R. Rodrigueza2bff262010-01-29 19:58:56 -05001960 reg_process_hint(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001961
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001962 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001963}
1964
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001965/* User hints */
1966int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001967{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001968 struct regulatory_request *request;
1969
Johannes Bergbe3d4812008-10-24 20:32:21 +02001970 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001971
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001972 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1973 if (!request)
1974 return -ENOMEM;
1975
1976 request->wiphy_idx = WIPHY_IDX_STALE;
1977 request->alpha2[0] = alpha2[0];
1978 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001979 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001980
1981 queue_regulatory_request(request);
1982
1983 return 0;
1984}
1985
1986/* Driver hints */
1987int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1988{
1989 struct regulatory_request *request;
1990
1991 BUG_ON(!alpha2);
1992 BUG_ON(!wiphy);
1993
1994 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1995 if (!request)
1996 return -ENOMEM;
1997
1998 request->wiphy_idx = get_wiphy_idx(wiphy);
1999
2000 /* Must have registered wiphy first */
2001 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
2002
2003 request->alpha2[0] = alpha2[0];
2004 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002005 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002006
2007 queue_regulatory_request(request);
2008
2009 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002010}
2011EXPORT_SYMBOL(regulatory_hint);
2012
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002013/* Caller must hold reg_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002014static bool reg_same_country_ie_hint(struct wiphy *wiphy,
2015 u32 country_ie_checksum)
2016{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002017 struct wiphy *request_wiphy;
2018
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002019 assert_reg_lock();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002020
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04002021 if (unlikely(last_request->initiator !=
2022 NL80211_REGDOM_SET_BY_COUNTRY_IE))
2023 return false;
2024
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002025 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2026
2027 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002028 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002029
2030 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002031 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002032 /*
2033 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002034 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002035 * on the device
2036 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002037 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
2038 return true;
2039 return false;
2040}
2041
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002042/*
2043 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
2044 * therefore cannot iterate over the rdev list here.
2045 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002046void regulatory_hint_11d(struct wiphy *wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05002047 enum ieee80211_band band,
2048 u8 *country_ie,
2049 u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002050{
2051 struct ieee80211_regdomain *rd = NULL;
2052 char alpha2[2];
2053 u32 checksum = 0;
2054 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002055 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002056
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002057 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002058
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07002059 if (unlikely(!last_request))
2060 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002061
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002062 /* IE len must be evenly divisible by 2 */
2063 if (country_ie_len & 0x01)
2064 goto out;
2065
2066 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2067 goto out;
2068
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002069 /*
2070 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002071 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002072 * we were able to process the last regulatory_hint_11d() call
2073 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002074 if (country_ie_regdomain)
2075 goto out;
2076
2077 alpha2[0] = country_ie[0];
2078 alpha2[1] = country_ie[1];
2079
2080 if (country_ie[2] == 'I')
2081 env = ENVIRON_INDOOR;
2082 else if (country_ie[2] == 'O')
2083 env = ENVIRON_OUTDOOR;
2084
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002085 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002086 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002087 * We leave conflict resolution to the workqueue, where can hold
2088 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002089 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04002090 if (likely(last_request->initiator ==
2091 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002092 wiphy_idx_valid(last_request->wiphy_idx)))
2093 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002094
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05002095 rd = country_ie_2_rd(band, country_ie, country_ie_len, &checksum);
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002096 if (!rd) {
2097 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002098 goto out;
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002099 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002100
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002101 /*
2102 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002103 * the future when we want to add suspend/resume support and having
2104 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002105 * move to another AP. Right now we just trust the first AP.
2106 *
2107 * If we hit this before we add this support we want to be informed of
2108 * it as it would indicate a mistake in the current design
2109 */
2110 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002111 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002112
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002113 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2114 if (!request)
2115 goto free_rd_out;
2116
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002117 /*
2118 * We keep this around for when CRDA comes back with a response so
2119 * we can intersect with that
2120 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002121 country_ie_regdomain = rd;
2122
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002123 request->wiphy_idx = get_wiphy_idx(wiphy);
2124 request->alpha2[0] = rd->alpha2[0];
2125 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002126 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002127 request->country_ie_checksum = checksum;
2128 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002129
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002130 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002131
2132 queue_regulatory_request(request);
2133
2134 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002135
2136free_rd_out:
2137 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002138out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002139 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002140}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002141
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002142static void restore_alpha2(char *alpha2, bool reset_user)
2143{
2144 /* indicates there is no alpha2 to consider for restoration */
2145 alpha2[0] = '9';
2146 alpha2[1] = '7';
2147
2148 /* The user setting has precedence over the module parameter */
2149 if (is_user_regdom_saved()) {
2150 /* Unless we're asked to ignore it and reset it */
2151 if (reset_user) {
2152 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2153 "including user preference\n");
2154 user_alpha2[0] = '9';
2155 user_alpha2[1] = '7';
2156
2157 /*
2158 * If we're ignoring user settings, we still need to
2159 * check the module parameter to ensure we put things
2160 * back as they were for a full restore.
2161 */
2162 if (!is_world_regdom(ieee80211_regdom)) {
2163 REG_DBG_PRINT("cfg80211: Keeping preference on "
2164 "module parameter ieee80211_regdom: %c%c\n",
2165 ieee80211_regdom[0],
2166 ieee80211_regdom[1]);
2167 alpha2[0] = ieee80211_regdom[0];
2168 alpha2[1] = ieee80211_regdom[1];
2169 }
2170 } else {
2171 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2172 "while preserving user preference for: %c%c\n",
2173 user_alpha2[0],
2174 user_alpha2[1]);
2175 alpha2[0] = user_alpha2[0];
2176 alpha2[1] = user_alpha2[1];
2177 }
2178 } else if (!is_world_regdom(ieee80211_regdom)) {
2179 REG_DBG_PRINT("cfg80211: Keeping preference on "
2180 "module parameter ieee80211_regdom: %c%c\n",
2181 ieee80211_regdom[0],
2182 ieee80211_regdom[1]);
2183 alpha2[0] = ieee80211_regdom[0];
2184 alpha2[1] = ieee80211_regdom[1];
2185 } else
2186 REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
2187}
2188
2189/*
2190 * Restoring regulatory settings involves ingoring any
2191 * possibly stale country IE information and user regulatory
2192 * settings if so desired, this includes any beacon hints
2193 * learned as we could have traveled outside to another country
2194 * after disconnection. To restore regulatory settings we do
2195 * exactly what we did at bootup:
2196 *
2197 * - send a core regulatory hint
2198 * - send a user regulatory hint if applicable
2199 *
2200 * Device drivers that send a regulatory hint for a specific country
2201 * keep their own regulatory domain on wiphy->regd so that does does
2202 * not need to be remembered.
2203 */
2204static void restore_regulatory_settings(bool reset_user)
2205{
2206 char alpha2[2];
2207 struct reg_beacon *reg_beacon, *btmp;
2208
2209 mutex_lock(&cfg80211_mutex);
2210 mutex_lock(&reg_mutex);
2211
2212 reset_regdomains();
2213 restore_alpha2(alpha2, reset_user);
2214
2215 /* Clear beacon hints */
2216 spin_lock_bh(&reg_pending_beacons_lock);
2217 if (!list_empty(&reg_pending_beacons)) {
2218 list_for_each_entry_safe(reg_beacon, btmp,
2219 &reg_pending_beacons, list) {
2220 list_del(&reg_beacon->list);
2221 kfree(reg_beacon);
2222 }
2223 }
2224 spin_unlock_bh(&reg_pending_beacons_lock);
2225
2226 if (!list_empty(&reg_beacon_list)) {
2227 list_for_each_entry_safe(reg_beacon, btmp,
2228 &reg_beacon_list, list) {
2229 list_del(&reg_beacon->list);
2230 kfree(reg_beacon);
2231 }
2232 }
2233
2234 /* First restore to the basic regulatory settings */
2235 cfg80211_regdomain = cfg80211_world_regdom;
2236
2237 mutex_unlock(&reg_mutex);
2238 mutex_unlock(&cfg80211_mutex);
2239
2240 regulatory_hint_core(cfg80211_regdomain->alpha2);
2241
2242 /*
2243 * This restores the ieee80211_regdom module parameter
2244 * preference or the last user requested regulatory
2245 * settings, user regulatory settings takes precedence.
2246 */
2247 if (is_an_alpha2(alpha2))
2248 regulatory_hint_user(user_alpha2);
2249}
2250
2251
2252void regulatory_hint_disconnect(void)
2253{
2254 REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
2255 "restore regulatory settings\n");
2256 restore_regulatory_settings(false);
2257}
2258
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002259static bool freq_is_chan_12_13_14(u16 freq)
2260{
2261 if (freq == ieee80211_channel_to_frequency(12) ||
2262 freq == ieee80211_channel_to_frequency(13) ||
2263 freq == ieee80211_channel_to_frequency(14))
2264 return true;
2265 return false;
2266}
2267
2268int regulatory_hint_found_beacon(struct wiphy *wiphy,
2269 struct ieee80211_channel *beacon_chan,
2270 gfp_t gfp)
2271{
2272 struct reg_beacon *reg_beacon;
2273
2274 if (likely((beacon_chan->beacon_found ||
2275 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
2276 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
2277 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
2278 return 0;
2279
2280 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2281 if (!reg_beacon)
2282 return -ENOMEM;
2283
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002284 REG_DBG_PRINT("cfg80211: Found new beacon on "
2285 "frequency: %d MHz (Ch %d) on %s\n",
2286 beacon_chan->center_freq,
2287 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2288 wiphy_name(wiphy));
2289
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002290 memcpy(&reg_beacon->chan, beacon_chan,
2291 sizeof(struct ieee80211_channel));
2292
2293
2294 /*
2295 * Since we can be called from BH or and non-BH context
2296 * we must use spin_lock_bh()
2297 */
2298 spin_lock_bh(&reg_pending_beacons_lock);
2299 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2300 spin_unlock_bh(&reg_pending_beacons_lock);
2301
2302 schedule_work(&reg_work);
2303
2304 return 0;
2305}
2306
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002307static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002308{
2309 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002310 const struct ieee80211_reg_rule *reg_rule = NULL;
2311 const struct ieee80211_freq_range *freq_range = NULL;
2312 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002313
Kalle Valo269ac5f2009-12-01 10:47:15 +02002314 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002315 "(max_antenna_gain, max_eirp)\n");
2316
2317 for (i = 0; i < rd->n_reg_rules; i++) {
2318 reg_rule = &rd->reg_rules[i];
2319 freq_range = &reg_rule->freq_range;
2320 power_rule = &reg_rule->power_rule;
2321
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002322 /*
2323 * There may not be documentation for max antenna gain
2324 * in certain regions
2325 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002326 if (power_rule->max_antenna_gain)
Kalle Valo269ac5f2009-12-01 10:47:15 +02002327 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002328 "(%d mBi, %d mBm)\n",
2329 freq_range->start_freq_khz,
2330 freq_range->end_freq_khz,
2331 freq_range->max_bandwidth_khz,
2332 power_rule->max_antenna_gain,
2333 power_rule->max_eirp);
2334 else
Kalle Valo269ac5f2009-12-01 10:47:15 +02002335 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002336 "(N/A, %d mBm)\n",
2337 freq_range->start_freq_khz,
2338 freq_range->end_freq_khz,
2339 freq_range->max_bandwidth_khz,
2340 power_rule->max_eirp);
2341 }
2342}
2343
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002344static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002345{
2346
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002347 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002348
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002349 if (last_request->initiator ==
2350 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002351 struct cfg80211_registered_device *rdev;
2352 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002353 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002354 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002355 printk(KERN_INFO "cfg80211: Current regulatory "
2356 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002357 rdev->country_ie_alpha2[0],
2358 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002359 } else
2360 printk(KERN_INFO "cfg80211: Current regulatory "
2361 "domain intersected: \n");
2362 } else
2363 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08002364 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002365 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002366 printk(KERN_INFO "cfg80211: World regulatory "
2367 "domain updated:\n");
2368 else {
2369 if (is_unknown_alpha2(rd->alpha2))
2370 printk(KERN_INFO "cfg80211: Regulatory domain "
2371 "changed to driver built-in settings "
2372 "(unknown country)\n");
2373 else
2374 printk(KERN_INFO "cfg80211: Regulatory domain "
2375 "changed to country: %c%c\n",
2376 rd->alpha2[0], rd->alpha2[1]);
2377 }
2378 print_rd_rules(rd);
2379}
2380
Johannes Berg2df78162008-10-28 16:49:41 +01002381static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002382{
2383 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2384 rd->alpha2[0], rd->alpha2[1]);
2385 print_rd_rules(rd);
2386}
2387
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002388#ifdef CONFIG_CFG80211_REG_DEBUG
2389static void reg_country_ie_process_debug(
2390 const struct ieee80211_regdomain *rd,
2391 const struct ieee80211_regdomain *country_ie_regdomain,
2392 const struct ieee80211_regdomain *intersected_rd)
2393{
2394 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2395 print_regdomain_info(country_ie_regdomain);
2396 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2397 print_regdomain_info(rd);
2398 if (intersected_rd) {
2399 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2400 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08002401 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002402 return;
2403 }
2404 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2405}
2406#else
2407static inline void reg_country_ie_process_debug(
2408 const struct ieee80211_regdomain *rd,
2409 const struct ieee80211_regdomain *country_ie_regdomain,
2410 const struct ieee80211_regdomain *intersected_rd)
2411{
2412}
2413#endif
2414
Johannes Bergd2372b32008-10-24 20:32:20 +02002415/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002416static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002417{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002418 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002419 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002420 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002421 /* Some basic sanity checks first */
2422
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002423 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002424 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002425 return -EINVAL;
2426 update_world_regdomain(rd);
2427 return 0;
2428 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002429
2430 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2431 !is_unknown_alpha2(rd->alpha2))
2432 return -EINVAL;
2433
Johannes Bergf6037d02008-10-21 11:01:33 +02002434 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002435 return -EINVAL;
2436
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002437 /*
2438 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002439 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002440 * and the pending request came in from a country IE
2441 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002442 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002443 /*
2444 * If someone else asked us to change the rd lets only bother
2445 * checking if the alpha2 changes if CRDA was already called
2446 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002447 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002448 return -EINVAL;
2449 }
2450
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002451 /*
2452 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002453 * and finally inform them of what we have done, in case they want
2454 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002455 * internal EEPROM data
2456 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002457
Johannes Bergf6037d02008-10-21 11:01:33 +02002458 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002459 return -EINVAL;
2460
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002461 if (!is_valid_rd(rd)) {
2462 printk(KERN_ERR "cfg80211: Invalid "
2463 "regulatory domain detected:\n");
2464 print_regdomain_info(rd);
2465 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002466 }
2467
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002468 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2469
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002470 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002471 int r;
2472
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002473 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002474 reset_regdomains();
2475 cfg80211_regdomain = rd;
2476 return 0;
2477 }
2478
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002479 /*
2480 * For a driver hint, lets copy the regulatory domain the
2481 * driver wanted to the wiphy to deal with conflicts
2482 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002483
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002484 /*
2485 * Userspace could have sent two replies with only
2486 * one kernel request.
2487 */
2488 if (request_wiphy->regd)
2489 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002490
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002491 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002492 if (r)
2493 return r;
2494
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002495 reset_regdomains();
2496 cfg80211_regdomain = rd;
2497 return 0;
2498 }
2499
2500 /* Intersection requires a bit more work */
2501
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002502 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002503
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002504 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2505 if (!intersected_rd)
2506 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002507
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002508 /*
2509 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002510 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002511 * domain we keep it for its private use
2512 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002513 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002514 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002515 else
2516 kfree(rd);
2517
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002518 rd = NULL;
2519
2520 reset_regdomains();
2521 cfg80211_regdomain = intersected_rd;
2522
2523 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002524 }
2525
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002526 /*
2527 * Country IE requests are handled a bit differently, we intersect
2528 * the country IE rd with what CRDA believes that country should have
2529 */
2530
Luis R. Rodriguez729e9c72009-05-31 18:24:34 -04002531 /*
2532 * Userspace could have sent two replies with only
2533 * one kernel request. By the second reply we would have
2534 * already processed and consumed the country_ie_regdomain.
2535 */
2536 if (!country_ie_regdomain)
2537 return -EALREADY;
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002538 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002539
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002540 /*
2541 * Intersect what CRDA returned and our what we
2542 * had built from the Country IE received
2543 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002544
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002545 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002546
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002547 reg_country_ie_process_debug(rd,
2548 country_ie_regdomain,
2549 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002550
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002551 kfree(country_ie_regdomain);
2552 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002553
2554 if (!intersected_rd)
2555 return -EINVAL;
2556
Johannes Berg79c97e92009-07-07 03:56:12 +02002557 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002558
Johannes Berg79c97e92009-07-07 03:56:12 +02002559 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2560 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2561 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002562
2563 BUG_ON(intersected_rd == rd);
2564
2565 kfree(rd);
2566 rd = NULL;
2567
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002568 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002569 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002570
2571 return 0;
2572}
2573
2574
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002575/*
2576 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002577 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002578 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2579 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002580int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002581{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002582 int r;
2583
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002584 assert_cfg80211_lock();
2585
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002586 mutex_lock(&reg_mutex);
2587
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002588 /* Note that this doesn't update the wiphys, this is done below */
2589 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002590 if (r) {
2591 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002592 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002593 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002594 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002595
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002596 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002597 if (!last_request->intersect)
2598 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002599
2600 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002601 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002602
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002603 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002604
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002605 nl80211_send_reg_change_event(last_request);
2606
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002607 mutex_unlock(&reg_mutex);
2608
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002609 return r;
2610}
2611
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002612/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002613void reg_device_remove(struct wiphy *wiphy)
2614{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002615 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002616
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002617 assert_cfg80211_lock();
2618
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002619 mutex_lock(&reg_mutex);
2620
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002621 kfree(wiphy->regd);
2622
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002623 if (last_request)
2624 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002625
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002626 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002627 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002628
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002629 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002630 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002631out:
2632 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002633}
2634
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002635int regulatory_init(void)
2636{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002637 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002638
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002639 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2640 if (IS_ERR(reg_pdev))
2641 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002642
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002643 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002644 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002645
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002646 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002647
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002648 user_alpha2[0] = '9';
2649 user_alpha2[1] = '7';
2650
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002651 /* We always try to get an update for the static regdomain */
2652 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002653 if (err) {
2654 if (err == -ENOMEM)
2655 return err;
2656 /*
2657 * N.B. kobject_uevent_env() can fail mainly for when we're out
2658 * memory which is handled and propagated appropriately above
2659 * but it can also fail during a netlink_broadcast() or during
2660 * early boot for call_usermodehelper(). For now treat these
2661 * errors as non-fatal.
2662 */
2663 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2664 "to call CRDA during init");
2665#ifdef CONFIG_CFG80211_REG_DEBUG
2666 /* We want to find out exactly why when debugging */
2667 WARN_ON(err);
2668#endif
2669 }
Johannes Berg734366d2008-09-15 10:56:48 +02002670
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002671 /*
2672 * Finally, if the user set the module parameter treat it
2673 * as a user hint.
2674 */
2675 if (!is_world_regdom(ieee80211_regdom))
2676 regulatory_hint_user(ieee80211_regdom);
2677
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002678 return 0;
2679}
2680
2681void regulatory_exit(void)
2682{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002683 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002684 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002685
2686 cancel_work_sync(&reg_work);
2687
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002688 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002689 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002690
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002691 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002692
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002693 kfree(country_ie_regdomain);
2694 country_ie_regdomain = NULL;
2695
Johannes Bergf6037d02008-10-21 11:01:33 +02002696 kfree(last_request);
2697
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002698 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002699
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002700 spin_lock_bh(&reg_pending_beacons_lock);
2701 if (!list_empty(&reg_pending_beacons)) {
2702 list_for_each_entry_safe(reg_beacon, btmp,
2703 &reg_pending_beacons, list) {
2704 list_del(&reg_beacon->list);
2705 kfree(reg_beacon);
2706 }
2707 }
2708 spin_unlock_bh(&reg_pending_beacons_lock);
2709
2710 if (!list_empty(&reg_beacon_list)) {
2711 list_for_each_entry_safe(reg_beacon, btmp,
2712 &reg_beacon_list, list) {
2713 list_del(&reg_beacon->list);
2714 kfree(reg_beacon);
2715 }
2716 }
2717
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002718 spin_lock(&reg_requests_lock);
2719 if (!list_empty(&reg_requests_list)) {
2720 list_for_each_entry_safe(reg_request, tmp,
2721 &reg_requests_list, list) {
2722 list_del(&reg_request->list);
2723 kfree(reg_request);
2724 }
2725 }
2726 spin_unlock(&reg_requests_lock);
2727
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002728 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002729 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002730}