blob: 537af62ec42b27371bb3d6f2edb2df30a5402dc4 [file] [log] [blame]
Johannes Berg8318d782008-01-24 19:38:38 +01001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
Johannes Berg8318d782008-01-24 19:38:38 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012/**
13 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010014 *
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
19 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070020 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
23 *
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
29 *
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
33 *
Johannes Berg8318d782008-01-24 19:38:38 +010034 */
35#include <linux/kernel.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070036#include <linux/list.h>
37#include <linux/random.h>
38#include <linux/nl80211.h>
39#include <linux/platform_device.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070040#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010041#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070042#include "reg.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040043#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010044
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070045/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020046static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020047
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070048/* To trigger userspace events */
49static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010050
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070051/* Keep the ordering from large to small */
52static u32 supported_bandwidths[] = {
53 MHZ_TO_KHZ(40),
54 MHZ_TO_KHZ(20),
Johannes Berg8318d782008-01-24 19:38:38 +010055};
56
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050057/*
58 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020059 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050060 * information to give us an alpha2
61 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080062const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020063
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050064/*
65 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080066 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050067 * what it thinks should apply for the same country
68 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080069static const struct ieee80211_regdomain *country_ie_regdomain;
70
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050071/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050072static LIST_HEAD(reg_requests_list);
73static spinlock_t reg_requests_lock;
74
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050075/* Used to queue up beacon hints for review */
76static LIST_HEAD(reg_pending_beacons);
77static spinlock_t reg_pending_beacons_lock;
78
79/* Used to keep track of processed beacon hints */
80static LIST_HEAD(reg_beacon_list);
81
82struct reg_beacon {
83 struct list_head list;
84 struct ieee80211_channel chan;
85};
86
Johannes Berg734366d2008-09-15 10:56:48 +020087/* We keep a static world regulatory domain in case of the absence of CRDA */
88static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080089 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +020090 .alpha2 = "00",
91 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -050092 /* IEEE 802.11b/g, channels 1..11 */
93 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080094 /* IEEE 802.11b/g, channels 12..13. No HT40
95 * channel fits here. */
96 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -050097 NL80211_RRF_PASSIVE_SCAN |
98 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080099 /* IEEE 802.11 channel 14 - Only JP enables
100 * this and for 802.11b only */
101 REG_RULE(2484-10, 2484+10, 20, 6, 20,
102 NL80211_RRF_PASSIVE_SCAN |
103 NL80211_RRF_NO_IBSS |
104 NL80211_RRF_NO_OFDM),
105 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800106 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800107 NL80211_RRF_PASSIVE_SCAN |
108 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500109
110 /* NB: 5260 MHz - 5700 MHz requies DFS */
111
112 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800113 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500114 NL80211_RRF_PASSIVE_SCAN |
115 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200116 }
117};
118
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200119static const struct ieee80211_regdomain *cfg80211_world_regdom =
120 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200121
122#ifdef CONFIG_WIRELESS_OLD_REGULATORY
123static char *ieee80211_regdom = "US";
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400124#else
125static char *ieee80211_regdom = "00";
126#endif
127
Johannes Berg734366d2008-09-15 10:56:48 +0200128module_param(ieee80211_regdom, charp, 0444);
129MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
130
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400131#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500132/*
133 * We assume 40 MHz bandwidth for the old regulatory work.
Johannes Berg734366d2008-09-15 10:56:48 +0200134 * We make emphasis we are using the exact same frequencies
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500135 * as before
136 */
Johannes Berg734366d2008-09-15 10:56:48 +0200137
138static const struct ieee80211_regdomain us_regdom = {
139 .n_reg_rules = 6,
140 .alpha2 = "US",
141 .reg_rules = {
142 /* IEEE 802.11b/g, channels 1..11 */
143 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
144 /* IEEE 802.11a, channel 36 */
145 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
146 /* IEEE 802.11a, channel 40 */
147 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
148 /* IEEE 802.11a, channel 44 */
149 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
150 /* IEEE 802.11a, channels 48..64 */
151 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
152 /* IEEE 802.11a, channels 149..165, outdoor */
153 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
154 }
155};
156
157static const struct ieee80211_regdomain jp_regdom = {
158 .n_reg_rules = 3,
159 .alpha2 = "JP",
160 .reg_rules = {
161 /* IEEE 802.11b/g, channels 1..14 */
162 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
163 /* IEEE 802.11a, channels 34..48 */
164 REG_RULE(5170-10, 5240+10, 40, 6, 20,
165 NL80211_RRF_PASSIVE_SCAN),
166 /* IEEE 802.11a, channels 52..64 */
167 REG_RULE(5260-10, 5320+10, 40, 6, 20,
168 NL80211_RRF_NO_IBSS |
169 NL80211_RRF_DFS),
170 }
171};
172
173static const struct ieee80211_regdomain eu_regdom = {
174 .n_reg_rules = 6,
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500175 /*
176 * This alpha2 is bogus, we leave it here just for stupid
177 * backward compatibility
178 */
Johannes Berg734366d2008-09-15 10:56:48 +0200179 .alpha2 = "EU",
180 .reg_rules = {
181 /* IEEE 802.11b/g, channels 1..13 */
182 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
183 /* IEEE 802.11a, channel 36 */
184 REG_RULE(5180-10, 5180+10, 40, 6, 23,
185 NL80211_RRF_PASSIVE_SCAN),
186 /* IEEE 802.11a, channel 40 */
187 REG_RULE(5200-10, 5200+10, 40, 6, 23,
188 NL80211_RRF_PASSIVE_SCAN),
189 /* IEEE 802.11a, channel 44 */
190 REG_RULE(5220-10, 5220+10, 40, 6, 23,
191 NL80211_RRF_PASSIVE_SCAN),
192 /* IEEE 802.11a, channels 48..64 */
193 REG_RULE(5240-10, 5320+10, 40, 6, 20,
194 NL80211_RRF_NO_IBSS |
195 NL80211_RRF_DFS),
196 /* IEEE 802.11a, channels 100..140 */
197 REG_RULE(5500-10, 5700+10, 40, 6, 30,
198 NL80211_RRF_NO_IBSS |
199 NL80211_RRF_DFS),
200 }
201};
202
203static const struct ieee80211_regdomain *static_regdom(char *alpha2)
204{
205 if (alpha2[0] == 'U' && alpha2[1] == 'S')
206 return &us_regdom;
207 if (alpha2[0] == 'J' && alpha2[1] == 'P')
208 return &jp_regdom;
209 if (alpha2[0] == 'E' && alpha2[1] == 'U')
210 return &eu_regdom;
211 /* Default, as per the old rules */
212 return &us_regdom;
213}
214
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200215static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200216{
217 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
218 return true;
219 return false;
220}
Johannes Berg734366d2008-09-15 10:56:48 +0200221#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200222static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
223{
224 return false;
225}
226#endif
227
Johannes Berg734366d2008-09-15 10:56:48 +0200228static void reset_regdomains(void)
229{
Johannes Berg942b25c2008-09-15 11:26:47 +0200230 /* avoid freeing static information or freeing something twice */
231 if (cfg80211_regdomain == cfg80211_world_regdom)
232 cfg80211_regdomain = NULL;
233 if (cfg80211_world_regdom == &world_regdom)
234 cfg80211_world_regdom = NULL;
235 if (cfg80211_regdomain == &world_regdom)
236 cfg80211_regdomain = NULL;
237 if (is_old_static_regdom(cfg80211_regdomain))
238 cfg80211_regdomain = NULL;
239
240 kfree(cfg80211_regdomain);
241 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200242
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200243 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200244 cfg80211_regdomain = NULL;
245}
246
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500247/*
248 * Dynamic world regulatory domain requested by the wireless
249 * core upon initialization
250 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200251static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200252{
Johannes Bergf6037d02008-10-21 11:01:33 +0200253 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200254
255 reset_regdomains();
256
257 cfg80211_world_regdom = rd;
258 cfg80211_regdomain = rd;
259}
Johannes Berg734366d2008-09-15 10:56:48 +0200260
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200261bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100262{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263 if (!alpha2)
264 return false;
265 if (alpha2[0] == '0' && alpha2[1] == '0')
266 return true;
267 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100268}
269
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200270static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700271{
272 if (!alpha2)
273 return false;
274 if (alpha2[0] != 0 && alpha2[1] != 0)
275 return true;
276 return false;
277}
Johannes Berg8318d782008-01-24 19:38:38 +0100278
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700279static bool is_alpha_upper(char letter)
280{
281 /* ASCII A - Z */
282 if (letter >= 65 && letter <= 90)
283 return true;
284 return false;
285}
286
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200287static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700288{
289 if (!alpha2)
290 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500291 /*
292 * Special case where regulatory domain was built by driver
293 * but a specific alpha2 cannot be determined
294 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700295 if (alpha2[0] == '9' && alpha2[1] == '9')
296 return true;
297 return false;
298}
299
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800300static bool is_intersected_alpha2(const char *alpha2)
301{
302 if (!alpha2)
303 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500304 /*
305 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800306 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500307 * structures
308 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800309 if (alpha2[0] == '9' && alpha2[1] == '8')
310 return true;
311 return false;
312}
313
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200314static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700315{
316 if (!alpha2)
317 return false;
318 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
319 return true;
320 return false;
321}
322
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200323static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700324{
325 if (!alpha2_x || !alpha2_y)
326 return false;
327 if (alpha2_x[0] == alpha2_y[0] &&
328 alpha2_x[1] == alpha2_y[1])
329 return true;
330 return false;
331}
332
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500333static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700334{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500335 assert_cfg80211_lock();
336
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700337 if (!cfg80211_regdomain)
338 return true;
339 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
340 return false;
341 return true;
342}
343
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800344/**
345 * country_ie_integrity_changes - tells us if the country IE has changed
346 * @checksum: checksum of country IE of fields we are interested in
347 *
348 * If the country IE has not changed you can ignore it safely. This is
349 * useful to determine if two devices are seeing two different country IEs
350 * even on the same alpha2. Note that this will return false if no IE has
351 * been set on the wireless core yet.
352 */
353static bool country_ie_integrity_changes(u32 checksum)
354{
355 /* If no IE has been set then the checksum doesn't change */
356 if (unlikely(!last_request->country_ie_checksum))
357 return false;
358 if (unlikely(last_request->country_ie_checksum != checksum))
359 return true;
360 return false;
361}
362
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500363/*
364 * This lets us keep regulatory code which is updated on a regulatory
365 * basis in userspace.
366 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700367static int call_crda(const char *alpha2)
368{
369 char country_env[9 + 2] = "COUNTRY=";
370 char *envp[] = {
371 country_env,
372 NULL
373 };
374
375 if (!is_world_regdom((char *) alpha2))
376 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
377 alpha2[0], alpha2[1]);
378 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700379 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
380 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700381
382 country_env[8] = alpha2[0];
383 country_env[9] = alpha2[1];
384
385 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
386}
387
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200389bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700390{
Johannes Bergf6037d02008-10-21 11:01:33 +0200391 if (!last_request)
392 return false;
393
394 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700395}
396
397/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200398static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700399{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200400 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700401 u32 freq_diff;
402
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800403 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700404 return false;
405
406 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
407 return false;
408
409 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
410
Roel Kluinbd05f282009-03-03 22:55:21 +0100411 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
412 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700413 return false;
414
415 return true;
416}
417
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200418static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700419{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200420 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700421 unsigned int i;
422
423 if (!rd->n_reg_rules)
424 return false;
425
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800426 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
427 return false;
428
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700429 for (i = 0; i < rd->n_reg_rules; i++) {
430 reg_rule = &rd->reg_rules[i];
431 if (!is_valid_reg_rule(reg_rule))
432 return false;
433 }
434
435 return true;
436}
437
438/* Returns value in KHz */
439static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
440 u32 freq)
441{
442 unsigned int i;
443 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
444 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
445 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
446 if (start_freq_khz >= freq_range->start_freq_khz &&
447 end_freq_khz <= freq_range->end_freq_khz)
448 return supported_bandwidths[i];
449 }
450 return 0;
451}
452
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800453/**
454 * freq_in_rule_band - tells us if a frequency is in a frequency band
455 * @freq_range: frequency rule we want to query
456 * @freq_khz: frequency we are inquiring about
457 *
458 * This lets us know if a specific frequency rule is or is not relevant to
459 * a specific frequency's band. Bands are device specific and artificial
460 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
461 * safe for now to assume that a frequency rule should not be part of a
462 * frequency's band if the start freq or end freq are off by more than 2 GHz.
463 * This resolution can be lowered and should be considered as we add
464 * regulatory rule support for other "bands".
465 **/
466static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
467 u32 freq_khz)
468{
469#define ONE_GHZ_IN_KHZ 1000000
470 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
471 return true;
472 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
473 return true;
474 return false;
475#undef ONE_GHZ_IN_KHZ
476}
477
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500478/*
479 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800480 * structure has a lot of information which the IE doesn't yet have,
481 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500482 * with our userspace regulatory agent to get lower bounds.
483 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800484static struct ieee80211_regdomain *country_ie_2_rd(
485 u8 *country_ie,
486 u8 country_ie_len,
487 u32 *checksum)
488{
489 struct ieee80211_regdomain *rd = NULL;
490 unsigned int i = 0;
491 char alpha2[2];
492 u32 flags = 0;
493 u32 num_rules = 0, size_of_regd = 0;
494 u8 *triplets_start = NULL;
495 u8 len_at_triplet = 0;
496 /* the last channel we have registered in a subband (triplet) */
497 int last_sub_max_channel = 0;
498
499 *checksum = 0xDEADBEEF;
500
501 /* Country IE requirements */
502 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
503 country_ie_len & 0x01);
504
505 alpha2[0] = country_ie[0];
506 alpha2[1] = country_ie[1];
507
508 /*
509 * Third octet can be:
510 * 'I' - Indoor
511 * 'O' - Outdoor
512 *
513 * anything else we assume is no restrictions
514 */
515 if (country_ie[2] == 'I')
516 flags = NL80211_RRF_NO_OUTDOOR;
517 else if (country_ie[2] == 'O')
518 flags = NL80211_RRF_NO_INDOOR;
519
520 country_ie += 3;
521 country_ie_len -= 3;
522
523 triplets_start = country_ie;
524 len_at_triplet = country_ie_len;
525
526 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
527
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500528 /*
529 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800530 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500531 * for each channel subband
532 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800533 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800534 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800535 struct ieee80211_country_ie_triplet *triplet =
536 (struct ieee80211_country_ie_triplet *) country_ie;
537 int cur_sub_max_channel = 0, cur_channel = 0;
538
539 if (triplet->ext.reg_extension_id >=
540 IEEE80211_COUNTRY_EXTENSION_ID) {
541 country_ie += 3;
542 country_ie_len -= 3;
543 continue;
544 }
545
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800546 /* 2 GHz */
547 if (triplet->chans.first_channel <= 14)
548 end_channel = triplet->chans.first_channel +
549 triplet->chans.num_channels;
550 else
551 /*
552 * 5 GHz -- For example in country IEs if the first
553 * channel given is 36 and the number of channels is 4
554 * then the individual channel numbers defined for the
555 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
556 * and not 36, 37, 38, 39.
557 *
558 * See: http://tinyurl.com/11d-clarification
559 */
560 end_channel = triplet->chans.first_channel +
561 (4 * (triplet->chans.num_channels - 1));
562
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800563 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800564 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800565
566 /* Basic sanity check */
567 if (cur_sub_max_channel < cur_channel)
568 return NULL;
569
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500570 /*
571 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800572 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500573 * increasing
574 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800575 if (last_sub_max_channel) {
576 if (cur_channel <= last_sub_max_channel)
577 return NULL;
578 if (cur_sub_max_channel <= last_sub_max_channel)
579 return NULL;
580 }
581
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500582 /*
583 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800584 * we can throw ext triplets as part of this soup,
585 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500586 * don't support them
587 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800588 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
589 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
590 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
591
592 last_sub_max_channel = cur_sub_max_channel;
593
594 country_ie += 3;
595 country_ie_len -= 3;
596 num_rules++;
597
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500598 /*
599 * Note: this is not a IEEE requirement but
600 * simply a memory requirement
601 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800602 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
603 return NULL;
604 }
605
606 country_ie = triplets_start;
607 country_ie_len = len_at_triplet;
608
609 size_of_regd = sizeof(struct ieee80211_regdomain) +
610 (num_rules * sizeof(struct ieee80211_reg_rule));
611
612 rd = kzalloc(size_of_regd, GFP_KERNEL);
613 if (!rd)
614 return NULL;
615
616 rd->n_reg_rules = num_rules;
617 rd->alpha2[0] = alpha2[0];
618 rd->alpha2[1] = alpha2[1];
619
620 /* This time around we fill in the rd */
621 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800622 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800623 struct ieee80211_country_ie_triplet *triplet =
624 (struct ieee80211_country_ie_triplet *) country_ie;
625 struct ieee80211_reg_rule *reg_rule = NULL;
626 struct ieee80211_freq_range *freq_range = NULL;
627 struct ieee80211_power_rule *power_rule = NULL;
628
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500629 /*
630 * Must parse if dot11RegulatoryClassesRequired is true,
631 * we don't support this yet
632 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800633 if (triplet->ext.reg_extension_id >=
634 IEEE80211_COUNTRY_EXTENSION_ID) {
635 country_ie += 3;
636 country_ie_len -= 3;
637 continue;
638 }
639
640 reg_rule = &rd->reg_rules[i];
641 freq_range = &reg_rule->freq_range;
642 power_rule = &reg_rule->power_rule;
643
644 reg_rule->flags = flags;
645
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800646 /* 2 GHz */
647 if (triplet->chans.first_channel <= 14)
648 end_channel = triplet->chans.first_channel +
649 triplet->chans.num_channels;
650 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800651 end_channel = triplet->chans.first_channel +
652 (4 * (triplet->chans.num_channels - 1));
653
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500654 /*
655 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800656 * the actual band edge, not the center of freq for
657 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500658 * the channels passed
659 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800660 freq_range->start_freq_khz =
661 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
662 triplet->chans.first_channel) - 10);
663 freq_range->end_freq_khz =
664 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800665 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800666
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500667 /*
668 * These are large arbitrary values we use to intersect later.
669 * Increment this if we ever support >= 40 MHz channels
670 * in IEEE 802.11
671 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800672 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
673 power_rule->max_antenna_gain = DBI_TO_MBI(100);
674 power_rule->max_eirp = DBM_TO_MBM(100);
675
676 country_ie += 3;
677 country_ie_len -= 3;
678 i++;
679
680 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
681 }
682
683 return rd;
684}
685
686
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500687/*
688 * Helper for regdom_intersect(), this does the real
689 * mathematical intersection fun
690 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700691static int reg_rules_intersect(
692 const struct ieee80211_reg_rule *rule1,
693 const struct ieee80211_reg_rule *rule2,
694 struct ieee80211_reg_rule *intersected_rule)
695{
696 const struct ieee80211_freq_range *freq_range1, *freq_range2;
697 struct ieee80211_freq_range *freq_range;
698 const struct ieee80211_power_rule *power_rule1, *power_rule2;
699 struct ieee80211_power_rule *power_rule;
700 u32 freq_diff;
701
702 freq_range1 = &rule1->freq_range;
703 freq_range2 = &rule2->freq_range;
704 freq_range = &intersected_rule->freq_range;
705
706 power_rule1 = &rule1->power_rule;
707 power_rule2 = &rule2->power_rule;
708 power_rule = &intersected_rule->power_rule;
709
710 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
711 freq_range2->start_freq_khz);
712 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
713 freq_range2->end_freq_khz);
714 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
715 freq_range2->max_bandwidth_khz);
716
717 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
718 if (freq_range->max_bandwidth_khz > freq_diff)
719 freq_range->max_bandwidth_khz = freq_diff;
720
721 power_rule->max_eirp = min(power_rule1->max_eirp,
722 power_rule2->max_eirp);
723 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
724 power_rule2->max_antenna_gain);
725
726 intersected_rule->flags = (rule1->flags | rule2->flags);
727
728 if (!is_valid_reg_rule(intersected_rule))
729 return -EINVAL;
730
731 return 0;
732}
733
734/**
735 * regdom_intersect - do the intersection between two regulatory domains
736 * @rd1: first regulatory domain
737 * @rd2: second regulatory domain
738 *
739 * Use this function to get the intersection between two regulatory domains.
740 * Once completed we will mark the alpha2 for the rd as intersected, "98",
741 * as no one single alpha2 can represent this regulatory domain.
742 *
743 * Returns a pointer to the regulatory domain structure which will hold the
744 * resulting intersection of rules between rd1 and rd2. We will
745 * kzalloc() this structure for you.
746 */
747static struct ieee80211_regdomain *regdom_intersect(
748 const struct ieee80211_regdomain *rd1,
749 const struct ieee80211_regdomain *rd2)
750{
751 int r, size_of_regd;
752 unsigned int x, y;
753 unsigned int num_rules = 0, rule_idx = 0;
754 const struct ieee80211_reg_rule *rule1, *rule2;
755 struct ieee80211_reg_rule *intersected_rule;
756 struct ieee80211_regdomain *rd;
757 /* This is just a dummy holder to help us count */
758 struct ieee80211_reg_rule irule;
759
760 /* Uses the stack temporarily for counter arithmetic */
761 intersected_rule = &irule;
762
763 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
764
765 if (!rd1 || !rd2)
766 return NULL;
767
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500768 /*
769 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700770 * build them. This is to so we can malloc() and free() a
771 * regdomain once. The reason we use reg_rules_intersect() here
772 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500773 * All rules that do check out OK are valid.
774 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700775
776 for (x = 0; x < rd1->n_reg_rules; x++) {
777 rule1 = &rd1->reg_rules[x];
778 for (y = 0; y < rd2->n_reg_rules; y++) {
779 rule2 = &rd2->reg_rules[y];
780 if (!reg_rules_intersect(rule1, rule2,
781 intersected_rule))
782 num_rules++;
783 memset(intersected_rule, 0,
784 sizeof(struct ieee80211_reg_rule));
785 }
786 }
787
788 if (!num_rules)
789 return NULL;
790
791 size_of_regd = sizeof(struct ieee80211_regdomain) +
792 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
793
794 rd = kzalloc(size_of_regd, GFP_KERNEL);
795 if (!rd)
796 return NULL;
797
798 for (x = 0; x < rd1->n_reg_rules; x++) {
799 rule1 = &rd1->reg_rules[x];
800 for (y = 0; y < rd2->n_reg_rules; y++) {
801 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500802 /*
803 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700804 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500805 * a memcpy()
806 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700807 intersected_rule = &rd->reg_rules[rule_idx];
808 r = reg_rules_intersect(rule1, rule2,
809 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500810 /*
811 * No need to memset here the intersected rule here as
812 * we're not using the stack anymore
813 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700814 if (r)
815 continue;
816 rule_idx++;
817 }
818 }
819
820 if (rule_idx != num_rules) {
821 kfree(rd);
822 return NULL;
823 }
824
825 rd->n_reg_rules = num_rules;
826 rd->alpha2[0] = '9';
827 rd->alpha2[1] = '8';
828
829 return rd;
830}
831
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500832/*
833 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
834 * want to just have the channel structure use these
835 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700836static u32 map_regdom_flags(u32 rd_flags)
837{
838 u32 channel_flags = 0;
839 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
840 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
841 if (rd_flags & NL80211_RRF_NO_IBSS)
842 channel_flags |= IEEE80211_CHAN_NO_IBSS;
843 if (rd_flags & NL80211_RRF_DFS)
844 channel_flags |= IEEE80211_CHAN_RADAR;
845 return channel_flags;
846}
847
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800848static int freq_reg_info_regd(struct wiphy *wiphy,
849 u32 center_freq,
850 u32 *bandwidth,
851 const struct ieee80211_reg_rule **reg_rule,
852 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100853{
854 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800855 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800856 const struct ieee80211_regdomain *regd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700857 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100858
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800859 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800860
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500861 /*
862 * Follow the driver's regulatory domain, if present, unless a country
863 * IE has been processed or a user wants to help complaince further
864 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400865 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
866 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800867 wiphy->regd)
868 regd = wiphy->regd;
869
870 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700871 return -EINVAL;
872
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800873 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700874 const struct ieee80211_reg_rule *rr;
875 const struct ieee80211_freq_range *fr = NULL;
876 const struct ieee80211_power_rule *pr = NULL;
877
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800878 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700879 fr = &rr->freq_range;
880 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800881
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500882 /*
883 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800884 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500885 * not overwrite it once found
886 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800887 if (!band_rule_found)
888 band_rule_found = freq_in_rule_band(fr, center_freq);
889
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700890 max_bandwidth = freq_max_bandwidth(fr, center_freq);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800891
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700892 if (max_bandwidth && *bandwidth <= max_bandwidth) {
893 *reg_rule = rr;
894 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100895 break;
896 }
897 }
898
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800899 if (!band_rule_found)
900 return -ERANGE;
901
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700902 return !max_bandwidth;
903}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800904EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700905
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800906int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800907 const struct ieee80211_reg_rule **reg_rule)
908{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400909 assert_cfg80211_lock();
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800910 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{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001052 struct ieee80211_supported_band *sband;
1053 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001054 bool channel_changed = false;
1055 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001056
1057 assert_cfg80211_lock();
1058
1059 sband = wiphy->bands[reg_beacon->chan.band];
1060 chan = &sband->channels[chan_idx];
1061
1062 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1063 return;
1064
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001065 if (chan->beacon_found)
1066 return;
1067
1068 chan->beacon_found = true;
1069
1070 chan_before.center_freq = chan->center_freq;
1071 chan_before.flags = chan->flags;
1072
Luis R. Rodrigueza4ed90d2009-04-02 14:08:07 -04001073 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
1074 !(chan->orig_flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001075 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001076 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001077 }
1078
Luis R. Rodrigueza4ed90d2009-04-02 14:08:07 -04001079 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
1080 !(chan->orig_flags & IEEE80211_CHAN_NO_IBSS)) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001081 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001082 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001083 }
1084
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001085 if (channel_changed)
1086 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001087}
1088
1089/*
1090 * Called when a scan on a wiphy finds a beacon on
1091 * new channel
1092 */
1093static void wiphy_update_new_beacon(struct wiphy *wiphy,
1094 struct reg_beacon *reg_beacon)
1095{
1096 unsigned int i;
1097 struct ieee80211_supported_band *sband;
1098
1099 assert_cfg80211_lock();
1100
1101 if (!wiphy->bands[reg_beacon->chan.band])
1102 return;
1103
1104 sband = wiphy->bands[reg_beacon->chan.band];
1105
1106 for (i = 0; i < sband->n_channels; i++)
1107 handle_reg_beacon(wiphy, i, reg_beacon);
1108}
1109
1110/*
1111 * Called upon reg changes or a new wiphy is added
1112 */
1113static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1114{
1115 unsigned int i;
1116 struct ieee80211_supported_band *sband;
1117 struct reg_beacon *reg_beacon;
1118
1119 assert_cfg80211_lock();
1120
1121 if (list_empty(&reg_beacon_list))
1122 return;
1123
1124 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1125 if (!wiphy->bands[reg_beacon->chan.band])
1126 continue;
1127 sband = wiphy->bands[reg_beacon->chan.band];
1128 for (i = 0; i < sband->n_channels; i++)
1129 handle_reg_beacon(wiphy, i, reg_beacon);
1130 }
1131}
1132
1133static bool reg_is_world_roaming(struct wiphy *wiphy)
1134{
1135 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1136 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1137 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001138 if (last_request &&
1139 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001140 wiphy->custom_regulatory)
1141 return true;
1142 return false;
1143}
1144
1145/* Reap the advantages of previously found beacons */
1146static void reg_process_beacons(struct wiphy *wiphy)
1147{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001148 /*
1149 * Means we are just firing up cfg80211, so no beacons would
1150 * have been processed yet.
1151 */
1152 if (!last_request)
1153 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001154 if (!reg_is_world_roaming(wiphy))
1155 return;
1156 wiphy_update_beacon_reg(wiphy);
1157}
1158
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001159void wiphy_update_regulatory(struct wiphy *wiphy,
1160 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001161{
1162 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001163
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001164 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001165 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001166 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001167 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001168 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001169 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001170out:
1171 reg_process_beacons(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001172 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001173 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001174}
1175
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001176static void handle_channel_custom(struct wiphy *wiphy,
1177 enum ieee80211_band band,
1178 unsigned int chan_idx,
1179 const struct ieee80211_regdomain *regd)
1180{
1181 int r;
1182 u32 max_bandwidth = 0;
1183 const struct ieee80211_reg_rule *reg_rule = NULL;
1184 const struct ieee80211_power_rule *power_rule = NULL;
1185 struct ieee80211_supported_band *sband;
1186 struct ieee80211_channel *chan;
1187
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001188 assert_cfg80211_lock();
1189
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001190 sband = wiphy->bands[band];
1191 BUG_ON(chan_idx >= sband->n_channels);
1192 chan = &sband->channels[chan_idx];
1193
1194 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1195 &max_bandwidth, &reg_rule, regd);
1196
1197 if (r) {
1198 chan->flags = IEEE80211_CHAN_DISABLED;
1199 return;
1200 }
1201
1202 power_rule = &reg_rule->power_rule;
1203
1204 chan->flags |= map_regdom_flags(reg_rule->flags);
1205 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1206 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1207 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1208}
1209
1210static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1211 const struct ieee80211_regdomain *regd)
1212{
1213 unsigned int i;
1214 struct ieee80211_supported_band *sband;
1215
1216 BUG_ON(!wiphy->bands[band]);
1217 sband = wiphy->bands[band];
1218
1219 for (i = 0; i < sband->n_channels; i++)
1220 handle_channel_custom(wiphy, band, i, regd);
1221}
1222
1223/* Used by drivers prior to wiphy registration */
1224void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1225 const struct ieee80211_regdomain *regd)
1226{
1227 enum ieee80211_band band;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001228
1229 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001230 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1231 if (wiphy->bands[band])
1232 handle_band_custom(wiphy, band, regd);
1233 }
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001234 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001235}
1236EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1237
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001238static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1239 const struct ieee80211_regdomain *src_regd)
1240{
1241 struct ieee80211_regdomain *regd;
1242 int size_of_regd = 0;
1243 unsigned int i;
1244
1245 size_of_regd = sizeof(struct ieee80211_regdomain) +
1246 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1247
1248 regd = kzalloc(size_of_regd, GFP_KERNEL);
1249 if (!regd)
1250 return -ENOMEM;
1251
1252 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1253
1254 for (i = 0; i < src_regd->n_reg_rules; i++)
1255 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1256 sizeof(struct ieee80211_reg_rule));
1257
1258 *dst_regd = regd;
1259 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001260}
1261
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001262/*
1263 * Return value which can be used by ignore_request() to indicate
1264 * it has been determined we should intersect two regulatory domains
1265 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001266#define REG_INTERSECT 1
1267
Johannes Berg84fa4f42008-10-24 20:32:23 +02001268/* This has the logic which determines when a new request
1269 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001270static int ignore_request(struct wiphy *wiphy,
1271 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001272{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001273 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001274
1275 assert_cfg80211_lock();
1276
Johannes Berg84fa4f42008-10-24 20:32:23 +02001277 /* All initial requests are respected */
1278 if (!last_request)
1279 return 0;
1280
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001281 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001282 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001283 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001284 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001285
1286 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1287
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001288 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001289 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001290 if (last_request->initiator ==
1291 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001292 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001293 /*
1294 * Two cards with two APs claiming different
1295 * different Country IE alpha2s. We could
1296 * intersect them, but that seems unlikely
1297 * to be correct. Reject second one for now.
1298 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001299 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001300 return -EOPNOTSUPP;
1301 return -EALREADY;
1302 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001303 /*
1304 * Two consecutive Country IE hints on the same wiphy.
1305 * This should be picked up early by the driver/stack
1306 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001307 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001308 return 0;
1309 return -EALREADY;
1310 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001311 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001312 case NL80211_REGDOM_SET_BY_DRIVER:
1313 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001314 if (is_old_static_regdom(cfg80211_regdomain))
1315 return 0;
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001316 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001317 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001318 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001319 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001320
1321 /*
1322 * This would happen if you unplug and plug your card
1323 * back in or if you add a new device for which the previously
1324 * loaded card also agrees on the regulatory domain.
1325 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001326 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001327 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001328 return -EALREADY;
1329
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001330 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001331 case NL80211_REGDOM_SET_BY_USER:
1332 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001333 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001334 /*
1335 * If the user knows better the user should set the regdom
1336 * to their country before the IE is picked up
1337 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001338 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001339 last_request->intersect)
1340 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001341 /*
1342 * Process user requests only after previous user/driver/core
1343 * requests have been processed
1344 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001345 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1346 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1347 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001348 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001349 return -EAGAIN;
1350 }
1351
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001352 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001353 !regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001354 return -EALREADY;
1355
Johannes Berg84fa4f42008-10-24 20:32:23 +02001356 return 0;
1357 }
1358
1359 return -EINVAL;
1360}
1361
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001362/**
1363 * __regulatory_hint - hint to the wireless core a regulatory domain
1364 * @wiphy: if the hint comes from country information from an AP, this
1365 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001366 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001367 *
1368 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001369 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001370 *
1371 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1372 * already been set or other standard error codes.
1373 *
1374 * Caller must hold &cfg80211_mutex
1375 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001376static int __regulatory_hint(struct wiphy *wiphy,
1377 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001378{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001379 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001380 int r = 0;
1381
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001382 assert_cfg80211_lock();
1383
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001384 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001385
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001386 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001387 if (pending_request->initiator ==
1388 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001389 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001390 if (r) {
1391 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001392 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001393 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001394 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001395 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001396 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001397 /*
1398 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001399 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001400 * wiphy
1401 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001402 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001403 pending_request->initiator ==
1404 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001405 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001406 if (r) {
1407 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001408 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001409 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001410 r = -EALREADY;
1411 goto new_request;
1412 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001413 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001414 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001415 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001416
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001417new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001418 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001419
1420 last_request = pending_request;
1421 last_request->intersect = intersect;
1422
1423 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001424
1425 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001426 if (r < 0) {
1427 /*
1428 * Since CRDA will not be called in this case as we already
1429 * have applied the requested regulatory domain before we just
1430 * inform userspace we have processed the request
1431 */
1432 if (r == -EALREADY)
1433 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001434 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001435 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001436
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001437 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001438}
1439
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001440/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001441static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001442{
1443 int r = 0;
1444 struct wiphy *wiphy = NULL;
1445
1446 BUG_ON(!reg_request->alpha2);
1447
1448 mutex_lock(&cfg80211_mutex);
1449
1450 if (wiphy_idx_valid(reg_request->wiphy_idx))
1451 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1452
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001453 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001454 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001455 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001456 goto out;
1457 }
1458
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001459 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001460 /* This is required so that the orig_* parameters are saved */
1461 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1462 wiphy_update_regulatory(wiphy, reg_request->initiator);
1463out:
1464 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001465}
1466
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001467/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001468static void reg_process_pending_hints(void)
1469 {
1470 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001471
1472 spin_lock(&reg_requests_lock);
1473 while (!list_empty(&reg_requests_list)) {
1474 reg_request = list_first_entry(&reg_requests_list,
1475 struct regulatory_request,
1476 list);
1477 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001478
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001479 spin_unlock(&reg_requests_lock);
1480 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001481 spin_lock(&reg_requests_lock);
1482 }
1483 spin_unlock(&reg_requests_lock);
1484}
1485
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001486/* Processes beacon hints -- this has nothing to do with country IEs */
1487static void reg_process_pending_beacon_hints(void)
1488{
1489 struct cfg80211_registered_device *drv;
1490 struct reg_beacon *pending_beacon, *tmp;
1491
1492 mutex_lock(&cfg80211_mutex);
1493
1494 /* This goes through the _pending_ beacon list */
1495 spin_lock_bh(&reg_pending_beacons_lock);
1496
1497 if (list_empty(&reg_pending_beacons)) {
1498 spin_unlock_bh(&reg_pending_beacons_lock);
1499 goto out;
1500 }
1501
1502 list_for_each_entry_safe(pending_beacon, tmp,
1503 &reg_pending_beacons, list) {
1504
1505 list_del_init(&pending_beacon->list);
1506
1507 /* Applies the beacon hint to current wiphys */
1508 list_for_each_entry(drv, &cfg80211_drv_list, list)
1509 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1510
1511 /* Remembers the beacon hint for new wiphys or reg changes */
1512 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1513 }
1514
1515 spin_unlock_bh(&reg_pending_beacons_lock);
1516out:
1517 mutex_unlock(&cfg80211_mutex);
1518}
1519
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001520static void reg_todo(struct work_struct *work)
1521{
1522 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001523 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001524}
1525
1526static DECLARE_WORK(reg_work, reg_todo);
1527
1528static void queue_regulatory_request(struct regulatory_request *request)
1529{
1530 spin_lock(&reg_requests_lock);
1531 list_add_tail(&request->list, &reg_requests_list);
1532 spin_unlock(&reg_requests_lock);
1533
1534 schedule_work(&reg_work);
1535}
1536
1537/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001538static int regulatory_hint_core(const char *alpha2)
1539{
1540 struct regulatory_request *request;
1541
1542 BUG_ON(last_request);
1543
1544 request = kzalloc(sizeof(struct regulatory_request),
1545 GFP_KERNEL);
1546 if (!request)
1547 return -ENOMEM;
1548
1549 request->alpha2[0] = alpha2[0];
1550 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001551 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001552
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001553 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001554
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001555 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001556}
1557
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001558/* User hints */
1559int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001560{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001561 struct regulatory_request *request;
1562
Johannes Bergbe3d4812008-10-24 20:32:21 +02001563 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001564
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001565 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1566 if (!request)
1567 return -ENOMEM;
1568
1569 request->wiphy_idx = WIPHY_IDX_STALE;
1570 request->alpha2[0] = alpha2[0];
1571 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001572 request->initiator = NL80211_REGDOM_SET_BY_USER,
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001573
1574 queue_regulatory_request(request);
1575
1576 return 0;
1577}
1578
1579/* Driver hints */
1580int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1581{
1582 struct regulatory_request *request;
1583
1584 BUG_ON(!alpha2);
1585 BUG_ON(!wiphy);
1586
1587 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1588 if (!request)
1589 return -ENOMEM;
1590
1591 request->wiphy_idx = get_wiphy_idx(wiphy);
1592
1593 /* Must have registered wiphy first */
1594 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1595
1596 request->alpha2[0] = alpha2[0];
1597 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001598 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001599
1600 queue_regulatory_request(request);
1601
1602 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001603}
1604EXPORT_SYMBOL(regulatory_hint);
1605
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001606static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1607 u32 country_ie_checksum)
1608{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001609 struct wiphy *request_wiphy;
1610
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001611 assert_cfg80211_lock();
1612
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001613 if (unlikely(last_request->initiator !=
1614 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1615 return false;
1616
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001617 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1618
1619 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001620 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001621
1622 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001623 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001624 /*
1625 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001626 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001627 * on the device
1628 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001629 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1630 return true;
1631 return false;
1632}
1633
1634void regulatory_hint_11d(struct wiphy *wiphy,
1635 u8 *country_ie,
1636 u8 country_ie_len)
1637{
1638 struct ieee80211_regdomain *rd = NULL;
1639 char alpha2[2];
1640 u32 checksum = 0;
1641 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001642 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001643
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001644 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001645
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001646 if (unlikely(!last_request)) {
1647 mutex_unlock(&cfg80211_mutex);
1648 return;
1649 }
1650
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001651 /* IE len must be evenly divisible by 2 */
1652 if (country_ie_len & 0x01)
1653 goto out;
1654
1655 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1656 goto out;
1657
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001658 /*
1659 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001660 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001661 * we were able to process the last regulatory_hint_11d() call
1662 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001663 if (country_ie_regdomain)
1664 goto out;
1665
1666 alpha2[0] = country_ie[0];
1667 alpha2[1] = country_ie[1];
1668
1669 if (country_ie[2] == 'I')
1670 env = ENVIRON_INDOOR;
1671 else if (country_ie[2] == 'O')
1672 env = ENVIRON_OUTDOOR;
1673
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001674 /*
1675 * We will run this for *every* beacon processed for the BSSID, so
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001676 * we optimize an early check to exit out early if we don't have to
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001677 * do anything
1678 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001679 if (likely(last_request->initiator ==
1680 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1681 wiphy_idx_valid(last_request->wiphy_idx))) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001682 struct cfg80211_registered_device *drv_last_ie;
1683
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001684 drv_last_ie =
1685 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001686
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001687 /*
1688 * Lets keep this simple -- we trust the first AP
1689 * after we intersect with CRDA
1690 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001691 if (likely(&drv_last_ie->wiphy == wiphy)) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001692 /*
1693 * Ignore IEs coming in on this wiphy with
1694 * the same alpha2 and environment cap
1695 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001696 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1697 alpha2) &&
1698 env == drv_last_ie->env)) {
1699 goto out;
1700 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001701 /*
1702 * the wiphy moved on to another BSSID or the AP
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001703 * was reconfigured. XXX: We need to deal with the
1704 * case where the user suspends and goes to goes
1705 * to another country, and then gets IEs from an
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001706 * AP with different settings
1707 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001708 goto out;
1709 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001710 /*
1711 * Ignore IEs coming in on two separate wiphys with
1712 * the same alpha2 and environment cap
1713 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001714 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1715 alpha2) &&
1716 env == drv_last_ie->env)) {
1717 goto out;
1718 }
1719 /* We could potentially intersect though */
1720 goto out;
1721 }
1722 }
1723
1724 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1725 if (!rd)
1726 goto out;
1727
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001728 /*
1729 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001730 * the future when we want to add suspend/resume support and having
1731 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001732 * move to another AP. Right now we just trust the first AP.
1733 *
1734 * If we hit this before we add this support we want to be informed of
1735 * it as it would indicate a mistake in the current design
1736 */
1737 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001738 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001739
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001740 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1741 if (!request)
1742 goto free_rd_out;
1743
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001744 /*
1745 * We keep this around for when CRDA comes back with a response so
1746 * we can intersect with that
1747 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001748 country_ie_regdomain = rd;
1749
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001750 request->wiphy_idx = get_wiphy_idx(wiphy);
1751 request->alpha2[0] = rd->alpha2[0];
1752 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001753 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001754 request->country_ie_checksum = checksum;
1755 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001756
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001757 mutex_unlock(&cfg80211_mutex);
1758
1759 queue_regulatory_request(request);
1760
1761 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001762
1763free_rd_out:
1764 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001765out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001766 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001767}
1768EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001769
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001770static bool freq_is_chan_12_13_14(u16 freq)
1771{
1772 if (freq == ieee80211_channel_to_frequency(12) ||
1773 freq == ieee80211_channel_to_frequency(13) ||
1774 freq == ieee80211_channel_to_frequency(14))
1775 return true;
1776 return false;
1777}
1778
1779int regulatory_hint_found_beacon(struct wiphy *wiphy,
1780 struct ieee80211_channel *beacon_chan,
1781 gfp_t gfp)
1782{
1783 struct reg_beacon *reg_beacon;
1784
1785 if (likely((beacon_chan->beacon_found ||
1786 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1787 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1788 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1789 return 0;
1790
1791 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1792 if (!reg_beacon)
1793 return -ENOMEM;
1794
1795#ifdef CONFIG_CFG80211_REG_DEBUG
1796 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1797 "frequency: %d MHz (Ch %d) on %s\n",
1798 beacon_chan->center_freq,
1799 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1800 wiphy_name(wiphy));
1801#endif
1802 memcpy(&reg_beacon->chan, beacon_chan,
1803 sizeof(struct ieee80211_channel));
1804
1805
1806 /*
1807 * Since we can be called from BH or and non-BH context
1808 * we must use spin_lock_bh()
1809 */
1810 spin_lock_bh(&reg_pending_beacons_lock);
1811 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1812 spin_unlock_bh(&reg_pending_beacons_lock);
1813
1814 schedule_work(&reg_work);
1815
1816 return 0;
1817}
1818
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001819static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001820{
1821 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001822 const struct ieee80211_reg_rule *reg_rule = NULL;
1823 const struct ieee80211_freq_range *freq_range = NULL;
1824 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001825
1826 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1827 "(max_antenna_gain, max_eirp)\n");
1828
1829 for (i = 0; i < rd->n_reg_rules; i++) {
1830 reg_rule = &rd->reg_rules[i];
1831 freq_range = &reg_rule->freq_range;
1832 power_rule = &reg_rule->power_rule;
1833
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001834 /*
1835 * There may not be documentation for max antenna gain
1836 * in certain regions
1837 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001838 if (power_rule->max_antenna_gain)
1839 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1840 "(%d mBi, %d mBm)\n",
1841 freq_range->start_freq_khz,
1842 freq_range->end_freq_khz,
1843 freq_range->max_bandwidth_khz,
1844 power_rule->max_antenna_gain,
1845 power_rule->max_eirp);
1846 else
1847 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1848 "(N/A, %d mBm)\n",
1849 freq_range->start_freq_khz,
1850 freq_range->end_freq_khz,
1851 freq_range->max_bandwidth_khz,
1852 power_rule->max_eirp);
1853 }
1854}
1855
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001856static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001857{
1858
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001859 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001860
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001861 if (last_request->initiator ==
1862 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001863 struct cfg80211_registered_device *drv;
1864 drv = cfg80211_drv_by_wiphy_idx(
1865 last_request->wiphy_idx);
1866 if (drv) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001867 printk(KERN_INFO "cfg80211: Current regulatory "
1868 "domain updated by AP to: %c%c\n",
1869 drv->country_ie_alpha2[0],
1870 drv->country_ie_alpha2[1]);
1871 } else
1872 printk(KERN_INFO "cfg80211: Current regulatory "
1873 "domain intersected: \n");
1874 } else
1875 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001876 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001877 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001878 printk(KERN_INFO "cfg80211: World regulatory "
1879 "domain updated:\n");
1880 else {
1881 if (is_unknown_alpha2(rd->alpha2))
1882 printk(KERN_INFO "cfg80211: Regulatory domain "
1883 "changed to driver built-in settings "
1884 "(unknown country)\n");
1885 else
1886 printk(KERN_INFO "cfg80211: Regulatory domain "
1887 "changed to country: %c%c\n",
1888 rd->alpha2[0], rd->alpha2[1]);
1889 }
1890 print_rd_rules(rd);
1891}
1892
Johannes Berg2df78162008-10-28 16:49:41 +01001893static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001894{
1895 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1896 rd->alpha2[0], rd->alpha2[1]);
1897 print_rd_rules(rd);
1898}
1899
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001900#ifdef CONFIG_CFG80211_REG_DEBUG
1901static void reg_country_ie_process_debug(
1902 const struct ieee80211_regdomain *rd,
1903 const struct ieee80211_regdomain *country_ie_regdomain,
1904 const struct ieee80211_regdomain *intersected_rd)
1905{
1906 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1907 print_regdomain_info(country_ie_regdomain);
1908 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1909 print_regdomain_info(rd);
1910 if (intersected_rd) {
1911 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1912 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08001913 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001914 return;
1915 }
1916 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1917}
1918#else
1919static inline void reg_country_ie_process_debug(
1920 const struct ieee80211_regdomain *rd,
1921 const struct ieee80211_regdomain *country_ie_regdomain,
1922 const struct ieee80211_regdomain *intersected_rd)
1923{
1924}
1925#endif
1926
Johannes Bergd2372b32008-10-24 20:32:20 +02001927/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001928static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001929{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001930 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001931 struct cfg80211_registered_device *drv = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001932 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001933 /* Some basic sanity checks first */
1934
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001935 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001936 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001937 return -EINVAL;
1938 update_world_regdomain(rd);
1939 return 0;
1940 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001941
1942 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1943 !is_unknown_alpha2(rd->alpha2))
1944 return -EINVAL;
1945
Johannes Bergf6037d02008-10-21 11:01:33 +02001946 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001947 return -EINVAL;
1948
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001949 /*
1950 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001951 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001952 * and the pending request came in from a country IE
1953 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001954 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001955 /*
1956 * If someone else asked us to change the rd lets only bother
1957 * checking if the alpha2 changes if CRDA was already called
1958 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001959 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001960 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001961 return -EINVAL;
1962 }
1963
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001964 /*
1965 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001966 * and finally inform them of what we have done, in case they want
1967 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001968 * internal EEPROM data
1969 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001970
Johannes Bergf6037d02008-10-21 11:01:33 +02001971 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001972 return -EINVAL;
1973
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001974 if (!is_valid_rd(rd)) {
1975 printk(KERN_ERR "cfg80211: Invalid "
1976 "regulatory domain detected:\n");
1977 print_regdomain_info(rd);
1978 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001979 }
1980
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001981 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1982
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001983 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001984 int r;
1985
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001986 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001987 reset_regdomains();
1988 cfg80211_regdomain = rd;
1989 return 0;
1990 }
1991
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001992 /*
1993 * For a driver hint, lets copy the regulatory domain the
1994 * driver wanted to the wiphy to deal with conflicts
1995 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001996
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001997 BUG_ON(request_wiphy->regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001998
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001999 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002000 if (r)
2001 return r;
2002
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002003 reset_regdomains();
2004 cfg80211_regdomain = rd;
2005 return 0;
2006 }
2007
2008 /* Intersection requires a bit more work */
2009
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002010 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002011
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002012 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2013 if (!intersected_rd)
2014 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002015
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002016 /*
2017 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002018 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002019 * domain we keep it for its private use
2020 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002021 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002022 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002023 else
2024 kfree(rd);
2025
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002026 rd = NULL;
2027
2028 reset_regdomains();
2029 cfg80211_regdomain = intersected_rd;
2030
2031 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002032 }
2033
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002034 /*
2035 * Country IE requests are handled a bit differently, we intersect
2036 * the country IE rd with what CRDA believes that country should have
2037 */
2038
2039 BUG_ON(!country_ie_regdomain);
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002040 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002041
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002042 /*
2043 * Intersect what CRDA returned and our what we
2044 * had built from the Country IE received
2045 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002046
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002047 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002048
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002049 reg_country_ie_process_debug(rd,
2050 country_ie_regdomain,
2051 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002052
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002053 kfree(country_ie_regdomain);
2054 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002055
2056 if (!intersected_rd)
2057 return -EINVAL;
2058
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002059 drv = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002060
2061 drv->country_ie_alpha2[0] = rd->alpha2[0];
2062 drv->country_ie_alpha2[1] = rd->alpha2[1];
2063 drv->env = last_request->country_ie_env;
2064
2065 BUG_ON(intersected_rd == rd);
2066
2067 kfree(rd);
2068 rd = NULL;
2069
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002070 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002071 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002072
2073 return 0;
2074}
2075
2076
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002077/*
2078 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002079 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002080 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2081 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002082int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002083{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002084 int r;
2085
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002086 assert_cfg80211_lock();
2087
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002088 /* Note that this doesn't update the wiphys, this is done below */
2089 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002090 if (r) {
2091 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002092 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002093 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002094
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002095 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002096 if (!last_request->intersect)
2097 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002098
2099 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002100 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002101
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002102 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002103
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002104 nl80211_send_reg_change_event(last_request);
2105
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002106 return r;
2107}
2108
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002109/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002110void reg_device_remove(struct wiphy *wiphy)
2111{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002112 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002113
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002114 assert_cfg80211_lock();
2115
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002116 kfree(wiphy->regd);
2117
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002118 if (last_request)
2119 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002120
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002121 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002122 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002123
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002124 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002125 last_request->country_ie_env = ENVIRON_ANY;
2126}
2127
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002128int regulatory_init(void)
2129{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002130 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002131
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002132 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2133 if (IS_ERR(reg_pdev))
2134 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002135
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002136 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002137 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002138
Johannes Berg734366d2008-09-15 10:56:48 +02002139#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002140 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002141
Johannes Berg942b25c2008-09-15 11:26:47 +02002142 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02002143 print_regdomain_info(cfg80211_regdomain);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002144 /*
2145 * The old code still requests for a new regdomain and if
Johannes Berg734366d2008-09-15 10:56:48 +02002146 * you have CRDA you get it updated, otherwise you get
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002147 * stuck with the static values. Since "EU" is not a valid
2148 * ISO / IEC 3166 alpha2 code we can't expect userpace to
2149 * give us a regulatory domain for it. We need last_request
2150 * iniitalized though so lets just send a request which we
2151 * know will be ignored... this crap will be removed once
2152 * OLD_REG dies.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002153 */
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002154 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002155#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002156 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002157
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -04002158 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002159#endif
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002160 if (err) {
2161 if (err == -ENOMEM)
2162 return err;
2163 /*
2164 * N.B. kobject_uevent_env() can fail mainly for when we're out
2165 * memory which is handled and propagated appropriately above
2166 * but it can also fail during a netlink_broadcast() or during
2167 * early boot for call_usermodehelper(). For now treat these
2168 * errors as non-fatal.
2169 */
2170 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2171 "to call CRDA during init");
2172#ifdef CONFIG_CFG80211_REG_DEBUG
2173 /* We want to find out exactly why when debugging */
2174 WARN_ON(err);
2175#endif
2176 }
Johannes Berg734366d2008-09-15 10:56:48 +02002177
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002178 return 0;
2179}
2180
2181void regulatory_exit(void)
2182{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002183 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002184 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002185
2186 cancel_work_sync(&reg_work);
2187
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002188 mutex_lock(&cfg80211_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002189
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002190 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002191
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002192 kfree(country_ie_regdomain);
2193 country_ie_regdomain = NULL;
2194
Johannes Bergf6037d02008-10-21 11:01:33 +02002195 kfree(last_request);
2196
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002197 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002198
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002199 spin_lock_bh(&reg_pending_beacons_lock);
2200 if (!list_empty(&reg_pending_beacons)) {
2201 list_for_each_entry_safe(reg_beacon, btmp,
2202 &reg_pending_beacons, list) {
2203 list_del(&reg_beacon->list);
2204 kfree(reg_beacon);
2205 }
2206 }
2207 spin_unlock_bh(&reg_pending_beacons_lock);
2208
2209 if (!list_empty(&reg_beacon_list)) {
2210 list_for_each_entry_safe(reg_beacon, btmp,
2211 &reg_beacon_list, list) {
2212 list_del(&reg_beacon->list);
2213 kfree(reg_beacon);
2214 }
2215 }
2216
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002217 spin_lock(&reg_requests_lock);
2218 if (!list_empty(&reg_requests_list)) {
2219 list_for_each_entry_safe(reg_request, tmp,
2220 &reg_requests_list, list) {
2221 list_del(&reg_request->list);
2222 kfree(reg_request);
2223 }
2224 }
2225 spin_unlock(&reg_requests_lock);
2226
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002227 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002228}