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