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