blob: 8d176a8010baebcef113478a2e845ec6c09f497a [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. Rodriguezfb1fc7a2009-02-21 00:04:31 -050051/*
52 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020053 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050054 * information to give us an alpha2
55 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080056const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020057
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050058/*
59 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080060 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050061 * what it thinks should apply for the same country
62 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080063static const struct ieee80211_regdomain *country_ie_regdomain;
64
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050065/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050066static LIST_HEAD(reg_requests_list);
67static spinlock_t reg_requests_lock;
68
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050069/* Used to queue up beacon hints for review */
70static LIST_HEAD(reg_pending_beacons);
71static spinlock_t reg_pending_beacons_lock;
72
73/* Used to keep track of processed beacon hints */
74static LIST_HEAD(reg_beacon_list);
75
76struct reg_beacon {
77 struct list_head list;
78 struct ieee80211_channel chan;
79};
80
Johannes Berg734366d2008-09-15 10:56:48 +020081/* We keep a static world regulatory domain in case of the absence of CRDA */
82static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080083 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +020084 .alpha2 = "00",
85 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -050086 /* IEEE 802.11b/g, channels 1..11 */
87 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080088 /* IEEE 802.11b/g, channels 12..13. No HT40
89 * channel fits here. */
90 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -050091 NL80211_RRF_PASSIVE_SCAN |
92 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080093 /* IEEE 802.11 channel 14 - Only JP enables
94 * this and for 802.11b only */
95 REG_RULE(2484-10, 2484+10, 20, 6, 20,
96 NL80211_RRF_PASSIVE_SCAN |
97 NL80211_RRF_NO_IBSS |
98 NL80211_RRF_NO_OFDM),
99 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800100 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800101 NL80211_RRF_PASSIVE_SCAN |
102 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500103
104 /* NB: 5260 MHz - 5700 MHz requies DFS */
105
106 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800107 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500108 NL80211_RRF_PASSIVE_SCAN |
109 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200110 }
111};
112
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200113static const struct ieee80211_regdomain *cfg80211_world_regdom =
114 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200115
116#ifdef CONFIG_WIRELESS_OLD_REGULATORY
117static char *ieee80211_regdom = "US";
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400118#else
119static char *ieee80211_regdom = "00";
120#endif
121
Johannes Berg734366d2008-09-15 10:56:48 +0200122module_param(ieee80211_regdom, charp, 0444);
123MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
124
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400125#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500126/*
127 * We assume 40 MHz bandwidth for the old regulatory work.
Johannes Berg734366d2008-09-15 10:56:48 +0200128 * We make emphasis we are using the exact same frequencies
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500129 * as before
130 */
Johannes Berg734366d2008-09-15 10:56:48 +0200131
132static const struct ieee80211_regdomain us_regdom = {
133 .n_reg_rules = 6,
134 .alpha2 = "US",
135 .reg_rules = {
136 /* IEEE 802.11b/g, channels 1..11 */
137 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
138 /* IEEE 802.11a, channel 36 */
139 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
140 /* IEEE 802.11a, channel 40 */
141 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
142 /* IEEE 802.11a, channel 44 */
143 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
144 /* IEEE 802.11a, channels 48..64 */
145 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
146 /* IEEE 802.11a, channels 149..165, outdoor */
147 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
148 }
149};
150
151static const struct ieee80211_regdomain jp_regdom = {
152 .n_reg_rules = 3,
153 .alpha2 = "JP",
154 .reg_rules = {
155 /* IEEE 802.11b/g, channels 1..14 */
156 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
157 /* IEEE 802.11a, channels 34..48 */
158 REG_RULE(5170-10, 5240+10, 40, 6, 20,
159 NL80211_RRF_PASSIVE_SCAN),
160 /* IEEE 802.11a, channels 52..64 */
161 REG_RULE(5260-10, 5320+10, 40, 6, 20,
162 NL80211_RRF_NO_IBSS |
163 NL80211_RRF_DFS),
164 }
165};
166
167static const struct ieee80211_regdomain eu_regdom = {
168 .n_reg_rules = 6,
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500169 /*
170 * This alpha2 is bogus, we leave it here just for stupid
171 * backward compatibility
172 */
Johannes Berg734366d2008-09-15 10:56:48 +0200173 .alpha2 = "EU",
174 .reg_rules = {
175 /* IEEE 802.11b/g, channels 1..13 */
176 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
177 /* IEEE 802.11a, channel 36 */
178 REG_RULE(5180-10, 5180+10, 40, 6, 23,
179 NL80211_RRF_PASSIVE_SCAN),
180 /* IEEE 802.11a, channel 40 */
181 REG_RULE(5200-10, 5200+10, 40, 6, 23,
182 NL80211_RRF_PASSIVE_SCAN),
183 /* IEEE 802.11a, channel 44 */
184 REG_RULE(5220-10, 5220+10, 40, 6, 23,
185 NL80211_RRF_PASSIVE_SCAN),
186 /* IEEE 802.11a, channels 48..64 */
187 REG_RULE(5240-10, 5320+10, 40, 6, 20,
188 NL80211_RRF_NO_IBSS |
189 NL80211_RRF_DFS),
190 /* IEEE 802.11a, channels 100..140 */
191 REG_RULE(5500-10, 5700+10, 40, 6, 30,
192 NL80211_RRF_NO_IBSS |
193 NL80211_RRF_DFS),
194 }
195};
196
197static const struct ieee80211_regdomain *static_regdom(char *alpha2)
198{
199 if (alpha2[0] == 'U' && alpha2[1] == 'S')
200 return &us_regdom;
201 if (alpha2[0] == 'J' && alpha2[1] == 'P')
202 return &jp_regdom;
203 if (alpha2[0] == 'E' && alpha2[1] == 'U')
204 return &eu_regdom;
205 /* Default, as per the old rules */
206 return &us_regdom;
207}
208
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200209static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200210{
211 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
212 return true;
213 return false;
214}
Johannes Berg734366d2008-09-15 10:56:48 +0200215#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200216static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
217{
218 return false;
219}
220#endif
221
Johannes Berg734366d2008-09-15 10:56:48 +0200222static void reset_regdomains(void)
223{
Johannes Berg942b25c2008-09-15 11:26:47 +0200224 /* avoid freeing static information or freeing something twice */
225 if (cfg80211_regdomain == cfg80211_world_regdom)
226 cfg80211_regdomain = NULL;
227 if (cfg80211_world_regdom == &world_regdom)
228 cfg80211_world_regdom = NULL;
229 if (cfg80211_regdomain == &world_regdom)
230 cfg80211_regdomain = NULL;
231 if (is_old_static_regdom(cfg80211_regdomain))
232 cfg80211_regdomain = NULL;
233
234 kfree(cfg80211_regdomain);
235 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200236
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200237 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200238 cfg80211_regdomain = NULL;
239}
240
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500241/*
242 * Dynamic world regulatory domain requested by the wireless
243 * core upon initialization
244 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200245static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200246{
Johannes Bergf6037d02008-10-21 11:01:33 +0200247 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200248
249 reset_regdomains();
250
251 cfg80211_world_regdom = rd;
252 cfg80211_regdomain = rd;
253}
Johannes Berg734366d2008-09-15 10:56:48 +0200254
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200255bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100256{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700257 if (!alpha2)
258 return false;
259 if (alpha2[0] == '0' && alpha2[1] == '0')
260 return true;
261 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100262}
263
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200264static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700265{
266 if (!alpha2)
267 return false;
268 if (alpha2[0] != 0 && alpha2[1] != 0)
269 return true;
270 return false;
271}
Johannes Berg8318d782008-01-24 19:38:38 +0100272
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700273static bool is_alpha_upper(char letter)
274{
275 /* ASCII A - Z */
276 if (letter >= 65 && letter <= 90)
277 return true;
278 return false;
279}
280
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200281static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700282{
283 if (!alpha2)
284 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500285 /*
286 * Special case where regulatory domain was built by driver
287 * but a specific alpha2 cannot be determined
288 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700289 if (alpha2[0] == '9' && alpha2[1] == '9')
290 return true;
291 return false;
292}
293
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800294static bool is_intersected_alpha2(const char *alpha2)
295{
296 if (!alpha2)
297 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500298 /*
299 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800300 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500301 * structures
302 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800303 if (alpha2[0] == '9' && alpha2[1] == '8')
304 return true;
305 return false;
306}
307
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200308static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700309{
310 if (!alpha2)
311 return false;
312 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
313 return true;
314 return false;
315}
316
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200317static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700318{
319 if (!alpha2_x || !alpha2_y)
320 return false;
321 if (alpha2_x[0] == alpha2_y[0] &&
322 alpha2_x[1] == alpha2_y[1])
323 return true;
324 return false;
325}
326
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500327static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700328{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500329 assert_cfg80211_lock();
330
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700331 if (!cfg80211_regdomain)
332 return true;
333 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
334 return false;
335 return true;
336}
337
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800338/**
339 * country_ie_integrity_changes - tells us if the country IE has changed
340 * @checksum: checksum of country IE of fields we are interested in
341 *
342 * If the country IE has not changed you can ignore it safely. This is
343 * useful to determine if two devices are seeing two different country IEs
344 * even on the same alpha2. Note that this will return false if no IE has
345 * been set on the wireless core yet.
346 */
347static bool country_ie_integrity_changes(u32 checksum)
348{
349 /* If no IE has been set then the checksum doesn't change */
350 if (unlikely(!last_request->country_ie_checksum))
351 return false;
352 if (unlikely(last_request->country_ie_checksum != checksum))
353 return true;
354 return false;
355}
356
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500357/*
358 * This lets us keep regulatory code which is updated on a regulatory
359 * basis in userspace.
360 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700361static int call_crda(const char *alpha2)
362{
363 char country_env[9 + 2] = "COUNTRY=";
364 char *envp[] = {
365 country_env,
366 NULL
367 };
368
369 if (!is_world_regdom((char *) alpha2))
370 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
371 alpha2[0], alpha2[1]);
372 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700373 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
374 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700375
376 country_env[8] = alpha2[0];
377 country_env[9] = alpha2[1];
378
379 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
380}
381
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700382/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200383bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700384{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400385 assert_cfg80211_lock();
386
Johannes Bergf6037d02008-10-21 11:01:33 +0200387 if (!last_request)
388 return false;
389
390 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700391}
392
393/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200394static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700395{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200396 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700397 u32 freq_diff;
398
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800399 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700400 return false;
401
402 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
403 return false;
404
405 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
406
Roel Kluinbd05f282009-03-03 22:55:21 +0100407 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
408 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700409 return false;
410
411 return true;
412}
413
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200414static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700415{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200416 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700417 unsigned int i;
418
419 if (!rd->n_reg_rules)
420 return false;
421
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800422 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
423 return false;
424
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700425 for (i = 0; i < rd->n_reg_rules; i++) {
426 reg_rule = &rd->reg_rules[i];
427 if (!is_valid_reg_rule(reg_rule))
428 return false;
429 }
430
431 return true;
432}
433
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400434static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
435 u32 center_freq_khz,
436 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700437{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400438 u32 start_freq_khz, end_freq_khz;
439
440 start_freq_khz = center_freq_khz - (bw_khz/2);
441 end_freq_khz = center_freq_khz + (bw_khz/2);
442
443 if (start_freq_khz >= freq_range->start_freq_khz &&
444 end_freq_khz <= freq_range->end_freq_khz)
445 return true;
446
447 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700448}
449
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800450/**
451 * freq_in_rule_band - tells us if a frequency is in a frequency band
452 * @freq_range: frequency rule we want to query
453 * @freq_khz: frequency we are inquiring about
454 *
455 * This lets us know if a specific frequency rule is or is not relevant to
456 * a specific frequency's band. Bands are device specific and artificial
457 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
458 * safe for now to assume that a frequency rule should not be part of a
459 * frequency's band if the start freq or end freq are off by more than 2 GHz.
460 * This resolution can be lowered and should be considered as we add
461 * regulatory rule support for other "bands".
462 **/
463static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
464 u32 freq_khz)
465{
466#define ONE_GHZ_IN_KHZ 1000000
467 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
468 return true;
469 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
470 return true;
471 return false;
472#undef ONE_GHZ_IN_KHZ
473}
474
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500475/*
476 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800477 * structure has a lot of information which the IE doesn't yet have,
478 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500479 * with our userspace regulatory agent to get lower bounds.
480 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800481static struct ieee80211_regdomain *country_ie_2_rd(
482 u8 *country_ie,
483 u8 country_ie_len,
484 u32 *checksum)
485{
486 struct ieee80211_regdomain *rd = NULL;
487 unsigned int i = 0;
488 char alpha2[2];
489 u32 flags = 0;
490 u32 num_rules = 0, size_of_regd = 0;
491 u8 *triplets_start = NULL;
492 u8 len_at_triplet = 0;
493 /* the last channel we have registered in a subband (triplet) */
494 int last_sub_max_channel = 0;
495
496 *checksum = 0xDEADBEEF;
497
498 /* Country IE requirements */
499 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
500 country_ie_len & 0x01);
501
502 alpha2[0] = country_ie[0];
503 alpha2[1] = country_ie[1];
504
505 /*
506 * Third octet can be:
507 * 'I' - Indoor
508 * 'O' - Outdoor
509 *
510 * anything else we assume is no restrictions
511 */
512 if (country_ie[2] == 'I')
513 flags = NL80211_RRF_NO_OUTDOOR;
514 else if (country_ie[2] == 'O')
515 flags = NL80211_RRF_NO_INDOOR;
516
517 country_ie += 3;
518 country_ie_len -= 3;
519
520 triplets_start = country_ie;
521 len_at_triplet = country_ie_len;
522
523 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
524
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500525 /*
526 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800527 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500528 * for each channel subband
529 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800530 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800531 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800532 struct ieee80211_country_ie_triplet *triplet =
533 (struct ieee80211_country_ie_triplet *) country_ie;
534 int cur_sub_max_channel = 0, cur_channel = 0;
535
536 if (triplet->ext.reg_extension_id >=
537 IEEE80211_COUNTRY_EXTENSION_ID) {
538 country_ie += 3;
539 country_ie_len -= 3;
540 continue;
541 }
542
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800543 /* 2 GHz */
544 if (triplet->chans.first_channel <= 14)
545 end_channel = triplet->chans.first_channel +
546 triplet->chans.num_channels;
547 else
548 /*
549 * 5 GHz -- For example in country IEs if the first
550 * channel given is 36 and the number of channels is 4
551 * then the individual channel numbers defined for the
552 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
553 * and not 36, 37, 38, 39.
554 *
555 * See: http://tinyurl.com/11d-clarification
556 */
557 end_channel = triplet->chans.first_channel +
558 (4 * (triplet->chans.num_channels - 1));
559
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800560 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800561 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800562
563 /* Basic sanity check */
564 if (cur_sub_max_channel < cur_channel)
565 return NULL;
566
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500567 /*
568 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800569 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500570 * increasing
571 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800572 if (last_sub_max_channel) {
573 if (cur_channel <= last_sub_max_channel)
574 return NULL;
575 if (cur_sub_max_channel <= last_sub_max_channel)
576 return NULL;
577 }
578
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500579 /*
580 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800581 * we can throw ext triplets as part of this soup,
582 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500583 * don't support them
584 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800585 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
586 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
587 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
588
589 last_sub_max_channel = cur_sub_max_channel;
590
591 country_ie += 3;
592 country_ie_len -= 3;
593 num_rules++;
594
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500595 /*
596 * Note: this is not a IEEE requirement but
597 * simply a memory requirement
598 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800599 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
600 return NULL;
601 }
602
603 country_ie = triplets_start;
604 country_ie_len = len_at_triplet;
605
606 size_of_regd = sizeof(struct ieee80211_regdomain) +
607 (num_rules * sizeof(struct ieee80211_reg_rule));
608
609 rd = kzalloc(size_of_regd, GFP_KERNEL);
610 if (!rd)
611 return NULL;
612
613 rd->n_reg_rules = num_rules;
614 rd->alpha2[0] = alpha2[0];
615 rd->alpha2[1] = alpha2[1];
616
617 /* This time around we fill in the rd */
618 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800619 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800620 struct ieee80211_country_ie_triplet *triplet =
621 (struct ieee80211_country_ie_triplet *) country_ie;
622 struct ieee80211_reg_rule *reg_rule = NULL;
623 struct ieee80211_freq_range *freq_range = NULL;
624 struct ieee80211_power_rule *power_rule = NULL;
625
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500626 /*
627 * Must parse if dot11RegulatoryClassesRequired is true,
628 * we don't support this yet
629 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800630 if (triplet->ext.reg_extension_id >=
631 IEEE80211_COUNTRY_EXTENSION_ID) {
632 country_ie += 3;
633 country_ie_len -= 3;
634 continue;
635 }
636
637 reg_rule = &rd->reg_rules[i];
638 freq_range = &reg_rule->freq_range;
639 power_rule = &reg_rule->power_rule;
640
641 reg_rule->flags = flags;
642
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800643 /* 2 GHz */
644 if (triplet->chans.first_channel <= 14)
645 end_channel = triplet->chans.first_channel +
646 triplet->chans.num_channels;
647 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800648 end_channel = triplet->chans.first_channel +
649 (4 * (triplet->chans.num_channels - 1));
650
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500651 /*
652 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800653 * the actual band edge, not the center of freq for
654 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500655 * the channels passed
656 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800657 freq_range->start_freq_khz =
658 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
659 triplet->chans.first_channel) - 10);
660 freq_range->end_freq_khz =
661 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800662 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800663
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500664 /*
665 * These are large arbitrary values we use to intersect later.
666 * Increment this if we ever support >= 40 MHz channels
667 * in IEEE 802.11
668 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800669 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
670 power_rule->max_antenna_gain = DBI_TO_MBI(100);
671 power_rule->max_eirp = DBM_TO_MBM(100);
672
673 country_ie += 3;
674 country_ie_len -= 3;
675 i++;
676
677 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
678 }
679
680 return rd;
681}
682
683
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500684/*
685 * Helper for regdom_intersect(), this does the real
686 * mathematical intersection fun
687 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700688static int reg_rules_intersect(
689 const struct ieee80211_reg_rule *rule1,
690 const struct ieee80211_reg_rule *rule2,
691 struct ieee80211_reg_rule *intersected_rule)
692{
693 const struct ieee80211_freq_range *freq_range1, *freq_range2;
694 struct ieee80211_freq_range *freq_range;
695 const struct ieee80211_power_rule *power_rule1, *power_rule2;
696 struct ieee80211_power_rule *power_rule;
697 u32 freq_diff;
698
699 freq_range1 = &rule1->freq_range;
700 freq_range2 = &rule2->freq_range;
701 freq_range = &intersected_rule->freq_range;
702
703 power_rule1 = &rule1->power_rule;
704 power_rule2 = &rule2->power_rule;
705 power_rule = &intersected_rule->power_rule;
706
707 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
708 freq_range2->start_freq_khz);
709 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
710 freq_range2->end_freq_khz);
711 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
712 freq_range2->max_bandwidth_khz);
713
714 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
715 if (freq_range->max_bandwidth_khz > freq_diff)
716 freq_range->max_bandwidth_khz = freq_diff;
717
718 power_rule->max_eirp = min(power_rule1->max_eirp,
719 power_rule2->max_eirp);
720 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
721 power_rule2->max_antenna_gain);
722
723 intersected_rule->flags = (rule1->flags | rule2->flags);
724
725 if (!is_valid_reg_rule(intersected_rule))
726 return -EINVAL;
727
728 return 0;
729}
730
731/**
732 * regdom_intersect - do the intersection between two regulatory domains
733 * @rd1: first regulatory domain
734 * @rd2: second regulatory domain
735 *
736 * Use this function to get the intersection between two regulatory domains.
737 * Once completed we will mark the alpha2 for the rd as intersected, "98",
738 * as no one single alpha2 can represent this regulatory domain.
739 *
740 * Returns a pointer to the regulatory domain structure which will hold the
741 * resulting intersection of rules between rd1 and rd2. We will
742 * kzalloc() this structure for you.
743 */
744static struct ieee80211_regdomain *regdom_intersect(
745 const struct ieee80211_regdomain *rd1,
746 const struct ieee80211_regdomain *rd2)
747{
748 int r, size_of_regd;
749 unsigned int x, y;
750 unsigned int num_rules = 0, rule_idx = 0;
751 const struct ieee80211_reg_rule *rule1, *rule2;
752 struct ieee80211_reg_rule *intersected_rule;
753 struct ieee80211_regdomain *rd;
754 /* This is just a dummy holder to help us count */
755 struct ieee80211_reg_rule irule;
756
757 /* Uses the stack temporarily for counter arithmetic */
758 intersected_rule = &irule;
759
760 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
761
762 if (!rd1 || !rd2)
763 return NULL;
764
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500765 /*
766 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700767 * build them. This is to so we can malloc() and free() a
768 * regdomain once. The reason we use reg_rules_intersect() here
769 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500770 * All rules that do check out OK are valid.
771 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700772
773 for (x = 0; x < rd1->n_reg_rules; x++) {
774 rule1 = &rd1->reg_rules[x];
775 for (y = 0; y < rd2->n_reg_rules; y++) {
776 rule2 = &rd2->reg_rules[y];
777 if (!reg_rules_intersect(rule1, rule2,
778 intersected_rule))
779 num_rules++;
780 memset(intersected_rule, 0,
781 sizeof(struct ieee80211_reg_rule));
782 }
783 }
784
785 if (!num_rules)
786 return NULL;
787
788 size_of_regd = sizeof(struct ieee80211_regdomain) +
789 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
790
791 rd = kzalloc(size_of_regd, GFP_KERNEL);
792 if (!rd)
793 return NULL;
794
795 for (x = 0; x < rd1->n_reg_rules; x++) {
796 rule1 = &rd1->reg_rules[x];
797 for (y = 0; y < rd2->n_reg_rules; y++) {
798 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500799 /*
800 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700801 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500802 * a memcpy()
803 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700804 intersected_rule = &rd->reg_rules[rule_idx];
805 r = reg_rules_intersect(rule1, rule2,
806 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500807 /*
808 * No need to memset here the intersected rule here as
809 * we're not using the stack anymore
810 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700811 if (r)
812 continue;
813 rule_idx++;
814 }
815 }
816
817 if (rule_idx != num_rules) {
818 kfree(rd);
819 return NULL;
820 }
821
822 rd->n_reg_rules = num_rules;
823 rd->alpha2[0] = '9';
824 rd->alpha2[1] = '8';
825
826 return rd;
827}
828
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500829/*
830 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
831 * want to just have the channel structure use these
832 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700833static u32 map_regdom_flags(u32 rd_flags)
834{
835 u32 channel_flags = 0;
836 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
837 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
838 if (rd_flags & NL80211_RRF_NO_IBSS)
839 channel_flags |= IEEE80211_CHAN_NO_IBSS;
840 if (rd_flags & NL80211_RRF_DFS)
841 channel_flags |= IEEE80211_CHAN_RADAR;
842 return channel_flags;
843}
844
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800845static int freq_reg_info_regd(struct wiphy *wiphy,
846 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400847 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800848 const struct ieee80211_reg_rule **reg_rule,
849 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100850{
851 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800852 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800853 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400854 bool bw_fits = false;
855
856 if (!desired_bw_khz)
857 desired_bw_khz = MHZ_TO_KHZ(20);
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. Rodriguez038659e2009-05-02 00:37:17 -0400890 bw_fits = reg_does_bw_fit(fr,
891 center_freq,
892 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800893
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400894 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700895 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400896 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100897 }
898 }
899
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800900 if (!band_rule_found)
901 return -ERANGE;
902
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400903 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700904}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800905EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700906
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400907int freq_reg_info(struct wiphy *wiphy,
908 u32 center_freq,
909 u32 desired_bw_khz,
910 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800911{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400912 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400913 return freq_reg_info_regd(wiphy,
914 center_freq,
915 desired_bw_khz,
916 reg_rule,
917 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800918}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700919
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400920/*
921 * Note that right now we assume the desired channel bandwidth
922 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
923 * per channel, the primary and the extension channel). To support
924 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
925 * new ieee80211_channel.target_bw and re run the regulatory check
926 * on the wiphy with the target_bw specified. Then we can simply use
927 * that below for the desired_bw_khz below.
928 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800929static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
930 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700931{
932 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400933 u32 flags, bw_flags = 0;
934 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700935 const struct ieee80211_reg_rule *reg_rule = NULL;
936 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400937 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800938 struct ieee80211_supported_band *sband;
939 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500940 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800941
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500942 assert_cfg80211_lock();
943
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500944 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
945
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800946 sband = wiphy->bands[band];
947 BUG_ON(chan_idx >= sband->n_channels);
948 chan = &sband->channels[chan_idx];
949
950 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700951
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400952 r = freq_reg_info(wiphy,
953 MHZ_TO_KHZ(chan->center_freq),
954 desired_bw_khz,
955 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700956
957 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500958 /*
959 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800960 * with a frequency range on the center_freq's band, since
961 * IEEE-802.11 allows for a country IE to have a subset of the
962 * regulatory information provided in a country we ignore
963 * disabling the channel unless at least one reg rule was
964 * found on the center_freq's band. For details see this
965 * clarification:
966 *
967 * http://tinyurl.com/11d-clarification
968 */
969 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400970 last_request->initiator ==
971 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800972#ifdef CONFIG_CFG80211_REG_DEBUG
973 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
974 "intact on %s - no rule found in band on "
975 "Country IE\n",
976 chan->center_freq, wiphy_name(wiphy));
977#endif
978 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500979 /*
980 * In this case we know the country IE has at least one reg rule
981 * for the band so we respect its band definitions
982 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800983#ifdef CONFIG_CFG80211_REG_DEBUG
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400984 if (last_request->initiator ==
985 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800986 printk(KERN_DEBUG "cfg80211: Disabling "
987 "channel %d MHz on %s due to "
988 "Country IE\n",
989 chan->center_freq, wiphy_name(wiphy));
990#endif
991 flags |= IEEE80211_CHAN_DISABLED;
992 chan->flags = flags;
993 }
Johannes Berg8318d782008-01-24 19:38:38 +0100994 return;
995 }
996
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700997 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400998 freq_range = &reg_rule->freq_range;
999
1000 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1001 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001002
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001003 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001004 request_wiphy && request_wiphy == wiphy &&
1005 request_wiphy->strict_regulatory) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001006 /*
1007 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001008 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001009 * settings
1010 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001011 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001012 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001013 chan->max_antenna_gain = chan->orig_mag =
1014 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001015 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001016 chan->max_power = chan->orig_mpwr =
1017 (int) MBM_TO_DBM(power_rule->max_eirp);
1018 return;
1019 }
1020
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001021 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001022 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001023 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001024 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
John W. Linville253898c2008-04-03 15:32:54 -04001025 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001026 chan->max_power = min(chan->orig_mpwr,
1027 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001028 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001029 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001030}
1031
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001032static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001033{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001034 unsigned int i;
1035 struct ieee80211_supported_band *sband;
1036
1037 BUG_ON(!wiphy->bands[band]);
1038 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001039
1040 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001041 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001042}
1043
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001044static bool ignore_reg_update(struct wiphy *wiphy,
1045 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001046{
1047 if (!last_request)
1048 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001049 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -08001050 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001051 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001052 /*
1053 * wiphy->regd will be set once the device has its own
1054 * desired regulatory domain set
1055 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001056 if (wiphy->strict_regulatory && !wiphy->regd &&
1057 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001058 return true;
1059 return false;
1060}
1061
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001062static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001063{
1064 struct cfg80211_registered_device *drv;
1065
1066 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001067 wiphy_update_regulatory(&drv->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001068}
1069
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001070static void handle_reg_beacon(struct wiphy *wiphy,
1071 unsigned int chan_idx,
1072 struct reg_beacon *reg_beacon)
1073{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001074 struct ieee80211_supported_band *sband;
1075 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001076 bool channel_changed = false;
1077 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001078
1079 assert_cfg80211_lock();
1080
1081 sband = wiphy->bands[reg_beacon->chan.band];
1082 chan = &sband->channels[chan_idx];
1083
1084 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1085 return;
1086
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001087 if (chan->beacon_found)
1088 return;
1089
1090 chan->beacon_found = true;
1091
1092 chan_before.center_freq = chan->center_freq;
1093 chan_before.flags = chan->flags;
1094
Luis R. Rodrigueza4ed90d2009-04-02 14:08:07 -04001095 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
1096 !(chan->orig_flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001097 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001098 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001099 }
1100
Luis R. Rodrigueza4ed90d2009-04-02 14:08:07 -04001101 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
1102 !(chan->orig_flags & IEEE80211_CHAN_NO_IBSS)) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001103 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001104 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001105 }
1106
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001107 if (channel_changed)
1108 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001109}
1110
1111/*
1112 * Called when a scan on a wiphy finds a beacon on
1113 * new channel
1114 */
1115static void wiphy_update_new_beacon(struct wiphy *wiphy,
1116 struct reg_beacon *reg_beacon)
1117{
1118 unsigned int i;
1119 struct ieee80211_supported_band *sband;
1120
1121 assert_cfg80211_lock();
1122
1123 if (!wiphy->bands[reg_beacon->chan.band])
1124 return;
1125
1126 sband = wiphy->bands[reg_beacon->chan.band];
1127
1128 for (i = 0; i < sband->n_channels; i++)
1129 handle_reg_beacon(wiphy, i, reg_beacon);
1130}
1131
1132/*
1133 * Called upon reg changes or a new wiphy is added
1134 */
1135static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1136{
1137 unsigned int i;
1138 struct ieee80211_supported_band *sband;
1139 struct reg_beacon *reg_beacon;
1140
1141 assert_cfg80211_lock();
1142
1143 if (list_empty(&reg_beacon_list))
1144 return;
1145
1146 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1147 if (!wiphy->bands[reg_beacon->chan.band])
1148 continue;
1149 sband = wiphy->bands[reg_beacon->chan.band];
1150 for (i = 0; i < sband->n_channels; i++)
1151 handle_reg_beacon(wiphy, i, reg_beacon);
1152 }
1153}
1154
1155static bool reg_is_world_roaming(struct wiphy *wiphy)
1156{
1157 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1158 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1159 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001160 if (last_request &&
1161 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001162 wiphy->custom_regulatory)
1163 return true;
1164 return false;
1165}
1166
1167/* Reap the advantages of previously found beacons */
1168static void reg_process_beacons(struct wiphy *wiphy)
1169{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001170 /*
1171 * Means we are just firing up cfg80211, so no beacons would
1172 * have been processed yet.
1173 */
1174 if (!last_request)
1175 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001176 if (!reg_is_world_roaming(wiphy))
1177 return;
1178 wiphy_update_beacon_reg(wiphy);
1179}
1180
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001181static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1182{
1183 if (!chan)
1184 return true;
1185 if (chan->flags & IEEE80211_CHAN_DISABLED)
1186 return true;
1187 /* This would happen when regulatory rules disallow HT40 completely */
1188 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1189 return true;
1190 return false;
1191}
1192
1193static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1194 enum ieee80211_band band,
1195 unsigned int chan_idx)
1196{
1197 struct ieee80211_supported_band *sband;
1198 struct ieee80211_channel *channel;
1199 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1200 unsigned int i;
1201
1202 assert_cfg80211_lock();
1203
1204 sband = wiphy->bands[band];
1205 BUG_ON(chan_idx >= sband->n_channels);
1206 channel = &sband->channels[chan_idx];
1207
1208 if (is_ht40_not_allowed(channel)) {
1209 channel->flags |= IEEE80211_CHAN_NO_HT40;
1210 return;
1211 }
1212
1213 /*
1214 * We need to ensure the extension channels exist to
1215 * be able to use HT40- or HT40+, this finds them (or not)
1216 */
1217 for (i = 0; i < sband->n_channels; i++) {
1218 struct ieee80211_channel *c = &sband->channels[i];
1219 if (c->center_freq == (channel->center_freq - 20))
1220 channel_before = c;
1221 if (c->center_freq == (channel->center_freq + 20))
1222 channel_after = c;
1223 }
1224
1225 /*
1226 * Please note that this assumes target bandwidth is 20 MHz,
1227 * if that ever changes we also need to change the below logic
1228 * to include that as well.
1229 */
1230 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001231 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001232 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001233 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001234
1235 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001236 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001237 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001238 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001239}
1240
1241static void reg_process_ht_flags_band(struct wiphy *wiphy,
1242 enum ieee80211_band band)
1243{
1244 unsigned int i;
1245 struct ieee80211_supported_band *sband;
1246
1247 BUG_ON(!wiphy->bands[band]);
1248 sband = wiphy->bands[band];
1249
1250 for (i = 0; i < sband->n_channels; i++)
1251 reg_process_ht_flags_channel(wiphy, band, i);
1252}
1253
1254static void reg_process_ht_flags(struct wiphy *wiphy)
1255{
1256 enum ieee80211_band band;
1257
1258 if (!wiphy)
1259 return;
1260
1261 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1262 if (wiphy->bands[band])
1263 reg_process_ht_flags_band(wiphy, band);
1264 }
1265
1266}
1267
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001268void wiphy_update_regulatory(struct wiphy *wiphy,
1269 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001270{
1271 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001272
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001273 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001274 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001275 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001276 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001277 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001278 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001279out:
1280 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001281 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001282 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001283 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001284}
1285
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001286static void handle_channel_custom(struct wiphy *wiphy,
1287 enum ieee80211_band band,
1288 unsigned int chan_idx,
1289 const struct ieee80211_regdomain *regd)
1290{
1291 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001292 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1293 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001294 const struct ieee80211_reg_rule *reg_rule = NULL;
1295 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001296 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001297 struct ieee80211_supported_band *sband;
1298 struct ieee80211_channel *chan;
1299
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001300 assert_cfg80211_lock();
1301
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001302 sband = wiphy->bands[band];
1303 BUG_ON(chan_idx >= sband->n_channels);
1304 chan = &sband->channels[chan_idx];
1305
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001306 r = freq_reg_info_regd(wiphy,
1307 MHZ_TO_KHZ(chan->center_freq),
1308 desired_bw_khz,
1309 &reg_rule,
1310 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001311
1312 if (r) {
1313 chan->flags = IEEE80211_CHAN_DISABLED;
1314 return;
1315 }
1316
1317 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001318 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001319
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001320 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1321 bw_flags = IEEE80211_CHAN_NO_HT40;
1322
1323 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001324 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001325 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001326 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1327}
1328
1329static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1330 const struct ieee80211_regdomain *regd)
1331{
1332 unsigned int i;
1333 struct ieee80211_supported_band *sband;
1334
1335 BUG_ON(!wiphy->bands[band]);
1336 sband = wiphy->bands[band];
1337
1338 for (i = 0; i < sband->n_channels; i++)
1339 handle_channel_custom(wiphy, band, i, regd);
1340}
1341
1342/* Used by drivers prior to wiphy registration */
1343void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1344 const struct ieee80211_regdomain *regd)
1345{
1346 enum ieee80211_band band;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001347
1348 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001349 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1350 if (wiphy->bands[band])
1351 handle_band_custom(wiphy, band, regd);
1352 }
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001353 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001354}
1355EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1356
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001357static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1358 const struct ieee80211_regdomain *src_regd)
1359{
1360 struct ieee80211_regdomain *regd;
1361 int size_of_regd = 0;
1362 unsigned int i;
1363
1364 size_of_regd = sizeof(struct ieee80211_regdomain) +
1365 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1366
1367 regd = kzalloc(size_of_regd, GFP_KERNEL);
1368 if (!regd)
1369 return -ENOMEM;
1370
1371 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1372
1373 for (i = 0; i < src_regd->n_reg_rules; i++)
1374 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1375 sizeof(struct ieee80211_reg_rule));
1376
1377 *dst_regd = regd;
1378 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001379}
1380
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001381/*
1382 * Return value which can be used by ignore_request() to indicate
1383 * it has been determined we should intersect two regulatory domains
1384 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001385#define REG_INTERSECT 1
1386
Johannes Berg84fa4f42008-10-24 20:32:23 +02001387/* This has the logic which determines when a new request
1388 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001389static int ignore_request(struct wiphy *wiphy,
1390 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001391{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001392 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001393
1394 assert_cfg80211_lock();
1395
Johannes Berg84fa4f42008-10-24 20:32:23 +02001396 /* All initial requests are respected */
1397 if (!last_request)
1398 return 0;
1399
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001400 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001401 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001402 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001403 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001404
1405 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1406
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001407 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001408 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001409 if (last_request->initiator ==
1410 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001411 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001412 /*
1413 * Two cards with two APs claiming different
1414 * different Country IE alpha2s. We could
1415 * intersect them, but that seems unlikely
1416 * to be correct. Reject second one for now.
1417 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001418 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001419 return -EOPNOTSUPP;
1420 return -EALREADY;
1421 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001422 /*
1423 * Two consecutive Country IE hints on the same wiphy.
1424 * This should be picked up early by the driver/stack
1425 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001426 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001427 return 0;
1428 return -EALREADY;
1429 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001430 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001431 case NL80211_REGDOM_SET_BY_DRIVER:
1432 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001433 if (is_old_static_regdom(cfg80211_regdomain))
1434 return 0;
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001435 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001436 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001437 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001438 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001439
1440 /*
1441 * This would happen if you unplug and plug your card
1442 * back in or if you add a new device for which the previously
1443 * loaded card also agrees on the regulatory domain.
1444 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001445 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001446 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001447 return -EALREADY;
1448
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001449 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001450 case NL80211_REGDOM_SET_BY_USER:
1451 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001452 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001453 /*
1454 * If the user knows better the user should set the regdom
1455 * to their country before the IE is picked up
1456 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001457 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001458 last_request->intersect)
1459 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001460 /*
1461 * Process user requests only after previous user/driver/core
1462 * requests have been processed
1463 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001464 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1465 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1466 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001467 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001468 return -EAGAIN;
1469 }
1470
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001471 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001472 !regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001473 return -EALREADY;
1474
Johannes Berg84fa4f42008-10-24 20:32:23 +02001475 return 0;
1476 }
1477
1478 return -EINVAL;
1479}
1480
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001481/**
1482 * __regulatory_hint - hint to the wireless core a regulatory domain
1483 * @wiphy: if the hint comes from country information from an AP, this
1484 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001485 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001486 *
1487 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001488 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001489 *
1490 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1491 * already been set or other standard error codes.
1492 *
1493 * Caller must hold &cfg80211_mutex
1494 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001495static int __regulatory_hint(struct wiphy *wiphy,
1496 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001497{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001498 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001499 int r = 0;
1500
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001501 assert_cfg80211_lock();
1502
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001503 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001504
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001505 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001506 if (pending_request->initiator ==
1507 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001508 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001509 if (r) {
1510 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001511 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001512 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001513 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001514 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001515 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001516 /*
1517 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001518 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001519 * wiphy
1520 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001521 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001522 pending_request->initiator ==
1523 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001524 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001525 if (r) {
1526 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001527 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001528 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001529 r = -EALREADY;
1530 goto new_request;
1531 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001532 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001533 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001534 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001535
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001536new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001537 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001538
1539 last_request = pending_request;
1540 last_request->intersect = intersect;
1541
1542 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001543
1544 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001545 if (r < 0) {
1546 /*
1547 * Since CRDA will not be called in this case as we already
1548 * have applied the requested regulatory domain before we just
1549 * inform userspace we have processed the request
1550 */
1551 if (r == -EALREADY)
1552 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001553 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001554 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001555
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001556 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001557}
1558
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001559/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001560static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001561{
1562 int r = 0;
1563 struct wiphy *wiphy = NULL;
1564
1565 BUG_ON(!reg_request->alpha2);
1566
1567 mutex_lock(&cfg80211_mutex);
1568
1569 if (wiphy_idx_valid(reg_request->wiphy_idx))
1570 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1571
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001572 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001573 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001574 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001575 goto out;
1576 }
1577
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001578 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001579 /* This is required so that the orig_* parameters are saved */
1580 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1581 wiphy_update_regulatory(wiphy, reg_request->initiator);
1582out:
1583 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001584}
1585
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001586/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001587static void reg_process_pending_hints(void)
1588 {
1589 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001590
1591 spin_lock(&reg_requests_lock);
1592 while (!list_empty(&reg_requests_list)) {
1593 reg_request = list_first_entry(&reg_requests_list,
1594 struct regulatory_request,
1595 list);
1596 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001597
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001598 spin_unlock(&reg_requests_lock);
1599 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001600 spin_lock(&reg_requests_lock);
1601 }
1602 spin_unlock(&reg_requests_lock);
1603}
1604
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001605/* Processes beacon hints -- this has nothing to do with country IEs */
1606static void reg_process_pending_beacon_hints(void)
1607{
1608 struct cfg80211_registered_device *drv;
1609 struct reg_beacon *pending_beacon, *tmp;
1610
1611 mutex_lock(&cfg80211_mutex);
1612
1613 /* This goes through the _pending_ beacon list */
1614 spin_lock_bh(&reg_pending_beacons_lock);
1615
1616 if (list_empty(&reg_pending_beacons)) {
1617 spin_unlock_bh(&reg_pending_beacons_lock);
1618 goto out;
1619 }
1620
1621 list_for_each_entry_safe(pending_beacon, tmp,
1622 &reg_pending_beacons, list) {
1623
1624 list_del_init(&pending_beacon->list);
1625
1626 /* Applies the beacon hint to current wiphys */
1627 list_for_each_entry(drv, &cfg80211_drv_list, list)
1628 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1629
1630 /* Remembers the beacon hint for new wiphys or reg changes */
1631 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1632 }
1633
1634 spin_unlock_bh(&reg_pending_beacons_lock);
1635out:
1636 mutex_unlock(&cfg80211_mutex);
1637}
1638
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001639static void reg_todo(struct work_struct *work)
1640{
1641 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001642 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001643}
1644
1645static DECLARE_WORK(reg_work, reg_todo);
1646
1647static void queue_regulatory_request(struct regulatory_request *request)
1648{
1649 spin_lock(&reg_requests_lock);
1650 list_add_tail(&request->list, &reg_requests_list);
1651 spin_unlock(&reg_requests_lock);
1652
1653 schedule_work(&reg_work);
1654}
1655
1656/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001657static int regulatory_hint_core(const char *alpha2)
1658{
1659 struct regulatory_request *request;
1660
1661 BUG_ON(last_request);
1662
1663 request = kzalloc(sizeof(struct regulatory_request),
1664 GFP_KERNEL);
1665 if (!request)
1666 return -ENOMEM;
1667
1668 request->alpha2[0] = alpha2[0];
1669 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001670 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001671
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001672 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001673
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001674 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001675}
1676
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001677/* User hints */
1678int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001679{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001680 struct regulatory_request *request;
1681
Johannes Bergbe3d4812008-10-24 20:32:21 +02001682 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001683
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001684 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1685 if (!request)
1686 return -ENOMEM;
1687
1688 request->wiphy_idx = WIPHY_IDX_STALE;
1689 request->alpha2[0] = alpha2[0];
1690 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001691 request->initiator = NL80211_REGDOM_SET_BY_USER,
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001692
1693 queue_regulatory_request(request);
1694
1695 return 0;
1696}
1697
1698/* Driver hints */
1699int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1700{
1701 struct regulatory_request *request;
1702
1703 BUG_ON(!alpha2);
1704 BUG_ON(!wiphy);
1705
1706 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1707 if (!request)
1708 return -ENOMEM;
1709
1710 request->wiphy_idx = get_wiphy_idx(wiphy);
1711
1712 /* Must have registered wiphy first */
1713 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1714
1715 request->alpha2[0] = alpha2[0];
1716 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001717 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001718
1719 queue_regulatory_request(request);
1720
1721 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001722}
1723EXPORT_SYMBOL(regulatory_hint);
1724
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001725static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1726 u32 country_ie_checksum)
1727{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001728 struct wiphy *request_wiphy;
1729
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001730 assert_cfg80211_lock();
1731
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001732 if (unlikely(last_request->initiator !=
1733 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1734 return false;
1735
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001736 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1737
1738 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001739 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001740
1741 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001742 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001743 /*
1744 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001745 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001746 * on the device
1747 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001748 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1749 return true;
1750 return false;
1751}
1752
1753void regulatory_hint_11d(struct wiphy *wiphy,
1754 u8 *country_ie,
1755 u8 country_ie_len)
1756{
1757 struct ieee80211_regdomain *rd = NULL;
1758 char alpha2[2];
1759 u32 checksum = 0;
1760 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001761 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001762
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001763 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001764
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001765 if (unlikely(!last_request)) {
1766 mutex_unlock(&cfg80211_mutex);
1767 return;
1768 }
1769
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001770 /* IE len must be evenly divisible by 2 */
1771 if (country_ie_len & 0x01)
1772 goto out;
1773
1774 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1775 goto out;
1776
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001777 /*
1778 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001779 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001780 * we were able to process the last regulatory_hint_11d() call
1781 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001782 if (country_ie_regdomain)
1783 goto out;
1784
1785 alpha2[0] = country_ie[0];
1786 alpha2[1] = country_ie[1];
1787
1788 if (country_ie[2] == 'I')
1789 env = ENVIRON_INDOOR;
1790 else if (country_ie[2] == 'O')
1791 env = ENVIRON_OUTDOOR;
1792
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001793 /*
1794 * We will run this for *every* beacon processed for the BSSID, so
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001795 * we optimize an early check to exit out early if we don't have to
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001796 * do anything
1797 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001798 if (likely(last_request->initiator ==
1799 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1800 wiphy_idx_valid(last_request->wiphy_idx))) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001801 struct cfg80211_registered_device *drv_last_ie;
1802
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001803 drv_last_ie =
1804 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001805
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001806 /*
1807 * Lets keep this simple -- we trust the first AP
1808 * after we intersect with CRDA
1809 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001810 if (likely(&drv_last_ie->wiphy == wiphy)) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001811 /*
1812 * Ignore IEs coming in on this wiphy with
1813 * the same alpha2 and environment cap
1814 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001815 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1816 alpha2) &&
1817 env == drv_last_ie->env)) {
1818 goto out;
1819 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001820 /*
1821 * the wiphy moved on to another BSSID or the AP
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001822 * was reconfigured. XXX: We need to deal with the
1823 * case where the user suspends and goes to goes
1824 * to another country, and then gets IEs from an
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001825 * AP with different settings
1826 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001827 goto out;
1828 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001829 /*
1830 * Ignore IEs coming in on two separate wiphys with
1831 * the same alpha2 and environment cap
1832 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001833 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1834 alpha2) &&
1835 env == drv_last_ie->env)) {
1836 goto out;
1837 }
1838 /* We could potentially intersect though */
1839 goto out;
1840 }
1841 }
1842
1843 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1844 if (!rd)
1845 goto out;
1846
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001847 /*
1848 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001849 * the future when we want to add suspend/resume support and having
1850 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001851 * move to another AP. Right now we just trust the first AP.
1852 *
1853 * If we hit this before we add this support we want to be informed of
1854 * it as it would indicate a mistake in the current design
1855 */
1856 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001857 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001858
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001859 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1860 if (!request)
1861 goto free_rd_out;
1862
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001863 /*
1864 * We keep this around for when CRDA comes back with a response so
1865 * we can intersect with that
1866 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001867 country_ie_regdomain = rd;
1868
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001869 request->wiphy_idx = get_wiphy_idx(wiphy);
1870 request->alpha2[0] = rd->alpha2[0];
1871 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001872 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001873 request->country_ie_checksum = checksum;
1874 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001875
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001876 mutex_unlock(&cfg80211_mutex);
1877
1878 queue_regulatory_request(request);
1879
1880 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001881
1882free_rd_out:
1883 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001884out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001885 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001886}
1887EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001888
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001889static bool freq_is_chan_12_13_14(u16 freq)
1890{
1891 if (freq == ieee80211_channel_to_frequency(12) ||
1892 freq == ieee80211_channel_to_frequency(13) ||
1893 freq == ieee80211_channel_to_frequency(14))
1894 return true;
1895 return false;
1896}
1897
1898int regulatory_hint_found_beacon(struct wiphy *wiphy,
1899 struct ieee80211_channel *beacon_chan,
1900 gfp_t gfp)
1901{
1902 struct reg_beacon *reg_beacon;
1903
1904 if (likely((beacon_chan->beacon_found ||
1905 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1906 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1907 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1908 return 0;
1909
1910 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1911 if (!reg_beacon)
1912 return -ENOMEM;
1913
1914#ifdef CONFIG_CFG80211_REG_DEBUG
1915 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1916 "frequency: %d MHz (Ch %d) on %s\n",
1917 beacon_chan->center_freq,
1918 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1919 wiphy_name(wiphy));
1920#endif
1921 memcpy(&reg_beacon->chan, beacon_chan,
1922 sizeof(struct ieee80211_channel));
1923
1924
1925 /*
1926 * Since we can be called from BH or and non-BH context
1927 * we must use spin_lock_bh()
1928 */
1929 spin_lock_bh(&reg_pending_beacons_lock);
1930 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1931 spin_unlock_bh(&reg_pending_beacons_lock);
1932
1933 schedule_work(&reg_work);
1934
1935 return 0;
1936}
1937
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001938static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001939{
1940 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001941 const struct ieee80211_reg_rule *reg_rule = NULL;
1942 const struct ieee80211_freq_range *freq_range = NULL;
1943 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001944
1945 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1946 "(max_antenna_gain, max_eirp)\n");
1947
1948 for (i = 0; i < rd->n_reg_rules; i++) {
1949 reg_rule = &rd->reg_rules[i];
1950 freq_range = &reg_rule->freq_range;
1951 power_rule = &reg_rule->power_rule;
1952
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001953 /*
1954 * There may not be documentation for max antenna gain
1955 * in certain regions
1956 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001957 if (power_rule->max_antenna_gain)
1958 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1959 "(%d mBi, %d mBm)\n",
1960 freq_range->start_freq_khz,
1961 freq_range->end_freq_khz,
1962 freq_range->max_bandwidth_khz,
1963 power_rule->max_antenna_gain,
1964 power_rule->max_eirp);
1965 else
1966 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1967 "(N/A, %d mBm)\n",
1968 freq_range->start_freq_khz,
1969 freq_range->end_freq_khz,
1970 freq_range->max_bandwidth_khz,
1971 power_rule->max_eirp);
1972 }
1973}
1974
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001975static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001976{
1977
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001978 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001979
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001980 if (last_request->initiator ==
1981 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001982 struct cfg80211_registered_device *drv;
1983 drv = cfg80211_drv_by_wiphy_idx(
1984 last_request->wiphy_idx);
1985 if (drv) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001986 printk(KERN_INFO "cfg80211: Current regulatory "
1987 "domain updated by AP to: %c%c\n",
1988 drv->country_ie_alpha2[0],
1989 drv->country_ie_alpha2[1]);
1990 } else
1991 printk(KERN_INFO "cfg80211: Current regulatory "
1992 "domain intersected: \n");
1993 } else
1994 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001995 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001996 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001997 printk(KERN_INFO "cfg80211: World regulatory "
1998 "domain updated:\n");
1999 else {
2000 if (is_unknown_alpha2(rd->alpha2))
2001 printk(KERN_INFO "cfg80211: Regulatory domain "
2002 "changed to driver built-in settings "
2003 "(unknown country)\n");
2004 else
2005 printk(KERN_INFO "cfg80211: Regulatory domain "
2006 "changed to country: %c%c\n",
2007 rd->alpha2[0], rd->alpha2[1]);
2008 }
2009 print_rd_rules(rd);
2010}
2011
Johannes Berg2df78162008-10-28 16:49:41 +01002012static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002013{
2014 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2015 rd->alpha2[0], rd->alpha2[1]);
2016 print_rd_rules(rd);
2017}
2018
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002019#ifdef CONFIG_CFG80211_REG_DEBUG
2020static void reg_country_ie_process_debug(
2021 const struct ieee80211_regdomain *rd,
2022 const struct ieee80211_regdomain *country_ie_regdomain,
2023 const struct ieee80211_regdomain *intersected_rd)
2024{
2025 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2026 print_regdomain_info(country_ie_regdomain);
2027 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2028 print_regdomain_info(rd);
2029 if (intersected_rd) {
2030 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2031 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08002032 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002033 return;
2034 }
2035 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2036}
2037#else
2038static inline void reg_country_ie_process_debug(
2039 const struct ieee80211_regdomain *rd,
2040 const struct ieee80211_regdomain *country_ie_regdomain,
2041 const struct ieee80211_regdomain *intersected_rd)
2042{
2043}
2044#endif
2045
Johannes Bergd2372b32008-10-24 20:32:20 +02002046/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002047static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002048{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002049 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002050 struct cfg80211_registered_device *drv = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002051 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002052 /* Some basic sanity checks first */
2053
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002054 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002055 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002056 return -EINVAL;
2057 update_world_regdomain(rd);
2058 return 0;
2059 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002060
2061 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2062 !is_unknown_alpha2(rd->alpha2))
2063 return -EINVAL;
2064
Johannes Bergf6037d02008-10-21 11:01:33 +02002065 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002066 return -EINVAL;
2067
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002068 /*
2069 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002070 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002071 * and the pending request came in from a country IE
2072 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002073 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002074 /*
2075 * If someone else asked us to change the rd lets only bother
2076 * checking if the alpha2 changes if CRDA was already called
2077 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002078 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05002079 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002080 return -EINVAL;
2081 }
2082
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002083 /*
2084 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002085 * and finally inform them of what we have done, in case they want
2086 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002087 * internal EEPROM data
2088 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002089
Johannes Bergf6037d02008-10-21 11:01:33 +02002090 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002091 return -EINVAL;
2092
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002093 if (!is_valid_rd(rd)) {
2094 printk(KERN_ERR "cfg80211: Invalid "
2095 "regulatory domain detected:\n");
2096 print_regdomain_info(rd);
2097 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002098 }
2099
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002100 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2101
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002102 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002103 int r;
2104
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002105 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002106 reset_regdomains();
2107 cfg80211_regdomain = rd;
2108 return 0;
2109 }
2110
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002111 /*
2112 * For a driver hint, lets copy the regulatory domain the
2113 * driver wanted to the wiphy to deal with conflicts
2114 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002115
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002116 BUG_ON(request_wiphy->regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002117
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002118 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002119 if (r)
2120 return r;
2121
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002122 reset_regdomains();
2123 cfg80211_regdomain = rd;
2124 return 0;
2125 }
2126
2127 /* Intersection requires a bit more work */
2128
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002129 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002130
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002131 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2132 if (!intersected_rd)
2133 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002134
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002135 /*
2136 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002137 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002138 * domain we keep it for its private use
2139 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002140 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002141 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002142 else
2143 kfree(rd);
2144
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002145 rd = NULL;
2146
2147 reset_regdomains();
2148 cfg80211_regdomain = intersected_rd;
2149
2150 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002151 }
2152
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002153 /*
2154 * Country IE requests are handled a bit differently, we intersect
2155 * the country IE rd with what CRDA believes that country should have
2156 */
2157
2158 BUG_ON(!country_ie_regdomain);
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002159 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002160
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002161 /*
2162 * Intersect what CRDA returned and our what we
2163 * had built from the Country IE received
2164 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002165
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002166 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002167
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002168 reg_country_ie_process_debug(rd,
2169 country_ie_regdomain,
2170 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002171
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002172 kfree(country_ie_regdomain);
2173 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002174
2175 if (!intersected_rd)
2176 return -EINVAL;
2177
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002178 drv = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002179
2180 drv->country_ie_alpha2[0] = rd->alpha2[0];
2181 drv->country_ie_alpha2[1] = rd->alpha2[1];
2182 drv->env = last_request->country_ie_env;
2183
2184 BUG_ON(intersected_rd == rd);
2185
2186 kfree(rd);
2187 rd = NULL;
2188
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002189 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002190 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002191
2192 return 0;
2193}
2194
2195
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002196/*
2197 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002198 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002199 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2200 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002201int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002202{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002203 int r;
2204
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002205 assert_cfg80211_lock();
2206
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002207 /* Note that this doesn't update the wiphys, this is done below */
2208 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002209 if (r) {
2210 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002211 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002212 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002213
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002214 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002215 if (!last_request->intersect)
2216 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002217
2218 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002219 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002220
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002221 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002222
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002223 nl80211_send_reg_change_event(last_request);
2224
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002225 return r;
2226}
2227
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002228/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002229void reg_device_remove(struct wiphy *wiphy)
2230{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002231 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002232
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002233 assert_cfg80211_lock();
2234
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002235 kfree(wiphy->regd);
2236
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002237 if (last_request)
2238 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002239
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002240 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002241 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002242
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002243 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002244 last_request->country_ie_env = ENVIRON_ANY;
2245}
2246
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002247int regulatory_init(void)
2248{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002249 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002250
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002251 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2252 if (IS_ERR(reg_pdev))
2253 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002254
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002255 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002256 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002257
Johannes Berg734366d2008-09-15 10:56:48 +02002258#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002259 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002260
Johannes Berg942b25c2008-09-15 11:26:47 +02002261 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02002262 print_regdomain_info(cfg80211_regdomain);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002263 /*
2264 * The old code still requests for a new regdomain and if
Johannes Berg734366d2008-09-15 10:56:48 +02002265 * you have CRDA you get it updated, otherwise you get
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002266 * stuck with the static values. Since "EU" is not a valid
2267 * ISO / IEC 3166 alpha2 code we can't expect userpace to
2268 * give us a regulatory domain for it. We need last_request
2269 * iniitalized though so lets just send a request which we
2270 * know will be ignored... this crap will be removed once
2271 * OLD_REG dies.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002272 */
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002273 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002274#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002275 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002276
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -04002277 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002278#endif
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002279 if (err) {
2280 if (err == -ENOMEM)
2281 return err;
2282 /*
2283 * N.B. kobject_uevent_env() can fail mainly for when we're out
2284 * memory which is handled and propagated appropriately above
2285 * but it can also fail during a netlink_broadcast() or during
2286 * early boot for call_usermodehelper(). For now treat these
2287 * errors as non-fatal.
2288 */
2289 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2290 "to call CRDA during init");
2291#ifdef CONFIG_CFG80211_REG_DEBUG
2292 /* We want to find out exactly why when debugging */
2293 WARN_ON(err);
2294#endif
2295 }
Johannes Berg734366d2008-09-15 10:56:48 +02002296
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002297 return 0;
2298}
2299
2300void regulatory_exit(void)
2301{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002302 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002303 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002304
2305 cancel_work_sync(&reg_work);
2306
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002307 mutex_lock(&cfg80211_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002308
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002309 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002310
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002311 kfree(country_ie_regdomain);
2312 country_ie_regdomain = NULL;
2313
Johannes Bergf6037d02008-10-21 11:01:33 +02002314 kfree(last_request);
2315
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002316 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002317
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002318 spin_lock_bh(&reg_pending_beacons_lock);
2319 if (!list_empty(&reg_pending_beacons)) {
2320 list_for_each_entry_safe(reg_beacon, btmp,
2321 &reg_pending_beacons, list) {
2322 list_del(&reg_beacon->list);
2323 kfree(reg_beacon);
2324 }
2325 }
2326 spin_unlock_bh(&reg_pending_beacons_lock);
2327
2328 if (!list_empty(&reg_beacon_list)) {
2329 list_for_each_entry_safe(reg_beacon, btmp,
2330 &reg_beacon_list, list) {
2331 list_del(&reg_beacon->list);
2332 kfree(reg_beacon);
2333 }
2334 }
2335
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002336 spin_lock(&reg_requests_lock);
2337 if (!list_empty(&reg_requests_list)) {
2338 list_for_each_entry_safe(reg_request, tmp,
2339 &reg_requests_list, list) {
2340 list_del(&reg_request->list);
2341 kfree(reg_request);
2342 }
2343 }
2344 spin_unlock(&reg_requests_lock);
2345
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002346 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002347}