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