blob: 85c2c31721ec0f269b3b9a54f6034ba8c597b75c [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. Rodriguezfe33eb32009-02-21 00:04:30 -050071static LIST_HEAD(reg_requests_list);
72static spinlock_t reg_requests_lock;
73
Johannes Berg734366d2008-09-15 10:56:48 +020074/* We keep a static world regulatory domain in case of the absence of CRDA */
75static const struct ieee80211_regdomain world_regdom = {
76 .n_reg_rules = 1,
77 .alpha2 = "00",
78 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -050079 /* IEEE 802.11b/g, channels 1..11 */
80 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Johannes Berg734366d2008-09-15 10:56:48 +020081 }
82};
83
Johannes Berga3d2eaf2008-09-15 11:10:52 +020084static const struct ieee80211_regdomain *cfg80211_world_regdom =
85 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +020086
87#ifdef CONFIG_WIRELESS_OLD_REGULATORY
88static char *ieee80211_regdom = "US";
89module_param(ieee80211_regdom, charp, 0444);
90MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
91
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050092/*
93 * We assume 40 MHz bandwidth for the old regulatory work.
Johannes Berg734366d2008-09-15 10:56:48 +020094 * We make emphasis we are using the exact same frequencies
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050095 * as before
96 */
Johannes Berg734366d2008-09-15 10:56:48 +020097
98static const struct ieee80211_regdomain us_regdom = {
99 .n_reg_rules = 6,
100 .alpha2 = "US",
101 .reg_rules = {
102 /* IEEE 802.11b/g, channels 1..11 */
103 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
104 /* IEEE 802.11a, channel 36 */
105 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
106 /* IEEE 802.11a, channel 40 */
107 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
108 /* IEEE 802.11a, channel 44 */
109 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
110 /* IEEE 802.11a, channels 48..64 */
111 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
112 /* IEEE 802.11a, channels 149..165, outdoor */
113 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
114 }
115};
116
117static const struct ieee80211_regdomain jp_regdom = {
118 .n_reg_rules = 3,
119 .alpha2 = "JP",
120 .reg_rules = {
121 /* IEEE 802.11b/g, channels 1..14 */
122 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
123 /* IEEE 802.11a, channels 34..48 */
124 REG_RULE(5170-10, 5240+10, 40, 6, 20,
125 NL80211_RRF_PASSIVE_SCAN),
126 /* IEEE 802.11a, channels 52..64 */
127 REG_RULE(5260-10, 5320+10, 40, 6, 20,
128 NL80211_RRF_NO_IBSS |
129 NL80211_RRF_DFS),
130 }
131};
132
133static const struct ieee80211_regdomain eu_regdom = {
134 .n_reg_rules = 6,
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500135 /*
136 * This alpha2 is bogus, we leave it here just for stupid
137 * backward compatibility
138 */
Johannes Berg734366d2008-09-15 10:56:48 +0200139 .alpha2 = "EU",
140 .reg_rules = {
141 /* IEEE 802.11b/g, channels 1..13 */
142 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
143 /* IEEE 802.11a, channel 36 */
144 REG_RULE(5180-10, 5180+10, 40, 6, 23,
145 NL80211_RRF_PASSIVE_SCAN),
146 /* IEEE 802.11a, channel 40 */
147 REG_RULE(5200-10, 5200+10, 40, 6, 23,
148 NL80211_RRF_PASSIVE_SCAN),
149 /* IEEE 802.11a, channel 44 */
150 REG_RULE(5220-10, 5220+10, 40, 6, 23,
151 NL80211_RRF_PASSIVE_SCAN),
152 /* IEEE 802.11a, channels 48..64 */
153 REG_RULE(5240-10, 5320+10, 40, 6, 20,
154 NL80211_RRF_NO_IBSS |
155 NL80211_RRF_DFS),
156 /* IEEE 802.11a, channels 100..140 */
157 REG_RULE(5500-10, 5700+10, 40, 6, 30,
158 NL80211_RRF_NO_IBSS |
159 NL80211_RRF_DFS),
160 }
161};
162
163static const struct ieee80211_regdomain *static_regdom(char *alpha2)
164{
165 if (alpha2[0] == 'U' && alpha2[1] == 'S')
166 return &us_regdom;
167 if (alpha2[0] == 'J' && alpha2[1] == 'P')
168 return &jp_regdom;
169 if (alpha2[0] == 'E' && alpha2[1] == 'U')
170 return &eu_regdom;
171 /* Default, as per the old rules */
172 return &us_regdom;
173}
174
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200175static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200176{
177 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
178 return true;
179 return false;
180}
Johannes Berg734366d2008-09-15 10:56:48 +0200181#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200182static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
183{
184 return false;
185}
186#endif
187
Johannes Berg734366d2008-09-15 10:56:48 +0200188static void reset_regdomains(void)
189{
Johannes Berg942b25c2008-09-15 11:26:47 +0200190 /* avoid freeing static information or freeing something twice */
191 if (cfg80211_regdomain == cfg80211_world_regdom)
192 cfg80211_regdomain = NULL;
193 if (cfg80211_world_regdom == &world_regdom)
194 cfg80211_world_regdom = NULL;
195 if (cfg80211_regdomain == &world_regdom)
196 cfg80211_regdomain = NULL;
197 if (is_old_static_regdom(cfg80211_regdomain))
198 cfg80211_regdomain = NULL;
199
200 kfree(cfg80211_regdomain);
201 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200202
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200203 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200204 cfg80211_regdomain = NULL;
205}
206
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500207/*
208 * Dynamic world regulatory domain requested by the wireless
209 * core upon initialization
210 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200211static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200212{
Johannes Bergf6037d02008-10-21 11:01:33 +0200213 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200214
215 reset_regdomains();
216
217 cfg80211_world_regdom = rd;
218 cfg80211_regdomain = rd;
219}
Johannes Berg734366d2008-09-15 10:56:48 +0200220
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200221bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100222{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700223 if (!alpha2)
224 return false;
225 if (alpha2[0] == '0' && alpha2[1] == '0')
226 return true;
227 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100228}
229
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200230static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700231{
232 if (!alpha2)
233 return false;
234 if (alpha2[0] != 0 && alpha2[1] != 0)
235 return true;
236 return false;
237}
Johannes Berg8318d782008-01-24 19:38:38 +0100238
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700239static bool is_alpha_upper(char letter)
240{
241 /* ASCII A - Z */
242 if (letter >= 65 && letter <= 90)
243 return true;
244 return false;
245}
246
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200247static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700248{
249 if (!alpha2)
250 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500251 /*
252 * Special case where regulatory domain was built by driver
253 * but a specific alpha2 cannot be determined
254 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700255 if (alpha2[0] == '9' && alpha2[1] == '9')
256 return true;
257 return false;
258}
259
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800260static bool is_intersected_alpha2(const char *alpha2)
261{
262 if (!alpha2)
263 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500264 /*
265 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800266 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500267 * structures
268 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800269 if (alpha2[0] == '9' && alpha2[1] == '8')
270 return true;
271 return false;
272}
273
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200274static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700275{
276 if (!alpha2)
277 return false;
278 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
279 return true;
280 return false;
281}
282
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200283static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700284{
285 if (!alpha2_x || !alpha2_y)
286 return false;
287 if (alpha2_x[0] == alpha2_y[0] &&
288 alpha2_x[1] == alpha2_y[1])
289 return true;
290 return false;
291}
292
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500293static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700294{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500295 assert_cfg80211_lock();
296
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700297 if (!cfg80211_regdomain)
298 return true;
299 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
300 return false;
301 return true;
302}
303
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800304/**
305 * country_ie_integrity_changes - tells us if the country IE has changed
306 * @checksum: checksum of country IE of fields we are interested in
307 *
308 * If the country IE has not changed you can ignore it safely. This is
309 * useful to determine if two devices are seeing two different country IEs
310 * even on the same alpha2. Note that this will return false if no IE has
311 * been set on the wireless core yet.
312 */
313static bool country_ie_integrity_changes(u32 checksum)
314{
315 /* If no IE has been set then the checksum doesn't change */
316 if (unlikely(!last_request->country_ie_checksum))
317 return false;
318 if (unlikely(last_request->country_ie_checksum != checksum))
319 return true;
320 return false;
321}
322
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500323/*
324 * This lets us keep regulatory code which is updated on a regulatory
325 * basis in userspace.
326 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700327static int call_crda(const char *alpha2)
328{
329 char country_env[9 + 2] = "COUNTRY=";
330 char *envp[] = {
331 country_env,
332 NULL
333 };
334
335 if (!is_world_regdom((char *) alpha2))
336 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
337 alpha2[0], alpha2[1]);
338 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700339 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
340 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700341
342 country_env[8] = alpha2[0];
343 country_env[9] = alpha2[1];
344
345 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
346}
347
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700348/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200349bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700350{
Johannes Bergf6037d02008-10-21 11:01:33 +0200351 if (!last_request)
352 return false;
353
354 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700355}
356
357/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200358static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700359{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200360 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700361 u32 freq_diff;
362
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800363 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700364 return false;
365
366 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
367 return false;
368
369 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
370
Luis R. Rodriguezd71aaf62008-10-30 13:33:52 -0700371 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700372 return false;
373
374 return true;
375}
376
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200377static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700378{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200379 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700380 unsigned int i;
381
382 if (!rd->n_reg_rules)
383 return false;
384
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800385 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
386 return false;
387
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388 for (i = 0; i < rd->n_reg_rules; i++) {
389 reg_rule = &rd->reg_rules[i];
390 if (!is_valid_reg_rule(reg_rule))
391 return false;
392 }
393
394 return true;
395}
396
397/* Returns value in KHz */
398static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
399 u32 freq)
400{
401 unsigned int i;
402 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
403 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
404 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
405 if (start_freq_khz >= freq_range->start_freq_khz &&
406 end_freq_khz <= freq_range->end_freq_khz)
407 return supported_bandwidths[i];
408 }
409 return 0;
410}
411
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800412/**
413 * freq_in_rule_band - tells us if a frequency is in a frequency band
414 * @freq_range: frequency rule we want to query
415 * @freq_khz: frequency we are inquiring about
416 *
417 * This lets us know if a specific frequency rule is or is not relevant to
418 * a specific frequency's band. Bands are device specific and artificial
419 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
420 * safe for now to assume that a frequency rule should not be part of a
421 * frequency's band if the start freq or end freq are off by more than 2 GHz.
422 * This resolution can be lowered and should be considered as we add
423 * regulatory rule support for other "bands".
424 **/
425static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
426 u32 freq_khz)
427{
428#define ONE_GHZ_IN_KHZ 1000000
429 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
430 return true;
431 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
432 return true;
433 return false;
434#undef ONE_GHZ_IN_KHZ
435}
436
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500437/*
438 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800439 * structure has a lot of information which the IE doesn't yet have,
440 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500441 * with our userspace regulatory agent to get lower bounds.
442 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800443static struct ieee80211_regdomain *country_ie_2_rd(
444 u8 *country_ie,
445 u8 country_ie_len,
446 u32 *checksum)
447{
448 struct ieee80211_regdomain *rd = NULL;
449 unsigned int i = 0;
450 char alpha2[2];
451 u32 flags = 0;
452 u32 num_rules = 0, size_of_regd = 0;
453 u8 *triplets_start = NULL;
454 u8 len_at_triplet = 0;
455 /* the last channel we have registered in a subband (triplet) */
456 int last_sub_max_channel = 0;
457
458 *checksum = 0xDEADBEEF;
459
460 /* Country IE requirements */
461 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
462 country_ie_len & 0x01);
463
464 alpha2[0] = country_ie[0];
465 alpha2[1] = country_ie[1];
466
467 /*
468 * Third octet can be:
469 * 'I' - Indoor
470 * 'O' - Outdoor
471 *
472 * anything else we assume is no restrictions
473 */
474 if (country_ie[2] == 'I')
475 flags = NL80211_RRF_NO_OUTDOOR;
476 else if (country_ie[2] == 'O')
477 flags = NL80211_RRF_NO_INDOOR;
478
479 country_ie += 3;
480 country_ie_len -= 3;
481
482 triplets_start = country_ie;
483 len_at_triplet = country_ie_len;
484
485 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
486
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500487 /*
488 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800489 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500490 * for each channel subband
491 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800492 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800493 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800494 struct ieee80211_country_ie_triplet *triplet =
495 (struct ieee80211_country_ie_triplet *) country_ie;
496 int cur_sub_max_channel = 0, cur_channel = 0;
497
498 if (triplet->ext.reg_extension_id >=
499 IEEE80211_COUNTRY_EXTENSION_ID) {
500 country_ie += 3;
501 country_ie_len -= 3;
502 continue;
503 }
504
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800505 /* 2 GHz */
506 if (triplet->chans.first_channel <= 14)
507 end_channel = triplet->chans.first_channel +
508 triplet->chans.num_channels;
509 else
510 /*
511 * 5 GHz -- For example in country IEs if the first
512 * channel given is 36 and the number of channels is 4
513 * then the individual channel numbers defined for the
514 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
515 * and not 36, 37, 38, 39.
516 *
517 * See: http://tinyurl.com/11d-clarification
518 */
519 end_channel = triplet->chans.first_channel +
520 (4 * (triplet->chans.num_channels - 1));
521
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800522 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800523 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800524
525 /* Basic sanity check */
526 if (cur_sub_max_channel < cur_channel)
527 return NULL;
528
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500529 /*
530 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800531 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500532 * increasing
533 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800534 if (last_sub_max_channel) {
535 if (cur_channel <= last_sub_max_channel)
536 return NULL;
537 if (cur_sub_max_channel <= last_sub_max_channel)
538 return NULL;
539 }
540
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500541 /*
542 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800543 * we can throw ext triplets as part of this soup,
544 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500545 * don't support them
546 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800547 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
548 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
549 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
550
551 last_sub_max_channel = cur_sub_max_channel;
552
553 country_ie += 3;
554 country_ie_len -= 3;
555 num_rules++;
556
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500557 /*
558 * Note: this is not a IEEE requirement but
559 * simply a memory requirement
560 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800561 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
562 return NULL;
563 }
564
565 country_ie = triplets_start;
566 country_ie_len = len_at_triplet;
567
568 size_of_regd = sizeof(struct ieee80211_regdomain) +
569 (num_rules * sizeof(struct ieee80211_reg_rule));
570
571 rd = kzalloc(size_of_regd, GFP_KERNEL);
572 if (!rd)
573 return NULL;
574
575 rd->n_reg_rules = num_rules;
576 rd->alpha2[0] = alpha2[0];
577 rd->alpha2[1] = alpha2[1];
578
579 /* This time around we fill in the rd */
580 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800581 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800582 struct ieee80211_country_ie_triplet *triplet =
583 (struct ieee80211_country_ie_triplet *) country_ie;
584 struct ieee80211_reg_rule *reg_rule = NULL;
585 struct ieee80211_freq_range *freq_range = NULL;
586 struct ieee80211_power_rule *power_rule = NULL;
587
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500588 /*
589 * Must parse if dot11RegulatoryClassesRequired is true,
590 * we don't support this yet
591 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800592 if (triplet->ext.reg_extension_id >=
593 IEEE80211_COUNTRY_EXTENSION_ID) {
594 country_ie += 3;
595 country_ie_len -= 3;
596 continue;
597 }
598
599 reg_rule = &rd->reg_rules[i];
600 freq_range = &reg_rule->freq_range;
601 power_rule = &reg_rule->power_rule;
602
603 reg_rule->flags = flags;
604
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800605 /* 2 GHz */
606 if (triplet->chans.first_channel <= 14)
607 end_channel = triplet->chans.first_channel +
608 triplet->chans.num_channels;
609 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800610 end_channel = triplet->chans.first_channel +
611 (4 * (triplet->chans.num_channels - 1));
612
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500613 /*
614 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800615 * the actual band edge, not the center of freq for
616 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500617 * the channels passed
618 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800619 freq_range->start_freq_khz =
620 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
621 triplet->chans.first_channel) - 10);
622 freq_range->end_freq_khz =
623 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800624 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800625
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500626 /*
627 * These are large arbitrary values we use to intersect later.
628 * Increment this if we ever support >= 40 MHz channels
629 * in IEEE 802.11
630 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800631 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
632 power_rule->max_antenna_gain = DBI_TO_MBI(100);
633 power_rule->max_eirp = DBM_TO_MBM(100);
634
635 country_ie += 3;
636 country_ie_len -= 3;
637 i++;
638
639 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
640 }
641
642 return rd;
643}
644
645
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500646/*
647 * Helper for regdom_intersect(), this does the real
648 * mathematical intersection fun
649 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700650static int reg_rules_intersect(
651 const struct ieee80211_reg_rule *rule1,
652 const struct ieee80211_reg_rule *rule2,
653 struct ieee80211_reg_rule *intersected_rule)
654{
655 const struct ieee80211_freq_range *freq_range1, *freq_range2;
656 struct ieee80211_freq_range *freq_range;
657 const struct ieee80211_power_rule *power_rule1, *power_rule2;
658 struct ieee80211_power_rule *power_rule;
659 u32 freq_diff;
660
661 freq_range1 = &rule1->freq_range;
662 freq_range2 = &rule2->freq_range;
663 freq_range = &intersected_rule->freq_range;
664
665 power_rule1 = &rule1->power_rule;
666 power_rule2 = &rule2->power_rule;
667 power_rule = &intersected_rule->power_rule;
668
669 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
670 freq_range2->start_freq_khz);
671 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
672 freq_range2->end_freq_khz);
673 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
674 freq_range2->max_bandwidth_khz);
675
676 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
677 if (freq_range->max_bandwidth_khz > freq_diff)
678 freq_range->max_bandwidth_khz = freq_diff;
679
680 power_rule->max_eirp = min(power_rule1->max_eirp,
681 power_rule2->max_eirp);
682 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
683 power_rule2->max_antenna_gain);
684
685 intersected_rule->flags = (rule1->flags | rule2->flags);
686
687 if (!is_valid_reg_rule(intersected_rule))
688 return -EINVAL;
689
690 return 0;
691}
692
693/**
694 * regdom_intersect - do the intersection between two regulatory domains
695 * @rd1: first regulatory domain
696 * @rd2: second regulatory domain
697 *
698 * Use this function to get the intersection between two regulatory domains.
699 * Once completed we will mark the alpha2 for the rd as intersected, "98",
700 * as no one single alpha2 can represent this regulatory domain.
701 *
702 * Returns a pointer to the regulatory domain structure which will hold the
703 * resulting intersection of rules between rd1 and rd2. We will
704 * kzalloc() this structure for you.
705 */
706static struct ieee80211_regdomain *regdom_intersect(
707 const struct ieee80211_regdomain *rd1,
708 const struct ieee80211_regdomain *rd2)
709{
710 int r, size_of_regd;
711 unsigned int x, y;
712 unsigned int num_rules = 0, rule_idx = 0;
713 const struct ieee80211_reg_rule *rule1, *rule2;
714 struct ieee80211_reg_rule *intersected_rule;
715 struct ieee80211_regdomain *rd;
716 /* This is just a dummy holder to help us count */
717 struct ieee80211_reg_rule irule;
718
719 /* Uses the stack temporarily for counter arithmetic */
720 intersected_rule = &irule;
721
722 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
723
724 if (!rd1 || !rd2)
725 return NULL;
726
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500727 /*
728 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700729 * build them. This is to so we can malloc() and free() a
730 * regdomain once. The reason we use reg_rules_intersect() here
731 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500732 * All rules that do check out OK are valid.
733 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700734
735 for (x = 0; x < rd1->n_reg_rules; x++) {
736 rule1 = &rd1->reg_rules[x];
737 for (y = 0; y < rd2->n_reg_rules; y++) {
738 rule2 = &rd2->reg_rules[y];
739 if (!reg_rules_intersect(rule1, rule2,
740 intersected_rule))
741 num_rules++;
742 memset(intersected_rule, 0,
743 sizeof(struct ieee80211_reg_rule));
744 }
745 }
746
747 if (!num_rules)
748 return NULL;
749
750 size_of_regd = sizeof(struct ieee80211_regdomain) +
751 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
752
753 rd = kzalloc(size_of_regd, GFP_KERNEL);
754 if (!rd)
755 return NULL;
756
757 for (x = 0; x < rd1->n_reg_rules; x++) {
758 rule1 = &rd1->reg_rules[x];
759 for (y = 0; y < rd2->n_reg_rules; y++) {
760 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500761 /*
762 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700763 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500764 * a memcpy()
765 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700766 intersected_rule = &rd->reg_rules[rule_idx];
767 r = reg_rules_intersect(rule1, rule2,
768 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500769 /*
770 * No need to memset here the intersected rule here as
771 * we're not using the stack anymore
772 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700773 if (r)
774 continue;
775 rule_idx++;
776 }
777 }
778
779 if (rule_idx != num_rules) {
780 kfree(rd);
781 return NULL;
782 }
783
784 rd->n_reg_rules = num_rules;
785 rd->alpha2[0] = '9';
786 rd->alpha2[1] = '8';
787
788 return rd;
789}
790
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500791/*
792 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
793 * want to just have the channel structure use these
794 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700795static u32 map_regdom_flags(u32 rd_flags)
796{
797 u32 channel_flags = 0;
798 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
799 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
800 if (rd_flags & NL80211_RRF_NO_IBSS)
801 channel_flags |= IEEE80211_CHAN_NO_IBSS;
802 if (rd_flags & NL80211_RRF_DFS)
803 channel_flags |= IEEE80211_CHAN_RADAR;
804 return channel_flags;
805}
806
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800807static int freq_reg_info_regd(struct wiphy *wiphy,
808 u32 center_freq,
809 u32 *bandwidth,
810 const struct ieee80211_reg_rule **reg_rule,
811 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100812{
813 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800814 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800815 const struct ieee80211_regdomain *regd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700816 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100817
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800818 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800819
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500820 /*
821 * Follow the driver's regulatory domain, if present, unless a country
822 * IE has been processed or a user wants to help complaince further
823 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800824 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez24ed1da2009-01-22 15:05:54 -0800825 last_request->initiator != REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800826 wiphy->regd)
827 regd = wiphy->regd;
828
829 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700830 return -EINVAL;
831
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800832 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700833 const struct ieee80211_reg_rule *rr;
834 const struct ieee80211_freq_range *fr = NULL;
835 const struct ieee80211_power_rule *pr = NULL;
836
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800837 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700838 fr = &rr->freq_range;
839 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800840
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500841 /*
842 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800843 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500844 * not overwrite it once found
845 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800846 if (!band_rule_found)
847 band_rule_found = freq_in_rule_band(fr, center_freq);
848
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700849 max_bandwidth = freq_max_bandwidth(fr, center_freq);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800850
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700851 if (max_bandwidth && *bandwidth <= max_bandwidth) {
852 *reg_rule = rr;
853 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100854 break;
855 }
856 }
857
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800858 if (!band_rule_found)
859 return -ERANGE;
860
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700861 return !max_bandwidth;
862}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800863EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700864
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800865int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800866 const struct ieee80211_reg_rule **reg_rule)
867{
868 return freq_reg_info_regd(wiphy, center_freq,
869 bandwidth, reg_rule, NULL);
870}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700871
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800872static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
873 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700874{
875 int r;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800876 u32 flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700877 u32 max_bandwidth = 0;
878 const struct ieee80211_reg_rule *reg_rule = NULL;
879 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800880 struct ieee80211_supported_band *sband;
881 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500882 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800883
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500884 assert_cfg80211_lock();
885
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500886 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
887
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800888 sband = wiphy->bands[band];
889 BUG_ON(chan_idx >= sband->n_channels);
890 chan = &sband->channels[chan_idx];
891
892 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700893
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800894 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700895 &max_bandwidth, &reg_rule);
896
897 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500898 /*
899 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800900 * with a frequency range on the center_freq's band, since
901 * IEEE-802.11 allows for a country IE to have a subset of the
902 * regulatory information provided in a country we ignore
903 * disabling the channel unless at least one reg rule was
904 * found on the center_freq's band. For details see this
905 * clarification:
906 *
907 * http://tinyurl.com/11d-clarification
908 */
909 if (r == -ERANGE &&
910 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
911#ifdef CONFIG_CFG80211_REG_DEBUG
912 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
913 "intact on %s - no rule found in band on "
914 "Country IE\n",
915 chan->center_freq, wiphy_name(wiphy));
916#endif
917 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500918 /*
919 * In this case we know the country IE has at least one reg rule
920 * for the band so we respect its band definitions
921 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800922#ifdef CONFIG_CFG80211_REG_DEBUG
923 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
924 printk(KERN_DEBUG "cfg80211: Disabling "
925 "channel %d MHz on %s due to "
926 "Country IE\n",
927 chan->center_freq, wiphy_name(wiphy));
928#endif
929 flags |= IEEE80211_CHAN_DISABLED;
930 chan->flags = flags;
931 }
Johannes Berg8318d782008-01-24 19:38:38 +0100932 return;
933 }
934
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700935 power_rule = &reg_rule->power_rule;
936
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800937 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500938 request_wiphy && request_wiphy == wiphy &&
939 request_wiphy->strict_regulatory) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500940 /*
941 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800942 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500943 * settings
944 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800945 chan->flags = chan->orig_flags =
946 map_regdom_flags(reg_rule->flags);
947 chan->max_antenna_gain = chan->orig_mag =
948 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
949 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
950 chan->max_power = chan->orig_mpwr =
951 (int) MBM_TO_DBM(power_rule->max_eirp);
952 return;
953 }
954
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700955 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100956 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700957 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
958 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400959 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700960 chan->max_power = min(chan->orig_mpwr,
961 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400962 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700963 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100964}
965
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800966static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +0100967{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800968 unsigned int i;
969 struct ieee80211_supported_band *sband;
970
971 BUG_ON(!wiphy->bands[band]);
972 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +0100973
974 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800975 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +0100976}
977
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800978static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
979{
980 if (!last_request)
981 return true;
982 if (setby == REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -0800983 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800984 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500985 /*
986 * wiphy->regd will be set once the device has its own
987 * desired regulatory domain set
988 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800989 if (wiphy->strict_regulatory && !wiphy->regd &&
990 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800991 return true;
992 return false;
993}
994
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700995static void update_all_wiphy_regulatory(enum reg_set_by setby)
996{
997 struct cfg80211_registered_device *drv;
998
999 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001000 wiphy_update_regulatory(&drv->wiphy, setby);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001001}
1002
1003void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
Johannes Berg8318d782008-01-24 19:38:38 +01001004{
1005 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001006
1007 if (ignore_reg_update(wiphy, setby))
1008 return;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001009 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001010 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001011 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001012 }
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001013 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001014 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001015}
1016
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001017static void handle_channel_custom(struct wiphy *wiphy,
1018 enum ieee80211_band band,
1019 unsigned int chan_idx,
1020 const struct ieee80211_regdomain *regd)
1021{
1022 int r;
1023 u32 max_bandwidth = 0;
1024 const struct ieee80211_reg_rule *reg_rule = NULL;
1025 const struct ieee80211_power_rule *power_rule = NULL;
1026 struct ieee80211_supported_band *sband;
1027 struct ieee80211_channel *chan;
1028
1029 sband = wiphy->bands[band];
1030 BUG_ON(chan_idx >= sband->n_channels);
1031 chan = &sband->channels[chan_idx];
1032
1033 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1034 &max_bandwidth, &reg_rule, regd);
1035
1036 if (r) {
1037 chan->flags = IEEE80211_CHAN_DISABLED;
1038 return;
1039 }
1040
1041 power_rule = &reg_rule->power_rule;
1042
1043 chan->flags |= map_regdom_flags(reg_rule->flags);
1044 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1045 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1046 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1047}
1048
1049static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1050 const struct ieee80211_regdomain *regd)
1051{
1052 unsigned int i;
1053 struct ieee80211_supported_band *sband;
1054
1055 BUG_ON(!wiphy->bands[band]);
1056 sband = wiphy->bands[band];
1057
1058 for (i = 0; i < sband->n_channels; i++)
1059 handle_channel_custom(wiphy, band, i, regd);
1060}
1061
1062/* Used by drivers prior to wiphy registration */
1063void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1064 const struct ieee80211_regdomain *regd)
1065{
1066 enum ieee80211_band band;
1067 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1068 if (wiphy->bands[band])
1069 handle_band_custom(wiphy, band, regd);
1070 }
1071}
1072EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1073
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001074static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1075 const struct ieee80211_regdomain *src_regd)
1076{
1077 struct ieee80211_regdomain *regd;
1078 int size_of_regd = 0;
1079 unsigned int i;
1080
1081 size_of_regd = sizeof(struct ieee80211_regdomain) +
1082 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1083
1084 regd = kzalloc(size_of_regd, GFP_KERNEL);
1085 if (!regd)
1086 return -ENOMEM;
1087
1088 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1089
1090 for (i = 0; i < src_regd->n_reg_rules; i++)
1091 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1092 sizeof(struct ieee80211_reg_rule));
1093
1094 *dst_regd = regd;
1095 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001096}
1097
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001098/*
1099 * Return value which can be used by ignore_request() to indicate
1100 * it has been determined we should intersect two regulatory domains
1101 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001102#define REG_INTERSECT 1
1103
Johannes Berg84fa4f42008-10-24 20:32:23 +02001104/* This has the logic which determines when a new request
1105 * should be ignored. */
1106static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
1107 const char *alpha2)
1108{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001109 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001110
1111 assert_cfg80211_lock();
1112
Johannes Berg84fa4f42008-10-24 20:32:23 +02001113 /* All initial requests are respected */
1114 if (!last_request)
1115 return 0;
1116
1117 switch (set_by) {
1118 case REGDOM_SET_BY_INIT:
1119 return -EINVAL;
1120 case REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001121 return -EINVAL;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001122 case REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001123
1124 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1125
Johannes Berg84fa4f42008-10-24 20:32:23 +02001126 if (unlikely(!is_an_alpha2(alpha2)))
1127 return -EINVAL;
1128 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001129 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001130 /*
1131 * Two cards with two APs claiming different
1132 * different Country IE alpha2s. We could
1133 * intersect them, but that seems unlikely
1134 * to be correct. Reject second one for now.
1135 */
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001136 if (regdom_changes(alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001137 return -EOPNOTSUPP;
1138 return -EALREADY;
1139 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001140 /*
1141 * Two consecutive Country IE hints on the same wiphy.
1142 * This should be picked up early by the driver/stack
1143 */
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001144 if (WARN_ON(regdom_changes(alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001145 return 0;
1146 return -EALREADY;
1147 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001148 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001149 case REGDOM_SET_BY_DRIVER:
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001150 if (last_request->initiator == REGDOM_SET_BY_CORE) {
1151 if (is_old_static_regdom(cfg80211_regdomain))
1152 return 0;
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001153 if (regdom_changes(alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001154 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001155 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001156 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001157
1158 /*
1159 * This would happen if you unplug and plug your card
1160 * back in or if you add a new device for which the previously
1161 * loaded card also agrees on the regulatory domain.
1162 */
1163 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001164 !regdom_changes(alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001165 return -EALREADY;
1166
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001167 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001168 case REGDOM_SET_BY_USER:
Johannes Berg84fa4f42008-10-24 20:32:23 +02001169 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001170 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001171 /*
1172 * If the user knows better the user should set the regdom
1173 * to their country before the IE is picked up
1174 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001175 if (last_request->initiator == REGDOM_SET_BY_USER &&
1176 last_request->intersect)
1177 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001178 /*
1179 * Process user requests only after previous user/driver/core
1180 * requests have been processed
1181 */
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001182 if (last_request->initiator == REGDOM_SET_BY_CORE ||
1183 last_request->initiator == REGDOM_SET_BY_DRIVER ||
1184 last_request->initiator == REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001185 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001186 return -EAGAIN;
1187 }
1188
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001189 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001190 !regdom_changes(alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001191 return -EALREADY;
1192
Johannes Berg84fa4f42008-10-24 20:32:23 +02001193 return 0;
1194 }
1195
1196 return -EINVAL;
1197}
1198
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001199/* Caller must hold &cfg80211_mutex */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001200int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001201 const char *alpha2,
1202 u32 country_ie_checksum,
1203 enum environment_cap env)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001204{
1205 struct regulatory_request *request;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001206 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001207 int r = 0;
1208
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001209 assert_cfg80211_lock();
1210
Johannes Bergbe3d4812008-10-24 20:32:21 +02001211 r = ignore_request(wiphy, set_by, alpha2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001212
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001213 if (r == REG_INTERSECT) {
1214 if (set_by == REGDOM_SET_BY_DRIVER) {
1215 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1216 if (r)
1217 return r;
1218 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001219 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001220 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001221 /*
1222 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001223 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001224 * wiphy
1225 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001226 if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
1227 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1228 if (r)
1229 return r;
1230 r = -EALREADY;
1231 goto new_request;
1232 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001233 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001234 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001235
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001236new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001237 request = kzalloc(sizeof(struct regulatory_request),
1238 GFP_KERNEL);
1239 if (!request)
1240 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001241
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001242 request->alpha2[0] = alpha2[0];
1243 request->alpha2[1] = alpha2[1];
1244 request->initiator = set_by;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001245 request->wiphy_idx = get_wiphy_idx(wiphy);
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001246 request->intersect = intersect;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001247 request->country_ie_checksum = country_ie_checksum;
1248 request->country_ie_env = env;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001249
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001250 kfree(last_request);
1251 last_request = request;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001252
1253 /* When r == REG_INTERSECT we do need to call CRDA */
1254 if (r < 0)
1255 return r;
1256
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001257 /*
1258 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1259 * AND if CRDA is NOT present nothing will happen, if someone
1260 * wants to bother with 11d with OLD_REG you can add a timer.
1261 * If after x amount of time nothing happens you can call:
1262 *
1263 * return set_regdom(country_ie_regdomain);
1264 *
1265 * to intersect with the static rd
1266 */
Luis R. Rodriguez02ba0b32008-11-12 14:22:00 -08001267 return call_crda(alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001268}
1269
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001270/* This currently only processes user and driver regulatory hints */
1271static int reg_process_hint(struct regulatory_request *reg_request)
1272{
1273 int r = 0;
1274 struct wiphy *wiphy = NULL;
1275
1276 BUG_ON(!reg_request->alpha2);
1277
1278 mutex_lock(&cfg80211_mutex);
1279
1280 if (wiphy_idx_valid(reg_request->wiphy_idx))
1281 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1282
1283 if (reg_request->initiator == REGDOM_SET_BY_DRIVER &&
1284 !wiphy) {
1285 r = -ENODEV;
1286 goto out;
1287 }
1288
1289 r = __regulatory_hint(wiphy,
1290 reg_request->initiator,
1291 reg_request->alpha2,
1292 reg_request->country_ie_checksum,
1293 reg_request->country_ie_env);
1294 /* This is required so that the orig_* parameters are saved */
1295 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1296 wiphy_update_regulatory(wiphy, reg_request->initiator);
1297out:
1298 mutex_unlock(&cfg80211_mutex);
1299
1300 if (r == -EALREADY)
1301 r = 0;
1302
1303 return r;
1304}
1305
1306static void reg_process_pending_hints(void)
1307 {
1308 struct regulatory_request *reg_request;
1309 int r;
1310
1311 spin_lock(&reg_requests_lock);
1312 while (!list_empty(&reg_requests_list)) {
1313 reg_request = list_first_entry(&reg_requests_list,
1314 struct regulatory_request,
1315 list);
1316 list_del_init(&reg_request->list);
1317 spin_unlock(&reg_requests_lock);
1318
1319 r = reg_process_hint(reg_request);
1320#ifdef CONFIG_CFG80211_REG_DEBUG
1321 if (r && (reg_request->initiator == REGDOM_SET_BY_DRIVER ||
1322 reg_request->initiator == REGDOM_SET_BY_COUNTRY_IE))
1323 printk(KERN_ERR "cfg80211: wiphy_idx %d sent a "
1324 "regulatory hint for %c%c but now has "
1325 "gone fishing, ignoring request\n",
1326 reg_request->wiphy_idx,
1327 reg_request->alpha2[0],
1328 reg_request->alpha2[1]);
1329#endif
1330 kfree(reg_request);
1331 spin_lock(&reg_requests_lock);
1332 }
1333 spin_unlock(&reg_requests_lock);
1334}
1335
1336static void reg_todo(struct work_struct *work)
1337{
1338 reg_process_pending_hints();
1339}
1340
1341static DECLARE_WORK(reg_work, reg_todo);
1342
1343static void queue_regulatory_request(struct regulatory_request *request)
1344{
1345 spin_lock(&reg_requests_lock);
1346 list_add_tail(&request->list, &reg_requests_list);
1347 spin_unlock(&reg_requests_lock);
1348
1349 schedule_work(&reg_work);
1350}
1351
1352/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001353static int regulatory_hint_core(const char *alpha2)
1354{
1355 struct regulatory_request *request;
1356
1357 BUG_ON(last_request);
1358
1359 request = kzalloc(sizeof(struct regulatory_request),
1360 GFP_KERNEL);
1361 if (!request)
1362 return -ENOMEM;
1363
1364 request->alpha2[0] = alpha2[0];
1365 request->alpha2[1] = alpha2[1];
1366 request->initiator = REGDOM_SET_BY_CORE;
1367
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001368 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001369
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001370 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001371}
1372
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001373/* User hints */
1374int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001375{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001376 struct regulatory_request *request;
1377
Johannes Bergbe3d4812008-10-24 20:32:21 +02001378 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001379
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001380 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1381 if (!request)
1382 return -ENOMEM;
1383
1384 request->wiphy_idx = WIPHY_IDX_STALE;
1385 request->alpha2[0] = alpha2[0];
1386 request->alpha2[1] = alpha2[1];
1387 request->initiator = REGDOM_SET_BY_USER,
1388
1389 queue_regulatory_request(request);
1390
1391 return 0;
1392}
1393
1394/* Driver hints */
1395int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1396{
1397 struct regulatory_request *request;
1398
1399 BUG_ON(!alpha2);
1400 BUG_ON(!wiphy);
1401
1402 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1403 if (!request)
1404 return -ENOMEM;
1405
1406 request->wiphy_idx = get_wiphy_idx(wiphy);
1407
1408 /* Must have registered wiphy first */
1409 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1410
1411 request->alpha2[0] = alpha2[0];
1412 request->alpha2[1] = alpha2[1];
1413 request->initiator = REGDOM_SET_BY_DRIVER;
1414
1415 queue_regulatory_request(request);
1416
1417 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001418}
1419EXPORT_SYMBOL(regulatory_hint);
1420
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001421static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1422 u32 country_ie_checksum)
1423{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001424 struct wiphy *request_wiphy;
1425
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001426 assert_cfg80211_lock();
1427
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001428 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1429
1430 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001431 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001432
1433 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001434 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001435 /*
1436 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001437 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001438 * on the device
1439 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001440 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1441 return true;
1442 return false;
1443}
1444
1445void regulatory_hint_11d(struct wiphy *wiphy,
1446 u8 *country_ie,
1447 u8 country_ie_len)
1448{
1449 struct ieee80211_regdomain *rd = NULL;
1450 char alpha2[2];
1451 u32 checksum = 0;
1452 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001453 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001454
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001455 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001456
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001457 if (unlikely(!last_request)) {
1458 mutex_unlock(&cfg80211_mutex);
1459 return;
1460 }
1461
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001462 /* IE len must be evenly divisible by 2 */
1463 if (country_ie_len & 0x01)
1464 goto out;
1465
1466 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1467 goto out;
1468
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001469 /*
1470 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001471 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001472 * we were able to process the last regulatory_hint_11d() call
1473 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001474 if (country_ie_regdomain)
1475 goto out;
1476
1477 alpha2[0] = country_ie[0];
1478 alpha2[1] = country_ie[1];
1479
1480 if (country_ie[2] == 'I')
1481 env = ENVIRON_INDOOR;
1482 else if (country_ie[2] == 'O')
1483 env = ENVIRON_OUTDOOR;
1484
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001485 /*
1486 * We will run this for *every* beacon processed for the BSSID, so
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001487 * we optimize an early check to exit out early if we don't have to
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001488 * do anything
1489 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001490 if (likely(wiphy_idx_valid(last_request->wiphy_idx))) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001491 struct cfg80211_registered_device *drv_last_ie;
1492
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001493 drv_last_ie =
1494 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001495
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001496 /*
1497 * Lets keep this simple -- we trust the first AP
1498 * after we intersect with CRDA
1499 */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001500 if (likely(&drv_last_ie->wiphy == wiphy)) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001501 /*
1502 * Ignore IEs coming in on this wiphy with
1503 * the same alpha2 and environment cap
1504 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001505 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1506 alpha2) &&
1507 env == drv_last_ie->env)) {
1508 goto out;
1509 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001510 /*
1511 * the wiphy moved on to another BSSID or the AP
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001512 * was reconfigured. XXX: We need to deal with the
1513 * case where the user suspends and goes to goes
1514 * to another country, and then gets IEs from an
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001515 * AP with different settings
1516 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001517 goto out;
1518 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001519 /*
1520 * Ignore IEs coming in on two separate wiphys with
1521 * the same alpha2 and environment cap
1522 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001523 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1524 alpha2) &&
1525 env == drv_last_ie->env)) {
1526 goto out;
1527 }
1528 /* We could potentially intersect though */
1529 goto out;
1530 }
1531 }
1532
1533 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1534 if (!rd)
1535 goto out;
1536
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001537 /*
1538 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001539 * the future when we want to add suspend/resume support and having
1540 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001541 * move to another AP. Right now we just trust the first AP.
1542 *
1543 * If we hit this before we add this support we want to be informed of
1544 * it as it would indicate a mistake in the current design
1545 */
1546 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001547 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001548
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001549 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1550 if (!request)
1551 goto free_rd_out;
1552
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001553 /*
1554 * We keep this around for when CRDA comes back with a response so
1555 * we can intersect with that
1556 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001557 country_ie_regdomain = rd;
1558
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001559 request->wiphy_idx = get_wiphy_idx(wiphy);
1560 request->alpha2[0] = rd->alpha2[0];
1561 request->alpha2[1] = rd->alpha2[1];
1562 request->initiator = REGDOM_SET_BY_COUNTRY_IE;
1563 request->country_ie_checksum = checksum;
1564 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001565
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001566 mutex_unlock(&cfg80211_mutex);
1567
1568 queue_regulatory_request(request);
1569
1570 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001571
1572free_rd_out:
1573 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001574out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001575 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001576}
1577EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001578
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001579static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001580{
1581 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001582 const struct ieee80211_reg_rule *reg_rule = NULL;
1583 const struct ieee80211_freq_range *freq_range = NULL;
1584 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001585
1586 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1587 "(max_antenna_gain, max_eirp)\n");
1588
1589 for (i = 0; i < rd->n_reg_rules; i++) {
1590 reg_rule = &rd->reg_rules[i];
1591 freq_range = &reg_rule->freq_range;
1592 power_rule = &reg_rule->power_rule;
1593
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001594 /*
1595 * There may not be documentation for max antenna gain
1596 * in certain regions
1597 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001598 if (power_rule->max_antenna_gain)
1599 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1600 "(%d mBi, %d mBm)\n",
1601 freq_range->start_freq_khz,
1602 freq_range->end_freq_khz,
1603 freq_range->max_bandwidth_khz,
1604 power_rule->max_antenna_gain,
1605 power_rule->max_eirp);
1606 else
1607 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1608 "(N/A, %d mBm)\n",
1609 freq_range->start_freq_khz,
1610 freq_range->end_freq_khz,
1611 freq_range->max_bandwidth_khz,
1612 power_rule->max_eirp);
1613 }
1614}
1615
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001616static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001617{
1618
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001619 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001620
1621 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001622 struct cfg80211_registered_device *drv;
1623 drv = cfg80211_drv_by_wiphy_idx(
1624 last_request->wiphy_idx);
1625 if (drv) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001626 printk(KERN_INFO "cfg80211: Current regulatory "
1627 "domain updated by AP to: %c%c\n",
1628 drv->country_ie_alpha2[0],
1629 drv->country_ie_alpha2[1]);
1630 } else
1631 printk(KERN_INFO "cfg80211: Current regulatory "
1632 "domain intersected: \n");
1633 } else
1634 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001635 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001636 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001637 printk(KERN_INFO "cfg80211: World regulatory "
1638 "domain updated:\n");
1639 else {
1640 if (is_unknown_alpha2(rd->alpha2))
1641 printk(KERN_INFO "cfg80211: Regulatory domain "
1642 "changed to driver built-in settings "
1643 "(unknown country)\n");
1644 else
1645 printk(KERN_INFO "cfg80211: Regulatory domain "
1646 "changed to country: %c%c\n",
1647 rd->alpha2[0], rd->alpha2[1]);
1648 }
1649 print_rd_rules(rd);
1650}
1651
Johannes Berg2df78162008-10-28 16:49:41 +01001652static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001653{
1654 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1655 rd->alpha2[0], rd->alpha2[1]);
1656 print_rd_rules(rd);
1657}
1658
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001659#ifdef CONFIG_CFG80211_REG_DEBUG
1660static void reg_country_ie_process_debug(
1661 const struct ieee80211_regdomain *rd,
1662 const struct ieee80211_regdomain *country_ie_regdomain,
1663 const struct ieee80211_regdomain *intersected_rd)
1664{
1665 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1666 print_regdomain_info(country_ie_regdomain);
1667 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1668 print_regdomain_info(rd);
1669 if (intersected_rd) {
1670 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1671 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08001672 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001673 return;
1674 }
1675 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1676}
1677#else
1678static inline void reg_country_ie_process_debug(
1679 const struct ieee80211_regdomain *rd,
1680 const struct ieee80211_regdomain *country_ie_regdomain,
1681 const struct ieee80211_regdomain *intersected_rd)
1682{
1683}
1684#endif
1685
Johannes Bergd2372b32008-10-24 20:32:20 +02001686/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001687static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001688{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001689 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001690 struct cfg80211_registered_device *drv = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001691 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001692 /* Some basic sanity checks first */
1693
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001694 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001695 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001696 return -EINVAL;
1697 update_world_regdomain(rd);
1698 return 0;
1699 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001700
1701 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1702 !is_unknown_alpha2(rd->alpha2))
1703 return -EINVAL;
1704
Johannes Bergf6037d02008-10-21 11:01:33 +02001705 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001706 return -EINVAL;
1707
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001708 /*
1709 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001710 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001711 * and the pending request came in from a country IE
1712 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001713 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001714 /*
1715 * If someone else asked us to change the rd lets only bother
1716 * checking if the alpha2 changes if CRDA was already called
1717 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001718 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001719 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001720 return -EINVAL;
1721 }
1722
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001723 /*
1724 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001725 * and finally inform them of what we have done, in case they want
1726 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001727 * internal EEPROM data
1728 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001729
Johannes Bergf6037d02008-10-21 11:01:33 +02001730 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001731 return -EINVAL;
1732
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001733 if (!is_valid_rd(rd)) {
1734 printk(KERN_ERR "cfg80211: Invalid "
1735 "regulatory domain detected:\n");
1736 print_regdomain_info(rd);
1737 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001738 }
1739
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001740 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1741
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001742 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001743 int r;
1744
1745 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1746 reset_regdomains();
1747 cfg80211_regdomain = rd;
1748 return 0;
1749 }
1750
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001751 /*
1752 * For a driver hint, lets copy the regulatory domain the
1753 * driver wanted to the wiphy to deal with conflicts
1754 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001755
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001756 BUG_ON(request_wiphy->regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001757
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001758 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001759 if (r)
1760 return r;
1761
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001762 reset_regdomains();
1763 cfg80211_regdomain = rd;
1764 return 0;
1765 }
1766
1767 /* Intersection requires a bit more work */
1768
1769 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1770
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001771 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1772 if (!intersected_rd)
1773 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001774
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001775 /*
1776 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001777 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001778 * domain we keep it for its private use
1779 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001780 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001781 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001782 else
1783 kfree(rd);
1784
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001785 rd = NULL;
1786
1787 reset_regdomains();
1788 cfg80211_regdomain = intersected_rd;
1789
1790 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001791 }
1792
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001793 /*
1794 * Country IE requests are handled a bit differently, we intersect
1795 * the country IE rd with what CRDA believes that country should have
1796 */
1797
1798 BUG_ON(!country_ie_regdomain);
1799
1800 if (rd != country_ie_regdomain) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001801 /*
1802 * Intersect what CRDA returned and our what we
1803 * had built from the Country IE received
1804 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001805
1806 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1807
1808 reg_country_ie_process_debug(rd, country_ie_regdomain,
1809 intersected_rd);
1810
1811 kfree(country_ie_regdomain);
1812 country_ie_regdomain = NULL;
1813 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001814 /*
1815 * This would happen when CRDA was not present and
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001816 * OLD_REGULATORY was enabled. We intersect our Country
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001817 * IE rd and what was set on cfg80211 originally
1818 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001819 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1820 }
1821
1822 if (!intersected_rd)
1823 return -EINVAL;
1824
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001825 drv = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001826
1827 drv->country_ie_alpha2[0] = rd->alpha2[0];
1828 drv->country_ie_alpha2[1] = rd->alpha2[1];
1829 drv->env = last_request->country_ie_env;
1830
1831 BUG_ON(intersected_rd == rd);
1832
1833 kfree(rd);
1834 rd = NULL;
1835
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001836 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001837 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001838
1839 return 0;
1840}
1841
1842
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001843/*
1844 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001845 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001846 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
1847 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001848int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001849{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001850 int r;
1851
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001852 assert_cfg80211_lock();
1853
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001854 /* Note that this doesn't update the wiphys, this is done below */
1855 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02001856 if (r) {
1857 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001858 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02001859 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001860
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001861 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001862 if (!last_request->intersect)
1863 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001864
1865 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02001866 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001867
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001868 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001869
1870 return r;
1871}
1872
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001873/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001874void reg_device_remove(struct wiphy *wiphy)
1875{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001876 struct wiphy *request_wiphy;
1877
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001878 assert_cfg80211_lock();
1879
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001880 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1881
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001882 kfree(wiphy->regd);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001883 if (!last_request || !request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001884 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001885 if (request_wiphy != wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001886 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001887 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001888 last_request->country_ie_env = ENVIRON_ANY;
1889}
1890
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001891int regulatory_init(void)
1892{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05001893 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02001894
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001895 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1896 if (IS_ERR(reg_pdev))
1897 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001898
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001899 spin_lock_init(&reg_requests_lock);
1900
Johannes Berg734366d2008-09-15 10:56:48 +02001901#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001902 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02001903
Johannes Berg942b25c2008-09-15 11:26:47 +02001904 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02001905 print_regdomain_info(cfg80211_regdomain);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001906 /*
1907 * The old code still requests for a new regdomain and if
Johannes Berg734366d2008-09-15 10:56:48 +02001908 * you have CRDA you get it updated, otherwise you get
1909 * stuck with the static values. We ignore "EU" code as
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001910 * that is not a valid ISO / IEC 3166 alpha2
1911 */
Johannes Bergac9440a2008-10-21 11:08:27 +02001912 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001913 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02001914#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001915 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02001916
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001917 err = regulatory_hint_core("00");
Johannes Berg734366d2008-09-15 10:56:48 +02001918#endif
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05001919 if (err) {
1920 if (err == -ENOMEM)
1921 return err;
1922 /*
1923 * N.B. kobject_uevent_env() can fail mainly for when we're out
1924 * memory which is handled and propagated appropriately above
1925 * but it can also fail during a netlink_broadcast() or during
1926 * early boot for call_usermodehelper(). For now treat these
1927 * errors as non-fatal.
1928 */
1929 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
1930 "to call CRDA during init");
1931#ifdef CONFIG_CFG80211_REG_DEBUG
1932 /* We want to find out exactly why when debugging */
1933 WARN_ON(err);
1934#endif
1935 }
Johannes Berg734366d2008-09-15 10:56:48 +02001936
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001937 return 0;
1938}
1939
1940void regulatory_exit(void)
1941{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001942 struct regulatory_request *reg_request, *tmp;
1943
1944 cancel_work_sync(&reg_work);
1945
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001946 mutex_lock(&cfg80211_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02001947
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001948 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02001949
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001950 kfree(country_ie_regdomain);
1951 country_ie_regdomain = NULL;
1952
Johannes Bergf6037d02008-10-21 11:01:33 +02001953 kfree(last_request);
1954
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001955 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001956
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001957 spin_lock(&reg_requests_lock);
1958 if (!list_empty(&reg_requests_list)) {
1959 list_for_each_entry_safe(reg_request, tmp,
1960 &reg_requests_list, list) {
1961 list_del(&reg_request->list);
1962 kfree(reg_request);
1963 }
1964 }
1965 spin_unlock(&reg_requests_lock);
1966
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001967 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01001968}