blob: 0b8c4b86789a40c3ff536edc1679f83f20799072 [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 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080060const 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
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050067static LIST_HEAD(reg_requests_list);
68static spinlock_t reg_requests_lock;
69
Johannes Berg734366d2008-09-15 10:56:48 +020070/* We keep a static world regulatory domain in case of the absence of CRDA */
71static const struct ieee80211_regdomain world_regdom = {
72 .n_reg_rules = 1,
73 .alpha2 = "00",
74 .reg_rules = {
75 REG_RULE(2412-10, 2462+10, 40, 6, 20,
76 NL80211_RRF_PASSIVE_SCAN |
77 NL80211_RRF_NO_IBSS),
78 }
79};
80
Johannes Berga3d2eaf2008-09-15 11:10:52 +020081static const struct ieee80211_regdomain *cfg80211_world_regdom =
82 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +020083
84#ifdef CONFIG_WIRELESS_OLD_REGULATORY
85static char *ieee80211_regdom = "US";
86module_param(ieee80211_regdom, charp, 0444);
87MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
88
89/* We assume 40 MHz bandwidth for the old regulatory work.
90 * We make emphasis we are using the exact same frequencies
91 * as before */
92
93static const struct ieee80211_regdomain us_regdom = {
94 .n_reg_rules = 6,
95 .alpha2 = "US",
96 .reg_rules = {
97 /* IEEE 802.11b/g, channels 1..11 */
98 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
99 /* IEEE 802.11a, channel 36 */
100 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
101 /* IEEE 802.11a, channel 40 */
102 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
103 /* IEEE 802.11a, channel 44 */
104 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
105 /* IEEE 802.11a, channels 48..64 */
106 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
107 /* IEEE 802.11a, channels 149..165, outdoor */
108 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
109 }
110};
111
112static const struct ieee80211_regdomain jp_regdom = {
113 .n_reg_rules = 3,
114 .alpha2 = "JP",
115 .reg_rules = {
116 /* IEEE 802.11b/g, channels 1..14 */
117 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
118 /* IEEE 802.11a, channels 34..48 */
119 REG_RULE(5170-10, 5240+10, 40, 6, 20,
120 NL80211_RRF_PASSIVE_SCAN),
121 /* IEEE 802.11a, channels 52..64 */
122 REG_RULE(5260-10, 5320+10, 40, 6, 20,
123 NL80211_RRF_NO_IBSS |
124 NL80211_RRF_DFS),
125 }
126};
127
128static const struct ieee80211_regdomain eu_regdom = {
129 .n_reg_rules = 6,
130 /* This alpha2 is bogus, we leave it here just for stupid
131 * backward compatibility */
132 .alpha2 = "EU",
133 .reg_rules = {
134 /* IEEE 802.11b/g, channels 1..13 */
135 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
136 /* IEEE 802.11a, channel 36 */
137 REG_RULE(5180-10, 5180+10, 40, 6, 23,
138 NL80211_RRF_PASSIVE_SCAN),
139 /* IEEE 802.11a, channel 40 */
140 REG_RULE(5200-10, 5200+10, 40, 6, 23,
141 NL80211_RRF_PASSIVE_SCAN),
142 /* IEEE 802.11a, channel 44 */
143 REG_RULE(5220-10, 5220+10, 40, 6, 23,
144 NL80211_RRF_PASSIVE_SCAN),
145 /* IEEE 802.11a, channels 48..64 */
146 REG_RULE(5240-10, 5320+10, 40, 6, 20,
147 NL80211_RRF_NO_IBSS |
148 NL80211_RRF_DFS),
149 /* IEEE 802.11a, channels 100..140 */
150 REG_RULE(5500-10, 5700+10, 40, 6, 30,
151 NL80211_RRF_NO_IBSS |
152 NL80211_RRF_DFS),
153 }
154};
155
156static const struct ieee80211_regdomain *static_regdom(char *alpha2)
157{
158 if (alpha2[0] == 'U' && alpha2[1] == 'S')
159 return &us_regdom;
160 if (alpha2[0] == 'J' && alpha2[1] == 'P')
161 return &jp_regdom;
162 if (alpha2[0] == 'E' && alpha2[1] == 'U')
163 return &eu_regdom;
164 /* Default, as per the old rules */
165 return &us_regdom;
166}
167
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200168static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200169{
170 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
171 return true;
172 return false;
173}
Johannes Berg734366d2008-09-15 10:56:48 +0200174#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200175static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
176{
177 return false;
178}
179#endif
180
Johannes Berg734366d2008-09-15 10:56:48 +0200181static void reset_regdomains(void)
182{
Johannes Berg942b25c2008-09-15 11:26:47 +0200183 /* avoid freeing static information or freeing something twice */
184 if (cfg80211_regdomain == cfg80211_world_regdom)
185 cfg80211_regdomain = NULL;
186 if (cfg80211_world_regdom == &world_regdom)
187 cfg80211_world_regdom = NULL;
188 if (cfg80211_regdomain == &world_regdom)
189 cfg80211_regdomain = NULL;
190 if (is_old_static_regdom(cfg80211_regdomain))
191 cfg80211_regdomain = NULL;
192
193 kfree(cfg80211_regdomain);
194 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200195
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200196 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200197 cfg80211_regdomain = NULL;
198}
199
200/* Dynamic world regulatory domain requested by the wireless
201 * core upon initialization */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200202static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200203{
Johannes Bergf6037d02008-10-21 11:01:33 +0200204 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200205
206 reset_regdomains();
207
208 cfg80211_world_regdom = rd;
209 cfg80211_regdomain = rd;
210}
Johannes Berg734366d2008-09-15 10:56:48 +0200211
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200212bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100213{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700214 if (!alpha2)
215 return false;
216 if (alpha2[0] == '0' && alpha2[1] == '0')
217 return true;
218 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100219}
220
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200221static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700222{
223 if (!alpha2)
224 return false;
225 if (alpha2[0] != 0 && alpha2[1] != 0)
226 return true;
227 return false;
228}
Johannes Berg8318d782008-01-24 19:38:38 +0100229
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700230static bool is_alpha_upper(char letter)
231{
232 /* ASCII A - Z */
233 if (letter >= 65 && letter <= 90)
234 return true;
235 return false;
236}
237
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200238static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700239{
240 if (!alpha2)
241 return false;
242 /* Special case where regulatory domain was built by driver
243 * but a specific alpha2 cannot be determined */
244 if (alpha2[0] == '9' && alpha2[1] == '9')
245 return true;
246 return false;
247}
248
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800249static bool is_intersected_alpha2(const char *alpha2)
250{
251 if (!alpha2)
252 return false;
253 /* Special case where regulatory domain is the
254 * result of an intersection between two regulatory domain
255 * structures */
256 if (alpha2[0] == '9' && alpha2[1] == '8')
257 return true;
258 return false;
259}
260
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200261static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700262{
263 if (!alpha2)
264 return false;
265 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
266 return true;
267 return false;
268}
269
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200270static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700271{
272 if (!alpha2_x || !alpha2_y)
273 return false;
274 if (alpha2_x[0] == alpha2_y[0] &&
275 alpha2_x[1] == alpha2_y[1])
276 return true;
277 return false;
278}
279
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200280static bool regdom_changed(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700281{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500282 assert_cfg80211_lock();
283
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700284 if (!cfg80211_regdomain)
285 return true;
286 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
287 return false;
288 return true;
289}
290
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800291/**
292 * country_ie_integrity_changes - tells us if the country IE has changed
293 * @checksum: checksum of country IE of fields we are interested in
294 *
295 * If the country IE has not changed you can ignore it safely. This is
296 * useful to determine if two devices are seeing two different country IEs
297 * even on the same alpha2. Note that this will return false if no IE has
298 * been set on the wireless core yet.
299 */
300static bool country_ie_integrity_changes(u32 checksum)
301{
302 /* If no IE has been set then the checksum doesn't change */
303 if (unlikely(!last_request->country_ie_checksum))
304 return false;
305 if (unlikely(last_request->country_ie_checksum != checksum))
306 return true;
307 return false;
308}
309
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700310/* This lets us keep regulatory code which is updated on a regulatory
311 * basis in userspace. */
312static int call_crda(const char *alpha2)
313{
314 char country_env[9 + 2] = "COUNTRY=";
315 char *envp[] = {
316 country_env,
317 NULL
318 };
319
320 if (!is_world_regdom((char *) alpha2))
321 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
322 alpha2[0], alpha2[1]);
323 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700324 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
325 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700326
327 country_env[8] = alpha2[0];
328 country_env[9] = alpha2[1];
329
330 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
331}
332
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700333/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200334bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700335{
Johannes Bergf6037d02008-10-21 11:01:33 +0200336 if (!last_request)
337 return false;
338
339 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700340}
341
342/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200343static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700344{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200345 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700346 u32 freq_diff;
347
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800348 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700349 return false;
350
351 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
352 return false;
353
354 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
355
Luis R. Rodriguezd71aaf62008-10-30 13:33:52 -0700356 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700357 return false;
358
359 return true;
360}
361
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200362static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700363{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200364 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700365 unsigned int i;
366
367 if (!rd->n_reg_rules)
368 return false;
369
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800370 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
371 return false;
372
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700373 for (i = 0; i < rd->n_reg_rules; i++) {
374 reg_rule = &rd->reg_rules[i];
375 if (!is_valid_reg_rule(reg_rule))
376 return false;
377 }
378
379 return true;
380}
381
382/* Returns value in KHz */
383static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
384 u32 freq)
385{
386 unsigned int i;
387 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
388 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
389 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
390 if (start_freq_khz >= freq_range->start_freq_khz &&
391 end_freq_khz <= freq_range->end_freq_khz)
392 return supported_bandwidths[i];
393 }
394 return 0;
395}
396
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800397/**
398 * freq_in_rule_band - tells us if a frequency is in a frequency band
399 * @freq_range: frequency rule we want to query
400 * @freq_khz: frequency we are inquiring about
401 *
402 * This lets us know if a specific frequency rule is or is not relevant to
403 * a specific frequency's band. Bands are device specific and artificial
404 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
405 * safe for now to assume that a frequency rule should not be part of a
406 * frequency's band if the start freq or end freq are off by more than 2 GHz.
407 * This resolution can be lowered and should be considered as we add
408 * regulatory rule support for other "bands".
409 **/
410static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
411 u32 freq_khz)
412{
413#define ONE_GHZ_IN_KHZ 1000000
414 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
415 return true;
416 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
417 return true;
418 return false;
419#undef ONE_GHZ_IN_KHZ
420}
421
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800422/* Converts a country IE to a regulatory domain. A regulatory domain
423 * structure has a lot of information which the IE doesn't yet have,
424 * so for the other values we use upper max values as we will intersect
425 * with our userspace regulatory agent to get lower bounds. */
426static struct ieee80211_regdomain *country_ie_2_rd(
427 u8 *country_ie,
428 u8 country_ie_len,
429 u32 *checksum)
430{
431 struct ieee80211_regdomain *rd = NULL;
432 unsigned int i = 0;
433 char alpha2[2];
434 u32 flags = 0;
435 u32 num_rules = 0, size_of_regd = 0;
436 u8 *triplets_start = NULL;
437 u8 len_at_triplet = 0;
438 /* the last channel we have registered in a subband (triplet) */
439 int last_sub_max_channel = 0;
440
441 *checksum = 0xDEADBEEF;
442
443 /* Country IE requirements */
444 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
445 country_ie_len & 0x01);
446
447 alpha2[0] = country_ie[0];
448 alpha2[1] = country_ie[1];
449
450 /*
451 * Third octet can be:
452 * 'I' - Indoor
453 * 'O' - Outdoor
454 *
455 * anything else we assume is no restrictions
456 */
457 if (country_ie[2] == 'I')
458 flags = NL80211_RRF_NO_OUTDOOR;
459 else if (country_ie[2] == 'O')
460 flags = NL80211_RRF_NO_INDOOR;
461
462 country_ie += 3;
463 country_ie_len -= 3;
464
465 triplets_start = country_ie;
466 len_at_triplet = country_ie_len;
467
468 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
469
470 /* We need to build a reg rule for each triplet, but first we must
471 * calculate the number of reg rules we will need. We will need one
472 * for each channel subband */
473 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800474 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800475 struct ieee80211_country_ie_triplet *triplet =
476 (struct ieee80211_country_ie_triplet *) country_ie;
477 int cur_sub_max_channel = 0, cur_channel = 0;
478
479 if (triplet->ext.reg_extension_id >=
480 IEEE80211_COUNTRY_EXTENSION_ID) {
481 country_ie += 3;
482 country_ie_len -= 3;
483 continue;
484 }
485
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800486 /* 2 GHz */
487 if (triplet->chans.first_channel <= 14)
488 end_channel = triplet->chans.first_channel +
489 triplet->chans.num_channels;
490 else
491 /*
492 * 5 GHz -- For example in country IEs if the first
493 * channel given is 36 and the number of channels is 4
494 * then the individual channel numbers defined for the
495 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
496 * and not 36, 37, 38, 39.
497 *
498 * See: http://tinyurl.com/11d-clarification
499 */
500 end_channel = triplet->chans.first_channel +
501 (4 * (triplet->chans.num_channels - 1));
502
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800503 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800504 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800505
506 /* Basic sanity check */
507 if (cur_sub_max_channel < cur_channel)
508 return NULL;
509
510 /* Do not allow overlapping channels. Also channels
511 * passed in each subband must be monotonically
512 * increasing */
513 if (last_sub_max_channel) {
514 if (cur_channel <= last_sub_max_channel)
515 return NULL;
516 if (cur_sub_max_channel <= last_sub_max_channel)
517 return NULL;
518 }
519
520 /* When dot11RegulatoryClassesRequired is supported
521 * we can throw ext triplets as part of this soup,
522 * for now we don't care when those change as we
523 * don't support them */
524 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
525 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
526 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
527
528 last_sub_max_channel = cur_sub_max_channel;
529
530 country_ie += 3;
531 country_ie_len -= 3;
532 num_rules++;
533
534 /* Note: this is not a IEEE requirement but
535 * simply a memory requirement */
536 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
537 return NULL;
538 }
539
540 country_ie = triplets_start;
541 country_ie_len = len_at_triplet;
542
543 size_of_regd = sizeof(struct ieee80211_regdomain) +
544 (num_rules * sizeof(struct ieee80211_reg_rule));
545
546 rd = kzalloc(size_of_regd, GFP_KERNEL);
547 if (!rd)
548 return NULL;
549
550 rd->n_reg_rules = num_rules;
551 rd->alpha2[0] = alpha2[0];
552 rd->alpha2[1] = alpha2[1];
553
554 /* This time around we fill in the rd */
555 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800556 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800557 struct ieee80211_country_ie_triplet *triplet =
558 (struct ieee80211_country_ie_triplet *) country_ie;
559 struct ieee80211_reg_rule *reg_rule = NULL;
560 struct ieee80211_freq_range *freq_range = NULL;
561 struct ieee80211_power_rule *power_rule = NULL;
562
563 /* Must parse if dot11RegulatoryClassesRequired is true,
564 * we don't support this yet */
565 if (triplet->ext.reg_extension_id >=
566 IEEE80211_COUNTRY_EXTENSION_ID) {
567 country_ie += 3;
568 country_ie_len -= 3;
569 continue;
570 }
571
572 reg_rule = &rd->reg_rules[i];
573 freq_range = &reg_rule->freq_range;
574 power_rule = &reg_rule->power_rule;
575
576 reg_rule->flags = flags;
577
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800578 /* 2 GHz */
579 if (triplet->chans.first_channel <= 14)
580 end_channel = triplet->chans.first_channel +
581 triplet->chans.num_channels;
582 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800583 end_channel = triplet->chans.first_channel +
584 (4 * (triplet->chans.num_channels - 1));
585
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800586 /* The +10 is since the regulatory domain expects
587 * the actual band edge, not the center of freq for
588 * its start and end freqs, assuming 20 MHz bandwidth on
589 * the channels passed */
590 freq_range->start_freq_khz =
591 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
592 triplet->chans.first_channel) - 10);
593 freq_range->end_freq_khz =
594 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800595 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800596
597 /* Large arbitrary values, we intersect later */
598 /* Increment this if we ever support >= 40 MHz channels
599 * in IEEE 802.11 */
600 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
601 power_rule->max_antenna_gain = DBI_TO_MBI(100);
602 power_rule->max_eirp = DBM_TO_MBM(100);
603
604 country_ie += 3;
605 country_ie_len -= 3;
606 i++;
607
608 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
609 }
610
611 return rd;
612}
613
614
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700615/* Helper for regdom_intersect(), this does the real
616 * mathematical intersection fun */
617static int reg_rules_intersect(
618 const struct ieee80211_reg_rule *rule1,
619 const struct ieee80211_reg_rule *rule2,
620 struct ieee80211_reg_rule *intersected_rule)
621{
622 const struct ieee80211_freq_range *freq_range1, *freq_range2;
623 struct ieee80211_freq_range *freq_range;
624 const struct ieee80211_power_rule *power_rule1, *power_rule2;
625 struct ieee80211_power_rule *power_rule;
626 u32 freq_diff;
627
628 freq_range1 = &rule1->freq_range;
629 freq_range2 = &rule2->freq_range;
630 freq_range = &intersected_rule->freq_range;
631
632 power_rule1 = &rule1->power_rule;
633 power_rule2 = &rule2->power_rule;
634 power_rule = &intersected_rule->power_rule;
635
636 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
637 freq_range2->start_freq_khz);
638 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
639 freq_range2->end_freq_khz);
640 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
641 freq_range2->max_bandwidth_khz);
642
643 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
644 if (freq_range->max_bandwidth_khz > freq_diff)
645 freq_range->max_bandwidth_khz = freq_diff;
646
647 power_rule->max_eirp = min(power_rule1->max_eirp,
648 power_rule2->max_eirp);
649 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
650 power_rule2->max_antenna_gain);
651
652 intersected_rule->flags = (rule1->flags | rule2->flags);
653
654 if (!is_valid_reg_rule(intersected_rule))
655 return -EINVAL;
656
657 return 0;
658}
659
660/**
661 * regdom_intersect - do the intersection between two regulatory domains
662 * @rd1: first regulatory domain
663 * @rd2: second regulatory domain
664 *
665 * Use this function to get the intersection between two regulatory domains.
666 * Once completed we will mark the alpha2 for the rd as intersected, "98",
667 * as no one single alpha2 can represent this regulatory domain.
668 *
669 * Returns a pointer to the regulatory domain structure which will hold the
670 * resulting intersection of rules between rd1 and rd2. We will
671 * kzalloc() this structure for you.
672 */
673static struct ieee80211_regdomain *regdom_intersect(
674 const struct ieee80211_regdomain *rd1,
675 const struct ieee80211_regdomain *rd2)
676{
677 int r, size_of_regd;
678 unsigned int x, y;
679 unsigned int num_rules = 0, rule_idx = 0;
680 const struct ieee80211_reg_rule *rule1, *rule2;
681 struct ieee80211_reg_rule *intersected_rule;
682 struct ieee80211_regdomain *rd;
683 /* This is just a dummy holder to help us count */
684 struct ieee80211_reg_rule irule;
685
686 /* Uses the stack temporarily for counter arithmetic */
687 intersected_rule = &irule;
688
689 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
690
691 if (!rd1 || !rd2)
692 return NULL;
693
694 /* First we get a count of the rules we'll need, then we actually
695 * build them. This is to so we can malloc() and free() a
696 * regdomain once. The reason we use reg_rules_intersect() here
697 * is it will return -EINVAL if the rule computed makes no sense.
698 * All rules that do check out OK are valid. */
699
700 for (x = 0; x < rd1->n_reg_rules; x++) {
701 rule1 = &rd1->reg_rules[x];
702 for (y = 0; y < rd2->n_reg_rules; y++) {
703 rule2 = &rd2->reg_rules[y];
704 if (!reg_rules_intersect(rule1, rule2,
705 intersected_rule))
706 num_rules++;
707 memset(intersected_rule, 0,
708 sizeof(struct ieee80211_reg_rule));
709 }
710 }
711
712 if (!num_rules)
713 return NULL;
714
715 size_of_regd = sizeof(struct ieee80211_regdomain) +
716 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
717
718 rd = kzalloc(size_of_regd, GFP_KERNEL);
719 if (!rd)
720 return NULL;
721
722 for (x = 0; x < rd1->n_reg_rules; x++) {
723 rule1 = &rd1->reg_rules[x];
724 for (y = 0; y < rd2->n_reg_rules; y++) {
725 rule2 = &rd2->reg_rules[y];
726 /* This time around instead of using the stack lets
727 * write to the target rule directly saving ourselves
728 * a memcpy() */
729 intersected_rule = &rd->reg_rules[rule_idx];
730 r = reg_rules_intersect(rule1, rule2,
731 intersected_rule);
732 /* No need to memset here the intersected rule here as
733 * we're not using the stack anymore */
734 if (r)
735 continue;
736 rule_idx++;
737 }
738 }
739
740 if (rule_idx != num_rules) {
741 kfree(rd);
742 return NULL;
743 }
744
745 rd->n_reg_rules = num_rules;
746 rd->alpha2[0] = '9';
747 rd->alpha2[1] = '8';
748
749 return rd;
750}
751
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700752/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
753 * want to just have the channel structure use these */
754static u32 map_regdom_flags(u32 rd_flags)
755{
756 u32 channel_flags = 0;
757 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
758 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
759 if (rd_flags & NL80211_RRF_NO_IBSS)
760 channel_flags |= IEEE80211_CHAN_NO_IBSS;
761 if (rd_flags & NL80211_RRF_DFS)
762 channel_flags |= IEEE80211_CHAN_RADAR;
763 return channel_flags;
764}
765
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800766static int freq_reg_info_regd(struct wiphy *wiphy,
767 u32 center_freq,
768 u32 *bandwidth,
769 const struct ieee80211_reg_rule **reg_rule,
770 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100771{
772 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800773 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800774 const struct ieee80211_regdomain *regd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700775 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100776
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800777 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800778
779 /* Follow the driver's regulatory domain, if present, unless a country
Luis R. Rodriguez24ed1da2009-01-22 15:05:54 -0800780 * IE has been processed or a user wants to help complaince further */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800781 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez24ed1da2009-01-22 15:05:54 -0800782 last_request->initiator != REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800783 wiphy->regd)
784 regd = wiphy->regd;
785
786 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700787 return -EINVAL;
788
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800789 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700790 const struct ieee80211_reg_rule *rr;
791 const struct ieee80211_freq_range *fr = NULL;
792 const struct ieee80211_power_rule *pr = NULL;
793
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800794 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700795 fr = &rr->freq_range;
796 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800797
798 /* We only need to know if one frequency rule was
799 * was in center_freq's band, that's enough, so lets
800 * not overwrite it once found */
801 if (!band_rule_found)
802 band_rule_found = freq_in_rule_band(fr, center_freq);
803
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700804 max_bandwidth = freq_max_bandwidth(fr, center_freq);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800805
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700806 if (max_bandwidth && *bandwidth <= max_bandwidth) {
807 *reg_rule = rr;
808 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100809 break;
810 }
811 }
812
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800813 if (!band_rule_found)
814 return -ERANGE;
815
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700816 return !max_bandwidth;
817}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800818EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700819
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800820int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800821 const struct ieee80211_reg_rule **reg_rule)
822{
823 return freq_reg_info_regd(wiphy, center_freq,
824 bandwidth, reg_rule, NULL);
825}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700826
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800827static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
828 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700829{
830 int r;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800831 u32 flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700832 u32 max_bandwidth = 0;
833 const struct ieee80211_reg_rule *reg_rule = NULL;
834 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800835 struct ieee80211_supported_band *sband;
836 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500837 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800838
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500839 assert_cfg80211_lock();
840
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500841 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
842
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800843 sband = wiphy->bands[band];
844 BUG_ON(chan_idx >= sband->n_channels);
845 chan = &sband->channels[chan_idx];
846
847 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700848
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800849 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700850 &max_bandwidth, &reg_rule);
851
852 if (r) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800853 /* This means no regulatory rule was found in the country IE
854 * with a frequency range on the center_freq's band, since
855 * IEEE-802.11 allows for a country IE to have a subset of the
856 * regulatory information provided in a country we ignore
857 * disabling the channel unless at least one reg rule was
858 * found on the center_freq's band. For details see this
859 * clarification:
860 *
861 * http://tinyurl.com/11d-clarification
862 */
863 if (r == -ERANGE &&
864 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
865#ifdef CONFIG_CFG80211_REG_DEBUG
866 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
867 "intact on %s - no rule found in band on "
868 "Country IE\n",
869 chan->center_freq, wiphy_name(wiphy));
870#endif
871 } else {
872 /* In this case we know the country IE has at least one reg rule
873 * for the band so we respect its band definitions */
874#ifdef CONFIG_CFG80211_REG_DEBUG
875 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
876 printk(KERN_DEBUG "cfg80211: Disabling "
877 "channel %d MHz on %s due to "
878 "Country IE\n",
879 chan->center_freq, wiphy_name(wiphy));
880#endif
881 flags |= IEEE80211_CHAN_DISABLED;
882 chan->flags = flags;
883 }
Johannes Berg8318d782008-01-24 19:38:38 +0100884 return;
885 }
886
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700887 power_rule = &reg_rule->power_rule;
888
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800889 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500890 request_wiphy && request_wiphy == wiphy &&
891 request_wiphy->strict_regulatory) {
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800892 /* This gaurantees the driver's requested regulatory domain
893 * will always be used as a base for further regulatory
894 * settings */
895 chan->flags = chan->orig_flags =
896 map_regdom_flags(reg_rule->flags);
897 chan->max_antenna_gain = chan->orig_mag =
898 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
899 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
900 chan->max_power = chan->orig_mpwr =
901 (int) MBM_TO_DBM(power_rule->max_eirp);
902 return;
903 }
904
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700905 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100906 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700907 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
908 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400909 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700910 chan->max_power = min(chan->orig_mpwr,
911 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400912 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700913 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100914}
915
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800916static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +0100917{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800918 unsigned int i;
919 struct ieee80211_supported_band *sband;
920
921 BUG_ON(!wiphy->bands[band]);
922 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +0100923
924 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800925 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +0100926}
927
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800928static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
929{
930 if (!last_request)
931 return true;
932 if (setby == REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -0800933 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800934 return true;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800935 /* wiphy->regd will be set once the device has its own
936 * desired regulatory domain set */
937 if (wiphy->strict_regulatory && !wiphy->regd &&
938 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800939 return true;
940 return false;
941}
942
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700943static void update_all_wiphy_regulatory(enum reg_set_by setby)
944{
945 struct cfg80211_registered_device *drv;
946
947 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -0800948 wiphy_update_regulatory(&drv->wiphy, setby);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700949}
950
951void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
Johannes Berg8318d782008-01-24 19:38:38 +0100952{
953 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -0800954
955 if (ignore_reg_update(wiphy, setby))
956 return;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700957 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100958 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800959 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700960 }
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -0800961 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -0800962 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700963}
964
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800965static void handle_channel_custom(struct wiphy *wiphy,
966 enum ieee80211_band band,
967 unsigned int chan_idx,
968 const struct ieee80211_regdomain *regd)
969{
970 int r;
971 u32 max_bandwidth = 0;
972 const struct ieee80211_reg_rule *reg_rule = NULL;
973 const struct ieee80211_power_rule *power_rule = NULL;
974 struct ieee80211_supported_band *sband;
975 struct ieee80211_channel *chan;
976
977 sband = wiphy->bands[band];
978 BUG_ON(chan_idx >= sband->n_channels);
979 chan = &sband->channels[chan_idx];
980
981 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
982 &max_bandwidth, &reg_rule, regd);
983
984 if (r) {
985 chan->flags = IEEE80211_CHAN_DISABLED;
986 return;
987 }
988
989 power_rule = &reg_rule->power_rule;
990
991 chan->flags |= map_regdom_flags(reg_rule->flags);
992 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
993 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
994 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
995}
996
997static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
998 const struct ieee80211_regdomain *regd)
999{
1000 unsigned int i;
1001 struct ieee80211_supported_band *sband;
1002
1003 BUG_ON(!wiphy->bands[band]);
1004 sband = wiphy->bands[band];
1005
1006 for (i = 0; i < sband->n_channels; i++)
1007 handle_channel_custom(wiphy, band, i, regd);
1008}
1009
1010/* Used by drivers prior to wiphy registration */
1011void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1012 const struct ieee80211_regdomain *regd)
1013{
1014 enum ieee80211_band band;
1015 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1016 if (wiphy->bands[band])
1017 handle_band_custom(wiphy, band, regd);
1018 }
1019}
1020EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1021
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001022static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1023 const struct ieee80211_regdomain *src_regd)
1024{
1025 struct ieee80211_regdomain *regd;
1026 int size_of_regd = 0;
1027 unsigned int i;
1028
1029 size_of_regd = sizeof(struct ieee80211_regdomain) +
1030 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1031
1032 regd = kzalloc(size_of_regd, GFP_KERNEL);
1033 if (!regd)
1034 return -ENOMEM;
1035
1036 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1037
1038 for (i = 0; i < src_regd->n_reg_rules; i++)
1039 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1040 sizeof(struct ieee80211_reg_rule));
1041
1042 *dst_regd = regd;
1043 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001044}
1045
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001046/* Return value which can be used by ignore_request() to indicate
1047 * it has been determined we should intersect two regulatory domains */
1048#define REG_INTERSECT 1
1049
Johannes Berg84fa4f42008-10-24 20:32:23 +02001050/* This has the logic which determines when a new request
1051 * should be ignored. */
1052static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
1053 const char *alpha2)
1054{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001055 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001056
1057 assert_cfg80211_lock();
1058
Johannes Berg84fa4f42008-10-24 20:32:23 +02001059 /* All initial requests are respected */
1060 if (!last_request)
1061 return 0;
1062
1063 switch (set_by) {
1064 case REGDOM_SET_BY_INIT:
1065 return -EINVAL;
1066 case REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001067 return -EINVAL;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001068 case REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001069
1070 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1071
Johannes Berg84fa4f42008-10-24 20:32:23 +02001072 if (unlikely(!is_an_alpha2(alpha2)))
1073 return -EINVAL;
1074 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001075 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001076 /*
1077 * Two cards with two APs claiming different
1078 * different Country IE alpha2s. We could
1079 * intersect them, but that seems unlikely
1080 * to be correct. Reject second one for now.
1081 */
1082 if (!alpha2_equal(alpha2,
1083 cfg80211_regdomain->alpha2))
1084 return -EOPNOTSUPP;
1085 return -EALREADY;
1086 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001087 /* Two consecutive Country IE hints on the same wiphy.
1088 * This should be picked up early by the driver/stack */
1089 if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
1090 alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001091 return 0;
1092 return -EALREADY;
1093 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001094 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001095 case REGDOM_SET_BY_DRIVER:
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001096 if (last_request->initiator == REGDOM_SET_BY_CORE) {
1097 if (is_old_static_regdom(cfg80211_regdomain))
1098 return 0;
1099 if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
1100 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001101 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001102 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001103 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001104 case REGDOM_SET_BY_USER:
Johannes Berg84fa4f42008-10-24 20:32:23 +02001105 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001106 return REG_INTERSECT;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001107 /* If the user knows better the user should set the regdom
1108 * to their country before the IE is picked up */
1109 if (last_request->initiator == REGDOM_SET_BY_USER &&
1110 last_request->intersect)
1111 return -EOPNOTSUPP;
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001112 /* Process user requests only after previous user/driver/core
1113 * requests have been processed */
1114 if (last_request->initiator == REGDOM_SET_BY_CORE ||
1115 last_request->initiator == REGDOM_SET_BY_DRIVER ||
1116 last_request->initiator == REGDOM_SET_BY_USER) {
1117 if (!alpha2_equal(last_request->alpha2,
1118 cfg80211_regdomain->alpha2))
1119 return -EAGAIN;
1120 }
1121
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001122 if (!is_old_static_regdom(cfg80211_regdomain) &&
1123 alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
1124 return -EALREADY;
1125
Johannes Berg84fa4f42008-10-24 20:32:23 +02001126 return 0;
1127 }
1128
1129 return -EINVAL;
1130}
1131
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001132/* Caller must hold &cfg80211_mutex */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001133int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001134 const char *alpha2,
1135 u32 country_ie_checksum,
1136 enum environment_cap env)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001137{
1138 struct regulatory_request *request;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001139 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001140 int r = 0;
1141
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001142 assert_cfg80211_lock();
1143
Johannes Bergbe3d4812008-10-24 20:32:21 +02001144 r = ignore_request(wiphy, set_by, alpha2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001145
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001146 if (r == REG_INTERSECT) {
1147 if (set_by == REGDOM_SET_BY_DRIVER) {
1148 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1149 if (r)
1150 return r;
1151 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001152 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001153 } else if (r) {
1154 /* If the regulatory domain being requested by the
1155 * driver has already been set just copy it to the
1156 * wiphy */
1157 if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
1158 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1159 if (r)
1160 return r;
1161 r = -EALREADY;
1162 goto new_request;
1163 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001164 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001165 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001166
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001167new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001168 request = kzalloc(sizeof(struct regulatory_request),
1169 GFP_KERNEL);
1170 if (!request)
1171 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001172
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001173 request->alpha2[0] = alpha2[0];
1174 request->alpha2[1] = alpha2[1];
1175 request->initiator = set_by;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001176 request->wiphy_idx = get_wiphy_idx(wiphy);
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001177 request->intersect = intersect;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001178 request->country_ie_checksum = country_ie_checksum;
1179 request->country_ie_env = env;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001180
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001181 kfree(last_request);
1182 last_request = request;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001183
1184 /* When r == REG_INTERSECT we do need to call CRDA */
1185 if (r < 0)
1186 return r;
1187
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001188 /*
1189 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1190 * AND if CRDA is NOT present nothing will happen, if someone
1191 * wants to bother with 11d with OLD_REG you can add a timer.
1192 * If after x amount of time nothing happens you can call:
1193 *
1194 * return set_regdom(country_ie_regdomain);
1195 *
1196 * to intersect with the static rd
1197 */
Luis R. Rodriguez02ba0b32008-11-12 14:22:00 -08001198 return call_crda(alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001199}
1200
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001201/* This currently only processes user and driver regulatory hints */
1202static int reg_process_hint(struct regulatory_request *reg_request)
1203{
1204 int r = 0;
1205 struct wiphy *wiphy = NULL;
1206
1207 BUG_ON(!reg_request->alpha2);
1208
1209 mutex_lock(&cfg80211_mutex);
1210
1211 if (wiphy_idx_valid(reg_request->wiphy_idx))
1212 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1213
1214 if (reg_request->initiator == REGDOM_SET_BY_DRIVER &&
1215 !wiphy) {
1216 r = -ENODEV;
1217 goto out;
1218 }
1219
1220 r = __regulatory_hint(wiphy,
1221 reg_request->initiator,
1222 reg_request->alpha2,
1223 reg_request->country_ie_checksum,
1224 reg_request->country_ie_env);
1225 /* This is required so that the orig_* parameters are saved */
1226 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1227 wiphy_update_regulatory(wiphy, reg_request->initiator);
1228out:
1229 mutex_unlock(&cfg80211_mutex);
1230
1231 if (r == -EALREADY)
1232 r = 0;
1233
1234 return r;
1235}
1236
1237static void reg_process_pending_hints(void)
1238 {
1239 struct regulatory_request *reg_request;
1240 int r;
1241
1242 spin_lock(&reg_requests_lock);
1243 while (!list_empty(&reg_requests_list)) {
1244 reg_request = list_first_entry(&reg_requests_list,
1245 struct regulatory_request,
1246 list);
1247 list_del_init(&reg_request->list);
1248 spin_unlock(&reg_requests_lock);
1249
1250 r = reg_process_hint(reg_request);
1251#ifdef CONFIG_CFG80211_REG_DEBUG
1252 if (r && (reg_request->initiator == REGDOM_SET_BY_DRIVER ||
1253 reg_request->initiator == REGDOM_SET_BY_COUNTRY_IE))
1254 printk(KERN_ERR "cfg80211: wiphy_idx %d sent a "
1255 "regulatory hint for %c%c but now has "
1256 "gone fishing, ignoring request\n",
1257 reg_request->wiphy_idx,
1258 reg_request->alpha2[0],
1259 reg_request->alpha2[1]);
1260#endif
1261 kfree(reg_request);
1262 spin_lock(&reg_requests_lock);
1263 }
1264 spin_unlock(&reg_requests_lock);
1265}
1266
1267static void reg_todo(struct work_struct *work)
1268{
1269 reg_process_pending_hints();
1270}
1271
1272static DECLARE_WORK(reg_work, reg_todo);
1273
1274static void queue_regulatory_request(struct regulatory_request *request)
1275{
1276 spin_lock(&reg_requests_lock);
1277 list_add_tail(&request->list, &reg_requests_list);
1278 spin_unlock(&reg_requests_lock);
1279
1280 schedule_work(&reg_work);
1281}
1282
1283/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001284static int regulatory_hint_core(const char *alpha2)
1285{
1286 struct regulatory_request *request;
1287
1288 BUG_ON(last_request);
1289
1290 request = kzalloc(sizeof(struct regulatory_request),
1291 GFP_KERNEL);
1292 if (!request)
1293 return -ENOMEM;
1294
1295 request->alpha2[0] = alpha2[0];
1296 request->alpha2[1] = alpha2[1];
1297 request->initiator = REGDOM_SET_BY_CORE;
1298
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001299 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001300
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001301 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001302}
1303
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001304/* User hints */
1305int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001306{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001307 struct regulatory_request *request;
1308
Johannes Bergbe3d4812008-10-24 20:32:21 +02001309 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001310
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001311 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1312 if (!request)
1313 return -ENOMEM;
1314
1315 request->wiphy_idx = WIPHY_IDX_STALE;
1316 request->alpha2[0] = alpha2[0];
1317 request->alpha2[1] = alpha2[1];
1318 request->initiator = REGDOM_SET_BY_USER,
1319
1320 queue_regulatory_request(request);
1321
1322 return 0;
1323}
1324
1325/* Driver hints */
1326int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1327{
1328 struct regulatory_request *request;
1329
1330 BUG_ON(!alpha2);
1331 BUG_ON(!wiphy);
1332
1333 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1334 if (!request)
1335 return -ENOMEM;
1336
1337 request->wiphy_idx = get_wiphy_idx(wiphy);
1338
1339 /* Must have registered wiphy first */
1340 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1341
1342 request->alpha2[0] = alpha2[0];
1343 request->alpha2[1] = alpha2[1];
1344 request->initiator = REGDOM_SET_BY_DRIVER;
1345
1346 queue_regulatory_request(request);
1347
1348 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001349}
1350EXPORT_SYMBOL(regulatory_hint);
1351
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001352static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1353 u32 country_ie_checksum)
1354{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001355 struct wiphy *request_wiphy;
1356
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001357 assert_cfg80211_lock();
1358
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001359 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1360
1361 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001362 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001363
1364 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001365 return !country_ie_integrity_changes(country_ie_checksum);
1366 /* We should not have let these through at this point, they
1367 * should have been picked up earlier by the first alpha2 check
1368 * on the device */
1369 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1370 return true;
1371 return false;
1372}
1373
1374void regulatory_hint_11d(struct wiphy *wiphy,
1375 u8 *country_ie,
1376 u8 country_ie_len)
1377{
1378 struct ieee80211_regdomain *rd = NULL;
1379 char alpha2[2];
1380 u32 checksum = 0;
1381 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001382 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001383
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001384 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001385
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001386 if (unlikely(!last_request)) {
1387 mutex_unlock(&cfg80211_mutex);
1388 return;
1389 }
1390
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001391 /* IE len must be evenly divisible by 2 */
1392 if (country_ie_len & 0x01)
1393 goto out;
1394
1395 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1396 goto out;
1397
1398 /* Pending country IE processing, this can happen after we
1399 * call CRDA and wait for a response if a beacon was received before
1400 * we were able to process the last regulatory_hint_11d() call */
1401 if (country_ie_regdomain)
1402 goto out;
1403
1404 alpha2[0] = country_ie[0];
1405 alpha2[1] = country_ie[1];
1406
1407 if (country_ie[2] == 'I')
1408 env = ENVIRON_INDOOR;
1409 else if (country_ie[2] == 'O')
1410 env = ENVIRON_OUTDOOR;
1411
1412 /* We will run this for *every* beacon processed for the BSSID, so
1413 * we optimize an early check to exit out early if we don't have to
1414 * do anything */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001415 if (likely(wiphy_idx_valid(last_request->wiphy_idx))) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001416 struct cfg80211_registered_device *drv_last_ie;
1417
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001418 drv_last_ie =
1419 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001420
1421 /* Lets keep this simple -- we trust the first AP
1422 * after we intersect with CRDA */
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001423 if (likely(&drv_last_ie->wiphy == wiphy)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001424 /* Ignore IEs coming in on this wiphy with
1425 * the same alpha2 and environment cap */
1426 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1427 alpha2) &&
1428 env == drv_last_ie->env)) {
1429 goto out;
1430 }
1431 /* the wiphy moved on to another BSSID or the AP
1432 * was reconfigured. XXX: We need to deal with the
1433 * case where the user suspends and goes to goes
1434 * to another country, and then gets IEs from an
1435 * AP with different settings */
1436 goto out;
1437 } else {
1438 /* Ignore IEs coming in on two separate wiphys with
1439 * the same alpha2 and environment cap */
1440 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1441 alpha2) &&
1442 env == drv_last_ie->env)) {
1443 goto out;
1444 }
1445 /* We could potentially intersect though */
1446 goto out;
1447 }
1448 }
1449
1450 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1451 if (!rd)
1452 goto out;
1453
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001454 /*
1455 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001456 * the future when we want to add suspend/resume support and having
1457 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001458 * move to another AP. Right now we just trust the first AP.
1459 *
1460 * If we hit this before we add this support we want to be informed of
1461 * it as it would indicate a mistake in the current design
1462 */
1463 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001464 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001465
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001466 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1467 if (!request)
1468 goto free_rd_out;
1469
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001470 /* We keep this around for when CRDA comes back with a response so
1471 * we can intersect with that */
1472 country_ie_regdomain = rd;
1473
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001474 request->wiphy_idx = get_wiphy_idx(wiphy);
1475 request->alpha2[0] = rd->alpha2[0];
1476 request->alpha2[1] = rd->alpha2[1];
1477 request->initiator = REGDOM_SET_BY_COUNTRY_IE;
1478 request->country_ie_checksum = checksum;
1479 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001480
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001481 mutex_unlock(&cfg80211_mutex);
1482
1483 queue_regulatory_request(request);
1484
1485 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001486
1487free_rd_out:
1488 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001489out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001490 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001491}
1492EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001493
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001494static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001495{
1496 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001497 const struct ieee80211_reg_rule *reg_rule = NULL;
1498 const struct ieee80211_freq_range *freq_range = NULL;
1499 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001500
1501 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1502 "(max_antenna_gain, max_eirp)\n");
1503
1504 for (i = 0; i < rd->n_reg_rules; i++) {
1505 reg_rule = &rd->reg_rules[i];
1506 freq_range = &reg_rule->freq_range;
1507 power_rule = &reg_rule->power_rule;
1508
1509 /* There may not be documentation for max antenna gain
1510 * in certain regions */
1511 if (power_rule->max_antenna_gain)
1512 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1513 "(%d mBi, %d mBm)\n",
1514 freq_range->start_freq_khz,
1515 freq_range->end_freq_khz,
1516 freq_range->max_bandwidth_khz,
1517 power_rule->max_antenna_gain,
1518 power_rule->max_eirp);
1519 else
1520 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1521 "(N/A, %d mBm)\n",
1522 freq_range->start_freq_khz,
1523 freq_range->end_freq_khz,
1524 freq_range->max_bandwidth_khz,
1525 power_rule->max_eirp);
1526 }
1527}
1528
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001529static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001530{
1531
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001532 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001533
1534 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001535 struct cfg80211_registered_device *drv;
1536 drv = cfg80211_drv_by_wiphy_idx(
1537 last_request->wiphy_idx);
1538 if (drv) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001539 printk(KERN_INFO "cfg80211: Current regulatory "
1540 "domain updated by AP to: %c%c\n",
1541 drv->country_ie_alpha2[0],
1542 drv->country_ie_alpha2[1]);
1543 } else
1544 printk(KERN_INFO "cfg80211: Current regulatory "
1545 "domain intersected: \n");
1546 } else
1547 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001548 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001549 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001550 printk(KERN_INFO "cfg80211: World regulatory "
1551 "domain updated:\n");
1552 else {
1553 if (is_unknown_alpha2(rd->alpha2))
1554 printk(KERN_INFO "cfg80211: Regulatory domain "
1555 "changed to driver built-in settings "
1556 "(unknown country)\n");
1557 else
1558 printk(KERN_INFO "cfg80211: Regulatory domain "
1559 "changed to country: %c%c\n",
1560 rd->alpha2[0], rd->alpha2[1]);
1561 }
1562 print_rd_rules(rd);
1563}
1564
Johannes Berg2df78162008-10-28 16:49:41 +01001565static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001566{
1567 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1568 rd->alpha2[0], rd->alpha2[1]);
1569 print_rd_rules(rd);
1570}
1571
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001572#ifdef CONFIG_CFG80211_REG_DEBUG
1573static void reg_country_ie_process_debug(
1574 const struct ieee80211_regdomain *rd,
1575 const struct ieee80211_regdomain *country_ie_regdomain,
1576 const struct ieee80211_regdomain *intersected_rd)
1577{
1578 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1579 print_regdomain_info(country_ie_regdomain);
1580 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1581 print_regdomain_info(rd);
1582 if (intersected_rd) {
1583 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1584 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08001585 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001586 return;
1587 }
1588 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1589}
1590#else
1591static inline void reg_country_ie_process_debug(
1592 const struct ieee80211_regdomain *rd,
1593 const struct ieee80211_regdomain *country_ie_regdomain,
1594 const struct ieee80211_regdomain *intersected_rd)
1595{
1596}
1597#endif
1598
Johannes Bergd2372b32008-10-24 20:32:20 +02001599/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001600static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001601{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001602 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001603 struct cfg80211_registered_device *drv = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001604 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001605 /* Some basic sanity checks first */
1606
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001607 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001608 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001609 return -EINVAL;
1610 update_world_regdomain(rd);
1611 return 0;
1612 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001613
1614 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1615 !is_unknown_alpha2(rd->alpha2))
1616 return -EINVAL;
1617
Johannes Bergf6037d02008-10-21 11:01:33 +02001618 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001619 return -EINVAL;
1620
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001621 /* Lets only bother proceeding on the same alpha2 if the current
1622 * rd is non static (it means CRDA was present and was used last)
1623 * and the pending request came in from a country IE */
1624 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1625 /* If someone else asked us to change the rd lets only bother
1626 * checking if the alpha2 changes if CRDA was already called */
1627 if (!is_old_static_regdom(cfg80211_regdomain) &&
1628 !regdom_changed(rd->alpha2))
1629 return -EINVAL;
1630 }
1631
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001632 /* Now lets set the regulatory domain, update all driver channels
1633 * and finally inform them of what we have done, in case they want
1634 * to review or adjust their own settings based on their own
1635 * internal EEPROM data */
1636
Johannes Bergf6037d02008-10-21 11:01:33 +02001637 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001638 return -EINVAL;
1639
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001640 if (!is_valid_rd(rd)) {
1641 printk(KERN_ERR "cfg80211: Invalid "
1642 "regulatory domain detected:\n");
1643 print_regdomain_info(rd);
1644 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001645 }
1646
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001647 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1648
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001649 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001650 int r;
1651
1652 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1653 reset_regdomains();
1654 cfg80211_regdomain = rd;
1655 return 0;
1656 }
1657
1658 /* For a driver hint, lets copy the regulatory domain the
1659 * driver wanted to the wiphy to deal with conflicts */
1660
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001661 BUG_ON(request_wiphy->regd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001662
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001663 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001664 if (r)
1665 return r;
1666
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001667 reset_regdomains();
1668 cfg80211_regdomain = rd;
1669 return 0;
1670 }
1671
1672 /* Intersection requires a bit more work */
1673
1674 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1675
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001676 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1677 if (!intersected_rd)
1678 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001679
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001680 /* We can trash what CRDA provided now.
1681 * However if a driver requested this specific regulatory
1682 * domain we keep it for its private use */
1683 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001684 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001685 else
1686 kfree(rd);
1687
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001688 rd = NULL;
1689
1690 reset_regdomains();
1691 cfg80211_regdomain = intersected_rd;
1692
1693 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001694 }
1695
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001696 /*
1697 * Country IE requests are handled a bit differently, we intersect
1698 * the country IE rd with what CRDA believes that country should have
1699 */
1700
1701 BUG_ON(!country_ie_regdomain);
1702
1703 if (rd != country_ie_regdomain) {
1704 /* Intersect what CRDA returned and our what we
1705 * had built from the Country IE received */
1706
1707 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1708
1709 reg_country_ie_process_debug(rd, country_ie_regdomain,
1710 intersected_rd);
1711
1712 kfree(country_ie_regdomain);
1713 country_ie_regdomain = NULL;
1714 } else {
1715 /* This would happen when CRDA was not present and
1716 * OLD_REGULATORY was enabled. We intersect our Country
1717 * IE rd and what was set on cfg80211 originally */
1718 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1719 }
1720
1721 if (!intersected_rd)
1722 return -EINVAL;
1723
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001724 drv = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001725
1726 drv->country_ie_alpha2[0] = rd->alpha2[0];
1727 drv->country_ie_alpha2[1] = rd->alpha2[1];
1728 drv->env = last_request->country_ie_env;
1729
1730 BUG_ON(intersected_rd == rd);
1731
1732 kfree(rd);
1733 rd = NULL;
1734
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001735 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001736 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001737
1738 return 0;
1739}
1740
1741
1742/* Use this call to set the current regulatory domain. Conflicts with
1743 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001744 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001745int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001746{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001747 int r;
1748
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001749 assert_cfg80211_lock();
1750
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001751 /* Note that this doesn't update the wiphys, this is done below */
1752 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02001753 if (r) {
1754 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001755 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02001756 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001757
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001758 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001759 if (!last_request->intersect)
1760 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001761
1762 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02001763 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001764
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001765 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001766
1767 return r;
1768}
1769
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001770/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001771void reg_device_remove(struct wiphy *wiphy)
1772{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001773 struct wiphy *request_wiphy;
1774
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001775 assert_cfg80211_lock();
1776
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001777 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1778
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001779 kfree(wiphy->regd);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001780 if (!last_request || !request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001781 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001782 if (request_wiphy != wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001783 return;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001784 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001785 last_request->country_ie_env = ENVIRON_ANY;
1786}
1787
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001788int regulatory_init(void)
1789{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05001790 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02001791
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001792 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1793 if (IS_ERR(reg_pdev))
1794 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001795
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001796 spin_lock_init(&reg_requests_lock);
1797
Johannes Berg734366d2008-09-15 10:56:48 +02001798#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001799 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02001800
Johannes Berg942b25c2008-09-15 11:26:47 +02001801 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02001802 print_regdomain_info(cfg80211_regdomain);
1803 /* The old code still requests for a new regdomain and if
1804 * you have CRDA you get it updated, otherwise you get
1805 * stuck with the static values. We ignore "EU" code as
1806 * that is not a valid ISO / IEC 3166 alpha2 */
Johannes Bergac9440a2008-10-21 11:08:27 +02001807 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001808 err = regulatory_hint_core(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02001809#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001810 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02001811
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001812 err = regulatory_hint_core("00");
Johannes Berg734366d2008-09-15 10:56:48 +02001813#endif
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05001814 if (err) {
1815 if (err == -ENOMEM)
1816 return err;
1817 /*
1818 * N.B. kobject_uevent_env() can fail mainly for when we're out
1819 * memory which is handled and propagated appropriately above
1820 * but it can also fail during a netlink_broadcast() or during
1821 * early boot for call_usermodehelper(). For now treat these
1822 * errors as non-fatal.
1823 */
1824 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
1825 "to call CRDA during init");
1826#ifdef CONFIG_CFG80211_REG_DEBUG
1827 /* We want to find out exactly why when debugging */
1828 WARN_ON(err);
1829#endif
1830 }
Johannes Berg734366d2008-09-15 10:56:48 +02001831
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001832 return 0;
1833}
1834
1835void regulatory_exit(void)
1836{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001837 struct regulatory_request *reg_request, *tmp;
1838
1839 cancel_work_sync(&reg_work);
1840
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001841 mutex_lock(&cfg80211_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02001842
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001843 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02001844
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001845 kfree(country_ie_regdomain);
1846 country_ie_regdomain = NULL;
1847
Johannes Bergf6037d02008-10-21 11:01:33 +02001848 kfree(last_request);
1849
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001850 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001851
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001852 spin_lock(&reg_requests_lock);
1853 if (!list_empty(&reg_requests_list)) {
1854 list_for_each_entry_safe(reg_request, tmp,
1855 &reg_requests_list, list) {
1856 list_del(&reg_request->list);
1857 kfree(reg_request);
1858 }
1859 }
1860 spin_unlock(&reg_requests_lock);
1861
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001862 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01001863}