blob: d897528ee7fcc617d82064a4f609832fe837c7a9 [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{
Johannes Bergf6037d02008-10-21 11:01:33 +0200385 if (!last_request)
386 return false;
387
388 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700389}
390
391/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200392static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700393{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200394 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700395 u32 freq_diff;
396
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800397 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700398 return false;
399
400 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
401 return false;
402
403 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
404
Roel Kluinbd05f282009-03-03 22:55:21 +0100405 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
406 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700407 return false;
408
409 return true;
410}
411
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200412static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700413{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200414 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700415 unsigned int i;
416
417 if (!rd->n_reg_rules)
418 return false;
419
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800420 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
421 return false;
422
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700423 for (i = 0; i < rd->n_reg_rules; i++) {
424 reg_rule = &rd->reg_rules[i];
425 if (!is_valid_reg_rule(reg_rule))
426 return false;
427 }
428
429 return true;
430}
431
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400432static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
433 u32 center_freq_khz,
434 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700435{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400436 u32 start_freq_khz, end_freq_khz;
437
438 start_freq_khz = center_freq_khz - (bw_khz/2);
439 end_freq_khz = center_freq_khz + (bw_khz/2);
440
441 if (start_freq_khz >= freq_range->start_freq_khz &&
442 end_freq_khz <= freq_range->end_freq_khz)
443 return true;
444
445 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700446}
447
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800448/**
449 * freq_in_rule_band - tells us if a frequency is in a frequency band
450 * @freq_range: frequency rule we want to query
451 * @freq_khz: frequency we are inquiring about
452 *
453 * This lets us know if a specific frequency rule is or is not relevant to
454 * a specific frequency's band. Bands are device specific and artificial
455 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
456 * safe for now to assume that a frequency rule should not be part of a
457 * frequency's band if the start freq or end freq are off by more than 2 GHz.
458 * This resolution can be lowered and should be considered as we add
459 * regulatory rule support for other "bands".
460 **/
461static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
462 u32 freq_khz)
463{
464#define ONE_GHZ_IN_KHZ 1000000
465 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
466 return true;
467 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
468 return true;
469 return false;
470#undef ONE_GHZ_IN_KHZ
471}
472
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500473/*
474 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800475 * structure has a lot of information which the IE doesn't yet have,
476 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500477 * with our userspace regulatory agent to get lower bounds.
478 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800479static struct ieee80211_regdomain *country_ie_2_rd(
480 u8 *country_ie,
481 u8 country_ie_len,
482 u32 *checksum)
483{
484 struct ieee80211_regdomain *rd = NULL;
485 unsigned int i = 0;
486 char alpha2[2];
487 u32 flags = 0;
488 u32 num_rules = 0, size_of_regd = 0;
489 u8 *triplets_start = NULL;
490 u8 len_at_triplet = 0;
491 /* the last channel we have registered in a subband (triplet) */
492 int last_sub_max_channel = 0;
493
494 *checksum = 0xDEADBEEF;
495
496 /* Country IE requirements */
497 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
498 country_ie_len & 0x01);
499
500 alpha2[0] = country_ie[0];
501 alpha2[1] = country_ie[1];
502
503 /*
504 * Third octet can be:
505 * 'I' - Indoor
506 * 'O' - Outdoor
507 *
508 * anything else we assume is no restrictions
509 */
510 if (country_ie[2] == 'I')
511 flags = NL80211_RRF_NO_OUTDOOR;
512 else if (country_ie[2] == 'O')
513 flags = NL80211_RRF_NO_INDOOR;
514
515 country_ie += 3;
516 country_ie_len -= 3;
517
518 triplets_start = country_ie;
519 len_at_triplet = country_ie_len;
520
521 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
522
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500523 /*
524 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800525 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500526 * for each channel subband
527 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800528 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800529 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800530 struct ieee80211_country_ie_triplet *triplet =
531 (struct ieee80211_country_ie_triplet *) country_ie;
532 int cur_sub_max_channel = 0, cur_channel = 0;
533
534 if (triplet->ext.reg_extension_id >=
535 IEEE80211_COUNTRY_EXTENSION_ID) {
536 country_ie += 3;
537 country_ie_len -= 3;
538 continue;
539 }
540
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800541 /* 2 GHz */
542 if (triplet->chans.first_channel <= 14)
543 end_channel = triplet->chans.first_channel +
544 triplet->chans.num_channels;
545 else
546 /*
547 * 5 GHz -- For example in country IEs if the first
548 * channel given is 36 and the number of channels is 4
549 * then the individual channel numbers defined for the
550 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
551 * and not 36, 37, 38, 39.
552 *
553 * See: http://tinyurl.com/11d-clarification
554 */
555 end_channel = triplet->chans.first_channel +
556 (4 * (triplet->chans.num_channels - 1));
557
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800558 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800559 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800560
561 /* Basic sanity check */
562 if (cur_sub_max_channel < cur_channel)
563 return NULL;
564
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500565 /*
566 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800567 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500568 * increasing
569 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800570 if (last_sub_max_channel) {
571 if (cur_channel <= last_sub_max_channel)
572 return NULL;
573 if (cur_sub_max_channel <= last_sub_max_channel)
574 return NULL;
575 }
576
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500577 /*
578 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800579 * we can throw ext triplets as part of this soup,
580 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500581 * don't support them
582 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800583 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
584 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
585 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
586
587 last_sub_max_channel = cur_sub_max_channel;
588
589 country_ie += 3;
590 country_ie_len -= 3;
591 num_rules++;
592
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500593 /*
594 * Note: this is not a IEEE requirement but
595 * simply a memory requirement
596 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800597 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
598 return NULL;
599 }
600
601 country_ie = triplets_start;
602 country_ie_len = len_at_triplet;
603
604 size_of_regd = sizeof(struct ieee80211_regdomain) +
605 (num_rules * sizeof(struct ieee80211_reg_rule));
606
607 rd = kzalloc(size_of_regd, GFP_KERNEL);
608 if (!rd)
609 return NULL;
610
611 rd->n_reg_rules = num_rules;
612 rd->alpha2[0] = alpha2[0];
613 rd->alpha2[1] = alpha2[1];
614
615 /* This time around we fill in the rd */
616 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800617 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800618 struct ieee80211_country_ie_triplet *triplet =
619 (struct ieee80211_country_ie_triplet *) country_ie;
620 struct ieee80211_reg_rule *reg_rule = NULL;
621 struct ieee80211_freq_range *freq_range = NULL;
622 struct ieee80211_power_rule *power_rule = NULL;
623
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500624 /*
625 * Must parse if dot11RegulatoryClassesRequired is true,
626 * we don't support this yet
627 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800628 if (triplet->ext.reg_extension_id >=
629 IEEE80211_COUNTRY_EXTENSION_ID) {
630 country_ie += 3;
631 country_ie_len -= 3;
632 continue;
633 }
634
635 reg_rule = &rd->reg_rules[i];
636 freq_range = &reg_rule->freq_range;
637 power_rule = &reg_rule->power_rule;
638
639 reg_rule->flags = flags;
640
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800641 /* 2 GHz */
642 if (triplet->chans.first_channel <= 14)
643 end_channel = triplet->chans.first_channel +
644 triplet->chans.num_channels;
645 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800646 end_channel = triplet->chans.first_channel +
647 (4 * (triplet->chans.num_channels - 1));
648
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500649 /*
650 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800651 * the actual band edge, not the center of freq for
652 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500653 * the channels passed
654 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800655 freq_range->start_freq_khz =
656 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
657 triplet->chans.first_channel) - 10);
658 freq_range->end_freq_khz =
659 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800660 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800661
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500662 /*
663 * These are large arbitrary values we use to intersect later.
664 * Increment this if we ever support >= 40 MHz channels
665 * in IEEE 802.11
666 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800667 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
668 power_rule->max_antenna_gain = DBI_TO_MBI(100);
669 power_rule->max_eirp = DBM_TO_MBM(100);
670
671 country_ie += 3;
672 country_ie_len -= 3;
673 i++;
674
675 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
676 }
677
678 return rd;
679}
680
681
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500682/*
683 * Helper for regdom_intersect(), this does the real
684 * mathematical intersection fun
685 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700686static int reg_rules_intersect(
687 const struct ieee80211_reg_rule *rule1,
688 const struct ieee80211_reg_rule *rule2,
689 struct ieee80211_reg_rule *intersected_rule)
690{
691 const struct ieee80211_freq_range *freq_range1, *freq_range2;
692 struct ieee80211_freq_range *freq_range;
693 const struct ieee80211_power_rule *power_rule1, *power_rule2;
694 struct ieee80211_power_rule *power_rule;
695 u32 freq_diff;
696
697 freq_range1 = &rule1->freq_range;
698 freq_range2 = &rule2->freq_range;
699 freq_range = &intersected_rule->freq_range;
700
701 power_rule1 = &rule1->power_rule;
702 power_rule2 = &rule2->power_rule;
703 power_rule = &intersected_rule->power_rule;
704
705 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
706 freq_range2->start_freq_khz);
707 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
708 freq_range2->end_freq_khz);
709 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
710 freq_range2->max_bandwidth_khz);
711
712 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
713 if (freq_range->max_bandwidth_khz > freq_diff)
714 freq_range->max_bandwidth_khz = freq_diff;
715
716 power_rule->max_eirp = min(power_rule1->max_eirp,
717 power_rule2->max_eirp);
718 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
719 power_rule2->max_antenna_gain);
720
721 intersected_rule->flags = (rule1->flags | rule2->flags);
722
723 if (!is_valid_reg_rule(intersected_rule))
724 return -EINVAL;
725
726 return 0;
727}
728
729/**
730 * regdom_intersect - do the intersection between two regulatory domains
731 * @rd1: first regulatory domain
732 * @rd2: second regulatory domain
733 *
734 * Use this function to get the intersection between two regulatory domains.
735 * Once completed we will mark the alpha2 for the rd as intersected, "98",
736 * as no one single alpha2 can represent this regulatory domain.
737 *
738 * Returns a pointer to the regulatory domain structure which will hold the
739 * resulting intersection of rules between rd1 and rd2. We will
740 * kzalloc() this structure for you.
741 */
742static struct ieee80211_regdomain *regdom_intersect(
743 const struct ieee80211_regdomain *rd1,
744 const struct ieee80211_regdomain *rd2)
745{
746 int r, size_of_regd;
747 unsigned int x, y;
748 unsigned int num_rules = 0, rule_idx = 0;
749 const struct ieee80211_reg_rule *rule1, *rule2;
750 struct ieee80211_reg_rule *intersected_rule;
751 struct ieee80211_regdomain *rd;
752 /* This is just a dummy holder to help us count */
753 struct ieee80211_reg_rule irule;
754
755 /* Uses the stack temporarily for counter arithmetic */
756 intersected_rule = &irule;
757
758 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
759
760 if (!rd1 || !rd2)
761 return NULL;
762
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500763 /*
764 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700765 * build them. This is to so we can malloc() and free() a
766 * regdomain once. The reason we use reg_rules_intersect() here
767 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500768 * All rules that do check out OK are valid.
769 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700770
771 for (x = 0; x < rd1->n_reg_rules; x++) {
772 rule1 = &rd1->reg_rules[x];
773 for (y = 0; y < rd2->n_reg_rules; y++) {
774 rule2 = &rd2->reg_rules[y];
775 if (!reg_rules_intersect(rule1, rule2,
776 intersected_rule))
777 num_rules++;
778 memset(intersected_rule, 0,
779 sizeof(struct ieee80211_reg_rule));
780 }
781 }
782
783 if (!num_rules)
784 return NULL;
785
786 size_of_regd = sizeof(struct ieee80211_regdomain) +
787 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
788
789 rd = kzalloc(size_of_regd, GFP_KERNEL);
790 if (!rd)
791 return NULL;
792
793 for (x = 0; x < rd1->n_reg_rules; x++) {
794 rule1 = &rd1->reg_rules[x];
795 for (y = 0; y < rd2->n_reg_rules; y++) {
796 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500797 /*
798 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700799 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500800 * a memcpy()
801 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700802 intersected_rule = &rd->reg_rules[rule_idx];
803 r = reg_rules_intersect(rule1, rule2,
804 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500805 /*
806 * No need to memset here the intersected rule here as
807 * we're not using the stack anymore
808 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700809 if (r)
810 continue;
811 rule_idx++;
812 }
813 }
814
815 if (rule_idx != num_rules) {
816 kfree(rd);
817 return NULL;
818 }
819
820 rd->n_reg_rules = num_rules;
821 rd->alpha2[0] = '9';
822 rd->alpha2[1] = '8';
823
824 return rd;
825}
826
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500827/*
828 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
829 * want to just have the channel structure use these
830 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700831static u32 map_regdom_flags(u32 rd_flags)
832{
833 u32 channel_flags = 0;
834 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
835 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
836 if (rd_flags & NL80211_RRF_NO_IBSS)
837 channel_flags |= IEEE80211_CHAN_NO_IBSS;
838 if (rd_flags & NL80211_RRF_DFS)
839 channel_flags |= IEEE80211_CHAN_RADAR;
840 return channel_flags;
841}
842
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800843static int freq_reg_info_regd(struct wiphy *wiphy,
844 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400845 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800846 const struct ieee80211_reg_rule **reg_rule,
847 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100848{
849 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800850 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800851 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400852 bool bw_fits = false;
853
854 if (!desired_bw_khz)
855 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +0100856
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800857 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800858
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500859 /*
860 * Follow the driver's regulatory domain, if present, unless a country
861 * IE has been processed or a user wants to help complaince further
862 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400863 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
864 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800865 wiphy->regd)
866 regd = wiphy->regd;
867
868 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700869 return -EINVAL;
870
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800871 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700872 const struct ieee80211_reg_rule *rr;
873 const struct ieee80211_freq_range *fr = NULL;
874 const struct ieee80211_power_rule *pr = NULL;
875
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800876 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700877 fr = &rr->freq_range;
878 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800879
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500880 /*
881 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800882 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500883 * not overwrite it once found
884 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800885 if (!band_rule_found)
886 band_rule_found = freq_in_rule_band(fr, center_freq);
887
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400888 bw_fits = reg_does_bw_fit(fr,
889 center_freq,
890 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800891
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400892 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700893 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400894 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100895 }
896 }
897
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800898 if (!band_rule_found)
899 return -ERANGE;
900
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400901 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700902}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800903EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700904
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400905int freq_reg_info(struct wiphy *wiphy,
906 u32 center_freq,
907 u32 desired_bw_khz,
908 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800909{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400910 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400911 return freq_reg_info_regd(wiphy,
912 center_freq,
913 desired_bw_khz,
914 reg_rule,
915 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800916}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700917
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400918/*
919 * Note that right now we assume the desired channel bandwidth
920 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
921 * per channel, the primary and the extension channel). To support
922 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
923 * new ieee80211_channel.target_bw and re run the regulatory check
924 * on the wiphy with the target_bw specified. Then we can simply use
925 * that below for the desired_bw_khz below.
926 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800927static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
928 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700929{
930 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400931 u32 flags, bw_flags = 0;
932 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700933 const struct ieee80211_reg_rule *reg_rule = NULL;
934 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400935 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800936 struct ieee80211_supported_band *sband;
937 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500938 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800939
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500940 assert_cfg80211_lock();
941
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500942 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
943
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800944 sband = wiphy->bands[band];
945 BUG_ON(chan_idx >= sband->n_channels);
946 chan = &sband->channels[chan_idx];
947
948 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700949
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400950 r = freq_reg_info(wiphy,
951 MHZ_TO_KHZ(chan->center_freq),
952 desired_bw_khz,
953 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700954
955 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500956 /*
957 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800958 * with a frequency range on the center_freq's band, since
959 * IEEE-802.11 allows for a country IE to have a subset of the
960 * regulatory information provided in a country we ignore
961 * disabling the channel unless at least one reg rule was
962 * found on the center_freq's band. For details see this
963 * clarification:
964 *
965 * http://tinyurl.com/11d-clarification
966 */
967 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400968 last_request->initiator ==
969 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800970#ifdef CONFIG_CFG80211_REG_DEBUG
971 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
972 "intact on %s - no rule found in band on "
973 "Country IE\n",
974 chan->center_freq, wiphy_name(wiphy));
975#endif
976 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500977 /*
978 * In this case we know the country IE has at least one reg rule
979 * for the band so we respect its band definitions
980 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800981#ifdef CONFIG_CFG80211_REG_DEBUG
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400982 if (last_request->initiator ==
983 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800984 printk(KERN_DEBUG "cfg80211: Disabling "
985 "channel %d MHz on %s due to "
986 "Country IE\n",
987 chan->center_freq, wiphy_name(wiphy));
988#endif
989 flags |= IEEE80211_CHAN_DISABLED;
990 chan->flags = flags;
991 }
Johannes Berg8318d782008-01-24 19:38:38 +0100992 return;
993 }
994
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700995 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400996 freq_range = &reg_rule->freq_range;
997
998 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
999 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001000
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001001 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001002 request_wiphy && request_wiphy == wiphy &&
1003 request_wiphy->strict_regulatory) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001004 /*
1005 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001006 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001007 * settings
1008 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001009 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001010 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001011 chan->max_antenna_gain = chan->orig_mag =
1012 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001013 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001014 chan->max_power = chan->orig_mpwr =
1015 (int) MBM_TO_DBM(power_rule->max_eirp);
1016 return;
1017 }
1018
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001019 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001020 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001021 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001022 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
John W. Linville253898c2008-04-03 15:32:54 -04001023 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001024 chan->max_power = min(chan->orig_mpwr,
1025 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001026 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001027 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001028}
1029
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001030static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001031{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001032 unsigned int i;
1033 struct ieee80211_supported_band *sband;
1034
1035 BUG_ON(!wiphy->bands[band]);
1036 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001037
1038 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001039 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001040}
1041
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001042static bool ignore_reg_update(struct wiphy *wiphy,
1043 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001044{
1045 if (!last_request)
1046 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001047 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -08001048 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001049 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001050 /*
1051 * wiphy->regd will be set once the device has its own
1052 * desired regulatory domain set
1053 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001054 if (wiphy->strict_regulatory && !wiphy->regd &&
1055 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001056 return true;
1057 return false;
1058}
1059
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001060static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001061{
1062 struct cfg80211_registered_device *drv;
1063
1064 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001065 wiphy_update_regulatory(&drv->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001066}
1067
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001068static void handle_reg_beacon(struct wiphy *wiphy,
1069 unsigned int chan_idx,
1070 struct reg_beacon *reg_beacon)
1071{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001072 struct ieee80211_supported_band *sband;
1073 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001074 bool channel_changed = false;
1075 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001076
1077 assert_cfg80211_lock();
1078
1079 sband = wiphy->bands[reg_beacon->chan.band];
1080 chan = &sband->channels[chan_idx];
1081
1082 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1083 return;
1084
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001085 if (chan->beacon_found)
1086 return;
1087
1088 chan->beacon_found = true;
1089
1090 chan_before.center_freq = chan->center_freq;
1091 chan_before.flags = chan->flags;
1092
Luis R. Rodrigueza4ed90d2009-04-02 14:08:07 -04001093 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
1094 !(chan->orig_flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001095 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001096 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001097 }
1098
Luis R. Rodrigueza4ed90d2009-04-02 14:08:07 -04001099 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
1100 !(chan->orig_flags & IEEE80211_CHAN_NO_IBSS)) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001101 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001102 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001103 }
1104
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001105 if (channel_changed)
1106 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001107}
1108
1109/*
1110 * Called when a scan on a wiphy finds a beacon on
1111 * new channel
1112 */
1113static void wiphy_update_new_beacon(struct wiphy *wiphy,
1114 struct reg_beacon *reg_beacon)
1115{
1116 unsigned int i;
1117 struct ieee80211_supported_band *sband;
1118
1119 assert_cfg80211_lock();
1120
1121 if (!wiphy->bands[reg_beacon->chan.band])
1122 return;
1123
1124 sband = wiphy->bands[reg_beacon->chan.band];
1125
1126 for (i = 0; i < sband->n_channels; i++)
1127 handle_reg_beacon(wiphy, i, reg_beacon);
1128}
1129
1130/*
1131 * Called upon reg changes or a new wiphy is added
1132 */
1133static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1134{
1135 unsigned int i;
1136 struct ieee80211_supported_band *sband;
1137 struct reg_beacon *reg_beacon;
1138
1139 assert_cfg80211_lock();
1140
1141 if (list_empty(&reg_beacon_list))
1142 return;
1143
1144 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1145 if (!wiphy->bands[reg_beacon->chan.band])
1146 continue;
1147 sband = wiphy->bands[reg_beacon->chan.band];
1148 for (i = 0; i < sband->n_channels; i++)
1149 handle_reg_beacon(wiphy, i, reg_beacon);
1150 }
1151}
1152
1153static bool reg_is_world_roaming(struct wiphy *wiphy)
1154{
1155 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1156 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1157 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001158 if (last_request &&
1159 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001160 wiphy->custom_regulatory)
1161 return true;
1162 return false;
1163}
1164
1165/* Reap the advantages of previously found beacons */
1166static void reg_process_beacons(struct wiphy *wiphy)
1167{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001168 /*
1169 * Means we are just firing up cfg80211, so no beacons would
1170 * have been processed yet.
1171 */
1172 if (!last_request)
1173 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001174 if (!reg_is_world_roaming(wiphy))
1175 return;
1176 wiphy_update_beacon_reg(wiphy);
1177}
1178
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001179static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1180{
1181 if (!chan)
1182 return true;
1183 if (chan->flags & IEEE80211_CHAN_DISABLED)
1184 return true;
1185 /* This would happen when regulatory rules disallow HT40 completely */
1186 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1187 return true;
1188 return false;
1189}
1190
1191static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1192 enum ieee80211_band band,
1193 unsigned int chan_idx)
1194{
1195 struct ieee80211_supported_band *sband;
1196 struct ieee80211_channel *channel;
1197 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1198 unsigned int i;
1199
1200 assert_cfg80211_lock();
1201
1202 sband = wiphy->bands[band];
1203 BUG_ON(chan_idx >= sband->n_channels);
1204 channel = &sband->channels[chan_idx];
1205
1206 if (is_ht40_not_allowed(channel)) {
1207 channel->flags |= IEEE80211_CHAN_NO_HT40;
1208 return;
1209 }
1210
1211 /*
1212 * We need to ensure the extension channels exist to
1213 * be able to use HT40- or HT40+, this finds them (or not)
1214 */
1215 for (i = 0; i < sband->n_channels; i++) {
1216 struct ieee80211_channel *c = &sband->channels[i];
1217 if (c->center_freq == (channel->center_freq - 20))
1218 channel_before = c;
1219 if (c->center_freq == (channel->center_freq + 20))
1220 channel_after = c;
1221 }
1222
1223 /*
1224 * Please note that this assumes target bandwidth is 20 MHz,
1225 * if that ever changes we also need to change the below logic
1226 * to include that as well.
1227 */
1228 if (is_ht40_not_allowed(channel_before))
1229 channel->flags |= IEEE80211_CHAN_NO_FAT_BELOW;
1230 else
1231 channel->flags &= ~IEEE80211_CHAN_NO_FAT_BELOW;
1232
1233 if (is_ht40_not_allowed(channel_after))
1234 channel->flags |= IEEE80211_CHAN_NO_FAT_ABOVE;
1235 else
1236 channel->flags &= ~IEEE80211_CHAN_NO_FAT_ABOVE;
1237}
1238
1239static void reg_process_ht_flags_band(struct wiphy *wiphy,
1240 enum ieee80211_band band)
1241{
1242 unsigned int i;
1243 struct ieee80211_supported_band *sband;
1244
1245 BUG_ON(!wiphy->bands[band]);
1246 sband = wiphy->bands[band];
1247
1248 for (i = 0; i < sband->n_channels; i++)
1249 reg_process_ht_flags_channel(wiphy, band, i);
1250}
1251
1252static void reg_process_ht_flags(struct wiphy *wiphy)
1253{
1254 enum ieee80211_band band;
1255
1256 if (!wiphy)
1257 return;
1258
1259 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1260 if (wiphy->bands[band])
1261 reg_process_ht_flags_band(wiphy, band);
1262 }
1263
1264}
1265
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001266void wiphy_update_regulatory(struct wiphy *wiphy,
1267 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001268{
1269 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001270
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001271 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001272 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001273 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001274 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001275 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001276 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001277out:
1278 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001279 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001280 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001281 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001282}
1283
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001284static void handle_channel_custom(struct wiphy *wiphy,
1285 enum ieee80211_band band,
1286 unsigned int chan_idx,
1287 const struct ieee80211_regdomain *regd)
1288{
1289 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001290 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1291 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001292 const struct ieee80211_reg_rule *reg_rule = NULL;
1293 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001294 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001295 struct ieee80211_supported_band *sband;
1296 struct ieee80211_channel *chan;
1297
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001298 assert_cfg80211_lock();
1299
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001300 sband = wiphy->bands[band];
1301 BUG_ON(chan_idx >= sband->n_channels);
1302 chan = &sband->channels[chan_idx];
1303
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001304 r = freq_reg_info_regd(wiphy,
1305 MHZ_TO_KHZ(chan->center_freq),
1306 desired_bw_khz,
1307 &reg_rule,
1308 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001309
1310 if (r) {
1311 chan->flags = IEEE80211_CHAN_DISABLED;
1312 return;
1313 }
1314
1315 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001316 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001317
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001318 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1319 bw_flags = IEEE80211_CHAN_NO_HT40;
1320
1321 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001322 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001323 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001324 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1325}
1326
1327static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1328 const struct ieee80211_regdomain *regd)
1329{
1330 unsigned int i;
1331 struct ieee80211_supported_band *sband;
1332
1333 BUG_ON(!wiphy->bands[band]);
1334 sband = wiphy->bands[band];
1335
1336 for (i = 0; i < sband->n_channels; i++)
1337 handle_channel_custom(wiphy, band, i, regd);
1338}
1339
1340/* Used by drivers prior to wiphy registration */
1341void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1342 const struct ieee80211_regdomain *regd)
1343{
1344 enum ieee80211_band band;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001345
1346 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001347 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1348 if (wiphy->bands[band])
1349 handle_band_custom(wiphy, band, regd);
1350 }
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001351 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001352}
1353EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1354
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001355static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1356 const struct ieee80211_regdomain *src_regd)
1357{
1358 struct ieee80211_regdomain *regd;
1359 int size_of_regd = 0;
1360 unsigned int i;
1361
1362 size_of_regd = sizeof(struct ieee80211_regdomain) +
1363 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1364
1365 regd = kzalloc(size_of_regd, GFP_KERNEL);
1366 if (!regd)
1367 return -ENOMEM;
1368
1369 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1370
1371 for (i = 0; i < src_regd->n_reg_rules; i++)
1372 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1373 sizeof(struct ieee80211_reg_rule));
1374
1375 *dst_regd = regd;
1376 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001377}
1378
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001379/*
1380 * Return value which can be used by ignore_request() to indicate
1381 * it has been determined we should intersect two regulatory domains
1382 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001383#define REG_INTERSECT 1
1384
Johannes Berg84fa4f42008-10-24 20:32:23 +02001385/* This has the logic which determines when a new request
1386 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001387static int ignore_request(struct wiphy *wiphy,
1388 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001389{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001390 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001391
1392 assert_cfg80211_lock();
1393
Johannes Berg84fa4f42008-10-24 20:32:23 +02001394 /* All initial requests are respected */
1395 if (!last_request)
1396 return 0;
1397
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001398 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001399 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001400 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001401 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001402
1403 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1404
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001405 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001406 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001407 if (last_request->initiator ==
1408 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001409 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001410 /*
1411 * Two cards with two APs claiming different
1412 * different Country IE alpha2s. We could
1413 * intersect them, but that seems unlikely
1414 * to be correct. Reject second one for now.
1415 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001416 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001417 return -EOPNOTSUPP;
1418 return -EALREADY;
1419 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001420 /*
1421 * Two consecutive Country IE hints on the same wiphy.
1422 * This should be picked up early by the driver/stack
1423 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001424 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001425 return 0;
1426 return -EALREADY;
1427 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001428 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001429 case NL80211_REGDOM_SET_BY_DRIVER:
1430 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001431 if (is_old_static_regdom(cfg80211_regdomain))
1432 return 0;
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001433 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001434 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001435 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001436 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001437
1438 /*
1439 * This would happen if you unplug and plug your card
1440 * back in or if you add a new device for which the previously
1441 * loaded card also agrees on the regulatory domain.
1442 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001443 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001444 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001445 return -EALREADY;
1446
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001447 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001448 case NL80211_REGDOM_SET_BY_USER:
1449 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001450 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001451 /*
1452 * If the user knows better the user should set the regdom
1453 * to their country before the IE is picked up
1454 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001455 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001456 last_request->intersect)
1457 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001458 /*
1459 * Process user requests only after previous user/driver/core
1460 * requests have been processed
1461 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001462 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1463 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1464 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001465 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001466 return -EAGAIN;
1467 }
1468
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001469 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001470 !regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001471 return -EALREADY;
1472
Johannes Berg84fa4f42008-10-24 20:32:23 +02001473 return 0;
1474 }
1475
1476 return -EINVAL;
1477}
1478
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001479/**
1480 * __regulatory_hint - hint to the wireless core a regulatory domain
1481 * @wiphy: if the hint comes from country information from an AP, this
1482 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001483 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001484 *
1485 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001486 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001487 *
1488 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1489 * already been set or other standard error codes.
1490 *
1491 * Caller must hold &cfg80211_mutex
1492 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001493static int __regulatory_hint(struct wiphy *wiphy,
1494 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001495{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001496 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001497 int r = 0;
1498
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001499 assert_cfg80211_lock();
1500
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001501 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001502
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001503 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001504 if (pending_request->initiator ==
1505 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001506 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001507 if (r) {
1508 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001509 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001510 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001511 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001512 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001513 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001514 /*
1515 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001516 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001517 * wiphy
1518 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001519 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001520 pending_request->initiator ==
1521 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001522 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001523 if (r) {
1524 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001525 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001526 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001527 r = -EALREADY;
1528 goto new_request;
1529 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001530 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001531 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001532 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001533
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001534new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001535 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001536
1537 last_request = pending_request;
1538 last_request->intersect = intersect;
1539
1540 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001541
1542 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001543 if (r < 0) {
1544 /*
1545 * Since CRDA will not be called in this case as we already
1546 * have applied the requested regulatory domain before we just
1547 * inform userspace we have processed the request
1548 */
1549 if (r == -EALREADY)
1550 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001551 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001552 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001553
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001554 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001555}
1556
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001557/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001558static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001559{
1560 int r = 0;
1561 struct wiphy *wiphy = NULL;
1562
1563 BUG_ON(!reg_request->alpha2);
1564
1565 mutex_lock(&cfg80211_mutex);
1566
1567 if (wiphy_idx_valid(reg_request->wiphy_idx))
1568 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1569
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001570 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001571 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001572 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001573 goto out;
1574 }
1575
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001576 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001577 /* This is required so that the orig_* parameters are saved */
1578 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1579 wiphy_update_regulatory(wiphy, reg_request->initiator);
1580out:
1581 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001582}
1583
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001584/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001585static void reg_process_pending_hints(void)
1586 {
1587 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001588
1589 spin_lock(&reg_requests_lock);
1590 while (!list_empty(&reg_requests_list)) {
1591 reg_request = list_first_entry(&reg_requests_list,
1592 struct regulatory_request,
1593 list);
1594 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001595
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001596 spin_unlock(&reg_requests_lock);
1597 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001598 spin_lock(&reg_requests_lock);
1599 }
1600 spin_unlock(&reg_requests_lock);
1601}
1602
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001603/* Processes beacon hints -- this has nothing to do with country IEs */
1604static void reg_process_pending_beacon_hints(void)
1605{
1606 struct cfg80211_registered_device *drv;
1607 struct reg_beacon *pending_beacon, *tmp;
1608
1609 mutex_lock(&cfg80211_mutex);
1610
1611 /* This goes through the _pending_ beacon list */
1612 spin_lock_bh(&reg_pending_beacons_lock);
1613
1614 if (list_empty(&reg_pending_beacons)) {
1615 spin_unlock_bh(&reg_pending_beacons_lock);
1616 goto out;
1617 }
1618
1619 list_for_each_entry_safe(pending_beacon, tmp,
1620 &reg_pending_beacons, list) {
1621
1622 list_del_init(&pending_beacon->list);
1623
1624 /* Applies the beacon hint to current wiphys */
1625 list_for_each_entry(drv, &cfg80211_drv_list, list)
1626 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1627
1628 /* Remembers the beacon hint for new wiphys or reg changes */
1629 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1630 }
1631
1632 spin_unlock_bh(&reg_pending_beacons_lock);
1633out:
1634 mutex_unlock(&cfg80211_mutex);
1635}
1636
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001637static void reg_todo(struct work_struct *work)
1638{
1639 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001640 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001641}
1642
1643static DECLARE_WORK(reg_work, reg_todo);
1644
1645static void queue_regulatory_request(struct regulatory_request *request)
1646{
1647 spin_lock(&reg_requests_lock);
1648 list_add_tail(&request->list, &reg_requests_list);
1649 spin_unlock(&reg_requests_lock);
1650
1651 schedule_work(&reg_work);
1652}
1653
1654/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001655static int regulatory_hint_core(const char *alpha2)
1656{
1657 struct regulatory_request *request;
1658
1659 BUG_ON(last_request);
1660
1661 request = kzalloc(sizeof(struct regulatory_request),
1662 GFP_KERNEL);
1663 if (!request)
1664 return -ENOMEM;
1665
1666 request->alpha2[0] = alpha2[0];
1667 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001668 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001669
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001670 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001671
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001672 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001673}
1674
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001675/* User hints */
1676int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001677{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001678 struct regulatory_request *request;
1679
Johannes Bergbe3d4812008-10-24 20:32:21 +02001680 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001681
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001682 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1683 if (!request)
1684 return -ENOMEM;
1685
1686 request->wiphy_idx = WIPHY_IDX_STALE;
1687 request->alpha2[0] = alpha2[0];
1688 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001689 request->initiator = NL80211_REGDOM_SET_BY_USER,
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001690
1691 queue_regulatory_request(request);
1692
1693 return 0;
1694}
1695
1696/* Driver hints */
1697int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1698{
1699 struct regulatory_request *request;
1700
1701 BUG_ON(!alpha2);
1702 BUG_ON(!wiphy);
1703
1704 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1705 if (!request)
1706 return -ENOMEM;
1707
1708 request->wiphy_idx = get_wiphy_idx(wiphy);
1709
1710 /* Must have registered wiphy first */
1711 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1712
1713 request->alpha2[0] = alpha2[0];
1714 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001715 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001716
1717 queue_regulatory_request(request);
1718
1719 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001720}
1721EXPORT_SYMBOL(regulatory_hint);
1722
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001723static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1724 u32 country_ie_checksum)
1725{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001726 struct wiphy *request_wiphy;
1727
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001728 assert_cfg80211_lock();
1729
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001730 if (unlikely(last_request->initiator !=
1731 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1732 return false;
1733
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001734 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1735
1736 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001737 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001738
1739 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001740 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001741 /*
1742 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001743 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001744 * on the device
1745 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001746 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1747 return true;
1748 return false;
1749}
1750
1751void regulatory_hint_11d(struct wiphy *wiphy,
1752 u8 *country_ie,
1753 u8 country_ie_len)
1754{
1755 struct ieee80211_regdomain *rd = NULL;
1756 char alpha2[2];
1757 u32 checksum = 0;
1758 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001759 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001760
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001761 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001762
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001763 if (unlikely(!last_request)) {
1764 mutex_unlock(&cfg80211_mutex);
1765 return;
1766 }
1767
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001768 /* IE len must be evenly divisible by 2 */
1769 if (country_ie_len & 0x01)
1770 goto out;
1771
1772 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1773 goto out;
1774
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001775 /*
1776 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001777 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001778 * we were able to process the last regulatory_hint_11d() call
1779 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001780 if (country_ie_regdomain)
1781 goto out;
1782
1783 alpha2[0] = country_ie[0];
1784 alpha2[1] = country_ie[1];
1785
1786 if (country_ie[2] == 'I')
1787 env = ENVIRON_INDOOR;
1788 else if (country_ie[2] == 'O')
1789 env = ENVIRON_OUTDOOR;
1790
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001791 /*
1792 * We will run this for *every* beacon processed for the BSSID, so
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001793 * we optimize an early check to exit out early if we don't have to
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001794 * do anything
1795 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001796 if (likely(last_request->initiator ==
1797 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1798 wiphy_idx_valid(last_request->wiphy_idx))) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001799 struct cfg80211_registered_device *drv_last_ie;
1800
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001801 drv_last_ie =
1802 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001803
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001804 /*
1805 * Lets keep this simple -- we trust the first AP
1806 * after we intersect with CRDA
1807 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001808 if (likely(&drv_last_ie->wiphy == wiphy)) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001809 /*
1810 * Ignore IEs coming in on this wiphy with
1811 * the same alpha2 and environment cap
1812 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001813 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1814 alpha2) &&
1815 env == drv_last_ie->env)) {
1816 goto out;
1817 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001818 /*
1819 * the wiphy moved on to another BSSID or the AP
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001820 * was reconfigured. XXX: We need to deal with the
1821 * case where the user suspends and goes to goes
1822 * to another country, and then gets IEs from an
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001823 * AP with different settings
1824 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001825 goto out;
1826 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001827 /*
1828 * Ignore IEs coming in on two separate wiphys with
1829 * the same alpha2 and environment cap
1830 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001831 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1832 alpha2) &&
1833 env == drv_last_ie->env)) {
1834 goto out;
1835 }
1836 /* We could potentially intersect though */
1837 goto out;
1838 }
1839 }
1840
1841 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1842 if (!rd)
1843 goto out;
1844
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001845 /*
1846 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001847 * the future when we want to add suspend/resume support and having
1848 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001849 * move to another AP. Right now we just trust the first AP.
1850 *
1851 * If we hit this before we add this support we want to be informed of
1852 * it as it would indicate a mistake in the current design
1853 */
1854 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001855 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001856
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001857 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1858 if (!request)
1859 goto free_rd_out;
1860
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001861 /*
1862 * We keep this around for when CRDA comes back with a response so
1863 * we can intersect with that
1864 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001865 country_ie_regdomain = rd;
1866
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001867 request->wiphy_idx = get_wiphy_idx(wiphy);
1868 request->alpha2[0] = rd->alpha2[0];
1869 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001870 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001871 request->country_ie_checksum = checksum;
1872 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001873
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001874 mutex_unlock(&cfg80211_mutex);
1875
1876 queue_regulatory_request(request);
1877
1878 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001879
1880free_rd_out:
1881 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001882out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001883 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001884}
1885EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001886
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001887static bool freq_is_chan_12_13_14(u16 freq)
1888{
1889 if (freq == ieee80211_channel_to_frequency(12) ||
1890 freq == ieee80211_channel_to_frequency(13) ||
1891 freq == ieee80211_channel_to_frequency(14))
1892 return true;
1893 return false;
1894}
1895
1896int regulatory_hint_found_beacon(struct wiphy *wiphy,
1897 struct ieee80211_channel *beacon_chan,
1898 gfp_t gfp)
1899{
1900 struct reg_beacon *reg_beacon;
1901
1902 if (likely((beacon_chan->beacon_found ||
1903 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1904 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1905 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1906 return 0;
1907
1908 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1909 if (!reg_beacon)
1910 return -ENOMEM;
1911
1912#ifdef CONFIG_CFG80211_REG_DEBUG
1913 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1914 "frequency: %d MHz (Ch %d) on %s\n",
1915 beacon_chan->center_freq,
1916 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1917 wiphy_name(wiphy));
1918#endif
1919 memcpy(&reg_beacon->chan, beacon_chan,
1920 sizeof(struct ieee80211_channel));
1921
1922
1923 /*
1924 * Since we can be called from BH or and non-BH context
1925 * we must use spin_lock_bh()
1926 */
1927 spin_lock_bh(&reg_pending_beacons_lock);
1928 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1929 spin_unlock_bh(&reg_pending_beacons_lock);
1930
1931 schedule_work(&reg_work);
1932
1933 return 0;
1934}
1935
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001936static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001937{
1938 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001939 const struct ieee80211_reg_rule *reg_rule = NULL;
1940 const struct ieee80211_freq_range *freq_range = NULL;
1941 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001942
1943 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1944 "(max_antenna_gain, max_eirp)\n");
1945
1946 for (i = 0; i < rd->n_reg_rules; i++) {
1947 reg_rule = &rd->reg_rules[i];
1948 freq_range = &reg_rule->freq_range;
1949 power_rule = &reg_rule->power_rule;
1950
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001951 /*
1952 * There may not be documentation for max antenna gain
1953 * in certain regions
1954 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001955 if (power_rule->max_antenna_gain)
1956 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1957 "(%d mBi, %d mBm)\n",
1958 freq_range->start_freq_khz,
1959 freq_range->end_freq_khz,
1960 freq_range->max_bandwidth_khz,
1961 power_rule->max_antenna_gain,
1962 power_rule->max_eirp);
1963 else
1964 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1965 "(N/A, %d mBm)\n",
1966 freq_range->start_freq_khz,
1967 freq_range->end_freq_khz,
1968 freq_range->max_bandwidth_khz,
1969 power_rule->max_eirp);
1970 }
1971}
1972
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001973static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001974{
1975
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001976 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001977
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001978 if (last_request->initiator ==
1979 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001980 struct cfg80211_registered_device *drv;
1981 drv = cfg80211_drv_by_wiphy_idx(
1982 last_request->wiphy_idx);
1983 if (drv) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001984 printk(KERN_INFO "cfg80211: Current regulatory "
1985 "domain updated by AP to: %c%c\n",
1986 drv->country_ie_alpha2[0],
1987 drv->country_ie_alpha2[1]);
1988 } else
1989 printk(KERN_INFO "cfg80211: Current regulatory "
1990 "domain intersected: \n");
1991 } else
1992 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001993 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001994 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001995 printk(KERN_INFO "cfg80211: World regulatory "
1996 "domain updated:\n");
1997 else {
1998 if (is_unknown_alpha2(rd->alpha2))
1999 printk(KERN_INFO "cfg80211: Regulatory domain "
2000 "changed to driver built-in settings "
2001 "(unknown country)\n");
2002 else
2003 printk(KERN_INFO "cfg80211: Regulatory domain "
2004 "changed to country: %c%c\n",
2005 rd->alpha2[0], rd->alpha2[1]);
2006 }
2007 print_rd_rules(rd);
2008}
2009
Johannes Berg2df78162008-10-28 16:49:41 +01002010static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002011{
2012 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2013 rd->alpha2[0], rd->alpha2[1]);
2014 print_rd_rules(rd);
2015}
2016
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002017#ifdef CONFIG_CFG80211_REG_DEBUG
2018static void reg_country_ie_process_debug(
2019 const struct ieee80211_regdomain *rd,
2020 const struct ieee80211_regdomain *country_ie_regdomain,
2021 const struct ieee80211_regdomain *intersected_rd)
2022{
2023 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2024 print_regdomain_info(country_ie_regdomain);
2025 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2026 print_regdomain_info(rd);
2027 if (intersected_rd) {
2028 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2029 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08002030 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002031 return;
2032 }
2033 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2034}
2035#else
2036static inline void reg_country_ie_process_debug(
2037 const struct ieee80211_regdomain *rd,
2038 const struct ieee80211_regdomain *country_ie_regdomain,
2039 const struct ieee80211_regdomain *intersected_rd)
2040{
2041}
2042#endif
2043
Johannes Bergd2372b32008-10-24 20:32:20 +02002044/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002045static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002046{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002047 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002048 struct cfg80211_registered_device *drv = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002049 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002050 /* Some basic sanity checks first */
2051
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002052 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002053 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002054 return -EINVAL;
2055 update_world_regdomain(rd);
2056 return 0;
2057 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002058
2059 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2060 !is_unknown_alpha2(rd->alpha2))
2061 return -EINVAL;
2062
Johannes Bergf6037d02008-10-21 11:01:33 +02002063 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002064 return -EINVAL;
2065
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002066 /*
2067 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002068 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002069 * and the pending request came in from a country IE
2070 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002071 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002072 /*
2073 * If someone else asked us to change the rd lets only bother
2074 * checking if the alpha2 changes if CRDA was already called
2075 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002076 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05002077 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002078 return -EINVAL;
2079 }
2080
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002081 /*
2082 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002083 * and finally inform them of what we have done, in case they want
2084 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002085 * internal EEPROM data
2086 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002087
Johannes Bergf6037d02008-10-21 11:01:33 +02002088 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002089 return -EINVAL;
2090
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002091 if (!is_valid_rd(rd)) {
2092 printk(KERN_ERR "cfg80211: Invalid "
2093 "regulatory domain detected:\n");
2094 print_regdomain_info(rd);
2095 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002096 }
2097
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002098 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2099
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002100 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002101 int r;
2102
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002103 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002104 reset_regdomains();
2105 cfg80211_regdomain = rd;
2106 return 0;
2107 }
2108
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002109 /*
2110 * For a driver hint, lets copy the regulatory domain the
2111 * driver wanted to the wiphy to deal with conflicts
2112 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002113
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002114 BUG_ON(request_wiphy->regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002115
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002116 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002117 if (r)
2118 return r;
2119
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002120 reset_regdomains();
2121 cfg80211_regdomain = rd;
2122 return 0;
2123 }
2124
2125 /* Intersection requires a bit more work */
2126
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002127 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002128
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002129 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2130 if (!intersected_rd)
2131 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002132
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002133 /*
2134 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002135 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002136 * domain we keep it for its private use
2137 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002138 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002139 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002140 else
2141 kfree(rd);
2142
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002143 rd = NULL;
2144
2145 reset_regdomains();
2146 cfg80211_regdomain = intersected_rd;
2147
2148 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002149 }
2150
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002151 /*
2152 * Country IE requests are handled a bit differently, we intersect
2153 * the country IE rd with what CRDA believes that country should have
2154 */
2155
2156 BUG_ON(!country_ie_regdomain);
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002157 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002158
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002159 /*
2160 * Intersect what CRDA returned and our what we
2161 * had built from the Country IE received
2162 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002163
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002164 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002165
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002166 reg_country_ie_process_debug(rd,
2167 country_ie_regdomain,
2168 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002169
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002170 kfree(country_ie_regdomain);
2171 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002172
2173 if (!intersected_rd)
2174 return -EINVAL;
2175
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002176 drv = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002177
2178 drv->country_ie_alpha2[0] = rd->alpha2[0];
2179 drv->country_ie_alpha2[1] = rd->alpha2[1];
2180 drv->env = last_request->country_ie_env;
2181
2182 BUG_ON(intersected_rd == rd);
2183
2184 kfree(rd);
2185 rd = NULL;
2186
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002187 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002188 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002189
2190 return 0;
2191}
2192
2193
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002194/*
2195 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002196 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002197 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2198 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002199int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002200{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002201 int r;
2202
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002203 assert_cfg80211_lock();
2204
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002205 /* Note that this doesn't update the wiphys, this is done below */
2206 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002207 if (r) {
2208 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002209 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002210 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002211
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002212 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002213 if (!last_request->intersect)
2214 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002215
2216 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002217 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002218
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002219 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002220
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002221 nl80211_send_reg_change_event(last_request);
2222
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002223 return r;
2224}
2225
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002226/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002227void reg_device_remove(struct wiphy *wiphy)
2228{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002229 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002230
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002231 assert_cfg80211_lock();
2232
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002233 kfree(wiphy->regd);
2234
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002235 if (last_request)
2236 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002237
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002238 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002239 return;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002240
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002241 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002242 last_request->country_ie_env = ENVIRON_ANY;
2243}
2244
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002245int regulatory_init(void)
2246{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002247 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002248
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002249 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2250 if (IS_ERR(reg_pdev))
2251 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002252
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002253 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002254 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002255
Johannes Berg734366d2008-09-15 10:56:48 +02002256#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002257 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002258
Johannes Berg942b25c2008-09-15 11:26:47 +02002259 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02002260 print_regdomain_info(cfg80211_regdomain);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002261 /*
2262 * The old code still requests for a new regdomain and if
Johannes Berg734366d2008-09-15 10:56:48 +02002263 * you have CRDA you get it updated, otherwise you get
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002264 * stuck with the static values. Since "EU" is not a valid
2265 * ISO / IEC 3166 alpha2 code we can't expect userpace to
2266 * give us a regulatory domain for it. We need last_request
2267 * iniitalized though so lets just send a request which we
2268 * know will be ignored... this crap will be removed once
2269 * OLD_REG dies.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002270 */
Luis R. Rodriguez2e097dc2009-03-20 23:53:04 -04002271 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002272#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002273 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002274
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -04002275 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002276#endif
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002277 if (err) {
2278 if (err == -ENOMEM)
2279 return err;
2280 /*
2281 * N.B. kobject_uevent_env() can fail mainly for when we're out
2282 * memory which is handled and propagated appropriately above
2283 * but it can also fail during a netlink_broadcast() or during
2284 * early boot for call_usermodehelper(). For now treat these
2285 * errors as non-fatal.
2286 */
2287 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2288 "to call CRDA during init");
2289#ifdef CONFIG_CFG80211_REG_DEBUG
2290 /* We want to find out exactly why when debugging */
2291 WARN_ON(err);
2292#endif
2293 }
Johannes Berg734366d2008-09-15 10:56:48 +02002294
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002295 return 0;
2296}
2297
2298void regulatory_exit(void)
2299{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002300 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002301 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002302
2303 cancel_work_sync(&reg_work);
2304
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002305 mutex_lock(&cfg80211_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002306
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002307 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002308
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002309 kfree(country_ie_regdomain);
2310 country_ie_regdomain = NULL;
2311
Johannes Bergf6037d02008-10-21 11:01:33 +02002312 kfree(last_request);
2313
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002314 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002315
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002316 spin_lock_bh(&reg_pending_beacons_lock);
2317 if (!list_empty(&reg_pending_beacons)) {
2318 list_for_each_entry_safe(reg_beacon, btmp,
2319 &reg_pending_beacons, list) {
2320 list_del(&reg_beacon->list);
2321 kfree(reg_beacon);
2322 }
2323 }
2324 spin_unlock_bh(&reg_pending_beacons_lock);
2325
2326 if (!list_empty(&reg_beacon_list)) {
2327 list_for_each_entry_safe(reg_beacon, btmp,
2328 &reg_beacon_list, list) {
2329 list_del(&reg_beacon->list);
2330 kfree(reg_beacon);
2331 }
2332 }
2333
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002334 spin_lock(&reg_requests_lock);
2335 if (!list_empty(&reg_requests_list)) {
2336 list_for_each_entry_safe(reg_request, tmp,
2337 &reg_requests_list, list) {
2338 list_del(&reg_request->list);
2339 kfree(reg_request);
2340 }
2341 }
2342 spin_unlock(&reg_requests_lock);
2343
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002344 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002345}