mac80211_hwsim: add support for 5 GHz
[linux-2.6-block.git] / net / wireless / reg.c
CommitLineData
8318d78a
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
b2e1b302 5 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
8318d78a
JB
6 *
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
b2e1b302
LR
12/**
13 * DOC: Wireless regulatory infrastructure
8318d78a
JB
14 *
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 *
b2e1b302
LR
20 * 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 *
8318d78a
JB
34 */
35#include <linux/kernel.h>
b2e1b302
LR
36#include <linux/list.h>
37#include <linux/random.h>
38#include <linux/nl80211.h>
39#include <linux/platform_device.h>
8318d78a 40#include <net/wireless.h>
b2e1b302 41#include <net/cfg80211.h>
8318d78a 42#include "core.h"
b2e1b302 43#include "reg.h"
8318d78a 44
5166ccd2 45/* Receipt of information from last regulatory request */
f6037d09 46static struct regulatory_request *last_request;
734366de 47
b2e1b302
LR
48/* To trigger userspace events */
49static struct platform_device *reg_pdev;
8318d78a 50
b2e1b302
LR
51/* Keep the ordering from large to small */
52static u32 supported_bandwidths[] = {
53 MHZ_TO_KHZ(40),
54 MHZ_TO_KHZ(20),
8318d78a
JB
55};
56
fb1fc7ad
LR
57/*
58 * Central wireless core regulatory domains, we only need two,
734366de 59 * the current one and a world regulatory domain in case we have no
fb1fc7ad
LR
60 * information to give us an alpha2
61 */
f130347c 62const struct ieee80211_regdomain *cfg80211_regdomain;
734366de 63
fb1fc7ad
LR
64/*
65 * We use this as a place for the rd structure built from the
3f2355cb 66 * last parsed country IE to rest until CRDA gets back to us with
fb1fc7ad
LR
67 * what it thinks should apply for the same country
68 */
3f2355cb
LR
69static const struct ieee80211_regdomain *country_ie_regdomain;
70
e38f8a7a 71/* Used to queue up regulatory hints */
fe33eb39
LR
72static LIST_HEAD(reg_requests_list);
73static spinlock_t reg_requests_lock;
74
e38f8a7a
LR
75/* Used to queue up beacon hints for review */
76static LIST_HEAD(reg_pending_beacons);
77static spinlock_t reg_pending_beacons_lock;
78
79/* Used to keep track of processed beacon hints */
80static LIST_HEAD(reg_beacon_list);
81
82struct reg_beacon {
83 struct list_head list;
84 struct ieee80211_channel chan;
85};
86
734366de
JB
87/* We keep a static world regulatory domain in case of the absence of CRDA */
88static const struct ieee80211_regdomain world_regdom = {
3fc71f77 89 .n_reg_rules = 3,
734366de
JB
90 .alpha2 = "00",
91 .reg_rules = {
68798a62
LR
92 /* IEEE 802.11b/g, channels 1..11 */
93 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
3fc71f77
LR
94 /* IEEE 802.11a, channel 36..48 */
95 REG_RULE(5180-10, 5240+10, 40, 6, 23,
96 NL80211_RRF_PASSIVE_SCAN |
97 NL80211_RRF_NO_IBSS),
98
99 /* NB: 5260 MHz - 5700 MHz requies DFS */
100
101 /* IEEE 802.11a, channel 149..165 */
102 REG_RULE(5745-10, 5825+10, 40, 6, 23,
103 NL80211_RRF_PASSIVE_SCAN |
104 NL80211_RRF_NO_IBSS),
734366de
JB
105 }
106};
107
a3d2eaf0
JB
108static const struct ieee80211_regdomain *cfg80211_world_regdom =
109 &world_regdom;
734366de
JB
110
111#ifdef CONFIG_WIRELESS_OLD_REGULATORY
112static char *ieee80211_regdom = "US";
113module_param(ieee80211_regdom, charp, 0444);
114MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
115
fb1fc7ad
LR
116/*
117 * We assume 40 MHz bandwidth for the old regulatory work.
734366de 118 * We make emphasis we are using the exact same frequencies
fb1fc7ad
LR
119 * as before
120 */
734366de
JB
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,
fb1fc7ad
LR
159 /*
160 * This alpha2 is bogus, we leave it here just for stupid
161 * backward compatibility
162 */
734366de
JB
163 .alpha2 = "EU",
164 .reg_rules = {
165 /* IEEE 802.11b/g, channels 1..13 */
166 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
167 /* IEEE 802.11a, channel 36 */
168 REG_RULE(5180-10, 5180+10, 40, 6, 23,
169 NL80211_RRF_PASSIVE_SCAN),
170 /* IEEE 802.11a, channel 40 */
171 REG_RULE(5200-10, 5200+10, 40, 6, 23,
172 NL80211_RRF_PASSIVE_SCAN),
173 /* IEEE 802.11a, channel 44 */
174 REG_RULE(5220-10, 5220+10, 40, 6, 23,
175 NL80211_RRF_PASSIVE_SCAN),
176 /* IEEE 802.11a, channels 48..64 */
177 REG_RULE(5240-10, 5320+10, 40, 6, 20,
178 NL80211_RRF_NO_IBSS |
179 NL80211_RRF_DFS),
180 /* IEEE 802.11a, channels 100..140 */
181 REG_RULE(5500-10, 5700+10, 40, 6, 30,
182 NL80211_RRF_NO_IBSS |
183 NL80211_RRF_DFS),
184 }
185};
186
187static const struct ieee80211_regdomain *static_regdom(char *alpha2)
188{
189 if (alpha2[0] == 'U' && alpha2[1] == 'S')
190 return &us_regdom;
191 if (alpha2[0] == 'J' && alpha2[1] == 'P')
192 return &jp_regdom;
193 if (alpha2[0] == 'E' && alpha2[1] == 'U')
194 return &eu_regdom;
195 /* Default, as per the old rules */
196 return &us_regdom;
197}
198
a3d2eaf0 199static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
734366de
JB
200{
201 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
202 return true;
203 return false;
204}
942b25cf
JB
205#else
206static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
734366de 207{
942b25cf 208 return false;
734366de 209}
942b25cf
JB
210#endif
211
734366de
JB
212static void reset_regdomains(void)
213{
942b25cf
JB
214 /* avoid freeing static information or freeing something twice */
215 if (cfg80211_regdomain == cfg80211_world_regdom)
216 cfg80211_regdomain = NULL;
217 if (cfg80211_world_regdom == &world_regdom)
218 cfg80211_world_regdom = NULL;
219 if (cfg80211_regdomain == &world_regdom)
220 cfg80211_regdomain = NULL;
221 if (is_old_static_regdom(cfg80211_regdomain))
222 cfg80211_regdomain = NULL;
223
224 kfree(cfg80211_regdomain);
225 kfree(cfg80211_world_regdom);
734366de 226
a3d2eaf0 227 cfg80211_world_regdom = &world_regdom;
734366de
JB
228 cfg80211_regdomain = NULL;
229}
230
fb1fc7ad
LR
231/*
232 * Dynamic world regulatory domain requested by the wireless
233 * core upon initialization
234 */
a3d2eaf0 235static void update_world_regdomain(const struct ieee80211_regdomain *rd)
734366de 236{
f6037d09 237 BUG_ON(!last_request);
734366de
JB
238
239 reset_regdomains();
240
241 cfg80211_world_regdom = rd;
242 cfg80211_regdomain = rd;
243}
734366de 244
a3d2eaf0 245bool is_world_regdom(const char *alpha2)
b2e1b302
LR
246{
247 if (!alpha2)
248 return false;
249 if (alpha2[0] == '0' && alpha2[1] == '0')
250 return true;
251 return false;
252}
8318d78a 253
a3d2eaf0 254static bool is_alpha2_set(const char *alpha2)
b2e1b302
LR
255{
256 if (!alpha2)
257 return false;
258 if (alpha2[0] != 0 && alpha2[1] != 0)
259 return true;
260 return false;
261}
8318d78a 262
b2e1b302
LR
263static bool is_alpha_upper(char letter)
264{
265 /* ASCII A - Z */
266 if (letter >= 65 && letter <= 90)
267 return true;
268 return false;
269}
8318d78a 270
a3d2eaf0 271static bool is_unknown_alpha2(const char *alpha2)
b2e1b302
LR
272{
273 if (!alpha2)
274 return false;
fb1fc7ad
LR
275 /*
276 * Special case where regulatory domain was built by driver
277 * but a specific alpha2 cannot be determined
278 */
b2e1b302
LR
279 if (alpha2[0] == '9' && alpha2[1] == '9')
280 return true;
281 return false;
282}
8318d78a 283
3f2355cb
LR
284static bool is_intersected_alpha2(const char *alpha2)
285{
286 if (!alpha2)
287 return false;
fb1fc7ad
LR
288 /*
289 * Special case where regulatory domain is the
3f2355cb 290 * result of an intersection between two regulatory domain
fb1fc7ad
LR
291 * structures
292 */
3f2355cb
LR
293 if (alpha2[0] == '9' && alpha2[1] == '8')
294 return true;
295 return false;
296}
297
a3d2eaf0 298static bool is_an_alpha2(const char *alpha2)
b2e1b302
LR
299{
300 if (!alpha2)
301 return false;
302 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
303 return true;
304 return false;
305}
8318d78a 306
a3d2eaf0 307static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
b2e1b302
LR
308{
309 if (!alpha2_x || !alpha2_y)
310 return false;
311 if (alpha2_x[0] == alpha2_y[0] &&
312 alpha2_x[1] == alpha2_y[1])
313 return true;
314 return false;
315}
316
69b1572b 317static bool regdom_changes(const char *alpha2)
b2e1b302 318{
761cf7ec
LR
319 assert_cfg80211_lock();
320
b2e1b302
LR
321 if (!cfg80211_regdomain)
322 return true;
323 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
324 return false;
325 return true;
326}
327
3f2355cb
LR
328/**
329 * country_ie_integrity_changes - tells us if the country IE has changed
330 * @checksum: checksum of country IE of fields we are interested in
331 *
332 * If the country IE has not changed you can ignore it safely. This is
333 * useful to determine if two devices are seeing two different country IEs
334 * even on the same alpha2. Note that this will return false if no IE has
335 * been set on the wireless core yet.
336 */
337static bool country_ie_integrity_changes(u32 checksum)
338{
339 /* If no IE has been set then the checksum doesn't change */
340 if (unlikely(!last_request->country_ie_checksum))
341 return false;
342 if (unlikely(last_request->country_ie_checksum != checksum))
343 return true;
344 return false;
345}
346
fb1fc7ad
LR
347/*
348 * This lets us keep regulatory code which is updated on a regulatory
349 * basis in userspace.
350 */
b2e1b302
LR
351static int call_crda(const char *alpha2)
352{
353 char country_env[9 + 2] = "COUNTRY=";
354 char *envp[] = {
355 country_env,
356 NULL
357 };
358
359 if (!is_world_regdom((char *) alpha2))
360 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
361 alpha2[0], alpha2[1]);
362 else
b2e1b302
LR
363 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
364 "regulatory domain\n");
b2e1b302
LR
365
366 country_env[8] = alpha2[0];
367 country_env[9] = alpha2[1];
368
369 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
370}
371
b2e1b302 372/* Used by nl80211 before kmalloc'ing our regulatory domain */
a3d2eaf0 373bool reg_is_valid_request(const char *alpha2)
b2e1b302 374{
f6037d09
JB
375 if (!last_request)
376 return false;
377
378 return alpha2_equal(last_request->alpha2, alpha2);
b2e1b302 379}
8318d78a 380
b2e1b302 381/* Sanity check on a regulatory rule */
a3d2eaf0 382static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
8318d78a 383{
a3d2eaf0 384 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
b2e1b302
LR
385 u32 freq_diff;
386
91e99004 387 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
b2e1b302
LR
388 return false;
389
390 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
391 return false;
392
393 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
394
bd05f28e
RK
395 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
396 freq_range->max_bandwidth_khz > freq_diff)
b2e1b302
LR
397 return false;
398
399 return true;
400}
401
a3d2eaf0 402static bool is_valid_rd(const struct ieee80211_regdomain *rd)
b2e1b302 403{
a3d2eaf0 404 const struct ieee80211_reg_rule *reg_rule = NULL;
b2e1b302 405 unsigned int i;
8318d78a 406
b2e1b302
LR
407 if (!rd->n_reg_rules)
408 return false;
8318d78a 409
88dc1c3f
LR
410 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
411 return false;
412
b2e1b302
LR
413 for (i = 0; i < rd->n_reg_rules; i++) {
414 reg_rule = &rd->reg_rules[i];
415 if (!is_valid_reg_rule(reg_rule))
416 return false;
417 }
418
419 return true;
8318d78a
JB
420}
421
b2e1b302
LR
422/* Returns value in KHz */
423static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
424 u32 freq)
425{
426 unsigned int i;
427 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
428 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
429 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
430 if (start_freq_khz >= freq_range->start_freq_khz &&
431 end_freq_khz <= freq_range->end_freq_khz)
432 return supported_bandwidths[i];
433 }
434 return 0;
435}
8318d78a 436
0c7dc45d
LR
437/**
438 * freq_in_rule_band - tells us if a frequency is in a frequency band
439 * @freq_range: frequency rule we want to query
440 * @freq_khz: frequency we are inquiring about
441 *
442 * This lets us know if a specific frequency rule is or is not relevant to
443 * a specific frequency's band. Bands are device specific and artificial
444 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
445 * safe for now to assume that a frequency rule should not be part of a
446 * frequency's band if the start freq or end freq are off by more than 2 GHz.
447 * This resolution can be lowered and should be considered as we add
448 * regulatory rule support for other "bands".
449 **/
450static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
451 u32 freq_khz)
452{
453#define ONE_GHZ_IN_KHZ 1000000
454 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
455 return true;
456 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
457 return true;
458 return false;
459#undef ONE_GHZ_IN_KHZ
460}
461
fb1fc7ad
LR
462/*
463 * Converts a country IE to a regulatory domain. A regulatory domain
3f2355cb
LR
464 * structure has a lot of information which the IE doesn't yet have,
465 * so for the other values we use upper max values as we will intersect
fb1fc7ad
LR
466 * with our userspace regulatory agent to get lower bounds.
467 */
3f2355cb
LR
468static struct ieee80211_regdomain *country_ie_2_rd(
469 u8 *country_ie,
470 u8 country_ie_len,
471 u32 *checksum)
472{
473 struct ieee80211_regdomain *rd = NULL;
474 unsigned int i = 0;
475 char alpha2[2];
476 u32 flags = 0;
477 u32 num_rules = 0, size_of_regd = 0;
478 u8 *triplets_start = NULL;
479 u8 len_at_triplet = 0;
480 /* the last channel we have registered in a subband (triplet) */
481 int last_sub_max_channel = 0;
482
483 *checksum = 0xDEADBEEF;
484
485 /* Country IE requirements */
486 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
487 country_ie_len & 0x01);
488
489 alpha2[0] = country_ie[0];
490 alpha2[1] = country_ie[1];
491
492 /*
493 * Third octet can be:
494 * 'I' - Indoor
495 * 'O' - Outdoor
496 *
497 * anything else we assume is no restrictions
498 */
499 if (country_ie[2] == 'I')
500 flags = NL80211_RRF_NO_OUTDOOR;
501 else if (country_ie[2] == 'O')
502 flags = NL80211_RRF_NO_INDOOR;
503
504 country_ie += 3;
505 country_ie_len -= 3;
506
507 triplets_start = country_ie;
508 len_at_triplet = country_ie_len;
509
510 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
511
fb1fc7ad
LR
512 /*
513 * We need to build a reg rule for each triplet, but first we must
3f2355cb 514 * calculate the number of reg rules we will need. We will need one
fb1fc7ad
LR
515 * for each channel subband
516 */
3f2355cb 517 while (country_ie_len >= 3) {
615aab4b 518 int end_channel = 0;
3f2355cb
LR
519 struct ieee80211_country_ie_triplet *triplet =
520 (struct ieee80211_country_ie_triplet *) country_ie;
521 int cur_sub_max_channel = 0, cur_channel = 0;
522
523 if (triplet->ext.reg_extension_id >=
524 IEEE80211_COUNTRY_EXTENSION_ID) {
525 country_ie += 3;
526 country_ie_len -= 3;
527 continue;
528 }
529
615aab4b
LR
530 /* 2 GHz */
531 if (triplet->chans.first_channel <= 14)
532 end_channel = triplet->chans.first_channel +
533 triplet->chans.num_channels;
534 else
535 /*
536 * 5 GHz -- For example in country IEs if the first
537 * channel given is 36 and the number of channels is 4
538 * then the individual channel numbers defined for the
539 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
540 * and not 36, 37, 38, 39.
541 *
542 * See: http://tinyurl.com/11d-clarification
543 */
544 end_channel = triplet->chans.first_channel +
545 (4 * (triplet->chans.num_channels - 1));
546
3f2355cb 547 cur_channel = triplet->chans.first_channel;
615aab4b 548 cur_sub_max_channel = end_channel;
3f2355cb
LR
549
550 /* Basic sanity check */
551 if (cur_sub_max_channel < cur_channel)
552 return NULL;
553
fb1fc7ad
LR
554 /*
555 * Do not allow overlapping channels. Also channels
3f2355cb 556 * passed in each subband must be monotonically
fb1fc7ad
LR
557 * increasing
558 */
3f2355cb
LR
559 if (last_sub_max_channel) {
560 if (cur_channel <= last_sub_max_channel)
561 return NULL;
562 if (cur_sub_max_channel <= last_sub_max_channel)
563 return NULL;
564 }
565
fb1fc7ad
LR
566 /*
567 * When dot11RegulatoryClassesRequired is supported
3f2355cb
LR
568 * we can throw ext triplets as part of this soup,
569 * for now we don't care when those change as we
fb1fc7ad
LR
570 * don't support them
571 */
3f2355cb
LR
572 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
573 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
574 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
575
576 last_sub_max_channel = cur_sub_max_channel;
577
578 country_ie += 3;
579 country_ie_len -= 3;
580 num_rules++;
581
fb1fc7ad
LR
582 /*
583 * Note: this is not a IEEE requirement but
584 * simply a memory requirement
585 */
3f2355cb
LR
586 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
587 return NULL;
588 }
589
590 country_ie = triplets_start;
591 country_ie_len = len_at_triplet;
592
593 size_of_regd = sizeof(struct ieee80211_regdomain) +
594 (num_rules * sizeof(struct ieee80211_reg_rule));
595
596 rd = kzalloc(size_of_regd, GFP_KERNEL);
597 if (!rd)
598 return NULL;
599
600 rd->n_reg_rules = num_rules;
601 rd->alpha2[0] = alpha2[0];
602 rd->alpha2[1] = alpha2[1];
603
604 /* This time around we fill in the rd */
605 while (country_ie_len >= 3) {
02e68a3d 606 int end_channel = 0;
3f2355cb
LR
607 struct ieee80211_country_ie_triplet *triplet =
608 (struct ieee80211_country_ie_triplet *) country_ie;
609 struct ieee80211_reg_rule *reg_rule = NULL;
610 struct ieee80211_freq_range *freq_range = NULL;
611 struct ieee80211_power_rule *power_rule = NULL;
612
fb1fc7ad
LR
613 /*
614 * Must parse if dot11RegulatoryClassesRequired is true,
615 * we don't support this yet
616 */
3f2355cb
LR
617 if (triplet->ext.reg_extension_id >=
618 IEEE80211_COUNTRY_EXTENSION_ID) {
619 country_ie += 3;
620 country_ie_len -= 3;
621 continue;
622 }
623
624 reg_rule = &rd->reg_rules[i];
625 freq_range = &reg_rule->freq_range;
626 power_rule = &reg_rule->power_rule;
627
628 reg_rule->flags = flags;
629
02e68a3d
LR
630 /* 2 GHz */
631 if (triplet->chans.first_channel <= 14)
632 end_channel = triplet->chans.first_channel +
633 triplet->chans.num_channels;
634 else
02e68a3d
LR
635 end_channel = triplet->chans.first_channel +
636 (4 * (triplet->chans.num_channels - 1));
637
fb1fc7ad
LR
638 /*
639 * The +10 is since the regulatory domain expects
3f2355cb
LR
640 * the actual band edge, not the center of freq for
641 * its start and end freqs, assuming 20 MHz bandwidth on
fb1fc7ad
LR
642 * the channels passed
643 */
3f2355cb
LR
644 freq_range->start_freq_khz =
645 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
646 triplet->chans.first_channel) - 10);
647 freq_range->end_freq_khz =
648 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
02e68a3d 649 end_channel) + 10);
3f2355cb 650
fb1fc7ad
LR
651 /*
652 * These are large arbitrary values we use to intersect later.
653 * Increment this if we ever support >= 40 MHz channels
654 * in IEEE 802.11
655 */
3f2355cb
LR
656 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
657 power_rule->max_antenna_gain = DBI_TO_MBI(100);
658 power_rule->max_eirp = DBM_TO_MBM(100);
659
660 country_ie += 3;
661 country_ie_len -= 3;
662 i++;
663
664 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
665 }
666
667 return rd;
668}
669
670
fb1fc7ad
LR
671/*
672 * Helper for regdom_intersect(), this does the real
673 * mathematical intersection fun
674 */
9c96477d
LR
675static int reg_rules_intersect(
676 const struct ieee80211_reg_rule *rule1,
677 const struct ieee80211_reg_rule *rule2,
678 struct ieee80211_reg_rule *intersected_rule)
679{
680 const struct ieee80211_freq_range *freq_range1, *freq_range2;
681 struct ieee80211_freq_range *freq_range;
682 const struct ieee80211_power_rule *power_rule1, *power_rule2;
683 struct ieee80211_power_rule *power_rule;
684 u32 freq_diff;
685
686 freq_range1 = &rule1->freq_range;
687 freq_range2 = &rule2->freq_range;
688 freq_range = &intersected_rule->freq_range;
689
690 power_rule1 = &rule1->power_rule;
691 power_rule2 = &rule2->power_rule;
692 power_rule = &intersected_rule->power_rule;
693
694 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
695 freq_range2->start_freq_khz);
696 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
697 freq_range2->end_freq_khz);
698 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
699 freq_range2->max_bandwidth_khz);
700
701 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
702 if (freq_range->max_bandwidth_khz > freq_diff)
703 freq_range->max_bandwidth_khz = freq_diff;
704
705 power_rule->max_eirp = min(power_rule1->max_eirp,
706 power_rule2->max_eirp);
707 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
708 power_rule2->max_antenna_gain);
709
710 intersected_rule->flags = (rule1->flags | rule2->flags);
711
712 if (!is_valid_reg_rule(intersected_rule))
713 return -EINVAL;
714
715 return 0;
716}
717
718/**
719 * regdom_intersect - do the intersection between two regulatory domains
720 * @rd1: first regulatory domain
721 * @rd2: second regulatory domain
722 *
723 * Use this function to get the intersection between two regulatory domains.
724 * Once completed we will mark the alpha2 for the rd as intersected, "98",
725 * as no one single alpha2 can represent this regulatory domain.
726 *
727 * Returns a pointer to the regulatory domain structure which will hold the
728 * resulting intersection of rules between rd1 and rd2. We will
729 * kzalloc() this structure for you.
730 */
731static struct ieee80211_regdomain *regdom_intersect(
732 const struct ieee80211_regdomain *rd1,
733 const struct ieee80211_regdomain *rd2)
734{
735 int r, size_of_regd;
736 unsigned int x, y;
737 unsigned int num_rules = 0, rule_idx = 0;
738 const struct ieee80211_reg_rule *rule1, *rule2;
739 struct ieee80211_reg_rule *intersected_rule;
740 struct ieee80211_regdomain *rd;
741 /* This is just a dummy holder to help us count */
742 struct ieee80211_reg_rule irule;
743
744 /* Uses the stack temporarily for counter arithmetic */
745 intersected_rule = &irule;
746
747 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
748
749 if (!rd1 || !rd2)
750 return NULL;
751
fb1fc7ad
LR
752 /*
753 * First we get a count of the rules we'll need, then we actually
9c96477d
LR
754 * build them. This is to so we can malloc() and free() a
755 * regdomain once. The reason we use reg_rules_intersect() here
756 * is it will return -EINVAL if the rule computed makes no sense.
fb1fc7ad
LR
757 * All rules that do check out OK are valid.
758 */
9c96477d
LR
759
760 for (x = 0; x < rd1->n_reg_rules; x++) {
761 rule1 = &rd1->reg_rules[x];
762 for (y = 0; y < rd2->n_reg_rules; y++) {
763 rule2 = &rd2->reg_rules[y];
764 if (!reg_rules_intersect(rule1, rule2,
765 intersected_rule))
766 num_rules++;
767 memset(intersected_rule, 0,
768 sizeof(struct ieee80211_reg_rule));
769 }
770 }
771
772 if (!num_rules)
773 return NULL;
774
775 size_of_regd = sizeof(struct ieee80211_regdomain) +
776 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
777
778 rd = kzalloc(size_of_regd, GFP_KERNEL);
779 if (!rd)
780 return NULL;
781
782 for (x = 0; x < rd1->n_reg_rules; x++) {
783 rule1 = &rd1->reg_rules[x];
784 for (y = 0; y < rd2->n_reg_rules; y++) {
785 rule2 = &rd2->reg_rules[y];
fb1fc7ad
LR
786 /*
787 * This time around instead of using the stack lets
9c96477d 788 * write to the target rule directly saving ourselves
fb1fc7ad
LR
789 * a memcpy()
790 */
9c96477d
LR
791 intersected_rule = &rd->reg_rules[rule_idx];
792 r = reg_rules_intersect(rule1, rule2,
793 intersected_rule);
fb1fc7ad
LR
794 /*
795 * No need to memset here the intersected rule here as
796 * we're not using the stack anymore
797 */
9c96477d
LR
798 if (r)
799 continue;
800 rule_idx++;
801 }
802 }
803
804 if (rule_idx != num_rules) {
805 kfree(rd);
806 return NULL;
807 }
808
809 rd->n_reg_rules = num_rules;
810 rd->alpha2[0] = '9';
811 rd->alpha2[1] = '8';
812
813 return rd;
814}
815
fb1fc7ad
LR
816/*
817 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
818 * want to just have the channel structure use these
819 */
b2e1b302
LR
820static u32 map_regdom_flags(u32 rd_flags)
821{
822 u32 channel_flags = 0;
823 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
824 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
825 if (rd_flags & NL80211_RRF_NO_IBSS)
826 channel_flags |= IEEE80211_CHAN_NO_IBSS;
827 if (rd_flags & NL80211_RRF_DFS)
828 channel_flags |= IEEE80211_CHAN_RADAR;
829 return channel_flags;
830}
831
1fa25e41
LR
832static int freq_reg_info_regd(struct wiphy *wiphy,
833 u32 center_freq,
834 u32 *bandwidth,
835 const struct ieee80211_reg_rule **reg_rule,
836 const struct ieee80211_regdomain *custom_regd)
8318d78a
JB
837{
838 int i;
0c7dc45d 839 bool band_rule_found = false;
3e0c3ff3 840 const struct ieee80211_regdomain *regd;
b2e1b302 841 u32 max_bandwidth = 0;
8318d78a 842
1fa25e41 843 regd = custom_regd ? custom_regd : cfg80211_regdomain;
3e0c3ff3 844
fb1fc7ad
LR
845 /*
846 * Follow the driver's regulatory domain, if present, unless a country
847 * IE has been processed or a user wants to help complaince further
848 */
3e0c3ff3 849 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
24ed1da1 850 last_request->initiator != REGDOM_SET_BY_USER &&
3e0c3ff3
LR
851 wiphy->regd)
852 regd = wiphy->regd;
853
854 if (!regd)
b2e1b302
LR
855 return -EINVAL;
856
3e0c3ff3 857 for (i = 0; i < regd->n_reg_rules; i++) {
b2e1b302
LR
858 const struct ieee80211_reg_rule *rr;
859 const struct ieee80211_freq_range *fr = NULL;
860 const struct ieee80211_power_rule *pr = NULL;
861
3e0c3ff3 862 rr = &regd->reg_rules[i];
b2e1b302
LR
863 fr = &rr->freq_range;
864 pr = &rr->power_rule;
0c7dc45d 865
fb1fc7ad
LR
866 /*
867 * We only need to know if one frequency rule was
0c7dc45d 868 * was in center_freq's band, that's enough, so lets
fb1fc7ad
LR
869 * not overwrite it once found
870 */
0c7dc45d
LR
871 if (!band_rule_found)
872 band_rule_found = freq_in_rule_band(fr, center_freq);
873
b2e1b302 874 max_bandwidth = freq_max_bandwidth(fr, center_freq);
0c7dc45d 875
b2e1b302
LR
876 if (max_bandwidth && *bandwidth <= max_bandwidth) {
877 *reg_rule = rr;
878 *bandwidth = max_bandwidth;
8318d78a
JB
879 break;
880 }
881 }
882
0c7dc45d
LR
883 if (!band_rule_found)
884 return -ERANGE;
885
b2e1b302
LR
886 return !max_bandwidth;
887}
34f57347 888EXPORT_SYMBOL(freq_reg_info);
b2e1b302 889
34f57347 890int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
1fa25e41
LR
891 const struct ieee80211_reg_rule **reg_rule)
892{
893 return freq_reg_info_regd(wiphy, center_freq,
894 bandwidth, reg_rule, NULL);
895}
b2e1b302 896
a92a3ce7
LR
897static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
898 unsigned int chan_idx)
b2e1b302
LR
899{
900 int r;
a92a3ce7 901 u32 flags;
b2e1b302
LR
902 u32 max_bandwidth = 0;
903 const struct ieee80211_reg_rule *reg_rule = NULL;
904 const struct ieee80211_power_rule *power_rule = NULL;
a92a3ce7
LR
905 struct ieee80211_supported_band *sband;
906 struct ieee80211_channel *chan;
fe33eb39 907 struct wiphy *request_wiphy = NULL;
a92a3ce7 908
761cf7ec
LR
909 assert_cfg80211_lock();
910
806a9e39
LR
911 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
912
a92a3ce7
LR
913 sband = wiphy->bands[band];
914 BUG_ON(chan_idx >= sband->n_channels);
915 chan = &sband->channels[chan_idx];
916
917 flags = chan->orig_flags;
b2e1b302 918
3e0c3ff3 919 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
b2e1b302
LR
920 &max_bandwidth, &reg_rule);
921
922 if (r) {
fb1fc7ad
LR
923 /*
924 * This means no regulatory rule was found in the country IE
0c7dc45d
LR
925 * with a frequency range on the center_freq's band, since
926 * IEEE-802.11 allows for a country IE to have a subset of the
927 * regulatory information provided in a country we ignore
928 * disabling the channel unless at least one reg rule was
929 * found on the center_freq's band. For details see this
930 * clarification:
931 *
932 * http://tinyurl.com/11d-clarification
933 */
934 if (r == -ERANGE &&
935 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
936#ifdef CONFIG_CFG80211_REG_DEBUG
937 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
938 "intact on %s - no rule found in band on "
939 "Country IE\n",
940 chan->center_freq, wiphy_name(wiphy));
941#endif
942 } else {
fb1fc7ad
LR
943 /*
944 * In this case we know the country IE has at least one reg rule
945 * for the band so we respect its band definitions
946 */
0c7dc45d
LR
947#ifdef CONFIG_CFG80211_REG_DEBUG
948 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
949 printk(KERN_DEBUG "cfg80211: Disabling "
950 "channel %d MHz on %s due to "
951 "Country IE\n",
952 chan->center_freq, wiphy_name(wiphy));
953#endif
954 flags |= IEEE80211_CHAN_DISABLED;
955 chan->flags = flags;
956 }
8318d78a
JB
957 return;
958 }
959
b2e1b302
LR
960 power_rule = &reg_rule->power_rule;
961
f976376d 962 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
806a9e39
LR
963 request_wiphy && request_wiphy == wiphy &&
964 request_wiphy->strict_regulatory) {
fb1fc7ad
LR
965 /*
966 * This gaurantees the driver's requested regulatory domain
f976376d 967 * will always be used as a base for further regulatory
fb1fc7ad
LR
968 * settings
969 */
f976376d
LR
970 chan->flags = chan->orig_flags =
971 map_regdom_flags(reg_rule->flags);
972 chan->max_antenna_gain = chan->orig_mag =
973 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
974 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
975 chan->max_power = chan->orig_mpwr =
976 (int) MBM_TO_DBM(power_rule->max_eirp);
977 return;
978 }
979
b2e1b302 980 chan->flags = flags | map_regdom_flags(reg_rule->flags);
8318d78a 981 chan->max_antenna_gain = min(chan->orig_mag,
b2e1b302
LR
982 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
983 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
253898c4 984 if (chan->orig_mpwr)
b2e1b302
LR
985 chan->max_power = min(chan->orig_mpwr,
986 (int) MBM_TO_DBM(power_rule->max_eirp));
253898c4 987 else
b2e1b302 988 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
8318d78a
JB
989}
990
a92a3ce7 991static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
8318d78a 992{
a92a3ce7
LR
993 unsigned int i;
994 struct ieee80211_supported_band *sband;
995
996 BUG_ON(!wiphy->bands[band]);
997 sband = wiphy->bands[band];
8318d78a
JB
998
999 for (i = 0; i < sband->n_channels; i++)
a92a3ce7 1000 handle_channel(wiphy, band, i);
8318d78a
JB
1001}
1002
14b9815a
LR
1003static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
1004{
1005 if (!last_request)
1006 return true;
1007 if (setby == REGDOM_SET_BY_CORE &&
2a44f911 1008 wiphy->custom_regulatory)
14b9815a 1009 return true;
fb1fc7ad
LR
1010 /*
1011 * wiphy->regd will be set once the device has its own
1012 * desired regulatory domain set
1013 */
f976376d
LR
1014 if (wiphy->strict_regulatory && !wiphy->regd &&
1015 !is_world_regdom(last_request->alpha2))
14b9815a
LR
1016 return true;
1017 return false;
1018}
1019
b2e1b302 1020static void update_all_wiphy_regulatory(enum reg_set_by setby)
8318d78a 1021{
b2e1b302 1022 struct cfg80211_registered_device *drv;
8318d78a 1023
b2e1b302 1024 list_for_each_entry(drv, &cfg80211_drv_list, list)
d46e5b1d 1025 wiphy_update_regulatory(&drv->wiphy, setby);
b2e1b302
LR
1026}
1027
e38f8a7a
LR
1028static void handle_reg_beacon(struct wiphy *wiphy,
1029 unsigned int chan_idx,
1030 struct reg_beacon *reg_beacon)
1031{
1032#ifdef CONFIG_CFG80211_REG_DEBUG
1033#define REG_DEBUG_BEACON_FLAG(desc) \
1034 printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \
1035 "frequency: %d MHz (Ch %d) on %s\n", \
1036 reg_beacon->chan.center_freq, \
1037 ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \
1038 wiphy_name(wiphy));
1039#else
1040#define REG_DEBUG_BEACON_FLAG(desc) do {} while (0)
1041#endif
1042 struct ieee80211_supported_band *sband;
1043 struct ieee80211_channel *chan;
1044
1045 assert_cfg80211_lock();
1046
1047 sband = wiphy->bands[reg_beacon->chan.band];
1048 chan = &sband->channels[chan_idx];
1049
1050 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1051 return;
1052
1053 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1054 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1055 REG_DEBUG_BEACON_FLAG("active scanning");
1056 }
1057
1058 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1059 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1060 REG_DEBUG_BEACON_FLAG("beaconing");
1061 }
1062
1063 chan->beacon_found = true;
1064#undef REG_DEBUG_BEACON_FLAG
1065}
1066
1067/*
1068 * Called when a scan on a wiphy finds a beacon on
1069 * new channel
1070 */
1071static void wiphy_update_new_beacon(struct wiphy *wiphy,
1072 struct reg_beacon *reg_beacon)
1073{
1074 unsigned int i;
1075 struct ieee80211_supported_band *sband;
1076
1077 assert_cfg80211_lock();
1078
1079 if (!wiphy->bands[reg_beacon->chan.band])
1080 return;
1081
1082 sband = wiphy->bands[reg_beacon->chan.band];
1083
1084 for (i = 0; i < sband->n_channels; i++)
1085 handle_reg_beacon(wiphy, i, reg_beacon);
1086}
1087
1088/*
1089 * Called upon reg changes or a new wiphy is added
1090 */
1091static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1092{
1093 unsigned int i;
1094 struct ieee80211_supported_band *sband;
1095 struct reg_beacon *reg_beacon;
1096
1097 assert_cfg80211_lock();
1098
1099 if (list_empty(&reg_beacon_list))
1100 return;
1101
1102 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1103 if (!wiphy->bands[reg_beacon->chan.band])
1104 continue;
1105 sband = wiphy->bands[reg_beacon->chan.band];
1106 for (i = 0; i < sband->n_channels; i++)
1107 handle_reg_beacon(wiphy, i, reg_beacon);
1108 }
1109}
1110
1111static bool reg_is_world_roaming(struct wiphy *wiphy)
1112{
1113 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1114 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1115 return true;
1116 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
1117 wiphy->custom_regulatory)
1118 return true;
1119 return false;
1120}
1121
1122/* Reap the advantages of previously found beacons */
1123static void reg_process_beacons(struct wiphy *wiphy)
1124{
1125 if (!reg_is_world_roaming(wiphy))
1126 return;
1127 wiphy_update_beacon_reg(wiphy);
1128}
1129
b2e1b302
LR
1130void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
1131{
1132 enum ieee80211_band band;
d46e5b1d
LR
1133
1134 if (ignore_reg_update(wiphy, setby))
e38f8a7a 1135 goto out;
b2e1b302 1136 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
8318d78a 1137 if (wiphy->bands[band])
a92a3ce7 1138 handle_band(wiphy, band);
b2e1b302 1139 }
e38f8a7a
LR
1140out:
1141 reg_process_beacons(wiphy);
560e28e1 1142 if (wiphy->reg_notifier)
716f9392 1143 wiphy->reg_notifier(wiphy, last_request);
b2e1b302
LR
1144}
1145
1fa25e41
LR
1146static void handle_channel_custom(struct wiphy *wiphy,
1147 enum ieee80211_band band,
1148 unsigned int chan_idx,
1149 const struct ieee80211_regdomain *regd)
1150{
1151 int r;
1152 u32 max_bandwidth = 0;
1153 const struct ieee80211_reg_rule *reg_rule = NULL;
1154 const struct ieee80211_power_rule *power_rule = NULL;
1155 struct ieee80211_supported_band *sband;
1156 struct ieee80211_channel *chan;
1157
1158 sband = wiphy->bands[band];
1159 BUG_ON(chan_idx >= sband->n_channels);
1160 chan = &sband->channels[chan_idx];
1161
1162 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
1163 &max_bandwidth, &reg_rule, regd);
1164
1165 if (r) {
1166 chan->flags = IEEE80211_CHAN_DISABLED;
1167 return;
1168 }
1169
1170 power_rule = &reg_rule->power_rule;
1171
1172 chan->flags |= map_regdom_flags(reg_rule->flags);
1173 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1174 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1175 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1176}
1177
1178static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1179 const struct ieee80211_regdomain *regd)
1180{
1181 unsigned int i;
1182 struct ieee80211_supported_band *sband;
1183
1184 BUG_ON(!wiphy->bands[band]);
1185 sband = wiphy->bands[band];
1186
1187 for (i = 0; i < sband->n_channels; i++)
1188 handle_channel_custom(wiphy, band, i, regd);
1189}
1190
1191/* Used by drivers prior to wiphy registration */
1192void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1193 const struct ieee80211_regdomain *regd)
1194{
1195 enum ieee80211_band band;
1196 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1197 if (wiphy->bands[band])
1198 handle_band_custom(wiphy, band, regd);
b2e1b302
LR
1199 }
1200}
1fa25e41
LR
1201EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1202
3e0c3ff3
LR
1203static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1204 const struct ieee80211_regdomain *src_regd)
1205{
1206 struct ieee80211_regdomain *regd;
1207 int size_of_regd = 0;
1208 unsigned int i;
1209
1210 size_of_regd = sizeof(struct ieee80211_regdomain) +
1211 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1212
1213 regd = kzalloc(size_of_regd, GFP_KERNEL);
1214 if (!regd)
1215 return -ENOMEM;
1216
1217 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1218
1219 for (i = 0; i < src_regd->n_reg_rules; i++)
1220 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1221 sizeof(struct ieee80211_reg_rule));
1222
1223 *dst_regd = regd;
1224 return 0;
1225}
b2e1b302 1226
fb1fc7ad
LR
1227/*
1228 * Return value which can be used by ignore_request() to indicate
1229 * it has been determined we should intersect two regulatory domains
1230 */
9c96477d
LR
1231#define REG_INTERSECT 1
1232
84fa4f43
JB
1233/* This has the logic which determines when a new request
1234 * should be ignored. */
2f92cd2e
LR
1235static int ignore_request(struct wiphy *wiphy,
1236 struct regulatory_request *pending_request)
84fa4f43 1237{
806a9e39 1238 struct wiphy *last_wiphy = NULL;
761cf7ec
LR
1239
1240 assert_cfg80211_lock();
1241
84fa4f43
JB
1242 /* All initial requests are respected */
1243 if (!last_request)
1244 return 0;
1245
2f92cd2e 1246 switch (pending_request->initiator) {
84fa4f43
JB
1247 case REGDOM_SET_BY_INIT:
1248 return -EINVAL;
1249 case REGDOM_SET_BY_CORE:
ba25c141 1250 return -EINVAL;
84fa4f43 1251 case REGDOM_SET_BY_COUNTRY_IE:
806a9e39
LR
1252
1253 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1254
2f92cd2e 1255 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
84fa4f43
JB
1256 return -EINVAL;
1257 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
806a9e39 1258 if (last_wiphy != wiphy) {
84fa4f43
JB
1259 /*
1260 * Two cards with two APs claiming different
1261 * different Country IE alpha2s. We could
1262 * intersect them, but that seems unlikely
1263 * to be correct. Reject second one for now.
1264 */
2f92cd2e 1265 if (regdom_changes(pending_request->alpha2))
84fa4f43
JB
1266 return -EOPNOTSUPP;
1267 return -EALREADY;
1268 }
fb1fc7ad
LR
1269 /*
1270 * Two consecutive Country IE hints on the same wiphy.
1271 * This should be picked up early by the driver/stack
1272 */
2f92cd2e 1273 if (WARN_ON(regdom_changes(pending_request->alpha2)))
84fa4f43
JB
1274 return 0;
1275 return -EALREADY;
1276 }
3f2355cb 1277 return REG_INTERSECT;
84fa4f43 1278 case REGDOM_SET_BY_DRIVER:
e74b1e7f
LR
1279 if (last_request->initiator == REGDOM_SET_BY_CORE) {
1280 if (is_old_static_regdom(cfg80211_regdomain))
1281 return 0;
2f92cd2e 1282 if (regdom_changes(pending_request->alpha2))
e74b1e7f 1283 return 0;
84fa4f43 1284 return -EALREADY;
e74b1e7f 1285 }
fff32c04
LR
1286
1287 /*
1288 * This would happen if you unplug and plug your card
1289 * back in or if you add a new device for which the previously
1290 * loaded card also agrees on the regulatory domain.
1291 */
1292 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
2f92cd2e 1293 !regdom_changes(pending_request->alpha2))
fff32c04
LR
1294 return -EALREADY;
1295
3e0c3ff3 1296 return REG_INTERSECT;
84fa4f43 1297 case REGDOM_SET_BY_USER:
84fa4f43 1298 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
9c96477d 1299 return REG_INTERSECT;
fb1fc7ad
LR
1300 /*
1301 * If the user knows better the user should set the regdom
1302 * to their country before the IE is picked up
1303 */
3f2355cb
LR
1304 if (last_request->initiator == REGDOM_SET_BY_USER &&
1305 last_request->intersect)
1306 return -EOPNOTSUPP;
fb1fc7ad
LR
1307 /*
1308 * Process user requests only after previous user/driver/core
1309 * requests have been processed
1310 */
5eebade6
LR
1311 if (last_request->initiator == REGDOM_SET_BY_CORE ||
1312 last_request->initiator == REGDOM_SET_BY_DRIVER ||
1313 last_request->initiator == REGDOM_SET_BY_USER) {
69b1572b 1314 if (regdom_changes(last_request->alpha2))
5eebade6
LR
1315 return -EAGAIN;
1316 }
1317
e74b1e7f 1318 if (!is_old_static_regdom(cfg80211_regdomain) &&
2f92cd2e 1319 !regdom_changes(pending_request->alpha2))
e74b1e7f
LR
1320 return -EALREADY;
1321
84fa4f43
JB
1322 return 0;
1323 }
1324
1325 return -EINVAL;
1326}
1327
d1c96a9a
LR
1328/**
1329 * __regulatory_hint - hint to the wireless core a regulatory domain
1330 * @wiphy: if the hint comes from country information from an AP, this
1331 * is required to be set to the wiphy that received the information
28da32d7 1332 * @pending_request: the regulatory request currently being processed
d1c96a9a
LR
1333 *
1334 * The Wireless subsystem can use this function to hint to the wireless core
28da32d7 1335 * what it believes should be the current regulatory domain.
d1c96a9a
LR
1336 *
1337 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1338 * already been set or other standard error codes.
1339 *
1340 * Caller must hold &cfg80211_mutex
1341 */
28da32d7
LR
1342static int __regulatory_hint(struct wiphy *wiphy,
1343 struct regulatory_request *pending_request)
b2e1b302 1344{
9c96477d 1345 bool intersect = false;
b2e1b302
LR
1346 int r = 0;
1347
761cf7ec
LR
1348 assert_cfg80211_lock();
1349
2f92cd2e 1350 r = ignore_request(wiphy, pending_request);
9c96477d 1351
3e0c3ff3 1352 if (r == REG_INTERSECT) {
28da32d7 1353 if (pending_request->initiator == REGDOM_SET_BY_DRIVER) {
3e0c3ff3 1354 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
d951c1dd
LR
1355 if (r) {
1356 kfree(pending_request);
3e0c3ff3 1357 return r;
d951c1dd 1358 }
3e0c3ff3 1359 }
9c96477d 1360 intersect = true;
3e0c3ff3 1361 } else if (r) {
fb1fc7ad
LR
1362 /*
1363 * If the regulatory domain being requested by the
3e0c3ff3 1364 * driver has already been set just copy it to the
fb1fc7ad
LR
1365 * wiphy
1366 */
28da32d7
LR
1367 if (r == -EALREADY &&
1368 pending_request->initiator == REGDOM_SET_BY_DRIVER) {
3e0c3ff3 1369 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
d951c1dd
LR
1370 if (r) {
1371 kfree(pending_request);
3e0c3ff3 1372 return r;
d951c1dd 1373 }
3e0c3ff3
LR
1374 r = -EALREADY;
1375 goto new_request;
1376 }
d951c1dd 1377 kfree(pending_request);
b2e1b302 1378 return r;
3e0c3ff3 1379 }
b2e1b302 1380
3e0c3ff3 1381new_request:
d951c1dd 1382 kfree(last_request);
5203cdb6 1383
d951c1dd
LR
1384 last_request = pending_request;
1385 last_request->intersect = intersect;
5203cdb6 1386
d951c1dd 1387 pending_request = NULL;
3e0c3ff3
LR
1388
1389 /* When r == REG_INTERSECT we do need to call CRDA */
1390 if (r < 0)
1391 return r;
1392
3f2355cb
LR
1393 /*
1394 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1395 * AND if CRDA is NOT present nothing will happen, if someone
1396 * wants to bother with 11d with OLD_REG you can add a timer.
1397 * If after x amount of time nothing happens you can call:
1398 *
1399 * return set_regdom(country_ie_regdomain);
1400 *
1401 * to intersect with the static rd
1402 */
d951c1dd 1403 return call_crda(last_request->alpha2);
b2e1b302
LR
1404}
1405
fe33eb39 1406/* This currently only processes user and driver regulatory hints */
d951c1dd 1407static void reg_process_hint(struct regulatory_request *reg_request)
fe33eb39
LR
1408{
1409 int r = 0;
1410 struct wiphy *wiphy = NULL;
1411
1412 BUG_ON(!reg_request->alpha2);
1413
1414 mutex_lock(&cfg80211_mutex);
1415
1416 if (wiphy_idx_valid(reg_request->wiphy_idx))
1417 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1418
1419 if (reg_request->initiator == REGDOM_SET_BY_DRIVER &&
1420 !wiphy) {
d951c1dd 1421 kfree(reg_request);
fe33eb39
LR
1422 goto out;
1423 }
1424
28da32d7 1425 r = __regulatory_hint(wiphy, reg_request);
fe33eb39
LR
1426 /* This is required so that the orig_* parameters are saved */
1427 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1428 wiphy_update_regulatory(wiphy, reg_request->initiator);
1429out:
1430 mutex_unlock(&cfg80211_mutex);
fe33eb39
LR
1431}
1432
e38f8a7a 1433/* Processes regulatory hints, this is all the REGDOM_SET_BY_* */
fe33eb39
LR
1434static void reg_process_pending_hints(void)
1435 {
1436 struct regulatory_request *reg_request;
fe33eb39
LR
1437
1438 spin_lock(&reg_requests_lock);
1439 while (!list_empty(&reg_requests_list)) {
1440 reg_request = list_first_entry(&reg_requests_list,
1441 struct regulatory_request,
1442 list);
1443 list_del_init(&reg_request->list);
fe33eb39 1444
d951c1dd
LR
1445 spin_unlock(&reg_requests_lock);
1446 reg_process_hint(reg_request);
fe33eb39
LR
1447 spin_lock(&reg_requests_lock);
1448 }
1449 spin_unlock(&reg_requests_lock);
1450}
1451
e38f8a7a
LR
1452/* Processes beacon hints -- this has nothing to do with country IEs */
1453static void reg_process_pending_beacon_hints(void)
1454{
1455 struct cfg80211_registered_device *drv;
1456 struct reg_beacon *pending_beacon, *tmp;
1457
1458 mutex_lock(&cfg80211_mutex);
1459
1460 /* This goes through the _pending_ beacon list */
1461 spin_lock_bh(&reg_pending_beacons_lock);
1462
1463 if (list_empty(&reg_pending_beacons)) {
1464 spin_unlock_bh(&reg_pending_beacons_lock);
1465 goto out;
1466 }
1467
1468 list_for_each_entry_safe(pending_beacon, tmp,
1469 &reg_pending_beacons, list) {
1470
1471 list_del_init(&pending_beacon->list);
1472
1473 /* Applies the beacon hint to current wiphys */
1474 list_for_each_entry(drv, &cfg80211_drv_list, list)
1475 wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
1476
1477 /* Remembers the beacon hint for new wiphys or reg changes */
1478 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1479 }
1480
1481 spin_unlock_bh(&reg_pending_beacons_lock);
1482out:
1483 mutex_unlock(&cfg80211_mutex);
1484}
1485
fe33eb39
LR
1486static void reg_todo(struct work_struct *work)
1487{
1488 reg_process_pending_hints();
e38f8a7a 1489 reg_process_pending_beacon_hints();
fe33eb39
LR
1490}
1491
1492static DECLARE_WORK(reg_work, reg_todo);
1493
1494static void queue_regulatory_request(struct regulatory_request *request)
1495{
1496 spin_lock(&reg_requests_lock);
1497 list_add_tail(&request->list, &reg_requests_list);
1498 spin_unlock(&reg_requests_lock);
1499
1500 schedule_work(&reg_work);
1501}
1502
1503/* Core regulatory hint -- happens once during cfg80211_init() */
ba25c141
LR
1504static int regulatory_hint_core(const char *alpha2)
1505{
1506 struct regulatory_request *request;
1507
1508 BUG_ON(last_request);
1509
1510 request = kzalloc(sizeof(struct regulatory_request),
1511 GFP_KERNEL);
1512 if (!request)
1513 return -ENOMEM;
1514
1515 request->alpha2[0] = alpha2[0];
1516 request->alpha2[1] = alpha2[1];
1517 request->initiator = REGDOM_SET_BY_CORE;
1518
fe33eb39 1519 queue_regulatory_request(request);
ba25c141 1520
fe33eb39 1521 return 0;
ba25c141
LR
1522}
1523
fe33eb39
LR
1524/* User hints */
1525int regulatory_hint_user(const char *alpha2)
b2e1b302 1526{
fe33eb39
LR
1527 struct regulatory_request *request;
1528
be3d4810 1529 BUG_ON(!alpha2);
b2e1b302 1530
fe33eb39
LR
1531 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1532 if (!request)
1533 return -ENOMEM;
1534
1535 request->wiphy_idx = WIPHY_IDX_STALE;
1536 request->alpha2[0] = alpha2[0];
1537 request->alpha2[1] = alpha2[1];
1538 request->initiator = REGDOM_SET_BY_USER,
1539
1540 queue_regulatory_request(request);
1541
1542 return 0;
1543}
1544
1545/* Driver hints */
1546int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1547{
1548 struct regulatory_request *request;
1549
1550 BUG_ON(!alpha2);
1551 BUG_ON(!wiphy);
1552
1553 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1554 if (!request)
1555 return -ENOMEM;
1556
1557 request->wiphy_idx = get_wiphy_idx(wiphy);
1558
1559 /* Must have registered wiphy first */
1560 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1561
1562 request->alpha2[0] = alpha2[0];
1563 request->alpha2[1] = alpha2[1];
1564 request->initiator = REGDOM_SET_BY_DRIVER;
1565
1566 queue_regulatory_request(request);
1567
1568 return 0;
b2e1b302
LR
1569}
1570EXPORT_SYMBOL(regulatory_hint);
1571
3f2355cb
LR
1572static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1573 u32 country_ie_checksum)
1574{
806a9e39
LR
1575 struct wiphy *request_wiphy;
1576
761cf7ec
LR
1577 assert_cfg80211_lock();
1578
806a9e39
LR
1579 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1580
1581 if (!request_wiphy)
3f2355cb 1582 return false;
806a9e39
LR
1583
1584 if (likely(request_wiphy != wiphy))
3f2355cb 1585 return !country_ie_integrity_changes(country_ie_checksum);
fb1fc7ad
LR
1586 /*
1587 * We should not have let these through at this point, they
3f2355cb 1588 * should have been picked up earlier by the first alpha2 check
fb1fc7ad
LR
1589 * on the device
1590 */
3f2355cb
LR
1591 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1592 return true;
1593 return false;
1594}
1595
1596void regulatory_hint_11d(struct wiphy *wiphy,
1597 u8 *country_ie,
1598 u8 country_ie_len)
1599{
1600 struct ieee80211_regdomain *rd = NULL;
1601 char alpha2[2];
1602 u32 checksum = 0;
1603 enum environment_cap env = ENVIRON_ANY;
fe33eb39 1604 struct regulatory_request *request;
3f2355cb 1605
a1794390 1606 mutex_lock(&cfg80211_mutex);
3f2355cb 1607
d335fe63
LR
1608 if (unlikely(!last_request)) {
1609 mutex_unlock(&cfg80211_mutex);
1610 return;
1611 }
1612
3f2355cb
LR
1613 /* IE len must be evenly divisible by 2 */
1614 if (country_ie_len & 0x01)
1615 goto out;
1616
1617 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1618 goto out;
1619
fb1fc7ad
LR
1620 /*
1621 * Pending country IE processing, this can happen after we
3f2355cb 1622 * call CRDA and wait for a response if a beacon was received before
fb1fc7ad
LR
1623 * we were able to process the last regulatory_hint_11d() call
1624 */
3f2355cb
LR
1625 if (country_ie_regdomain)
1626 goto out;
1627
1628 alpha2[0] = country_ie[0];
1629 alpha2[1] = country_ie[1];
1630
1631 if (country_ie[2] == 'I')
1632 env = ENVIRON_INDOOR;
1633 else if (country_ie[2] == 'O')
1634 env = ENVIRON_OUTDOOR;
1635
fb1fc7ad
LR
1636 /*
1637 * We will run this for *every* beacon processed for the BSSID, so
3f2355cb 1638 * we optimize an early check to exit out early if we don't have to
fb1fc7ad
LR
1639 * do anything
1640 */
806a9e39 1641 if (likely(wiphy_idx_valid(last_request->wiphy_idx))) {
3f2355cb
LR
1642 struct cfg80211_registered_device *drv_last_ie;
1643
806a9e39
LR
1644 drv_last_ie =
1645 cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
3f2355cb 1646
fb1fc7ad
LR
1647 /*
1648 * Lets keep this simple -- we trust the first AP
1649 * after we intersect with CRDA
1650 */
806a9e39 1651 if (likely(&drv_last_ie->wiphy == wiphy)) {
fb1fc7ad
LR
1652 /*
1653 * Ignore IEs coming in on this wiphy with
1654 * the same alpha2 and environment cap
1655 */
3f2355cb
LR
1656 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1657 alpha2) &&
1658 env == drv_last_ie->env)) {
1659 goto out;
1660 }
fb1fc7ad
LR
1661 /*
1662 * the wiphy moved on to another BSSID or the AP
3f2355cb
LR
1663 * was reconfigured. XXX: We need to deal with the
1664 * case where the user suspends and goes to goes
1665 * to another country, and then gets IEs from an
fb1fc7ad
LR
1666 * AP with different settings
1667 */
3f2355cb
LR
1668 goto out;
1669 } else {
fb1fc7ad
LR
1670 /*
1671 * Ignore IEs coming in on two separate wiphys with
1672 * the same alpha2 and environment cap
1673 */
3f2355cb
LR
1674 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1675 alpha2) &&
1676 env == drv_last_ie->env)) {
1677 goto out;
1678 }
1679 /* We could potentially intersect though */
1680 goto out;
1681 }
1682 }
1683
1684 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1685 if (!rd)
1686 goto out;
1687
915278e0
LR
1688 /*
1689 * This will not happen right now but we leave it here for the
3f2355cb
LR
1690 * the future when we want to add suspend/resume support and having
1691 * the user move to another country after doing so, or having the user
915278e0
LR
1692 * move to another AP. Right now we just trust the first AP.
1693 *
1694 * If we hit this before we add this support we want to be informed of
1695 * it as it would indicate a mistake in the current design
1696 */
1697 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
0441d6ff 1698 goto free_rd_out;
3f2355cb 1699
fe33eb39
LR
1700 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1701 if (!request)
1702 goto free_rd_out;
1703
fb1fc7ad
LR
1704 /*
1705 * We keep this around for when CRDA comes back with a response so
1706 * we can intersect with that
1707 */
3f2355cb
LR
1708 country_ie_regdomain = rd;
1709
fe33eb39
LR
1710 request->wiphy_idx = get_wiphy_idx(wiphy);
1711 request->alpha2[0] = rd->alpha2[0];
1712 request->alpha2[1] = rd->alpha2[1];
1713 request->initiator = REGDOM_SET_BY_COUNTRY_IE;
1714 request->country_ie_checksum = checksum;
1715 request->country_ie_env = env;
1716
1717 mutex_unlock(&cfg80211_mutex);
3f2355cb 1718
fe33eb39
LR
1719 queue_regulatory_request(request);
1720
1721 return;
0441d6ff
LR
1722
1723free_rd_out:
1724 kfree(rd);
3f2355cb 1725out:
a1794390 1726 mutex_unlock(&cfg80211_mutex);
3f2355cb
LR
1727}
1728EXPORT_SYMBOL(regulatory_hint_11d);
b2e1b302 1729
e38f8a7a
LR
1730static bool freq_is_chan_12_13_14(u16 freq)
1731{
1732 if (freq == ieee80211_channel_to_frequency(12) ||
1733 freq == ieee80211_channel_to_frequency(13) ||
1734 freq == ieee80211_channel_to_frequency(14))
1735 return true;
1736 return false;
1737}
1738
1739int regulatory_hint_found_beacon(struct wiphy *wiphy,
1740 struct ieee80211_channel *beacon_chan,
1741 gfp_t gfp)
1742{
1743 struct reg_beacon *reg_beacon;
1744
1745 if (likely((beacon_chan->beacon_found ||
1746 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1747 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1748 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1749 return 0;
1750
1751 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1752 if (!reg_beacon)
1753 return -ENOMEM;
1754
1755#ifdef CONFIG_CFG80211_REG_DEBUG
1756 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1757 "frequency: %d MHz (Ch %d) on %s\n",
1758 beacon_chan->center_freq,
1759 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1760 wiphy_name(wiphy));
1761#endif
1762 memcpy(&reg_beacon->chan, beacon_chan,
1763 sizeof(struct ieee80211_channel));
1764
1765
1766 /*
1767 * Since we can be called from BH or and non-BH context
1768 * we must use spin_lock_bh()
1769 */
1770 spin_lock_bh(&reg_pending_beacons_lock);
1771 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1772 spin_unlock_bh(&reg_pending_beacons_lock);
1773
1774 schedule_work(&reg_work);
1775
1776 return 0;
1777}
1778
a3d2eaf0 1779static void print_rd_rules(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1780{
1781 unsigned int i;
a3d2eaf0
JB
1782 const struct ieee80211_reg_rule *reg_rule = NULL;
1783 const struct ieee80211_freq_range *freq_range = NULL;
1784 const struct ieee80211_power_rule *power_rule = NULL;
b2e1b302
LR
1785
1786 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1787 "(max_antenna_gain, max_eirp)\n");
1788
1789 for (i = 0; i < rd->n_reg_rules; i++) {
1790 reg_rule = &rd->reg_rules[i];
1791 freq_range = &reg_rule->freq_range;
1792 power_rule = &reg_rule->power_rule;
1793
fb1fc7ad
LR
1794 /*
1795 * There may not be documentation for max antenna gain
1796 * in certain regions
1797 */
b2e1b302
LR
1798 if (power_rule->max_antenna_gain)
1799 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1800 "(%d mBi, %d mBm)\n",
1801 freq_range->start_freq_khz,
1802 freq_range->end_freq_khz,
1803 freq_range->max_bandwidth_khz,
1804 power_rule->max_antenna_gain,
1805 power_rule->max_eirp);
1806 else
1807 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1808 "(N/A, %d mBm)\n",
1809 freq_range->start_freq_khz,
1810 freq_range->end_freq_khz,
1811 freq_range->max_bandwidth_khz,
1812 power_rule->max_eirp);
1813 }
1814}
1815
a3d2eaf0 1816static void print_regdomain(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1817{
1818
3f2355cb 1819 if (is_intersected_alpha2(rd->alpha2)) {
3f2355cb
LR
1820
1821 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
806a9e39
LR
1822 struct cfg80211_registered_device *drv;
1823 drv = cfg80211_drv_by_wiphy_idx(
1824 last_request->wiphy_idx);
1825 if (drv) {
3f2355cb
LR
1826 printk(KERN_INFO "cfg80211: Current regulatory "
1827 "domain updated by AP to: %c%c\n",
1828 drv->country_ie_alpha2[0],
1829 drv->country_ie_alpha2[1]);
1830 } else
1831 printk(KERN_INFO "cfg80211: Current regulatory "
1832 "domain intersected: \n");
1833 } else
1834 printk(KERN_INFO "cfg80211: Current regulatory "
039498c6 1835 "domain intersected: \n");
3f2355cb 1836 } else if (is_world_regdom(rd->alpha2))
b2e1b302
LR
1837 printk(KERN_INFO "cfg80211: World regulatory "
1838 "domain updated:\n");
1839 else {
1840 if (is_unknown_alpha2(rd->alpha2))
1841 printk(KERN_INFO "cfg80211: Regulatory domain "
1842 "changed to driver built-in settings "
1843 "(unknown country)\n");
1844 else
1845 printk(KERN_INFO "cfg80211: Regulatory domain "
1846 "changed to country: %c%c\n",
1847 rd->alpha2[0], rd->alpha2[1]);
1848 }
1849 print_rd_rules(rd);
1850}
1851
2df78167 1852static void print_regdomain_info(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1853{
1854 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1855 rd->alpha2[0], rd->alpha2[1]);
1856 print_rd_rules(rd);
1857}
1858
3f2355cb
LR
1859#ifdef CONFIG_CFG80211_REG_DEBUG
1860static void reg_country_ie_process_debug(
1861 const struct ieee80211_regdomain *rd,
1862 const struct ieee80211_regdomain *country_ie_regdomain,
1863 const struct ieee80211_regdomain *intersected_rd)
1864{
1865 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1866 print_regdomain_info(country_ie_regdomain);
1867 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1868 print_regdomain_info(rd);
1869 if (intersected_rd) {
1870 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1871 "and get:\n");
667ecd01 1872 print_regdomain_info(intersected_rd);
3f2355cb
LR
1873 return;
1874 }
1875 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1876}
1877#else
1878static inline void reg_country_ie_process_debug(
1879 const struct ieee80211_regdomain *rd,
1880 const struct ieee80211_regdomain *country_ie_regdomain,
1881 const struct ieee80211_regdomain *intersected_rd)
1882{
1883}
1884#endif
1885
d2372b31 1886/* Takes ownership of rd only if it doesn't fail */
a3d2eaf0 1887static int __set_regdom(const struct ieee80211_regdomain *rd)
b2e1b302 1888{
9c96477d 1889 const struct ieee80211_regdomain *intersected_rd = NULL;
3f2355cb 1890 struct cfg80211_registered_device *drv = NULL;
806a9e39 1891 struct wiphy *request_wiphy;
b2e1b302
LR
1892 /* Some basic sanity checks first */
1893
b2e1b302 1894 if (is_world_regdom(rd->alpha2)) {
f6037d09 1895 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
b2e1b302
LR
1896 return -EINVAL;
1897 update_world_regdomain(rd);
1898 return 0;
1899 }
b2e1b302
LR
1900
1901 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1902 !is_unknown_alpha2(rd->alpha2))
1903 return -EINVAL;
1904
f6037d09 1905 if (!last_request)
b2e1b302
LR
1906 return -EINVAL;
1907
fb1fc7ad
LR
1908 /*
1909 * Lets only bother proceeding on the same alpha2 if the current
3f2355cb 1910 * rd is non static (it means CRDA was present and was used last)
fb1fc7ad
LR
1911 * and the pending request came in from a country IE
1912 */
3f2355cb 1913 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
fb1fc7ad
LR
1914 /*
1915 * If someone else asked us to change the rd lets only bother
1916 * checking if the alpha2 changes if CRDA was already called
1917 */
3f2355cb 1918 if (!is_old_static_regdom(cfg80211_regdomain) &&
69b1572b 1919 !regdom_changes(rd->alpha2))
3f2355cb
LR
1920 return -EINVAL;
1921 }
1922
fb1fc7ad
LR
1923 /*
1924 * Now lets set the regulatory domain, update all driver channels
b2e1b302
LR
1925 * and finally inform them of what we have done, in case they want
1926 * to review or adjust their own settings based on their own
fb1fc7ad
LR
1927 * internal EEPROM data
1928 */
b2e1b302 1929
f6037d09 1930 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
b2e1b302
LR
1931 return -EINVAL;
1932
8375af3b
LR
1933 if (!is_valid_rd(rd)) {
1934 printk(KERN_ERR "cfg80211: Invalid "
1935 "regulatory domain detected:\n");
1936 print_regdomain_info(rd);
1937 return -EINVAL;
b2e1b302
LR
1938 }
1939
806a9e39
LR
1940 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1941
b8295acd 1942 if (!last_request->intersect) {
3e0c3ff3
LR
1943 int r;
1944
1945 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1946 reset_regdomains();
1947 cfg80211_regdomain = rd;
1948 return 0;
1949 }
1950
fb1fc7ad
LR
1951 /*
1952 * For a driver hint, lets copy the regulatory domain the
1953 * driver wanted to the wiphy to deal with conflicts
1954 */
3e0c3ff3 1955
806a9e39 1956 BUG_ON(request_wiphy->regd);
3e0c3ff3 1957
806a9e39 1958 r = reg_copy_regd(&request_wiphy->regd, rd);
3e0c3ff3
LR
1959 if (r)
1960 return r;
1961
b8295acd
LR
1962 reset_regdomains();
1963 cfg80211_regdomain = rd;
1964 return 0;
1965 }
1966
1967 /* Intersection requires a bit more work */
1968
1969 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1970
9c96477d
LR
1971 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1972 if (!intersected_rd)
1973 return -EINVAL;
b8295acd 1974
fb1fc7ad
LR
1975 /*
1976 * We can trash what CRDA provided now.
3e0c3ff3 1977 * However if a driver requested this specific regulatory
fb1fc7ad
LR
1978 * domain we keep it for its private use
1979 */
3e0c3ff3 1980 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
806a9e39 1981 request_wiphy->regd = rd;
3e0c3ff3
LR
1982 else
1983 kfree(rd);
1984
b8295acd
LR
1985 rd = NULL;
1986
1987 reset_regdomains();
1988 cfg80211_regdomain = intersected_rd;
1989
1990 return 0;
9c96477d
LR
1991 }
1992
3f2355cb
LR
1993 /*
1994 * Country IE requests are handled a bit differently, we intersect
1995 * the country IE rd with what CRDA believes that country should have
1996 */
1997
1998 BUG_ON(!country_ie_regdomain);
1999
2000 if (rd != country_ie_regdomain) {
fb1fc7ad
LR
2001 /*
2002 * Intersect what CRDA returned and our what we
2003 * had built from the Country IE received
2004 */
3f2355cb
LR
2005
2006 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2007
2008 reg_country_ie_process_debug(rd, country_ie_regdomain,
2009 intersected_rd);
2010
2011 kfree(country_ie_regdomain);
2012 country_ie_regdomain = NULL;
2013 } else {
fb1fc7ad
LR
2014 /*
2015 * This would happen when CRDA was not present and
3f2355cb 2016 * OLD_REGULATORY was enabled. We intersect our Country
fb1fc7ad
LR
2017 * IE rd and what was set on cfg80211 originally
2018 */
3f2355cb
LR
2019 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2020 }
2021
2022 if (!intersected_rd)
2023 return -EINVAL;
2024
806a9e39 2025 drv = wiphy_to_dev(request_wiphy);
3f2355cb
LR
2026
2027 drv->country_ie_alpha2[0] = rd->alpha2[0];
2028 drv->country_ie_alpha2[1] = rd->alpha2[1];
2029 drv->env = last_request->country_ie_env;
2030
2031 BUG_ON(intersected_rd == rd);
2032
2033 kfree(rd);
2034 rd = NULL;
2035
b8295acd 2036 reset_regdomains();
3f2355cb 2037 cfg80211_regdomain = intersected_rd;
b2e1b302
LR
2038
2039 return 0;
2040}
2041
2042
fb1fc7ad
LR
2043/*
2044 * Use this call to set the current regulatory domain. Conflicts with
b2e1b302 2045 * multiple drivers can be ironed out later. Caller must've already
fb1fc7ad
LR
2046 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2047 */
a3d2eaf0 2048int set_regdom(const struct ieee80211_regdomain *rd)
b2e1b302 2049{
b2e1b302
LR
2050 int r;
2051
761cf7ec
LR
2052 assert_cfg80211_lock();
2053
b2e1b302
LR
2054 /* Note that this doesn't update the wiphys, this is done below */
2055 r = __set_regdom(rd);
d2372b31
JB
2056 if (r) {
2057 kfree(rd);
b2e1b302 2058 return r;
d2372b31 2059 }
b2e1b302 2060
b2e1b302 2061 /* This would make this whole thing pointless */
a01ddafd
LR
2062 if (!last_request->intersect)
2063 BUG_ON(rd != cfg80211_regdomain);
b2e1b302
LR
2064
2065 /* update all wiphys now with the new established regulatory domain */
f6037d09 2066 update_all_wiphy_regulatory(last_request->initiator);
b2e1b302 2067
a01ddafd 2068 print_regdomain(cfg80211_regdomain);
b2e1b302
LR
2069
2070 return r;
2071}
2072
a1794390 2073/* Caller must hold cfg80211_mutex */
3f2355cb
LR
2074void reg_device_remove(struct wiphy *wiphy)
2075{
806a9e39
LR
2076 struct wiphy *request_wiphy;
2077
761cf7ec
LR
2078 assert_cfg80211_lock();
2079
806a9e39
LR
2080 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2081
3e0c3ff3 2082 kfree(wiphy->regd);
806a9e39 2083 if (!last_request || !request_wiphy)
3f2355cb 2084 return;
806a9e39 2085 if (request_wiphy != wiphy)
3f2355cb 2086 return;
806a9e39 2087 last_request->wiphy_idx = WIPHY_IDX_STALE;
3f2355cb
LR
2088 last_request->country_ie_env = ENVIRON_ANY;
2089}
2090
b2e1b302
LR
2091int regulatory_init(void)
2092{
bcf4f99b 2093 int err = 0;
734366de 2094
b2e1b302
LR
2095 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2096 if (IS_ERR(reg_pdev))
2097 return PTR_ERR(reg_pdev);
734366de 2098
fe33eb39 2099 spin_lock_init(&reg_requests_lock);
e38f8a7a 2100 spin_lock_init(&reg_pending_beacons_lock);
fe33eb39 2101
734366de 2102#ifdef CONFIG_WIRELESS_OLD_REGULATORY
a3d2eaf0 2103 cfg80211_regdomain = static_regdom(ieee80211_regdom);
734366de 2104
942b25cf 2105 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
734366de 2106 print_regdomain_info(cfg80211_regdomain);
fb1fc7ad
LR
2107 /*
2108 * The old code still requests for a new regdomain and if
734366de
JB
2109 * you have CRDA you get it updated, otherwise you get
2110 * stuck with the static values. We ignore "EU" code as
fb1fc7ad
LR
2111 * that is not a valid ISO / IEC 3166 alpha2
2112 */
ac9440a4 2113 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
ba25c141 2114 err = regulatory_hint_core(ieee80211_regdom);
734366de 2115#else
a3d2eaf0 2116 cfg80211_regdomain = cfg80211_world_regdom;
734366de 2117
ba25c141 2118 err = regulatory_hint_core("00");
bcf4f99b 2119#endif
ba25c141 2120 if (err) {
bcf4f99b
LR
2121 if (err == -ENOMEM)
2122 return err;
2123 /*
2124 * N.B. kobject_uevent_env() can fail mainly for when we're out
2125 * memory which is handled and propagated appropriately above
2126 * but it can also fail during a netlink_broadcast() or during
2127 * early boot for call_usermodehelper(). For now treat these
2128 * errors as non-fatal.
2129 */
2130 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2131 "to call CRDA during init");
2132#ifdef CONFIG_CFG80211_REG_DEBUG
2133 /* We want to find out exactly why when debugging */
2134 WARN_ON(err);
734366de 2135#endif
bcf4f99b 2136 }
734366de 2137
b2e1b302
LR
2138 return 0;
2139}
2140
2141void regulatory_exit(void)
2142{
fe33eb39 2143 struct regulatory_request *reg_request, *tmp;
e38f8a7a 2144 struct reg_beacon *reg_beacon, *btmp;
fe33eb39
LR
2145
2146 cancel_work_sync(&reg_work);
2147
a1794390 2148 mutex_lock(&cfg80211_mutex);
734366de 2149
b2e1b302 2150 reset_regdomains();
734366de 2151
3f2355cb
LR
2152 kfree(country_ie_regdomain);
2153 country_ie_regdomain = NULL;
2154
f6037d09
JB
2155 kfree(last_request);
2156
b2e1b302 2157 platform_device_unregister(reg_pdev);
734366de 2158
e38f8a7a
LR
2159 spin_lock_bh(&reg_pending_beacons_lock);
2160 if (!list_empty(&reg_pending_beacons)) {
2161 list_for_each_entry_safe(reg_beacon, btmp,
2162 &reg_pending_beacons, list) {
2163 list_del(&reg_beacon->list);
2164 kfree(reg_beacon);
2165 }
2166 }
2167 spin_unlock_bh(&reg_pending_beacons_lock);
2168
2169 if (!list_empty(&reg_beacon_list)) {
2170 list_for_each_entry_safe(reg_beacon, btmp,
2171 &reg_beacon_list, list) {
2172 list_del(&reg_beacon->list);
2173 kfree(reg_beacon);
2174 }
2175 }
2176
fe33eb39
LR
2177 spin_lock(&reg_requests_lock);
2178 if (!list_empty(&reg_requests_list)) {
2179 list_for_each_entry_safe(reg_request, tmp,
2180 &reg_requests_list, list) {
2181 list_del(&reg_request->list);
2182 kfree(reg_request);
2183 }
2184 }
2185 spin_unlock(&reg_requests_lock);
2186
a1794390 2187 mutex_unlock(&cfg80211_mutex);
8318d78a 2188}