blob: 5f6d20d98eebd6b1e0533c02498e16cf35c6ce91 [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/**
46 * struct regulatory_request - receipt of last regulatory request
47 *
48 * @wiphy: this is set if this request's initiator is
49 * %REGDOM_SET_BY_COUNTRY_IE or %REGDOM_SET_BY_DRIVER. This
50 * can be used by the wireless core to deal with conflicts
51 * and potentially inform users of which devices specifically
52 * cased the conflicts.
53 * @initiator: indicates who sent this request, could be any of
54 * of those set in reg_set_by, %REGDOM_SET_BY_*
55 * @alpha2: the ISO / IEC 3166 alpha2 country code of the requested
56 * regulatory domain. We have a few special codes:
57 * 00 - World regulatory domain
58 * 99 - built by driver but a specific alpha2 cannot be determined
59 * 98 - result of an intersection between two regulatory domains
60 * @intersect: indicates whether the wireless core should intersect
61 * the requested regulatory domain with the presently set regulatory
62 * domain.
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080063 * @country_ie_checksum: checksum of the last processed and accepted
64 * country IE
65 * @country_ie_env: lets us know if the AP is telling us we are outdoor,
66 * indoor, or if it doesn't matter
Johannes Bergbe3d4812008-10-24 20:32:21 +020067 */
Johannes Berg734366d2008-09-15 10:56:48 +020068struct regulatory_request {
Johannes Berg734366d2008-09-15 10:56:48 +020069 struct wiphy *wiphy;
Johannes Berg734366d2008-09-15 10:56:48 +020070 enum reg_set_by initiator;
71 char alpha2[2];
Luis R. Rodriguez9c964772008-10-30 13:33:53 -070072 bool intersect;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080073 u32 country_ie_checksum;
74 enum environment_cap country_ie_env;
Johannes Berg734366d2008-09-15 10:56:48 +020075};
76
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070077/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020078static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020079
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070080/* To trigger userspace events */
81static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010082
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070083/* Keep the ordering from large to small */
84static u32 supported_bandwidths[] = {
85 MHZ_TO_KHZ(40),
86 MHZ_TO_KHZ(20),
Johannes Berg8318d782008-01-24 19:38:38 +010087};
88
Johannes Berg734366d2008-09-15 10:56:48 +020089/* Central wireless core regulatory domains, we only need two,
90 * the current one and a world regulatory domain in case we have no
91 * information to give us an alpha2 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +020092static const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020093
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080094/* We use this as a place for the rd structure built from the
95 * last parsed country IE to rest until CRDA gets back to us with
96 * what it thinks should apply for the same country */
97static const struct ieee80211_regdomain *country_ie_regdomain;
98
Johannes Berg734366d2008-09-15 10:56:48 +020099/* We keep a static world regulatory domain in case of the absence of CRDA */
100static const struct ieee80211_regdomain world_regdom = {
101 .n_reg_rules = 1,
102 .alpha2 = "00",
103 .reg_rules = {
104 REG_RULE(2412-10, 2462+10, 40, 6, 20,
105 NL80211_RRF_PASSIVE_SCAN |
106 NL80211_RRF_NO_IBSS),
107 }
108};
109
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200110static const struct ieee80211_regdomain *cfg80211_world_regdom =
111 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200112
113#ifdef CONFIG_WIRELESS_OLD_REGULATORY
114static char *ieee80211_regdom = "US";
115module_param(ieee80211_regdom, charp, 0444);
116MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
117
118/* We assume 40 MHz bandwidth for the old regulatory work.
119 * We make emphasis we are using the exact same frequencies
120 * as before */
121
122static const struct ieee80211_regdomain us_regdom = {
123 .n_reg_rules = 6,
124 .alpha2 = "US",
125 .reg_rules = {
126 /* IEEE 802.11b/g, channels 1..11 */
127 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
128 /* IEEE 802.11a, channel 36 */
129 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
130 /* IEEE 802.11a, channel 40 */
131 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
132 /* IEEE 802.11a, channel 44 */
133 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
134 /* IEEE 802.11a, channels 48..64 */
135 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
136 /* IEEE 802.11a, channels 149..165, outdoor */
137 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
138 }
139};
140
141static const struct ieee80211_regdomain jp_regdom = {
142 .n_reg_rules = 3,
143 .alpha2 = "JP",
144 .reg_rules = {
145 /* IEEE 802.11b/g, channels 1..14 */
146 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
147 /* IEEE 802.11a, channels 34..48 */
148 REG_RULE(5170-10, 5240+10, 40, 6, 20,
149 NL80211_RRF_PASSIVE_SCAN),
150 /* IEEE 802.11a, channels 52..64 */
151 REG_RULE(5260-10, 5320+10, 40, 6, 20,
152 NL80211_RRF_NO_IBSS |
153 NL80211_RRF_DFS),
154 }
155};
156
157static const struct ieee80211_regdomain eu_regdom = {
158 .n_reg_rules = 6,
159 /* This alpha2 is bogus, we leave it here just for stupid
160 * backward compatibility */
161 .alpha2 = "EU",
162 .reg_rules = {
163 /* IEEE 802.11b/g, channels 1..13 */
164 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
165 /* IEEE 802.11a, channel 36 */
166 REG_RULE(5180-10, 5180+10, 40, 6, 23,
167 NL80211_RRF_PASSIVE_SCAN),
168 /* IEEE 802.11a, channel 40 */
169 REG_RULE(5200-10, 5200+10, 40, 6, 23,
170 NL80211_RRF_PASSIVE_SCAN),
171 /* IEEE 802.11a, channel 44 */
172 REG_RULE(5220-10, 5220+10, 40, 6, 23,
173 NL80211_RRF_PASSIVE_SCAN),
174 /* IEEE 802.11a, channels 48..64 */
175 REG_RULE(5240-10, 5320+10, 40, 6, 20,
176 NL80211_RRF_NO_IBSS |
177 NL80211_RRF_DFS),
178 /* IEEE 802.11a, channels 100..140 */
179 REG_RULE(5500-10, 5700+10, 40, 6, 30,
180 NL80211_RRF_NO_IBSS |
181 NL80211_RRF_DFS),
182 }
183};
184
185static const struct ieee80211_regdomain *static_regdom(char *alpha2)
186{
187 if (alpha2[0] == 'U' && alpha2[1] == 'S')
188 return &us_regdom;
189 if (alpha2[0] == 'J' && alpha2[1] == 'P')
190 return &jp_regdom;
191 if (alpha2[0] == 'E' && alpha2[1] == 'U')
192 return &eu_regdom;
193 /* Default, as per the old rules */
194 return &us_regdom;
195}
196
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200197static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200198{
199 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
200 return true;
201 return false;
202}
Johannes Berg734366d2008-09-15 10:56:48 +0200203#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200204static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
205{
206 return false;
207}
208#endif
209
Johannes Berg734366d2008-09-15 10:56:48 +0200210static void reset_regdomains(void)
211{
Johannes Berg942b25c2008-09-15 11:26:47 +0200212 /* avoid freeing static information or freeing something twice */
213 if (cfg80211_regdomain == cfg80211_world_regdom)
214 cfg80211_regdomain = NULL;
215 if (cfg80211_world_regdom == &world_regdom)
216 cfg80211_world_regdom = NULL;
217 if (cfg80211_regdomain == &world_regdom)
218 cfg80211_regdomain = NULL;
219 if (is_old_static_regdom(cfg80211_regdomain))
220 cfg80211_regdomain = NULL;
221
222 kfree(cfg80211_regdomain);
223 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200224
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200225 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200226 cfg80211_regdomain = NULL;
227}
228
229/* Dynamic world regulatory domain requested by the wireless
230 * core upon initialization */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200231static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200232{
Johannes Bergf6037d02008-10-21 11:01:33 +0200233 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200234
235 reset_regdomains();
236
237 cfg80211_world_regdom = rd;
238 cfg80211_regdomain = rd;
239}
Johannes Berg734366d2008-09-15 10:56:48 +0200240
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200241bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100242{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700243 if (!alpha2)
244 return false;
245 if (alpha2[0] == '0' && alpha2[1] == '0')
246 return true;
247 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100248}
249
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200250static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700251{
252 if (!alpha2)
253 return false;
254 if (alpha2[0] != 0 && alpha2[1] != 0)
255 return true;
256 return false;
257}
Johannes Berg8318d782008-01-24 19:38:38 +0100258
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700259static bool is_alpha_upper(char letter)
260{
261 /* ASCII A - Z */
262 if (letter >= 65 && letter <= 90)
263 return true;
264 return false;
265}
266
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200267static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268{
269 if (!alpha2)
270 return false;
271 /* Special case where regulatory domain was built by driver
272 * but a specific alpha2 cannot be determined */
273 if (alpha2[0] == '9' && alpha2[1] == '9')
274 return true;
275 return false;
276}
277
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800278static bool is_intersected_alpha2(const char *alpha2)
279{
280 if (!alpha2)
281 return false;
282 /* Special case where regulatory domain is the
283 * result of an intersection between two regulatory domain
284 * structures */
285 if (alpha2[0] == '9' && alpha2[1] == '8')
286 return true;
287 return false;
288}
289
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200290static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700291{
292 if (!alpha2)
293 return false;
294 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
295 return true;
296 return false;
297}
298
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200299static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700300{
301 if (!alpha2_x || !alpha2_y)
302 return false;
303 if (alpha2_x[0] == alpha2_y[0] &&
304 alpha2_x[1] == alpha2_y[1])
305 return true;
306 return false;
307}
308
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200309static bool regdom_changed(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700310{
311 if (!cfg80211_regdomain)
312 return true;
313 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
314 return false;
315 return true;
316}
317
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800318/**
319 * country_ie_integrity_changes - tells us if the country IE has changed
320 * @checksum: checksum of country IE of fields we are interested in
321 *
322 * If the country IE has not changed you can ignore it safely. This is
323 * useful to determine if two devices are seeing two different country IEs
324 * even on the same alpha2. Note that this will return false if no IE has
325 * been set on the wireless core yet.
326 */
327static bool country_ie_integrity_changes(u32 checksum)
328{
329 /* If no IE has been set then the checksum doesn't change */
330 if (unlikely(!last_request->country_ie_checksum))
331 return false;
332 if (unlikely(last_request->country_ie_checksum != checksum))
333 return true;
334 return false;
335}
336
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700337/* This lets us keep regulatory code which is updated on a regulatory
338 * basis in userspace. */
339static int call_crda(const char *alpha2)
340{
341 char country_env[9 + 2] = "COUNTRY=";
342 char *envp[] = {
343 country_env,
344 NULL
345 };
346
347 if (!is_world_regdom((char *) alpha2))
348 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
349 alpha2[0], alpha2[1]);
350 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700351 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
352 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700353
354 country_env[8] = alpha2[0];
355 country_env[9] = alpha2[1];
356
357 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
358}
359
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700360/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200361bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700362{
Johannes Bergf6037d02008-10-21 11:01:33 +0200363 if (!last_request)
364 return false;
365
366 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700367}
368
369/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200370static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700371{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200372 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700373 u32 freq_diff;
374
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800375 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700376 return false;
377
378 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
379 return false;
380
381 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
382
Luis R. Rodriguezd71aaf62008-10-30 13:33:52 -0700383 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700384 return false;
385
386 return true;
387}
388
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200389static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700390{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200391 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700392 unsigned int i;
393
394 if (!rd->n_reg_rules)
395 return false;
396
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800397 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
398 return false;
399
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700400 for (i = 0; i < rd->n_reg_rules; i++) {
401 reg_rule = &rd->reg_rules[i];
402 if (!is_valid_reg_rule(reg_rule))
403 return false;
404 }
405
406 return true;
407}
408
409/* Returns value in KHz */
410static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
411 u32 freq)
412{
413 unsigned int i;
414 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
415 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
416 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
417 if (start_freq_khz >= freq_range->start_freq_khz &&
418 end_freq_khz <= freq_range->end_freq_khz)
419 return supported_bandwidths[i];
420 }
421 return 0;
422}
423
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800424/**
425 * freq_in_rule_band - tells us if a frequency is in a frequency band
426 * @freq_range: frequency rule we want to query
427 * @freq_khz: frequency we are inquiring about
428 *
429 * This lets us know if a specific frequency rule is or is not relevant to
430 * a specific frequency's band. Bands are device specific and artificial
431 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
432 * safe for now to assume that a frequency rule should not be part of a
433 * frequency's band if the start freq or end freq are off by more than 2 GHz.
434 * This resolution can be lowered and should be considered as we add
435 * regulatory rule support for other "bands".
436 **/
437static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
438 u32 freq_khz)
439{
440#define ONE_GHZ_IN_KHZ 1000000
441 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
442 return true;
443 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
444 return true;
445 return false;
446#undef ONE_GHZ_IN_KHZ
447}
448
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800449/* Converts a country IE to a regulatory domain. A regulatory domain
450 * structure has a lot of information which the IE doesn't yet have,
451 * so for the other values we use upper max values as we will intersect
452 * with our userspace regulatory agent to get lower bounds. */
453static struct ieee80211_regdomain *country_ie_2_rd(
454 u8 *country_ie,
455 u8 country_ie_len,
456 u32 *checksum)
457{
458 struct ieee80211_regdomain *rd = NULL;
459 unsigned int i = 0;
460 char alpha2[2];
461 u32 flags = 0;
462 u32 num_rules = 0, size_of_regd = 0;
463 u8 *triplets_start = NULL;
464 u8 len_at_triplet = 0;
465 /* the last channel we have registered in a subband (triplet) */
466 int last_sub_max_channel = 0;
467
468 *checksum = 0xDEADBEEF;
469
470 /* Country IE requirements */
471 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
472 country_ie_len & 0x01);
473
474 alpha2[0] = country_ie[0];
475 alpha2[1] = country_ie[1];
476
477 /*
478 * Third octet can be:
479 * 'I' - Indoor
480 * 'O' - Outdoor
481 *
482 * anything else we assume is no restrictions
483 */
484 if (country_ie[2] == 'I')
485 flags = NL80211_RRF_NO_OUTDOOR;
486 else if (country_ie[2] == 'O')
487 flags = NL80211_RRF_NO_INDOOR;
488
489 country_ie += 3;
490 country_ie_len -= 3;
491
492 triplets_start = country_ie;
493 len_at_triplet = country_ie_len;
494
495 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
496
497 /* We need to build a reg rule for each triplet, but first we must
498 * calculate the number of reg rules we will need. We will need one
499 * for each channel subband */
500 while (country_ie_len >= 3) {
501 struct ieee80211_country_ie_triplet *triplet =
502 (struct ieee80211_country_ie_triplet *) country_ie;
503 int cur_sub_max_channel = 0, cur_channel = 0;
504
505 if (triplet->ext.reg_extension_id >=
506 IEEE80211_COUNTRY_EXTENSION_ID) {
507 country_ie += 3;
508 country_ie_len -= 3;
509 continue;
510 }
511
512 cur_channel = triplet->chans.first_channel;
513 cur_sub_max_channel = ieee80211_channel_to_frequency(
514 cur_channel + triplet->chans.num_channels);
515
516 /* Basic sanity check */
517 if (cur_sub_max_channel < cur_channel)
518 return NULL;
519
520 /* Do not allow overlapping channels. Also channels
521 * passed in each subband must be monotonically
522 * increasing */
523 if (last_sub_max_channel) {
524 if (cur_channel <= last_sub_max_channel)
525 return NULL;
526 if (cur_sub_max_channel <= last_sub_max_channel)
527 return NULL;
528 }
529
530 /* When dot11RegulatoryClassesRequired is supported
531 * we can throw ext triplets as part of this soup,
532 * for now we don't care when those change as we
533 * don't support them */
534 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
535 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
536 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
537
538 last_sub_max_channel = cur_sub_max_channel;
539
540 country_ie += 3;
541 country_ie_len -= 3;
542 num_rules++;
543
544 /* Note: this is not a IEEE requirement but
545 * simply a memory requirement */
546 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
547 return NULL;
548 }
549
550 country_ie = triplets_start;
551 country_ie_len = len_at_triplet;
552
553 size_of_regd = sizeof(struct ieee80211_regdomain) +
554 (num_rules * sizeof(struct ieee80211_reg_rule));
555
556 rd = kzalloc(size_of_regd, GFP_KERNEL);
557 if (!rd)
558 return NULL;
559
560 rd->n_reg_rules = num_rules;
561 rd->alpha2[0] = alpha2[0];
562 rd->alpha2[1] = alpha2[1];
563
564 /* This time around we fill in the rd */
565 while (country_ie_len >= 3) {
566 struct ieee80211_country_ie_triplet *triplet =
567 (struct ieee80211_country_ie_triplet *) country_ie;
568 struct ieee80211_reg_rule *reg_rule = NULL;
569 struct ieee80211_freq_range *freq_range = NULL;
570 struct ieee80211_power_rule *power_rule = NULL;
571
572 /* Must parse if dot11RegulatoryClassesRequired is true,
573 * we don't support this yet */
574 if (triplet->ext.reg_extension_id >=
575 IEEE80211_COUNTRY_EXTENSION_ID) {
576 country_ie += 3;
577 country_ie_len -= 3;
578 continue;
579 }
580
581 reg_rule = &rd->reg_rules[i];
582 freq_range = &reg_rule->freq_range;
583 power_rule = &reg_rule->power_rule;
584
585 reg_rule->flags = flags;
586
587 /* The +10 is since the regulatory domain expects
588 * the actual band edge, not the center of freq for
589 * its start and end freqs, assuming 20 MHz bandwidth on
590 * the channels passed */
591 freq_range->start_freq_khz =
592 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
593 triplet->chans.first_channel) - 10);
594 freq_range->end_freq_khz =
595 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
596 triplet->chans.first_channel +
597 triplet->chans.num_channels) + 10);
598
599 /* Large arbitrary values, we intersect later */
600 /* Increment this if we ever support >= 40 MHz channels
601 * in IEEE 802.11 */
602 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
603 power_rule->max_antenna_gain = DBI_TO_MBI(100);
604 power_rule->max_eirp = DBM_TO_MBM(100);
605
606 country_ie += 3;
607 country_ie_len -= 3;
608 i++;
609
610 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
611 }
612
613 return rd;
614}
615
616
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700617/* Helper for regdom_intersect(), this does the real
618 * mathematical intersection fun */
619static int reg_rules_intersect(
620 const struct ieee80211_reg_rule *rule1,
621 const struct ieee80211_reg_rule *rule2,
622 struct ieee80211_reg_rule *intersected_rule)
623{
624 const struct ieee80211_freq_range *freq_range1, *freq_range2;
625 struct ieee80211_freq_range *freq_range;
626 const struct ieee80211_power_rule *power_rule1, *power_rule2;
627 struct ieee80211_power_rule *power_rule;
628 u32 freq_diff;
629
630 freq_range1 = &rule1->freq_range;
631 freq_range2 = &rule2->freq_range;
632 freq_range = &intersected_rule->freq_range;
633
634 power_rule1 = &rule1->power_rule;
635 power_rule2 = &rule2->power_rule;
636 power_rule = &intersected_rule->power_rule;
637
638 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
639 freq_range2->start_freq_khz);
640 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
641 freq_range2->end_freq_khz);
642 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
643 freq_range2->max_bandwidth_khz);
644
645 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
646 if (freq_range->max_bandwidth_khz > freq_diff)
647 freq_range->max_bandwidth_khz = freq_diff;
648
649 power_rule->max_eirp = min(power_rule1->max_eirp,
650 power_rule2->max_eirp);
651 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
652 power_rule2->max_antenna_gain);
653
654 intersected_rule->flags = (rule1->flags | rule2->flags);
655
656 if (!is_valid_reg_rule(intersected_rule))
657 return -EINVAL;
658
659 return 0;
660}
661
662/**
663 * regdom_intersect - do the intersection between two regulatory domains
664 * @rd1: first regulatory domain
665 * @rd2: second regulatory domain
666 *
667 * Use this function to get the intersection between two regulatory domains.
668 * Once completed we will mark the alpha2 for the rd as intersected, "98",
669 * as no one single alpha2 can represent this regulatory domain.
670 *
671 * Returns a pointer to the regulatory domain structure which will hold the
672 * resulting intersection of rules between rd1 and rd2. We will
673 * kzalloc() this structure for you.
674 */
675static struct ieee80211_regdomain *regdom_intersect(
676 const struct ieee80211_regdomain *rd1,
677 const struct ieee80211_regdomain *rd2)
678{
679 int r, size_of_regd;
680 unsigned int x, y;
681 unsigned int num_rules = 0, rule_idx = 0;
682 const struct ieee80211_reg_rule *rule1, *rule2;
683 struct ieee80211_reg_rule *intersected_rule;
684 struct ieee80211_regdomain *rd;
685 /* This is just a dummy holder to help us count */
686 struct ieee80211_reg_rule irule;
687
688 /* Uses the stack temporarily for counter arithmetic */
689 intersected_rule = &irule;
690
691 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
692
693 if (!rd1 || !rd2)
694 return NULL;
695
696 /* First we get a count of the rules we'll need, then we actually
697 * build them. This is to so we can malloc() and free() a
698 * regdomain once. The reason we use reg_rules_intersect() here
699 * is it will return -EINVAL if the rule computed makes no sense.
700 * All rules that do check out OK are valid. */
701
702 for (x = 0; x < rd1->n_reg_rules; x++) {
703 rule1 = &rd1->reg_rules[x];
704 for (y = 0; y < rd2->n_reg_rules; y++) {
705 rule2 = &rd2->reg_rules[y];
706 if (!reg_rules_intersect(rule1, rule2,
707 intersected_rule))
708 num_rules++;
709 memset(intersected_rule, 0,
710 sizeof(struct ieee80211_reg_rule));
711 }
712 }
713
714 if (!num_rules)
715 return NULL;
716
717 size_of_regd = sizeof(struct ieee80211_regdomain) +
718 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
719
720 rd = kzalloc(size_of_regd, GFP_KERNEL);
721 if (!rd)
722 return NULL;
723
724 for (x = 0; x < rd1->n_reg_rules; x++) {
725 rule1 = &rd1->reg_rules[x];
726 for (y = 0; y < rd2->n_reg_rules; y++) {
727 rule2 = &rd2->reg_rules[y];
728 /* This time around instead of using the stack lets
729 * write to the target rule directly saving ourselves
730 * a memcpy() */
731 intersected_rule = &rd->reg_rules[rule_idx];
732 r = reg_rules_intersect(rule1, rule2,
733 intersected_rule);
734 /* No need to memset here the intersected rule here as
735 * we're not using the stack anymore */
736 if (r)
737 continue;
738 rule_idx++;
739 }
740 }
741
742 if (rule_idx != num_rules) {
743 kfree(rd);
744 return NULL;
745 }
746
747 rd->n_reg_rules = num_rules;
748 rd->alpha2[0] = '9';
749 rd->alpha2[1] = '8';
750
751 return rd;
752}
753
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700754/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
755 * want to just have the channel structure use these */
756static u32 map_regdom_flags(u32 rd_flags)
757{
758 u32 channel_flags = 0;
759 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
760 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
761 if (rd_flags & NL80211_RRF_NO_IBSS)
762 channel_flags |= IEEE80211_CHAN_NO_IBSS;
763 if (rd_flags & NL80211_RRF_DFS)
764 channel_flags |= IEEE80211_CHAN_RADAR;
765 return channel_flags;
766}
767
768/**
769 * freq_reg_info - get regulatory information for the given frequency
770 * @center_freq: Frequency in KHz for which we want regulatory information for
771 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
772 * you can set this to 0. If this frequency is allowed we then set
773 * this value to the maximum allowed bandwidth.
774 * @reg_rule: the regulatory rule which we have for this frequency
775 *
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800776 * Use this function to get the regulatory rule for a specific frequency on
777 * a given wireless device. If the device has a specific regulatory domain
778 * it wants to follow we respect that unless a country IE has been received
779 * and processed already.
780 *
781 * Returns 0 if it was able to find a valid regulatory rule which does
782 * apply to the given center_freq otherwise it returns non-zero. It will
783 * also return -ERANGE if we determine the given center_freq does not even have
784 * a regulatory rule for a frequency range in the center_freq's band. See
785 * freq_in_rule_band() for our current definition of a band -- this is purely
786 * subjective and right now its 802.11 specific.
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700787 */
788static int freq_reg_info(u32 center_freq, u32 *bandwidth,
789 const struct ieee80211_reg_rule **reg_rule)
Johannes Berg8318d782008-01-24 19:38:38 +0100790{
791 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800792 bool band_rule_found = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700793 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100794
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700795 if (!cfg80211_regdomain)
796 return -EINVAL;
797
798 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
799 const struct ieee80211_reg_rule *rr;
800 const struct ieee80211_freq_range *fr = NULL;
801 const struct ieee80211_power_rule *pr = NULL;
802
803 rr = &cfg80211_regdomain->reg_rules[i];
804 fr = &rr->freq_range;
805 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800806
807 /* We only need to know if one frequency rule was
808 * was in center_freq's band, that's enough, so lets
809 * not overwrite it once found */
810 if (!band_rule_found)
811 band_rule_found = freq_in_rule_band(fr, center_freq);
812
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700813 max_bandwidth = freq_max_bandwidth(fr, center_freq);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800814
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700815 if (max_bandwidth && *bandwidth <= max_bandwidth) {
816 *reg_rule = rr;
817 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100818 break;
819 }
820 }
821
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800822 if (!band_rule_found)
823 return -ERANGE;
824
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700825 return !max_bandwidth;
826}
827
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800828static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
829 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700830{
831 int r;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800832 u32 flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700833 u32 max_bandwidth = 0;
834 const struct ieee80211_reg_rule *reg_rule = NULL;
835 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800836 struct ieee80211_supported_band *sband;
837 struct ieee80211_channel *chan;
838
839 sband = wiphy->bands[band];
840 BUG_ON(chan_idx >= sband->n_channels);
841 chan = &sband->channels[chan_idx];
842
843 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700844
845 r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
846 &max_bandwidth, &reg_rule);
847
848 if (r) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800849 /* This means no regulatory rule was found in the country IE
850 * with a frequency range on the center_freq's band, since
851 * IEEE-802.11 allows for a country IE to have a subset of the
852 * regulatory information provided in a country we ignore
853 * disabling the channel unless at least one reg rule was
854 * found on the center_freq's band. For details see this
855 * clarification:
856 *
857 * http://tinyurl.com/11d-clarification
858 */
859 if (r == -ERANGE &&
860 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
861#ifdef CONFIG_CFG80211_REG_DEBUG
862 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
863 "intact on %s - no rule found in band on "
864 "Country IE\n",
865 chan->center_freq, wiphy_name(wiphy));
866#endif
867 } else {
868 /* In this case we know the country IE has at least one reg rule
869 * for the band so we respect its band definitions */
870#ifdef CONFIG_CFG80211_REG_DEBUG
871 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
872 printk(KERN_DEBUG "cfg80211: Disabling "
873 "channel %d MHz on %s due to "
874 "Country IE\n",
875 chan->center_freq, wiphy_name(wiphy));
876#endif
877 flags |= IEEE80211_CHAN_DISABLED;
878 chan->flags = flags;
879 }
Johannes Berg8318d782008-01-24 19:38:38 +0100880 return;
881 }
882
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700883 power_rule = &reg_rule->power_rule;
884
885 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100886 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700887 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
888 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400889 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700890 chan->max_power = min(chan->orig_mpwr,
891 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400892 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700893 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100894}
895
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800896static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +0100897{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800898 unsigned int i;
899 struct ieee80211_supported_band *sband;
900
901 BUG_ON(!wiphy->bands[band]);
902 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +0100903
904 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800905 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +0100906}
907
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800908static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
909{
910 if (!last_request)
911 return true;
912 if (setby == REGDOM_SET_BY_CORE &&
913 wiphy->fw_handles_regulatory)
914 return true;
915 return false;
916}
917
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700918static void update_all_wiphy_regulatory(enum reg_set_by setby)
919{
920 struct cfg80211_registered_device *drv;
921
922 list_for_each_entry(drv, &cfg80211_drv_list, list)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -0800923 if (!ignore_reg_update(&drv->wiphy, setby))
924 wiphy_update_regulatory(&drv->wiphy, setby);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700925}
926
927void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
Johannes Berg8318d782008-01-24 19:38:38 +0100928{
929 enum ieee80211_band band;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700930 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100931 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800932 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700933 if (wiphy->reg_notifier)
934 wiphy->reg_notifier(wiphy, setby);
935 }
936}
937
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700938/* Return value which can be used by ignore_request() to indicate
939 * it has been determined we should intersect two regulatory domains */
940#define REG_INTERSECT 1
941
Johannes Berg84fa4f42008-10-24 20:32:23 +0200942/* This has the logic which determines when a new request
943 * should be ignored. */
944static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
945 const char *alpha2)
946{
947 /* All initial requests are respected */
948 if (!last_request)
949 return 0;
950
951 switch (set_by) {
952 case REGDOM_SET_BY_INIT:
953 return -EINVAL;
954 case REGDOM_SET_BY_CORE:
955 /*
956 * Always respect new wireless core hints, should only happen
957 * when updating the world regulatory domain at init.
958 */
959 return 0;
960 case REGDOM_SET_BY_COUNTRY_IE:
961 if (unlikely(!is_an_alpha2(alpha2)))
962 return -EINVAL;
963 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
964 if (last_request->wiphy != wiphy) {
965 /*
966 * Two cards with two APs claiming different
967 * different Country IE alpha2s. We could
968 * intersect them, but that seems unlikely
969 * to be correct. Reject second one for now.
970 */
971 if (!alpha2_equal(alpha2,
972 cfg80211_regdomain->alpha2))
973 return -EOPNOTSUPP;
974 return -EALREADY;
975 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800976 /* Two consecutive Country IE hints on the same wiphy.
977 * This should be picked up early by the driver/stack */
978 if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
979 alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +0200980 return 0;
981 return -EALREADY;
982 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800983 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +0200984 case REGDOM_SET_BY_DRIVER:
985 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
986 return -EALREADY;
987 return 0;
988 case REGDOM_SET_BY_USER:
Johannes Berg84fa4f42008-10-24 20:32:23 +0200989 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700990 return REG_INTERSECT;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800991 /* If the user knows better the user should set the regdom
992 * to their country before the IE is picked up */
993 if (last_request->initiator == REGDOM_SET_BY_USER &&
994 last_request->intersect)
995 return -EOPNOTSUPP;
Johannes Berg84fa4f42008-10-24 20:32:23 +0200996 return 0;
997 }
998
999 return -EINVAL;
1000}
1001
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001002/* Caller must hold &cfg80211_drv_mutex */
1003int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001004 const char *alpha2,
1005 u32 country_ie_checksum,
1006 enum environment_cap env)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001007{
1008 struct regulatory_request *request;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001009 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001010 int r = 0;
1011
Johannes Bergbe3d4812008-10-24 20:32:21 +02001012 r = ignore_request(wiphy, set_by, alpha2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001013
1014 if (r == REG_INTERSECT)
1015 intersect = true;
1016 else if (r)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001017 return r;
1018
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001019 request = kzalloc(sizeof(struct regulatory_request),
1020 GFP_KERNEL);
1021 if (!request)
1022 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001023
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001024 request->alpha2[0] = alpha2[0];
1025 request->alpha2[1] = alpha2[1];
1026 request->initiator = set_by;
1027 request->wiphy = wiphy;
1028 request->intersect = intersect;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001029 request->country_ie_checksum = country_ie_checksum;
1030 request->country_ie_env = env;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001031
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001032 kfree(last_request);
1033 last_request = request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001034 /*
1035 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1036 * AND if CRDA is NOT present nothing will happen, if someone
1037 * wants to bother with 11d with OLD_REG you can add a timer.
1038 * If after x amount of time nothing happens you can call:
1039 *
1040 * return set_regdom(country_ie_regdomain);
1041 *
1042 * to intersect with the static rd
1043 */
Luis R. Rodriguez02ba0b32008-11-12 14:22:00 -08001044 return call_crda(alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001045}
1046
Johannes Bergbe3d4812008-10-24 20:32:21 +02001047void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001048{
Johannes Bergbe3d4812008-10-24 20:32:21 +02001049 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001050
1051 mutex_lock(&cfg80211_drv_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001052 __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001053 mutex_unlock(&cfg80211_drv_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001054}
1055EXPORT_SYMBOL(regulatory_hint);
1056
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001057static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1058 u32 country_ie_checksum)
1059{
1060 if (!last_request->wiphy)
1061 return false;
1062 if (likely(last_request->wiphy != wiphy))
1063 return !country_ie_integrity_changes(country_ie_checksum);
1064 /* We should not have let these through at this point, they
1065 * should have been picked up earlier by the first alpha2 check
1066 * on the device */
1067 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1068 return true;
1069 return false;
1070}
1071
1072void regulatory_hint_11d(struct wiphy *wiphy,
1073 u8 *country_ie,
1074 u8 country_ie_len)
1075{
1076 struct ieee80211_regdomain *rd = NULL;
1077 char alpha2[2];
1078 u32 checksum = 0;
1079 enum environment_cap env = ENVIRON_ANY;
1080
Johannes Berg0f70f392008-12-01 18:13:05 +01001081 if (!last_request)
1082 return;
1083
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001084 mutex_lock(&cfg80211_drv_mutex);
1085
1086 /* IE len must be evenly divisible by 2 */
1087 if (country_ie_len & 0x01)
1088 goto out;
1089
1090 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1091 goto out;
1092
1093 /* Pending country IE processing, this can happen after we
1094 * call CRDA and wait for a response if a beacon was received before
1095 * we were able to process the last regulatory_hint_11d() call */
1096 if (country_ie_regdomain)
1097 goto out;
1098
1099 alpha2[0] = country_ie[0];
1100 alpha2[1] = country_ie[1];
1101
1102 if (country_ie[2] == 'I')
1103 env = ENVIRON_INDOOR;
1104 else if (country_ie[2] == 'O')
1105 env = ENVIRON_OUTDOOR;
1106
1107 /* We will run this for *every* beacon processed for the BSSID, so
1108 * we optimize an early check to exit out early if we don't have to
1109 * do anything */
1110 if (likely(last_request->wiphy)) {
1111 struct cfg80211_registered_device *drv_last_ie;
1112
1113 drv_last_ie = wiphy_to_dev(last_request->wiphy);
1114
1115 /* Lets keep this simple -- we trust the first AP
1116 * after we intersect with CRDA */
1117 if (likely(last_request->wiphy == wiphy)) {
1118 /* Ignore IEs coming in on this wiphy with
1119 * the same alpha2 and environment cap */
1120 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1121 alpha2) &&
1122 env == drv_last_ie->env)) {
1123 goto out;
1124 }
1125 /* the wiphy moved on to another BSSID or the AP
1126 * was reconfigured. XXX: We need to deal with the
1127 * case where the user suspends and goes to goes
1128 * to another country, and then gets IEs from an
1129 * AP with different settings */
1130 goto out;
1131 } else {
1132 /* Ignore IEs coming in on two separate wiphys with
1133 * the same alpha2 and environment cap */
1134 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1135 alpha2) &&
1136 env == drv_last_ie->env)) {
1137 goto out;
1138 }
1139 /* We could potentially intersect though */
1140 goto out;
1141 }
1142 }
1143
1144 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1145 if (!rd)
1146 goto out;
1147
1148 /* This will not happen right now but we leave it here for the
1149 * the future when we want to add suspend/resume support and having
1150 * the user move to another country after doing so, or having the user
1151 * move to another AP. Right now we just trust the first AP. This is why
1152 * this is marked as likley(). If we hit this before we add this support
1153 * we want to be informed of it as it would indicate a mistake in the
1154 * current design */
1155 if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
1156 goto out;
1157
1158 /* We keep this around for when CRDA comes back with a response so
1159 * we can intersect with that */
1160 country_ie_regdomain = rd;
1161
1162 __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
1163 country_ie_regdomain->alpha2, checksum, env);
1164
1165out:
1166 mutex_unlock(&cfg80211_drv_mutex);
1167}
1168EXPORT_SYMBOL(regulatory_hint_11d);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001169
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001170static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001171{
1172 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001173 const struct ieee80211_reg_rule *reg_rule = NULL;
1174 const struct ieee80211_freq_range *freq_range = NULL;
1175 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001176
1177 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1178 "(max_antenna_gain, max_eirp)\n");
1179
1180 for (i = 0; i < rd->n_reg_rules; i++) {
1181 reg_rule = &rd->reg_rules[i];
1182 freq_range = &reg_rule->freq_range;
1183 power_rule = &reg_rule->power_rule;
1184
1185 /* There may not be documentation for max antenna gain
1186 * in certain regions */
1187 if (power_rule->max_antenna_gain)
1188 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1189 "(%d mBi, %d mBm)\n",
1190 freq_range->start_freq_khz,
1191 freq_range->end_freq_khz,
1192 freq_range->max_bandwidth_khz,
1193 power_rule->max_antenna_gain,
1194 power_rule->max_eirp);
1195 else
1196 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1197 "(N/A, %d mBm)\n",
1198 freq_range->start_freq_khz,
1199 freq_range->end_freq_khz,
1200 freq_range->max_bandwidth_khz,
1201 power_rule->max_eirp);
1202 }
1203}
1204
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001205static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001206{
1207
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001208 if (is_intersected_alpha2(rd->alpha2)) {
1209 struct wiphy *wiphy = NULL;
1210 struct cfg80211_registered_device *drv;
1211
1212 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1213 if (last_request->wiphy) {
1214 wiphy = last_request->wiphy;
1215 drv = wiphy_to_dev(wiphy);
1216 printk(KERN_INFO "cfg80211: Current regulatory "
1217 "domain updated by AP to: %c%c\n",
1218 drv->country_ie_alpha2[0],
1219 drv->country_ie_alpha2[1]);
1220 } else
1221 printk(KERN_INFO "cfg80211: Current regulatory "
1222 "domain intersected: \n");
1223 } else
1224 printk(KERN_INFO "cfg80211: Current regulatory "
1225 "intersected: \n");
1226 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001227 printk(KERN_INFO "cfg80211: World regulatory "
1228 "domain updated:\n");
1229 else {
1230 if (is_unknown_alpha2(rd->alpha2))
1231 printk(KERN_INFO "cfg80211: Regulatory domain "
1232 "changed to driver built-in settings "
1233 "(unknown country)\n");
1234 else
1235 printk(KERN_INFO "cfg80211: Regulatory domain "
1236 "changed to country: %c%c\n",
1237 rd->alpha2[0], rd->alpha2[1]);
1238 }
1239 print_rd_rules(rd);
1240}
1241
Johannes Berg2df78162008-10-28 16:49:41 +01001242static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001243{
1244 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1245 rd->alpha2[0], rd->alpha2[1]);
1246 print_rd_rules(rd);
1247}
1248
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001249#ifdef CONFIG_CFG80211_REG_DEBUG
1250static void reg_country_ie_process_debug(
1251 const struct ieee80211_regdomain *rd,
1252 const struct ieee80211_regdomain *country_ie_regdomain,
1253 const struct ieee80211_regdomain *intersected_rd)
1254{
1255 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1256 print_regdomain_info(country_ie_regdomain);
1257 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1258 print_regdomain_info(rd);
1259 if (intersected_rd) {
1260 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1261 "and get:\n");
1262 print_regdomain_info(rd);
1263 return;
1264 }
1265 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1266}
1267#else
1268static inline void reg_country_ie_process_debug(
1269 const struct ieee80211_regdomain *rd,
1270 const struct ieee80211_regdomain *country_ie_regdomain,
1271 const struct ieee80211_regdomain *intersected_rd)
1272{
1273}
1274#endif
1275
Johannes Bergd2372b32008-10-24 20:32:20 +02001276/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001277static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001278{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001279 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001280 struct cfg80211_registered_device *drv = NULL;
1281 struct wiphy *wiphy = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001282 /* Some basic sanity checks first */
1283
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001284 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02001285 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001286 return -EINVAL;
1287 update_world_regdomain(rd);
1288 return 0;
1289 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001290
1291 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1292 !is_unknown_alpha2(rd->alpha2))
1293 return -EINVAL;
1294
Johannes Bergf6037d02008-10-21 11:01:33 +02001295 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001296 return -EINVAL;
1297
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001298 /* Lets only bother proceeding on the same alpha2 if the current
1299 * rd is non static (it means CRDA was present and was used last)
1300 * and the pending request came in from a country IE */
1301 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1302 /* If someone else asked us to change the rd lets only bother
1303 * checking if the alpha2 changes if CRDA was already called */
1304 if (!is_old_static_regdom(cfg80211_regdomain) &&
1305 !regdom_changed(rd->alpha2))
1306 return -EINVAL;
1307 }
1308
1309 wiphy = last_request->wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001310
1311 /* Now lets set the regulatory domain, update all driver channels
1312 * and finally inform them of what we have done, in case they want
1313 * to review or adjust their own settings based on their own
1314 * internal EEPROM data */
1315
Johannes Bergf6037d02008-10-21 11:01:33 +02001316 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001317 return -EINVAL;
1318
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08001319 if (!is_valid_rd(rd)) {
1320 printk(KERN_ERR "cfg80211: Invalid "
1321 "regulatory domain detected:\n");
1322 print_regdomain_info(rd);
1323 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001324 }
1325
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001326 if (!last_request->intersect) {
1327 reset_regdomains();
1328 cfg80211_regdomain = rd;
1329 return 0;
1330 }
1331
1332 /* Intersection requires a bit more work */
1333
1334 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1335
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001336 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1337 if (!intersected_rd)
1338 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001339
1340 /* We can trash what CRDA provided now */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001341 kfree(rd);
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001342 rd = NULL;
1343
1344 reset_regdomains();
1345 cfg80211_regdomain = intersected_rd;
1346
1347 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001348 }
1349
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001350 /*
1351 * Country IE requests are handled a bit differently, we intersect
1352 * the country IE rd with what CRDA believes that country should have
1353 */
1354
1355 BUG_ON(!country_ie_regdomain);
1356
1357 if (rd != country_ie_regdomain) {
1358 /* Intersect what CRDA returned and our what we
1359 * had built from the Country IE received */
1360
1361 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1362
1363 reg_country_ie_process_debug(rd, country_ie_regdomain,
1364 intersected_rd);
1365
1366 kfree(country_ie_regdomain);
1367 country_ie_regdomain = NULL;
1368 } else {
1369 /* This would happen when CRDA was not present and
1370 * OLD_REGULATORY was enabled. We intersect our Country
1371 * IE rd and what was set on cfg80211 originally */
1372 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1373 }
1374
1375 if (!intersected_rd)
1376 return -EINVAL;
1377
1378 drv = wiphy_to_dev(wiphy);
1379
1380 drv->country_ie_alpha2[0] = rd->alpha2[0];
1381 drv->country_ie_alpha2[1] = rd->alpha2[1];
1382 drv->env = last_request->country_ie_env;
1383
1384 BUG_ON(intersected_rd == rd);
1385
1386 kfree(rd);
1387 rd = NULL;
1388
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08001389 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001390 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001391
1392 return 0;
1393}
1394
1395
1396/* Use this call to set the current regulatory domain. Conflicts with
1397 * multiple drivers can be ironed out later. Caller must've already
Johannes Bergd2372b32008-10-24 20:32:20 +02001398 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001399int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001400{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001401 int r;
1402
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001403 /* Note that this doesn't update the wiphys, this is done below */
1404 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02001405 if (r) {
1406 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001407 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02001408 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001409
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001410 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001411 if (!last_request->intersect)
1412 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001413
1414 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02001415 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001416
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08001417 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001418
1419 return r;
1420}
1421
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001422/* Caller must hold cfg80211_drv_mutex */
1423void reg_device_remove(struct wiphy *wiphy)
1424{
Johannes Berg4a4f4d82008-12-01 12:07:56 +01001425 if (!last_request || !last_request->wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001426 return;
1427 if (last_request->wiphy != wiphy)
1428 return;
1429 last_request->wiphy = NULL;
1430 last_request->country_ie_env = ENVIRON_ANY;
1431}
1432
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001433int regulatory_init(void)
1434{
Johannes Berg734366d2008-09-15 10:56:48 +02001435 int err;
1436
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001437 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1438 if (IS_ERR(reg_pdev))
1439 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001440
1441#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001442 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02001443
Johannes Berg942b25c2008-09-15 11:26:47 +02001444 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02001445 print_regdomain_info(cfg80211_regdomain);
1446 /* The old code still requests for a new regdomain and if
1447 * you have CRDA you get it updated, otherwise you get
1448 * stuck with the static values. We ignore "EU" code as
1449 * that is not a valid ISO / IEC 3166 alpha2 */
Johannes Bergac9440a2008-10-21 11:08:27 +02001450 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
Johannes Berg734366d2008-09-15 10:56:48 +02001451 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001452 ieee80211_regdom, 0, ENVIRON_ANY);
Johannes Berg734366d2008-09-15 10:56:48 +02001453#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001454 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02001455
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001456 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
Johannes Berg734366d2008-09-15 10:56:48 +02001457 if (err)
1458 printk(KERN_ERR "cfg80211: calling CRDA failed - "
1459 "unable to update world regulatory domain, "
1460 "using static definition\n");
1461#endif
1462
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001463 return 0;
1464}
1465
1466void regulatory_exit(void)
1467{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001468 mutex_lock(&cfg80211_drv_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02001469
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001470 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02001471
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001472 kfree(country_ie_regdomain);
1473 country_ie_regdomain = NULL;
1474
Johannes Bergf6037d02008-10-21 11:01:33 +02001475 kfree(last_request);
1476
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001477 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02001478
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001479 mutex_unlock(&cfg80211_drv_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01001480}