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