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