blob: 9afc9168748b8695dfe4634e8ea80a7efc308877 [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>
Johannes Berg8318d782008-01-24 19:38:38 +010040#include <net/wireless.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070041#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010042#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070043#include "reg.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040044#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010045
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070046/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020047static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020048
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070049/* To trigger userspace events */
50static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010051
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070052/* Keep the ordering from large to small */
53static u32 supported_bandwidths[] = {
54 MHZ_TO_KHZ(40),
55 MHZ_TO_KHZ(20),
Johannes Berg8318d782008-01-24 19:38:38 +010056};
57
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050058/*
59 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020060 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050061 * information to give us an alpha2
62 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080063const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020064
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050065/*
66 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080067 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050068 * what it thinks should apply for the same country
69 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080070static const struct ieee80211_regdomain *country_ie_regdomain;
71
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050072/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050073static LIST_HEAD(reg_requests_list);
74static spinlock_t reg_requests_lock;
75
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050076/* Used to queue up beacon hints for review */
77static LIST_HEAD(reg_pending_beacons);
78static spinlock_t reg_pending_beacons_lock;
79
80/* Used to keep track of processed beacon hints */
81static LIST_HEAD(reg_beacon_list);
82
83struct reg_beacon {
84 struct list_head list;
85 struct ieee80211_channel chan;
86};
87
Johannes Berg734366d2008-09-15 10:56:48 +020088/* We keep a static world regulatory domain in case of the absence of CRDA */
89static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080090 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +020091 .alpha2 = "00",
92 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -050093 /* IEEE 802.11b/g, channels 1..11 */
94 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080095 /* IEEE 802.11b/g, channels 12..13. No HT40
96 * channel fits here. */
97 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -050098 NL80211_RRF_PASSIVE_SCAN |
99 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800100 /* IEEE 802.11 channel 14 - Only JP enables
101 * this and for 802.11b only */
102 REG_RULE(2484-10, 2484+10, 20, 6, 20,
103 NL80211_RRF_PASSIVE_SCAN |
104 NL80211_RRF_NO_IBSS |
105 NL80211_RRF_NO_OFDM),
106 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800107 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800108 NL80211_RRF_PASSIVE_SCAN |
109 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500110
111 /* NB: 5260 MHz - 5700 MHz requies DFS */
112
113 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800114 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500115 NL80211_RRF_PASSIVE_SCAN |
116 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200117 }
118};
119
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200120static const struct ieee80211_regdomain *cfg80211_world_regdom =
121 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200122
123#ifdef CONFIG_WIRELESS_OLD_REGULATORY
124static char *ieee80211_regdom = "US";
125module_param(ieee80211_regdom, charp, 0444);
126MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
127
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500128/*
129 * We assume 40 MHz bandwidth for the old regulatory work.
Johannes Berg734366d2008-09-15 10:56:48 +0200130 * We make emphasis we are using the exact same frequencies
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500131 * as before
132 */
Johannes Berg734366d2008-09-15 10:56:48 +0200133
134static const struct ieee80211_regdomain us_regdom = {
135 .n_reg_rules = 6,
136 .alpha2 = "US",
137 .reg_rules = {
138 /* IEEE 802.11b/g, channels 1..11 */
139 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
140 /* IEEE 802.11a, channel 36 */
141 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
142 /* IEEE 802.11a, channel 40 */
143 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
144 /* IEEE 802.11a, channel 44 */
145 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
146 /* IEEE 802.11a, channels 48..64 */
147 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
148 /* IEEE 802.11a, channels 149..165, outdoor */
149 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
150 }
151};
152
153static const struct ieee80211_regdomain jp_regdom = {
154 .n_reg_rules = 3,
155 .alpha2 = "JP",
156 .reg_rules = {
157 /* IEEE 802.11b/g, channels 1..14 */
158 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
159 /* IEEE 802.11a, channels 34..48 */
160 REG_RULE(5170-10, 5240+10, 40, 6, 20,
161 NL80211_RRF_PASSIVE_SCAN),
162 /* IEEE 802.11a, channels 52..64 */
163 REG_RULE(5260-10, 5320+10, 40, 6, 20,
164 NL80211_RRF_NO_IBSS |
165 NL80211_RRF_DFS),
166 }
167};
168
169static const struct ieee80211_regdomain eu_regdom = {
170 .n_reg_rules = 6,
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500171 /*
172 * This alpha2 is bogus, we leave it here just for stupid
173 * backward compatibility
174 */
Johannes Berg734366d2008-09-15 10:56:48 +0200175 .alpha2 = "EU",
176 .reg_rules = {
177 /* IEEE 802.11b/g, channels 1..13 */
178 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
179 /* IEEE 802.11a, channel 36 */
180 REG_RULE(5180-10, 5180+10, 40, 6, 23,
181 NL80211_RRF_PASSIVE_SCAN),
182 /* IEEE 802.11a, channel 40 */
183 REG_RULE(5200-10, 5200+10, 40, 6, 23,
184 NL80211_RRF_PASSIVE_SCAN),
185 /* IEEE 802.11a, channel 44 */
186 REG_RULE(5220-10, 5220+10, 40, 6, 23,
187 NL80211_RRF_PASSIVE_SCAN),
188 /* IEEE 802.11a, channels 48..64 */
189 REG_RULE(5240-10, 5320+10, 40, 6, 20,
190 NL80211_RRF_NO_IBSS |
191 NL80211_RRF_DFS),
192 /* IEEE 802.11a, channels 100..140 */
193 REG_RULE(5500-10, 5700+10, 40, 6, 30,
194 NL80211_RRF_NO_IBSS |
195 NL80211_RRF_DFS),
196 }
197};
198
199static const struct ieee80211_regdomain *static_regdom(char *alpha2)
200{
201 if (alpha2[0] == 'U' && alpha2[1] == 'S')
202 return &us_regdom;
203 if (alpha2[0] == 'J' && alpha2[1] == 'P')
204 return &jp_regdom;
205 if (alpha2[0] == 'E' && alpha2[1] == 'U')
206 return &eu_regdom;
207 /* Default, as per the old rules */
208 return &us_regdom;
209}
210
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200211static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200212{
213 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
214 return true;
215 return false;
216}
Johannes Berg734366d2008-09-15 10:56:48 +0200217#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200218static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
219{
220 return false;
221}
222#endif
223
Johannes Berg734366d2008-09-15 10:56:48 +0200224static void reset_regdomains(void)
225{
Johannes Berg942b25c2008-09-15 11:26:47 +0200226 /* avoid freeing static information or freeing something twice */
227 if (cfg80211_regdomain == cfg80211_world_regdom)
228 cfg80211_regdomain = NULL;
229 if (cfg80211_world_regdom == &world_regdom)
230 cfg80211_world_regdom = NULL;
231 if (cfg80211_regdomain == &world_regdom)
232 cfg80211_regdomain = NULL;
233 if (is_old_static_regdom(cfg80211_regdomain))
234 cfg80211_regdomain = NULL;
235
236 kfree(cfg80211_regdomain);
237 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200238
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200239 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200240 cfg80211_regdomain = NULL;
241}
242
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500243/*
244 * Dynamic world regulatory domain requested by the wireless
245 * core upon initialization
246 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200247static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200248{
Johannes Bergf6037d02008-10-21 11:01:33 +0200249 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200250
251 reset_regdomains();
252
253 cfg80211_world_regdom = rd;
254 cfg80211_regdomain = rd;
255}
Johannes Berg734366d2008-09-15 10:56:48 +0200256
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200257bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100258{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700259 if (!alpha2)
260 return false;
261 if (alpha2[0] == '0' && alpha2[1] == '0')
262 return true;
263 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100264}
265
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200266static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700267{
268 if (!alpha2)
269 return false;
270 if (alpha2[0] != 0 && alpha2[1] != 0)
271 return true;
272 return false;
273}
Johannes Berg8318d782008-01-24 19:38:38 +0100274
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700275static bool is_alpha_upper(char letter)
276{
277 /* ASCII A - Z */
278 if (letter >= 65 && letter <= 90)
279 return true;
280 return false;
281}
282
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200283static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700284{
285 if (!alpha2)
286 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500287 /*
288 * Special case where regulatory domain was built by driver
289 * but a specific alpha2 cannot be determined
290 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700291 if (alpha2[0] == '9' && alpha2[1] == '9')
292 return true;
293 return false;
294}
295
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800296static bool is_intersected_alpha2(const char *alpha2)
297{
298 if (!alpha2)
299 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500300 /*
301 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800302 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500303 * structures
304 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800305 if (alpha2[0] == '9' && alpha2[1] == '8')
306 return true;
307 return false;
308}
309
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200310static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700311{
312 if (!alpha2)
313 return false;
314 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
315 return true;
316 return false;
317}
318
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200319static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700320{
321 if (!alpha2_x || !alpha2_y)
322 return false;
323 if (alpha2_x[0] == alpha2_y[0] &&
324 alpha2_x[1] == alpha2_y[1])
325 return true;
326 return false;
327}
328
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500329static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700330{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500331 assert_cfg80211_lock();
332
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700333 if (!cfg80211_regdomain)
334 return true;
335 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
336 return false;
337 return true;
338}
339
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800340/**
341 * country_ie_integrity_changes - tells us if the country IE has changed
342 * @checksum: checksum of country IE of fields we are interested in
343 *
344 * If the country IE has not changed you can ignore it safely. This is
345 * useful to determine if two devices are seeing two different country IEs
346 * even on the same alpha2. Note that this will return false if no IE has
347 * been set on the wireless core yet.
348 */
349static bool country_ie_integrity_changes(u32 checksum)
350{
351 /* If no IE has been set then the checksum doesn't change */
352 if (unlikely(!last_request->country_ie_checksum))
353 return false;
354 if (unlikely(last_request->country_ie_checksum != checksum))
355 return true;
356 return false;
357}
358
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500359/*
360 * This lets us keep regulatory code which is updated on a regulatory
361 * basis in userspace.
362 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700363static int call_crda(const char *alpha2)
364{
365 char country_env[9 + 2] = "COUNTRY=";
366 char *envp[] = {
367 country_env,
368 NULL
369 };
370
371 if (!is_world_regdom((char *) alpha2))
372 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
373 alpha2[0], alpha2[1]);
374 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700375 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
376 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700377
378 country_env[8] = alpha2[0];
379 country_env[9] = alpha2[1];
380
381 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
382}
383
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700384/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200385bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700386{
Johannes Bergf6037d02008-10-21 11:01:33 +0200387 if (!last_request)
388 return false;
389
390 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700391}
392
393/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200394static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700395{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200396 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700397 u32 freq_diff;
398
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800399 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700400 return false;
401
402 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
403 return false;
404
405 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
406
Roel Kluinbd05f282009-03-03 22:55:21 +0100407 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
408 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700409 return false;
410
411 return true;
412}
413
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200414static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700415{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200416 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700417 unsigned int i;
418
419 if (!rd->n_reg_rules)
420 return false;
421
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800422 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
423 return false;
424
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700425 for (i = 0; i < rd->n_reg_rules; i++) {
426 reg_rule = &rd->reg_rules[i];
427 if (!is_valid_reg_rule(reg_rule))
428 return false;
429 }
430
431 return true;
432}
433
434/* Returns value in KHz */
435static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
436 u32 freq)
437{
438 unsigned int i;
439 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
440 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
441 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
442 if (start_freq_khz >= freq_range->start_freq_khz &&
443 end_freq_khz <= freq_range->end_freq_khz)
444 return supported_bandwidths[i];
445 }
446 return 0;
447}
448
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800449/**
450 * freq_in_rule_band - tells us if a frequency is in a frequency band
451 * @freq_range: frequency rule we want to query
452 * @freq_khz: frequency we are inquiring about
453 *
454 * This lets us know if a specific frequency rule is or is not relevant to
455 * a specific frequency's band. Bands are device specific and artificial
456 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
457 * safe for now to assume that a frequency rule should not be part of a
458 * frequency's band if the start freq or end freq are off by more than 2 GHz.
459 * This resolution can be lowered and should be considered as we add
460 * regulatory rule support for other "bands".
461 **/
462static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
463 u32 freq_khz)
464{
465#define ONE_GHZ_IN_KHZ 1000000
466 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
467 return true;
468 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
469 return true;
470 return false;
471#undef ONE_GHZ_IN_KHZ
472}
473
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500474/*
475 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800476 * structure has a lot of information which the IE doesn't yet have,
477 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500478 * with our userspace regulatory agent to get lower bounds.
479 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800480static struct ieee80211_regdomain *country_ie_2_rd(
481 u8 *country_ie,
482 u8 country_ie_len,
483 u32 *checksum)
484{
485 struct ieee80211_regdomain *rd = NULL;
486 unsigned int i = 0;
487 char alpha2[2];
488 u32 flags = 0;
489 u32 num_rules = 0, size_of_regd = 0;
490 u8 *triplets_start = NULL;
491 u8 len_at_triplet = 0;
492 /* the last channel we have registered in a subband (triplet) */
493 int last_sub_max_channel = 0;
494
495 *checksum = 0xDEADBEEF;
496
497 /* Country IE requirements */
498 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
499 country_ie_len & 0x01);
500
501 alpha2[0] = country_ie[0];
502 alpha2[1] = country_ie[1];
503
504 /*
505 * Third octet can be:
506 * 'I' - Indoor
507 * 'O' - Outdoor
508 *
509 * anything else we assume is no restrictions
510 */
511 if (country_ie[2] == 'I')
512 flags = NL80211_RRF_NO_OUTDOOR;
513 else if (country_ie[2] == 'O')
514 flags = NL80211_RRF_NO_INDOOR;
515
516 country_ie += 3;
517 country_ie_len -= 3;
518
519 triplets_start = country_ie;
520 len_at_triplet = country_ie_len;
521
522 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
523
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500524 /*
525 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800526 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500527 * for each channel subband
528 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800529 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800530 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800531 struct ieee80211_country_ie_triplet *triplet =
532 (struct ieee80211_country_ie_triplet *) country_ie;
533 int cur_sub_max_channel = 0, cur_channel = 0;
534
535 if (triplet->ext.reg_extension_id >=
536 IEEE80211_COUNTRY_EXTENSION_ID) {
537 country_ie += 3;
538 country_ie_len -= 3;
539 continue;
540 }
541
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800542 /* 2 GHz */
543 if (triplet->chans.first_channel <= 14)
544 end_channel = triplet->chans.first_channel +
545 triplet->chans.num_channels;
546 else
547 /*
548 * 5 GHz -- For example in country IEs if the first
549 * channel given is 36 and the number of channels is 4
550 * then the individual channel numbers defined for the
551 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
552 * and not 36, 37, 38, 39.
553 *
554 * See: http://tinyurl.com/11d-clarification
555 */
556 end_channel = triplet->chans.first_channel +
557 (4 * (triplet->chans.num_channels - 1));
558
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800559 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800560 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800561
562 /* Basic sanity check */
563 if (cur_sub_max_channel < cur_channel)
564 return NULL;
565
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500566 /*
567 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800568 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500569 * increasing
570 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800571 if (last_sub_max_channel) {
572 if (cur_channel <= last_sub_max_channel)
573 return NULL;
574 if (cur_sub_max_channel <= last_sub_max_channel)
575 return NULL;
576 }
577
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500578 /*
579 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800580 * we can throw ext triplets as part of this soup,
581 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500582 * don't support them
583 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800584 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
585 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
586 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
587
588 last_sub_max_channel = cur_sub_max_channel;
589
590 country_ie += 3;
591 country_ie_len -= 3;
592 num_rules++;
593
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500594 /*
595 * Note: this is not a IEEE requirement but
596 * simply a memory requirement
597 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800598 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
599 return NULL;
600 }
601
602 country_ie = triplets_start;
603 country_ie_len = len_at_triplet;
604
605 size_of_regd = sizeof(struct ieee80211_regdomain) +
606 (num_rules * sizeof(struct ieee80211_reg_rule));
607
608 rd = kzalloc(size_of_regd, GFP_KERNEL);
609 if (!rd)
610 return NULL;
611
612 rd->n_reg_rules = num_rules;
613 rd->alpha2[0] = alpha2[0];
614 rd->alpha2[1] = alpha2[1];
615
616 /* This time around we fill in the rd */
617 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800618 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800619 struct ieee80211_country_ie_triplet *triplet =
620 (struct ieee80211_country_ie_triplet *) country_ie;
621 struct ieee80211_reg_rule *reg_rule = NULL;
622 struct ieee80211_freq_range *freq_range = NULL;
623 struct ieee80211_power_rule *power_rule = NULL;
624
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500625 /*
626 * Must parse if dot11RegulatoryClassesRequired is true,
627 * we don't support this yet
628 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800629 if (triplet->ext.reg_extension_id >=
630 IEEE80211_COUNTRY_EXTENSION_ID) {
631 country_ie += 3;
632 country_ie_len -= 3;
633 continue;
634 }
635
636 reg_rule = &rd->reg_rules[i];
637 freq_range = &reg_rule->freq_range;
638 power_rule = &reg_rule->power_rule;
639
640 reg_rule->flags = flags;
641
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800642 /* 2 GHz */
643 if (triplet->chans.first_channel <= 14)
644 end_channel = triplet->chans.first_channel +
645 triplet->chans.num_channels;
646 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800647 end_channel = triplet->chans.first_channel +
648 (4 * (triplet->chans.num_channels - 1));
649
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500650 /*
651 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800652 * the actual band edge, not the center of freq for
653 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500654 * the channels passed
655 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800656 freq_range->start_freq_khz =
657 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
658 triplet->chans.first_channel) - 10);
659 freq_range->end_freq_khz =
660 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800661 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800662
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500663 /*
664 * These are large arbitrary values we use to intersect later.
665 * Increment this if we ever support >= 40 MHz channels
666 * in IEEE 802.11
667 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800668 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
669 power_rule->max_antenna_gain = DBI_TO_MBI(100);
670 power_rule->max_eirp = DBM_TO_MBM(100);
671
672 country_ie += 3;
673 country_ie_len -= 3;
674 i++;
675
676 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
677 }
678
679 return rd;
680}
681
682
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500683/*
684 * Helper for regdom_intersect(), this does the real
685 * mathematical intersection fun
686 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700687static int reg_rules_intersect(
688 const struct ieee80211_reg_rule *rule1,
689 const struct ieee80211_reg_rule *rule2,
690 struct ieee80211_reg_rule *intersected_rule)
691{
692 const struct ieee80211_freq_range *freq_range1, *freq_range2;
693 struct ieee80211_freq_range *freq_range;
694 const struct ieee80211_power_rule *power_rule1, *power_rule2;
695 struct ieee80211_power_rule *power_rule;
696 u32 freq_diff;
697
698 freq_range1 = &rule1->freq_range;
699 freq_range2 = &rule2->freq_range;
700 freq_range = &intersected_rule->freq_range;
701
702 power_rule1 = &rule1->power_rule;
703 power_rule2 = &rule2->power_rule;
704 power_rule = &intersected_rule->power_rule;
705
706 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
707 freq_range2->start_freq_khz);
708 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
709 freq_range2->end_freq_khz);
710 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
711 freq_range2->max_bandwidth_khz);
712
713 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
714 if (freq_range->max_bandwidth_khz > freq_diff)
715 freq_range->max_bandwidth_khz = freq_diff;
716
717 power_rule->max_eirp = min(power_rule1->max_eirp,
718 power_rule2->max_eirp);
719 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
720 power_rule2->max_antenna_gain);
721
722 intersected_rule->flags = (rule1->flags | rule2->flags);
723
724 if (!is_valid_reg_rule(intersected_rule))
725 return -EINVAL;
726
727 return 0;
728}
729
730/**
731 * regdom_intersect - do the intersection between two regulatory domains
732 * @rd1: first regulatory domain
733 * @rd2: second regulatory domain
734 *
735 * Use this function to get the intersection between two regulatory domains.
736 * Once completed we will mark the alpha2 for the rd as intersected, "98",
737 * as no one single alpha2 can represent this regulatory domain.
738 *
739 * Returns a pointer to the regulatory domain structure which will hold the
740 * resulting intersection of rules between rd1 and rd2. We will
741 * kzalloc() this structure for you.
742 */
743static struct ieee80211_regdomain *regdom_intersect(
744 const struct ieee80211_regdomain *rd1,
745 const struct ieee80211_regdomain *rd2)
746{
747 int r, size_of_regd;
748 unsigned int x, y;
749 unsigned int num_rules = 0, rule_idx = 0;
750 const struct ieee80211_reg_rule *rule1, *rule2;
751 struct ieee80211_reg_rule *intersected_rule;
752 struct ieee80211_regdomain *rd;
753 /* This is just a dummy holder to help us count */
754 struct ieee80211_reg_rule irule;
755
756 /* Uses the stack temporarily for counter arithmetic */
757 intersected_rule = &irule;
758
759 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
760
761 if (!rd1 || !rd2)
762 return NULL;
763
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500764 /*
765 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700766 * build them. This is to so we can malloc() and free() a
767 * regdomain once. The reason we use reg_rules_intersect() here
768 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500769 * All rules that do check out OK are valid.
770 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700771
772 for (x = 0; x < rd1->n_reg_rules; x++) {
773 rule1 = &rd1->reg_rules[x];
774 for (y = 0; y < rd2->n_reg_rules; y++) {
775 rule2 = &rd2->reg_rules[y];
776 if (!reg_rules_intersect(rule1, rule2,
777 intersected_rule))
778 num_rules++;
779 memset(intersected_rule, 0,
780 sizeof(struct ieee80211_reg_rule));
781 }
782 }
783
784 if (!num_rules)
785 return NULL;
786
787 size_of_regd = sizeof(struct ieee80211_regdomain) +
788 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
789
790 rd = kzalloc(size_of_regd, GFP_KERNEL);
791 if (!rd)
792 return NULL;
793
794 for (x = 0; x < rd1->n_reg_rules; x++) {
795 rule1 = &rd1->reg_rules[x];
796 for (y = 0; y < rd2->n_reg_rules; y++) {
797 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500798 /*
799 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700800 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500801 * a memcpy()
802 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700803 intersected_rule = &rd->reg_rules[rule_idx];
804 r = reg_rules_intersect(rule1, rule2,
805 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500806 /*
807 * No need to memset here the intersected rule here as
808 * we're not using the stack anymore
809 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700810 if (r)
811 continue;
812 rule_idx++;
813 }
814 }
815
816 if (rule_idx != num_rules) {
817 kfree(rd);
818 return NULL;
819 }
820
821 rd->n_reg_rules = num_rules;
822 rd->alpha2[0] = '9';
823 rd->alpha2[1] = '8';
824
825 return rd;
826}
827
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500828/*
829 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
830 * want to just have the channel structure use these
831 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700832static u32 map_regdom_flags(u32 rd_flags)
833{
834 u32 channel_flags = 0;
835 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
836 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
837 if (rd_flags & NL80211_RRF_NO_IBSS)
838 channel_flags |= IEEE80211_CHAN_NO_IBSS;
839 if (rd_flags & NL80211_RRF_DFS)
840 channel_flags |= IEEE80211_CHAN_RADAR;
841 return channel_flags;
842}
843
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800844static int freq_reg_info_regd(struct wiphy *wiphy,
845 u32 center_freq,
846 u32 *bandwidth,
847 const struct ieee80211_reg_rule **reg_rule,
848 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100849{
850 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800851 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800852 const struct ieee80211_regdomain *regd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700853 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100854
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800855 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800856
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500857 /*
858 * Follow the driver's regulatory domain, if present, unless a country
859 * IE has been processed or a user wants to help complaince further
860 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400861 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
862 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800863 wiphy->regd)
864 regd = wiphy->regd;
865
866 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700867 return -EINVAL;
868
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800869 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700870 const struct ieee80211_reg_rule *rr;
871 const struct ieee80211_freq_range *fr = NULL;
872 const struct ieee80211_power_rule *pr = NULL;
873
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800874 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700875 fr = &rr->freq_range;
876 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800877
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500878 /*
879 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800880 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500881 * not overwrite it once found
882 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800883 if (!band_rule_found)
884 band_rule_found = freq_in_rule_band(fr, center_freq);
885
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700886 max_bandwidth = freq_max_bandwidth(fr, center_freq);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800887
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700888 if (max_bandwidth && *bandwidth <= max_bandwidth) {
889 *reg_rule = rr;
890 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100891 break;
892 }
893 }
894
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800895 if (!band_rule_found)
896 return -ERANGE;
897
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700898 return !max_bandwidth;
899}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800900EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700901
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800902int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800903 const struct ieee80211_reg_rule **reg_rule)
904{
905 return freq_reg_info_regd(wiphy, center_freq,
906 bandwidth, reg_rule, NULL);
907}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700908
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800909static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
910 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700911{
912 int r;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800913 u32 flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700914 u32 max_bandwidth = 0;
915 const struct ieee80211_reg_rule *reg_rule = NULL;
916 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800917 struct ieee80211_supported_band *sband;
918 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500919 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800920
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500921 assert_cfg80211_lock();
922
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500923 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
924
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800925 sband = wiphy->bands[band];
926 BUG_ON(chan_idx >= sband->n_channels);
927 chan = &sband->channels[chan_idx];
928
929 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700930
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800931 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700932 &max_bandwidth, &reg_rule);
933
934 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500935 /*
936 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800937 * with a frequency range on the center_freq's band, since
938 * IEEE-802.11 allows for a country IE to have a subset of the
939 * regulatory information provided in a country we ignore
940 * disabling the channel unless at least one reg rule was
941 * found on the center_freq's band. For details see this
942 * clarification:
943 *
944 * http://tinyurl.com/11d-clarification
945 */
946 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400947 last_request->initiator ==
948 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800949#ifdef CONFIG_CFG80211_REG_DEBUG
950 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
951 "intact on %s - no rule found in band on "
952 "Country IE\n",
953 chan->center_freq, wiphy_name(wiphy));
954#endif
955 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500956 /*
957 * In this case we know the country IE has at least one reg rule
958 * for the band so we respect its band definitions
959 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800960#ifdef CONFIG_CFG80211_REG_DEBUG
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400961 if (last_request->initiator ==
962 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800963 printk(KERN_DEBUG "cfg80211: Disabling "
964 "channel %d MHz on %s due to "
965 "Country IE\n",
966 chan->center_freq, wiphy_name(wiphy));
967#endif
968 flags |= IEEE80211_CHAN_DISABLED;
969 chan->flags = flags;
970 }
Johannes Berg8318d782008-01-24 19:38:38 +0100971 return;
972 }
973
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700974 power_rule = &reg_rule->power_rule;
975
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400976 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500977 request_wiphy && request_wiphy == wiphy &&
978 request_wiphy->strict_regulatory) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500979 /*
980 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800981 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500982 * settings
983 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800984 chan->flags = chan->orig_flags =
985 map_regdom_flags(reg_rule->flags);
986 chan->max_antenna_gain = chan->orig_mag =
987 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
988 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
989 chan->max_power = chan->orig_mpwr =
990 (int) MBM_TO_DBM(power_rule->max_eirp);
991 return;
992 }
993
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700994 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100995 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700996 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
997 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400998 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700999 chan->max_power = min(chan->orig_mpwr,
1000 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001001 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001002 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001003}
1004
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001005static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001006{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001007 unsigned int i;
1008 struct ieee80211_supported_band *sband;
1009
1010 BUG_ON(!wiphy->bands[band]);
1011 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001012
1013 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001014 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001015}
1016
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001017static bool ignore_reg_update(struct wiphy *wiphy,
1018 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001019{
1020 if (!last_request)
1021 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001022 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -08001023 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001024 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001025 /*
1026 * wiphy->regd will be set once the device has its own
1027 * desired regulatory domain set
1028 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001029 if (wiphy->strict_regulatory && !wiphy->regd &&
1030 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001031 return true;
1032 return false;
1033}
1034
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001035static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001036{
1037 struct cfg80211_registered_device *drv;
1038
1039 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001040 wiphy_update_regulatory(&drv->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001041}
1042
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001043static void handle_reg_beacon(struct wiphy *wiphy,
1044 unsigned int chan_idx,
1045 struct reg_beacon *reg_beacon)
1046{
1047#ifdef CONFIG_CFG80211_REG_DEBUG
1048#define REG_DEBUG_BEACON_FLAG(desc) \
1049 printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \
1050 "frequency: %d MHz (Ch %d) on %s\n", \
1051 reg_beacon->chan.center_freq, \
1052 ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \
1053 wiphy_name(wiphy));
1054#else
1055#define REG_DEBUG_BEACON_FLAG(desc) do {} while (0)
1056#endif
1057 struct ieee80211_supported_band *sband;
1058 struct ieee80211_channel *chan;
1059
1060 assert_cfg80211_lock();
1061
1062 sband = wiphy->bands[reg_beacon->chan.band];
1063 chan = &sband->channels[chan_idx];
1064
1065 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1066 return;
1067
1068 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1069 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1070 REG_DEBUG_BEACON_FLAG("active scanning");
1071 }
1072
1073 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1074 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1075 REG_DEBUG_BEACON_FLAG("beaconing");
1076 }
1077
1078 chan->beacon_found = true;
1079#undef REG_DEBUG_BEACON_FLAG
1080}
1081
1082/*
1083 * Called when a scan on a wiphy finds a beacon on
1084 * new channel
1085 */
1086static void wiphy_update_new_beacon(struct wiphy *wiphy,
1087 struct reg_beacon *reg_beacon)
1088{
1089 unsigned int i;
1090 struct ieee80211_supported_band *sband;
1091
1092 assert_cfg80211_lock();
1093
1094 if (!wiphy->bands[reg_beacon->chan.band])
1095 return;
1096
1097 sband = wiphy->bands[reg_beacon->chan.band];
1098
1099 for (i = 0; i < sband->n_channels; i++)
1100 handle_reg_beacon(wiphy, i, reg_beacon);
1101}
1102
1103/*
1104 * Called upon reg changes or a new wiphy is added
1105 */
1106static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1107{
1108 unsigned int i;
1109 struct ieee80211_supported_band *sband;
1110 struct reg_beacon *reg_beacon;
1111
1112 assert_cfg80211_lock();
1113
1114 if (list_empty(&reg_beacon_list))
1115 return;
1116
1117 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1118 if (!wiphy->bands[reg_beacon->chan.band])
1119 continue;
1120 sband = wiphy->bands[reg_beacon->chan.band];
1121 for (i = 0; i < sband->n_channels; i++)
1122 handle_reg_beacon(wiphy, i, reg_beacon);
1123 }
1124}
1125
1126static bool reg_is_world_roaming(struct wiphy *wiphy)
1127{
1128 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1129 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1130 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001131 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001132 wiphy->custom_regulatory)
1133 return true;
1134 return false;
1135}
1136
1137/* Reap the advantages of previously found beacons */
1138static void reg_process_beacons(struct wiphy *wiphy)
1139{
1140 if (!reg_is_world_roaming(wiphy))
1141 return;
1142 wiphy_update_beacon_reg(wiphy);
1143}
1144
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001145void wiphy_update_regulatory(struct wiphy *wiphy,
1146 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001147{
1148 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001149
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001150 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001151 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001152 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001153 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001154 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001155 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001156out:
1157 reg_process_beacons(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001158 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001159 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001160}
1161
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001162static void handle_channel_custom(struct wiphy *wiphy,
1163 enum ieee80211_band band,
1164 unsigned int chan_idx,
1165 const struct ieee80211_regdomain *regd)
1166{
1167 int r;
1168 u32 max_bandwidth = 0;
1169 const struct ieee80211_reg_rule *reg_rule = NULL;
1170 const struct ieee80211_power_rule *power_rule = NULL;
1171 struct ieee80211_supported_band *sband;
1172 struct ieee80211_channel *chan;
1173
1174 sband = wiphy->bands[band];
1175 BUG_ON(chan_idx >= sband->n_channels);
1176 chan = &sband->channels[chan_idx];
1177
1178 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1179 &max_bandwidth, &reg_rule, regd);
1180
1181 if (r) {
1182 chan->flags = IEEE80211_CHAN_DISABLED;
1183 return;
1184 }
1185
1186 power_rule = &reg_rule->power_rule;
1187
1188 chan->flags |= map_regdom_flags(reg_rule->flags);
1189 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1190 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1191 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1192}
1193
1194static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1195 const struct ieee80211_regdomain *regd)
1196{
1197 unsigned int i;
1198 struct ieee80211_supported_band *sband;
1199
1200 BUG_ON(!wiphy->bands[band]);
1201 sband = wiphy->bands[band];
1202
1203 for (i = 0; i < sband->n_channels; i++)
1204 handle_channel_custom(wiphy, band, i, regd);
1205}
1206
1207/* Used by drivers prior to wiphy registration */
1208void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1209 const struct ieee80211_regdomain *regd)
1210{
1211 enum ieee80211_band band;
1212 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1213 if (wiphy->bands[band])
1214 handle_band_custom(wiphy, band, regd);
1215 }
1216}
1217EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1218
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001219static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1220 const struct ieee80211_regdomain *src_regd)
1221{
1222 struct ieee80211_regdomain *regd;
1223 int size_of_regd = 0;
1224 unsigned int i;
1225
1226 size_of_regd = sizeof(struct ieee80211_regdomain) +
1227 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1228
1229 regd = kzalloc(size_of_regd, GFP_KERNEL);
1230 if (!regd)
1231 return -ENOMEM;
1232
1233 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1234
1235 for (i = 0; i < src_regd->n_reg_rules; i++)
1236 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1237 sizeof(struct ieee80211_reg_rule));
1238
1239 *dst_regd = regd;
1240 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001241}
1242
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001243/*
1244 * Return value which can be used by ignore_request() to indicate
1245 * it has been determined we should intersect two regulatory domains
1246 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001247#define REG_INTERSECT 1
1248
Johannes Berg84fa4f42008-10-24 20:32:23 +02001249/* This has the logic which determines when a new request
1250 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001251static int ignore_request(struct wiphy *wiphy,
1252 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001253{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001254 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001255
1256 assert_cfg80211_lock();
1257
Johannes Berg84fa4f42008-10-24 20:32:23 +02001258 /* All initial requests are respected */
1259 if (!last_request)
1260 return 0;
1261
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001262 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001263 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001264 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001265 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001266
1267 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1268
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001269 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001270 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001271 if (last_request->initiator ==
1272 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001273 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001274 /*
1275 * Two cards with two APs claiming different
1276 * different Country IE alpha2s. We could
1277 * intersect them, but that seems unlikely
1278 * to be correct. Reject second one for now.
1279 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001280 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001281 return -EOPNOTSUPP;
1282 return -EALREADY;
1283 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001284 /*
1285 * Two consecutive Country IE hints on the same wiphy.
1286 * This should be picked up early by the driver/stack
1287 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001288 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001289 return 0;
1290 return -EALREADY;
1291 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001292 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001293 case NL80211_REGDOM_SET_BY_DRIVER:
1294 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001295 if (is_old_static_regdom(cfg80211_regdomain))
1296 return 0;
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001297 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001298 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001299 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001300 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001301
1302 /*
1303 * This would happen if you unplug and plug your card
1304 * back in or if you add a new device for which the previously
1305 * loaded card also agrees on the regulatory domain.
1306 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001307 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001308 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001309 return -EALREADY;
1310
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001311 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001312 case NL80211_REGDOM_SET_BY_USER:
1313 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001314 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001315 /*
1316 * If the user knows better the user should set the regdom
1317 * to their country before the IE is picked up
1318 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001319 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001320 last_request->intersect)
1321 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001322 /*
1323 * Process user requests only after previous user/driver/core
1324 * requests have been processed
1325 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001326 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1327 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1328 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001329 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001330 return -EAGAIN;
1331 }
1332
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001333 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001334 !regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001335 return -EALREADY;
1336
Johannes Berg84fa4f42008-10-24 20:32:23 +02001337 return 0;
1338 }
1339
1340 return -EINVAL;
1341}
1342
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001343/**
1344 * __regulatory_hint - hint to the wireless core a regulatory domain
1345 * @wiphy: if the hint comes from country information from an AP, this
1346 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001347 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001348 *
1349 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001350 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001351 *
1352 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1353 * already been set or other standard error codes.
1354 *
1355 * Caller must hold &cfg80211_mutex
1356 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001357static int __regulatory_hint(struct wiphy *wiphy,
1358 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001359{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001360 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001361 int r = 0;
1362
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001363 assert_cfg80211_lock();
1364
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001365 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001366
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001367 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001368 if (pending_request->initiator ==
1369 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001370 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001371 if (r) {
1372 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001373 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001374 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001375 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001376 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001377 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001378 /*
1379 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001380 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001381 * wiphy
1382 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001383 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001384 pending_request->initiator ==
1385 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001386 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001387 if (r) {
1388 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001389 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001390 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001391 r = -EALREADY;
1392 goto new_request;
1393 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001394 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001395 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001396 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001397
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001398new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001399 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001400
1401 last_request = pending_request;
1402 last_request->intersect = intersect;
1403
1404 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001405
1406 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001407 if (r < 0) {
1408 /*
1409 * Since CRDA will not be called in this case as we already
1410 * have applied the requested regulatory domain before we just
1411 * inform userspace we have processed the request
1412 */
1413 if (r == -EALREADY)
1414 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001415 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001416 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001417
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001418 /*
1419 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1420 * AND if CRDA is NOT present nothing will happen, if someone
1421 * wants to bother with 11d with OLD_REG you can add a timer.
1422 * If after x amount of time nothing happens you can call:
1423 *
1424 * return set_regdom(country_ie_regdomain);
1425 *
1426 * to intersect with the static rd
1427 */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001428 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001429}
1430
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001431/* This currently only processes user and driver regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001432static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001433{
1434 int r = 0;
1435 struct wiphy *wiphy = NULL;
1436
1437 BUG_ON(!reg_request->alpha2);
1438
1439 mutex_lock(&cfg80211_mutex);
1440
1441 if (wiphy_idx_valid(reg_request->wiphy_idx))
1442 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1443
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001444 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001445 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001446 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001447 goto out;
1448 }
1449
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001450 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001451 /* This is required so that the orig_* parameters are saved */
1452 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1453 wiphy_update_regulatory(wiphy, reg_request->initiator);
1454out:
1455 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001456}
1457
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001458/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001459static void reg_process_pending_hints(void)
1460 {
1461 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001462
1463 spin_lock(&reg_requests_lock);
1464 while (!list_empty(&reg_requests_list)) {
1465 reg_request = list_first_entry(&reg_requests_list,
1466 struct regulatory_request,
1467 list);
1468 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001469
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001470 spin_unlock(&reg_requests_lock);
1471 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001472 spin_lock(&reg_requests_lock);
1473 }
1474 spin_unlock(&reg_requests_lock);
1475}
1476
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001477/* Processes beacon hints -- this has nothing to do with country IEs */
1478static void reg_process_pending_beacon_hints(void)
1479{
1480 struct cfg80211_registered_device *drv;
1481 struct reg_beacon *pending_beacon, *tmp;
1482
1483 mutex_lock(&cfg80211_mutex);
1484
1485 /* This goes through the _pending_ beacon list */
1486 spin_lock_bh(&reg_pending_beacons_lock);
1487
1488 if (list_empty(&reg_pending_beacons)) {
1489 spin_unlock_bh(&reg_pending_beacons_lock);
1490 goto out;
1491 }
1492
1493 list_for_each_entry_safe(pending_beacon, tmp,
1494 &reg_pending_beacons, list) {
1495
1496 list_del_init(&pending_beacon->list);
1497
1498 /* Applies the beacon hint to current wiphys */
1499 list_for_each_entry(drv, &cfg80211_drv_list, list)
1500 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1501
1502 /* Remembers the beacon hint for new wiphys or reg changes */
1503 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1504 }
1505
1506 spin_unlock_bh(&reg_pending_beacons_lock);
1507out:
1508 mutex_unlock(&cfg80211_mutex);
1509}
1510
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001511static void reg_todo(struct work_struct *work)
1512{
1513 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001514 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001515}
1516
1517static DECLARE_WORK(reg_work, reg_todo);
1518
1519static void queue_regulatory_request(struct regulatory_request *request)
1520{
1521 spin_lock(&reg_requests_lock);
1522 list_add_tail(&request->list, &reg_requests_list);
1523 spin_unlock(&reg_requests_lock);
1524
1525 schedule_work(&reg_work);
1526}
1527
1528/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001529static int regulatory_hint_core(const char *alpha2)
1530{
1531 struct regulatory_request *request;
1532
1533 BUG_ON(last_request);
1534
1535 request = kzalloc(sizeof(struct regulatory_request),
1536 GFP_KERNEL);
1537 if (!request)
1538 return -ENOMEM;
1539
1540 request->alpha2[0] = alpha2[0];
1541 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001542 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001543
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001544 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001545
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001546 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001547}
1548
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001549/* User hints */
1550int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001551{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001552 struct regulatory_request *request;
1553
Johannes Bergbe3d4812008-10-24 20:32:21 +02001554 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001555
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001556 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1557 if (!request)
1558 return -ENOMEM;
1559
1560 request->wiphy_idx = WIPHY_IDX_STALE;
1561 request->alpha2[0] = alpha2[0];
1562 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001563 request->initiator = NL80211_REGDOM_SET_BY_USER,
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001564
1565 queue_regulatory_request(request);
1566
1567 return 0;
1568}
1569
1570/* Driver hints */
1571int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1572{
1573 struct regulatory_request *request;
1574
1575 BUG_ON(!alpha2);
1576 BUG_ON(!wiphy);
1577
1578 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1579 if (!request)
1580 return -ENOMEM;
1581
1582 request->wiphy_idx = get_wiphy_idx(wiphy);
1583
1584 /* Must have registered wiphy first */
1585 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1586
1587 request->alpha2[0] = alpha2[0];
1588 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001589 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001590
1591 queue_regulatory_request(request);
1592
1593 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001594}
1595EXPORT_SYMBOL(regulatory_hint);
1596
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001597static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1598 u32 country_ie_checksum)
1599{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001600 struct wiphy *request_wiphy;
1601
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001602 assert_cfg80211_lock();
1603
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001604 if (unlikely(last_request->initiator !=
1605 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1606 return false;
1607
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001608 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1609
1610 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001611 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001612
1613 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001614 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001615 /*
1616 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001617 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001618 * on the device
1619 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001620 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1621 return true;
1622 return false;
1623}
1624
1625void regulatory_hint_11d(struct wiphy *wiphy,
1626 u8 *country_ie,
1627 u8 country_ie_len)
1628{
1629 struct ieee80211_regdomain *rd = NULL;
1630 char alpha2[2];
1631 u32 checksum = 0;
1632 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001633 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001634
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001635 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001636
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001637 if (unlikely(!last_request)) {
1638 mutex_unlock(&cfg80211_mutex);
1639 return;
1640 }
1641
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001642 /* IE len must be evenly divisible by 2 */
1643 if (country_ie_len & 0x01)
1644 goto out;
1645
1646 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1647 goto out;
1648
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001649 /*
1650 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001651 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001652 * we were able to process the last regulatory_hint_11d() call
1653 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001654 if (country_ie_regdomain)
1655 goto out;
1656
1657 alpha2[0] = country_ie[0];
1658 alpha2[1] = country_ie[1];
1659
1660 if (country_ie[2] == 'I')
1661 env = ENVIRON_INDOOR;
1662 else if (country_ie[2] == 'O')
1663 env = ENVIRON_OUTDOOR;
1664
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001665 /*
1666 * We will run this for *every* beacon processed for the BSSID, so
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001667 * we optimize an early check to exit out early if we don't have to
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001668 * do anything
1669 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001670 if (likely(last_request->initiator ==
1671 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1672 wiphy_idx_valid(last_request->wiphy_idx))) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001673 struct cfg80211_registered_device *drv_last_ie;
1674
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001675 drv_last_ie =
1676 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001677
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001678 /*
1679 * Lets keep this simple -- we trust the first AP
1680 * after we intersect with CRDA
1681 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001682 if (likely(&drv_last_ie->wiphy == wiphy)) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001683 /*
1684 * Ignore IEs coming in on this wiphy with
1685 * the same alpha2 and environment cap
1686 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001687 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1688 alpha2) &&
1689 env == drv_last_ie->env)) {
1690 goto out;
1691 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001692 /*
1693 * the wiphy moved on to another BSSID or the AP
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001694 * was reconfigured. XXX: We need to deal with the
1695 * case where the user suspends and goes to goes
1696 * to another country, and then gets IEs from an
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001697 * AP with different settings
1698 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001699 goto out;
1700 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001701 /*
1702 * Ignore IEs coming in on two separate wiphys with
1703 * the same alpha2 and environment cap
1704 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001705 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1706 alpha2) &&
1707 env == drv_last_ie->env)) {
1708 goto out;
1709 }
1710 /* We could potentially intersect though */
1711 goto out;
1712 }
1713 }
1714
1715 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1716 if (!rd)
1717 goto out;
1718
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001719 /*
1720 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001721 * the future when we want to add suspend/resume support and having
1722 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001723 * move to another AP. Right now we just trust the first AP.
1724 *
1725 * If we hit this before we add this support we want to be informed of
1726 * it as it would indicate a mistake in the current design
1727 */
1728 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001729 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001730
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001731 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1732 if (!request)
1733 goto free_rd_out;
1734
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001735 /*
1736 * We keep this around for when CRDA comes back with a response so
1737 * we can intersect with that
1738 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001739 country_ie_regdomain = rd;
1740
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001741 request->wiphy_idx = get_wiphy_idx(wiphy);
1742 request->alpha2[0] = rd->alpha2[0];
1743 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001744 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001745 request->country_ie_checksum = checksum;
1746 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001747
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001748 mutex_unlock(&cfg80211_mutex);
1749
1750 queue_regulatory_request(request);
1751
1752 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001753
1754free_rd_out:
1755 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001756out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001757 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001758}
1759EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001760
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001761static bool freq_is_chan_12_13_14(u16 freq)
1762{
1763 if (freq == ieee80211_channel_to_frequency(12) ||
1764 freq == ieee80211_channel_to_frequency(13) ||
1765 freq == ieee80211_channel_to_frequency(14))
1766 return true;
1767 return false;
1768}
1769
1770int regulatory_hint_found_beacon(struct wiphy *wiphy,
1771 struct ieee80211_channel *beacon_chan,
1772 gfp_t gfp)
1773{
1774 struct reg_beacon *reg_beacon;
1775
1776 if (likely((beacon_chan->beacon_found ||
1777 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1778 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1779 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1780 return 0;
1781
1782 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1783 if (!reg_beacon)
1784 return -ENOMEM;
1785
1786#ifdef CONFIG_CFG80211_REG_DEBUG
1787 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1788 "frequency: %d MHz (Ch %d) on %s\n",
1789 beacon_chan->center_freq,
1790 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1791 wiphy_name(wiphy));
1792#endif
1793 memcpy(&reg_beacon->chan, beacon_chan,
1794 sizeof(struct ieee80211_channel));
1795
1796
1797 /*
1798 * Since we can be called from BH or and non-BH context
1799 * we must use spin_lock_bh()
1800 */
1801 spin_lock_bh(&reg_pending_beacons_lock);
1802 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1803 spin_unlock_bh(&reg_pending_beacons_lock);
1804
1805 schedule_work(&reg_work);
1806
1807 return 0;
1808}
1809
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001810static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001811{
1812 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001813 const struct ieee80211_reg_rule *reg_rule = NULL;
1814 const struct ieee80211_freq_range *freq_range = NULL;
1815 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001816
1817 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1818 "(max_antenna_gain, max_eirp)\n");
1819
1820 for (i = 0; i < rd->n_reg_rules; i++) {
1821 reg_rule = &rd->reg_rules[i];
1822 freq_range = &reg_rule->freq_range;
1823 power_rule = &reg_rule->power_rule;
1824
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001825 /*
1826 * There may not be documentation for max antenna gain
1827 * in certain regions
1828 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001829 if (power_rule->max_antenna_gain)
1830 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1831 "(%d mBi, %d mBm)\n",
1832 freq_range->start_freq_khz,
1833 freq_range->end_freq_khz,
1834 freq_range->max_bandwidth_khz,
1835 power_rule->max_antenna_gain,
1836 power_rule->max_eirp);
1837 else
1838 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1839 "(N/A, %d mBm)\n",
1840 freq_range->start_freq_khz,
1841 freq_range->end_freq_khz,
1842 freq_range->max_bandwidth_khz,
1843 power_rule->max_eirp);
1844 }
1845}
1846
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001847static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001848{
1849
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001850 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001851
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001852 if (last_request->initiator ==
1853 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001854 struct cfg80211_registered_device *drv;
1855 drv = cfg80211_drv_by_wiphy_idx(
1856 last_request->wiphy_idx);
1857 if (drv) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001858 printk(KERN_INFO "cfg80211: Current regulatory "
1859 "domain updated by AP to: %c%c\n",
1860 drv->country_ie_alpha2[0],
1861 drv->country_ie_alpha2[1]);
1862 } else
1863 printk(KERN_INFO "cfg80211: Current regulatory "
1864 "domain intersected: \n");
1865 } else
1866 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001867 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001868 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001869 printk(KERN_INFO "cfg80211: World regulatory "
1870 "domain updated:\n");
1871 else {
1872 if (is_unknown_alpha2(rd->alpha2))
1873 printk(KERN_INFO "cfg80211: Regulatory domain "
1874 "changed to driver built-in settings "
1875 "(unknown country)\n");
1876 else
1877 printk(KERN_INFO "cfg80211: Regulatory domain "
1878 "changed to country: %c%c\n",
1879 rd->alpha2[0], rd->alpha2[1]);
1880 }
1881 print_rd_rules(rd);
1882}
1883
Johannes Berg2df78162008-10-28 16:49:41 +01001884static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001885{
1886 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1887 rd->alpha2[0], rd->alpha2[1]);
1888 print_rd_rules(rd);
1889}
1890
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001891#ifdef CONFIG_CFG80211_REG_DEBUG
1892static void reg_country_ie_process_debug(
1893 const struct ieee80211_regdomain *rd,
1894 const struct ieee80211_regdomain *country_ie_regdomain,
1895 const struct ieee80211_regdomain *intersected_rd)
1896{
1897 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1898 print_regdomain_info(country_ie_regdomain);
1899 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1900 print_regdomain_info(rd);
1901 if (intersected_rd) {
1902 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1903 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08001904 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001905 return;
1906 }
1907 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1908}
1909#else
1910static inline void reg_country_ie_process_debug(
1911 const struct ieee80211_regdomain *rd,
1912 const struct ieee80211_regdomain *country_ie_regdomain,
1913 const struct ieee80211_regdomain *intersected_rd)
1914{
1915}
1916#endif
1917
Johannes Bergd2372b32008-10-24 20:32:20 +02001918/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001919static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001920{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001921 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001922 struct cfg80211_registered_device *drv = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001923 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001924 /* Some basic sanity checks first */
1925
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001926 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001927 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001928 return -EINVAL;
1929 update_world_regdomain(rd);
1930 return 0;
1931 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001932
1933 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1934 !is_unknown_alpha2(rd->alpha2))
1935 return -EINVAL;
1936
Johannes Bergf6037d02008-10-21 11:01:33 +02001937 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001938 return -EINVAL;
1939
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001940 /*
1941 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001942 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001943 * and the pending request came in from a country IE
1944 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001945 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001946 /*
1947 * If someone else asked us to change the rd lets only bother
1948 * checking if the alpha2 changes if CRDA was already called
1949 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001950 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001951 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001952 return -EINVAL;
1953 }
1954
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001955 /*
1956 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001957 * and finally inform them of what we have done, in case they want
1958 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001959 * internal EEPROM data
1960 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001961
Johannes Bergf6037d02008-10-21 11:01:33 +02001962 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001963 return -EINVAL;
1964
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001965 if (!is_valid_rd(rd)) {
1966 printk(KERN_ERR "cfg80211: Invalid "
1967 "regulatory domain detected:\n");
1968 print_regdomain_info(rd);
1969 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001970 }
1971
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001972 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1973
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001974 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001975 int r;
1976
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001977 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001978 reset_regdomains();
1979 cfg80211_regdomain = rd;
1980 return 0;
1981 }
1982
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001983 /*
1984 * For a driver hint, lets copy the regulatory domain the
1985 * driver wanted to the wiphy to deal with conflicts
1986 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001987
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001988 BUG_ON(request_wiphy->regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001989
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001990 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001991 if (r)
1992 return r;
1993
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001994 reset_regdomains();
1995 cfg80211_regdomain = rd;
1996 return 0;
1997 }
1998
1999 /* Intersection requires a bit more work */
2000
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002001 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002002
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002003 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2004 if (!intersected_rd)
2005 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002006
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002007 /*
2008 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002009 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002010 * domain we keep it for its private use
2011 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002012 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002013 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002014 else
2015 kfree(rd);
2016
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002017 rd = NULL;
2018
2019 reset_regdomains();
2020 cfg80211_regdomain = intersected_rd;
2021
2022 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002023 }
2024
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002025 /*
2026 * Country IE requests are handled a bit differently, we intersect
2027 * the country IE rd with what CRDA believes that country should have
2028 */
2029
2030 BUG_ON(!country_ie_regdomain);
2031
2032 if (rd != country_ie_regdomain) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002033 /*
2034 * Intersect what CRDA returned and our what we
2035 * had built from the Country IE received
2036 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002037
2038 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2039
2040 reg_country_ie_process_debug(rd, country_ie_regdomain,
2041 intersected_rd);
2042
2043 kfree(country_ie_regdomain);
2044 country_ie_regdomain = NULL;
2045 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002046 /*
2047 * This would happen when CRDA was not present and
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002048 * OLD_REGULATORY was enabled. We intersect our Country
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002049 * IE rd and what was set on cfg80211 originally
2050 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002051 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2052 }
2053
2054 if (!intersected_rd)
2055 return -EINVAL;
2056
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002057 drv = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002058
2059 drv->country_ie_alpha2[0] = rd->alpha2[0];
2060 drv->country_ie_alpha2[1] = rd->alpha2[1];
2061 drv->env = last_request->country_ie_env;
2062
2063 BUG_ON(intersected_rd == rd);
2064
2065 kfree(rd);
2066 rd = NULL;
2067
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002068 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002069 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002070
2071 return 0;
2072}
2073
2074
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002075/*
2076 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002077 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002078 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2079 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002080int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002081{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002082 int r;
2083
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002084 assert_cfg80211_lock();
2085
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002086 /* Note that this doesn't update the wiphys, this is done below */
2087 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002088 if (r) {
2089 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002090 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002091 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002092
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002093 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002094 if (!last_request->intersect)
2095 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002096
2097 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002098 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002099
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002100 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002101
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002102 nl80211_send_reg_change_event(last_request);
2103
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002104 return r;
2105}
2106
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002107/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002108void reg_device_remove(struct wiphy *wiphy)
2109{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002110 struct wiphy *request_wiphy;
2111
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002112 assert_cfg80211_lock();
2113
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002114 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2115
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002116 kfree(wiphy->regd);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002117 if (!last_request || !request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002118 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002119 if (request_wiphy != wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002120 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002121 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002122 last_request->country_ie_env = ENVIRON_ANY;
2123}
2124
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002125int regulatory_init(void)
2126{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002127 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002128
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002129 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2130 if (IS_ERR(reg_pdev))
2131 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002132
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002133 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002134 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002135
Johannes Berg734366d2008-09-15 10:56:48 +02002136#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002137 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002138
Johannes Berg942b25c2008-09-15 11:26:47 +02002139 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02002140 print_regdomain_info(cfg80211_regdomain);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002141 /*
2142 * The old code still requests for a new regdomain and if
Johannes Berg734366d2008-09-15 10:56:48 +02002143 * you have CRDA you get it updated, otherwise you get
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002144 * stuck with the static values. Since "EU" is not a valid
2145 * ISO / IEC 3166 alpha2 code we can't expect userpace to
2146 * give us a regulatory domain for it. We need last_request
2147 * iniitalized though so lets just send a request which we
2148 * know will be ignored... this crap will be removed once
2149 * OLD_REG dies.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002150 */
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002151 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002152#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002153 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002154
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002155 err = regulatory_hint_core("00");
Johannes Berg734366d2008-09-15 10:56:48 +02002156#endif
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002157 if (err) {
2158 if (err == -ENOMEM)
2159 return err;
2160 /*
2161 * N.B. kobject_uevent_env() can fail mainly for when we're out
2162 * memory which is handled and propagated appropriately above
2163 * but it can also fail during a netlink_broadcast() or during
2164 * early boot for call_usermodehelper(). For now treat these
2165 * errors as non-fatal.
2166 */
2167 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2168 "to call CRDA during init");
2169#ifdef CONFIG_CFG80211_REG_DEBUG
2170 /* We want to find out exactly why when debugging */
2171 WARN_ON(err);
2172#endif
2173 }
Johannes Berg734366d2008-09-15 10:56:48 +02002174
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002175 return 0;
2176}
2177
2178void regulatory_exit(void)
2179{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002180 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002181 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002182
2183 cancel_work_sync(&reg_work);
2184
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002185 mutex_lock(&cfg80211_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002186
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002187 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002188
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002189 kfree(country_ie_regdomain);
2190 country_ie_regdomain = NULL;
2191
Johannes Bergf6037d02008-10-21 11:01:33 +02002192 kfree(last_request);
2193
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002194 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002195
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002196 spin_lock_bh(&reg_pending_beacons_lock);
2197 if (!list_empty(&reg_pending_beacons)) {
2198 list_for_each_entry_safe(reg_beacon, btmp,
2199 &reg_pending_beacons, list) {
2200 list_del(&reg_beacon->list);
2201 kfree(reg_beacon);
2202 }
2203 }
2204 spin_unlock_bh(&reg_pending_beacons_lock);
2205
2206 if (!list_empty(&reg_beacon_list)) {
2207 list_for_each_entry_safe(reg_beacon, btmp,
2208 &reg_beacon_list, list) {
2209 list_del(&reg_beacon->list);
2210 kfree(reg_beacon);
2211 }
2212 }
2213
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002214 spin_lock(&reg_requests_lock);
2215 if (!list_empty(&reg_requests_list)) {
2216 list_for_each_entry_safe(reg_request, tmp,
2217 &reg_requests_list, list) {
2218 list_del(&reg_request->list);
2219 kfree(reg_request);
2220 }
2221 }
2222 spin_unlock(&reg_requests_lock);
2223
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002224 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002225}