blob: 89e0d8b3cf1eec1d9798a070b5fe989ef37eb86e [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
Johannes Berg734366d2008-09-15 10:56:48 +020057/* Central wireless core regulatory domains, we only need two,
58 * the current one and a world regulatory domain in case we have no
59 * information to give us an alpha2 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +020060static const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020061
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080062/* We use this as a place for the rd structure built from the
63 * last parsed country IE to rest until CRDA gets back to us with
64 * what it thinks should apply for the same country */
65static const struct ieee80211_regdomain *country_ie_regdomain;
66
Johannes Berg734366d2008-09-15 10:56:48 +020067/* We keep a static world regulatory domain in case of the absence of CRDA */
68static const struct ieee80211_regdomain world_regdom = {
69 .n_reg_rules = 1,
70 .alpha2 = "00",
71 .reg_rules = {
72 REG_RULE(2412-10, 2462+10, 40, 6, 20,
73 NL80211_RRF_PASSIVE_SCAN |
74 NL80211_RRF_NO_IBSS),
75 }
76};
77
Johannes Berga3d2eaf2008-09-15 11:10:52 +020078static const struct ieee80211_regdomain *cfg80211_world_regdom =
79 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +020080
81#ifdef CONFIG_WIRELESS_OLD_REGULATORY
82static char *ieee80211_regdom = "US";
83module_param(ieee80211_regdom, charp, 0444);
84MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
85
86/* We assume 40 MHz bandwidth for the old regulatory work.
87 * We make emphasis we are using the exact same frequencies
88 * as before */
89
90static const struct ieee80211_regdomain us_regdom = {
91 .n_reg_rules = 6,
92 .alpha2 = "US",
93 .reg_rules = {
94 /* IEEE 802.11b/g, channels 1..11 */
95 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
96 /* IEEE 802.11a, channel 36 */
97 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
98 /* IEEE 802.11a, channel 40 */
99 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
100 /* IEEE 802.11a, channel 44 */
101 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
102 /* IEEE 802.11a, channels 48..64 */
103 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
104 /* IEEE 802.11a, channels 149..165, outdoor */
105 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
106 }
107};
108
109static const struct ieee80211_regdomain jp_regdom = {
110 .n_reg_rules = 3,
111 .alpha2 = "JP",
112 .reg_rules = {
113 /* IEEE 802.11b/g, channels 1..14 */
114 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
115 /* IEEE 802.11a, channels 34..48 */
116 REG_RULE(5170-10, 5240+10, 40, 6, 20,
117 NL80211_RRF_PASSIVE_SCAN),
118 /* IEEE 802.11a, channels 52..64 */
119 REG_RULE(5260-10, 5320+10, 40, 6, 20,
120 NL80211_RRF_NO_IBSS |
121 NL80211_RRF_DFS),
122 }
123};
124
125static const struct ieee80211_regdomain eu_regdom = {
126 .n_reg_rules = 6,
127 /* This alpha2 is bogus, we leave it here just for stupid
128 * backward compatibility */
129 .alpha2 = "EU",
130 .reg_rules = {
131 /* IEEE 802.11b/g, channels 1..13 */
132 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
133 /* IEEE 802.11a, channel 36 */
134 REG_RULE(5180-10, 5180+10, 40, 6, 23,
135 NL80211_RRF_PASSIVE_SCAN),
136 /* IEEE 802.11a, channel 40 */
137 REG_RULE(5200-10, 5200+10, 40, 6, 23,
138 NL80211_RRF_PASSIVE_SCAN),
139 /* IEEE 802.11a, channel 44 */
140 REG_RULE(5220-10, 5220+10, 40, 6, 23,
141 NL80211_RRF_PASSIVE_SCAN),
142 /* IEEE 802.11a, channels 48..64 */
143 REG_RULE(5240-10, 5320+10, 40, 6, 20,
144 NL80211_RRF_NO_IBSS |
145 NL80211_RRF_DFS),
146 /* IEEE 802.11a, channels 100..140 */
147 REG_RULE(5500-10, 5700+10, 40, 6, 30,
148 NL80211_RRF_NO_IBSS |
149 NL80211_RRF_DFS),
150 }
151};
152
153static const struct ieee80211_regdomain *static_regdom(char *alpha2)
154{
155 if (alpha2[0] == 'U' && alpha2[1] == 'S')
156 return &us_regdom;
157 if (alpha2[0] == 'J' && alpha2[1] == 'P')
158 return &jp_regdom;
159 if (alpha2[0] == 'E' && alpha2[1] == 'U')
160 return &eu_regdom;
161 /* Default, as per the old rules */
162 return &us_regdom;
163}
164
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200165static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200166{
167 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
168 return true;
169 return false;
170}
Johannes Berg734366d2008-09-15 10:56:48 +0200171#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200172static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
173{
174 return false;
175}
176#endif
177
Johannes Berg734366d2008-09-15 10:56:48 +0200178static void reset_regdomains(void)
179{
Johannes Berg942b25c2008-09-15 11:26:47 +0200180 /* avoid freeing static information or freeing something twice */
181 if (cfg80211_regdomain == cfg80211_world_regdom)
182 cfg80211_regdomain = NULL;
183 if (cfg80211_world_regdom == &world_regdom)
184 cfg80211_world_regdom = NULL;
185 if (cfg80211_regdomain == &world_regdom)
186 cfg80211_regdomain = NULL;
187 if (is_old_static_regdom(cfg80211_regdomain))
188 cfg80211_regdomain = NULL;
189
190 kfree(cfg80211_regdomain);
191 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200192
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200193 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200194 cfg80211_regdomain = NULL;
195}
196
197/* Dynamic world regulatory domain requested by the wireless
198 * core upon initialization */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200199static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200200{
Johannes Bergf6037d02008-10-21 11:01:33 +0200201 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200202
203 reset_regdomains();
204
205 cfg80211_world_regdom = rd;
206 cfg80211_regdomain = rd;
207}
Johannes Berg734366d2008-09-15 10:56:48 +0200208
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200209bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100210{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700211 if (!alpha2)
212 return false;
213 if (alpha2[0] == '0' && alpha2[1] == '0')
214 return true;
215 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100216}
217
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200218static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700219{
220 if (!alpha2)
221 return false;
222 if (alpha2[0] != 0 && alpha2[1] != 0)
223 return true;
224 return false;
225}
Johannes Berg8318d782008-01-24 19:38:38 +0100226
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700227static bool is_alpha_upper(char letter)
228{
229 /* ASCII A - Z */
230 if (letter >= 65 && letter <= 90)
231 return true;
232 return false;
233}
234
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200235static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700236{
237 if (!alpha2)
238 return false;
239 /* Special case where regulatory domain was built by driver
240 * but a specific alpha2 cannot be determined */
241 if (alpha2[0] == '9' && alpha2[1] == '9')
242 return true;
243 return false;
244}
245
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800246static bool is_intersected_alpha2(const char *alpha2)
247{
248 if (!alpha2)
249 return false;
250 /* Special case where regulatory domain is the
251 * result of an intersection between two regulatory domain
252 * structures */
253 if (alpha2[0] == '9' && alpha2[1] == '8')
254 return true;
255 return false;
256}
257
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200258static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700259{
260 if (!alpha2)
261 return false;
262 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
263 return true;
264 return false;
265}
266
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200267static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268{
269 if (!alpha2_x || !alpha2_y)
270 return false;
271 if (alpha2_x[0] == alpha2_y[0] &&
272 alpha2_x[1] == alpha2_y[1])
273 return true;
274 return false;
275}
276
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200277static bool regdom_changed(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700278{
279 if (!cfg80211_regdomain)
280 return true;
281 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
282 return false;
283 return true;
284}
285
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800286/**
287 * country_ie_integrity_changes - tells us if the country IE has changed
288 * @checksum: checksum of country IE of fields we are interested in
289 *
290 * If the country IE has not changed you can ignore it safely. This is
291 * useful to determine if two devices are seeing two different country IEs
292 * even on the same alpha2. Note that this will return false if no IE has
293 * been set on the wireless core yet.
294 */
295static bool country_ie_integrity_changes(u32 checksum)
296{
297 /* If no IE has been set then the checksum doesn't change */
298 if (unlikely(!last_request->country_ie_checksum))
299 return false;
300 if (unlikely(last_request->country_ie_checksum != checksum))
301 return true;
302 return false;
303}
304
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700305/* This lets us keep regulatory code which is updated on a regulatory
306 * basis in userspace. */
307static int call_crda(const char *alpha2)
308{
309 char country_env[9 + 2] = "COUNTRY=";
310 char *envp[] = {
311 country_env,
312 NULL
313 };
314
315 if (!is_world_regdom((char *) alpha2))
316 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
317 alpha2[0], alpha2[1]);
318 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700319 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
320 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700321
322 country_env[8] = alpha2[0];
323 country_env[9] = alpha2[1];
324
325 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
326}
327
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700328/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200329bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700330{
Johannes Bergf6037d02008-10-21 11:01:33 +0200331 if (!last_request)
332 return false;
333
334 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700335}
336
337/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200338static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700339{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200340 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700341 u32 freq_diff;
342
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800343 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700344 return false;
345
346 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
347 return false;
348
349 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
350
Luis R. Rodriguezd71aaf62008-10-30 13:33:52 -0700351 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700352 return false;
353
354 return true;
355}
356
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200357static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700358{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200359 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700360 unsigned int i;
361
362 if (!rd->n_reg_rules)
363 return false;
364
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800365 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
366 return false;
367
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700368 for (i = 0; i < rd->n_reg_rules; i++) {
369 reg_rule = &rd->reg_rules[i];
370 if (!is_valid_reg_rule(reg_rule))
371 return false;
372 }
373
374 return true;
375}
376
377/* Returns value in KHz */
378static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
379 u32 freq)
380{
381 unsigned int i;
382 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
383 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
384 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
385 if (start_freq_khz >= freq_range->start_freq_khz &&
386 end_freq_khz <= freq_range->end_freq_khz)
387 return supported_bandwidths[i];
388 }
389 return 0;
390}
391
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800392/**
393 * freq_in_rule_band - tells us if a frequency is in a frequency band
394 * @freq_range: frequency rule we want to query
395 * @freq_khz: frequency we are inquiring about
396 *
397 * This lets us know if a specific frequency rule is or is not relevant to
398 * a specific frequency's band. Bands are device specific and artificial
399 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
400 * safe for now to assume that a frequency rule should not be part of a
401 * frequency's band if the start freq or end freq are off by more than 2 GHz.
402 * This resolution can be lowered and should be considered as we add
403 * regulatory rule support for other "bands".
404 **/
405static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
406 u32 freq_khz)
407{
408#define ONE_GHZ_IN_KHZ 1000000
409 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
410 return true;
411 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
412 return true;
413 return false;
414#undef ONE_GHZ_IN_KHZ
415}
416
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800417/* Converts a country IE to a regulatory domain. A regulatory domain
418 * structure has a lot of information which the IE doesn't yet have,
419 * so for the other values we use upper max values as we will intersect
420 * with our userspace regulatory agent to get lower bounds. */
421static struct ieee80211_regdomain *country_ie_2_rd(
422 u8 *country_ie,
423 u8 country_ie_len,
424 u32 *checksum)
425{
426 struct ieee80211_regdomain *rd = NULL;
427 unsigned int i = 0;
428 char alpha2[2];
429 u32 flags = 0;
430 u32 num_rules = 0, size_of_regd = 0;
431 u8 *triplets_start = NULL;
432 u8 len_at_triplet = 0;
433 /* the last channel we have registered in a subband (triplet) */
434 int last_sub_max_channel = 0;
435
436 *checksum = 0xDEADBEEF;
437
438 /* Country IE requirements */
439 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
440 country_ie_len & 0x01);
441
442 alpha2[0] = country_ie[0];
443 alpha2[1] = country_ie[1];
444
445 /*
446 * Third octet can be:
447 * 'I' - Indoor
448 * 'O' - Outdoor
449 *
450 * anything else we assume is no restrictions
451 */
452 if (country_ie[2] == 'I')
453 flags = NL80211_RRF_NO_OUTDOOR;
454 else if (country_ie[2] == 'O')
455 flags = NL80211_RRF_NO_INDOOR;
456
457 country_ie += 3;
458 country_ie_len -= 3;
459
460 triplets_start = country_ie;
461 len_at_triplet = country_ie_len;
462
463 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
464
465 /* We need to build a reg rule for each triplet, but first we must
466 * calculate the number of reg rules we will need. We will need one
467 * for each channel subband */
468 while (country_ie_len >= 3) {
469 struct ieee80211_country_ie_triplet *triplet =
470 (struct ieee80211_country_ie_triplet *) country_ie;
471 int cur_sub_max_channel = 0, cur_channel = 0;
472
473 if (triplet->ext.reg_extension_id >=
474 IEEE80211_COUNTRY_EXTENSION_ID) {
475 country_ie += 3;
476 country_ie_len -= 3;
477 continue;
478 }
479
480 cur_channel = triplet->chans.first_channel;
481 cur_sub_max_channel = ieee80211_channel_to_frequency(
482 cur_channel + triplet->chans.num_channels);
483
484 /* Basic sanity check */
485 if (cur_sub_max_channel < cur_channel)
486 return NULL;
487
488 /* Do not allow overlapping channels. Also channels
489 * passed in each subband must be monotonically
490 * increasing */
491 if (last_sub_max_channel) {
492 if (cur_channel <= last_sub_max_channel)
493 return NULL;
494 if (cur_sub_max_channel <= last_sub_max_channel)
495 return NULL;
496 }
497
498 /* When dot11RegulatoryClassesRequired is supported
499 * we can throw ext triplets as part of this soup,
500 * for now we don't care when those change as we
501 * don't support them */
502 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
503 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
504 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
505
506 last_sub_max_channel = cur_sub_max_channel;
507
508 country_ie += 3;
509 country_ie_len -= 3;
510 num_rules++;
511
512 /* Note: this is not a IEEE requirement but
513 * simply a memory requirement */
514 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
515 return NULL;
516 }
517
518 country_ie = triplets_start;
519 country_ie_len = len_at_triplet;
520
521 size_of_regd = sizeof(struct ieee80211_regdomain) +
522 (num_rules * sizeof(struct ieee80211_reg_rule));
523
524 rd = kzalloc(size_of_regd, GFP_KERNEL);
525 if (!rd)
526 return NULL;
527
528 rd->n_reg_rules = num_rules;
529 rd->alpha2[0] = alpha2[0];
530 rd->alpha2[1] = alpha2[1];
531
532 /* This time around we fill in the rd */
533 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800534 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800535 struct ieee80211_country_ie_triplet *triplet =
536 (struct ieee80211_country_ie_triplet *) country_ie;
537 struct ieee80211_reg_rule *reg_rule = NULL;
538 struct ieee80211_freq_range *freq_range = NULL;
539 struct ieee80211_power_rule *power_rule = NULL;
540
541 /* Must parse if dot11RegulatoryClassesRequired is true,
542 * we don't support this yet */
543 if (triplet->ext.reg_extension_id >=
544 IEEE80211_COUNTRY_EXTENSION_ID) {
545 country_ie += 3;
546 country_ie_len -= 3;
547 continue;
548 }
549
550 reg_rule = &rd->reg_rules[i];
551 freq_range = &reg_rule->freq_range;
552 power_rule = &reg_rule->power_rule;
553
554 reg_rule->flags = flags;
555
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800556 /* 2 GHz */
557 if (triplet->chans.first_channel <= 14)
558 end_channel = triplet->chans.first_channel +
559 triplet->chans.num_channels;
560 else
561 /*
562 * 5 GHz -- For example in country IEs if the first
563 * channel given is 36 and the number of channels is 4
564 * then the individual channel numbers defined for the
565 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
566 * and not 36, 37, 38, 39.
567 *
568 * See: http://tinyurl.com/11d-clarification
569 */
570 end_channel = triplet->chans.first_channel +
571 (4 * (triplet->chans.num_channels - 1));
572
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800573 /* The +10 is since the regulatory domain expects
574 * the actual band edge, not the center of freq for
575 * its start and end freqs, assuming 20 MHz bandwidth on
576 * the channels passed */
577 freq_range->start_freq_khz =
578 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
579 triplet->chans.first_channel) - 10);
580 freq_range->end_freq_khz =
581 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800582 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800583
584 /* Large arbitrary values, we intersect later */
585 /* Increment this if we ever support >= 40 MHz channels
586 * in IEEE 802.11 */
587 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
588 power_rule->max_antenna_gain = DBI_TO_MBI(100);
589 power_rule->max_eirp = DBM_TO_MBM(100);
590
591 country_ie += 3;
592 country_ie_len -= 3;
593 i++;
594
595 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
596 }
597
598 return rd;
599}
600
601
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700602/* Helper for regdom_intersect(), this does the real
603 * mathematical intersection fun */
604static int reg_rules_intersect(
605 const struct ieee80211_reg_rule *rule1,
606 const struct ieee80211_reg_rule *rule2,
607 struct ieee80211_reg_rule *intersected_rule)
608{
609 const struct ieee80211_freq_range *freq_range1, *freq_range2;
610 struct ieee80211_freq_range *freq_range;
611 const struct ieee80211_power_rule *power_rule1, *power_rule2;
612 struct ieee80211_power_rule *power_rule;
613 u32 freq_diff;
614
615 freq_range1 = &rule1->freq_range;
616 freq_range2 = &rule2->freq_range;
617 freq_range = &intersected_rule->freq_range;
618
619 power_rule1 = &rule1->power_rule;
620 power_rule2 = &rule2->power_rule;
621 power_rule = &intersected_rule->power_rule;
622
623 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
624 freq_range2->start_freq_khz);
625 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
626 freq_range2->end_freq_khz);
627 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
628 freq_range2->max_bandwidth_khz);
629
630 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
631 if (freq_range->max_bandwidth_khz > freq_diff)
632 freq_range->max_bandwidth_khz = freq_diff;
633
634 power_rule->max_eirp = min(power_rule1->max_eirp,
635 power_rule2->max_eirp);
636 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
637 power_rule2->max_antenna_gain);
638
639 intersected_rule->flags = (rule1->flags | rule2->flags);
640
641 if (!is_valid_reg_rule(intersected_rule))
642 return -EINVAL;
643
644 return 0;
645}
646
647/**
648 * regdom_intersect - do the intersection between two regulatory domains
649 * @rd1: first regulatory domain
650 * @rd2: second regulatory domain
651 *
652 * Use this function to get the intersection between two regulatory domains.
653 * Once completed we will mark the alpha2 for the rd as intersected, "98",
654 * as no one single alpha2 can represent this regulatory domain.
655 *
656 * Returns a pointer to the regulatory domain structure which will hold the
657 * resulting intersection of rules between rd1 and rd2. We will
658 * kzalloc() this structure for you.
659 */
660static struct ieee80211_regdomain *regdom_intersect(
661 const struct ieee80211_regdomain *rd1,
662 const struct ieee80211_regdomain *rd2)
663{
664 int r, size_of_regd;
665 unsigned int x, y;
666 unsigned int num_rules = 0, rule_idx = 0;
667 const struct ieee80211_reg_rule *rule1, *rule2;
668 struct ieee80211_reg_rule *intersected_rule;
669 struct ieee80211_regdomain *rd;
670 /* This is just a dummy holder to help us count */
671 struct ieee80211_reg_rule irule;
672
673 /* Uses the stack temporarily for counter arithmetic */
674 intersected_rule = &irule;
675
676 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
677
678 if (!rd1 || !rd2)
679 return NULL;
680
681 /* First we get a count of the rules we'll need, then we actually
682 * build them. This is to so we can malloc() and free() a
683 * regdomain once. The reason we use reg_rules_intersect() here
684 * is it will return -EINVAL if the rule computed makes no sense.
685 * All rules that do check out OK are valid. */
686
687 for (x = 0; x < rd1->n_reg_rules; x++) {
688 rule1 = &rd1->reg_rules[x];
689 for (y = 0; y < rd2->n_reg_rules; y++) {
690 rule2 = &rd2->reg_rules[y];
691 if (!reg_rules_intersect(rule1, rule2,
692 intersected_rule))
693 num_rules++;
694 memset(intersected_rule, 0,
695 sizeof(struct ieee80211_reg_rule));
696 }
697 }
698
699 if (!num_rules)
700 return NULL;
701
702 size_of_regd = sizeof(struct ieee80211_regdomain) +
703 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
704
705 rd = kzalloc(size_of_regd, GFP_KERNEL);
706 if (!rd)
707 return NULL;
708
709 for (x = 0; x < rd1->n_reg_rules; x++) {
710 rule1 = &rd1->reg_rules[x];
711 for (y = 0; y < rd2->n_reg_rules; y++) {
712 rule2 = &rd2->reg_rules[y];
713 /* This time around instead of using the stack lets
714 * write to the target rule directly saving ourselves
715 * a memcpy() */
716 intersected_rule = &rd->reg_rules[rule_idx];
717 r = reg_rules_intersect(rule1, rule2,
718 intersected_rule);
719 /* No need to memset here the intersected rule here as
720 * we're not using the stack anymore */
721 if (r)
722 continue;
723 rule_idx++;
724 }
725 }
726
727 if (rule_idx != num_rules) {
728 kfree(rd);
729 return NULL;
730 }
731
732 rd->n_reg_rules = num_rules;
733 rd->alpha2[0] = '9';
734 rd->alpha2[1] = '8';
735
736 return rd;
737}
738
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700739/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
740 * want to just have the channel structure use these */
741static u32 map_regdom_flags(u32 rd_flags)
742{
743 u32 channel_flags = 0;
744 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
745 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
746 if (rd_flags & NL80211_RRF_NO_IBSS)
747 channel_flags |= IEEE80211_CHAN_NO_IBSS;
748 if (rd_flags & NL80211_RRF_DFS)
749 channel_flags |= IEEE80211_CHAN_RADAR;
750 return channel_flags;
751}
752
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800753static int freq_reg_info_regd(struct wiphy *wiphy,
754 u32 center_freq,
755 u32 *bandwidth,
756 const struct ieee80211_reg_rule **reg_rule,
757 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100758{
759 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800760 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800761 const struct ieee80211_regdomain *regd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700762 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100763
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800764 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800765
766 /* Follow the driver's regulatory domain, if present, unless a country
767 * IE has been processed */
768 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
769 wiphy->regd)
770 regd = wiphy->regd;
771
772 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700773 return -EINVAL;
774
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800775 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700776 const struct ieee80211_reg_rule *rr;
777 const struct ieee80211_freq_range *fr = NULL;
778 const struct ieee80211_power_rule *pr = NULL;
779
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800780 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700781 fr = &rr->freq_range;
782 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800783
784 /* We only need to know if one frequency rule was
785 * was in center_freq's band, that's enough, so lets
786 * not overwrite it once found */
787 if (!band_rule_found)
788 band_rule_found = freq_in_rule_band(fr, center_freq);
789
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700790 max_bandwidth = freq_max_bandwidth(fr, center_freq);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800791
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700792 if (max_bandwidth && *bandwidth <= max_bandwidth) {
793 *reg_rule = rr;
794 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100795 break;
796 }
797 }
798
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800799 if (!band_rule_found)
800 return -ERANGE;
801
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700802 return !max_bandwidth;
803}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800804EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700805
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800806int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800807 const struct ieee80211_reg_rule **reg_rule)
808{
809 return freq_reg_info_regd(wiphy, center_freq,
810 bandwidth, reg_rule, NULL);
811}
812
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800813static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
814 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700815{
816 int r;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800817 u32 flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700818 u32 max_bandwidth = 0;
819 const struct ieee80211_reg_rule *reg_rule = NULL;
820 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800821 struct ieee80211_supported_band *sband;
822 struct ieee80211_channel *chan;
823
824 sband = wiphy->bands[band];
825 BUG_ON(chan_idx >= sband->n_channels);
826 chan = &sband->channels[chan_idx];
827
828 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700829
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800830 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700831 &max_bandwidth, &reg_rule);
832
833 if (r) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800834 /* This means no regulatory rule was found in the country IE
835 * with a frequency range on the center_freq's band, since
836 * IEEE-802.11 allows for a country IE to have a subset of the
837 * regulatory information provided in a country we ignore
838 * disabling the channel unless at least one reg rule was
839 * found on the center_freq's band. For details see this
840 * clarification:
841 *
842 * http://tinyurl.com/11d-clarification
843 */
844 if (r == -ERANGE &&
845 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
846#ifdef CONFIG_CFG80211_REG_DEBUG
847 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
848 "intact on %s - no rule found in band on "
849 "Country IE\n",
850 chan->center_freq, wiphy_name(wiphy));
851#endif
852 } else {
853 /* In this case we know the country IE has at least one reg rule
854 * for the band so we respect its band definitions */
855#ifdef CONFIG_CFG80211_REG_DEBUG
856 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
857 printk(KERN_DEBUG "cfg80211: Disabling "
858 "channel %d MHz on %s due to "
859 "Country IE\n",
860 chan->center_freq, wiphy_name(wiphy));
861#endif
862 flags |= IEEE80211_CHAN_DISABLED;
863 chan->flags = flags;
864 }
Johannes Berg8318d782008-01-24 19:38:38 +0100865 return;
866 }
867
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700868 power_rule = &reg_rule->power_rule;
869
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800870 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
871 last_request->wiphy && last_request->wiphy == wiphy &&
872 last_request->wiphy->strict_regulatory) {
873 /* This gaurantees the driver's requested regulatory domain
874 * will always be used as a base for further regulatory
875 * settings */
876 chan->flags = chan->orig_flags =
877 map_regdom_flags(reg_rule->flags);
878 chan->max_antenna_gain = chan->orig_mag =
879 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
880 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
881 chan->max_power = chan->orig_mpwr =
882 (int) MBM_TO_DBM(power_rule->max_eirp);
883 return;
884 }
885
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700886 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100887 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700888 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
889 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400890 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700891 chan->max_power = min(chan->orig_mpwr,
892 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400893 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700894 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100895}
896
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800897static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +0100898{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800899 unsigned int i;
900 struct ieee80211_supported_band *sband;
901
902 BUG_ON(!wiphy->bands[band]);
903 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +0100904
905 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800906 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +0100907}
908
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800909static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
910{
911 if (!last_request)
912 return true;
913 if (setby == REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -0800914 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800915 return true;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800916 /* wiphy->regd will be set once the device has its own
917 * desired regulatory domain set */
918 if (wiphy->strict_regulatory && !wiphy->regd &&
919 !is_world_regdom(last_request->alpha2))
920 return true;
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800921 return false;
922}
923
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700924static void update_all_wiphy_regulatory(enum reg_set_by setby)
925{
926 struct cfg80211_registered_device *drv;
927
928 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -0800929 wiphy_update_regulatory(&drv->wiphy, setby);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700930}
931
932void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
Johannes Berg8318d782008-01-24 19:38:38 +0100933{
934 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -0800935
936 if (ignore_reg_update(wiphy, setby))
937 return;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700938 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100939 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800940 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700941 }
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -0800942 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -0800943 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700944}
945
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800946static void handle_channel_custom(struct wiphy *wiphy,
947 enum ieee80211_band band,
948 unsigned int chan_idx,
949 const struct ieee80211_regdomain *regd)
950{
951 int r;
952 u32 max_bandwidth = 0;
953 const struct ieee80211_reg_rule *reg_rule = NULL;
954 const struct ieee80211_power_rule *power_rule = NULL;
955 struct ieee80211_supported_band *sband;
956 struct ieee80211_channel *chan;
957
958 sband = wiphy->bands[band];
959 BUG_ON(chan_idx >= sband->n_channels);
960 chan = &sband->channels[chan_idx];
961
962 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
963 &max_bandwidth, &reg_rule, regd);
964
965 if (r) {
966 chan->flags = IEEE80211_CHAN_DISABLED;
967 return;
968 }
969
970 power_rule = &reg_rule->power_rule;
971
972 chan->flags |= map_regdom_flags(reg_rule->flags);
973 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
974 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
975 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
976}
977
978static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
979 const struct ieee80211_regdomain *regd)
980{
981 unsigned int i;
982 struct ieee80211_supported_band *sband;
983
984 BUG_ON(!wiphy->bands[band]);
985 sband = wiphy->bands[band];
986
987 for (i = 0; i < sband->n_channels; i++)
988 handle_channel_custom(wiphy, band, i, regd);
989}
990
991/* Used by drivers prior to wiphy registration */
992void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
993 const struct ieee80211_regdomain *regd)
994{
995 enum ieee80211_band band;
996 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
997 if (wiphy->bands[band])
998 handle_band_custom(wiphy, band, regd);
999 }
1000}
1001EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1002
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001003static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1004 const struct ieee80211_regdomain *src_regd)
1005{
1006 struct ieee80211_regdomain *regd;
1007 int size_of_regd = 0;
1008 unsigned int i;
1009
1010 size_of_regd = sizeof(struct ieee80211_regdomain) +
1011 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1012
1013 regd = kzalloc(size_of_regd, GFP_KERNEL);
1014 if (!regd)
1015 return -ENOMEM;
1016
1017 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1018
1019 for (i = 0; i < src_regd->n_reg_rules; i++)
1020 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1021 sizeof(struct ieee80211_reg_rule));
1022
1023 *dst_regd = regd;
1024 return 0;
1025}
1026
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001027/* Return value which can be used by ignore_request() to indicate
1028 * it has been determined we should intersect two regulatory domains */
1029#define REG_INTERSECT 1
1030
Johannes Berg84fa4f42008-10-24 20:32:23 +02001031/* This has the logic which determines when a new request
1032 * should be ignored. */
1033static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
1034 const char *alpha2)
1035{
1036 /* All initial requests are respected */
1037 if (!last_request)
1038 return 0;
1039
1040 switch (set_by) {
1041 case REGDOM_SET_BY_INIT:
1042 return -EINVAL;
1043 case REGDOM_SET_BY_CORE:
1044 /*
1045 * Always respect new wireless core hints, should only happen
1046 * when updating the world regulatory domain at init.
1047 */
1048 return 0;
1049 case REGDOM_SET_BY_COUNTRY_IE:
1050 if (unlikely(!is_an_alpha2(alpha2)))
1051 return -EINVAL;
1052 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1053 if (last_request->wiphy != wiphy) {
1054 /*
1055 * Two cards with two APs claiming different
1056 * different Country IE alpha2s. We could
1057 * intersect them, but that seems unlikely
1058 * to be correct. Reject second one for now.
1059 */
1060 if (!alpha2_equal(alpha2,
1061 cfg80211_regdomain->alpha2))
1062 return -EOPNOTSUPP;
1063 return -EALREADY;
1064 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001065 /* Two consecutive Country IE hints on the same wiphy.
1066 * This should be picked up early by the driver/stack */
1067 if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
1068 alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001069 return 0;
1070 return -EALREADY;
1071 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001072 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001073 case REGDOM_SET_BY_DRIVER:
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001074 if (last_request->initiator == REGDOM_SET_BY_CORE) {
1075 if (is_old_static_regdom(cfg80211_regdomain))
1076 return 0;
1077 if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
1078 return 0;
1079 return -EALREADY;
1080 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001081 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001082 case REGDOM_SET_BY_USER:
Johannes Berg84fa4f42008-10-24 20:32:23 +02001083 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001084 return REG_INTERSECT;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001085 /* If the user knows better the user should set the regdom
1086 * to their country before the IE is picked up */
1087 if (last_request->initiator == REGDOM_SET_BY_USER &&
1088 last_request->intersect)
1089 return -EOPNOTSUPP;
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001090 /* Process user requests only after previous user/driver/core
1091 * requests have been processed */
1092 if (last_request->initiator == REGDOM_SET_BY_CORE ||
1093 last_request->initiator == REGDOM_SET_BY_DRIVER ||
1094 last_request->initiator == REGDOM_SET_BY_USER) {
1095 if (!alpha2_equal(last_request->alpha2,
1096 cfg80211_regdomain->alpha2))
1097 return -EAGAIN;
1098 }
1099
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001100 if (!is_old_static_regdom(cfg80211_regdomain) &&
1101 alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
1102 return -EALREADY;
1103
Johannes Berg84fa4f42008-10-24 20:32:23 +02001104 return 0;
1105 }
1106
1107 return -EINVAL;
1108}
1109
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001110/* Caller must hold &cfg80211_drv_mutex */
1111int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001112 const char *alpha2,
1113 u32 country_ie_checksum,
1114 enum environment_cap env)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001115{
1116 struct regulatory_request *request;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001117 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001118 int r = 0;
1119
Johannes Bergbe3d4812008-10-24 20:32:21 +02001120 r = ignore_request(wiphy, set_by, alpha2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001121
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001122 if (r == REG_INTERSECT) {
1123 if (set_by == REGDOM_SET_BY_DRIVER) {
1124 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1125 if (r)
1126 return r;
1127 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001128 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001129 } else if (r) {
1130 /* If the regulatory domain being requested by the
1131 * driver has already been set just copy it to the
1132 * wiphy */
1133 if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
1134 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1135 if (r)
1136 return r;
1137 r = -EALREADY;
1138 goto new_request;
1139 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001140 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001141 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001142
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001143new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001144 request = kzalloc(sizeof(struct regulatory_request),
1145 GFP_KERNEL);
1146 if (!request)
1147 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001148
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001149 request->alpha2[0] = alpha2[0];
1150 request->alpha2[1] = alpha2[1];
1151 request->initiator = set_by;
1152 request->wiphy = wiphy;
1153 request->intersect = intersect;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001154 request->country_ie_checksum = country_ie_checksum;
1155 request->country_ie_env = env;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001156
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001157 kfree(last_request);
1158 last_request = request;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001159
1160 /* When r == REG_INTERSECT we do need to call CRDA */
1161 if (r < 0)
1162 return r;
1163
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001164 /*
1165 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1166 * AND if CRDA is NOT present nothing will happen, if someone
1167 * wants to bother with 11d with OLD_REG you can add a timer.
1168 * If after x amount of time nothing happens you can call:
1169 *
1170 * return set_regdom(country_ie_regdomain);
1171 *
1172 * to intersect with the static rd
1173 */
Luis R. Rodriguez02ba0b32008-11-12 14:22:00 -08001174 return call_crda(alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001175}
1176
Johannes Bergbe3d4812008-10-24 20:32:21 +02001177void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001178{
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001179 int r;
Johannes Bergbe3d4812008-10-24 20:32:21 +02001180 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001181
1182 mutex_lock(&cfg80211_drv_mutex);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001183 r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER,
1184 alpha2, 0, ENVIRON_ANY);
1185 /* This is required so that the orig_* parameters are saved */
1186 if (r == -EALREADY && wiphy->strict_regulatory)
1187 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_DRIVER);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001188 mutex_unlock(&cfg80211_drv_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001189}
1190EXPORT_SYMBOL(regulatory_hint);
1191
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001192static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1193 u32 country_ie_checksum)
1194{
1195 if (!last_request->wiphy)
1196 return false;
1197 if (likely(last_request->wiphy != wiphy))
1198 return !country_ie_integrity_changes(country_ie_checksum);
1199 /* We should not have let these through at this point, they
1200 * should have been picked up earlier by the first alpha2 check
1201 * on the device */
1202 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1203 return true;
1204 return false;
1205}
1206
1207void regulatory_hint_11d(struct wiphy *wiphy,
1208 u8 *country_ie,
1209 u8 country_ie_len)
1210{
1211 struct ieee80211_regdomain *rd = NULL;
1212 char alpha2[2];
1213 u32 checksum = 0;
1214 enum environment_cap env = ENVIRON_ANY;
1215
Johannes Berg0f70f392008-12-01 18:13:05 +01001216 if (!last_request)
1217 return;
1218
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001219 mutex_lock(&cfg80211_drv_mutex);
1220
1221 /* IE len must be evenly divisible by 2 */
1222 if (country_ie_len & 0x01)
1223 goto out;
1224
1225 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1226 goto out;
1227
1228 /* Pending country IE processing, this can happen after we
1229 * call CRDA and wait for a response if a beacon was received before
1230 * we were able to process the last regulatory_hint_11d() call */
1231 if (country_ie_regdomain)
1232 goto out;
1233
1234 alpha2[0] = country_ie[0];
1235 alpha2[1] = country_ie[1];
1236
1237 if (country_ie[2] == 'I')
1238 env = ENVIRON_INDOOR;
1239 else if (country_ie[2] == 'O')
1240 env = ENVIRON_OUTDOOR;
1241
1242 /* We will run this for *every* beacon processed for the BSSID, so
1243 * we optimize an early check to exit out early if we don't have to
1244 * do anything */
1245 if (likely(last_request->wiphy)) {
1246 struct cfg80211_registered_device *drv_last_ie;
1247
1248 drv_last_ie = wiphy_to_dev(last_request->wiphy);
1249
1250 /* Lets keep this simple -- we trust the first AP
1251 * after we intersect with CRDA */
1252 if (likely(last_request->wiphy == wiphy)) {
1253 /* Ignore IEs coming in on this wiphy with
1254 * the same alpha2 and environment cap */
1255 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1256 alpha2) &&
1257 env == drv_last_ie->env)) {
1258 goto out;
1259 }
1260 /* the wiphy moved on to another BSSID or the AP
1261 * was reconfigured. XXX: We need to deal with the
1262 * case where the user suspends and goes to goes
1263 * to another country, and then gets IEs from an
1264 * AP with different settings */
1265 goto out;
1266 } else {
1267 /* Ignore IEs coming in on two separate wiphys with
1268 * the same alpha2 and environment cap */
1269 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1270 alpha2) &&
1271 env == drv_last_ie->env)) {
1272 goto out;
1273 }
1274 /* We could potentially intersect though */
1275 goto out;
1276 }
1277 }
1278
1279 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1280 if (!rd)
1281 goto out;
1282
1283 /* This will not happen right now but we leave it here for the
1284 * the future when we want to add suspend/resume support and having
1285 * the user move to another country after doing so, or having the user
1286 * move to another AP. Right now we just trust the first AP. This is why
1287 * this is marked as likley(). If we hit this before we add this support
1288 * we want to be informed of it as it would indicate a mistake in the
1289 * current design */
1290 if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
1291 goto out;
1292
1293 /* We keep this around for when CRDA comes back with a response so
1294 * we can intersect with that */
1295 country_ie_regdomain = rd;
1296
1297 __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
1298 country_ie_regdomain->alpha2, checksum, env);
1299
1300out:
1301 mutex_unlock(&cfg80211_drv_mutex);
1302}
1303EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001304
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001305static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001306{
1307 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001308 const struct ieee80211_reg_rule *reg_rule = NULL;
1309 const struct ieee80211_freq_range *freq_range = NULL;
1310 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001311
1312 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1313 "(max_antenna_gain, max_eirp)\n");
1314
1315 for (i = 0; i < rd->n_reg_rules; i++) {
1316 reg_rule = &rd->reg_rules[i];
1317 freq_range = &reg_rule->freq_range;
1318 power_rule = &reg_rule->power_rule;
1319
1320 /* There may not be documentation for max antenna gain
1321 * in certain regions */
1322 if (power_rule->max_antenna_gain)
1323 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1324 "(%d mBi, %d mBm)\n",
1325 freq_range->start_freq_khz,
1326 freq_range->end_freq_khz,
1327 freq_range->max_bandwidth_khz,
1328 power_rule->max_antenna_gain,
1329 power_rule->max_eirp);
1330 else
1331 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1332 "(N/A, %d mBm)\n",
1333 freq_range->start_freq_khz,
1334 freq_range->end_freq_khz,
1335 freq_range->max_bandwidth_khz,
1336 power_rule->max_eirp);
1337 }
1338}
1339
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001340static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001341{
1342
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001343 if (is_intersected_alpha2(rd->alpha2)) {
1344 struct wiphy *wiphy = NULL;
1345 struct cfg80211_registered_device *drv;
1346
1347 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1348 if (last_request->wiphy) {
1349 wiphy = last_request->wiphy;
1350 drv = wiphy_to_dev(wiphy);
1351 printk(KERN_INFO "cfg80211: Current regulatory "
1352 "domain updated by AP to: %c%c\n",
1353 drv->country_ie_alpha2[0],
1354 drv->country_ie_alpha2[1]);
1355 } else
1356 printk(KERN_INFO "cfg80211: Current regulatory "
1357 "domain intersected: \n");
1358 } else
1359 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001360 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001361 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001362 printk(KERN_INFO "cfg80211: World regulatory "
1363 "domain updated:\n");
1364 else {
1365 if (is_unknown_alpha2(rd->alpha2))
1366 printk(KERN_INFO "cfg80211: Regulatory domain "
1367 "changed to driver built-in settings "
1368 "(unknown country)\n");
1369 else
1370 printk(KERN_INFO "cfg80211: Regulatory domain "
1371 "changed to country: %c%c\n",
1372 rd->alpha2[0], rd->alpha2[1]);
1373 }
1374 print_rd_rules(rd);
1375}
1376
Johannes Berg2df78162008-10-28 16:49:41 +01001377static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001378{
1379 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1380 rd->alpha2[0], rd->alpha2[1]);
1381 print_rd_rules(rd);
1382}
1383
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001384#ifdef CONFIG_CFG80211_REG_DEBUG
1385static void reg_country_ie_process_debug(
1386 const struct ieee80211_regdomain *rd,
1387 const struct ieee80211_regdomain *country_ie_regdomain,
1388 const struct ieee80211_regdomain *intersected_rd)
1389{
1390 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1391 print_regdomain_info(country_ie_regdomain);
1392 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1393 print_regdomain_info(rd);
1394 if (intersected_rd) {
1395 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1396 "and get:\n");
1397 print_regdomain_info(rd);
1398 return;
1399 }
1400 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1401}
1402#else
1403static inline void reg_country_ie_process_debug(
1404 const struct ieee80211_regdomain *rd,
1405 const struct ieee80211_regdomain *country_ie_regdomain,
1406 const struct ieee80211_regdomain *intersected_rd)
1407{
1408}
1409#endif
1410
Johannes Bergd2372b32008-10-24 20:32:20 +02001411/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001412static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001413{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001414 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001415 struct cfg80211_registered_device *drv = NULL;
1416 struct wiphy *wiphy = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001417 /* Some basic sanity checks first */
1418
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001419 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001420 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001421 return -EINVAL;
1422 update_world_regdomain(rd);
1423 return 0;
1424 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001425
1426 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1427 !is_unknown_alpha2(rd->alpha2))
1428 return -EINVAL;
1429
Johannes Bergf6037d02008-10-21 11:01:33 +02001430 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001431 return -EINVAL;
1432
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001433 /* Lets only bother proceeding on the same alpha2 if the current
1434 * rd is non static (it means CRDA was present and was used last)
1435 * and the pending request came in from a country IE */
1436 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1437 /* If someone else asked us to change the rd lets only bother
1438 * checking if the alpha2 changes if CRDA was already called */
1439 if (!is_old_static_regdom(cfg80211_regdomain) &&
1440 !regdom_changed(rd->alpha2))
1441 return -EINVAL;
1442 }
1443
1444 wiphy = last_request->wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001445
1446 /* Now lets set the regulatory domain, update all driver channels
1447 * and finally inform them of what we have done, in case they want
1448 * to review or adjust their own settings based on their own
1449 * internal EEPROM data */
1450
Johannes Bergf6037d02008-10-21 11:01:33 +02001451 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001452 return -EINVAL;
1453
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001454 if (!is_valid_rd(rd)) {
1455 printk(KERN_ERR "cfg80211: Invalid "
1456 "regulatory domain detected:\n");
1457 print_regdomain_info(rd);
1458 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001459 }
1460
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001461 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001462 int r;
1463
1464 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1465 reset_regdomains();
1466 cfg80211_regdomain = rd;
1467 return 0;
1468 }
1469
1470 /* For a driver hint, lets copy the regulatory domain the
1471 * driver wanted to the wiphy to deal with conflicts */
1472
1473 BUG_ON(last_request->wiphy->regd);
1474
1475 r = reg_copy_regd(&last_request->wiphy->regd, rd);
1476 if (r)
1477 return r;
1478
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001479 reset_regdomains();
1480 cfg80211_regdomain = rd;
1481 return 0;
1482 }
1483
1484 /* Intersection requires a bit more work */
1485
1486 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1487
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001488 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1489 if (!intersected_rd)
1490 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001491
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001492 /* We can trash what CRDA provided now.
1493 * However if a driver requested this specific regulatory
1494 * domain we keep it for its private use */
1495 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
1496 last_request->wiphy->regd = rd;
1497 else
1498 kfree(rd);
1499
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001500 rd = NULL;
1501
1502 reset_regdomains();
1503 cfg80211_regdomain = intersected_rd;
1504
1505 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001506 }
1507
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001508 /*
1509 * Country IE requests are handled a bit differently, we intersect
1510 * the country IE rd with what CRDA believes that country should have
1511 */
1512
1513 BUG_ON(!country_ie_regdomain);
1514
1515 if (rd != country_ie_regdomain) {
1516 /* Intersect what CRDA returned and our what we
1517 * had built from the Country IE received */
1518
1519 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1520
1521 reg_country_ie_process_debug(rd, country_ie_regdomain,
1522 intersected_rd);
1523
1524 kfree(country_ie_regdomain);
1525 country_ie_regdomain = NULL;
1526 } else {
1527 /* This would happen when CRDA was not present and
1528 * OLD_REGULATORY was enabled. We intersect our Country
1529 * IE rd and what was set on cfg80211 originally */
1530 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1531 }
1532
1533 if (!intersected_rd)
1534 return -EINVAL;
1535
1536 drv = wiphy_to_dev(wiphy);
1537
1538 drv->country_ie_alpha2[0] = rd->alpha2[0];
1539 drv->country_ie_alpha2[1] = rd->alpha2[1];
1540 drv->env = last_request->country_ie_env;
1541
1542 BUG_ON(intersected_rd == rd);
1543
1544 kfree(rd);
1545 rd = NULL;
1546
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001547 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001548 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001549
1550 return 0;
1551}
1552
1553
1554/* Use this call to set the current regulatory domain. Conflicts with
1555 * multiple drivers can be ironed out later. Caller must've already
Johannes Bergd2372b32008-10-24 20:32:20 +02001556 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001557int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001558{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001559 int r;
1560
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001561 /* Note that this doesn't update the wiphys, this is done below */
1562 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02001563 if (r) {
1564 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001565 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02001566 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001567
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001568 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001569 if (!last_request->intersect)
1570 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001571
1572 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02001573 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001574
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001575 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001576
1577 return r;
1578}
1579
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001580/* Caller must hold cfg80211_drv_mutex */
1581void reg_device_remove(struct wiphy *wiphy)
1582{
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001583 kfree(wiphy->regd);
Johannes Berg4a4f4d82008-12-01 12:07:56 +01001584 if (!last_request || !last_request->wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001585 return;
1586 if (last_request->wiphy != wiphy)
1587 return;
1588 last_request->wiphy = NULL;
1589 last_request->country_ie_env = ENVIRON_ANY;
1590}
1591
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001592int regulatory_init(void)
1593{
Johannes Berg734366d2008-09-15 10:56:48 +02001594 int err;
1595
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001596 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1597 if (IS_ERR(reg_pdev))
1598 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001599
1600#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001601 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02001602
Johannes Berg942b25c2008-09-15 11:26:47 +02001603 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02001604 print_regdomain_info(cfg80211_regdomain);
1605 /* The old code still requests for a new regdomain and if
1606 * you have CRDA you get it updated, otherwise you get
1607 * stuck with the static values. We ignore "EU" code as
1608 * that is not a valid ISO / IEC 3166 alpha2 */
Johannes Bergac9440a2008-10-21 11:08:27 +02001609 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
Johannes Berg734366d2008-09-15 10:56:48 +02001610 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001611 ieee80211_regdom, 0, ENVIRON_ANY);
Johannes Berg734366d2008-09-15 10:56:48 +02001612#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001613 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02001614
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001615 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
Johannes Berg734366d2008-09-15 10:56:48 +02001616 if (err)
1617 printk(KERN_ERR "cfg80211: calling CRDA failed - "
1618 "unable to update world regulatory domain, "
1619 "using static definition\n");
1620#endif
1621
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001622 return 0;
1623}
1624
1625void regulatory_exit(void)
1626{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001627 mutex_lock(&cfg80211_drv_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02001628
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001629 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02001630
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001631 kfree(country_ie_regdomain);
1632 country_ie_regdomain = NULL;
1633
Johannes Bergf6037d02008-10-21 11:01:33 +02001634 kfree(last_request);
1635
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001636 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001637
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001638 mutex_unlock(&cfg80211_drv_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01001639}