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