blob: 6e1733733e1828b5f0830a454ace795b65c38bcb [file] [log] [blame]
Johannes Berg8318d782008-01-24 19:38:38 +01001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
Johannes Berg8318d782008-01-24 19:38:38 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012/**
13 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010014 *
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
19 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070020 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
23 *
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
29 *
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
33 *
Johannes Berg8318d782008-01-24 19:38:38 +010034 */
35#include <linux/kernel.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070036#include <linux/list.h>
37#include <linux/random.h>
38#include <linux/nl80211.h>
39#include <linux/platform_device.h>
Johannes Berg8318d782008-01-24 19:38:38 +010040#include <net/wireless.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070041#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010042#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070043#include "reg.h"
Johannes Berg8318d782008-01-24 19:38:38 +010044
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070045/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020046static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020047
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070048/* To trigger userspace events */
49static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010050
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070051/* Keep the ordering from large to small */
52static u32 supported_bandwidths[] = {
53 MHZ_TO_KHZ(40),
54 MHZ_TO_KHZ(20),
Johannes Berg8318d782008-01-24 19:38:38 +010055};
56
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050057/*
58 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020059 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050060 * information to give us an alpha2
61 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080062const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020063
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050064/*
65 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080066 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050067 * what it thinks should apply for the same country
68 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080069static const struct ieee80211_regdomain *country_ie_regdomain;
70
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050071/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050072static LIST_HEAD(reg_requests_list);
73static spinlock_t reg_requests_lock;
74
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050075/* Used to queue up beacon hints for review */
76static LIST_HEAD(reg_pending_beacons);
77static spinlock_t reg_pending_beacons_lock;
78
79/* Used to keep track of processed beacon hints */
80static LIST_HEAD(reg_beacon_list);
81
82struct reg_beacon {
83 struct list_head list;
84 struct ieee80211_channel chan;
85};
86
Johannes Berg734366d2008-09-15 10:56:48 +020087/* We keep a static world regulatory domain in case of the absence of CRDA */
88static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -050089 .n_reg_rules = 3,
Johannes Berg734366d2008-09-15 10:56:48 +020090 .alpha2 = "00",
91 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -050092 /* IEEE 802.11b/g, channels 1..11 */
93 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -050094 /* IEEE 802.11a, channel 36..48 */
95 REG_RULE(5180-10, 5240+10, 40, 6, 23,
96 NL80211_RRF_PASSIVE_SCAN |
97 NL80211_RRF_NO_IBSS),
98
99 /* NB: 5260 MHz - 5700 MHz requies DFS */
100
101 /* IEEE 802.11a, channel 149..165 */
102 REG_RULE(5745-10, 5825+10, 40, 6, 23,
103 NL80211_RRF_PASSIVE_SCAN |
104 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200105 }
106};
107
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200108static const struct ieee80211_regdomain *cfg80211_world_regdom =
109 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200110
111#ifdef CONFIG_WIRELESS_OLD_REGULATORY
112static char *ieee80211_regdom = "US";
113module_param(ieee80211_regdom, charp, 0444);
114MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
115
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500116/*
117 * We assume 40 MHz bandwidth for the old regulatory work.
Johannes Berg734366d2008-09-15 10:56:48 +0200118 * We make emphasis we are using the exact same frequencies
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500119 * as before
120 */
Johannes Berg734366d2008-09-15 10:56:48 +0200121
122static const struct ieee80211_regdomain us_regdom = {
123 .n_reg_rules = 6,
124 .alpha2 = "US",
125 .reg_rules = {
126 /* IEEE 802.11b/g, channels 1..11 */
127 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
128 /* IEEE 802.11a, channel 36 */
129 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
130 /* IEEE 802.11a, channel 40 */
131 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
132 /* IEEE 802.11a, channel 44 */
133 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
134 /* IEEE 802.11a, channels 48..64 */
135 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
136 /* IEEE 802.11a, channels 149..165, outdoor */
137 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
138 }
139};
140
141static const struct ieee80211_regdomain jp_regdom = {
142 .n_reg_rules = 3,
143 .alpha2 = "JP",
144 .reg_rules = {
145 /* IEEE 802.11b/g, channels 1..14 */
146 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
147 /* IEEE 802.11a, channels 34..48 */
148 REG_RULE(5170-10, 5240+10, 40, 6, 20,
149 NL80211_RRF_PASSIVE_SCAN),
150 /* IEEE 802.11a, channels 52..64 */
151 REG_RULE(5260-10, 5320+10, 40, 6, 20,
152 NL80211_RRF_NO_IBSS |
153 NL80211_RRF_DFS),
154 }
155};
156
157static const struct ieee80211_regdomain eu_regdom = {
158 .n_reg_rules = 6,
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500159 /*
160 * This alpha2 is bogus, we leave it here just for stupid
161 * backward compatibility
162 */
Johannes Berg734366d2008-09-15 10:56:48 +0200163 .alpha2 = "EU",
164 .reg_rules = {
165 /* IEEE 802.11b/g, channels 1..13 */
166 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
167 /* IEEE 802.11a, channel 36 */
168 REG_RULE(5180-10, 5180+10, 40, 6, 23,
169 NL80211_RRF_PASSIVE_SCAN),
170 /* IEEE 802.11a, channel 40 */
171 REG_RULE(5200-10, 5200+10, 40, 6, 23,
172 NL80211_RRF_PASSIVE_SCAN),
173 /* IEEE 802.11a, channel 44 */
174 REG_RULE(5220-10, 5220+10, 40, 6, 23,
175 NL80211_RRF_PASSIVE_SCAN),
176 /* IEEE 802.11a, channels 48..64 */
177 REG_RULE(5240-10, 5320+10, 40, 6, 20,
178 NL80211_RRF_NO_IBSS |
179 NL80211_RRF_DFS),
180 /* IEEE 802.11a, channels 100..140 */
181 REG_RULE(5500-10, 5700+10, 40, 6, 30,
182 NL80211_RRF_NO_IBSS |
183 NL80211_RRF_DFS),
184 }
185};
186
187static const struct ieee80211_regdomain *static_regdom(char *alpha2)
188{
189 if (alpha2[0] == 'U' && alpha2[1] == 'S')
190 return &us_regdom;
191 if (alpha2[0] == 'J' && alpha2[1] == 'P')
192 return &jp_regdom;
193 if (alpha2[0] == 'E' && alpha2[1] == 'U')
194 return &eu_regdom;
195 /* Default, as per the old rules */
196 return &us_regdom;
197}
198
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200199static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200200{
201 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
202 return true;
203 return false;
204}
Johannes Berg734366d2008-09-15 10:56:48 +0200205#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200206static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
207{
208 return false;
209}
210#endif
211
Johannes Berg734366d2008-09-15 10:56:48 +0200212static void reset_regdomains(void)
213{
Johannes Berg942b25c2008-09-15 11:26:47 +0200214 /* avoid freeing static information or freeing something twice */
215 if (cfg80211_regdomain == cfg80211_world_regdom)
216 cfg80211_regdomain = NULL;
217 if (cfg80211_world_regdom == &world_regdom)
218 cfg80211_world_regdom = NULL;
219 if (cfg80211_regdomain == &world_regdom)
220 cfg80211_regdomain = NULL;
221 if (is_old_static_regdom(cfg80211_regdomain))
222 cfg80211_regdomain = NULL;
223
224 kfree(cfg80211_regdomain);
225 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200226
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200227 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200228 cfg80211_regdomain = NULL;
229}
230
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500231/*
232 * Dynamic world regulatory domain requested by the wireless
233 * core upon initialization
234 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200235static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200236{
Johannes Bergf6037d02008-10-21 11:01:33 +0200237 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200238
239 reset_regdomains();
240
241 cfg80211_world_regdom = rd;
242 cfg80211_regdomain = rd;
243}
Johannes Berg734366d2008-09-15 10:56:48 +0200244
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200245bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100246{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700247 if (!alpha2)
248 return false;
249 if (alpha2[0] == '0' && alpha2[1] == '0')
250 return true;
251 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100252}
253
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200254static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700255{
256 if (!alpha2)
257 return false;
258 if (alpha2[0] != 0 && alpha2[1] != 0)
259 return true;
260 return false;
261}
Johannes Berg8318d782008-01-24 19:38:38 +0100262
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263static bool is_alpha_upper(char letter)
264{
265 /* ASCII A - Z */
266 if (letter >= 65 && letter <= 90)
267 return true;
268 return false;
269}
270
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200271static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700272{
273 if (!alpha2)
274 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500275 /*
276 * Special case where regulatory domain was built by driver
277 * but a specific alpha2 cannot be determined
278 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700279 if (alpha2[0] == '9' && alpha2[1] == '9')
280 return true;
281 return false;
282}
283
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800284static bool is_intersected_alpha2(const char *alpha2)
285{
286 if (!alpha2)
287 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500288 /*
289 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800290 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500291 * structures
292 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800293 if (alpha2[0] == '9' && alpha2[1] == '8')
294 return true;
295 return false;
296}
297
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200298static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700299{
300 if (!alpha2)
301 return false;
302 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
303 return true;
304 return false;
305}
306
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200307static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700308{
309 if (!alpha2_x || !alpha2_y)
310 return false;
311 if (alpha2_x[0] == alpha2_y[0] &&
312 alpha2_x[1] == alpha2_y[1])
313 return true;
314 return false;
315}
316
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500317static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700318{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500319 assert_cfg80211_lock();
320
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700321 if (!cfg80211_regdomain)
322 return true;
323 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
324 return false;
325 return true;
326}
327
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800328/**
329 * country_ie_integrity_changes - tells us if the country IE has changed
330 * @checksum: checksum of country IE of fields we are interested in
331 *
332 * If the country IE has not changed you can ignore it safely. This is
333 * useful to determine if two devices are seeing two different country IEs
334 * even on the same alpha2. Note that this will return false if no IE has
335 * been set on the wireless core yet.
336 */
337static bool country_ie_integrity_changes(u32 checksum)
338{
339 /* If no IE has been set then the checksum doesn't change */
340 if (unlikely(!last_request->country_ie_checksum))
341 return false;
342 if (unlikely(last_request->country_ie_checksum != checksum))
343 return true;
344 return false;
345}
346
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500347/*
348 * This lets us keep regulatory code which is updated on a regulatory
349 * basis in userspace.
350 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700351static int call_crda(const char *alpha2)
352{
353 char country_env[9 + 2] = "COUNTRY=";
354 char *envp[] = {
355 country_env,
356 NULL
357 };
358
359 if (!is_world_regdom((char *) alpha2))
360 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
361 alpha2[0], alpha2[1]);
362 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700363 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
364 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700365
366 country_env[8] = alpha2[0];
367 country_env[9] = alpha2[1];
368
369 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
370}
371
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700372/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200373bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700374{
Johannes Bergf6037d02008-10-21 11:01:33 +0200375 if (!last_request)
376 return false;
377
378 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700379}
380
381/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200382static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700383{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200384 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700385 u32 freq_diff;
386
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800387 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388 return false;
389
390 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
391 return false;
392
393 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
394
Luis R. Rodriguezd71aaf62008-10-30 13:33:52 -0700395 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700396 return false;
397
398 return true;
399}
400
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200401static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700402{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200403 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700404 unsigned int i;
405
406 if (!rd->n_reg_rules)
407 return false;
408
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800409 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
410 return false;
411
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700412 for (i = 0; i < rd->n_reg_rules; i++) {
413 reg_rule = &rd->reg_rules[i];
414 if (!is_valid_reg_rule(reg_rule))
415 return false;
416 }
417
418 return true;
419}
420
421/* Returns value in KHz */
422static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
423 u32 freq)
424{
425 unsigned int i;
426 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
427 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
428 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
429 if (start_freq_khz >= freq_range->start_freq_khz &&
430 end_freq_khz <= freq_range->end_freq_khz)
431 return supported_bandwidths[i];
432 }
433 return 0;
434}
435
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800436/**
437 * freq_in_rule_band - tells us if a frequency is in a frequency band
438 * @freq_range: frequency rule we want to query
439 * @freq_khz: frequency we are inquiring about
440 *
441 * This lets us know if a specific frequency rule is or is not relevant to
442 * a specific frequency's band. Bands are device specific and artificial
443 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
444 * safe for now to assume that a frequency rule should not be part of a
445 * frequency's band if the start freq or end freq are off by more than 2 GHz.
446 * This resolution can be lowered and should be considered as we add
447 * regulatory rule support for other "bands".
448 **/
449static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
450 u32 freq_khz)
451{
452#define ONE_GHZ_IN_KHZ 1000000
453 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
454 return true;
455 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
456 return true;
457 return false;
458#undef ONE_GHZ_IN_KHZ
459}
460
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500461/*
462 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800463 * structure has a lot of information which the IE doesn't yet have,
464 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500465 * with our userspace regulatory agent to get lower bounds.
466 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800467static struct ieee80211_regdomain *country_ie_2_rd(
468 u8 *country_ie,
469 u8 country_ie_len,
470 u32 *checksum)
471{
472 struct ieee80211_regdomain *rd = NULL;
473 unsigned int i = 0;
474 char alpha2[2];
475 u32 flags = 0;
476 u32 num_rules = 0, size_of_regd = 0;
477 u8 *triplets_start = NULL;
478 u8 len_at_triplet = 0;
479 /* the last channel we have registered in a subband (triplet) */
480 int last_sub_max_channel = 0;
481
482 *checksum = 0xDEADBEEF;
483
484 /* Country IE requirements */
485 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
486 country_ie_len & 0x01);
487
488 alpha2[0] = country_ie[0];
489 alpha2[1] = country_ie[1];
490
491 /*
492 * Third octet can be:
493 * 'I' - Indoor
494 * 'O' - Outdoor
495 *
496 * anything else we assume is no restrictions
497 */
498 if (country_ie[2] == 'I')
499 flags = NL80211_RRF_NO_OUTDOOR;
500 else if (country_ie[2] == 'O')
501 flags = NL80211_RRF_NO_INDOOR;
502
503 country_ie += 3;
504 country_ie_len -= 3;
505
506 triplets_start = country_ie;
507 len_at_triplet = country_ie_len;
508
509 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
510
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500511 /*
512 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800513 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500514 * for each channel subband
515 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800516 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800517 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800518 struct ieee80211_country_ie_triplet *triplet =
519 (struct ieee80211_country_ie_triplet *) country_ie;
520 int cur_sub_max_channel = 0, cur_channel = 0;
521
522 if (triplet->ext.reg_extension_id >=
523 IEEE80211_COUNTRY_EXTENSION_ID) {
524 country_ie += 3;
525 country_ie_len -= 3;
526 continue;
527 }
528
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800529 /* 2 GHz */
530 if (triplet->chans.first_channel <= 14)
531 end_channel = triplet->chans.first_channel +
532 triplet->chans.num_channels;
533 else
534 /*
535 * 5 GHz -- For example in country IEs if the first
536 * channel given is 36 and the number of channels is 4
537 * then the individual channel numbers defined for the
538 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
539 * and not 36, 37, 38, 39.
540 *
541 * See: http://tinyurl.com/11d-clarification
542 */
543 end_channel = triplet->chans.first_channel +
544 (4 * (triplet->chans.num_channels - 1));
545
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800546 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800547 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800548
549 /* Basic sanity check */
550 if (cur_sub_max_channel < cur_channel)
551 return NULL;
552
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500553 /*
554 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800555 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500556 * increasing
557 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800558 if (last_sub_max_channel) {
559 if (cur_channel <= last_sub_max_channel)
560 return NULL;
561 if (cur_sub_max_channel <= last_sub_max_channel)
562 return NULL;
563 }
564
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500565 /*
566 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800567 * we can throw ext triplets as part of this soup,
568 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500569 * don't support them
570 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800571 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
572 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
573 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
574
575 last_sub_max_channel = cur_sub_max_channel;
576
577 country_ie += 3;
578 country_ie_len -= 3;
579 num_rules++;
580
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500581 /*
582 * Note: this is not a IEEE requirement but
583 * simply a memory requirement
584 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800585 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
586 return NULL;
587 }
588
589 country_ie = triplets_start;
590 country_ie_len = len_at_triplet;
591
592 size_of_regd = sizeof(struct ieee80211_regdomain) +
593 (num_rules * sizeof(struct ieee80211_reg_rule));
594
595 rd = kzalloc(size_of_regd, GFP_KERNEL);
596 if (!rd)
597 return NULL;
598
599 rd->n_reg_rules = num_rules;
600 rd->alpha2[0] = alpha2[0];
601 rd->alpha2[1] = alpha2[1];
602
603 /* This time around we fill in the rd */
604 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800605 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800606 struct ieee80211_country_ie_triplet *triplet =
607 (struct ieee80211_country_ie_triplet *) country_ie;
608 struct ieee80211_reg_rule *reg_rule = NULL;
609 struct ieee80211_freq_range *freq_range = NULL;
610 struct ieee80211_power_rule *power_rule = NULL;
611
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500612 /*
613 * Must parse if dot11RegulatoryClassesRequired is true,
614 * we don't support this yet
615 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800616 if (triplet->ext.reg_extension_id >=
617 IEEE80211_COUNTRY_EXTENSION_ID) {
618 country_ie += 3;
619 country_ie_len -= 3;
620 continue;
621 }
622
623 reg_rule = &rd->reg_rules[i];
624 freq_range = &reg_rule->freq_range;
625 power_rule = &reg_rule->power_rule;
626
627 reg_rule->flags = flags;
628
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800629 /* 2 GHz */
630 if (triplet->chans.first_channel <= 14)
631 end_channel = triplet->chans.first_channel +
632 triplet->chans.num_channels;
633 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800634 end_channel = triplet->chans.first_channel +
635 (4 * (triplet->chans.num_channels - 1));
636
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500637 /*
638 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800639 * the actual band edge, not the center of freq for
640 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500641 * the channels passed
642 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800643 freq_range->start_freq_khz =
644 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
645 triplet->chans.first_channel) - 10);
646 freq_range->end_freq_khz =
647 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800648 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800649
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500650 /*
651 * These are large arbitrary values we use to intersect later.
652 * Increment this if we ever support >= 40 MHz channels
653 * in IEEE 802.11
654 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800655 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
656 power_rule->max_antenna_gain = DBI_TO_MBI(100);
657 power_rule->max_eirp = DBM_TO_MBM(100);
658
659 country_ie += 3;
660 country_ie_len -= 3;
661 i++;
662
663 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
664 }
665
666 return rd;
667}
668
669
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500670/*
671 * Helper for regdom_intersect(), this does the real
672 * mathematical intersection fun
673 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700674static int reg_rules_intersect(
675 const struct ieee80211_reg_rule *rule1,
676 const struct ieee80211_reg_rule *rule2,
677 struct ieee80211_reg_rule *intersected_rule)
678{
679 const struct ieee80211_freq_range *freq_range1, *freq_range2;
680 struct ieee80211_freq_range *freq_range;
681 const struct ieee80211_power_rule *power_rule1, *power_rule2;
682 struct ieee80211_power_rule *power_rule;
683 u32 freq_diff;
684
685 freq_range1 = &rule1->freq_range;
686 freq_range2 = &rule2->freq_range;
687 freq_range = &intersected_rule->freq_range;
688
689 power_rule1 = &rule1->power_rule;
690 power_rule2 = &rule2->power_rule;
691 power_rule = &intersected_rule->power_rule;
692
693 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
694 freq_range2->start_freq_khz);
695 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
696 freq_range2->end_freq_khz);
697 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
698 freq_range2->max_bandwidth_khz);
699
700 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
701 if (freq_range->max_bandwidth_khz > freq_diff)
702 freq_range->max_bandwidth_khz = freq_diff;
703
704 power_rule->max_eirp = min(power_rule1->max_eirp,
705 power_rule2->max_eirp);
706 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
707 power_rule2->max_antenna_gain);
708
709 intersected_rule->flags = (rule1->flags | rule2->flags);
710
711 if (!is_valid_reg_rule(intersected_rule))
712 return -EINVAL;
713
714 return 0;
715}
716
717/**
718 * regdom_intersect - do the intersection between two regulatory domains
719 * @rd1: first regulatory domain
720 * @rd2: second regulatory domain
721 *
722 * Use this function to get the intersection between two regulatory domains.
723 * Once completed we will mark the alpha2 for the rd as intersected, "98",
724 * as no one single alpha2 can represent this regulatory domain.
725 *
726 * Returns a pointer to the regulatory domain structure which will hold the
727 * resulting intersection of rules between rd1 and rd2. We will
728 * kzalloc() this structure for you.
729 */
730static struct ieee80211_regdomain *regdom_intersect(
731 const struct ieee80211_regdomain *rd1,
732 const struct ieee80211_regdomain *rd2)
733{
734 int r, size_of_regd;
735 unsigned int x, y;
736 unsigned int num_rules = 0, rule_idx = 0;
737 const struct ieee80211_reg_rule *rule1, *rule2;
738 struct ieee80211_reg_rule *intersected_rule;
739 struct ieee80211_regdomain *rd;
740 /* This is just a dummy holder to help us count */
741 struct ieee80211_reg_rule irule;
742
743 /* Uses the stack temporarily for counter arithmetic */
744 intersected_rule = &irule;
745
746 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
747
748 if (!rd1 || !rd2)
749 return NULL;
750
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500751 /*
752 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700753 * build them. This is to so we can malloc() and free() a
754 * regdomain once. The reason we use reg_rules_intersect() here
755 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500756 * All rules that do check out OK are valid.
757 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700758
759 for (x = 0; x < rd1->n_reg_rules; x++) {
760 rule1 = &rd1->reg_rules[x];
761 for (y = 0; y < rd2->n_reg_rules; y++) {
762 rule2 = &rd2->reg_rules[y];
763 if (!reg_rules_intersect(rule1, rule2,
764 intersected_rule))
765 num_rules++;
766 memset(intersected_rule, 0,
767 sizeof(struct ieee80211_reg_rule));
768 }
769 }
770
771 if (!num_rules)
772 return NULL;
773
774 size_of_regd = sizeof(struct ieee80211_regdomain) +
775 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
776
777 rd = kzalloc(size_of_regd, GFP_KERNEL);
778 if (!rd)
779 return NULL;
780
781 for (x = 0; x < rd1->n_reg_rules; x++) {
782 rule1 = &rd1->reg_rules[x];
783 for (y = 0; y < rd2->n_reg_rules; y++) {
784 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500785 /*
786 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700787 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500788 * a memcpy()
789 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700790 intersected_rule = &rd->reg_rules[rule_idx];
791 r = reg_rules_intersect(rule1, rule2,
792 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500793 /*
794 * No need to memset here the intersected rule here as
795 * we're not using the stack anymore
796 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700797 if (r)
798 continue;
799 rule_idx++;
800 }
801 }
802
803 if (rule_idx != num_rules) {
804 kfree(rd);
805 return NULL;
806 }
807
808 rd->n_reg_rules = num_rules;
809 rd->alpha2[0] = '9';
810 rd->alpha2[1] = '8';
811
812 return rd;
813}
814
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500815/*
816 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
817 * want to just have the channel structure use these
818 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700819static u32 map_regdom_flags(u32 rd_flags)
820{
821 u32 channel_flags = 0;
822 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
823 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
824 if (rd_flags & NL80211_RRF_NO_IBSS)
825 channel_flags |= IEEE80211_CHAN_NO_IBSS;
826 if (rd_flags & NL80211_RRF_DFS)
827 channel_flags |= IEEE80211_CHAN_RADAR;
828 return channel_flags;
829}
830
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800831static int freq_reg_info_regd(struct wiphy *wiphy,
832 u32 center_freq,
833 u32 *bandwidth,
834 const struct ieee80211_reg_rule **reg_rule,
835 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100836{
837 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800838 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800839 const struct ieee80211_regdomain *regd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700840 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100841
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800842 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800843
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500844 /*
845 * Follow the driver's regulatory domain, if present, unless a country
846 * IE has been processed or a user wants to help complaince further
847 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800848 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez24ed1da2009-01-22 15:05:54 -0800849 last_request->initiator != REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800850 wiphy->regd)
851 regd = wiphy->regd;
852
853 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700854 return -EINVAL;
855
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800856 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700857 const struct ieee80211_reg_rule *rr;
858 const struct ieee80211_freq_range *fr = NULL;
859 const struct ieee80211_power_rule *pr = NULL;
860
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800861 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700862 fr = &rr->freq_range;
863 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800864
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500865 /*
866 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800867 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500868 * not overwrite it once found
869 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800870 if (!band_rule_found)
871 band_rule_found = freq_in_rule_band(fr, center_freq);
872
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700873 max_bandwidth = freq_max_bandwidth(fr, center_freq);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800874
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700875 if (max_bandwidth && *bandwidth <= max_bandwidth) {
876 *reg_rule = rr;
877 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100878 break;
879 }
880 }
881
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800882 if (!band_rule_found)
883 return -ERANGE;
884
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700885 return !max_bandwidth;
886}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800887EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700888
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800889int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800890 const struct ieee80211_reg_rule **reg_rule)
891{
892 return freq_reg_info_regd(wiphy, center_freq,
893 bandwidth, reg_rule, NULL);
894}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700895
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800896static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
897 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700898{
899 int r;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800900 u32 flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700901 u32 max_bandwidth = 0;
902 const struct ieee80211_reg_rule *reg_rule = NULL;
903 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800904 struct ieee80211_supported_band *sband;
905 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500906 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800907
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500908 assert_cfg80211_lock();
909
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500910 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
911
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800912 sband = wiphy->bands[band];
913 BUG_ON(chan_idx >= sband->n_channels);
914 chan = &sband->channels[chan_idx];
915
916 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700917
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800918 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700919 &max_bandwidth, &reg_rule);
920
921 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500922 /*
923 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800924 * with a frequency range on the center_freq's band, since
925 * IEEE-802.11 allows for a country IE to have a subset of the
926 * regulatory information provided in a country we ignore
927 * disabling the channel unless at least one reg rule was
928 * found on the center_freq's band. For details see this
929 * clarification:
930 *
931 * http://tinyurl.com/11d-clarification
932 */
933 if (r == -ERANGE &&
934 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
935#ifdef CONFIG_CFG80211_REG_DEBUG
936 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
937 "intact on %s - no rule found in band on "
938 "Country IE\n",
939 chan->center_freq, wiphy_name(wiphy));
940#endif
941 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500942 /*
943 * In this case we know the country IE has at least one reg rule
944 * for the band so we respect its band definitions
945 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800946#ifdef CONFIG_CFG80211_REG_DEBUG
947 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
948 printk(KERN_DEBUG "cfg80211: Disabling "
949 "channel %d MHz on %s due to "
950 "Country IE\n",
951 chan->center_freq, wiphy_name(wiphy));
952#endif
953 flags |= IEEE80211_CHAN_DISABLED;
954 chan->flags = flags;
955 }
Johannes Berg8318d782008-01-24 19:38:38 +0100956 return;
957 }
958
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700959 power_rule = &reg_rule->power_rule;
960
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800961 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500962 request_wiphy && request_wiphy == wiphy &&
963 request_wiphy->strict_regulatory) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500964 /*
965 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800966 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500967 * settings
968 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800969 chan->flags = chan->orig_flags =
970 map_regdom_flags(reg_rule->flags);
971 chan->max_antenna_gain = chan->orig_mag =
972 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
973 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
974 chan->max_power = chan->orig_mpwr =
975 (int) MBM_TO_DBM(power_rule->max_eirp);
976 return;
977 }
978
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700979 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100980 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700981 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
982 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400983 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700984 chan->max_power = min(chan->orig_mpwr,
985 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400986 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700987 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100988}
989
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800990static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +0100991{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800992 unsigned int i;
993 struct ieee80211_supported_band *sband;
994
995 BUG_ON(!wiphy->bands[band]);
996 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +0100997
998 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800999 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001000}
1001
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001002static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
1003{
1004 if (!last_request)
1005 return true;
1006 if (setby == REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -08001007 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001008 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001009 /*
1010 * wiphy->regd will be set once the device has its own
1011 * desired regulatory domain set
1012 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001013 if (wiphy->strict_regulatory && !wiphy->regd &&
1014 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001015 return true;
1016 return false;
1017}
1018
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001019static void update_all_wiphy_regulatory(enum reg_set_by setby)
1020{
1021 struct cfg80211_registered_device *drv;
1022
1023 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001024 wiphy_update_regulatory(&drv->wiphy, setby);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001025}
1026
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001027static void handle_reg_beacon(struct wiphy *wiphy,
1028 unsigned int chan_idx,
1029 struct reg_beacon *reg_beacon)
1030{
1031#ifdef CONFIG_CFG80211_REG_DEBUG
1032#define REG_DEBUG_BEACON_FLAG(desc) \
1033 printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \
1034 "frequency: %d MHz (Ch %d) on %s\n", \
1035 reg_beacon->chan.center_freq, \
1036 ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \
1037 wiphy_name(wiphy));
1038#else
1039#define REG_DEBUG_BEACON_FLAG(desc) do {} while (0)
1040#endif
1041 struct ieee80211_supported_band *sband;
1042 struct ieee80211_channel *chan;
1043
1044 assert_cfg80211_lock();
1045
1046 sband = wiphy->bands[reg_beacon->chan.band];
1047 chan = &sband->channels[chan_idx];
1048
1049 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1050 return;
1051
1052 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1053 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1054 REG_DEBUG_BEACON_FLAG("active scanning");
1055 }
1056
1057 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1058 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1059 REG_DEBUG_BEACON_FLAG("beaconing");
1060 }
1061
1062 chan->beacon_found = true;
1063#undef REG_DEBUG_BEACON_FLAG
1064}
1065
1066/*
1067 * Called when a scan on a wiphy finds a beacon on
1068 * new channel
1069 */
1070static void wiphy_update_new_beacon(struct wiphy *wiphy,
1071 struct reg_beacon *reg_beacon)
1072{
1073 unsigned int i;
1074 struct ieee80211_supported_band *sband;
1075
1076 assert_cfg80211_lock();
1077
1078 if (!wiphy->bands[reg_beacon->chan.band])
1079 return;
1080
1081 sband = wiphy->bands[reg_beacon->chan.band];
1082
1083 for (i = 0; i < sband->n_channels; i++)
1084 handle_reg_beacon(wiphy, i, reg_beacon);
1085}
1086
1087/*
1088 * Called upon reg changes or a new wiphy is added
1089 */
1090static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1091{
1092 unsigned int i;
1093 struct ieee80211_supported_band *sband;
1094 struct reg_beacon *reg_beacon;
1095
1096 assert_cfg80211_lock();
1097
1098 if (list_empty(&reg_beacon_list))
1099 return;
1100
1101 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1102 if (!wiphy->bands[reg_beacon->chan.band])
1103 continue;
1104 sband = wiphy->bands[reg_beacon->chan.band];
1105 for (i = 0; i < sband->n_channels; i++)
1106 handle_reg_beacon(wiphy, i, reg_beacon);
1107 }
1108}
1109
1110static bool reg_is_world_roaming(struct wiphy *wiphy)
1111{
1112 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1113 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1114 return true;
1115 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
1116 wiphy->custom_regulatory)
1117 return true;
1118 return false;
1119}
1120
1121/* Reap the advantages of previously found beacons */
1122static void reg_process_beacons(struct wiphy *wiphy)
1123{
1124 if (!reg_is_world_roaming(wiphy))
1125 return;
1126 wiphy_update_beacon_reg(wiphy);
1127}
1128
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001129void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
Johannes Berg8318d782008-01-24 19:38:38 +01001130{
1131 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001132
1133 if (ignore_reg_update(wiphy, setby))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001134 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001135 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001136 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001137 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001138 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001139out:
1140 reg_process_beacons(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001141 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001142 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001143}
1144
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001145static void handle_channel_custom(struct wiphy *wiphy,
1146 enum ieee80211_band band,
1147 unsigned int chan_idx,
1148 const struct ieee80211_regdomain *regd)
1149{
1150 int r;
1151 u32 max_bandwidth = 0;
1152 const struct ieee80211_reg_rule *reg_rule = NULL;
1153 const struct ieee80211_power_rule *power_rule = NULL;
1154 struct ieee80211_supported_band *sband;
1155 struct ieee80211_channel *chan;
1156
1157 sband = wiphy->bands[band];
1158 BUG_ON(chan_idx >= sband->n_channels);
1159 chan = &sband->channels[chan_idx];
1160
1161 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1162 &max_bandwidth, &reg_rule, regd);
1163
1164 if (r) {
1165 chan->flags = IEEE80211_CHAN_DISABLED;
1166 return;
1167 }
1168
1169 power_rule = &reg_rule->power_rule;
1170
1171 chan->flags |= map_regdom_flags(reg_rule->flags);
1172 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1173 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1174 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1175}
1176
1177static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1178 const struct ieee80211_regdomain *regd)
1179{
1180 unsigned int i;
1181 struct ieee80211_supported_band *sband;
1182
1183 BUG_ON(!wiphy->bands[band]);
1184 sband = wiphy->bands[band];
1185
1186 for (i = 0; i < sband->n_channels; i++)
1187 handle_channel_custom(wiphy, band, i, regd);
1188}
1189
1190/* Used by drivers prior to wiphy registration */
1191void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1192 const struct ieee80211_regdomain *regd)
1193{
1194 enum ieee80211_band band;
1195 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1196 if (wiphy->bands[band])
1197 handle_band_custom(wiphy, band, regd);
1198 }
1199}
1200EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1201
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001202static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1203 const struct ieee80211_regdomain *src_regd)
1204{
1205 struct ieee80211_regdomain *regd;
1206 int size_of_regd = 0;
1207 unsigned int i;
1208
1209 size_of_regd = sizeof(struct ieee80211_regdomain) +
1210 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1211
1212 regd = kzalloc(size_of_regd, GFP_KERNEL);
1213 if (!regd)
1214 return -ENOMEM;
1215
1216 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1217
1218 for (i = 0; i < src_regd->n_reg_rules; i++)
1219 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1220 sizeof(struct ieee80211_reg_rule));
1221
1222 *dst_regd = regd;
1223 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001224}
1225
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001226/*
1227 * Return value which can be used by ignore_request() to indicate
1228 * it has been determined we should intersect two regulatory domains
1229 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001230#define REG_INTERSECT 1
1231
Johannes Berg84fa4f42008-10-24 20:32:23 +02001232/* This has the logic which determines when a new request
1233 * should be ignored. */
1234static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
1235 const char *alpha2)
1236{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001237 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001238
1239 assert_cfg80211_lock();
1240
Johannes Berg84fa4f42008-10-24 20:32:23 +02001241 /* All initial requests are respected */
1242 if (!last_request)
1243 return 0;
1244
1245 switch (set_by) {
1246 case REGDOM_SET_BY_INIT:
1247 return -EINVAL;
1248 case REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001249 return -EINVAL;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001250 case REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001251
1252 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1253
Johannes Berg84fa4f42008-10-24 20:32:23 +02001254 if (unlikely(!is_an_alpha2(alpha2)))
1255 return -EINVAL;
1256 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001257 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001258 /*
1259 * Two cards with two APs claiming different
1260 * different Country IE alpha2s. We could
1261 * intersect them, but that seems unlikely
1262 * to be correct. Reject second one for now.
1263 */
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001264 if (regdom_changes(alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001265 return -EOPNOTSUPP;
1266 return -EALREADY;
1267 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001268 /*
1269 * Two consecutive Country IE hints on the same wiphy.
1270 * This should be picked up early by the driver/stack
1271 */
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001272 if (WARN_ON(regdom_changes(alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001273 return 0;
1274 return -EALREADY;
1275 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001276 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001277 case REGDOM_SET_BY_DRIVER:
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001278 if (last_request->initiator == REGDOM_SET_BY_CORE) {
1279 if (is_old_static_regdom(cfg80211_regdomain))
1280 return 0;
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001281 if (regdom_changes(alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001282 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001283 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001284 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001285
1286 /*
1287 * This would happen if you unplug and plug your card
1288 * back in or if you add a new device for which the previously
1289 * loaded card also agrees on the regulatory domain.
1290 */
1291 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001292 !regdom_changes(alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001293 return -EALREADY;
1294
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001295 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001296 case REGDOM_SET_BY_USER:
Johannes Berg84fa4f42008-10-24 20:32:23 +02001297 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001298 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001299 /*
1300 * If the user knows better the user should set the regdom
1301 * to their country before the IE is picked up
1302 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001303 if (last_request->initiator == REGDOM_SET_BY_USER &&
1304 last_request->intersect)
1305 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001306 /*
1307 * Process user requests only after previous user/driver/core
1308 * requests have been processed
1309 */
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001310 if (last_request->initiator == REGDOM_SET_BY_CORE ||
1311 last_request->initiator == REGDOM_SET_BY_DRIVER ||
1312 last_request->initiator == REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001313 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001314 return -EAGAIN;
1315 }
1316
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001317 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001318 !regdom_changes(alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001319 return -EALREADY;
1320
Johannes Berg84fa4f42008-10-24 20:32:23 +02001321 return 0;
1322 }
1323
1324 return -EINVAL;
1325}
1326
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001327/**
1328 * __regulatory_hint - hint to the wireless core a regulatory domain
1329 * @wiphy: if the hint comes from country information from an AP, this
1330 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001331 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001332 *
1333 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001334 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001335 *
1336 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1337 * already been set or other standard error codes.
1338 *
1339 * Caller must hold &cfg80211_mutex
1340 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001341static int __regulatory_hint(struct wiphy *wiphy,
1342 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001343{
1344 struct regulatory_request *request;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001345 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001346 int r = 0;
1347
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001348 assert_cfg80211_lock();
1349
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001350 r = ignore_request(wiphy,
1351 pending_request->initiator,
1352 pending_request->alpha2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001353
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001354 if (r == REG_INTERSECT) {
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001355 if (pending_request->initiator == REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001356 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1357 if (r)
1358 return r;
1359 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001360 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001361 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001362 /*
1363 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001364 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001365 * wiphy
1366 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001367 if (r == -EALREADY &&
1368 pending_request->initiator == REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001369 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1370 if (r)
1371 return r;
1372 r = -EALREADY;
1373 goto new_request;
1374 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001375 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001376 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001377
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001378new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001379 request = kzalloc(sizeof(struct regulatory_request),
1380 GFP_KERNEL);
1381 if (!request)
1382 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001383
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001384 request->alpha2[0] = pending_request->alpha2[0];
1385 request->alpha2[1] = pending_request->alpha2[1];
1386 request->initiator = pending_request->initiator;
1387 request->wiphy_idx = pending_request->wiphy_idx;
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001388 request->intersect = intersect;
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001389 request->country_ie_checksum = pending_request->country_ie_checksum;
1390 request->country_ie_env = pending_request->country_ie_env;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001391
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001392 kfree(last_request);
1393 last_request = request;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001394
1395 /* When r == REG_INTERSECT we do need to call CRDA */
1396 if (r < 0)
1397 return r;
1398
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001399 /*
1400 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1401 * AND if CRDA is NOT present nothing will happen, if someone
1402 * wants to bother with 11d with OLD_REG you can add a timer.
1403 * If after x amount of time nothing happens you can call:
1404 *
1405 * return set_regdom(country_ie_regdomain);
1406 *
1407 * to intersect with the static rd
1408 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001409 return call_crda(request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001410}
1411
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001412/* This currently only processes user and driver regulatory hints */
1413static int reg_process_hint(struct regulatory_request *reg_request)
1414{
1415 int r = 0;
1416 struct wiphy *wiphy = NULL;
1417
1418 BUG_ON(!reg_request->alpha2);
1419
1420 mutex_lock(&cfg80211_mutex);
1421
1422 if (wiphy_idx_valid(reg_request->wiphy_idx))
1423 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1424
1425 if (reg_request->initiator == REGDOM_SET_BY_DRIVER &&
1426 !wiphy) {
1427 r = -ENODEV;
1428 goto out;
1429 }
1430
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001431 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001432 /* This is required so that the orig_* parameters are saved */
1433 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1434 wiphy_update_regulatory(wiphy, reg_request->initiator);
1435out:
1436 mutex_unlock(&cfg80211_mutex);
1437
1438 if (r == -EALREADY)
1439 r = 0;
1440
1441 return r;
1442}
1443
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001444/* Processes regulatory hints, this is all the REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001445static void reg_process_pending_hints(void)
1446 {
1447 struct regulatory_request *reg_request;
1448 int r;
1449
1450 spin_lock(&reg_requests_lock);
1451 while (!list_empty(&reg_requests_list)) {
1452 reg_request = list_first_entry(&reg_requests_list,
1453 struct regulatory_request,
1454 list);
1455 list_del_init(&reg_request->list);
1456 spin_unlock(&reg_requests_lock);
1457
1458 r = reg_process_hint(reg_request);
1459#ifdef CONFIG_CFG80211_REG_DEBUG
1460 if (r && (reg_request->initiator == REGDOM_SET_BY_DRIVER ||
1461 reg_request->initiator == REGDOM_SET_BY_COUNTRY_IE))
1462 printk(KERN_ERR "cfg80211: wiphy_idx %d sent a "
1463 "regulatory hint for %c%c but now has "
1464 "gone fishing, ignoring request\n",
1465 reg_request->wiphy_idx,
1466 reg_request->alpha2[0],
1467 reg_request->alpha2[1]);
1468#endif
1469 kfree(reg_request);
1470 spin_lock(&reg_requests_lock);
1471 }
1472 spin_unlock(&reg_requests_lock);
1473}
1474
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001475/* Processes beacon hints -- this has nothing to do with country IEs */
1476static void reg_process_pending_beacon_hints(void)
1477{
1478 struct cfg80211_registered_device *drv;
1479 struct reg_beacon *pending_beacon, *tmp;
1480
1481 mutex_lock(&cfg80211_mutex);
1482
1483 /* This goes through the _pending_ beacon list */
1484 spin_lock_bh(&reg_pending_beacons_lock);
1485
1486 if (list_empty(&reg_pending_beacons)) {
1487 spin_unlock_bh(&reg_pending_beacons_lock);
1488 goto out;
1489 }
1490
1491 list_for_each_entry_safe(pending_beacon, tmp,
1492 &reg_pending_beacons, list) {
1493
1494 list_del_init(&pending_beacon->list);
1495
1496 /* Applies the beacon hint to current wiphys */
1497 list_for_each_entry(drv, &cfg80211_drv_list, list)
1498 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1499
1500 /* Remembers the beacon hint for new wiphys or reg changes */
1501 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1502 }
1503
1504 spin_unlock_bh(&reg_pending_beacons_lock);
1505out:
1506 mutex_unlock(&cfg80211_mutex);
1507}
1508
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001509static void reg_todo(struct work_struct *work)
1510{
1511 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001512 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001513}
1514
1515static DECLARE_WORK(reg_work, reg_todo);
1516
1517static void queue_regulatory_request(struct regulatory_request *request)
1518{
1519 spin_lock(&reg_requests_lock);
1520 list_add_tail(&request->list, &reg_requests_list);
1521 spin_unlock(&reg_requests_lock);
1522
1523 schedule_work(&reg_work);
1524}
1525
1526/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001527static int regulatory_hint_core(const char *alpha2)
1528{
1529 struct regulatory_request *request;
1530
1531 BUG_ON(last_request);
1532
1533 request = kzalloc(sizeof(struct regulatory_request),
1534 GFP_KERNEL);
1535 if (!request)
1536 return -ENOMEM;
1537
1538 request->alpha2[0] = alpha2[0];
1539 request->alpha2[1] = alpha2[1];
1540 request->initiator = REGDOM_SET_BY_CORE;
1541
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001542 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001543
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001544 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001545}
1546
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001547/* User hints */
1548int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001549{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001550 struct regulatory_request *request;
1551
Johannes Bergbe3d4812008-10-24 20:32:21 +02001552 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001553
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001554 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1555 if (!request)
1556 return -ENOMEM;
1557
1558 request->wiphy_idx = WIPHY_IDX_STALE;
1559 request->alpha2[0] = alpha2[0];
1560 request->alpha2[1] = alpha2[1];
1561 request->initiator = REGDOM_SET_BY_USER,
1562
1563 queue_regulatory_request(request);
1564
1565 return 0;
1566}
1567
1568/* Driver hints */
1569int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1570{
1571 struct regulatory_request *request;
1572
1573 BUG_ON(!alpha2);
1574 BUG_ON(!wiphy);
1575
1576 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1577 if (!request)
1578 return -ENOMEM;
1579
1580 request->wiphy_idx = get_wiphy_idx(wiphy);
1581
1582 /* Must have registered wiphy first */
1583 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1584
1585 request->alpha2[0] = alpha2[0];
1586 request->alpha2[1] = alpha2[1];
1587 request->initiator = REGDOM_SET_BY_DRIVER;
1588
1589 queue_regulatory_request(request);
1590
1591 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001592}
1593EXPORT_SYMBOL(regulatory_hint);
1594
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001595static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1596 u32 country_ie_checksum)
1597{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001598 struct wiphy *request_wiphy;
1599
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001600 assert_cfg80211_lock();
1601
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001602 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1603
1604 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001605 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001606
1607 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001608 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001609 /*
1610 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001611 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001612 * on the device
1613 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001614 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1615 return true;
1616 return false;
1617}
1618
1619void regulatory_hint_11d(struct wiphy *wiphy,
1620 u8 *country_ie,
1621 u8 country_ie_len)
1622{
1623 struct ieee80211_regdomain *rd = NULL;
1624 char alpha2[2];
1625 u32 checksum = 0;
1626 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001627 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001628
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001629 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001630
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001631 if (unlikely(!last_request)) {
1632 mutex_unlock(&cfg80211_mutex);
1633 return;
1634 }
1635
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001636 /* IE len must be evenly divisible by 2 */
1637 if (country_ie_len & 0x01)
1638 goto out;
1639
1640 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1641 goto out;
1642
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001643 /*
1644 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001645 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001646 * we were able to process the last regulatory_hint_11d() call
1647 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001648 if (country_ie_regdomain)
1649 goto out;
1650
1651 alpha2[0] = country_ie[0];
1652 alpha2[1] = country_ie[1];
1653
1654 if (country_ie[2] == 'I')
1655 env = ENVIRON_INDOOR;
1656 else if (country_ie[2] == 'O')
1657 env = ENVIRON_OUTDOOR;
1658
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001659 /*
1660 * We will run this for *every* beacon processed for the BSSID, so
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001661 * we optimize an early check to exit out early if we don't have to
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001662 * do anything
1663 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001664 if (likely(wiphy_idx_valid(last_request->wiphy_idx))) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001665 struct cfg80211_registered_device *drv_last_ie;
1666
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001667 drv_last_ie =
1668 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001669
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001670 /*
1671 * Lets keep this simple -- we trust the first AP
1672 * after we intersect with CRDA
1673 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001674 if (likely(&drv_last_ie->wiphy == wiphy)) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001675 /*
1676 * Ignore IEs coming in on this wiphy with
1677 * the same alpha2 and environment cap
1678 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001679 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1680 alpha2) &&
1681 env == drv_last_ie->env)) {
1682 goto out;
1683 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001684 /*
1685 * the wiphy moved on to another BSSID or the AP
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001686 * was reconfigured. XXX: We need to deal with the
1687 * case where the user suspends and goes to goes
1688 * to another country, and then gets IEs from an
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001689 * AP with different settings
1690 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001691 goto out;
1692 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001693 /*
1694 * Ignore IEs coming in on two separate wiphys with
1695 * the same alpha2 and environment cap
1696 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001697 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1698 alpha2) &&
1699 env == drv_last_ie->env)) {
1700 goto out;
1701 }
1702 /* We could potentially intersect though */
1703 goto out;
1704 }
1705 }
1706
1707 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1708 if (!rd)
1709 goto out;
1710
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001711 /*
1712 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001713 * the future when we want to add suspend/resume support and having
1714 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001715 * move to another AP. Right now we just trust the first AP.
1716 *
1717 * If we hit this before we add this support we want to be informed of
1718 * it as it would indicate a mistake in the current design
1719 */
1720 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001721 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001722
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001723 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1724 if (!request)
1725 goto free_rd_out;
1726
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001727 /*
1728 * We keep this around for when CRDA comes back with a response so
1729 * we can intersect with that
1730 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001731 country_ie_regdomain = rd;
1732
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001733 request->wiphy_idx = get_wiphy_idx(wiphy);
1734 request->alpha2[0] = rd->alpha2[0];
1735 request->alpha2[1] = rd->alpha2[1];
1736 request->initiator = REGDOM_SET_BY_COUNTRY_IE;
1737 request->country_ie_checksum = checksum;
1738 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001739
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001740 mutex_unlock(&cfg80211_mutex);
1741
1742 queue_regulatory_request(request);
1743
1744 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001745
1746free_rd_out:
1747 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001748out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001749 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001750}
1751EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001752
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001753static bool freq_is_chan_12_13_14(u16 freq)
1754{
1755 if (freq == ieee80211_channel_to_frequency(12) ||
1756 freq == ieee80211_channel_to_frequency(13) ||
1757 freq == ieee80211_channel_to_frequency(14))
1758 return true;
1759 return false;
1760}
1761
1762int regulatory_hint_found_beacon(struct wiphy *wiphy,
1763 struct ieee80211_channel *beacon_chan,
1764 gfp_t gfp)
1765{
1766 struct reg_beacon *reg_beacon;
1767
1768 if (likely((beacon_chan->beacon_found ||
1769 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1770 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1771 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1772 return 0;
1773
1774 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1775 if (!reg_beacon)
1776 return -ENOMEM;
1777
1778#ifdef CONFIG_CFG80211_REG_DEBUG
1779 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1780 "frequency: %d MHz (Ch %d) on %s\n",
1781 beacon_chan->center_freq,
1782 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1783 wiphy_name(wiphy));
1784#endif
1785 memcpy(&reg_beacon->chan, beacon_chan,
1786 sizeof(struct ieee80211_channel));
1787
1788
1789 /*
1790 * Since we can be called from BH or and non-BH context
1791 * we must use spin_lock_bh()
1792 */
1793 spin_lock_bh(&reg_pending_beacons_lock);
1794 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1795 spin_unlock_bh(&reg_pending_beacons_lock);
1796
1797 schedule_work(&reg_work);
1798
1799 return 0;
1800}
1801
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001802static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001803{
1804 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001805 const struct ieee80211_reg_rule *reg_rule = NULL;
1806 const struct ieee80211_freq_range *freq_range = NULL;
1807 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001808
1809 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1810 "(max_antenna_gain, max_eirp)\n");
1811
1812 for (i = 0; i < rd->n_reg_rules; i++) {
1813 reg_rule = &rd->reg_rules[i];
1814 freq_range = &reg_rule->freq_range;
1815 power_rule = &reg_rule->power_rule;
1816
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001817 /*
1818 * There may not be documentation for max antenna gain
1819 * in certain regions
1820 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001821 if (power_rule->max_antenna_gain)
1822 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1823 "(%d mBi, %d mBm)\n",
1824 freq_range->start_freq_khz,
1825 freq_range->end_freq_khz,
1826 freq_range->max_bandwidth_khz,
1827 power_rule->max_antenna_gain,
1828 power_rule->max_eirp);
1829 else
1830 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1831 "(N/A, %d mBm)\n",
1832 freq_range->start_freq_khz,
1833 freq_range->end_freq_khz,
1834 freq_range->max_bandwidth_khz,
1835 power_rule->max_eirp);
1836 }
1837}
1838
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001839static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001840{
1841
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001842 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001843
1844 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001845 struct cfg80211_registered_device *drv;
1846 drv = cfg80211_drv_by_wiphy_idx(
1847 last_request->wiphy_idx);
1848 if (drv) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001849 printk(KERN_INFO "cfg80211: Current regulatory "
1850 "domain updated by AP to: %c%c\n",
1851 drv->country_ie_alpha2[0],
1852 drv->country_ie_alpha2[1]);
1853 } else
1854 printk(KERN_INFO "cfg80211: Current regulatory "
1855 "domain intersected: \n");
1856 } else
1857 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001858 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001859 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001860 printk(KERN_INFO "cfg80211: World regulatory "
1861 "domain updated:\n");
1862 else {
1863 if (is_unknown_alpha2(rd->alpha2))
1864 printk(KERN_INFO "cfg80211: Regulatory domain "
1865 "changed to driver built-in settings "
1866 "(unknown country)\n");
1867 else
1868 printk(KERN_INFO "cfg80211: Regulatory domain "
1869 "changed to country: %c%c\n",
1870 rd->alpha2[0], rd->alpha2[1]);
1871 }
1872 print_rd_rules(rd);
1873}
1874
Johannes Berg2df78162008-10-28 16:49:41 +01001875static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001876{
1877 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1878 rd->alpha2[0], rd->alpha2[1]);
1879 print_rd_rules(rd);
1880}
1881
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001882#ifdef CONFIG_CFG80211_REG_DEBUG
1883static void reg_country_ie_process_debug(
1884 const struct ieee80211_regdomain *rd,
1885 const struct ieee80211_regdomain *country_ie_regdomain,
1886 const struct ieee80211_regdomain *intersected_rd)
1887{
1888 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1889 print_regdomain_info(country_ie_regdomain);
1890 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1891 print_regdomain_info(rd);
1892 if (intersected_rd) {
1893 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1894 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08001895 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001896 return;
1897 }
1898 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1899}
1900#else
1901static inline void reg_country_ie_process_debug(
1902 const struct ieee80211_regdomain *rd,
1903 const struct ieee80211_regdomain *country_ie_regdomain,
1904 const struct ieee80211_regdomain *intersected_rd)
1905{
1906}
1907#endif
1908
Johannes Bergd2372b32008-10-24 20:32:20 +02001909/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001910static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001911{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001912 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001913 struct cfg80211_registered_device *drv = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001914 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001915 /* Some basic sanity checks first */
1916
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001917 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001918 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001919 return -EINVAL;
1920 update_world_regdomain(rd);
1921 return 0;
1922 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001923
1924 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1925 !is_unknown_alpha2(rd->alpha2))
1926 return -EINVAL;
1927
Johannes Bergf6037d02008-10-21 11:01:33 +02001928 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001929 return -EINVAL;
1930
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001931 /*
1932 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001933 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001934 * and the pending request came in from a country IE
1935 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001936 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001937 /*
1938 * If someone else asked us to change the rd lets only bother
1939 * checking if the alpha2 changes if CRDA was already called
1940 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001941 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001942 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001943 return -EINVAL;
1944 }
1945
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001946 /*
1947 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001948 * and finally inform them of what we have done, in case they want
1949 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001950 * internal EEPROM data
1951 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001952
Johannes Bergf6037d02008-10-21 11:01:33 +02001953 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001954 return -EINVAL;
1955
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001956 if (!is_valid_rd(rd)) {
1957 printk(KERN_ERR "cfg80211: Invalid "
1958 "regulatory domain detected:\n");
1959 print_regdomain_info(rd);
1960 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001961 }
1962
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001963 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1964
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001965 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001966 int r;
1967
1968 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1969 reset_regdomains();
1970 cfg80211_regdomain = rd;
1971 return 0;
1972 }
1973
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001974 /*
1975 * For a driver hint, lets copy the regulatory domain the
1976 * driver wanted to the wiphy to deal with conflicts
1977 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001978
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001979 BUG_ON(request_wiphy->regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001980
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001981 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001982 if (r)
1983 return r;
1984
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001985 reset_regdomains();
1986 cfg80211_regdomain = rd;
1987 return 0;
1988 }
1989
1990 /* Intersection requires a bit more work */
1991
1992 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1993
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001994 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1995 if (!intersected_rd)
1996 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001997
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001998 /*
1999 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002000 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002001 * domain we keep it for its private use
2002 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002003 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002004 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002005 else
2006 kfree(rd);
2007
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002008 rd = NULL;
2009
2010 reset_regdomains();
2011 cfg80211_regdomain = intersected_rd;
2012
2013 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002014 }
2015
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002016 /*
2017 * Country IE requests are handled a bit differently, we intersect
2018 * the country IE rd with what CRDA believes that country should have
2019 */
2020
2021 BUG_ON(!country_ie_regdomain);
2022
2023 if (rd != country_ie_regdomain) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002024 /*
2025 * Intersect what CRDA returned and our what we
2026 * had built from the Country IE received
2027 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002028
2029 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2030
2031 reg_country_ie_process_debug(rd, country_ie_regdomain,
2032 intersected_rd);
2033
2034 kfree(country_ie_regdomain);
2035 country_ie_regdomain = NULL;
2036 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002037 /*
2038 * This would happen when CRDA was not present and
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002039 * OLD_REGULATORY was enabled. We intersect our Country
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002040 * IE rd and what was set on cfg80211 originally
2041 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002042 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2043 }
2044
2045 if (!intersected_rd)
2046 return -EINVAL;
2047
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002048 drv = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002049
2050 drv->country_ie_alpha2[0] = rd->alpha2[0];
2051 drv->country_ie_alpha2[1] = rd->alpha2[1];
2052 drv->env = last_request->country_ie_env;
2053
2054 BUG_ON(intersected_rd == rd);
2055
2056 kfree(rd);
2057 rd = NULL;
2058
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002059 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002060 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002061
2062 return 0;
2063}
2064
2065
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002066/*
2067 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002068 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002069 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2070 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002071int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002072{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002073 int r;
2074
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002075 assert_cfg80211_lock();
2076
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002077 /* Note that this doesn't update the wiphys, this is done below */
2078 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002079 if (r) {
2080 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002081 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002082 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002083
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002084 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002085 if (!last_request->intersect)
2086 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002087
2088 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002089 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002090
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002091 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002092
2093 return r;
2094}
2095
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002096/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002097void reg_device_remove(struct wiphy *wiphy)
2098{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002099 struct wiphy *request_wiphy;
2100
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002101 assert_cfg80211_lock();
2102
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002103 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2104
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002105 kfree(wiphy->regd);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002106 if (!last_request || !request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002107 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002108 if (request_wiphy != wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002109 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002110 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002111 last_request->country_ie_env = ENVIRON_ANY;
2112}
2113
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002114int regulatory_init(void)
2115{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002116 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002117
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002118 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2119 if (IS_ERR(reg_pdev))
2120 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002121
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002122 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002123 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002124
Johannes Berg734366d2008-09-15 10:56:48 +02002125#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002126 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002127
Johannes Berg942b25c2008-09-15 11:26:47 +02002128 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02002129 print_regdomain_info(cfg80211_regdomain);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002130 /*
2131 * The old code still requests for a new regdomain and if
Johannes Berg734366d2008-09-15 10:56:48 +02002132 * you have CRDA you get it updated, otherwise you get
2133 * stuck with the static values. We ignore "EU" code as
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002134 * that is not a valid ISO / IEC 3166 alpha2
2135 */
Johannes Bergac9440a2008-10-21 11:08:27 +02002136 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002137 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002138#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002139 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002140
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05002141 err = regulatory_hint_core("00");
Johannes Berg734366d2008-09-15 10:56:48 +02002142#endif
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002143 if (err) {
2144 if (err == -ENOMEM)
2145 return err;
2146 /*
2147 * N.B. kobject_uevent_env() can fail mainly for when we're out
2148 * memory which is handled and propagated appropriately above
2149 * but it can also fail during a netlink_broadcast() or during
2150 * early boot for call_usermodehelper(). For now treat these
2151 * errors as non-fatal.
2152 */
2153 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2154 "to call CRDA during init");
2155#ifdef CONFIG_CFG80211_REG_DEBUG
2156 /* We want to find out exactly why when debugging */
2157 WARN_ON(err);
2158#endif
2159 }
Johannes Berg734366d2008-09-15 10:56:48 +02002160
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002161 return 0;
2162}
2163
2164void regulatory_exit(void)
2165{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002166 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002167 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002168
2169 cancel_work_sync(&reg_work);
2170
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002171 mutex_lock(&cfg80211_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002172
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002173 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002174
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002175 kfree(country_ie_regdomain);
2176 country_ie_regdomain = NULL;
2177
Johannes Bergf6037d02008-10-21 11:01:33 +02002178 kfree(last_request);
2179
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002180 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002181
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002182 spin_lock_bh(&reg_pending_beacons_lock);
2183 if (!list_empty(&reg_pending_beacons)) {
2184 list_for_each_entry_safe(reg_beacon, btmp,
2185 &reg_pending_beacons, list) {
2186 list_del(&reg_beacon->list);
2187 kfree(reg_beacon);
2188 }
2189 }
2190 spin_unlock_bh(&reg_pending_beacons_lock);
2191
2192 if (!list_empty(&reg_beacon_list)) {
2193 list_for_each_entry_safe(reg_beacon, btmp,
2194 &reg_beacon_list, list) {
2195 list_del(&reg_beacon->list);
2196 kfree(reg_beacon);
2197 }
2198 }
2199
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002200 spin_lock(&reg_requests_lock);
2201 if (!list_empty(&reg_requests_list)) {
2202 list_for_each_entry_safe(reg_request, tmp,
2203 &reg_requests_list, list) {
2204 list_del(&reg_request->list);
2205 kfree(reg_request);
2206 }
2207 }
2208 spin_unlock(&reg_requests_lock);
2209
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002210 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002211}