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