]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/wireless/reg.c
cfg80211: add wiphy_idx_valid to check for wiphy_idx sanity
[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
734366de
JB
57/* Central wireless core regulatory domains, we only need two,
58 * the current one and a world regulatory domain in case we have no
59 * information to give us an alpha2 */
f130347c 60const struct ieee80211_regdomain *cfg80211_regdomain;
734366de 61
3f2355cb
LR
62/* We use this as a place for the rd structure built from the
63 * last parsed country IE to rest until CRDA gets back to us with
64 * what it thinks should apply for the same country */
65static const struct ieee80211_regdomain *country_ie_regdomain;
66
734366de
JB
67/* We keep a static world regulatory domain in case of the absence of CRDA */
68static const struct ieee80211_regdomain world_regdom = {
69 .n_reg_rules = 1,
70 .alpha2 = "00",
71 .reg_rules = {
72 REG_RULE(2412-10, 2462+10, 40, 6, 20,
73 NL80211_RRF_PASSIVE_SCAN |
74 NL80211_RRF_NO_IBSS),
75 }
76};
77
a3d2eaf0
JB
78static const struct ieee80211_regdomain *cfg80211_world_regdom =
79 &world_regdom;
734366de
JB
80
81#ifdef CONFIG_WIRELESS_OLD_REGULATORY
82static char *ieee80211_regdom = "US";
83module_param(ieee80211_regdom, charp, 0444);
84MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
85
86/* We assume 40 MHz bandwidth for the old regulatory work.
87 * We make emphasis we are using the exact same frequencies
88 * as before */
89
90static const struct ieee80211_regdomain us_regdom = {
91 .n_reg_rules = 6,
92 .alpha2 = "US",
93 .reg_rules = {
94 /* IEEE 802.11b/g, channels 1..11 */
95 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
96 /* IEEE 802.11a, channel 36 */
97 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
98 /* IEEE 802.11a, channel 40 */
99 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
100 /* IEEE 802.11a, channel 44 */
101 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
102 /* IEEE 802.11a, channels 48..64 */
103 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
104 /* IEEE 802.11a, channels 149..165, outdoor */
105 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
106 }
107};
108
109static const struct ieee80211_regdomain jp_regdom = {
110 .n_reg_rules = 3,
111 .alpha2 = "JP",
112 .reg_rules = {
113 /* IEEE 802.11b/g, channels 1..14 */
114 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
115 /* IEEE 802.11a, channels 34..48 */
116 REG_RULE(5170-10, 5240+10, 40, 6, 20,
117 NL80211_RRF_PASSIVE_SCAN),
118 /* IEEE 802.11a, channels 52..64 */
119 REG_RULE(5260-10, 5320+10, 40, 6, 20,
120 NL80211_RRF_NO_IBSS |
121 NL80211_RRF_DFS),
122 }
123};
124
125static const struct ieee80211_regdomain eu_regdom = {
126 .n_reg_rules = 6,
127 /* This alpha2 is bogus, we leave it here just for stupid
128 * backward compatibility */
129 .alpha2 = "EU",
130 .reg_rules = {
131 /* IEEE 802.11b/g, channels 1..13 */
132 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
133 /* IEEE 802.11a, channel 36 */
134 REG_RULE(5180-10, 5180+10, 40, 6, 23,
135 NL80211_RRF_PASSIVE_SCAN),
136 /* IEEE 802.11a, channel 40 */
137 REG_RULE(5200-10, 5200+10, 40, 6, 23,
138 NL80211_RRF_PASSIVE_SCAN),
139 /* IEEE 802.11a, channel 44 */
140 REG_RULE(5220-10, 5220+10, 40, 6, 23,
141 NL80211_RRF_PASSIVE_SCAN),
142 /* IEEE 802.11a, channels 48..64 */
143 REG_RULE(5240-10, 5320+10, 40, 6, 20,
144 NL80211_RRF_NO_IBSS |
145 NL80211_RRF_DFS),
146 /* IEEE 802.11a, channels 100..140 */
147 REG_RULE(5500-10, 5700+10, 40, 6, 30,
148 NL80211_RRF_NO_IBSS |
149 NL80211_RRF_DFS),
150 }
151};
152
153static const struct ieee80211_regdomain *static_regdom(char *alpha2)
154{
155 if (alpha2[0] == 'U' && alpha2[1] == 'S')
156 return &us_regdom;
157 if (alpha2[0] == 'J' && alpha2[1] == 'P')
158 return &jp_regdom;
159 if (alpha2[0] == 'E' && alpha2[1] == 'U')
160 return &eu_regdom;
161 /* Default, as per the old rules */
162 return &us_regdom;
163}
164
a3d2eaf0 165static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
734366de
JB
166{
167 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
168 return true;
169 return false;
170}
942b25cf
JB
171#else
172static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
734366de 173{
942b25cf 174 return false;
734366de 175}
942b25cf
JB
176#endif
177
734366de
JB
178static void reset_regdomains(void)
179{
942b25cf
JB
180 /* avoid freeing static information or freeing something twice */
181 if (cfg80211_regdomain == cfg80211_world_regdom)
182 cfg80211_regdomain = NULL;
183 if (cfg80211_world_regdom == &world_regdom)
184 cfg80211_world_regdom = NULL;
185 if (cfg80211_regdomain == &world_regdom)
186 cfg80211_regdomain = NULL;
187 if (is_old_static_regdom(cfg80211_regdomain))
188 cfg80211_regdomain = NULL;
189
190 kfree(cfg80211_regdomain);
191 kfree(cfg80211_world_regdom);
734366de 192
a3d2eaf0 193 cfg80211_world_regdom = &world_regdom;
734366de
JB
194 cfg80211_regdomain = NULL;
195}
196
197/* Dynamic world regulatory domain requested by the wireless
198 * core upon initialization */
a3d2eaf0 199static void update_world_regdomain(const struct ieee80211_regdomain *rd)
734366de 200{
f6037d09 201 BUG_ON(!last_request);
734366de
JB
202
203 reset_regdomains();
204
205 cfg80211_world_regdom = rd;
206 cfg80211_regdomain = rd;
207}
734366de 208
a3d2eaf0 209bool is_world_regdom(const char *alpha2)
b2e1b302
LR
210{
211 if (!alpha2)
212 return false;
213 if (alpha2[0] == '0' && alpha2[1] == '0')
214 return true;
215 return false;
216}
8318d78a 217
a3d2eaf0 218static bool is_alpha2_set(const char *alpha2)
b2e1b302
LR
219{
220 if (!alpha2)
221 return false;
222 if (alpha2[0] != 0 && alpha2[1] != 0)
223 return true;
224 return false;
225}
8318d78a 226
b2e1b302
LR
227static bool is_alpha_upper(char letter)
228{
229 /* ASCII A - Z */
230 if (letter >= 65 && letter <= 90)
231 return true;
232 return false;
233}
8318d78a 234
a3d2eaf0 235static bool is_unknown_alpha2(const char *alpha2)
b2e1b302
LR
236{
237 if (!alpha2)
238 return false;
239 /* Special case where regulatory domain was built by driver
240 * but a specific alpha2 cannot be determined */
241 if (alpha2[0] == '9' && alpha2[1] == '9')
242 return true;
243 return false;
244}
8318d78a 245
3f2355cb
LR
246static bool is_intersected_alpha2(const char *alpha2)
247{
248 if (!alpha2)
249 return false;
250 /* Special case where regulatory domain is the
251 * result of an intersection between two regulatory domain
252 * structures */
253 if (alpha2[0] == '9' && alpha2[1] == '8')
254 return true;
255 return false;
256}
257
a3d2eaf0 258static bool is_an_alpha2(const char *alpha2)
b2e1b302
LR
259{
260 if (!alpha2)
261 return false;
262 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
263 return true;
264 return false;
265}
8318d78a 266
a3d2eaf0 267static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
b2e1b302
LR
268{
269 if (!alpha2_x || !alpha2_y)
270 return false;
271 if (alpha2_x[0] == alpha2_y[0] &&
272 alpha2_x[1] == alpha2_y[1])
273 return true;
274 return false;
275}
276
a3d2eaf0 277static bool regdom_changed(const char *alpha2)
b2e1b302
LR
278{
279 if (!cfg80211_regdomain)
280 return true;
281 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
282 return false;
283 return true;
284}
285
3f2355cb
LR
286/**
287 * country_ie_integrity_changes - tells us if the country IE has changed
288 * @checksum: checksum of country IE of fields we are interested in
289 *
290 * If the country IE has not changed you can ignore it safely. This is
291 * useful to determine if two devices are seeing two different country IEs
292 * even on the same alpha2. Note that this will return false if no IE has
293 * been set on the wireless core yet.
294 */
295static bool country_ie_integrity_changes(u32 checksum)
296{
297 /* If no IE has been set then the checksum doesn't change */
298 if (unlikely(!last_request->country_ie_checksum))
299 return false;
300 if (unlikely(last_request->country_ie_checksum != checksum))
301 return true;
302 return false;
303}
304
b2e1b302
LR
305/* This lets us keep regulatory code which is updated on a regulatory
306 * basis in userspace. */
307static int call_crda(const char *alpha2)
308{
309 char country_env[9 + 2] = "COUNTRY=";
310 char *envp[] = {
311 country_env,
312 NULL
313 };
314
315 if (!is_world_regdom((char *) alpha2))
316 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
317 alpha2[0], alpha2[1]);
318 else
b2e1b302
LR
319 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
320 "regulatory domain\n");
b2e1b302
LR
321
322 country_env[8] = alpha2[0];
323 country_env[9] = alpha2[1];
324
325 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
326}
327
b2e1b302 328/* Used by nl80211 before kmalloc'ing our regulatory domain */
a3d2eaf0 329bool reg_is_valid_request(const char *alpha2)
b2e1b302 330{
f6037d09
JB
331 if (!last_request)
332 return false;
333
334 return alpha2_equal(last_request->alpha2, alpha2);
b2e1b302 335}
8318d78a 336
b2e1b302 337/* Sanity check on a regulatory rule */
a3d2eaf0 338static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
8318d78a 339{
a3d2eaf0 340 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
b2e1b302
LR
341 u32 freq_diff;
342
91e99004 343 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
b2e1b302
LR
344 return false;
345
346 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
347 return false;
348
349 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
350
d71aaf60 351 if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
b2e1b302
LR
352 return false;
353
354 return true;
355}
356
a3d2eaf0 357static bool is_valid_rd(const struct ieee80211_regdomain *rd)
b2e1b302 358{
a3d2eaf0 359 const struct ieee80211_reg_rule *reg_rule = NULL;
b2e1b302 360 unsigned int i;
8318d78a 361
b2e1b302
LR
362 if (!rd->n_reg_rules)
363 return false;
8318d78a 364
88dc1c3f
LR
365 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
366 return false;
367
b2e1b302
LR
368 for (i = 0; i < rd->n_reg_rules; i++) {
369 reg_rule = &rd->reg_rules[i];
370 if (!is_valid_reg_rule(reg_rule))
371 return false;
372 }
373
374 return true;
8318d78a
JB
375}
376
b2e1b302
LR
377/* Returns value in KHz */
378static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
379 u32 freq)
380{
381 unsigned int i;
382 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
383 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
384 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
385 if (start_freq_khz >= freq_range->start_freq_khz &&
386 end_freq_khz <= freq_range->end_freq_khz)
387 return supported_bandwidths[i];
388 }
389 return 0;
390}
8318d78a 391
0c7dc45d
LR
392/**
393 * freq_in_rule_band - tells us if a frequency is in a frequency band
394 * @freq_range: frequency rule we want to query
395 * @freq_khz: frequency we are inquiring about
396 *
397 * This lets us know if a specific frequency rule is or is not relevant to
398 * a specific frequency's band. Bands are device specific and artificial
399 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
400 * safe for now to assume that a frequency rule should not be part of a
401 * frequency's band if the start freq or end freq are off by more than 2 GHz.
402 * This resolution can be lowered and should be considered as we add
403 * regulatory rule support for other "bands".
404 **/
405static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
406 u32 freq_khz)
407{
408#define ONE_GHZ_IN_KHZ 1000000
409 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
410 return true;
411 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
412 return true;
413 return false;
414#undef ONE_GHZ_IN_KHZ
415}
416
3f2355cb
LR
417/* Converts a country IE to a regulatory domain. A regulatory domain
418 * structure has a lot of information which the IE doesn't yet have,
419 * so for the other values we use upper max values as we will intersect
420 * with our userspace regulatory agent to get lower bounds. */
421static struct ieee80211_regdomain *country_ie_2_rd(
422 u8 *country_ie,
423 u8 country_ie_len,
424 u32 *checksum)
425{
426 struct ieee80211_regdomain *rd = NULL;
427 unsigned int i = 0;
428 char alpha2[2];
429 u32 flags = 0;
430 u32 num_rules = 0, size_of_regd = 0;
431 u8 *triplets_start = NULL;
432 u8 len_at_triplet = 0;
433 /* the last channel we have registered in a subband (triplet) */
434 int last_sub_max_channel = 0;
435
436 *checksum = 0xDEADBEEF;
437
438 /* Country IE requirements */
439 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
440 country_ie_len & 0x01);
441
442 alpha2[0] = country_ie[0];
443 alpha2[1] = country_ie[1];
444
445 /*
446 * Third octet can be:
447 * 'I' - Indoor
448 * 'O' - Outdoor
449 *
450 * anything else we assume is no restrictions
451 */
452 if (country_ie[2] == 'I')
453 flags = NL80211_RRF_NO_OUTDOOR;
454 else if (country_ie[2] == 'O')
455 flags = NL80211_RRF_NO_INDOOR;
456
457 country_ie += 3;
458 country_ie_len -= 3;
459
460 triplets_start = country_ie;
461 len_at_triplet = country_ie_len;
462
463 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
464
465 /* We need to build a reg rule for each triplet, but first we must
466 * calculate the number of reg rules we will need. We will need one
467 * for each channel subband */
468 while (country_ie_len >= 3) {
615aab4b 469 int end_channel = 0;
3f2355cb
LR
470 struct ieee80211_country_ie_triplet *triplet =
471 (struct ieee80211_country_ie_triplet *) country_ie;
472 int cur_sub_max_channel = 0, cur_channel = 0;
473
474 if (triplet->ext.reg_extension_id >=
475 IEEE80211_COUNTRY_EXTENSION_ID) {
476 country_ie += 3;
477 country_ie_len -= 3;
478 continue;
479 }
480
615aab4b
LR
481 /* 2 GHz */
482 if (triplet->chans.first_channel <= 14)
483 end_channel = triplet->chans.first_channel +
484 triplet->chans.num_channels;
485 else
486 /*
487 * 5 GHz -- For example in country IEs if the first
488 * channel given is 36 and the number of channels is 4
489 * then the individual channel numbers defined for the
490 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
491 * and not 36, 37, 38, 39.
492 *
493 * See: http://tinyurl.com/11d-clarification
494 */
495 end_channel = triplet->chans.first_channel +
496 (4 * (triplet->chans.num_channels - 1));
497
3f2355cb 498 cur_channel = triplet->chans.first_channel;
615aab4b 499 cur_sub_max_channel = end_channel;
3f2355cb
LR
500
501 /* Basic sanity check */
502 if (cur_sub_max_channel < cur_channel)
503 return NULL;
504
505 /* Do not allow overlapping channels. Also channels
506 * passed in each subband must be monotonically
507 * increasing */
508 if (last_sub_max_channel) {
509 if (cur_channel <= last_sub_max_channel)
510 return NULL;
511 if (cur_sub_max_channel <= last_sub_max_channel)
512 return NULL;
513 }
514
515 /* When dot11RegulatoryClassesRequired is supported
516 * we can throw ext triplets as part of this soup,
517 * for now we don't care when those change as we
518 * don't support them */
519 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
520 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
521 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
522
523 last_sub_max_channel = cur_sub_max_channel;
524
525 country_ie += 3;
526 country_ie_len -= 3;
527 num_rules++;
528
529 /* Note: this is not a IEEE requirement but
530 * simply a memory requirement */
531 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
532 return NULL;
533 }
534
535 country_ie = triplets_start;
536 country_ie_len = len_at_triplet;
537
538 size_of_regd = sizeof(struct ieee80211_regdomain) +
539 (num_rules * sizeof(struct ieee80211_reg_rule));
540
541 rd = kzalloc(size_of_regd, GFP_KERNEL);
542 if (!rd)
543 return NULL;
544
545 rd->n_reg_rules = num_rules;
546 rd->alpha2[0] = alpha2[0];
547 rd->alpha2[1] = alpha2[1];
548
549 /* This time around we fill in the rd */
550 while (country_ie_len >= 3) {
02e68a3d 551 int end_channel = 0;
3f2355cb
LR
552 struct ieee80211_country_ie_triplet *triplet =
553 (struct ieee80211_country_ie_triplet *) country_ie;
554 struct ieee80211_reg_rule *reg_rule = NULL;
555 struct ieee80211_freq_range *freq_range = NULL;
556 struct ieee80211_power_rule *power_rule = NULL;
557
558 /* Must parse if dot11RegulatoryClassesRequired is true,
559 * we don't support this yet */
560 if (triplet->ext.reg_extension_id >=
561 IEEE80211_COUNTRY_EXTENSION_ID) {
562 country_ie += 3;
563 country_ie_len -= 3;
564 continue;
565 }
566
567 reg_rule = &rd->reg_rules[i];
568 freq_range = &reg_rule->freq_range;
569 power_rule = &reg_rule->power_rule;
570
571 reg_rule->flags = flags;
572
02e68a3d
LR
573 /* 2 GHz */
574 if (triplet->chans.first_channel <= 14)
575 end_channel = triplet->chans.first_channel +
576 triplet->chans.num_channels;
577 else
02e68a3d
LR
578 end_channel = triplet->chans.first_channel +
579 (4 * (triplet->chans.num_channels - 1));
580
3f2355cb
LR
581 /* The +10 is since the regulatory domain expects
582 * the actual band edge, not the center of freq for
583 * its start and end freqs, assuming 20 MHz bandwidth on
584 * the channels passed */
585 freq_range->start_freq_khz =
586 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
587 triplet->chans.first_channel) - 10);
588 freq_range->end_freq_khz =
589 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
02e68a3d 590 end_channel) + 10);
3f2355cb
LR
591
592 /* Large arbitrary values, we intersect later */
593 /* Increment this if we ever support >= 40 MHz channels
594 * in IEEE 802.11 */
595 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
596 power_rule->max_antenna_gain = DBI_TO_MBI(100);
597 power_rule->max_eirp = DBM_TO_MBM(100);
598
599 country_ie += 3;
600 country_ie_len -= 3;
601 i++;
602
603 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
604 }
605
606 return rd;
607}
608
609
9c96477d
LR
610/* Helper for regdom_intersect(), this does the real
611 * mathematical intersection fun */
612static int reg_rules_intersect(
613 const struct ieee80211_reg_rule *rule1,
614 const struct ieee80211_reg_rule *rule2,
615 struct ieee80211_reg_rule *intersected_rule)
616{
617 const struct ieee80211_freq_range *freq_range1, *freq_range2;
618 struct ieee80211_freq_range *freq_range;
619 const struct ieee80211_power_rule *power_rule1, *power_rule2;
620 struct ieee80211_power_rule *power_rule;
621 u32 freq_diff;
622
623 freq_range1 = &rule1->freq_range;
624 freq_range2 = &rule2->freq_range;
625 freq_range = &intersected_rule->freq_range;
626
627 power_rule1 = &rule1->power_rule;
628 power_rule2 = &rule2->power_rule;
629 power_rule = &intersected_rule->power_rule;
630
631 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
632 freq_range2->start_freq_khz);
633 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
634 freq_range2->end_freq_khz);
635 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
636 freq_range2->max_bandwidth_khz);
637
638 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
639 if (freq_range->max_bandwidth_khz > freq_diff)
640 freq_range->max_bandwidth_khz = freq_diff;
641
642 power_rule->max_eirp = min(power_rule1->max_eirp,
643 power_rule2->max_eirp);
644 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
645 power_rule2->max_antenna_gain);
646
647 intersected_rule->flags = (rule1->flags | rule2->flags);
648
649 if (!is_valid_reg_rule(intersected_rule))
650 return -EINVAL;
651
652 return 0;
653}
654
655/**
656 * regdom_intersect - do the intersection between two regulatory domains
657 * @rd1: first regulatory domain
658 * @rd2: second regulatory domain
659 *
660 * Use this function to get the intersection between two regulatory domains.
661 * Once completed we will mark the alpha2 for the rd as intersected, "98",
662 * as no one single alpha2 can represent this regulatory domain.
663 *
664 * Returns a pointer to the regulatory domain structure which will hold the
665 * resulting intersection of rules between rd1 and rd2. We will
666 * kzalloc() this structure for you.
667 */
668static struct ieee80211_regdomain *regdom_intersect(
669 const struct ieee80211_regdomain *rd1,
670 const struct ieee80211_regdomain *rd2)
671{
672 int r, size_of_regd;
673 unsigned int x, y;
674 unsigned int num_rules = 0, rule_idx = 0;
675 const struct ieee80211_reg_rule *rule1, *rule2;
676 struct ieee80211_reg_rule *intersected_rule;
677 struct ieee80211_regdomain *rd;
678 /* This is just a dummy holder to help us count */
679 struct ieee80211_reg_rule irule;
680
681 /* Uses the stack temporarily for counter arithmetic */
682 intersected_rule = &irule;
683
684 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
685
686 if (!rd1 || !rd2)
687 return NULL;
688
689 /* First we get a count of the rules we'll need, then we actually
690 * build them. This is to so we can malloc() and free() a
691 * regdomain once. The reason we use reg_rules_intersect() here
692 * is it will return -EINVAL if the rule computed makes no sense.
693 * All rules that do check out OK are valid. */
694
695 for (x = 0; x < rd1->n_reg_rules; x++) {
696 rule1 = &rd1->reg_rules[x];
697 for (y = 0; y < rd2->n_reg_rules; y++) {
698 rule2 = &rd2->reg_rules[y];
699 if (!reg_rules_intersect(rule1, rule2,
700 intersected_rule))
701 num_rules++;
702 memset(intersected_rule, 0,
703 sizeof(struct ieee80211_reg_rule));
704 }
705 }
706
707 if (!num_rules)
708 return NULL;
709
710 size_of_regd = sizeof(struct ieee80211_regdomain) +
711 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
712
713 rd = kzalloc(size_of_regd, GFP_KERNEL);
714 if (!rd)
715 return NULL;
716
717 for (x = 0; x < rd1->n_reg_rules; x++) {
718 rule1 = &rd1->reg_rules[x];
719 for (y = 0; y < rd2->n_reg_rules; y++) {
720 rule2 = &rd2->reg_rules[y];
721 /* This time around instead of using the stack lets
722 * write to the target rule directly saving ourselves
723 * a memcpy() */
724 intersected_rule = &rd->reg_rules[rule_idx];
725 r = reg_rules_intersect(rule1, rule2,
726 intersected_rule);
727 /* No need to memset here the intersected rule here as
728 * we're not using the stack anymore */
729 if (r)
730 continue;
731 rule_idx++;
732 }
733 }
734
735 if (rule_idx != num_rules) {
736 kfree(rd);
737 return NULL;
738 }
739
740 rd->n_reg_rules = num_rules;
741 rd->alpha2[0] = '9';
742 rd->alpha2[1] = '8';
743
744 return rd;
745}
746
b2e1b302
LR
747/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
748 * want to just have the channel structure use these */
749static u32 map_regdom_flags(u32 rd_flags)
750{
751 u32 channel_flags = 0;
752 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
753 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
754 if (rd_flags & NL80211_RRF_NO_IBSS)
755 channel_flags |= IEEE80211_CHAN_NO_IBSS;
756 if (rd_flags & NL80211_RRF_DFS)
757 channel_flags |= IEEE80211_CHAN_RADAR;
758 return channel_flags;
759}
760
1fa25e41
LR
761static int freq_reg_info_regd(struct wiphy *wiphy,
762 u32 center_freq,
763 u32 *bandwidth,
764 const struct ieee80211_reg_rule **reg_rule,
765 const struct ieee80211_regdomain *custom_regd)
8318d78a
JB
766{
767 int i;
0c7dc45d 768 bool band_rule_found = false;
3e0c3ff3 769 const struct ieee80211_regdomain *regd;
b2e1b302 770 u32 max_bandwidth = 0;
8318d78a 771
1fa25e41 772 regd = custom_regd ? custom_regd : cfg80211_regdomain;
3e0c3ff3
LR
773
774 /* Follow the driver's regulatory domain, if present, unless a country
24ed1da1 775 * IE has been processed or a user wants to help complaince further */
3e0c3ff3 776 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
24ed1da1 777 last_request->initiator != REGDOM_SET_BY_USER &&
3e0c3ff3
LR
778 wiphy->regd)
779 regd = wiphy->regd;
780
781 if (!regd)
b2e1b302
LR
782 return -EINVAL;
783
3e0c3ff3 784 for (i = 0; i < regd->n_reg_rules; i++) {
b2e1b302
LR
785 const struct ieee80211_reg_rule *rr;
786 const struct ieee80211_freq_range *fr = NULL;
787 const struct ieee80211_power_rule *pr = NULL;
788
3e0c3ff3 789 rr = &regd->reg_rules[i];
b2e1b302
LR
790 fr = &rr->freq_range;
791 pr = &rr->power_rule;
0c7dc45d
LR
792
793 /* We only need to know if one frequency rule was
794 * was in center_freq's band, that's enough, so lets
795 * not overwrite it once found */
796 if (!band_rule_found)
797 band_rule_found = freq_in_rule_band(fr, center_freq);
798
b2e1b302 799 max_bandwidth = freq_max_bandwidth(fr, center_freq);
0c7dc45d 800
b2e1b302
LR
801 if (max_bandwidth && *bandwidth <= max_bandwidth) {
802 *reg_rule = rr;
803 *bandwidth = max_bandwidth;
8318d78a
JB
804 break;
805 }
806 }
807
0c7dc45d
LR
808 if (!band_rule_found)
809 return -ERANGE;
810
b2e1b302
LR
811 return !max_bandwidth;
812}
34f57347 813EXPORT_SYMBOL(freq_reg_info);
b2e1b302 814
34f57347 815int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
1fa25e41
LR
816 const struct ieee80211_reg_rule **reg_rule)
817{
818 return freq_reg_info_regd(wiphy, center_freq,
819 bandwidth, reg_rule, NULL);
820}
b2e1b302 821
a92a3ce7
LR
822static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
823 unsigned int chan_idx)
b2e1b302
LR
824{
825 int r;
a92a3ce7 826 u32 flags;
b2e1b302
LR
827 u32 max_bandwidth = 0;
828 const struct ieee80211_reg_rule *reg_rule = NULL;
829 const struct ieee80211_power_rule *power_rule = NULL;
a92a3ce7
LR
830 struct ieee80211_supported_band *sband;
831 struct ieee80211_channel *chan;
832
833 sband = wiphy->bands[band];
834 BUG_ON(chan_idx >= sband->n_channels);
835 chan = &sband->channels[chan_idx];
836
837 flags = chan->orig_flags;
b2e1b302 838
3e0c3ff3 839 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
b2e1b302
LR
840 &max_bandwidth, &reg_rule);
841
842 if (r) {
0c7dc45d
LR
843 /* This means no regulatory rule was found in the country IE
844 * with a frequency range on the center_freq's band, since
845 * IEEE-802.11 allows for a country IE to have a subset of the
846 * regulatory information provided in a country we ignore
847 * disabling the channel unless at least one reg rule was
848 * found on the center_freq's band. For details see this
849 * clarification:
850 *
851 * http://tinyurl.com/11d-clarification
852 */
853 if (r == -ERANGE &&
854 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
855#ifdef CONFIG_CFG80211_REG_DEBUG
856 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
857 "intact on %s - no rule found in band on "
858 "Country IE\n",
859 chan->center_freq, wiphy_name(wiphy));
860#endif
861 } else {
862 /* In this case we know the country IE has at least one reg rule
863 * for the band so we respect its band definitions */
864#ifdef CONFIG_CFG80211_REG_DEBUG
865 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
866 printk(KERN_DEBUG "cfg80211: Disabling "
867 "channel %d MHz on %s due to "
868 "Country IE\n",
869 chan->center_freq, wiphy_name(wiphy));
870#endif
871 flags |= IEEE80211_CHAN_DISABLED;
872 chan->flags = flags;
873 }
8318d78a
JB
874 return;
875 }
876
b2e1b302
LR
877 power_rule = &reg_rule->power_rule;
878
f976376d
LR
879 if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
880 last_request->wiphy && last_request->wiphy == wiphy &&
881 last_request->wiphy->strict_regulatory) {
882 /* This gaurantees the driver's requested regulatory domain
883 * will always be used as a base for further regulatory
884 * settings */
885 chan->flags = chan->orig_flags =
886 map_regdom_flags(reg_rule->flags);
887 chan->max_antenna_gain = chan->orig_mag =
888 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
889 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
890 chan->max_power = chan->orig_mpwr =
891 (int) MBM_TO_DBM(power_rule->max_eirp);
892 return;
893 }
894
b2e1b302 895 chan->flags = flags | map_regdom_flags(reg_rule->flags);
8318d78a 896 chan->max_antenna_gain = min(chan->orig_mag,
b2e1b302
LR
897 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
898 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
253898c4 899 if (chan->orig_mpwr)
b2e1b302
LR
900 chan->max_power = min(chan->orig_mpwr,
901 (int) MBM_TO_DBM(power_rule->max_eirp));
253898c4 902 else
b2e1b302 903 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
8318d78a
JB
904}
905
a92a3ce7 906static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
8318d78a 907{
a92a3ce7
LR
908 unsigned int i;
909 struct ieee80211_supported_band *sband;
910
911 BUG_ON(!wiphy->bands[band]);
912 sband = wiphy->bands[band];
8318d78a
JB
913
914 for (i = 0; i < sband->n_channels; i++)
a92a3ce7 915 handle_channel(wiphy, band, i);
8318d78a
JB
916}
917
14b9815a
LR
918static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
919{
920 if (!last_request)
921 return true;
922 if (setby == REGDOM_SET_BY_CORE &&
2a44f911 923 wiphy->custom_regulatory)
14b9815a 924 return true;
f976376d
LR
925 /* wiphy->regd will be set once the device has its own
926 * desired regulatory domain set */
927 if (wiphy->strict_regulatory && !wiphy->regd &&
928 !is_world_regdom(last_request->alpha2))
14b9815a
LR
929 return true;
930 return false;
931}
932
b2e1b302 933static void update_all_wiphy_regulatory(enum reg_set_by setby)
8318d78a 934{
b2e1b302 935 struct cfg80211_registered_device *drv;
8318d78a 936
b2e1b302 937 list_for_each_entry(drv, &cfg80211_drv_list, list)
d46e5b1d 938 wiphy_update_regulatory(&drv->wiphy, setby);
b2e1b302
LR
939}
940
941void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
942{
943 enum ieee80211_band band;
d46e5b1d
LR
944
945 if (ignore_reg_update(wiphy, setby))
946 return;
b2e1b302 947 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
8318d78a 948 if (wiphy->bands[band])
a92a3ce7 949 handle_band(wiphy, band);
b2e1b302 950 }
560e28e1 951 if (wiphy->reg_notifier)
716f9392 952 wiphy->reg_notifier(wiphy, last_request);
b2e1b302
LR
953}
954
1fa25e41
LR
955static void handle_channel_custom(struct wiphy *wiphy,
956 enum ieee80211_band band,
957 unsigned int chan_idx,
958 const struct ieee80211_regdomain *regd)
959{
960 int r;
961 u32 max_bandwidth = 0;
962 const struct ieee80211_reg_rule *reg_rule = NULL;
963 const struct ieee80211_power_rule *power_rule = NULL;
964 struct ieee80211_supported_band *sband;
965 struct ieee80211_channel *chan;
966
967 sband = wiphy->bands[band];
968 BUG_ON(chan_idx >= sband->n_channels);
969 chan = &sband->channels[chan_idx];
970
971 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
972 &max_bandwidth, &reg_rule, regd);
973
974 if (r) {
975 chan->flags = IEEE80211_CHAN_DISABLED;
976 return;
977 }
978
979 power_rule = &reg_rule->power_rule;
980
981 chan->flags |= map_regdom_flags(reg_rule->flags);
982 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
983 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
984 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
985}
986
987static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
988 const struct ieee80211_regdomain *regd)
989{
990 unsigned int i;
991 struct ieee80211_supported_band *sband;
992
993 BUG_ON(!wiphy->bands[band]);
994 sband = wiphy->bands[band];
995
996 for (i = 0; i < sband->n_channels; i++)
997 handle_channel_custom(wiphy, band, i, regd);
998}
999
1000/* Used by drivers prior to wiphy registration */
1001void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1002 const struct ieee80211_regdomain *regd)
1003{
1004 enum ieee80211_band band;
1005 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1006 if (wiphy->bands[band])
1007 handle_band_custom(wiphy, band, regd);
b2e1b302
LR
1008 }
1009}
1fa25e41
LR
1010EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1011
3e0c3ff3
LR
1012static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1013 const struct ieee80211_regdomain *src_regd)
1014{
1015 struct ieee80211_regdomain *regd;
1016 int size_of_regd = 0;
1017 unsigned int i;
1018
1019 size_of_regd = sizeof(struct ieee80211_regdomain) +
1020 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1021
1022 regd = kzalloc(size_of_regd, GFP_KERNEL);
1023 if (!regd)
1024 return -ENOMEM;
1025
1026 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1027
1028 for (i = 0; i < src_regd->n_reg_rules; i++)
1029 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1030 sizeof(struct ieee80211_reg_rule));
1031
1032 *dst_regd = regd;
1033 return 0;
1034}
b2e1b302 1035
9c96477d
LR
1036/* Return value which can be used by ignore_request() to indicate
1037 * it has been determined we should intersect two regulatory domains */
1038#define REG_INTERSECT 1
1039
84fa4f43
JB
1040/* This has the logic which determines when a new request
1041 * should be ignored. */
1042static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
1043 const char *alpha2)
1044{
1045 /* All initial requests are respected */
1046 if (!last_request)
1047 return 0;
1048
1049 switch (set_by) {
1050 case REGDOM_SET_BY_INIT:
1051 return -EINVAL;
1052 case REGDOM_SET_BY_CORE:
1053 /*
1054 * Always respect new wireless core hints, should only happen
1055 * when updating the world regulatory domain at init.
1056 */
1057 return 0;
1058 case REGDOM_SET_BY_COUNTRY_IE:
1059 if (unlikely(!is_an_alpha2(alpha2)))
1060 return -EINVAL;
1061 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1062 if (last_request->wiphy != wiphy) {
1063 /*
1064 * Two cards with two APs claiming different
1065 * different Country IE alpha2s. We could
1066 * intersect them, but that seems unlikely
1067 * to be correct. Reject second one for now.
1068 */
1069 if (!alpha2_equal(alpha2,
1070 cfg80211_regdomain->alpha2))
1071 return -EOPNOTSUPP;
1072 return -EALREADY;
1073 }
3f2355cb
LR
1074 /* Two consecutive Country IE hints on the same wiphy.
1075 * This should be picked up early by the driver/stack */
1076 if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
1077 alpha2)))
84fa4f43
JB
1078 return 0;
1079 return -EALREADY;
1080 }
3f2355cb 1081 return REG_INTERSECT;
84fa4f43 1082 case REGDOM_SET_BY_DRIVER:
e74b1e7f
LR
1083 if (last_request->initiator == REGDOM_SET_BY_CORE) {
1084 if (is_old_static_regdom(cfg80211_regdomain))
1085 return 0;
1086 if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
1087 return 0;
84fa4f43 1088 return -EALREADY;
e74b1e7f 1089 }
3e0c3ff3 1090 return REG_INTERSECT;
84fa4f43 1091 case REGDOM_SET_BY_USER:
84fa4f43 1092 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
9c96477d 1093 return REG_INTERSECT;
3f2355cb
LR
1094 /* If the user knows better the user should set the regdom
1095 * to their country before the IE is picked up */
1096 if (last_request->initiator == REGDOM_SET_BY_USER &&
1097 last_request->intersect)
1098 return -EOPNOTSUPP;
5eebade6
LR
1099 /* Process user requests only after previous user/driver/core
1100 * requests have been processed */
1101 if (last_request->initiator == REGDOM_SET_BY_CORE ||
1102 last_request->initiator == REGDOM_SET_BY_DRIVER ||
1103 last_request->initiator == REGDOM_SET_BY_USER) {
1104 if (!alpha2_equal(last_request->alpha2,
1105 cfg80211_regdomain->alpha2))
1106 return -EAGAIN;
1107 }
1108
e74b1e7f
LR
1109 if (!is_old_static_regdom(cfg80211_regdomain) &&
1110 alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
1111 return -EALREADY;
1112
84fa4f43
JB
1113 return 0;
1114 }
1115
1116 return -EINVAL;
1117}
1118
b2e1b302
LR
1119/* Caller must hold &cfg80211_drv_mutex */
1120int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
3f2355cb
LR
1121 const char *alpha2,
1122 u32 country_ie_checksum,
1123 enum environment_cap env)
b2e1b302
LR
1124{
1125 struct regulatory_request *request;
9c96477d 1126 bool intersect = false;
b2e1b302
LR
1127 int r = 0;
1128
be3d4810 1129 r = ignore_request(wiphy, set_by, alpha2);
9c96477d 1130
3e0c3ff3
LR
1131 if (r == REG_INTERSECT) {
1132 if (set_by == REGDOM_SET_BY_DRIVER) {
1133 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1134 if (r)
1135 return r;
1136 }
9c96477d 1137 intersect = true;
3e0c3ff3
LR
1138 } else if (r) {
1139 /* If the regulatory domain being requested by the
1140 * driver has already been set just copy it to the
1141 * wiphy */
1142 if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
1143 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1144 if (r)
1145 return r;
1146 r = -EALREADY;
1147 goto new_request;
1148 }
b2e1b302 1149 return r;
3e0c3ff3 1150 }
b2e1b302 1151
3e0c3ff3 1152new_request:
5203cdb6
LR
1153 request = kzalloc(sizeof(struct regulatory_request),
1154 GFP_KERNEL);
1155 if (!request)
1156 return -ENOMEM;
1157
1158 request->alpha2[0] = alpha2[0];
1159 request->alpha2[1] = alpha2[1];
1160 request->initiator = set_by;
1161 request->wiphy = wiphy;
1162 request->intersect = intersect;
3f2355cb
LR
1163 request->country_ie_checksum = country_ie_checksum;
1164 request->country_ie_env = env;
5203cdb6
LR
1165
1166 kfree(last_request);
1167 last_request = request;
3e0c3ff3
LR
1168
1169 /* When r == REG_INTERSECT we do need to call CRDA */
1170 if (r < 0)
1171 return r;
1172
3f2355cb
LR
1173 /*
1174 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1175 * AND if CRDA is NOT present nothing will happen, if someone
1176 * wants to bother with 11d with OLD_REG you can add a timer.
1177 * If after x amount of time nothing happens you can call:
1178 *
1179 * return set_regdom(country_ie_regdomain);
1180 *
1181 * to intersect with the static rd
1182 */
02ba0b32 1183 return call_crda(alpha2);
b2e1b302
LR
1184}
1185
be3d4810 1186void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
b2e1b302 1187{
f976376d 1188 int r;
be3d4810 1189 BUG_ON(!alpha2);
b2e1b302
LR
1190
1191 mutex_lock(&cfg80211_drv_mutex);
f976376d
LR
1192 r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER,
1193 alpha2, 0, ENVIRON_ANY);
1194 /* This is required so that the orig_* parameters are saved */
1195 if (r == -EALREADY && wiphy->strict_regulatory)
1196 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_DRIVER);
b2e1b302 1197 mutex_unlock(&cfg80211_drv_mutex);
b2e1b302
LR
1198}
1199EXPORT_SYMBOL(regulatory_hint);
1200
3f2355cb
LR
1201static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1202 u32 country_ie_checksum)
1203{
1204 if (!last_request->wiphy)
1205 return false;
1206 if (likely(last_request->wiphy != wiphy))
1207 return !country_ie_integrity_changes(country_ie_checksum);
1208 /* We should not have let these through at this point, they
1209 * should have been picked up earlier by the first alpha2 check
1210 * on the device */
1211 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1212 return true;
1213 return false;
1214}
1215
1216void regulatory_hint_11d(struct wiphy *wiphy,
1217 u8 *country_ie,
1218 u8 country_ie_len)
1219{
1220 struct ieee80211_regdomain *rd = NULL;
1221 char alpha2[2];
1222 u32 checksum = 0;
1223 enum environment_cap env = ENVIRON_ANY;
1224
0f70f398
JB
1225 if (!last_request)
1226 return;
1227
3f2355cb
LR
1228 mutex_lock(&cfg80211_drv_mutex);
1229
1230 /* IE len must be evenly divisible by 2 */
1231 if (country_ie_len & 0x01)
1232 goto out;
1233
1234 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1235 goto out;
1236
1237 /* Pending country IE processing, this can happen after we
1238 * call CRDA and wait for a response if a beacon was received before
1239 * we were able to process the last regulatory_hint_11d() call */
1240 if (country_ie_regdomain)
1241 goto out;
1242
1243 alpha2[0] = country_ie[0];
1244 alpha2[1] = country_ie[1];
1245
1246 if (country_ie[2] == 'I')
1247 env = ENVIRON_INDOOR;
1248 else if (country_ie[2] == 'O')
1249 env = ENVIRON_OUTDOOR;
1250
1251 /* We will run this for *every* beacon processed for the BSSID, so
1252 * we optimize an early check to exit out early if we don't have to
1253 * do anything */
1254 if (likely(last_request->wiphy)) {
1255 struct cfg80211_registered_device *drv_last_ie;
1256
1257 drv_last_ie = wiphy_to_dev(last_request->wiphy);
1258
1259 /* Lets keep this simple -- we trust the first AP
1260 * after we intersect with CRDA */
1261 if (likely(last_request->wiphy == wiphy)) {
1262 /* Ignore IEs coming in on this wiphy with
1263 * the same alpha2 and environment cap */
1264 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1265 alpha2) &&
1266 env == drv_last_ie->env)) {
1267 goto out;
1268 }
1269 /* the wiphy moved on to another BSSID or the AP
1270 * was reconfigured. XXX: We need to deal with the
1271 * case where the user suspends and goes to goes
1272 * to another country, and then gets IEs from an
1273 * AP with different settings */
1274 goto out;
1275 } else {
1276 /* Ignore IEs coming in on two separate wiphys with
1277 * the same alpha2 and environment cap */
1278 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1279 alpha2) &&
1280 env == drv_last_ie->env)) {
1281 goto out;
1282 }
1283 /* We could potentially intersect though */
1284 goto out;
1285 }
1286 }
1287
1288 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1289 if (!rd)
1290 goto out;
1291
1292 /* This will not happen right now but we leave it here for the
1293 * the future when we want to add suspend/resume support and having
1294 * the user move to another country after doing so, or having the user
1295 * move to another AP. Right now we just trust the first AP. This is why
1296 * this is marked as likley(). If we hit this before we add this support
1297 * we want to be informed of it as it would indicate a mistake in the
1298 * current design */
1299 if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
1300 goto out;
1301
1302 /* We keep this around for when CRDA comes back with a response so
1303 * we can intersect with that */
1304 country_ie_regdomain = rd;
1305
1306 __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
1307 country_ie_regdomain->alpha2, checksum, env);
1308
1309out:
1310 mutex_unlock(&cfg80211_drv_mutex);
1311}
1312EXPORT_SYMBOL(regulatory_hint_11d);
b2e1b302 1313
a3d2eaf0 1314static void print_rd_rules(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1315{
1316 unsigned int i;
a3d2eaf0
JB
1317 const struct ieee80211_reg_rule *reg_rule = NULL;
1318 const struct ieee80211_freq_range *freq_range = NULL;
1319 const struct ieee80211_power_rule *power_rule = NULL;
b2e1b302
LR
1320
1321 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1322 "(max_antenna_gain, max_eirp)\n");
1323
1324 for (i = 0; i < rd->n_reg_rules; i++) {
1325 reg_rule = &rd->reg_rules[i];
1326 freq_range = &reg_rule->freq_range;
1327 power_rule = &reg_rule->power_rule;
1328
1329 /* There may not be documentation for max antenna gain
1330 * in certain regions */
1331 if (power_rule->max_antenna_gain)
1332 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1333 "(%d mBi, %d mBm)\n",
1334 freq_range->start_freq_khz,
1335 freq_range->end_freq_khz,
1336 freq_range->max_bandwidth_khz,
1337 power_rule->max_antenna_gain,
1338 power_rule->max_eirp);
1339 else
1340 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1341 "(N/A, %d mBm)\n",
1342 freq_range->start_freq_khz,
1343 freq_range->end_freq_khz,
1344 freq_range->max_bandwidth_khz,
1345 power_rule->max_eirp);
1346 }
1347}
1348
a3d2eaf0 1349static void print_regdomain(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1350{
1351
3f2355cb
LR
1352 if (is_intersected_alpha2(rd->alpha2)) {
1353 struct wiphy *wiphy = NULL;
1354 struct cfg80211_registered_device *drv;
1355
1356 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1357 if (last_request->wiphy) {
1358 wiphy = last_request->wiphy;
1359 drv = wiphy_to_dev(wiphy);
1360 printk(KERN_INFO "cfg80211: Current regulatory "
1361 "domain updated by AP to: %c%c\n",
1362 drv->country_ie_alpha2[0],
1363 drv->country_ie_alpha2[1]);
1364 } else
1365 printk(KERN_INFO "cfg80211: Current regulatory "
1366 "domain intersected: \n");
1367 } else
1368 printk(KERN_INFO "cfg80211: Current regulatory "
039498c6 1369 "domain intersected: \n");
3f2355cb 1370 } else if (is_world_regdom(rd->alpha2))
b2e1b302
LR
1371 printk(KERN_INFO "cfg80211: World regulatory "
1372 "domain updated:\n");
1373 else {
1374 if (is_unknown_alpha2(rd->alpha2))
1375 printk(KERN_INFO "cfg80211: Regulatory domain "
1376 "changed to driver built-in settings "
1377 "(unknown country)\n");
1378 else
1379 printk(KERN_INFO "cfg80211: Regulatory domain "
1380 "changed to country: %c%c\n",
1381 rd->alpha2[0], rd->alpha2[1]);
1382 }
1383 print_rd_rules(rd);
1384}
1385
2df78167 1386static void print_regdomain_info(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1387{
1388 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1389 rd->alpha2[0], rd->alpha2[1]);
1390 print_rd_rules(rd);
1391}
1392
3f2355cb
LR
1393#ifdef CONFIG_CFG80211_REG_DEBUG
1394static void reg_country_ie_process_debug(
1395 const struct ieee80211_regdomain *rd,
1396 const struct ieee80211_regdomain *country_ie_regdomain,
1397 const struct ieee80211_regdomain *intersected_rd)
1398{
1399 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1400 print_regdomain_info(country_ie_regdomain);
1401 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1402 print_regdomain_info(rd);
1403 if (intersected_rd) {
1404 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1405 "and get:\n");
667ecd01 1406 print_regdomain_info(intersected_rd);
3f2355cb
LR
1407 return;
1408 }
1409 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1410}
1411#else
1412static inline void reg_country_ie_process_debug(
1413 const struct ieee80211_regdomain *rd,
1414 const struct ieee80211_regdomain *country_ie_regdomain,
1415 const struct ieee80211_regdomain *intersected_rd)
1416{
1417}
1418#endif
1419
d2372b31 1420/* Takes ownership of rd only if it doesn't fail */
a3d2eaf0 1421static int __set_regdom(const struct ieee80211_regdomain *rd)
b2e1b302 1422{
9c96477d 1423 const struct ieee80211_regdomain *intersected_rd = NULL;
3f2355cb
LR
1424 struct cfg80211_registered_device *drv = NULL;
1425 struct wiphy *wiphy = NULL;
b2e1b302
LR
1426 /* Some basic sanity checks first */
1427
b2e1b302 1428 if (is_world_regdom(rd->alpha2)) {
f6037d09 1429 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
b2e1b302
LR
1430 return -EINVAL;
1431 update_world_regdomain(rd);
1432 return 0;
1433 }
b2e1b302
LR
1434
1435 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1436 !is_unknown_alpha2(rd->alpha2))
1437 return -EINVAL;
1438
f6037d09 1439 if (!last_request)
b2e1b302
LR
1440 return -EINVAL;
1441
3f2355cb
LR
1442 /* Lets only bother proceeding on the same alpha2 if the current
1443 * rd is non static (it means CRDA was present and was used last)
1444 * and the pending request came in from a country IE */
1445 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1446 /* If someone else asked us to change the rd lets only bother
1447 * checking if the alpha2 changes if CRDA was already called */
1448 if (!is_old_static_regdom(cfg80211_regdomain) &&
1449 !regdom_changed(rd->alpha2))
1450 return -EINVAL;
1451 }
1452
1453 wiphy = last_request->wiphy;
b2e1b302
LR
1454
1455 /* Now lets set the regulatory domain, update all driver channels
1456 * and finally inform them of what we have done, in case they want
1457 * to review or adjust their own settings based on their own
1458 * internal EEPROM data */
1459
f6037d09 1460 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
b2e1b302
LR
1461 return -EINVAL;
1462
8375af3b
LR
1463 if (!is_valid_rd(rd)) {
1464 printk(KERN_ERR "cfg80211: Invalid "
1465 "regulatory domain detected:\n");
1466 print_regdomain_info(rd);
1467 return -EINVAL;
b2e1b302
LR
1468 }
1469
b8295acd 1470 if (!last_request->intersect) {
3e0c3ff3
LR
1471 int r;
1472
1473 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1474 reset_regdomains();
1475 cfg80211_regdomain = rd;
1476 return 0;
1477 }
1478
1479 /* For a driver hint, lets copy the regulatory domain the
1480 * driver wanted to the wiphy to deal with conflicts */
1481
1482 BUG_ON(last_request->wiphy->regd);
1483
1484 r = reg_copy_regd(&last_request->wiphy->regd, rd);
1485 if (r)
1486 return r;
1487
b8295acd
LR
1488 reset_regdomains();
1489 cfg80211_regdomain = rd;
1490 return 0;
1491 }
1492
1493 /* Intersection requires a bit more work */
1494
1495 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1496
9c96477d
LR
1497 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1498 if (!intersected_rd)
1499 return -EINVAL;
b8295acd 1500
3e0c3ff3
LR
1501 /* We can trash what CRDA provided now.
1502 * However if a driver requested this specific regulatory
1503 * domain we keep it for its private use */
1504 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
1505 last_request->wiphy->regd = rd;
1506 else
1507 kfree(rd);
1508
b8295acd
LR
1509 rd = NULL;
1510
1511 reset_regdomains();
1512 cfg80211_regdomain = intersected_rd;
1513
1514 return 0;
9c96477d
LR
1515 }
1516
3f2355cb
LR
1517 /*
1518 * Country IE requests are handled a bit differently, we intersect
1519 * the country IE rd with what CRDA believes that country should have
1520 */
1521
1522 BUG_ON(!country_ie_regdomain);
1523
1524 if (rd != country_ie_regdomain) {
1525 /* Intersect what CRDA returned and our what we
1526 * had built from the Country IE received */
1527
1528 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1529
1530 reg_country_ie_process_debug(rd, country_ie_regdomain,
1531 intersected_rd);
1532
1533 kfree(country_ie_regdomain);
1534 country_ie_regdomain = NULL;
1535 } else {
1536 /* This would happen when CRDA was not present and
1537 * OLD_REGULATORY was enabled. We intersect our Country
1538 * IE rd and what was set on cfg80211 originally */
1539 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1540 }
1541
1542 if (!intersected_rd)
1543 return -EINVAL;
1544
1545 drv = wiphy_to_dev(wiphy);
1546
1547 drv->country_ie_alpha2[0] = rd->alpha2[0];
1548 drv->country_ie_alpha2[1] = rd->alpha2[1];
1549 drv->env = last_request->country_ie_env;
1550
1551 BUG_ON(intersected_rd == rd);
1552
1553 kfree(rd);
1554 rd = NULL;
1555
b8295acd 1556 reset_regdomains();
3f2355cb 1557 cfg80211_regdomain = intersected_rd;
b2e1b302
LR
1558
1559 return 0;
1560}
1561
1562
1563/* Use this call to set the current regulatory domain. Conflicts with
1564 * multiple drivers can be ironed out later. Caller must've already
d2372b31 1565 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
a3d2eaf0 1566int set_regdom(const struct ieee80211_regdomain *rd)
b2e1b302 1567{
b2e1b302
LR
1568 int r;
1569
b2e1b302
LR
1570 /* Note that this doesn't update the wiphys, this is done below */
1571 r = __set_regdom(rd);
d2372b31
JB
1572 if (r) {
1573 kfree(rd);
b2e1b302 1574 return r;
d2372b31 1575 }
b2e1b302 1576
b2e1b302 1577 /* This would make this whole thing pointless */
a01ddafd
LR
1578 if (!last_request->intersect)
1579 BUG_ON(rd != cfg80211_regdomain);
b2e1b302
LR
1580
1581 /* update all wiphys now with the new established regulatory domain */
f6037d09 1582 update_all_wiphy_regulatory(last_request->initiator);
b2e1b302 1583
a01ddafd 1584 print_regdomain(cfg80211_regdomain);
b2e1b302
LR
1585
1586 return r;
1587}
1588
3f2355cb
LR
1589/* Caller must hold cfg80211_drv_mutex */
1590void reg_device_remove(struct wiphy *wiphy)
1591{
3e0c3ff3 1592 kfree(wiphy->regd);
4a4f4d80 1593 if (!last_request || !last_request->wiphy)
3f2355cb
LR
1594 return;
1595 if (last_request->wiphy != wiphy)
1596 return;
1597 last_request->wiphy = NULL;
1598 last_request->country_ie_env = ENVIRON_ANY;
1599}
1600
b2e1b302
LR
1601int regulatory_init(void)
1602{
734366de
JB
1603 int err;
1604
b2e1b302
LR
1605 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1606 if (IS_ERR(reg_pdev))
1607 return PTR_ERR(reg_pdev);
734366de
JB
1608
1609#ifdef CONFIG_WIRELESS_OLD_REGULATORY
a3d2eaf0 1610 cfg80211_regdomain = static_regdom(ieee80211_regdom);
734366de 1611
942b25cf 1612 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
734366de
JB
1613 print_regdomain_info(cfg80211_regdomain);
1614 /* The old code still requests for a new regdomain and if
1615 * you have CRDA you get it updated, otherwise you get
1616 * stuck with the static values. We ignore "EU" code as
1617 * that is not a valid ISO / IEC 3166 alpha2 */
ac9440a4 1618 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
734366de 1619 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
3f2355cb 1620 ieee80211_regdom, 0, ENVIRON_ANY);
734366de 1621#else
a3d2eaf0 1622 cfg80211_regdomain = cfg80211_world_regdom;
734366de 1623
3f2355cb 1624 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
734366de
JB
1625 if (err)
1626 printk(KERN_ERR "cfg80211: calling CRDA failed - "
1627 "unable to update world regulatory domain, "
1628 "using static definition\n");
1629#endif
1630
b2e1b302
LR
1631 return 0;
1632}
1633
1634void regulatory_exit(void)
1635{
b2e1b302 1636 mutex_lock(&cfg80211_drv_mutex);
734366de 1637
b2e1b302 1638 reset_regdomains();
734366de 1639
3f2355cb
LR
1640 kfree(country_ie_regdomain);
1641 country_ie_regdomain = NULL;
1642
f6037d09
JB
1643 kfree(last_request);
1644
b2e1b302 1645 platform_device_unregister(reg_pdev);
734366de 1646
b2e1b302 1647 mutex_unlock(&cfg80211_drv_mutex);
8318d78a 1648}