blob: e857d72c7e8c9d19ea086082b68cce24d9e84834 [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>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070036#include <linux/list.h>
37#include <linux/random.h>
38#include <linux/nl80211.h>
39#include <linux/platform_device.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070040#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010041#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070042#include "reg.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050043#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040044#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010045
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050046#ifdef CONFIG_CFG80211_REG_DEBUG
John W. Linville82711952010-01-05 17:57:20 -050047#define REG_DBG_PRINT(format, args...) \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050048 do { \
John W. Linville82711952010-01-05 17:57:20 -050049 printk(KERN_DEBUG format , ## args); \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050050 } while (0)
51#else
John W. Linville82711952010-01-05 17:57:20 -050052#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050053#endif
54
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070055/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020056static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020057
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070058/* To trigger userspace events */
59static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010060
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050061/*
62 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020063 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050064 * information to give us an alpha2
65 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080066const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020067
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050068/*
69 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080070 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050071 * what it thinks should apply for the same country
72 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080073static const struct ieee80211_regdomain *country_ie_regdomain;
74
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070075/*
76 * Protects static reg.c components:
77 * - cfg80211_world_regdom
78 * - cfg80211_regdom
79 * - country_ie_regdomain
80 * - last_request
81 */
82DEFINE_MUTEX(reg_mutex);
83#define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
84
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050085/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050086static LIST_HEAD(reg_requests_list);
87static spinlock_t reg_requests_lock;
88
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050089/* Used to queue up beacon hints for review */
90static LIST_HEAD(reg_pending_beacons);
91static spinlock_t reg_pending_beacons_lock;
92
93/* Used to keep track of processed beacon hints */
94static LIST_HEAD(reg_beacon_list);
95
96struct reg_beacon {
97 struct list_head list;
98 struct ieee80211_channel chan;
99};
100
Johannes Berg734366d2008-09-15 10:56:48 +0200101/* We keep a static world regulatory domain in case of the absence of CRDA */
102static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800103 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +0200104 .alpha2 = "00",
105 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500106 /* IEEE 802.11b/g, channels 1..11 */
107 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800108 /* IEEE 802.11b/g, channels 12..13. No HT40
109 * channel fits here. */
110 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500111 NL80211_RRF_PASSIVE_SCAN |
112 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800113 /* IEEE 802.11 channel 14 - Only JP enables
114 * this and for 802.11b only */
115 REG_RULE(2484-10, 2484+10, 20, 6, 20,
116 NL80211_RRF_PASSIVE_SCAN |
117 NL80211_RRF_NO_IBSS |
118 NL80211_RRF_NO_OFDM),
119 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800120 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800121 NL80211_RRF_PASSIVE_SCAN |
122 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500123
124 /* NB: 5260 MHz - 5700 MHz requies DFS */
125
126 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800127 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500128 NL80211_RRF_PASSIVE_SCAN |
129 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200130 }
131};
132
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200133static const struct ieee80211_regdomain *cfg80211_world_regdom =
134 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200135
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400136static char *ieee80211_regdom = "00";
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500137static char user_alpha2[2];
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400138
Johannes Berg734366d2008-09-15 10:56:48 +0200139module_param(ieee80211_regdom, charp, 0444);
140MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
141
Johannes Berg734366d2008-09-15 10:56:48 +0200142static void reset_regdomains(void)
143{
Johannes Berg942b25c2008-09-15 11:26:47 +0200144 /* avoid freeing static information or freeing something twice */
145 if (cfg80211_regdomain == cfg80211_world_regdom)
146 cfg80211_regdomain = NULL;
147 if (cfg80211_world_regdom == &world_regdom)
148 cfg80211_world_regdom = NULL;
149 if (cfg80211_regdomain == &world_regdom)
150 cfg80211_regdomain = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200151
152 kfree(cfg80211_regdomain);
153 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200154
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200155 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200156 cfg80211_regdomain = NULL;
157}
158
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500159/*
160 * Dynamic world regulatory domain requested by the wireless
161 * core upon initialization
162 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200163static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200164{
Johannes Bergf6037d02008-10-21 11:01:33 +0200165 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200166
167 reset_regdomains();
168
169 cfg80211_world_regdom = rd;
170 cfg80211_regdomain = rd;
171}
Johannes Berg734366d2008-09-15 10:56:48 +0200172
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200173bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100174{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700175 if (!alpha2)
176 return false;
177 if (alpha2[0] == '0' && alpha2[1] == '0')
178 return true;
179 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100180}
181
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200182static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700183{
184 if (!alpha2)
185 return false;
186 if (alpha2[0] != 0 && alpha2[1] != 0)
187 return true;
188 return false;
189}
Johannes Berg8318d782008-01-24 19:38:38 +0100190
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700191static bool is_alpha_upper(char letter)
192{
193 /* ASCII A - Z */
194 if (letter >= 65 && letter <= 90)
195 return true;
196 return false;
197}
198
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200199static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700200{
201 if (!alpha2)
202 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500203 /*
204 * Special case where regulatory domain was built by driver
205 * but a specific alpha2 cannot be determined
206 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700207 if (alpha2[0] == '9' && alpha2[1] == '9')
208 return true;
209 return false;
210}
211
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800212static bool is_intersected_alpha2(const char *alpha2)
213{
214 if (!alpha2)
215 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500216 /*
217 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800218 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500219 * structures
220 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800221 if (alpha2[0] == '9' && alpha2[1] == '8')
222 return true;
223 return false;
224}
225
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200226static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700227{
228 if (!alpha2)
229 return false;
230 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
231 return true;
232 return false;
233}
234
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200235static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700236{
237 if (!alpha2_x || !alpha2_y)
238 return false;
239 if (alpha2_x[0] == alpha2_y[0] &&
240 alpha2_x[1] == alpha2_y[1])
241 return true;
242 return false;
243}
244
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500245static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700246{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500247 assert_cfg80211_lock();
248
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700249 if (!cfg80211_regdomain)
250 return true;
251 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
252 return false;
253 return true;
254}
255
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500256/*
257 * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
258 * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
259 * has ever been issued.
260 */
261static bool is_user_regdom_saved(void)
262{
263 if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
264 return false;
265
266 /* This would indicate a mistake on the design */
267 if (WARN((!is_world_regdom(user_alpha2) &&
268 !is_an_alpha2(user_alpha2)),
269 "Unexpected user alpha2: %c%c\n",
270 user_alpha2[0],
271 user_alpha2[1]))
272 return false;
273
274 return true;
275}
276
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800277/**
278 * country_ie_integrity_changes - tells us if the country IE has changed
279 * @checksum: checksum of country IE of fields we are interested in
280 *
281 * If the country IE has not changed you can ignore it safely. This is
282 * useful to determine if two devices are seeing two different country IEs
283 * even on the same alpha2. Note that this will return false if no IE has
284 * been set on the wireless core yet.
285 */
286static bool country_ie_integrity_changes(u32 checksum)
287{
288 /* If no IE has been set then the checksum doesn't change */
289 if (unlikely(!last_request->country_ie_checksum))
290 return false;
291 if (unlikely(last_request->country_ie_checksum != checksum))
292 return true;
293 return false;
294}
295
John W. Linville3b377ea2009-12-18 17:59:01 -0500296static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
297 const struct ieee80211_regdomain *src_regd)
298{
299 struct ieee80211_regdomain *regd;
300 int size_of_regd = 0;
301 unsigned int i;
302
303 size_of_regd = sizeof(struct ieee80211_regdomain) +
304 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
305
306 regd = kzalloc(size_of_regd, GFP_KERNEL);
307 if (!regd)
308 return -ENOMEM;
309
310 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
311
312 for (i = 0; i < src_regd->n_reg_rules; i++)
313 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
314 sizeof(struct ieee80211_reg_rule));
315
316 *dst_regd = regd;
317 return 0;
318}
319
320#ifdef CONFIG_CFG80211_INTERNAL_REGDB
321struct reg_regdb_search_request {
322 char alpha2[2];
323 struct list_head list;
324};
325
326static LIST_HEAD(reg_regdb_search_list);
327static DEFINE_SPINLOCK(reg_regdb_search_lock);
328
329static void reg_regdb_search(struct work_struct *work)
330{
331 struct reg_regdb_search_request *request;
332 const struct ieee80211_regdomain *curdom, *regdom;
333 int i, r;
334
335 spin_lock(&reg_regdb_search_lock);
336 while (!list_empty(&reg_regdb_search_list)) {
337 request = list_first_entry(&reg_regdb_search_list,
338 struct reg_regdb_search_request,
339 list);
340 list_del(&request->list);
341
342 for (i=0; i<reg_regdb_size; i++) {
343 curdom = reg_regdb[i];
344
345 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
346 r = reg_copy_regd(&regdom, curdom);
347 if (r)
348 break;
349 spin_unlock(&reg_regdb_search_lock);
350 mutex_lock(&cfg80211_mutex);
351 set_regdom(regdom);
352 mutex_unlock(&cfg80211_mutex);
353 spin_lock(&reg_regdb_search_lock);
354 break;
355 }
356 }
357
358 kfree(request);
359 }
360 spin_unlock(&reg_regdb_search_lock);
361}
362
363static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
364
365static void reg_regdb_query(const char *alpha2)
366{
367 struct reg_regdb_search_request *request;
368
369 if (!alpha2)
370 return;
371
372 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
373 if (!request)
374 return;
375
376 memcpy(request->alpha2, alpha2, 2);
377
378 spin_lock(&reg_regdb_search_lock);
379 list_add_tail(&request->list, &reg_regdb_search_list);
380 spin_unlock(&reg_regdb_search_lock);
381
382 schedule_work(&reg_regdb_work);
383}
384#else
385static inline void reg_regdb_query(const char *alpha2) {}
386#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
387
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500388/*
389 * This lets us keep regulatory code which is updated on a regulatory
390 * basis in userspace.
391 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700392static int call_crda(const char *alpha2)
393{
394 char country_env[9 + 2] = "COUNTRY=";
395 char *envp[] = {
396 country_env,
397 NULL
398 };
399
400 if (!is_world_regdom((char *) alpha2))
401 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
402 alpha2[0], alpha2[1]);
403 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700404 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
405 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700406
John W. Linville3b377ea2009-12-18 17:59:01 -0500407 /* query internal regulatory database (if it exists) */
408 reg_regdb_query(alpha2);
409
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700410 country_env[8] = alpha2[0];
411 country_env[9] = alpha2[1];
412
413 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
414}
415
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700416/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200417bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700418{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400419 assert_cfg80211_lock();
420
Johannes Bergf6037d02008-10-21 11:01:33 +0200421 if (!last_request)
422 return false;
423
424 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700425}
426
427/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200428static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700429{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200430 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700431 u32 freq_diff;
432
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800433 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700434 return false;
435
436 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
437 return false;
438
439 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
440
Roel Kluinbd05f282009-03-03 22:55:21 +0100441 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
442 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700443 return false;
444
445 return true;
446}
447
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200448static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700449{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200450 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700451 unsigned int i;
452
453 if (!rd->n_reg_rules)
454 return false;
455
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800456 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
457 return false;
458
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700459 for (i = 0; i < rd->n_reg_rules; i++) {
460 reg_rule = &rd->reg_rules[i];
461 if (!is_valid_reg_rule(reg_rule))
462 return false;
463 }
464
465 return true;
466}
467
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400468static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
469 u32 center_freq_khz,
470 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700471{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400472 u32 start_freq_khz, end_freq_khz;
473
474 start_freq_khz = center_freq_khz - (bw_khz/2);
475 end_freq_khz = center_freq_khz + (bw_khz/2);
476
477 if (start_freq_khz >= freq_range->start_freq_khz &&
478 end_freq_khz <= freq_range->end_freq_khz)
479 return true;
480
481 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700482}
483
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800484/**
485 * freq_in_rule_band - tells us if a frequency is in a frequency band
486 * @freq_range: frequency rule we want to query
487 * @freq_khz: frequency we are inquiring about
488 *
489 * This lets us know if a specific frequency rule is or is not relevant to
490 * a specific frequency's band. Bands are device specific and artificial
491 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
492 * safe for now to assume that a frequency rule should not be part of a
493 * frequency's band if the start freq or end freq are off by more than 2 GHz.
494 * This resolution can be lowered and should be considered as we add
495 * regulatory rule support for other "bands".
496 **/
497static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
498 u32 freq_khz)
499{
500#define ONE_GHZ_IN_KHZ 1000000
501 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
502 return true;
503 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
504 return true;
505 return false;
506#undef ONE_GHZ_IN_KHZ
507}
508
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500509/*
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500510 * This is a work around for sanity checking ieee80211_channel_to_frequency()'s
511 * work. ieee80211_channel_to_frequency() can for example currently provide a
512 * 2 GHz channel when in fact a 5 GHz channel was desired. An example would be
513 * an AP providing channel 8 on a country IE triplet when it sent this on the
514 * 5 GHz band, that channel is designed to be channel 8 on 5 GHz, not a 2 GHz
515 * channel.
516 *
517 * This can be removed once ieee80211_channel_to_frequency() takes in a band.
518 */
519static bool chan_in_band(int chan, enum ieee80211_band band)
520{
521 int center_freq = ieee80211_channel_to_frequency(chan);
522
523 switch (band) {
524 case IEEE80211_BAND_2GHZ:
525 if (center_freq <= 2484)
526 return true;
527 return false;
528 case IEEE80211_BAND_5GHZ:
529 if (center_freq >= 5005)
530 return true;
531 return false;
532 default:
533 return false;
534 }
535}
536
537/*
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500538 * Some APs may send a country IE triplet for each channel they
539 * support and while this is completely overkill and silly we still
540 * need to support it. We avoid making a single rule for each channel
541 * though and to help us with this we use this helper to find the
542 * actual subband end channel. These type of country IE triplet
543 * scenerios are handled then, all yielding two regulaotry rules from
544 * parsing a country IE:
545 *
546 * [1]
547 * [2]
548 * [36]
549 * [40]
550 *
551 * [1]
552 * [2-4]
553 * [5-12]
554 * [36]
555 * [40-44]
556 *
557 * [1-4]
558 * [5-7]
559 * [36-44]
560 * [48-64]
561 *
562 * [36-36]
563 * [40-40]
564 * [44-44]
565 * [48-48]
566 * [52-52]
567 * [56-56]
568 * [60-60]
569 * [64-64]
570 * [100-100]
571 * [104-104]
572 * [108-108]
573 * [112-112]
574 * [116-116]
575 * [120-120]
576 * [124-124]
577 * [128-128]
578 * [132-132]
579 * [136-136]
580 * [140-140]
581 *
582 * Returns 0 if the IE has been found to be invalid in the middle
583 * somewhere.
584 */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500585static int max_subband_chan(enum ieee80211_band band,
586 int orig_cur_chan,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500587 int orig_end_channel,
588 s8 orig_max_power,
589 u8 **country_ie,
590 u8 *country_ie_len)
591{
592 u8 *triplets_start = *country_ie;
593 u8 len_at_triplet = *country_ie_len;
594 int end_subband_chan = orig_end_channel;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500595
596 /*
597 * We'll deal with padding for the caller unless
598 * its not immediate and we don't process any channels
599 */
600 if (*country_ie_len == 1) {
601 *country_ie += 1;
602 *country_ie_len -= 1;
603 return orig_end_channel;
604 }
605
606 /* Move to the next triplet and then start search */
607 *country_ie += 3;
608 *country_ie_len -= 3;
609
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500610 if (!chan_in_band(orig_cur_chan, band))
611 return 0;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500612
613 while (*country_ie_len >= 3) {
614 int end_channel = 0;
615 struct ieee80211_country_ie_triplet *triplet =
616 (struct ieee80211_country_ie_triplet *) *country_ie;
617 int cur_channel = 0, next_expected_chan;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500618
619 /* means last triplet is completely unrelated to this one */
620 if (triplet->ext.reg_extension_id >=
621 IEEE80211_COUNTRY_EXTENSION_ID) {
622 *country_ie -= 3;
623 *country_ie_len += 3;
624 break;
625 }
626
627 if (triplet->chans.first_channel == 0) {
628 *country_ie += 1;
629 *country_ie_len -= 1;
630 if (*country_ie_len != 0)
631 return 0;
632 break;
633 }
634
Luis R. Rodrigueza0f2e0f2010-01-14 13:27:46 -0500635 if (triplet->chans.num_channels == 0)
636 return 0;
637
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500638 /* Monitonically increasing channel order */
639 if (triplet->chans.first_channel <= end_subband_chan)
640 return 0;
641
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500642 if (!chan_in_band(triplet->chans.first_channel, band))
643 return 0;
644
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500645 /* 2 GHz */
646 if (triplet->chans.first_channel <= 14) {
647 end_channel = triplet->chans.first_channel +
648 triplet->chans.num_channels - 1;
649 }
650 else {
651 end_channel = triplet->chans.first_channel +
652 (4 * (triplet->chans.num_channels - 1));
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500653 }
654
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500655 if (!chan_in_band(end_channel, band))
656 return 0;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500657
658 if (orig_max_power != triplet->chans.max_power) {
659 *country_ie -= 3;
660 *country_ie_len += 3;
661 break;
662 }
663
664 cur_channel = triplet->chans.first_channel;
665
666 /* The key is finding the right next expected channel */
667 if (band == IEEE80211_BAND_2GHZ)
668 next_expected_chan = end_subband_chan + 1;
669 else
670 next_expected_chan = end_subband_chan + 4;
671
672 if (cur_channel != next_expected_chan) {
673 *country_ie -= 3;
674 *country_ie_len += 3;
675 break;
676 }
677
678 end_subband_chan = end_channel;
679
680 /* Move to the next one */
681 *country_ie += 3;
682 *country_ie_len -= 3;
683
684 /*
685 * Padding needs to be dealt with if we processed
686 * some channels.
687 */
688 if (*country_ie_len == 1) {
689 *country_ie += 1;
690 *country_ie_len -= 1;
691 break;
692 }
693
694 /* If seen, the IE is invalid */
695 if (*country_ie_len == 2)
696 return 0;
697 }
698
699 if (end_subband_chan == orig_end_channel) {
700 *country_ie = triplets_start;
701 *country_ie_len = len_at_triplet;
702 return orig_end_channel;
703 }
704
705 return end_subband_chan;
706}
707
708/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500709 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800710 * structure has a lot of information which the IE doesn't yet have,
711 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500712 * with our userspace regulatory agent to get lower bounds.
713 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800714static struct ieee80211_regdomain *country_ie_2_rd(
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500715 enum ieee80211_band band,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800716 u8 *country_ie,
717 u8 country_ie_len,
718 u32 *checksum)
719{
720 struct ieee80211_regdomain *rd = NULL;
721 unsigned int i = 0;
722 char alpha2[2];
723 u32 flags = 0;
724 u32 num_rules = 0, size_of_regd = 0;
725 u8 *triplets_start = NULL;
726 u8 len_at_triplet = 0;
727 /* the last channel we have registered in a subband (triplet) */
728 int last_sub_max_channel = 0;
729
730 *checksum = 0xDEADBEEF;
731
732 /* Country IE requirements */
733 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
734 country_ie_len & 0x01);
735
736 alpha2[0] = country_ie[0];
737 alpha2[1] = country_ie[1];
738
739 /*
740 * Third octet can be:
741 * 'I' - Indoor
742 * 'O' - Outdoor
743 *
744 * anything else we assume is no restrictions
745 */
746 if (country_ie[2] == 'I')
747 flags = NL80211_RRF_NO_OUTDOOR;
748 else if (country_ie[2] == 'O')
749 flags = NL80211_RRF_NO_INDOOR;
750
751 country_ie += 3;
752 country_ie_len -= 3;
753
754 triplets_start = country_ie;
755 len_at_triplet = country_ie_len;
756
757 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
758
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500759 /*
760 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800761 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500762 * for each channel subband
763 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800764 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800765 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800766 struct ieee80211_country_ie_triplet *triplet =
767 (struct ieee80211_country_ie_triplet *) country_ie;
768 int cur_sub_max_channel = 0, cur_channel = 0;
769
770 if (triplet->ext.reg_extension_id >=
771 IEEE80211_COUNTRY_EXTENSION_ID) {
772 country_ie += 3;
773 country_ie_len -= 3;
774 continue;
775 }
776
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500777 /*
778 * APs can add padding to make length divisible
779 * by two, required by the spec.
780 */
781 if (triplet->chans.first_channel == 0) {
782 country_ie++;
783 country_ie_len--;
784 /* This is expected to be at the very end only */
785 if (country_ie_len != 0)
786 return NULL;
787 break;
788 }
789
Luis R. Rodrigueza0f2e0f2010-01-14 13:27:46 -0500790 if (triplet->chans.num_channels == 0)
791 return NULL;
792
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500793 if (!chan_in_band(triplet->chans.first_channel, band))
794 return NULL;
795
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800796 /* 2 GHz */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500797 if (band == IEEE80211_BAND_2GHZ)
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800798 end_channel = triplet->chans.first_channel +
Luis R. Rodrigueze99c7cd2010-01-07 17:24:55 -0500799 triplet->chans.num_channels - 1;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800800 else
801 /*
802 * 5 GHz -- For example in country IEs if the first
803 * channel given is 36 and the number of channels is 4
804 * then the individual channel numbers defined for the
805 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
806 * and not 36, 37, 38, 39.
807 *
808 * See: http://tinyurl.com/11d-clarification
809 */
810 end_channel = triplet->chans.first_channel +
811 (4 * (triplet->chans.num_channels - 1));
812
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800813 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500814
815 /*
816 * Enhancement for APs that send a triplet for every channel
817 * or for whatever reason sends triplets with multiple channels
818 * separated when in fact they should be together.
819 */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500820 end_channel = max_subband_chan(band,
821 cur_channel,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500822 end_channel,
823 triplet->chans.max_power,
824 &country_ie,
825 &country_ie_len);
826 if (!end_channel)
827 return NULL;
828
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500829 if (!chan_in_band(end_channel, band))
830 return NULL;
831
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800832 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800833
834 /* Basic sanity check */
835 if (cur_sub_max_channel < cur_channel)
836 return NULL;
837
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500838 /*
839 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800840 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500841 * increasing
842 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800843 if (last_sub_max_channel) {
844 if (cur_channel <= last_sub_max_channel)
845 return NULL;
846 if (cur_sub_max_channel <= last_sub_max_channel)
847 return NULL;
848 }
849
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500850 /*
851 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800852 * we can throw ext triplets as part of this soup,
853 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500854 * don't support them
855 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800856 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
857 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
858 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
859
860 last_sub_max_channel = cur_sub_max_channel;
861
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800862 num_rules++;
863
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500864 if (country_ie_len >= 3) {
865 country_ie += 3;
866 country_ie_len -= 3;
867 }
868
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500869 /*
870 * Note: this is not a IEEE requirement but
871 * simply a memory requirement
872 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800873 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
874 return NULL;
875 }
876
877 country_ie = triplets_start;
878 country_ie_len = len_at_triplet;
879
880 size_of_regd = sizeof(struct ieee80211_regdomain) +
881 (num_rules * sizeof(struct ieee80211_reg_rule));
882
883 rd = kzalloc(size_of_regd, GFP_KERNEL);
884 if (!rd)
885 return NULL;
886
887 rd->n_reg_rules = num_rules;
888 rd->alpha2[0] = alpha2[0];
889 rd->alpha2[1] = alpha2[1];
890
891 /* This time around we fill in the rd */
892 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800893 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800894 struct ieee80211_country_ie_triplet *triplet =
895 (struct ieee80211_country_ie_triplet *) country_ie;
896 struct ieee80211_reg_rule *reg_rule = NULL;
897 struct ieee80211_freq_range *freq_range = NULL;
898 struct ieee80211_power_rule *power_rule = NULL;
899
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500900 /*
901 * Must parse if dot11RegulatoryClassesRequired is true,
902 * we don't support this yet
903 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800904 if (triplet->ext.reg_extension_id >=
905 IEEE80211_COUNTRY_EXTENSION_ID) {
906 country_ie += 3;
907 country_ie_len -= 3;
908 continue;
909 }
910
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500911 if (triplet->chans.first_channel == 0) {
912 country_ie++;
913 country_ie_len--;
914 break;
915 }
916
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800917 reg_rule = &rd->reg_rules[i];
918 freq_range = &reg_rule->freq_range;
919 power_rule = &reg_rule->power_rule;
920
921 reg_rule->flags = flags;
922
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800923 /* 2 GHz */
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500924 if (band == IEEE80211_BAND_2GHZ)
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800925 end_channel = triplet->chans.first_channel +
Luis R. Rodrigueze99c7cd2010-01-07 17:24:55 -0500926 triplet->chans.num_channels -1;
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800927 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800928 end_channel = triplet->chans.first_channel +
929 (4 * (triplet->chans.num_channels - 1));
930
Luis R. Rodriguez84920e32010-01-14 20:08:20 -0500931 end_channel = max_subband_chan(band,
932 triplet->chans.first_channel,
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500933 end_channel,
934 triplet->chans.max_power,
935 &country_ie,
936 &country_ie_len);
937
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500938 /*
939 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800940 * the actual band edge, not the center of freq for
941 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500942 * the channels passed
943 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800944 freq_range->start_freq_khz =
945 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
946 triplet->chans.first_channel) - 10);
947 freq_range->end_freq_khz =
948 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800949 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800950
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500951 /*
952 * These are large arbitrary values we use to intersect later.
953 * Increment this if we ever support >= 40 MHz channels
954 * in IEEE 802.11
955 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800956 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
957 power_rule->max_antenna_gain = DBI_TO_MBI(100);
Luis R. Rodriguez08030db2010-01-07 17:24:56 -0500958 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800959
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800960 i++;
961
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500962 if (country_ie_len >= 3) {
963 country_ie += 3;
964 country_ie_len -= 3;
965 }
966
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800967 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
968 }
969
970 return rd;
971}
972
973
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500974/*
975 * Helper for regdom_intersect(), this does the real
976 * mathematical intersection fun
977 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700978static int reg_rules_intersect(
979 const struct ieee80211_reg_rule *rule1,
980 const struct ieee80211_reg_rule *rule2,
981 struct ieee80211_reg_rule *intersected_rule)
982{
983 const struct ieee80211_freq_range *freq_range1, *freq_range2;
984 struct ieee80211_freq_range *freq_range;
985 const struct ieee80211_power_rule *power_rule1, *power_rule2;
986 struct ieee80211_power_rule *power_rule;
987 u32 freq_diff;
988
989 freq_range1 = &rule1->freq_range;
990 freq_range2 = &rule2->freq_range;
991 freq_range = &intersected_rule->freq_range;
992
993 power_rule1 = &rule1->power_rule;
994 power_rule2 = &rule2->power_rule;
995 power_rule = &intersected_rule->power_rule;
996
997 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
998 freq_range2->start_freq_khz);
999 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
1000 freq_range2->end_freq_khz);
1001 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
1002 freq_range2->max_bandwidth_khz);
1003
1004 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
1005 if (freq_range->max_bandwidth_khz > freq_diff)
1006 freq_range->max_bandwidth_khz = freq_diff;
1007
1008 power_rule->max_eirp = min(power_rule1->max_eirp,
1009 power_rule2->max_eirp);
1010 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
1011 power_rule2->max_antenna_gain);
1012
1013 intersected_rule->flags = (rule1->flags | rule2->flags);
1014
1015 if (!is_valid_reg_rule(intersected_rule))
1016 return -EINVAL;
1017
1018 return 0;
1019}
1020
1021/**
1022 * regdom_intersect - do the intersection between two regulatory domains
1023 * @rd1: first regulatory domain
1024 * @rd2: second regulatory domain
1025 *
1026 * Use this function to get the intersection between two regulatory domains.
1027 * Once completed we will mark the alpha2 for the rd as intersected, "98",
1028 * as no one single alpha2 can represent this regulatory domain.
1029 *
1030 * Returns a pointer to the regulatory domain structure which will hold the
1031 * resulting intersection of rules between rd1 and rd2. We will
1032 * kzalloc() this structure for you.
1033 */
1034static struct ieee80211_regdomain *regdom_intersect(
1035 const struct ieee80211_regdomain *rd1,
1036 const struct ieee80211_regdomain *rd2)
1037{
1038 int r, size_of_regd;
1039 unsigned int x, y;
1040 unsigned int num_rules = 0, rule_idx = 0;
1041 const struct ieee80211_reg_rule *rule1, *rule2;
1042 struct ieee80211_reg_rule *intersected_rule;
1043 struct ieee80211_regdomain *rd;
1044 /* This is just a dummy holder to help us count */
1045 struct ieee80211_reg_rule irule;
1046
1047 /* Uses the stack temporarily for counter arithmetic */
1048 intersected_rule = &irule;
1049
1050 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
1051
1052 if (!rd1 || !rd2)
1053 return NULL;
1054
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001055 /*
1056 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001057 * build them. This is to so we can malloc() and free() a
1058 * regdomain once. The reason we use reg_rules_intersect() here
1059 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001060 * All rules that do check out OK are valid.
1061 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001062
1063 for (x = 0; x < rd1->n_reg_rules; x++) {
1064 rule1 = &rd1->reg_rules[x];
1065 for (y = 0; y < rd2->n_reg_rules; y++) {
1066 rule2 = &rd2->reg_rules[y];
1067 if (!reg_rules_intersect(rule1, rule2,
1068 intersected_rule))
1069 num_rules++;
1070 memset(intersected_rule, 0,
1071 sizeof(struct ieee80211_reg_rule));
1072 }
1073 }
1074
1075 if (!num_rules)
1076 return NULL;
1077
1078 size_of_regd = sizeof(struct ieee80211_regdomain) +
1079 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
1080
1081 rd = kzalloc(size_of_regd, GFP_KERNEL);
1082 if (!rd)
1083 return NULL;
1084
1085 for (x = 0; x < rd1->n_reg_rules; x++) {
1086 rule1 = &rd1->reg_rules[x];
1087 for (y = 0; y < rd2->n_reg_rules; y++) {
1088 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001089 /*
1090 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001091 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001092 * a memcpy()
1093 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001094 intersected_rule = &rd->reg_rules[rule_idx];
1095 r = reg_rules_intersect(rule1, rule2,
1096 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001097 /*
1098 * No need to memset here the intersected rule here as
1099 * we're not using the stack anymore
1100 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001101 if (r)
1102 continue;
1103 rule_idx++;
1104 }
1105 }
1106
1107 if (rule_idx != num_rules) {
1108 kfree(rd);
1109 return NULL;
1110 }
1111
1112 rd->n_reg_rules = num_rules;
1113 rd->alpha2[0] = '9';
1114 rd->alpha2[1] = '8';
1115
1116 return rd;
1117}
1118
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001119/*
1120 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1121 * want to just have the channel structure use these
1122 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001123static u32 map_regdom_flags(u32 rd_flags)
1124{
1125 u32 channel_flags = 0;
1126 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
1127 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1128 if (rd_flags & NL80211_RRF_NO_IBSS)
1129 channel_flags |= IEEE80211_CHAN_NO_IBSS;
1130 if (rd_flags & NL80211_RRF_DFS)
1131 channel_flags |= IEEE80211_CHAN_RADAR;
1132 return channel_flags;
1133}
1134
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001135static int freq_reg_info_regd(struct wiphy *wiphy,
1136 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001137 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001138 const struct ieee80211_reg_rule **reg_rule,
1139 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +01001140{
1141 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001142 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001143 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001144 bool bw_fits = false;
1145
1146 if (!desired_bw_khz)
1147 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +01001148
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001149 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001150
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001151 /*
1152 * Follow the driver's regulatory domain, if present, unless a country
1153 * IE has been processed or a user wants to help complaince further
1154 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001155 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1156 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001157 wiphy->regd)
1158 regd = wiphy->regd;
1159
1160 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001161 return -EINVAL;
1162
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001163 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001164 const struct ieee80211_reg_rule *rr;
1165 const struct ieee80211_freq_range *fr = NULL;
1166 const struct ieee80211_power_rule *pr = NULL;
1167
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001168 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001169 fr = &rr->freq_range;
1170 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001171
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001172 /*
1173 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001174 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001175 * not overwrite it once found
1176 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001177 if (!band_rule_found)
1178 band_rule_found = freq_in_rule_band(fr, center_freq);
1179
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001180 bw_fits = reg_does_bw_fit(fr,
1181 center_freq,
1182 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001183
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001184 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001185 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001186 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001187 }
1188 }
1189
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001190 if (!band_rule_found)
1191 return -ERANGE;
1192
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001193 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001194}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -08001195EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001196
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001197int freq_reg_info(struct wiphy *wiphy,
1198 u32 center_freq,
1199 u32 desired_bw_khz,
1200 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001201{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001202 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001203 return freq_reg_info_regd(wiphy,
1204 center_freq,
1205 desired_bw_khz,
1206 reg_rule,
1207 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001208}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001209
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001210/*
1211 * Note that right now we assume the desired channel bandwidth
1212 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1213 * per channel, the primary and the extension channel). To support
1214 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1215 * new ieee80211_channel.target_bw and re run the regulatory check
1216 * on the wiphy with the target_bw specified. Then we can simply use
1217 * that below for the desired_bw_khz below.
1218 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001219static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1220 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001221{
1222 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001223 u32 flags, bw_flags = 0;
1224 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001225 const struct ieee80211_reg_rule *reg_rule = NULL;
1226 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001227 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001228 struct ieee80211_supported_band *sband;
1229 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001230 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001231
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001232 assert_cfg80211_lock();
1233
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001234 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1235
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001236 sband = wiphy->bands[band];
1237 BUG_ON(chan_idx >= sband->n_channels);
1238 chan = &sband->channels[chan_idx];
1239
1240 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001241
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001242 r = freq_reg_info(wiphy,
1243 MHZ_TO_KHZ(chan->center_freq),
1244 desired_bw_khz,
1245 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001246
1247 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001248 /*
1249 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001250 * with a frequency range on the center_freq's band, since
1251 * IEEE-802.11 allows for a country IE to have a subset of the
1252 * regulatory information provided in a country we ignore
1253 * disabling the channel unless at least one reg rule was
1254 * found on the center_freq's band. For details see this
1255 * clarification:
1256 *
1257 * http://tinyurl.com/11d-clarification
1258 */
1259 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001260 last_request->initiator ==
1261 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001262 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001263 "intact on %s - no rule found in band on "
1264 "Country IE\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001265 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001266 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001267 /*
1268 * In this case we know the country IE has at least one reg rule
1269 * for the band so we respect its band definitions
1270 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001271 if (last_request->initiator ==
1272 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001273 REG_DBG_PRINT("cfg80211: Disabling "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001274 "channel %d MHz on %s due to "
1275 "Country IE\n",
1276 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001277 flags |= IEEE80211_CHAN_DISABLED;
1278 chan->flags = flags;
1279 }
Johannes Berg8318d782008-01-24 19:38:38 +01001280 return;
1281 }
1282
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001283 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001284 freq_range = &reg_rule->freq_range;
1285
1286 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1287 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001288
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001289 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001290 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001291 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001292 /*
1293 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001294 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001295 * settings
1296 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001297 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001298 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001299 chan->max_antenna_gain = chan->orig_mag =
1300 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001301 chan->max_power = chan->orig_mpwr =
1302 (int) MBM_TO_DBM(power_rule->max_eirp);
1303 return;
1304 }
1305
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001306 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001307 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001308 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -04001309 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001310 chan->max_power = min(chan->orig_mpwr,
1311 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001312 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001313 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001314}
1315
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001316static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001317{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001318 unsigned int i;
1319 struct ieee80211_supported_band *sband;
1320
1321 BUG_ON(!wiphy->bands[band]);
1322 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001323
1324 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001325 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001326}
1327
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001328static bool ignore_reg_update(struct wiphy *wiphy,
1329 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001330{
1331 if (!last_request)
1332 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001333 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001334 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001335 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001336 /*
1337 * wiphy->regd will be set once the device has its own
1338 * desired regulatory domain set
1339 */
Johannes Berg5be83de2009-11-19 00:56:28 +01001340 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001341 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001342 return true;
1343 return false;
1344}
1345
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001346static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001347{
Johannes Berg79c97e92009-07-07 03:56:12 +02001348 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001349
Johannes Berg79c97e92009-07-07 03:56:12 +02001350 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1351 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001352}
1353
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001354static void handle_reg_beacon(struct wiphy *wiphy,
1355 unsigned int chan_idx,
1356 struct reg_beacon *reg_beacon)
1357{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001358 struct ieee80211_supported_band *sband;
1359 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001360 bool channel_changed = false;
1361 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001362
1363 assert_cfg80211_lock();
1364
1365 sband = wiphy->bands[reg_beacon->chan.band];
1366 chan = &sband->channels[chan_idx];
1367
1368 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1369 return;
1370
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001371 if (chan->beacon_found)
1372 return;
1373
1374 chan->beacon_found = true;
1375
Johannes Berg5be83de2009-11-19 00:56:28 +01001376 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001377 return;
1378
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001379 chan_before.center_freq = chan->center_freq;
1380 chan_before.flags = chan->flags;
1381
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001382 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001383 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001384 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001385 }
1386
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001387 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001388 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001389 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001390 }
1391
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001392 if (channel_changed)
1393 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001394}
1395
1396/*
1397 * Called when a scan on a wiphy finds a beacon on
1398 * new channel
1399 */
1400static void wiphy_update_new_beacon(struct wiphy *wiphy,
1401 struct reg_beacon *reg_beacon)
1402{
1403 unsigned int i;
1404 struct ieee80211_supported_band *sband;
1405
1406 assert_cfg80211_lock();
1407
1408 if (!wiphy->bands[reg_beacon->chan.band])
1409 return;
1410
1411 sband = wiphy->bands[reg_beacon->chan.band];
1412
1413 for (i = 0; i < sband->n_channels; i++)
1414 handle_reg_beacon(wiphy, i, reg_beacon);
1415}
1416
1417/*
1418 * Called upon reg changes or a new wiphy is added
1419 */
1420static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1421{
1422 unsigned int i;
1423 struct ieee80211_supported_band *sband;
1424 struct reg_beacon *reg_beacon;
1425
1426 assert_cfg80211_lock();
1427
1428 if (list_empty(&reg_beacon_list))
1429 return;
1430
1431 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1432 if (!wiphy->bands[reg_beacon->chan.band])
1433 continue;
1434 sband = wiphy->bands[reg_beacon->chan.band];
1435 for (i = 0; i < sband->n_channels; i++)
1436 handle_reg_beacon(wiphy, i, reg_beacon);
1437 }
1438}
1439
1440static bool reg_is_world_roaming(struct wiphy *wiphy)
1441{
1442 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1443 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1444 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001445 if (last_request &&
1446 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001447 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001448 return true;
1449 return false;
1450}
1451
1452/* Reap the advantages of previously found beacons */
1453static void reg_process_beacons(struct wiphy *wiphy)
1454{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001455 /*
1456 * Means we are just firing up cfg80211, so no beacons would
1457 * have been processed yet.
1458 */
1459 if (!last_request)
1460 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001461 if (!reg_is_world_roaming(wiphy))
1462 return;
1463 wiphy_update_beacon_reg(wiphy);
1464}
1465
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001466static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1467{
1468 if (!chan)
1469 return true;
1470 if (chan->flags & IEEE80211_CHAN_DISABLED)
1471 return true;
1472 /* This would happen when regulatory rules disallow HT40 completely */
1473 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1474 return true;
1475 return false;
1476}
1477
1478static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1479 enum ieee80211_band band,
1480 unsigned int chan_idx)
1481{
1482 struct ieee80211_supported_band *sband;
1483 struct ieee80211_channel *channel;
1484 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1485 unsigned int i;
1486
1487 assert_cfg80211_lock();
1488
1489 sband = wiphy->bands[band];
1490 BUG_ON(chan_idx >= sband->n_channels);
1491 channel = &sband->channels[chan_idx];
1492
1493 if (is_ht40_not_allowed(channel)) {
1494 channel->flags |= IEEE80211_CHAN_NO_HT40;
1495 return;
1496 }
1497
1498 /*
1499 * We need to ensure the extension channels exist to
1500 * be able to use HT40- or HT40+, this finds them (or not)
1501 */
1502 for (i = 0; i < sband->n_channels; i++) {
1503 struct ieee80211_channel *c = &sband->channels[i];
1504 if (c->center_freq == (channel->center_freq - 20))
1505 channel_before = c;
1506 if (c->center_freq == (channel->center_freq + 20))
1507 channel_after = c;
1508 }
1509
1510 /*
1511 * Please note that this assumes target bandwidth is 20 MHz,
1512 * if that ever changes we also need to change the below logic
1513 * to include that as well.
1514 */
1515 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001516 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001517 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001518 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001519
1520 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001521 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001522 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001523 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001524}
1525
1526static void reg_process_ht_flags_band(struct wiphy *wiphy,
1527 enum ieee80211_band band)
1528{
1529 unsigned int i;
1530 struct ieee80211_supported_band *sband;
1531
1532 BUG_ON(!wiphy->bands[band]);
1533 sband = wiphy->bands[band];
1534
1535 for (i = 0; i < sband->n_channels; i++)
1536 reg_process_ht_flags_channel(wiphy, band, i);
1537}
1538
1539static void reg_process_ht_flags(struct wiphy *wiphy)
1540{
1541 enum ieee80211_band band;
1542
1543 if (!wiphy)
1544 return;
1545
1546 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1547 if (wiphy->bands[band])
1548 reg_process_ht_flags_band(wiphy, band);
1549 }
1550
1551}
1552
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001553void wiphy_update_regulatory(struct wiphy *wiphy,
1554 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001555{
1556 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001557
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001558 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001559 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001560 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001561 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001562 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001563 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001564out:
1565 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001566 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001567 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001568 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001569}
1570
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001571static void handle_channel_custom(struct wiphy *wiphy,
1572 enum ieee80211_band band,
1573 unsigned int chan_idx,
1574 const struct ieee80211_regdomain *regd)
1575{
1576 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001577 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1578 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001579 const struct ieee80211_reg_rule *reg_rule = NULL;
1580 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001581 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001582 struct ieee80211_supported_band *sband;
1583 struct ieee80211_channel *chan;
1584
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001585 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001586
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001587 sband = wiphy->bands[band];
1588 BUG_ON(chan_idx >= sband->n_channels);
1589 chan = &sband->channels[chan_idx];
1590
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001591 r = freq_reg_info_regd(wiphy,
1592 MHZ_TO_KHZ(chan->center_freq),
1593 desired_bw_khz,
1594 &reg_rule,
1595 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001596
1597 if (r) {
1598 chan->flags = IEEE80211_CHAN_DISABLED;
1599 return;
1600 }
1601
1602 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001603 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001604
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001605 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1606 bw_flags = IEEE80211_CHAN_NO_HT40;
1607
1608 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001609 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001610 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1611}
1612
1613static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1614 const struct ieee80211_regdomain *regd)
1615{
1616 unsigned int i;
1617 struct ieee80211_supported_band *sband;
1618
1619 BUG_ON(!wiphy->bands[band]);
1620 sband = wiphy->bands[band];
1621
1622 for (i = 0; i < sband->n_channels; i++)
1623 handle_channel_custom(wiphy, band, i, regd);
1624}
1625
1626/* Used by drivers prior to wiphy registration */
1627void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1628 const struct ieee80211_regdomain *regd)
1629{
1630 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001631 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001632
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001633 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001634 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001635 if (!wiphy->bands[band])
1636 continue;
1637 handle_band_custom(wiphy, band, regd);
1638 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001639 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001640 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001641
1642 /*
1643 * no point in calling this if it won't have any effect
1644 * on your device's supportd bands.
1645 */
1646 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001647}
1648EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1649
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001650/*
1651 * Return value which can be used by ignore_request() to indicate
1652 * it has been determined we should intersect two regulatory domains
1653 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001654#define REG_INTERSECT 1
1655
Johannes Berg84fa4f42008-10-24 20:32:23 +02001656/* This has the logic which determines when a new request
1657 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001658static int ignore_request(struct wiphy *wiphy,
1659 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001660{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001661 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001662
1663 assert_cfg80211_lock();
1664
Johannes Berg84fa4f42008-10-24 20:32:23 +02001665 /* All initial requests are respected */
1666 if (!last_request)
1667 return 0;
1668
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001669 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001670 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001671 return 0;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001672 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001673
1674 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1675
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001676 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001677 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001678 if (last_request->initiator ==
1679 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001680 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001681 /*
1682 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001683 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001684 * intersect them, but that seems unlikely
1685 * to be correct. Reject second one for now.
1686 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001687 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001688 return -EOPNOTSUPP;
1689 return -EALREADY;
1690 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001691 /*
1692 * Two consecutive Country IE hints on the same wiphy.
1693 * This should be picked up early by the driver/stack
1694 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001695 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001696 return 0;
1697 return -EALREADY;
1698 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001699 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001700 case NL80211_REGDOM_SET_BY_DRIVER:
1701 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001702 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001703 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001704 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001705 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001706
1707 /*
1708 * This would happen if you unplug and plug your card
1709 * back in or if you add a new device for which the previously
1710 * loaded card also agrees on the regulatory domain.
1711 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001712 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001713 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001714 return -EALREADY;
1715
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001716 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001717 case NL80211_REGDOM_SET_BY_USER:
1718 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001719 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001720 /*
1721 * If the user knows better the user should set the regdom
1722 * to their country before the IE is picked up
1723 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001724 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001725 last_request->intersect)
1726 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001727 /*
1728 * Process user requests only after previous user/driver/core
1729 * requests have been processed
1730 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001731 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1732 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1733 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001734 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001735 return -EAGAIN;
1736 }
1737
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001738 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001739 return -EALREADY;
1740
Johannes Berg84fa4f42008-10-24 20:32:23 +02001741 return 0;
1742 }
1743
1744 return -EINVAL;
1745}
1746
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001747/**
1748 * __regulatory_hint - hint to the wireless core a regulatory domain
1749 * @wiphy: if the hint comes from country information from an AP, this
1750 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001751 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001752 *
1753 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001754 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001755 *
1756 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1757 * already been set or other standard error codes.
1758 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001759 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001760 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001761static int __regulatory_hint(struct wiphy *wiphy,
1762 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001763{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001764 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001765 int r = 0;
1766
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001767 assert_cfg80211_lock();
1768
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001769 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001770
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001771 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001772 if (pending_request->initiator ==
1773 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001774 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001775 if (r) {
1776 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001777 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001778 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001779 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001780 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001781 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001782 /*
1783 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001784 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001785 * wiphy
1786 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001787 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001788 pending_request->initiator ==
1789 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001790 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001791 if (r) {
1792 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001793 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001794 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001795 r = -EALREADY;
1796 goto new_request;
1797 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001798 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001799 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001800 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001801
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001802new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001803 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001804
1805 last_request = pending_request;
1806 last_request->intersect = intersect;
1807
1808 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001809
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001810 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1811 user_alpha2[0] = last_request->alpha2[0];
1812 user_alpha2[1] = last_request->alpha2[1];
1813 }
1814
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001815 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001816 if (r < 0) {
1817 /*
1818 * Since CRDA will not be called in this case as we already
1819 * have applied the requested regulatory domain before we just
1820 * inform userspace we have processed the request
1821 */
1822 if (r == -EALREADY)
1823 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001824 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001825 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001826
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001827 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001828}
1829
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001830/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001831static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001832{
1833 int r = 0;
1834 struct wiphy *wiphy = NULL;
1835
1836 BUG_ON(!reg_request->alpha2);
1837
1838 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001839 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001840
1841 if (wiphy_idx_valid(reg_request->wiphy_idx))
1842 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1843
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001844 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001845 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001846 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001847 goto out;
1848 }
1849
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001850 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001851 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001852 if (r == -EALREADY && wiphy &&
1853 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001854 wiphy_update_regulatory(wiphy, reg_request->initiator);
1855out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001856 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001857 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001858}
1859
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001860/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001861static void reg_process_pending_hints(void)
1862 {
1863 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001864
1865 spin_lock(&reg_requests_lock);
1866 while (!list_empty(&reg_requests_list)) {
1867 reg_request = list_first_entry(&reg_requests_list,
1868 struct regulatory_request,
1869 list);
1870 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001871
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001872 spin_unlock(&reg_requests_lock);
1873 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001874 spin_lock(&reg_requests_lock);
1875 }
1876 spin_unlock(&reg_requests_lock);
1877}
1878
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001879/* Processes beacon hints -- this has nothing to do with country IEs */
1880static void reg_process_pending_beacon_hints(void)
1881{
Johannes Berg79c97e92009-07-07 03:56:12 +02001882 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001883 struct reg_beacon *pending_beacon, *tmp;
1884
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001885 /*
1886 * No need to hold the reg_mutex here as we just touch wiphys
1887 * and do not read or access regulatory variables.
1888 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001889 mutex_lock(&cfg80211_mutex);
1890
1891 /* This goes through the _pending_ beacon list */
1892 spin_lock_bh(&reg_pending_beacons_lock);
1893
1894 if (list_empty(&reg_pending_beacons)) {
1895 spin_unlock_bh(&reg_pending_beacons_lock);
1896 goto out;
1897 }
1898
1899 list_for_each_entry_safe(pending_beacon, tmp,
1900 &reg_pending_beacons, list) {
1901
1902 list_del_init(&pending_beacon->list);
1903
1904 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001905 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1906 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001907
1908 /* Remembers the beacon hint for new wiphys or reg changes */
1909 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1910 }
1911
1912 spin_unlock_bh(&reg_pending_beacons_lock);
1913out:
1914 mutex_unlock(&cfg80211_mutex);
1915}
1916
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001917static void reg_todo(struct work_struct *work)
1918{
1919 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001920 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001921}
1922
1923static DECLARE_WORK(reg_work, reg_todo);
1924
1925static void queue_regulatory_request(struct regulatory_request *request)
1926{
1927 spin_lock(&reg_requests_lock);
1928 list_add_tail(&request->list, &reg_requests_list);
1929 spin_unlock(&reg_requests_lock);
1930
1931 schedule_work(&reg_work);
1932}
1933
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001934/*
1935 * Core regulatory hint -- happens during cfg80211_init()
1936 * and when we restore regulatory settings.
1937 */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001938static int regulatory_hint_core(const char *alpha2)
1939{
1940 struct regulatory_request *request;
1941
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05001942 kfree(last_request);
1943 last_request = NULL;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001944
1945 request = kzalloc(sizeof(struct regulatory_request),
1946 GFP_KERNEL);
1947 if (!request)
1948 return -ENOMEM;
1949
1950 request->alpha2[0] = alpha2[0];
1951 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001952 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001953
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001954 /*
1955 * This ensures last_request is populated once modules
1956 * come swinging in and calling regulatory hints and
1957 * wiphy_apply_custom_regulatory().
1958 */
Luis R. Rodrigueza2bff262010-01-29 19:58:56 -05001959 reg_process_hint(request);
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001960
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001961 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001962}
1963
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001964/* User hints */
1965int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001966{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001967 struct regulatory_request *request;
1968
Johannes Bergbe3d4812008-10-24 20:32:21 +02001969 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001970
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001971 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1972 if (!request)
1973 return -ENOMEM;
1974
1975 request->wiphy_idx = WIPHY_IDX_STALE;
1976 request->alpha2[0] = alpha2[0];
1977 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001978 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001979
1980 queue_regulatory_request(request);
1981
1982 return 0;
1983}
1984
1985/* Driver hints */
1986int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1987{
1988 struct regulatory_request *request;
1989
1990 BUG_ON(!alpha2);
1991 BUG_ON(!wiphy);
1992
1993 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1994 if (!request)
1995 return -ENOMEM;
1996
1997 request->wiphy_idx = get_wiphy_idx(wiphy);
1998
1999 /* Must have registered wiphy first */
2000 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
2001
2002 request->alpha2[0] = alpha2[0];
2003 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002004 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002005
2006 queue_regulatory_request(request);
2007
2008 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002009}
2010EXPORT_SYMBOL(regulatory_hint);
2011
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002012/* Caller must hold reg_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002013static bool reg_same_country_ie_hint(struct wiphy *wiphy,
2014 u32 country_ie_checksum)
2015{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002016 struct wiphy *request_wiphy;
2017
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002018 assert_reg_lock();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002019
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04002020 if (unlikely(last_request->initiator !=
2021 NL80211_REGDOM_SET_BY_COUNTRY_IE))
2022 return false;
2023
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002024 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2025
2026 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002027 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002028
2029 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002030 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002031 /*
2032 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002033 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002034 * on the device
2035 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002036 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
2037 return true;
2038 return false;
2039}
2040
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002041/*
2042 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
2043 * therefore cannot iterate over the rdev list here.
2044 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002045void regulatory_hint_11d(struct wiphy *wiphy,
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05002046 enum ieee80211_band band,
2047 u8 *country_ie,
2048 u8 country_ie_len)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002049{
2050 struct ieee80211_regdomain *rd = NULL;
2051 char alpha2[2];
2052 u32 checksum = 0;
2053 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002054 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002055
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002056 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002057
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07002058 if (unlikely(!last_request))
2059 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05002060
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002061 /* IE len must be evenly divisible by 2 */
2062 if (country_ie_len & 0x01)
2063 goto out;
2064
2065 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2066 goto out;
2067
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002068 /*
2069 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002070 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002071 * we were able to process the last regulatory_hint_11d() call
2072 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002073 if (country_ie_regdomain)
2074 goto out;
2075
2076 alpha2[0] = country_ie[0];
2077 alpha2[1] = country_ie[1];
2078
2079 if (country_ie[2] == 'I')
2080 env = ENVIRON_INDOOR;
2081 else if (country_ie[2] == 'O')
2082 env = ENVIRON_OUTDOOR;
2083
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002084 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002085 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002086 * We leave conflict resolution to the workqueue, where can hold
2087 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002088 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04002089 if (likely(last_request->initiator ==
2090 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002091 wiphy_idx_valid(last_request->wiphy_idx)))
2092 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002093
Luis R. Rodriguez84920e32010-01-14 20:08:20 -05002094 rd = country_ie_2_rd(band, country_ie, country_ie_len, &checksum);
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002095 if (!rd) {
2096 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002097 goto out;
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002098 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002099
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002100 /*
2101 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002102 * the future when we want to add suspend/resume support and having
2103 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002104 * move to another AP. Right now we just trust the first AP.
2105 *
2106 * If we hit this before we add this support we want to be informed of
2107 * it as it would indicate a mistake in the current design
2108 */
2109 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002110 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002111
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002112 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2113 if (!request)
2114 goto free_rd_out;
2115
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002116 /*
2117 * We keep this around for when CRDA comes back with a response so
2118 * we can intersect with that
2119 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002120 country_ie_regdomain = rd;
2121
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002122 request->wiphy_idx = get_wiphy_idx(wiphy);
2123 request->alpha2[0] = rd->alpha2[0];
2124 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002125 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002126 request->country_ie_checksum = checksum;
2127 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002128
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002129 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002130
2131 queue_regulatory_request(request);
2132
2133 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002134
2135free_rd_out:
2136 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002137out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002138 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002139}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002140
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002141static void restore_alpha2(char *alpha2, bool reset_user)
2142{
2143 /* indicates there is no alpha2 to consider for restoration */
2144 alpha2[0] = '9';
2145 alpha2[1] = '7';
2146
2147 /* The user setting has precedence over the module parameter */
2148 if (is_user_regdom_saved()) {
2149 /* Unless we're asked to ignore it and reset it */
2150 if (reset_user) {
2151 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2152 "including user preference\n");
2153 user_alpha2[0] = '9';
2154 user_alpha2[1] = '7';
2155
2156 /*
2157 * If we're ignoring user settings, we still need to
2158 * check the module parameter to ensure we put things
2159 * back as they were for a full restore.
2160 */
2161 if (!is_world_regdom(ieee80211_regdom)) {
2162 REG_DBG_PRINT("cfg80211: Keeping preference on "
2163 "module parameter ieee80211_regdom: %c%c\n",
2164 ieee80211_regdom[0],
2165 ieee80211_regdom[1]);
2166 alpha2[0] = ieee80211_regdom[0];
2167 alpha2[1] = ieee80211_regdom[1];
2168 }
2169 } else {
2170 REG_DBG_PRINT("cfg80211: Restoring regulatory settings "
2171 "while preserving user preference for: %c%c\n",
2172 user_alpha2[0],
2173 user_alpha2[1]);
2174 alpha2[0] = user_alpha2[0];
2175 alpha2[1] = user_alpha2[1];
2176 }
2177 } else if (!is_world_regdom(ieee80211_regdom)) {
2178 REG_DBG_PRINT("cfg80211: Keeping preference on "
2179 "module parameter ieee80211_regdom: %c%c\n",
2180 ieee80211_regdom[0],
2181 ieee80211_regdom[1]);
2182 alpha2[0] = ieee80211_regdom[0];
2183 alpha2[1] = ieee80211_regdom[1];
2184 } else
2185 REG_DBG_PRINT("cfg80211: Restoring regulatory settings\n");
2186}
2187
2188/*
2189 * Restoring regulatory settings involves ingoring any
2190 * possibly stale country IE information and user regulatory
2191 * settings if so desired, this includes any beacon hints
2192 * learned as we could have traveled outside to another country
2193 * after disconnection. To restore regulatory settings we do
2194 * exactly what we did at bootup:
2195 *
2196 * - send a core regulatory hint
2197 * - send a user regulatory hint if applicable
2198 *
2199 * Device drivers that send a regulatory hint for a specific country
2200 * keep their own regulatory domain on wiphy->regd so that does does
2201 * not need to be remembered.
2202 */
2203static void restore_regulatory_settings(bool reset_user)
2204{
2205 char alpha2[2];
2206 struct reg_beacon *reg_beacon, *btmp;
2207
2208 mutex_lock(&cfg80211_mutex);
2209 mutex_lock(&reg_mutex);
2210
2211 reset_regdomains();
2212 restore_alpha2(alpha2, reset_user);
2213
2214 /* Clear beacon hints */
2215 spin_lock_bh(&reg_pending_beacons_lock);
2216 if (!list_empty(&reg_pending_beacons)) {
2217 list_for_each_entry_safe(reg_beacon, btmp,
2218 &reg_pending_beacons, list) {
2219 list_del(&reg_beacon->list);
2220 kfree(reg_beacon);
2221 }
2222 }
2223 spin_unlock_bh(&reg_pending_beacons_lock);
2224
2225 if (!list_empty(&reg_beacon_list)) {
2226 list_for_each_entry_safe(reg_beacon, btmp,
2227 &reg_beacon_list, list) {
2228 list_del(&reg_beacon->list);
2229 kfree(reg_beacon);
2230 }
2231 }
2232
2233 /* First restore to the basic regulatory settings */
2234 cfg80211_regdomain = cfg80211_world_regdom;
2235
2236 mutex_unlock(&reg_mutex);
2237 mutex_unlock(&cfg80211_mutex);
2238
2239 regulatory_hint_core(cfg80211_regdomain->alpha2);
2240
2241 /*
2242 * This restores the ieee80211_regdom module parameter
2243 * preference or the last user requested regulatory
2244 * settings, user regulatory settings takes precedence.
2245 */
2246 if (is_an_alpha2(alpha2))
2247 regulatory_hint_user(user_alpha2);
2248}
2249
2250
2251void regulatory_hint_disconnect(void)
2252{
2253 REG_DBG_PRINT("cfg80211: All devices are disconnected, going to "
2254 "restore regulatory settings\n");
2255 restore_regulatory_settings(false);
2256}
2257
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002258static bool freq_is_chan_12_13_14(u16 freq)
2259{
2260 if (freq == ieee80211_channel_to_frequency(12) ||
2261 freq == ieee80211_channel_to_frequency(13) ||
2262 freq == ieee80211_channel_to_frequency(14))
2263 return true;
2264 return false;
2265}
2266
2267int regulatory_hint_found_beacon(struct wiphy *wiphy,
2268 struct ieee80211_channel *beacon_chan,
2269 gfp_t gfp)
2270{
2271 struct reg_beacon *reg_beacon;
2272
2273 if (likely((beacon_chan->beacon_found ||
2274 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
2275 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
2276 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
2277 return 0;
2278
2279 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2280 if (!reg_beacon)
2281 return -ENOMEM;
2282
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002283 REG_DBG_PRINT("cfg80211: Found new beacon on "
2284 "frequency: %d MHz (Ch %d) on %s\n",
2285 beacon_chan->center_freq,
2286 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2287 wiphy_name(wiphy));
2288
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002289 memcpy(&reg_beacon->chan, beacon_chan,
2290 sizeof(struct ieee80211_channel));
2291
2292
2293 /*
2294 * Since we can be called from BH or and non-BH context
2295 * we must use spin_lock_bh()
2296 */
2297 spin_lock_bh(&reg_pending_beacons_lock);
2298 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2299 spin_unlock_bh(&reg_pending_beacons_lock);
2300
2301 schedule_work(&reg_work);
2302
2303 return 0;
2304}
2305
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002306static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002307{
2308 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002309 const struct ieee80211_reg_rule *reg_rule = NULL;
2310 const struct ieee80211_freq_range *freq_range = NULL;
2311 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002312
Kalle Valo269ac5f2009-12-01 10:47:15 +02002313 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002314 "(max_antenna_gain, max_eirp)\n");
2315
2316 for (i = 0; i < rd->n_reg_rules; i++) {
2317 reg_rule = &rd->reg_rules[i];
2318 freq_range = &reg_rule->freq_range;
2319 power_rule = &reg_rule->power_rule;
2320
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002321 /*
2322 * There may not be documentation for max antenna gain
2323 * in certain regions
2324 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002325 if (power_rule->max_antenna_gain)
Kalle Valo269ac5f2009-12-01 10:47:15 +02002326 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002327 "(%d mBi, %d mBm)\n",
2328 freq_range->start_freq_khz,
2329 freq_range->end_freq_khz,
2330 freq_range->max_bandwidth_khz,
2331 power_rule->max_antenna_gain,
2332 power_rule->max_eirp);
2333 else
Kalle Valo269ac5f2009-12-01 10:47:15 +02002334 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002335 "(N/A, %d mBm)\n",
2336 freq_range->start_freq_khz,
2337 freq_range->end_freq_khz,
2338 freq_range->max_bandwidth_khz,
2339 power_rule->max_eirp);
2340 }
2341}
2342
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002343static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002344{
2345
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002346 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002347
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002348 if (last_request->initiator ==
2349 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002350 struct cfg80211_registered_device *rdev;
2351 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002352 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002353 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002354 printk(KERN_INFO "cfg80211: Current regulatory "
2355 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002356 rdev->country_ie_alpha2[0],
2357 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002358 } else
2359 printk(KERN_INFO "cfg80211: Current regulatory "
Frans Pop55f98932010-03-24 19:46:29 +01002360 "domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002361 } else
Frans Pop55f98932010-03-24 19:46:29 +01002362 printk(KERN_INFO "cfg80211: Current regulatory "
2363 "domain intersected:\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002364 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002365 printk(KERN_INFO "cfg80211: World regulatory "
2366 "domain updated:\n");
2367 else {
2368 if (is_unknown_alpha2(rd->alpha2))
2369 printk(KERN_INFO "cfg80211: Regulatory domain "
2370 "changed to driver built-in settings "
2371 "(unknown country)\n");
2372 else
2373 printk(KERN_INFO "cfg80211: Regulatory domain "
2374 "changed to country: %c%c\n",
2375 rd->alpha2[0], rd->alpha2[1]);
2376 }
2377 print_rd_rules(rd);
2378}
2379
Johannes Berg2df78162008-10-28 16:49:41 +01002380static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002381{
2382 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2383 rd->alpha2[0], rd->alpha2[1]);
2384 print_rd_rules(rd);
2385}
2386
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002387#ifdef CONFIG_CFG80211_REG_DEBUG
2388static void reg_country_ie_process_debug(
2389 const struct ieee80211_regdomain *rd,
2390 const struct ieee80211_regdomain *country_ie_regdomain,
2391 const struct ieee80211_regdomain *intersected_rd)
2392{
2393 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2394 print_regdomain_info(country_ie_regdomain);
2395 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2396 print_regdomain_info(rd);
2397 if (intersected_rd) {
2398 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2399 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08002400 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002401 return;
2402 }
2403 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2404}
2405#else
2406static inline void reg_country_ie_process_debug(
2407 const struct ieee80211_regdomain *rd,
2408 const struct ieee80211_regdomain *country_ie_regdomain,
2409 const struct ieee80211_regdomain *intersected_rd)
2410{
2411}
2412#endif
2413
Johannes Bergd2372b32008-10-24 20:32:20 +02002414/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002415static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002416{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002417 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002418 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002419 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002420 /* Some basic sanity checks first */
2421
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002422 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002423 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002424 return -EINVAL;
2425 update_world_regdomain(rd);
2426 return 0;
2427 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002428
2429 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2430 !is_unknown_alpha2(rd->alpha2))
2431 return -EINVAL;
2432
Johannes Bergf6037d02008-10-21 11:01:33 +02002433 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002434 return -EINVAL;
2435
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002436 /*
2437 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002438 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002439 * and the pending request came in from a country IE
2440 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002441 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002442 /*
2443 * If someone else asked us to change the rd lets only bother
2444 * checking if the alpha2 changes if CRDA was already called
2445 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002446 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002447 return -EINVAL;
2448 }
2449
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002450 /*
2451 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002452 * and finally inform them of what we have done, in case they want
2453 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002454 * internal EEPROM data
2455 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002456
Johannes Bergf6037d02008-10-21 11:01:33 +02002457 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002458 return -EINVAL;
2459
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002460 if (!is_valid_rd(rd)) {
2461 printk(KERN_ERR "cfg80211: Invalid "
2462 "regulatory domain detected:\n");
2463 print_regdomain_info(rd);
2464 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002465 }
2466
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002467 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2468
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002469 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002470 int r;
2471
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002472 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002473 reset_regdomains();
2474 cfg80211_regdomain = rd;
2475 return 0;
2476 }
2477
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002478 /*
2479 * For a driver hint, lets copy the regulatory domain the
2480 * driver wanted to the wiphy to deal with conflicts
2481 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002482
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002483 /*
2484 * Userspace could have sent two replies with only
2485 * one kernel request.
2486 */
2487 if (request_wiphy->regd)
2488 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002489
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002490 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002491 if (r)
2492 return r;
2493
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002494 reset_regdomains();
2495 cfg80211_regdomain = rd;
2496 return 0;
2497 }
2498
2499 /* Intersection requires a bit more work */
2500
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002501 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002502
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002503 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2504 if (!intersected_rd)
2505 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002506
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002507 /*
2508 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002509 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002510 * domain we keep it for its private use
2511 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002512 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002513 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002514 else
2515 kfree(rd);
2516
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002517 rd = NULL;
2518
2519 reset_regdomains();
2520 cfg80211_regdomain = intersected_rd;
2521
2522 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002523 }
2524
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002525 /*
2526 * Country IE requests are handled a bit differently, we intersect
2527 * the country IE rd with what CRDA believes that country should have
2528 */
2529
Luis R. Rodriguez729e9c72009-05-31 18:24:34 -04002530 /*
2531 * Userspace could have sent two replies with only
2532 * one kernel request. By the second reply we would have
2533 * already processed and consumed the country_ie_regdomain.
2534 */
2535 if (!country_ie_regdomain)
2536 return -EALREADY;
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002537 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002538
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002539 /*
2540 * Intersect what CRDA returned and our what we
2541 * had built from the Country IE received
2542 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002543
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002544 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002545
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002546 reg_country_ie_process_debug(rd,
2547 country_ie_regdomain,
2548 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002549
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002550 kfree(country_ie_regdomain);
2551 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002552
2553 if (!intersected_rd)
2554 return -EINVAL;
2555
Johannes Berg79c97e92009-07-07 03:56:12 +02002556 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002557
Johannes Berg79c97e92009-07-07 03:56:12 +02002558 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2559 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2560 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002561
2562 BUG_ON(intersected_rd == rd);
2563
2564 kfree(rd);
2565 rd = NULL;
2566
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002567 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002568 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002569
2570 return 0;
2571}
2572
2573
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002574/*
2575 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002576 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002577 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2578 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002579int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002580{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002581 int r;
2582
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002583 assert_cfg80211_lock();
2584
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002585 mutex_lock(&reg_mutex);
2586
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002587 /* Note that this doesn't update the wiphys, this is done below */
2588 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002589 if (r) {
2590 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002591 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002592 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002593 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002594
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002595 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002596 if (!last_request->intersect)
2597 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002598
2599 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002600 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002601
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002602 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002603
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002604 nl80211_send_reg_change_event(last_request);
2605
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002606 mutex_unlock(&reg_mutex);
2607
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002608 return r;
2609}
2610
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002611/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002612void reg_device_remove(struct wiphy *wiphy)
2613{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002614 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002615
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002616 assert_cfg80211_lock();
2617
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002618 mutex_lock(&reg_mutex);
2619
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002620 kfree(wiphy->regd);
2621
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002622 if (last_request)
2623 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002624
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002625 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002626 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002627
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002628 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002629 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002630out:
2631 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002632}
2633
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002634int regulatory_init(void)
2635{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002636 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002637
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002638 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2639 if (IS_ERR(reg_pdev))
2640 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002641
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002642 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002643 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002644
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002645 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002646
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -05002647 user_alpha2[0] = '9';
2648 user_alpha2[1] = '7';
2649
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002650 /* We always try to get an update for the static regdomain */
2651 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002652 if (err) {
2653 if (err == -ENOMEM)
2654 return err;
2655 /*
2656 * N.B. kobject_uevent_env() can fail mainly for when we're out
2657 * memory which is handled and propagated appropriately above
2658 * but it can also fail during a netlink_broadcast() or during
2659 * early boot for call_usermodehelper(). For now treat these
2660 * errors as non-fatal.
2661 */
2662 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2663 "to call CRDA during init");
2664#ifdef CONFIG_CFG80211_REG_DEBUG
2665 /* We want to find out exactly why when debugging */
2666 WARN_ON(err);
2667#endif
2668 }
Johannes Berg734366d2008-09-15 10:56:48 +02002669
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002670 /*
2671 * Finally, if the user set the module parameter treat it
2672 * as a user hint.
2673 */
2674 if (!is_world_regdom(ieee80211_regdom))
2675 regulatory_hint_user(ieee80211_regdom);
2676
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002677 return 0;
2678}
2679
2680void regulatory_exit(void)
2681{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002682 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002683 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002684
2685 cancel_work_sync(&reg_work);
2686
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002687 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002688 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002689
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002690 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002691
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002692 kfree(country_ie_regdomain);
2693 country_ie_regdomain = NULL;
2694
Johannes Bergf6037d02008-10-21 11:01:33 +02002695 kfree(last_request);
2696
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002697 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002698
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002699 spin_lock_bh(&reg_pending_beacons_lock);
2700 if (!list_empty(&reg_pending_beacons)) {
2701 list_for_each_entry_safe(reg_beacon, btmp,
2702 &reg_pending_beacons, list) {
2703 list_del(&reg_beacon->list);
2704 kfree(reg_beacon);
2705 }
2706 }
2707 spin_unlock_bh(&reg_pending_beacons_lock);
2708
2709 if (!list_empty(&reg_beacon_list)) {
2710 list_for_each_entry_safe(reg_beacon, btmp,
2711 &reg_beacon_list, list) {
2712 list_del(&reg_beacon->list);
2713 kfree(reg_beacon);
2714 }
2715 }
2716
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002717 spin_lock(&reg_requests_lock);
2718 if (!list_empty(&reg_requests_list)) {
2719 list_for_each_entry_safe(reg_request, tmp,
2720 &reg_requests_list, list) {
2721 list_del(&reg_request->list);
2722 kfree(reg_request);
2723 }
2724 }
2725 spin_unlock(&reg_requests_lock);
2726
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002727 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002728 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002729}