blob: 3f18a1d03a552c0a33f794adc199bc0893e98277 [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
Johannes Bergbe3d4812008-10-24 20:32:21 +020045/*
46 * wiphy is set if this request's initiator is
47 * REGDOM_SET_BY_COUNTRY_IE or _DRIVER
48 */
Johannes Berg734366d2008-09-15 10:56:48 +020049struct regulatory_request {
Johannes Berg734366d2008-09-15 10:56:48 +020050 struct wiphy *wiphy;
Johannes Berg734366d2008-09-15 10:56:48 +020051 enum reg_set_by initiator;
52 char alpha2[2];
Luis R. Rodriguez9c964772008-10-30 13:33:53 -070053 bool intersect;
Johannes Berg734366d2008-09-15 10:56:48 +020054};
55
Johannes Bergf6037d02008-10-21 11:01:33 +020056static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020057
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070058/* To trigger userspace events */
59static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010060
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070061/* Keep the ordering from large to small */
62static u32 supported_bandwidths[] = {
63 MHZ_TO_KHZ(40),
64 MHZ_TO_KHZ(20),
Johannes Berg8318d782008-01-24 19:38:38 +010065};
66
Johannes Berg734366d2008-09-15 10:56:48 +020067/* Central wireless core regulatory domains, we only need two,
68 * the current one and a world regulatory domain in case we have no
69 * information to give us an alpha2 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +020070static const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020071
72/* We keep a static world regulatory domain in case of the absence of CRDA */
73static const struct ieee80211_regdomain world_regdom = {
74 .n_reg_rules = 1,
75 .alpha2 = "00",
76 .reg_rules = {
77 REG_RULE(2412-10, 2462+10, 40, 6, 20,
78 NL80211_RRF_PASSIVE_SCAN |
79 NL80211_RRF_NO_IBSS),
80 }
81};
82
Johannes Berga3d2eaf2008-09-15 11:10:52 +020083static const struct ieee80211_regdomain *cfg80211_world_regdom =
84 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +020085
86#ifdef CONFIG_WIRELESS_OLD_REGULATORY
87static char *ieee80211_regdom = "US";
88module_param(ieee80211_regdom, charp, 0444);
89MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
90
91/* We assume 40 MHz bandwidth for the old regulatory work.
92 * We make emphasis we are using the exact same frequencies
93 * as before */
94
95static const struct ieee80211_regdomain us_regdom = {
96 .n_reg_rules = 6,
97 .alpha2 = "US",
98 .reg_rules = {
99 /* IEEE 802.11b/g, channels 1..11 */
100 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
101 /* IEEE 802.11a, channel 36 */
102 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
103 /* IEEE 802.11a, channel 40 */
104 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
105 /* IEEE 802.11a, channel 44 */
106 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
107 /* IEEE 802.11a, channels 48..64 */
108 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
109 /* IEEE 802.11a, channels 149..165, outdoor */
110 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
111 }
112};
113
114static const struct ieee80211_regdomain jp_regdom = {
115 .n_reg_rules = 3,
116 .alpha2 = "JP",
117 .reg_rules = {
118 /* IEEE 802.11b/g, channels 1..14 */
119 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
120 /* IEEE 802.11a, channels 34..48 */
121 REG_RULE(5170-10, 5240+10, 40, 6, 20,
122 NL80211_RRF_PASSIVE_SCAN),
123 /* IEEE 802.11a, channels 52..64 */
124 REG_RULE(5260-10, 5320+10, 40, 6, 20,
125 NL80211_RRF_NO_IBSS |
126 NL80211_RRF_DFS),
127 }
128};
129
130static const struct ieee80211_regdomain eu_regdom = {
131 .n_reg_rules = 6,
132 /* This alpha2 is bogus, we leave it here just for stupid
133 * backward compatibility */
134 .alpha2 = "EU",
135 .reg_rules = {
136 /* IEEE 802.11b/g, channels 1..13 */
137 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
138 /* IEEE 802.11a, channel 36 */
139 REG_RULE(5180-10, 5180+10, 40, 6, 23,
140 NL80211_RRF_PASSIVE_SCAN),
141 /* IEEE 802.11a, channel 40 */
142 REG_RULE(5200-10, 5200+10, 40, 6, 23,
143 NL80211_RRF_PASSIVE_SCAN),
144 /* IEEE 802.11a, channel 44 */
145 REG_RULE(5220-10, 5220+10, 40, 6, 23,
146 NL80211_RRF_PASSIVE_SCAN),
147 /* IEEE 802.11a, channels 48..64 */
148 REG_RULE(5240-10, 5320+10, 40, 6, 20,
149 NL80211_RRF_NO_IBSS |
150 NL80211_RRF_DFS),
151 /* IEEE 802.11a, channels 100..140 */
152 REG_RULE(5500-10, 5700+10, 40, 6, 30,
153 NL80211_RRF_NO_IBSS |
154 NL80211_RRF_DFS),
155 }
156};
157
158static const struct ieee80211_regdomain *static_regdom(char *alpha2)
159{
160 if (alpha2[0] == 'U' && alpha2[1] == 'S')
161 return &us_regdom;
162 if (alpha2[0] == 'J' && alpha2[1] == 'P')
163 return &jp_regdom;
164 if (alpha2[0] == 'E' && alpha2[1] == 'U')
165 return &eu_regdom;
166 /* Default, as per the old rules */
167 return &us_regdom;
168}
169
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200170static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200171{
172 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
173 return true;
174 return false;
175}
Johannes Berg734366d2008-09-15 10:56:48 +0200176#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200177static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
178{
179 return false;
180}
181#endif
182
Johannes Berg734366d2008-09-15 10:56:48 +0200183static void reset_regdomains(void)
184{
Johannes Berg942b25c2008-09-15 11:26:47 +0200185 /* avoid freeing static information or freeing something twice */
186 if (cfg80211_regdomain == cfg80211_world_regdom)
187 cfg80211_regdomain = NULL;
188 if (cfg80211_world_regdom == &world_regdom)
189 cfg80211_world_regdom = NULL;
190 if (cfg80211_regdomain == &world_regdom)
191 cfg80211_regdomain = NULL;
192 if (is_old_static_regdom(cfg80211_regdomain))
193 cfg80211_regdomain = NULL;
194
195 kfree(cfg80211_regdomain);
196 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200197
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200198 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200199 cfg80211_regdomain = NULL;
200}
201
202/* Dynamic world regulatory domain requested by the wireless
203 * core upon initialization */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200204static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200205{
Johannes Bergf6037d02008-10-21 11:01:33 +0200206 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200207
208 reset_regdomains();
209
210 cfg80211_world_regdom = rd;
211 cfg80211_regdomain = rd;
212}
Johannes Berg734366d2008-09-15 10:56:48 +0200213
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200214bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100215{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700216 if (!alpha2)
217 return false;
218 if (alpha2[0] == '0' && alpha2[1] == '0')
219 return true;
220 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100221}
222
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200223static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700224{
225 if (!alpha2)
226 return false;
227 if (alpha2[0] != 0 && alpha2[1] != 0)
228 return true;
229 return false;
230}
Johannes Berg8318d782008-01-24 19:38:38 +0100231
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700232static bool is_alpha_upper(char letter)
233{
234 /* ASCII A - Z */
235 if (letter >= 65 && letter <= 90)
236 return true;
237 return false;
238}
239
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200240static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700241{
242 if (!alpha2)
243 return false;
244 /* Special case where regulatory domain was built by driver
245 * but a specific alpha2 cannot be determined */
246 if (alpha2[0] == '9' && alpha2[1] == '9')
247 return true;
248 return false;
249}
250
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200251static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700252{
253 if (!alpha2)
254 return false;
255 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
256 return true;
257 return false;
258}
259
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200260static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700261{
262 if (!alpha2_x || !alpha2_y)
263 return false;
264 if (alpha2_x[0] == alpha2_y[0] &&
265 alpha2_x[1] == alpha2_y[1])
266 return true;
267 return false;
268}
269
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200270static bool regdom_changed(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700271{
272 if (!cfg80211_regdomain)
273 return true;
274 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
275 return false;
276 return true;
277}
278
279/* This lets us keep regulatory code which is updated on a regulatory
280 * basis in userspace. */
281static int call_crda(const char *alpha2)
282{
283 char country_env[9 + 2] = "COUNTRY=";
284 char *envp[] = {
285 country_env,
286 NULL
287 };
288
289 if (!is_world_regdom((char *) alpha2))
290 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
291 alpha2[0], alpha2[1]);
292 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700293 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
294 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700295
296 country_env[8] = alpha2[0];
297 country_env[9] = alpha2[1];
298
299 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
300}
301
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700302/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200303bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700304{
Johannes Bergf6037d02008-10-21 11:01:33 +0200305 if (!last_request)
306 return false;
307
308 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700309}
310
311/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200312static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700313{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200314 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700315 u32 freq_diff;
316
317 if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
318 return false;
319
320 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
321 return false;
322
323 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
324
Luis R. Rodriguezd71aaf62008-10-30 13:33:52 -0700325 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700326 return false;
327
328 return true;
329}
330
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200331static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700332{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200333 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700334 unsigned int i;
335
336 if (!rd->n_reg_rules)
337 return false;
338
339 for (i = 0; i < rd->n_reg_rules; i++) {
340 reg_rule = &rd->reg_rules[i];
341 if (!is_valid_reg_rule(reg_rule))
342 return false;
343 }
344
345 return true;
346}
347
348/* Returns value in KHz */
349static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
350 u32 freq)
351{
352 unsigned int i;
353 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
354 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
355 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
356 if (start_freq_khz >= freq_range->start_freq_khz &&
357 end_freq_khz <= freq_range->end_freq_khz)
358 return supported_bandwidths[i];
359 }
360 return 0;
361}
362
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700363/* Helper for regdom_intersect(), this does the real
364 * mathematical intersection fun */
365static int reg_rules_intersect(
366 const struct ieee80211_reg_rule *rule1,
367 const struct ieee80211_reg_rule *rule2,
368 struct ieee80211_reg_rule *intersected_rule)
369{
370 const struct ieee80211_freq_range *freq_range1, *freq_range2;
371 struct ieee80211_freq_range *freq_range;
372 const struct ieee80211_power_rule *power_rule1, *power_rule2;
373 struct ieee80211_power_rule *power_rule;
374 u32 freq_diff;
375
376 freq_range1 = &rule1->freq_range;
377 freq_range2 = &rule2->freq_range;
378 freq_range = &intersected_rule->freq_range;
379
380 power_rule1 = &rule1->power_rule;
381 power_rule2 = &rule2->power_rule;
382 power_rule = &intersected_rule->power_rule;
383
384 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
385 freq_range2->start_freq_khz);
386 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
387 freq_range2->end_freq_khz);
388 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
389 freq_range2->max_bandwidth_khz);
390
391 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
392 if (freq_range->max_bandwidth_khz > freq_diff)
393 freq_range->max_bandwidth_khz = freq_diff;
394
395 power_rule->max_eirp = min(power_rule1->max_eirp,
396 power_rule2->max_eirp);
397 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
398 power_rule2->max_antenna_gain);
399
400 intersected_rule->flags = (rule1->flags | rule2->flags);
401
402 if (!is_valid_reg_rule(intersected_rule))
403 return -EINVAL;
404
405 return 0;
406}
407
408/**
409 * regdom_intersect - do the intersection between two regulatory domains
410 * @rd1: first regulatory domain
411 * @rd2: second regulatory domain
412 *
413 * Use this function to get the intersection between two regulatory domains.
414 * Once completed we will mark the alpha2 for the rd as intersected, "98",
415 * as no one single alpha2 can represent this regulatory domain.
416 *
417 * Returns a pointer to the regulatory domain structure which will hold the
418 * resulting intersection of rules between rd1 and rd2. We will
419 * kzalloc() this structure for you.
420 */
421static struct ieee80211_regdomain *regdom_intersect(
422 const struct ieee80211_regdomain *rd1,
423 const struct ieee80211_regdomain *rd2)
424{
425 int r, size_of_regd;
426 unsigned int x, y;
427 unsigned int num_rules = 0, rule_idx = 0;
428 const struct ieee80211_reg_rule *rule1, *rule2;
429 struct ieee80211_reg_rule *intersected_rule;
430 struct ieee80211_regdomain *rd;
431 /* This is just a dummy holder to help us count */
432 struct ieee80211_reg_rule irule;
433
434 /* Uses the stack temporarily for counter arithmetic */
435 intersected_rule = &irule;
436
437 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
438
439 if (!rd1 || !rd2)
440 return NULL;
441
442 /* First we get a count of the rules we'll need, then we actually
443 * build them. This is to so we can malloc() and free() a
444 * regdomain once. The reason we use reg_rules_intersect() here
445 * is it will return -EINVAL if the rule computed makes no sense.
446 * All rules that do check out OK are valid. */
447
448 for (x = 0; x < rd1->n_reg_rules; x++) {
449 rule1 = &rd1->reg_rules[x];
450 for (y = 0; y < rd2->n_reg_rules; y++) {
451 rule2 = &rd2->reg_rules[y];
452 if (!reg_rules_intersect(rule1, rule2,
453 intersected_rule))
454 num_rules++;
455 memset(intersected_rule, 0,
456 sizeof(struct ieee80211_reg_rule));
457 }
458 }
459
460 if (!num_rules)
461 return NULL;
462
463 size_of_regd = sizeof(struct ieee80211_regdomain) +
464 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
465
466 rd = kzalloc(size_of_regd, GFP_KERNEL);
467 if (!rd)
468 return NULL;
469
470 for (x = 0; x < rd1->n_reg_rules; x++) {
471 rule1 = &rd1->reg_rules[x];
472 for (y = 0; y < rd2->n_reg_rules; y++) {
473 rule2 = &rd2->reg_rules[y];
474 /* This time around instead of using the stack lets
475 * write to the target rule directly saving ourselves
476 * a memcpy() */
477 intersected_rule = &rd->reg_rules[rule_idx];
478 r = reg_rules_intersect(rule1, rule2,
479 intersected_rule);
480 /* No need to memset here the intersected rule here as
481 * we're not using the stack anymore */
482 if (r)
483 continue;
484 rule_idx++;
485 }
486 }
487
488 if (rule_idx != num_rules) {
489 kfree(rd);
490 return NULL;
491 }
492
493 rd->n_reg_rules = num_rules;
494 rd->alpha2[0] = '9';
495 rd->alpha2[1] = '8';
496
497 return rd;
498}
499
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700500/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
501 * want to just have the channel structure use these */
502static u32 map_regdom_flags(u32 rd_flags)
503{
504 u32 channel_flags = 0;
505 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
506 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
507 if (rd_flags & NL80211_RRF_NO_IBSS)
508 channel_flags |= IEEE80211_CHAN_NO_IBSS;
509 if (rd_flags & NL80211_RRF_DFS)
510 channel_flags |= IEEE80211_CHAN_RADAR;
511 return channel_flags;
512}
513
514/**
515 * freq_reg_info - get regulatory information for the given frequency
516 * @center_freq: Frequency in KHz for which we want regulatory information for
517 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
518 * you can set this to 0. If this frequency is allowed we then set
519 * this value to the maximum allowed bandwidth.
520 * @reg_rule: the regulatory rule which we have for this frequency
521 *
522 * Use this function to get the regulatory rule for a specific frequency.
523 */
524static int freq_reg_info(u32 center_freq, u32 *bandwidth,
525 const struct ieee80211_reg_rule **reg_rule)
Johannes Berg8318d782008-01-24 19:38:38 +0100526{
527 int i;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700528 u32 max_bandwidth = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100529
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700530 if (!cfg80211_regdomain)
531 return -EINVAL;
532
533 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
534 const struct ieee80211_reg_rule *rr;
535 const struct ieee80211_freq_range *fr = NULL;
536 const struct ieee80211_power_rule *pr = NULL;
537
538 rr = &cfg80211_regdomain->reg_rules[i];
539 fr = &rr->freq_range;
540 pr = &rr->power_rule;
541 max_bandwidth = freq_max_bandwidth(fr, center_freq);
542 if (max_bandwidth && *bandwidth <= max_bandwidth) {
543 *reg_rule = rr;
544 *bandwidth = max_bandwidth;
Johannes Berg8318d782008-01-24 19:38:38 +0100545 break;
546 }
547 }
548
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700549 return !max_bandwidth;
550}
551
552static void handle_channel(struct ieee80211_channel *chan)
553{
554 int r;
555 u32 flags = chan->orig_flags;
556 u32 max_bandwidth = 0;
557 const struct ieee80211_reg_rule *reg_rule = NULL;
558 const struct ieee80211_power_rule *power_rule = NULL;
559
560 r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
561 &max_bandwidth, &reg_rule);
562
563 if (r) {
Johannes Berg8318d782008-01-24 19:38:38 +0100564 flags |= IEEE80211_CHAN_DISABLED;
565 chan->flags = flags;
566 return;
567 }
568
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700569 power_rule = &reg_rule->power_rule;
570
571 chan->flags = flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +0100572 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700573 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
574 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
John W. Linville253898c2008-04-03 15:32:54 -0400575 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700576 chan->max_power = min(chan->orig_mpwr,
577 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -0400578 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700579 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +0100580}
581
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700582static void handle_band(struct ieee80211_supported_band *sband)
Johannes Berg8318d782008-01-24 19:38:38 +0100583{
584 int i;
585
586 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700587 handle_channel(&sband->channels[i]);
Johannes Berg8318d782008-01-24 19:38:38 +0100588}
589
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700590static void update_all_wiphy_regulatory(enum reg_set_by setby)
591{
592 struct cfg80211_registered_device *drv;
593
594 list_for_each_entry(drv, &cfg80211_drv_list, list)
595 wiphy_update_regulatory(&drv->wiphy, setby);
596}
597
598void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
Johannes Berg8318d782008-01-24 19:38:38 +0100599{
600 enum ieee80211_band band;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700601 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100602 if (wiphy->bands[band])
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700603 handle_band(wiphy->bands[band]);
604 if (wiphy->reg_notifier)
605 wiphy->reg_notifier(wiphy, setby);
606 }
607}
608
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700609/* Return value which can be used by ignore_request() to indicate
610 * it has been determined we should intersect two regulatory domains */
611#define REG_INTERSECT 1
612
Johannes Berg84fa4f42008-10-24 20:32:23 +0200613/* This has the logic which determines when a new request
614 * should be ignored. */
615static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
616 const char *alpha2)
617{
618 /* All initial requests are respected */
619 if (!last_request)
620 return 0;
621
622 switch (set_by) {
623 case REGDOM_SET_BY_INIT:
624 return -EINVAL;
625 case REGDOM_SET_BY_CORE:
626 /*
627 * Always respect new wireless core hints, should only happen
628 * when updating the world regulatory domain at init.
629 */
630 return 0;
631 case REGDOM_SET_BY_COUNTRY_IE:
632 if (unlikely(!is_an_alpha2(alpha2)))
633 return -EINVAL;
634 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
635 if (last_request->wiphy != wiphy) {
636 /*
637 * Two cards with two APs claiming different
638 * different Country IE alpha2s. We could
639 * intersect them, but that seems unlikely
640 * to be correct. Reject second one for now.
641 */
642 if (!alpha2_equal(alpha2,
643 cfg80211_regdomain->alpha2))
644 return -EOPNOTSUPP;
645 return -EALREADY;
646 }
647 /* Two consecutive Country IE hints on the same wiphy */
648 if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
649 return 0;
650 return -EALREADY;
651 }
652 /*
653 * Ignore Country IE hints for now, need to think about
654 * what we need to do to support multi-domain operation.
655 */
656 return -EOPNOTSUPP;
657 case REGDOM_SET_BY_DRIVER:
658 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
659 return -EALREADY;
660 return 0;
661 case REGDOM_SET_BY_USER:
Johannes Berg84fa4f42008-10-24 20:32:23 +0200662 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700663 return REG_INTERSECT;
Johannes Berg84fa4f42008-10-24 20:32:23 +0200664 return 0;
665 }
666
667 return -EINVAL;
668}
669
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700670/* Caller must hold &cfg80211_drv_mutex */
671int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
Johannes Bergbe3d4812008-10-24 20:32:21 +0200672 const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700673{
674 struct regulatory_request *request;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700675 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700676 int r = 0;
677
Johannes Bergbe3d4812008-10-24 20:32:21 +0200678 r = ignore_request(wiphy, set_by, alpha2);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700679
680 if (r == REG_INTERSECT)
681 intersect = true;
682 else if (r)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700683 return r;
684
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700685 switch (set_by) {
686 case REGDOM_SET_BY_CORE:
687 case REGDOM_SET_BY_COUNTRY_IE:
688 case REGDOM_SET_BY_DRIVER:
689 case REGDOM_SET_BY_USER:
690 request = kzalloc(sizeof(struct regulatory_request),
Johannes Bergbe3d4812008-10-24 20:32:21 +0200691 GFP_KERNEL);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700692 if (!request)
693 return -ENOMEM;
694
Johannes Bergbe3d4812008-10-24 20:32:21 +0200695 request->alpha2[0] = alpha2[0];
696 request->alpha2[1] = alpha2[1];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700697 request->initiator = set_by;
698 request->wiphy = wiphy;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700699 request->intersect = intersect;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700700
Johannes Bergf6037d02008-10-21 11:01:33 +0200701 kfree(last_request);
702 last_request = request;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700703 r = call_crda(alpha2);
704#ifndef CONFIG_WIRELESS_OLD_REGULATORY
705 if (r)
706 printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
707#endif
708 break;
709 default:
710 r = -ENOTSUPP;
711 break;
712 }
713
714 return r;
715}
716
Johannes Bergbe3d4812008-10-24 20:32:21 +0200717void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700718{
Johannes Bergbe3d4812008-10-24 20:32:21 +0200719 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700720
721 mutex_lock(&cfg80211_drv_mutex);
Johannes Bergbe3d4812008-10-24 20:32:21 +0200722 __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700723 mutex_unlock(&cfg80211_drv_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700724}
725EXPORT_SYMBOL(regulatory_hint);
726
727
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200728static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700729{
730 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200731 const struct ieee80211_reg_rule *reg_rule = NULL;
732 const struct ieee80211_freq_range *freq_range = NULL;
733 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700734
735 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
736 "(max_antenna_gain, max_eirp)\n");
737
738 for (i = 0; i < rd->n_reg_rules; i++) {
739 reg_rule = &rd->reg_rules[i];
740 freq_range = &reg_rule->freq_range;
741 power_rule = &reg_rule->power_rule;
742
743 /* There may not be documentation for max antenna gain
744 * in certain regions */
745 if (power_rule->max_antenna_gain)
746 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
747 "(%d mBi, %d mBm)\n",
748 freq_range->start_freq_khz,
749 freq_range->end_freq_khz,
750 freq_range->max_bandwidth_khz,
751 power_rule->max_antenna_gain,
752 power_rule->max_eirp);
753 else
754 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
755 "(N/A, %d mBm)\n",
756 freq_range->start_freq_khz,
757 freq_range->end_freq_khz,
758 freq_range->max_bandwidth_khz,
759 power_rule->max_eirp);
760 }
761}
762
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200763static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700764{
765
766 if (is_world_regdom(rd->alpha2))
767 printk(KERN_INFO "cfg80211: World regulatory "
768 "domain updated:\n");
769 else {
770 if (is_unknown_alpha2(rd->alpha2))
771 printk(KERN_INFO "cfg80211: Regulatory domain "
772 "changed to driver built-in settings "
773 "(unknown country)\n");
774 else
775 printk(KERN_INFO "cfg80211: Regulatory domain "
776 "changed to country: %c%c\n",
777 rd->alpha2[0], rd->alpha2[1]);
778 }
779 print_rd_rules(rd);
780}
781
Johannes Berg2df78162008-10-28 16:49:41 +0100782static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700783{
784 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
785 rd->alpha2[0], rd->alpha2[1]);
786 print_rd_rules(rd);
787}
788
Johannes Bergd2372b32008-10-24 20:32:20 +0200789/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200790static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700791{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700792 const struct ieee80211_regdomain *intersected_rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700793 /* Some basic sanity checks first */
794
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700795 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +0200796 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700797 return -EINVAL;
798 update_world_regdomain(rd);
799 return 0;
800 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700801
802 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
803 !is_unknown_alpha2(rd->alpha2))
804 return -EINVAL;
805
Johannes Bergf6037d02008-10-21 11:01:33 +0200806 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700807 return -EINVAL;
808
Johannes Berg942b25c2008-09-15 11:26:47 +0200809 /* allow overriding the static definitions if CRDA is present */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700810 if (!is_old_static_regdom(cfg80211_regdomain) &&
Johannes Berg942b25c2008-09-15 11:26:47 +0200811 !regdom_changed(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700812 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700813
814 /* Now lets set the regulatory domain, update all driver channels
815 * and finally inform them of what we have done, in case they want
816 * to review or adjust their own settings based on their own
817 * internal EEPROM data */
818
Johannes Bergf6037d02008-10-21 11:01:33 +0200819 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700820 return -EINVAL;
821
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700822 reset_regdomains();
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700823
824 /* Country IE parsing coming soon */
Johannes Bergf6037d02008-10-21 11:01:33 +0200825 switch (last_request->initiator) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700826 case REGDOM_SET_BY_CORE:
827 case REGDOM_SET_BY_DRIVER:
828 case REGDOM_SET_BY_USER:
829 if (!is_valid_rd(rd)) {
830 printk(KERN_ERR "cfg80211: Invalid "
831 "regulatory domain detected:\n");
832 print_regdomain_info(rd);
833 return -EINVAL;
834 }
835 break;
836 case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
837 WARN_ON(1);
838 default:
839 return -EOPNOTSUPP;
840 }
841
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700842 if (unlikely(last_request->intersect)) {
843 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
844 if (!intersected_rd)
845 return -EINVAL;
846 kfree(rd);
847 rd = intersected_rd;
848 }
849
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700850 /* Tada! */
851 cfg80211_regdomain = rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700852
853 return 0;
854}
855
856
857/* Use this call to set the current regulatory domain. Conflicts with
858 * multiple drivers can be ironed out later. Caller must've already
Johannes Bergd2372b32008-10-24 20:32:20 +0200859 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200860int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700861{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700862 int r;
863
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700864 /* Note that this doesn't update the wiphys, this is done below */
865 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +0200866 if (r) {
867 kfree(rd);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700868 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +0200869 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700870
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700871 /* This would make this whole thing pointless */
872 BUG_ON(rd != cfg80211_regdomain);
873
874 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +0200875 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700876
877 print_regdomain(rd);
878
879 return r;
880}
881
882int regulatory_init(void)
883{
Johannes Berg734366d2008-09-15 10:56:48 +0200884 int err;
885
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700886 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
887 if (IS_ERR(reg_pdev))
888 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +0200889
890#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200891 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200892
Johannes Berg942b25c2008-09-15 11:26:47 +0200893 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +0200894 print_regdomain_info(cfg80211_regdomain);
895 /* The old code still requests for a new regdomain and if
896 * you have CRDA you get it updated, otherwise you get
897 * stuck with the static values. We ignore "EU" code as
898 * that is not a valid ISO / IEC 3166 alpha2 */
Johannes Bergac9440a2008-10-21 11:08:27 +0200899 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
Johannes Berg734366d2008-09-15 10:56:48 +0200900 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
Johannes Bergbe3d4812008-10-24 20:32:21 +0200901 ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200902#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200903 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200904
Johannes Bergbe3d4812008-10-24 20:32:21 +0200905 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00");
Johannes Berg734366d2008-09-15 10:56:48 +0200906 if (err)
907 printk(KERN_ERR "cfg80211: calling CRDA failed - "
908 "unable to update world regulatory domain, "
909 "using static definition\n");
910#endif
911
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700912 return 0;
913}
914
915void regulatory_exit(void)
916{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700917 mutex_lock(&cfg80211_drv_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +0200918
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700919 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +0200920
Johannes Bergf6037d02008-10-21 11:01:33 +0200921 kfree(last_request);
922
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700923 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +0200924
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700925 mutex_unlock(&cfg80211_drv_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +0100926}