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