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