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