blob: ac048a158d8532a3e5e8be1327a0566b8be29802 [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";
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400125#else
126static char *ieee80211_regdom = "00";
127#endif
128
Johannes Berg734366d2008-09-15 10:56:48 +0200129module_param(ieee80211_regdom, charp, 0444);
130MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
131
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400132#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500133/*
134 * We assume 40 MHz bandwidth for the old regulatory work.
Johannes Berg734366d2008-09-15 10:56:48 +0200135 * We make emphasis we are using the exact same frequencies
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500136 * as before
137 */
Johannes Berg734366d2008-09-15 10:56:48 +0200138
139static const struct ieee80211_regdomain us_regdom = {
140 .n_reg_rules = 6,
141 .alpha2 = "US",
142 .reg_rules = {
143 /* IEEE 802.11b/g, channels 1..11 */
144 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
145 /* IEEE 802.11a, channel 36 */
146 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
147 /* IEEE 802.11a, channel 40 */
148 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
149 /* IEEE 802.11a, channel 44 */
150 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
151 /* IEEE 802.11a, channels 48..64 */
152 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
153 /* IEEE 802.11a, channels 149..165, outdoor */
154 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
155 }
156};
157
158static const struct ieee80211_regdomain jp_regdom = {
159 .n_reg_rules = 3,
160 .alpha2 = "JP",
161 .reg_rules = {
162 /* IEEE 802.11b/g, channels 1..14 */
163 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
164 /* IEEE 802.11a, channels 34..48 */
165 REG_RULE(5170-10, 5240+10, 40, 6, 20,
166 NL80211_RRF_PASSIVE_SCAN),
167 /* IEEE 802.11a, channels 52..64 */
168 REG_RULE(5260-10, 5320+10, 40, 6, 20,
169 NL80211_RRF_NO_IBSS |
170 NL80211_RRF_DFS),
171 }
172};
173
174static const struct ieee80211_regdomain eu_regdom = {
175 .n_reg_rules = 6,
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500176 /*
177 * This alpha2 is bogus, we leave it here just for stupid
178 * backward compatibility
179 */
Johannes Berg734366d2008-09-15 10:56:48 +0200180 .alpha2 = "EU",
181 .reg_rules = {
182 /* IEEE 802.11b/g, channels 1..13 */
183 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
184 /* IEEE 802.11a, channel 36 */
185 REG_RULE(5180-10, 5180+10, 40, 6, 23,
186 NL80211_RRF_PASSIVE_SCAN),
187 /* IEEE 802.11a, channel 40 */
188 REG_RULE(5200-10, 5200+10, 40, 6, 23,
189 NL80211_RRF_PASSIVE_SCAN),
190 /* IEEE 802.11a, channel 44 */
191 REG_RULE(5220-10, 5220+10, 40, 6, 23,
192 NL80211_RRF_PASSIVE_SCAN),
193 /* IEEE 802.11a, channels 48..64 */
194 REG_RULE(5240-10, 5320+10, 40, 6, 20,
195 NL80211_RRF_NO_IBSS |
196 NL80211_RRF_DFS),
197 /* IEEE 802.11a, channels 100..140 */
198 REG_RULE(5500-10, 5700+10, 40, 6, 30,
199 NL80211_RRF_NO_IBSS |
200 NL80211_RRF_DFS),
201 }
202};
203
204static const struct ieee80211_regdomain *static_regdom(char *alpha2)
205{
206 if (alpha2[0] == 'U' && alpha2[1] == 'S')
207 return &us_regdom;
208 if (alpha2[0] == 'J' && alpha2[1] == 'P')
209 return &jp_regdom;
210 if (alpha2[0] == 'E' && alpha2[1] == 'U')
211 return &eu_regdom;
212 /* Default, as per the old rules */
213 return &us_regdom;
214}
215
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200216static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200217{
218 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
219 return true;
220 return false;
221}
Johannes Berg734366d2008-09-15 10:56:48 +0200222#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200223static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
224{
225 return false;
226}
227#endif
228
Johannes Berg734366d2008-09-15 10:56:48 +0200229static void reset_regdomains(void)
230{
Johannes Berg942b25c2008-09-15 11:26:47 +0200231 /* avoid freeing static information or freeing something twice */
232 if (cfg80211_regdomain == cfg80211_world_regdom)
233 cfg80211_regdomain = NULL;
234 if (cfg80211_world_regdom == &world_regdom)
235 cfg80211_world_regdom = NULL;
236 if (cfg80211_regdomain == &world_regdom)
237 cfg80211_regdomain = NULL;
238 if (is_old_static_regdom(cfg80211_regdomain))
239 cfg80211_regdomain = NULL;
240
241 kfree(cfg80211_regdomain);
242 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200243
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200244 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200245 cfg80211_regdomain = NULL;
246}
247
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500248/*
249 * Dynamic world regulatory domain requested by the wireless
250 * core upon initialization
251 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200252static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200253{
Johannes Bergf6037d02008-10-21 11:01:33 +0200254 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200255
256 reset_regdomains();
257
258 cfg80211_world_regdom = rd;
259 cfg80211_regdomain = rd;
260}
Johannes Berg734366d2008-09-15 10:56:48 +0200261
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200262bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100263{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700264 if (!alpha2)
265 return false;
266 if (alpha2[0] == '0' && alpha2[1] == '0')
267 return true;
268 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100269}
270
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200271static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700272{
273 if (!alpha2)
274 return false;
275 if (alpha2[0] != 0 && alpha2[1] != 0)
276 return true;
277 return false;
278}
Johannes Berg8318d782008-01-24 19:38:38 +0100279
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700280static bool is_alpha_upper(char letter)
281{
282 /* ASCII A - Z */
283 if (letter >= 65 && letter <= 90)
284 return true;
285 return false;
286}
287
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200288static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700289{
290 if (!alpha2)
291 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500292 /*
293 * Special case where regulatory domain was built by driver
294 * but a specific alpha2 cannot be determined
295 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700296 if (alpha2[0] == '9' && alpha2[1] == '9')
297 return true;
298 return false;
299}
300
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800301static bool is_intersected_alpha2(const char *alpha2)
302{
303 if (!alpha2)
304 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500305 /*
306 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800307 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500308 * structures
309 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800310 if (alpha2[0] == '9' && alpha2[1] == '8')
311 return true;
312 return false;
313}
314
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200315static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700316{
317 if (!alpha2)
318 return false;
319 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
320 return true;
321 return false;
322}
323
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200324static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700325{
326 if (!alpha2_x || !alpha2_y)
327 return false;
328 if (alpha2_x[0] == alpha2_y[0] &&
329 alpha2_x[1] == alpha2_y[1])
330 return true;
331 return false;
332}
333
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500334static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700335{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500336 assert_cfg80211_lock();
337
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700338 if (!cfg80211_regdomain)
339 return true;
340 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
341 return false;
342 return true;
343}
344
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800345/**
346 * country_ie_integrity_changes - tells us if the country IE has changed
347 * @checksum: checksum of country IE of fields we are interested in
348 *
349 * If the country IE has not changed you can ignore it safely. This is
350 * useful to determine if two devices are seeing two different country IEs
351 * even on the same alpha2. Note that this will return false if no IE has
352 * been set on the wireless core yet.
353 */
354static bool country_ie_integrity_changes(u32 checksum)
355{
356 /* If no IE has been set then the checksum doesn't change */
357 if (unlikely(!last_request->country_ie_checksum))
358 return false;
359 if (unlikely(last_request->country_ie_checksum != checksum))
360 return true;
361 return false;
362}
363
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500364/*
365 * This lets us keep regulatory code which is updated on a regulatory
366 * basis in userspace.
367 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700368static int call_crda(const char *alpha2)
369{
370 char country_env[9 + 2] = "COUNTRY=";
371 char *envp[] = {
372 country_env,
373 NULL
374 };
375
376 if (!is_world_regdom((char *) alpha2))
377 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
378 alpha2[0], alpha2[1]);
379 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700380 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
381 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700382
383 country_env[8] = alpha2[0];
384 country_env[9] = alpha2[1];
385
386 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
387}
388
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700389/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200390bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700391{
Johannes Bergf6037d02008-10-21 11:01:33 +0200392 if (!last_request)
393 return false;
394
395 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700396}
397
398/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200399static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700400{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200401 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700402 u32 freq_diff;
403
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800404 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700405 return false;
406
407 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
408 return false;
409
410 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
411
Roel Kluinbd05f282009-03-03 22:55:21 +0100412 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
413 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700414 return false;
415
416 return true;
417}
418
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200419static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700420{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200421 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700422 unsigned int i;
423
424 if (!rd->n_reg_rules)
425 return false;
426
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800427 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
428 return false;
429
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700430 for (i = 0; i < rd->n_reg_rules; i++) {
431 reg_rule = &rd->reg_rules[i];
432 if (!is_valid_reg_rule(reg_rule))
433 return false;
434 }
435
436 return true;
437}
438
439/* Returns value in KHz */
440static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
441 u32 freq)
442{
443 unsigned int i;
444 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
445 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
446 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
447 if (start_freq_khz >= freq_range->start_freq_khz &&
448 end_freq_khz <= freq_range->end_freq_khz)
449 return supported_bandwidths[i];
450 }
451 return 0;
452}
453
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800454/**
455 * freq_in_rule_band - tells us if a frequency is in a frequency band
456 * @freq_range: frequency rule we want to query
457 * @freq_khz: frequency we are inquiring about
458 *
459 * This lets us know if a specific frequency rule is or is not relevant to
460 * a specific frequency's band. Bands are device specific and artificial
461 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
462 * safe for now to assume that a frequency rule should not be part of a
463 * frequency's band if the start freq or end freq are off by more than 2 GHz.
464 * This resolution can be lowered and should be considered as we add
465 * regulatory rule support for other "bands".
466 **/
467static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
468 u32 freq_khz)
469{
470#define ONE_GHZ_IN_KHZ 1000000
471 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
472 return true;
473 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
474 return true;
475 return false;
476#undef ONE_GHZ_IN_KHZ
477}
478
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500479/*
480 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800481 * structure has a lot of information which the IE doesn't yet have,
482 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500483 * with our userspace regulatory agent to get lower bounds.
484 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800485static struct ieee80211_regdomain *country_ie_2_rd(
486 u8 *country_ie,
487 u8 country_ie_len,
488 u32 *checksum)
489{
490 struct ieee80211_regdomain *rd = NULL;
491 unsigned int i = 0;
492 char alpha2[2];
493 u32 flags = 0;
494 u32 num_rules = 0, size_of_regd = 0;
495 u8 *triplets_start = NULL;
496 u8 len_at_triplet = 0;
497 /* the last channel we have registered in a subband (triplet) */
498 int last_sub_max_channel = 0;
499
500 *checksum = 0xDEADBEEF;
501
502 /* Country IE requirements */
503 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
504 country_ie_len & 0x01);
505
506 alpha2[0] = country_ie[0];
507 alpha2[1] = country_ie[1];
508
509 /*
510 * Third octet can be:
511 * 'I' - Indoor
512 * 'O' - Outdoor
513 *
514 * anything else we assume is no restrictions
515 */
516 if (country_ie[2] == 'I')
517 flags = NL80211_RRF_NO_OUTDOOR;
518 else if (country_ie[2] == 'O')
519 flags = NL80211_RRF_NO_INDOOR;
520
521 country_ie += 3;
522 country_ie_len -= 3;
523
524 triplets_start = country_ie;
525 len_at_triplet = country_ie_len;
526
527 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
528
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500529 /*
530 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800531 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500532 * for each channel subband
533 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800534 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800535 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800536 struct ieee80211_country_ie_triplet *triplet =
537 (struct ieee80211_country_ie_triplet *) country_ie;
538 int cur_sub_max_channel = 0, cur_channel = 0;
539
540 if (triplet->ext.reg_extension_id >=
541 IEEE80211_COUNTRY_EXTENSION_ID) {
542 country_ie += 3;
543 country_ie_len -= 3;
544 continue;
545 }
546
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800547 /* 2 GHz */
548 if (triplet->chans.first_channel <= 14)
549 end_channel = triplet->chans.first_channel +
550 triplet->chans.num_channels;
551 else
552 /*
553 * 5 GHz -- For example in country IEs if the first
554 * channel given is 36 and the number of channels is 4
555 * then the individual channel numbers defined for the
556 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
557 * and not 36, 37, 38, 39.
558 *
559 * See: http://tinyurl.com/11d-clarification
560 */
561 end_channel = triplet->chans.first_channel +
562 (4 * (triplet->chans.num_channels - 1));
563
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800564 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800565 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800566
567 /* Basic sanity check */
568 if (cur_sub_max_channel < cur_channel)
569 return NULL;
570
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500571 /*
572 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800573 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500574 * increasing
575 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800576 if (last_sub_max_channel) {
577 if (cur_channel <= last_sub_max_channel)
578 return NULL;
579 if (cur_sub_max_channel <= last_sub_max_channel)
580 return NULL;
581 }
582
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500583 /*
584 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800585 * we can throw ext triplets as part of this soup,
586 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500587 * don't support them
588 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800589 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
590 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
591 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
592
593 last_sub_max_channel = cur_sub_max_channel;
594
595 country_ie += 3;
596 country_ie_len -= 3;
597 num_rules++;
598
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500599 /*
600 * Note: this is not a IEEE requirement but
601 * simply a memory requirement
602 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800603 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
604 return NULL;
605 }
606
607 country_ie = triplets_start;
608 country_ie_len = len_at_triplet;
609
610 size_of_regd = sizeof(struct ieee80211_regdomain) +
611 (num_rules * sizeof(struct ieee80211_reg_rule));
612
613 rd = kzalloc(size_of_regd, GFP_KERNEL);
614 if (!rd)
615 return NULL;
616
617 rd->n_reg_rules = num_rules;
618 rd->alpha2[0] = alpha2[0];
619 rd->alpha2[1] = alpha2[1];
620
621 /* This time around we fill in the rd */
622 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800623 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800624 struct ieee80211_country_ie_triplet *triplet =
625 (struct ieee80211_country_ie_triplet *) country_ie;
626 struct ieee80211_reg_rule *reg_rule = NULL;
627 struct ieee80211_freq_range *freq_range = NULL;
628 struct ieee80211_power_rule *power_rule = NULL;
629
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500630 /*
631 * Must parse if dot11RegulatoryClassesRequired is true,
632 * we don't support this yet
633 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800634 if (triplet->ext.reg_extension_id >=
635 IEEE80211_COUNTRY_EXTENSION_ID) {
636 country_ie += 3;
637 country_ie_len -= 3;
638 continue;
639 }
640
641 reg_rule = &rd->reg_rules[i];
642 freq_range = &reg_rule->freq_range;
643 power_rule = &reg_rule->power_rule;
644
645 reg_rule->flags = flags;
646
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800647 /* 2 GHz */
648 if (triplet->chans.first_channel <= 14)
649 end_channel = triplet->chans.first_channel +
650 triplet->chans.num_channels;
651 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800652 end_channel = triplet->chans.first_channel +
653 (4 * (triplet->chans.num_channels - 1));
654
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500655 /*
656 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800657 * the actual band edge, not the center of freq for
658 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500659 * the channels passed
660 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800661 freq_range->start_freq_khz =
662 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
663 triplet->chans.first_channel) - 10);
664 freq_range->end_freq_khz =
665 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800666 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800667
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500668 /*
669 * These are large arbitrary values we use to intersect later.
670 * Increment this if we ever support >= 40 MHz channels
671 * in IEEE 802.11
672 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800673 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
674 power_rule->max_antenna_gain = DBI_TO_MBI(100);
675 power_rule->max_eirp = DBM_TO_MBM(100);
676
677 country_ie += 3;
678 country_ie_len -= 3;
679 i++;
680
681 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
682 }
683
684 return rd;
685}
686
687
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500688/*
689 * Helper for regdom_intersect(), this does the real
690 * mathematical intersection fun
691 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700692static int reg_rules_intersect(
693 const struct ieee80211_reg_rule *rule1,
694 const struct ieee80211_reg_rule *rule2,
695 struct ieee80211_reg_rule *intersected_rule)
696{
697 const struct ieee80211_freq_range *freq_range1, *freq_range2;
698 struct ieee80211_freq_range *freq_range;
699 const struct ieee80211_power_rule *power_rule1, *power_rule2;
700 struct ieee80211_power_rule *power_rule;
701 u32 freq_diff;
702
703 freq_range1 = &rule1->freq_range;
704 freq_range2 = &rule2->freq_range;
705 freq_range = &intersected_rule->freq_range;
706
707 power_rule1 = &rule1->power_rule;
708 power_rule2 = &rule2->power_rule;
709 power_rule = &intersected_rule->power_rule;
710
711 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
712 freq_range2->start_freq_khz);
713 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
714 freq_range2->end_freq_khz);
715 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
716 freq_range2->max_bandwidth_khz);
717
718 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
719 if (freq_range->max_bandwidth_khz > freq_diff)
720 freq_range->max_bandwidth_khz = freq_diff;
721
722 power_rule->max_eirp = min(power_rule1->max_eirp,
723 power_rule2->max_eirp);
724 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
725 power_rule2->max_antenna_gain);
726
727 intersected_rule->flags = (rule1->flags | rule2->flags);
728
729 if (!is_valid_reg_rule(intersected_rule))
730 return -EINVAL;
731
732 return 0;
733}
734
735/**
736 * regdom_intersect - do the intersection between two regulatory domains
737 * @rd1: first regulatory domain
738 * @rd2: second regulatory domain
739 *
740 * Use this function to get the intersection between two regulatory domains.
741 * Once completed we will mark the alpha2 for the rd as intersected, "98",
742 * as no one single alpha2 can represent this regulatory domain.
743 *
744 * Returns a pointer to the regulatory domain structure which will hold the
745 * resulting intersection of rules between rd1 and rd2. We will
746 * kzalloc() this structure for you.
747 */
748static struct ieee80211_regdomain *regdom_intersect(
749 const struct ieee80211_regdomain *rd1,
750 const struct ieee80211_regdomain *rd2)
751{
752 int r, size_of_regd;
753 unsigned int x, y;
754 unsigned int num_rules = 0, rule_idx = 0;
755 const struct ieee80211_reg_rule *rule1, *rule2;
756 struct ieee80211_reg_rule *intersected_rule;
757 struct ieee80211_regdomain *rd;
758 /* This is just a dummy holder to help us count */
759 struct ieee80211_reg_rule irule;
760
761 /* Uses the stack temporarily for counter arithmetic */
762 intersected_rule = &irule;
763
764 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
765
766 if (!rd1 || !rd2)
767 return NULL;
768
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500769 /*
770 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700771 * build them. This is to so we can malloc() and free() a
772 * regdomain once. The reason we use reg_rules_intersect() here
773 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500774 * All rules that do check out OK are valid.
775 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700776
777 for (x = 0; x < rd1->n_reg_rules; x++) {
778 rule1 = &rd1->reg_rules[x];
779 for (y = 0; y < rd2->n_reg_rules; y++) {
780 rule2 = &rd2->reg_rules[y];
781 if (!reg_rules_intersect(rule1, rule2,
782 intersected_rule))
783 num_rules++;
784 memset(intersected_rule, 0,
785 sizeof(struct ieee80211_reg_rule));
786 }
787 }
788
789 if (!num_rules)
790 return NULL;
791
792 size_of_regd = sizeof(struct ieee80211_regdomain) +
793 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
794
795 rd = kzalloc(size_of_regd, GFP_KERNEL);
796 if (!rd)
797 return NULL;
798
799 for (x = 0; x < rd1->n_reg_rules; x++) {
800 rule1 = &rd1->reg_rules[x];
801 for (y = 0; y < rd2->n_reg_rules; y++) {
802 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500803 /*
804 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700805 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500806 * a memcpy()
807 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700808 intersected_rule = &rd->reg_rules[rule_idx];
809 r = reg_rules_intersect(rule1, rule2,
810 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500811 /*
812 * No need to memset here the intersected rule here as
813 * we're not using the stack anymore
814 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700815 if (r)
816 continue;
817 rule_idx++;
818 }
819 }
820
821 if (rule_idx != num_rules) {
822 kfree(rd);
823 return NULL;
824 }
825
826 rd->n_reg_rules = num_rules;
827 rd->alpha2[0] = '9';
828 rd->alpha2[1] = '8';
829
830 return rd;
831}
832
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500833/*
834 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
835 * want to just have the channel structure use these
836 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700837static u32 map_regdom_flags(u32 rd_flags)
838{
839 u32 channel_flags = 0;
840 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
841 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
842 if (rd_flags & NL80211_RRF_NO_IBSS)
843 channel_flags |= IEEE80211_CHAN_NO_IBSS;
844 if (rd_flags & NL80211_RRF_DFS)
845 channel_flags |= IEEE80211_CHAN_RADAR;
846 return channel_flags;
847}
848
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800849static int freq_reg_info_regd(struct wiphy *wiphy,
850 u32 center_freq,
851 u32 *bandwidth,
852 const struct ieee80211_reg_rule **reg_rule,
853 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100854{
855 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800856 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800857 const struct ieee80211_regdomain *regd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700858 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100859
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800860 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800861
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500862 /*
863 * Follow the driver's regulatory domain, if present, unless a country
864 * IE has been processed or a user wants to help complaince further
865 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400866 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
867 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800868 wiphy->regd)
869 regd = wiphy->regd;
870
871 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700872 return -EINVAL;
873
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800874 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700875 const struct ieee80211_reg_rule *rr;
876 const struct ieee80211_freq_range *fr = NULL;
877 const struct ieee80211_power_rule *pr = NULL;
878
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800879 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700880 fr = &rr->freq_range;
881 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800882
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500883 /*
884 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800885 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500886 * not overwrite it once found
887 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800888 if (!band_rule_found)
889 band_rule_found = freq_in_rule_band(fr, center_freq);
890
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700891 max_bandwidth = freq_max_bandwidth(fr, center_freq);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800892
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700893 if (max_bandwidth && *bandwidth <= max_bandwidth) {
894 *reg_rule = rr;
895 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100896 break;
897 }
898 }
899
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800900 if (!band_rule_found)
901 return -ERANGE;
902
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700903 return !max_bandwidth;
904}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800905EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700906
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800907int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800908 const struct ieee80211_reg_rule **reg_rule)
909{
910 return freq_reg_info_regd(wiphy, center_freq,
911 bandwidth, reg_rule, NULL);
912}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700913
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800914static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
915 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700916{
917 int r;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800918 u32 flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700919 u32 max_bandwidth = 0;
920 const struct ieee80211_reg_rule *reg_rule = NULL;
921 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800922 struct ieee80211_supported_band *sband;
923 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500924 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800925
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500926 assert_cfg80211_lock();
927
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500928 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
929
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800930 sband = wiphy->bands[band];
931 BUG_ON(chan_idx >= sband->n_channels);
932 chan = &sband->channels[chan_idx];
933
934 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700935
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800936 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700937 &max_bandwidth, &reg_rule);
938
939 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500940 /*
941 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800942 * with a frequency range on the center_freq's band, since
943 * IEEE-802.11 allows for a country IE to have a subset of the
944 * regulatory information provided in a country we ignore
945 * disabling the channel unless at least one reg rule was
946 * found on the center_freq's band. For details see this
947 * clarification:
948 *
949 * http://tinyurl.com/11d-clarification
950 */
951 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400952 last_request->initiator ==
953 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800954#ifdef CONFIG_CFG80211_REG_DEBUG
955 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
956 "intact on %s - no rule found in band on "
957 "Country IE\n",
958 chan->center_freq, wiphy_name(wiphy));
959#endif
960 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500961 /*
962 * In this case we know the country IE has at least one reg rule
963 * for the band so we respect its band definitions
964 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800965#ifdef CONFIG_CFG80211_REG_DEBUG
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400966 if (last_request->initiator ==
967 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800968 printk(KERN_DEBUG "cfg80211: Disabling "
969 "channel %d MHz on %s due to "
970 "Country IE\n",
971 chan->center_freq, wiphy_name(wiphy));
972#endif
973 flags |= IEEE80211_CHAN_DISABLED;
974 chan->flags = flags;
975 }
Johannes Berg8318d782008-01-24 19:38:38 +0100976 return;
977 }
978
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700979 power_rule = &reg_rule->power_rule;
980
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400981 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500982 request_wiphy && request_wiphy == wiphy &&
983 request_wiphy->strict_regulatory) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500984 /*
985 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800986 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500987 * settings
988 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800989 chan->flags = chan->orig_flags =
990 map_regdom_flags(reg_rule->flags);
991 chan->max_antenna_gain = chan->orig_mag =
992 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
993 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
994 chan->max_power = chan->orig_mpwr =
995 (int) MBM_TO_DBM(power_rule->max_eirp);
996 return;
997 }
998
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700999 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001000 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001001 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
1002 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -04001003 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001004 chan->max_power = min(chan->orig_mpwr,
1005 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001006 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001007 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001008}
1009
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001010static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001011{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001012 unsigned int i;
1013 struct ieee80211_supported_band *sband;
1014
1015 BUG_ON(!wiphy->bands[band]);
1016 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001017
1018 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001019 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001020}
1021
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001022static bool ignore_reg_update(struct wiphy *wiphy,
1023 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001024{
1025 if (!last_request)
1026 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001027 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -08001028 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001029 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001030 /*
1031 * wiphy->regd will be set once the device has its own
1032 * desired regulatory domain set
1033 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001034 if (wiphy->strict_regulatory && !wiphy->regd &&
1035 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001036 return true;
1037 return false;
1038}
1039
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001040static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001041{
1042 struct cfg80211_registered_device *drv;
1043
1044 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001045 wiphy_update_regulatory(&drv->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001046}
1047
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001048static void handle_reg_beacon(struct wiphy *wiphy,
1049 unsigned int chan_idx,
1050 struct reg_beacon *reg_beacon)
1051{
1052#ifdef CONFIG_CFG80211_REG_DEBUG
1053#define REG_DEBUG_BEACON_FLAG(desc) \
1054 printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \
1055 "frequency: %d MHz (Ch %d) on %s\n", \
1056 reg_beacon->chan.center_freq, \
1057 ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \
1058 wiphy_name(wiphy));
1059#else
1060#define REG_DEBUG_BEACON_FLAG(desc) do {} while (0)
1061#endif
1062 struct ieee80211_supported_band *sband;
1063 struct ieee80211_channel *chan;
1064
1065 assert_cfg80211_lock();
1066
1067 sband = wiphy->bands[reg_beacon->chan.band];
1068 chan = &sband->channels[chan_idx];
1069
1070 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1071 return;
1072
1073 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1074 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1075 REG_DEBUG_BEACON_FLAG("active scanning");
1076 }
1077
1078 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1079 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1080 REG_DEBUG_BEACON_FLAG("beaconing");
1081 }
1082
1083 chan->beacon_found = true;
1084#undef REG_DEBUG_BEACON_FLAG
1085}
1086
1087/*
1088 * Called when a scan on a wiphy finds a beacon on
1089 * new channel
1090 */
1091static void wiphy_update_new_beacon(struct wiphy *wiphy,
1092 struct reg_beacon *reg_beacon)
1093{
1094 unsigned int i;
1095 struct ieee80211_supported_band *sband;
1096
1097 assert_cfg80211_lock();
1098
1099 if (!wiphy->bands[reg_beacon->chan.band])
1100 return;
1101
1102 sband = wiphy->bands[reg_beacon->chan.band];
1103
1104 for (i = 0; i < sband->n_channels; i++)
1105 handle_reg_beacon(wiphy, i, reg_beacon);
1106}
1107
1108/*
1109 * Called upon reg changes or a new wiphy is added
1110 */
1111static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1112{
1113 unsigned int i;
1114 struct ieee80211_supported_band *sband;
1115 struct reg_beacon *reg_beacon;
1116
1117 assert_cfg80211_lock();
1118
1119 if (list_empty(&reg_beacon_list))
1120 return;
1121
1122 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1123 if (!wiphy->bands[reg_beacon->chan.band])
1124 continue;
1125 sband = wiphy->bands[reg_beacon->chan.band];
1126 for (i = 0; i < sband->n_channels; i++)
1127 handle_reg_beacon(wiphy, i, reg_beacon);
1128 }
1129}
1130
1131static bool reg_is_world_roaming(struct wiphy *wiphy)
1132{
1133 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1134 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1135 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001136 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001137 wiphy->custom_regulatory)
1138 return true;
1139 return false;
1140}
1141
1142/* Reap the advantages of previously found beacons */
1143static void reg_process_beacons(struct wiphy *wiphy)
1144{
1145 if (!reg_is_world_roaming(wiphy))
1146 return;
1147 wiphy_update_beacon_reg(wiphy);
1148}
1149
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001150void wiphy_update_regulatory(struct wiphy *wiphy,
1151 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001152{
1153 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001154
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001155 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001156 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001157 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001158 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001159 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001160 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001161out:
1162 reg_process_beacons(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001163 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001164 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001165}
1166
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001167static void handle_channel_custom(struct wiphy *wiphy,
1168 enum ieee80211_band band,
1169 unsigned int chan_idx,
1170 const struct ieee80211_regdomain *regd)
1171{
1172 int r;
1173 u32 max_bandwidth = 0;
1174 const struct ieee80211_reg_rule *reg_rule = NULL;
1175 const struct ieee80211_power_rule *power_rule = NULL;
1176 struct ieee80211_supported_band *sband;
1177 struct ieee80211_channel *chan;
1178
1179 sband = wiphy->bands[band];
1180 BUG_ON(chan_idx >= sband->n_channels);
1181 chan = &sband->channels[chan_idx];
1182
1183 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1184 &max_bandwidth, &reg_rule, regd);
1185
1186 if (r) {
1187 chan->flags = IEEE80211_CHAN_DISABLED;
1188 return;
1189 }
1190
1191 power_rule = &reg_rule->power_rule;
1192
1193 chan->flags |= map_regdom_flags(reg_rule->flags);
1194 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1195 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1196 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1197}
1198
1199static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1200 const struct ieee80211_regdomain *regd)
1201{
1202 unsigned int i;
1203 struct ieee80211_supported_band *sband;
1204
1205 BUG_ON(!wiphy->bands[band]);
1206 sband = wiphy->bands[band];
1207
1208 for (i = 0; i < sband->n_channels; i++)
1209 handle_channel_custom(wiphy, band, i, regd);
1210}
1211
1212/* Used by drivers prior to wiphy registration */
1213void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1214 const struct ieee80211_regdomain *regd)
1215{
1216 enum ieee80211_band band;
1217 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1218 if (wiphy->bands[band])
1219 handle_band_custom(wiphy, band, regd);
1220 }
1221}
1222EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1223
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001224static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1225 const struct ieee80211_regdomain *src_regd)
1226{
1227 struct ieee80211_regdomain *regd;
1228 int size_of_regd = 0;
1229 unsigned int i;
1230
1231 size_of_regd = sizeof(struct ieee80211_regdomain) +
1232 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1233
1234 regd = kzalloc(size_of_regd, GFP_KERNEL);
1235 if (!regd)
1236 return -ENOMEM;
1237
1238 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1239
1240 for (i = 0; i < src_regd->n_reg_rules; i++)
1241 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1242 sizeof(struct ieee80211_reg_rule));
1243
1244 *dst_regd = regd;
1245 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001246}
1247
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001248/*
1249 * Return value which can be used by ignore_request() to indicate
1250 * it has been determined we should intersect two regulatory domains
1251 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001252#define REG_INTERSECT 1
1253
Johannes Berg84fa4f42008-10-24 20:32:23 +02001254/* This has the logic which determines when a new request
1255 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001256static int ignore_request(struct wiphy *wiphy,
1257 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001258{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001259 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001260
1261 assert_cfg80211_lock();
1262
Johannes Berg84fa4f42008-10-24 20:32:23 +02001263 /* All initial requests are respected */
1264 if (!last_request)
1265 return 0;
1266
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001267 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001268 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001269 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001270 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001271
1272 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1273
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001274 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001275 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001276 if (last_request->initiator ==
1277 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001278 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001279 /*
1280 * Two cards with two APs claiming different
1281 * different Country IE alpha2s. We could
1282 * intersect them, but that seems unlikely
1283 * to be correct. Reject second one for now.
1284 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001285 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001286 return -EOPNOTSUPP;
1287 return -EALREADY;
1288 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001289 /*
1290 * Two consecutive Country IE hints on the same wiphy.
1291 * This should be picked up early by the driver/stack
1292 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001293 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001294 return 0;
1295 return -EALREADY;
1296 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001297 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001298 case NL80211_REGDOM_SET_BY_DRIVER:
1299 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001300 if (is_old_static_regdom(cfg80211_regdomain))
1301 return 0;
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001302 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001303 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001304 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001305 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001306
1307 /*
1308 * This would happen if you unplug and plug your card
1309 * back in or if you add a new device for which the previously
1310 * loaded card also agrees on the regulatory domain.
1311 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001312 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001313 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001314 return -EALREADY;
1315
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001316 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001317 case NL80211_REGDOM_SET_BY_USER:
1318 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001319 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001320 /*
1321 * If the user knows better the user should set the regdom
1322 * to their country before the IE is picked up
1323 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001324 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001325 last_request->intersect)
1326 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001327 /*
1328 * Process user requests only after previous user/driver/core
1329 * requests have been processed
1330 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001331 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1332 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1333 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001334 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001335 return -EAGAIN;
1336 }
1337
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001338 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001339 !regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001340 return -EALREADY;
1341
Johannes Berg84fa4f42008-10-24 20:32:23 +02001342 return 0;
1343 }
1344
1345 return -EINVAL;
1346}
1347
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001348/**
1349 * __regulatory_hint - hint to the wireless core a regulatory domain
1350 * @wiphy: if the hint comes from country information from an AP, this
1351 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001352 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001353 *
1354 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001355 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001356 *
1357 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1358 * already been set or other standard error codes.
1359 *
1360 * Caller must hold &cfg80211_mutex
1361 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001362static int __regulatory_hint(struct wiphy *wiphy,
1363 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001364{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001365 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001366 int r = 0;
1367
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001368 assert_cfg80211_lock();
1369
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001370 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001371
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001372 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001373 if (pending_request->initiator ==
1374 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001375 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001376 if (r) {
1377 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001378 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001379 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001380 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001381 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001382 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001383 /*
1384 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001385 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001386 * wiphy
1387 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001388 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001389 pending_request->initiator ==
1390 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001391 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001392 if (r) {
1393 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001394 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001395 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001396 r = -EALREADY;
1397 goto new_request;
1398 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001399 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001400 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001401 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001402
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001403new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001404 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001405
1406 last_request = pending_request;
1407 last_request->intersect = intersect;
1408
1409 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001410
1411 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001412 if (r < 0) {
1413 /*
1414 * Since CRDA will not be called in this case as we already
1415 * have applied the requested regulatory domain before we just
1416 * inform userspace we have processed the request
1417 */
1418 if (r == -EALREADY)
1419 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001420 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001421 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001422
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001423 /*
1424 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1425 * AND if CRDA is NOT present nothing will happen, if someone
1426 * wants to bother with 11d with OLD_REG you can add a timer.
1427 * If after x amount of time nothing happens you can call:
1428 *
1429 * return set_regdom(country_ie_regdomain);
1430 *
1431 * to intersect with the static rd
1432 */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001433 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001434}
1435
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001436/* This currently only processes user and driver regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001437static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001438{
1439 int r = 0;
1440 struct wiphy *wiphy = NULL;
1441
1442 BUG_ON(!reg_request->alpha2);
1443
1444 mutex_lock(&cfg80211_mutex);
1445
1446 if (wiphy_idx_valid(reg_request->wiphy_idx))
1447 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1448
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001449 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001450 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001451 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001452 goto out;
1453 }
1454
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001455 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001456 /* This is required so that the orig_* parameters are saved */
1457 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1458 wiphy_update_regulatory(wiphy, reg_request->initiator);
1459out:
1460 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001461}
1462
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001463/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001464static void reg_process_pending_hints(void)
1465 {
1466 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001467
1468 spin_lock(&reg_requests_lock);
1469 while (!list_empty(&reg_requests_list)) {
1470 reg_request = list_first_entry(&reg_requests_list,
1471 struct regulatory_request,
1472 list);
1473 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001474
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001475 spin_unlock(&reg_requests_lock);
1476 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001477 spin_lock(&reg_requests_lock);
1478 }
1479 spin_unlock(&reg_requests_lock);
1480}
1481
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001482/* Processes beacon hints -- this has nothing to do with country IEs */
1483static void reg_process_pending_beacon_hints(void)
1484{
1485 struct cfg80211_registered_device *drv;
1486 struct reg_beacon *pending_beacon, *tmp;
1487
1488 mutex_lock(&cfg80211_mutex);
1489
1490 /* This goes through the _pending_ beacon list */
1491 spin_lock_bh(&reg_pending_beacons_lock);
1492
1493 if (list_empty(&reg_pending_beacons)) {
1494 spin_unlock_bh(&reg_pending_beacons_lock);
1495 goto out;
1496 }
1497
1498 list_for_each_entry_safe(pending_beacon, tmp,
1499 &reg_pending_beacons, list) {
1500
1501 list_del_init(&pending_beacon->list);
1502
1503 /* Applies the beacon hint to current wiphys */
1504 list_for_each_entry(drv, &cfg80211_drv_list, list)
1505 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1506
1507 /* Remembers the beacon hint for new wiphys or reg changes */
1508 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1509 }
1510
1511 spin_unlock_bh(&reg_pending_beacons_lock);
1512out:
1513 mutex_unlock(&cfg80211_mutex);
1514}
1515
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001516static void reg_todo(struct work_struct *work)
1517{
1518 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001519 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001520}
1521
1522static DECLARE_WORK(reg_work, reg_todo);
1523
1524static void queue_regulatory_request(struct regulatory_request *request)
1525{
1526 spin_lock(&reg_requests_lock);
1527 list_add_tail(&request->list, &reg_requests_list);
1528 spin_unlock(&reg_requests_lock);
1529
1530 schedule_work(&reg_work);
1531}
1532
1533/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001534static int regulatory_hint_core(const char *alpha2)
1535{
1536 struct regulatory_request *request;
1537
1538 BUG_ON(last_request);
1539
1540 request = kzalloc(sizeof(struct regulatory_request),
1541 GFP_KERNEL);
1542 if (!request)
1543 return -ENOMEM;
1544
1545 request->alpha2[0] = alpha2[0];
1546 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001547 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001548
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001549 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001550
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001551 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001552}
1553
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001554/* User hints */
1555int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001556{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001557 struct regulatory_request *request;
1558
Johannes Bergbe3d4812008-10-24 20:32:21 +02001559 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001560
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001561 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1562 if (!request)
1563 return -ENOMEM;
1564
1565 request->wiphy_idx = WIPHY_IDX_STALE;
1566 request->alpha2[0] = alpha2[0];
1567 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001568 request->initiator = NL80211_REGDOM_SET_BY_USER,
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001569
1570 queue_regulatory_request(request);
1571
1572 return 0;
1573}
1574
1575/* Driver hints */
1576int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1577{
1578 struct regulatory_request *request;
1579
1580 BUG_ON(!alpha2);
1581 BUG_ON(!wiphy);
1582
1583 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1584 if (!request)
1585 return -ENOMEM;
1586
1587 request->wiphy_idx = get_wiphy_idx(wiphy);
1588
1589 /* Must have registered wiphy first */
1590 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1591
1592 request->alpha2[0] = alpha2[0];
1593 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001594 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001595
1596 queue_regulatory_request(request);
1597
1598 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001599}
1600EXPORT_SYMBOL(regulatory_hint);
1601
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001602static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1603 u32 country_ie_checksum)
1604{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001605 struct wiphy *request_wiphy;
1606
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001607 assert_cfg80211_lock();
1608
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001609 if (unlikely(last_request->initiator !=
1610 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1611 return false;
1612
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001613 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1614
1615 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001616 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001617
1618 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001619 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001620 /*
1621 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001622 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001623 * on the device
1624 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001625 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1626 return true;
1627 return false;
1628}
1629
1630void regulatory_hint_11d(struct wiphy *wiphy,
1631 u8 *country_ie,
1632 u8 country_ie_len)
1633{
1634 struct ieee80211_regdomain *rd = NULL;
1635 char alpha2[2];
1636 u32 checksum = 0;
1637 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001638 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001639
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001640 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001641
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001642 if (unlikely(!last_request)) {
1643 mutex_unlock(&cfg80211_mutex);
1644 return;
1645 }
1646
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001647 /* IE len must be evenly divisible by 2 */
1648 if (country_ie_len & 0x01)
1649 goto out;
1650
1651 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1652 goto out;
1653
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001654 /*
1655 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001656 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001657 * we were able to process the last regulatory_hint_11d() call
1658 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001659 if (country_ie_regdomain)
1660 goto out;
1661
1662 alpha2[0] = country_ie[0];
1663 alpha2[1] = country_ie[1];
1664
1665 if (country_ie[2] == 'I')
1666 env = ENVIRON_INDOOR;
1667 else if (country_ie[2] == 'O')
1668 env = ENVIRON_OUTDOOR;
1669
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001670 /*
1671 * We will run this for *every* beacon processed for the BSSID, so
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001672 * we optimize an early check to exit out early if we don't have to
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001673 * do anything
1674 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001675 if (likely(last_request->initiator ==
1676 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1677 wiphy_idx_valid(last_request->wiphy_idx))) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001678 struct cfg80211_registered_device *drv_last_ie;
1679
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001680 drv_last_ie =
1681 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001682
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001683 /*
1684 * Lets keep this simple -- we trust the first AP
1685 * after we intersect with CRDA
1686 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001687 if (likely(&drv_last_ie->wiphy == wiphy)) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001688 /*
1689 * Ignore IEs coming in on this wiphy with
1690 * the same alpha2 and environment cap
1691 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001692 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1693 alpha2) &&
1694 env == drv_last_ie->env)) {
1695 goto out;
1696 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001697 /*
1698 * the wiphy moved on to another BSSID or the AP
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001699 * was reconfigured. XXX: We need to deal with the
1700 * case where the user suspends and goes to goes
1701 * to another country, and then gets IEs from an
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001702 * AP with different settings
1703 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001704 goto out;
1705 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001706 /*
1707 * Ignore IEs coming in on two separate wiphys with
1708 * the same alpha2 and environment cap
1709 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001710 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1711 alpha2) &&
1712 env == drv_last_ie->env)) {
1713 goto out;
1714 }
1715 /* We could potentially intersect though */
1716 goto out;
1717 }
1718 }
1719
1720 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1721 if (!rd)
1722 goto out;
1723
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001724 /*
1725 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001726 * the future when we want to add suspend/resume support and having
1727 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001728 * move to another AP. Right now we just trust the first AP.
1729 *
1730 * If we hit this before we add this support we want to be informed of
1731 * it as it would indicate a mistake in the current design
1732 */
1733 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001734 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001735
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001736 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1737 if (!request)
1738 goto free_rd_out;
1739
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001740 /*
1741 * We keep this around for when CRDA comes back with a response so
1742 * we can intersect with that
1743 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001744 country_ie_regdomain = rd;
1745
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001746 request->wiphy_idx = get_wiphy_idx(wiphy);
1747 request->alpha2[0] = rd->alpha2[0];
1748 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001749 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001750 request->country_ie_checksum = checksum;
1751 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001752
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001753 mutex_unlock(&cfg80211_mutex);
1754
1755 queue_regulatory_request(request);
1756
1757 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001758
1759free_rd_out:
1760 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001761out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001762 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001763}
1764EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001765
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001766static bool freq_is_chan_12_13_14(u16 freq)
1767{
1768 if (freq == ieee80211_channel_to_frequency(12) ||
1769 freq == ieee80211_channel_to_frequency(13) ||
1770 freq == ieee80211_channel_to_frequency(14))
1771 return true;
1772 return false;
1773}
1774
1775int regulatory_hint_found_beacon(struct wiphy *wiphy,
1776 struct ieee80211_channel *beacon_chan,
1777 gfp_t gfp)
1778{
1779 struct reg_beacon *reg_beacon;
1780
1781 if (likely((beacon_chan->beacon_found ||
1782 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1783 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1784 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1785 return 0;
1786
1787 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1788 if (!reg_beacon)
1789 return -ENOMEM;
1790
1791#ifdef CONFIG_CFG80211_REG_DEBUG
1792 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1793 "frequency: %d MHz (Ch %d) on %s\n",
1794 beacon_chan->center_freq,
1795 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1796 wiphy_name(wiphy));
1797#endif
1798 memcpy(&reg_beacon->chan, beacon_chan,
1799 sizeof(struct ieee80211_channel));
1800
1801
1802 /*
1803 * Since we can be called from BH or and non-BH context
1804 * we must use spin_lock_bh()
1805 */
1806 spin_lock_bh(&reg_pending_beacons_lock);
1807 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1808 spin_unlock_bh(&reg_pending_beacons_lock);
1809
1810 schedule_work(&reg_work);
1811
1812 return 0;
1813}
1814
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001815static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001816{
1817 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001818 const struct ieee80211_reg_rule *reg_rule = NULL;
1819 const struct ieee80211_freq_range *freq_range = NULL;
1820 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001821
1822 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1823 "(max_antenna_gain, max_eirp)\n");
1824
1825 for (i = 0; i < rd->n_reg_rules; i++) {
1826 reg_rule = &rd->reg_rules[i];
1827 freq_range = &reg_rule->freq_range;
1828 power_rule = &reg_rule->power_rule;
1829
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001830 /*
1831 * There may not be documentation for max antenna gain
1832 * in certain regions
1833 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001834 if (power_rule->max_antenna_gain)
1835 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1836 "(%d mBi, %d mBm)\n",
1837 freq_range->start_freq_khz,
1838 freq_range->end_freq_khz,
1839 freq_range->max_bandwidth_khz,
1840 power_rule->max_antenna_gain,
1841 power_rule->max_eirp);
1842 else
1843 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1844 "(N/A, %d mBm)\n",
1845 freq_range->start_freq_khz,
1846 freq_range->end_freq_khz,
1847 freq_range->max_bandwidth_khz,
1848 power_rule->max_eirp);
1849 }
1850}
1851
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001852static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001853{
1854
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001855 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001856
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001857 if (last_request->initiator ==
1858 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001859 struct cfg80211_registered_device *drv;
1860 drv = cfg80211_drv_by_wiphy_idx(
1861 last_request->wiphy_idx);
1862 if (drv) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001863 printk(KERN_INFO "cfg80211: Current regulatory "
1864 "domain updated by AP to: %c%c\n",
1865 drv->country_ie_alpha2[0],
1866 drv->country_ie_alpha2[1]);
1867 } else
1868 printk(KERN_INFO "cfg80211: Current regulatory "
1869 "domain intersected: \n");
1870 } else
1871 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001872 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001873 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001874 printk(KERN_INFO "cfg80211: World regulatory "
1875 "domain updated:\n");
1876 else {
1877 if (is_unknown_alpha2(rd->alpha2))
1878 printk(KERN_INFO "cfg80211: Regulatory domain "
1879 "changed to driver built-in settings "
1880 "(unknown country)\n");
1881 else
1882 printk(KERN_INFO "cfg80211: Regulatory domain "
1883 "changed to country: %c%c\n",
1884 rd->alpha2[0], rd->alpha2[1]);
1885 }
1886 print_rd_rules(rd);
1887}
1888
Johannes Berg2df78162008-10-28 16:49:41 +01001889static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001890{
1891 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1892 rd->alpha2[0], rd->alpha2[1]);
1893 print_rd_rules(rd);
1894}
1895
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001896#ifdef CONFIG_CFG80211_REG_DEBUG
1897static void reg_country_ie_process_debug(
1898 const struct ieee80211_regdomain *rd,
1899 const struct ieee80211_regdomain *country_ie_regdomain,
1900 const struct ieee80211_regdomain *intersected_rd)
1901{
1902 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1903 print_regdomain_info(country_ie_regdomain);
1904 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1905 print_regdomain_info(rd);
1906 if (intersected_rd) {
1907 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1908 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08001909 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001910 return;
1911 }
1912 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1913}
1914#else
1915static inline void reg_country_ie_process_debug(
1916 const struct ieee80211_regdomain *rd,
1917 const struct ieee80211_regdomain *country_ie_regdomain,
1918 const struct ieee80211_regdomain *intersected_rd)
1919{
1920}
1921#endif
1922
Johannes Bergd2372b32008-10-24 20:32:20 +02001923/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001924static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001925{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001926 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001927 struct cfg80211_registered_device *drv = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001928 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001929 /* Some basic sanity checks first */
1930
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001931 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001932 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001933 return -EINVAL;
1934 update_world_regdomain(rd);
1935 return 0;
1936 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001937
1938 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1939 !is_unknown_alpha2(rd->alpha2))
1940 return -EINVAL;
1941
Johannes Bergf6037d02008-10-21 11:01:33 +02001942 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001943 return -EINVAL;
1944
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001945 /*
1946 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001947 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001948 * and the pending request came in from a country IE
1949 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001950 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001951 /*
1952 * If someone else asked us to change the rd lets only bother
1953 * checking if the alpha2 changes if CRDA was already called
1954 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001955 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001956 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001957 return -EINVAL;
1958 }
1959
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001960 /*
1961 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001962 * and finally inform them of what we have done, in case they want
1963 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001964 * internal EEPROM data
1965 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001966
Johannes Bergf6037d02008-10-21 11:01:33 +02001967 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001968 return -EINVAL;
1969
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001970 if (!is_valid_rd(rd)) {
1971 printk(KERN_ERR "cfg80211: Invalid "
1972 "regulatory domain detected:\n");
1973 print_regdomain_info(rd);
1974 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001975 }
1976
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001977 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1978
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001979 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001980 int r;
1981
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001982 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001983 reset_regdomains();
1984 cfg80211_regdomain = rd;
1985 return 0;
1986 }
1987
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001988 /*
1989 * For a driver hint, lets copy the regulatory domain the
1990 * driver wanted to the wiphy to deal with conflicts
1991 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001992
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001993 BUG_ON(request_wiphy->regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001994
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001995 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001996 if (r)
1997 return r;
1998
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001999 reset_regdomains();
2000 cfg80211_regdomain = rd;
2001 return 0;
2002 }
2003
2004 /* Intersection requires a bit more work */
2005
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002006 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002007
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002008 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2009 if (!intersected_rd)
2010 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002011
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002012 /*
2013 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002014 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002015 * domain we keep it for its private use
2016 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002017 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002018 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002019 else
2020 kfree(rd);
2021
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002022 rd = NULL;
2023
2024 reset_regdomains();
2025 cfg80211_regdomain = intersected_rd;
2026
2027 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002028 }
2029
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002030 /*
2031 * Country IE requests are handled a bit differently, we intersect
2032 * the country IE rd with what CRDA believes that country should have
2033 */
2034
2035 BUG_ON(!country_ie_regdomain);
2036
2037 if (rd != country_ie_regdomain) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002038 /*
2039 * Intersect what CRDA returned and our what we
2040 * had built from the Country IE received
2041 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002042
2043 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2044
2045 reg_country_ie_process_debug(rd, country_ie_regdomain,
2046 intersected_rd);
2047
2048 kfree(country_ie_regdomain);
2049 country_ie_regdomain = NULL;
2050 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002051 /*
2052 * This would happen when CRDA was not present and
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002053 * OLD_REGULATORY was enabled. We intersect our Country
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002054 * IE rd and what was set on cfg80211 originally
2055 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002056 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2057 }
2058
2059 if (!intersected_rd)
2060 return -EINVAL;
2061
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002062 drv = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002063
2064 drv->country_ie_alpha2[0] = rd->alpha2[0];
2065 drv->country_ie_alpha2[1] = rd->alpha2[1];
2066 drv->env = last_request->country_ie_env;
2067
2068 BUG_ON(intersected_rd == rd);
2069
2070 kfree(rd);
2071 rd = NULL;
2072
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002073 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002074 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002075
2076 return 0;
2077}
2078
2079
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002080/*
2081 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002082 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002083 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2084 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002085int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002086{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002087 int r;
2088
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002089 assert_cfg80211_lock();
2090
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002091 /* Note that this doesn't update the wiphys, this is done below */
2092 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002093 if (r) {
2094 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002095 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002096 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002097
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002098 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002099 if (!last_request->intersect)
2100 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002101
2102 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002103 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002104
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002105 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002106
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002107 nl80211_send_reg_change_event(last_request);
2108
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002109 return r;
2110}
2111
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002112/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002113void reg_device_remove(struct wiphy *wiphy)
2114{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002115 struct wiphy *request_wiphy;
2116
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002117 assert_cfg80211_lock();
2118
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002119 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2120
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002121 kfree(wiphy->regd);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002122 if (!last_request || !request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002123 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002124 if (request_wiphy != wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002125 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002126 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002127 last_request->country_ie_env = ENVIRON_ANY;
2128}
2129
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002130int regulatory_init(void)
2131{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002132 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002133
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002134 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2135 if (IS_ERR(reg_pdev))
2136 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002137
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002138 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002139 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002140
Johannes Berg734366d2008-09-15 10:56:48 +02002141#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002142 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002143
Johannes Berg942b25c2008-09-15 11:26:47 +02002144 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02002145 print_regdomain_info(cfg80211_regdomain);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002146 /*
2147 * The old code still requests for a new regdomain and if
Johannes Berg734366d2008-09-15 10:56:48 +02002148 * you have CRDA you get it updated, otherwise you get
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002149 * stuck with the static values. Since "EU" is not a valid
2150 * ISO / IEC 3166 alpha2 code we can't expect userpace to
2151 * give us a regulatory domain for it. We need last_request
2152 * iniitalized though so lets just send a request which we
2153 * know will be ignored... this crap will be removed once
2154 * OLD_REG dies.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002155 */
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002156 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002157#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002158 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002159
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -04002160 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002161#endif
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002162 if (err) {
2163 if (err == -ENOMEM)
2164 return err;
2165 /*
2166 * N.B. kobject_uevent_env() can fail mainly for when we're out
2167 * memory which is handled and propagated appropriately above
2168 * but it can also fail during a netlink_broadcast() or during
2169 * early boot for call_usermodehelper(). For now treat these
2170 * errors as non-fatal.
2171 */
2172 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2173 "to call CRDA during init");
2174#ifdef CONFIG_CFG80211_REG_DEBUG
2175 /* We want to find out exactly why when debugging */
2176 WARN_ON(err);
2177#endif
2178 }
Johannes Berg734366d2008-09-15 10:56:48 +02002179
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002180 return 0;
2181}
2182
2183void regulatory_exit(void)
2184{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002185 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002186 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002187
2188 cancel_work_sync(&reg_work);
2189
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002190 mutex_lock(&cfg80211_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002191
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002192 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002193
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002194 kfree(country_ie_regdomain);
2195 country_ie_regdomain = NULL;
2196
Johannes Bergf6037d02008-10-21 11:01:33 +02002197 kfree(last_request);
2198
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002199 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002200
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002201 spin_lock_bh(&reg_pending_beacons_lock);
2202 if (!list_empty(&reg_pending_beacons)) {
2203 list_for_each_entry_safe(reg_beacon, btmp,
2204 &reg_pending_beacons, list) {
2205 list_del(&reg_beacon->list);
2206 kfree(reg_beacon);
2207 }
2208 }
2209 spin_unlock_bh(&reg_pending_beacons_lock);
2210
2211 if (!list_empty(&reg_beacon_list)) {
2212 list_for_each_entry_safe(reg_beacon, btmp,
2213 &reg_beacon_list, list) {
2214 list_del(&reg_beacon->list);
2215 kfree(reg_beacon);
2216 }
2217 }
2218
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002219 spin_lock(&reg_requests_lock);
2220 if (!list_empty(&reg_requests_list)) {
2221 list_for_each_entry_safe(reg_request, tmp,
2222 &reg_requests_list, list) {
2223 list_del(&reg_request->list);
2224 kfree(reg_request);
2225 }
2226 }
2227 spin_unlock(&reg_requests_lock);
2228
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002229 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002230}