1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 "nl80211.h"
44
45
46static struct regulatory_request *last_request;
47
48
49static struct platform_device *reg_pdev;
50
51
52
53
54
55
56const struct ieee80211_regdomain *cfg80211_regdomain;
57
58
59
60
61
62
63static const struct ieee80211_regdomain *country_ie_regdomain;
64
65
66
67
68
69
70
71
72DEFINE_MUTEX(reg_mutex);
73#define assert_reg_lock() WARN_ON(!mutex_is_locked(®_mutex))
74
75
76static LIST_HEAD(reg_requests_list);
77static spinlock_t reg_requests_lock;
78
79
80static LIST_HEAD(reg_pending_beacons);
81static spinlock_t reg_pending_beacons_lock;
82
83
84static LIST_HEAD(reg_beacon_list);
85
86struct reg_beacon {
87 struct list_head list;
88 struct ieee80211_channel chan;
89};
90
91
92static const struct ieee80211_regdomain world_regdom = {
93 .n_reg_rules = 5,
94 .alpha2 = "00",
95 .reg_rules = {
96
97 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
98
99
100 REG_RULE(2467-10, 2472+10, 20, 6, 20,
101 NL80211_RRF_PASSIVE_SCAN |
102 NL80211_RRF_NO_IBSS),
103
104
105 REG_RULE(2484-10, 2484+10, 20, 6, 20,
106 NL80211_RRF_PASSIVE_SCAN |
107 NL80211_RRF_NO_IBSS |
108 NL80211_RRF_NO_OFDM),
109
110 REG_RULE(5180-10, 5240+10, 40, 6, 20,
111 NL80211_RRF_PASSIVE_SCAN |
112 NL80211_RRF_NO_IBSS),
113
114
115
116
117 REG_RULE(5745-10, 5825+10, 40, 6, 20,
118 NL80211_RRF_PASSIVE_SCAN |
119 NL80211_RRF_NO_IBSS),
120 }
121};
122
123static const struct ieee80211_regdomain *cfg80211_world_regdom =
124 &world_regdom;
125
126static char *ieee80211_regdom = "00";
127
128module_param(ieee80211_regdom, charp, 0444);
129MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
130
131#ifdef CONFIG_WIRELESS_OLD_REGULATORY
132
133
134
135
136
137
138static const struct ieee80211_regdomain us_regdom = {
139 .n_reg_rules = 6,
140 .alpha2 = "US",
141 .reg_rules = {
142
143 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
144
145 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
146
147 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
148
149 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
150
151 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
152
153 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
154 }
155};
156
157static const struct ieee80211_regdomain jp_regdom = {
158 .n_reg_rules = 3,
159 .alpha2 = "JP",
160 .reg_rules = {
161
162 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
163
164 REG_RULE(5170-10, 5240+10, 40, 6, 20,
165 NL80211_RRF_PASSIVE_SCAN),
166
167 REG_RULE(5260-10, 5320+10, 40, 6, 20,
168 NL80211_RRF_NO_IBSS |
169 NL80211_RRF_DFS),
170 }
171};
172
173static const struct ieee80211_regdomain eu_regdom = {
174 .n_reg_rules = 6,
175
176
177
178
179 .alpha2 = "EU",
180 .reg_rules = {
181
182 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
183
184 REG_RULE(5180-10, 5180+10, 40, 6, 23,
185 NL80211_RRF_PASSIVE_SCAN),
186
187 REG_RULE(5200-10, 5200+10, 40, 6, 23,
188 NL80211_RRF_PASSIVE_SCAN),
189
190 REG_RULE(5220-10, 5220+10, 40, 6, 23,
191 NL80211_RRF_PASSIVE_SCAN),
192
193 REG_RULE(5240-10, 5320+10, 40, 6, 20,
194 NL80211_RRF_NO_IBSS |
195 NL80211_RRF_DFS),
196
197 REG_RULE(5500-10, 5700+10, 40, 6, 30,
198 NL80211_RRF_NO_IBSS |
199 NL80211_RRF_DFS),
200 }
201};
202
203static const struct ieee80211_regdomain *static_regdom(char *alpha2)
204{
205 if (alpha2[0] == 'U' && alpha2[1] == 'S')
206 return &us_regdom;
207 if (alpha2[0] == 'J' && alpha2[1] == 'P')
208 return &jp_regdom;
209 if (alpha2[0] == 'E' && alpha2[1] == 'U')
210 return &eu_regdom;
211
212 return &us_regdom;
213}
214
215static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
216{
217 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
218 return true;
219 return false;
220}
221#else
222static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
223{
224 return false;
225}
226#endif
227
228static void reset_regdomains(void)
229{
230
231 if (cfg80211_regdomain == cfg80211_world_regdom)
232 cfg80211_regdomain = NULL;
233 if (cfg80211_world_regdom == &world_regdom)
234 cfg80211_world_regdom = NULL;
235 if (cfg80211_regdomain == &world_regdom)
236 cfg80211_regdomain = NULL;
237 if (is_old_static_regdom(cfg80211_regdomain))
238 cfg80211_regdomain = NULL;
239
240 kfree(cfg80211_regdomain);
241 kfree(cfg80211_world_regdom);
242
243 cfg80211_world_regdom = &world_regdom;
244 cfg80211_regdomain = NULL;
245}
246
247
248
249
250
251static void update_world_regdomain(const struct ieee80211_regdomain *rd)
252{
253 BUG_ON(!last_request);
254
255 reset_regdomains();
256
257 cfg80211_world_regdom = rd;
258 cfg80211_regdomain = rd;
259}
260
261bool is_world_regdom(const char *alpha2)
262{
263 if (!alpha2)
264 return false;
265 if (alpha2[0] == '0' && alpha2[1] == '0')
266 return true;
267 return false;
268}
269
270static bool is_alpha2_set(const char *alpha2)
271{
272 if (!alpha2)
273 return false;
274 if (alpha2[0] != 0 && alpha2[1] != 0)
275 return true;
276 return false;
277}
278
279static bool is_alpha_upper(char letter)
280{
281
282 if (letter >= 65 && letter <= 90)
283 return true;
284 return false;
285}
286
287static bool is_unknown_alpha2(const char *alpha2)
288{
289 if (!alpha2)
290 return false;
291
292
293
294
295 if (alpha2[0] == '9' && alpha2[1] == '9')
296 return true;
297 return false;
298}
299
300static bool is_intersected_alpha2(const char *alpha2)
301{
302 if (!alpha2)
303 return false;
304
305
306
307
308
309 if (alpha2[0] == '9' && alpha2[1] == '8')
310 return true;
311 return false;
312}
313
314static bool is_an_alpha2(const char *alpha2)
315{
316 if (!alpha2)
317 return false;
318 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
319 return true;
320 return false;
321}
322
323static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
324{
325 if (!alpha2_x || !alpha2_y)
326 return false;
327 if (alpha2_x[0] == alpha2_y[0] &&
328 alpha2_x[1] == alpha2_y[1])
329 return true;
330 return false;
331}
332
333static bool regdom_changes(const char *alpha2)
334{
335 assert_cfg80211_lock();
336
337 if (!cfg80211_regdomain)
338 return true;
339 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
340 return false;
341 return true;
342}
343
344
345
346
347
348
349
350
351
352
353static bool country_ie_integrity_changes(u32 checksum)
354{
355
356 if (unlikely(!last_request->country_ie_checksum))
357 return false;
358 if (unlikely(last_request->country_ie_checksum != checksum))
359 return true;
360 return false;
361}
362
363
364
365
366
367static int call_crda(const char *alpha2)
368{
369 char country_env[9 + 2] = "COUNTRY=";
370 char *envp[] = {
371 country_env,
372 NULL
373 };
374
375 if (!is_world_regdom((char *) alpha2))
376 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
377 alpha2[0], alpha2[1]);
378 else
379 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
380 "regulatory domain\n");
381
382 country_env[8] = alpha2[0];
383 country_env[9] = alpha2[1];
384
385 return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp);
386}
387
388
389bool reg_is_valid_request(const char *alpha2)
390{
391 assert_cfg80211_lock();
392
393 if (!last_request)
394 return false;
395
396 return alpha2_equal(last_request->alpha2, alpha2);
397}
398
399
400static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
401{
402 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
403 u32 freq_diff;
404
405 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
406 return false;
407
408 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
409 return false;
410
411 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
412
413 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
414 freq_range->max_bandwidth_khz > freq_diff)
415 return false;
416
417 return true;
418}
419
420static bool is_valid_rd(const struct ieee80211_regdomain *rd)
421{
422 const struct ieee80211_reg_rule *reg_rule = NULL;
423 unsigned int i;
424
425 if (!rd->n_reg_rules)
426 return false;
427
428 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
429 return false;
430
431 for (i = 0; i < rd->n_reg_rules; i++) {
432 reg_rule = &rd->reg_rules[i];
433 if (!is_valid_reg_rule(reg_rule))
434 return false;
435 }
436
437 return true;
438}
439
440static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
441 u32 center_freq_khz,
442 u32 bw_khz)
443{
444 u32 start_freq_khz, end_freq_khz;
445
446 start_freq_khz = center_freq_khz - (bw_khz/2);
447 end_freq_khz = center_freq_khz + (bw_khz/2);
448
449 if (start_freq_khz >= freq_range->start_freq_khz &&
450 end_freq_khz <= freq_range->end_freq_khz)
451 return true;
452
453 return false;
454}
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
470 u32 freq_khz)
471{
472#define ONE_GHZ_IN_KHZ 1000000
473 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
474 return true;
475 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
476 return true;
477 return false;
478#undef ONE_GHZ_IN_KHZ
479}
480
481
482
483
484
485
486
487static struct ieee80211_regdomain *country_ie_2_rd(
488 u8 *country_ie,
489 u8 country_ie_len,
490 u32 *checksum)
491{
492 struct ieee80211_regdomain *rd = NULL;
493 unsigned int i = 0;
494 char alpha2[2];
495 u32 flags = 0;
496 u32 num_rules = 0, size_of_regd = 0;
497 u8 *triplets_start = NULL;
498 u8 len_at_triplet = 0;
499
500 int last_sub_max_channel = 0;
501
502 *checksum = 0xDEADBEEF;
503
504
505 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
506 country_ie_len & 0x01);
507
508 alpha2[0] = country_ie[0];
509 alpha2[1] = country_ie[1];
510
511
512
513
514
515
516
517
518 if (country_ie[2] == 'I')
519 flags = NL80211_RRF_NO_OUTDOOR;
520 else if (country_ie[2] == 'O')
521 flags = NL80211_RRF_NO_INDOOR;
522
523 country_ie += 3;
524 country_ie_len -= 3;
525
526 triplets_start = country_ie;
527 len_at_triplet = country_ie_len;
528
529 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
530
531
532
533
534
535
536 while (country_ie_len >= 3) {
537 int end_channel = 0;
538 struct ieee80211_country_ie_triplet *triplet =
539 (struct ieee80211_country_ie_triplet *) country_ie;
540 int cur_sub_max_channel = 0, cur_channel = 0;
541
542 if (triplet->ext.reg_extension_id >=
543 IEEE80211_COUNTRY_EXTENSION_ID) {
544 country_ie += 3;
545 country_ie_len -= 3;
546 continue;
547 }
548
549
550 if (triplet->chans.first_channel <= 14)
551 end_channel = triplet->chans.first_channel +
552 triplet->chans.num_channels;
553 else
554
555
556
557
558
559
560
561
562
563 end_channel = triplet->chans.first_channel +
564 (4 * (triplet->chans.num_channels - 1));
565
566 cur_channel = triplet->chans.first_channel;
567 cur_sub_max_channel = end_channel;
568
569
570 if (cur_sub_max_channel < cur_channel)
571 return NULL;
572
573
574
575
576
577
578 if (last_sub_max_channel) {
579 if (cur_channel <= last_sub_max_channel)
580 return NULL;
581 if (cur_sub_max_channel <= last_sub_max_channel)
582 return NULL;
583 }
584
585
586
587
588
589
590
591 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
592 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
593 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
594
595 last_sub_max_channel = cur_sub_max_channel;
596
597 country_ie += 3;
598 country_ie_len -= 3;
599 num_rules++;
600
601
602
603
604
605 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
606 return NULL;
607 }
608
609 country_ie = triplets_start;
610 country_ie_len = len_at_triplet;
611
612 size_of_regd = sizeof(struct ieee80211_regdomain) +
613 (num_rules * sizeof(struct ieee80211_reg_rule));
614
615 rd = kzalloc(size_of_regd, GFP_KERNEL);
616 if (!rd)
617 return NULL;
618
619 rd->n_reg_rules = num_rules;
620 rd->alpha2[0] = alpha2[0];
621 rd->alpha2[1] = alpha2[1];
622
623
624 while (country_ie_len >= 3) {
625 int end_channel = 0;
626 struct ieee80211_country_ie_triplet *triplet =
627 (struct ieee80211_country_ie_triplet *) country_ie;
628 struct ieee80211_reg_rule *reg_rule = NULL;
629 struct ieee80211_freq_range *freq_range = NULL;
630 struct ieee80211_power_rule *power_rule = NULL;
631
632
633
634
635
636 if (triplet->ext.reg_extension_id >=
637 IEEE80211_COUNTRY_EXTENSION_ID) {
638 country_ie += 3;
639 country_ie_len -= 3;
640 continue;
641 }
642
643 reg_rule = &rd->reg_rules[i];
644 freq_range = ®_rule->freq_range;
645 power_rule = ®_rule->power_rule;
646
647 reg_rule->flags = flags;
648
649
650 if (triplet->chans.first_channel <= 14)
651 end_channel = triplet->chans.first_channel +
652 triplet->chans.num_channels;
653 else
654 end_channel = triplet->chans.first_channel +
655 (4 * (triplet->chans.num_channels - 1));
656
657
658
659
660
661
662
663 freq_range->start_freq_khz =
664 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
665 triplet->chans.first_channel) - 10);
666 freq_range->end_freq_khz =
667 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
668 end_channel) + 10);
669
670
671
672
673
674
675 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
676 power_rule->max_antenna_gain = DBI_TO_MBI(100);
677 power_rule->max_eirp = DBM_TO_MBM(100);
678
679 country_ie += 3;
680 country_ie_len -= 3;
681 i++;
682
683 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
684 }
685
686 return rd;
687}
688
689
690
691
692
693
694static int reg_rules_intersect(
695 const struct ieee80211_reg_rule *rule1,
696 const struct ieee80211_reg_rule *rule2,
697 struct ieee80211_reg_rule *intersected_rule)
698{
699 const struct ieee80211_freq_range *freq_range1, *freq_range2;
700 struct ieee80211_freq_range *freq_range;
701 const struct ieee80211_power_rule *power_rule1, *power_rule2;
702 struct ieee80211_power_rule *power_rule;
703 u32 freq_diff;
704
705 freq_range1 = &rule1->freq_range;
706 freq_range2 = &rule2->freq_range;
707 freq_range = &intersected_rule->freq_range;
708
709 power_rule1 = &rule1->power_rule;
710 power_rule2 = &rule2->power_rule;
711 power_rule = &intersected_rule->power_rule;
712
713 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
714 freq_range2->start_freq_khz);
715 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
716 freq_range2->end_freq_khz);
717 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
718 freq_range2->max_bandwidth_khz);
719
720 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
721 if (freq_range->max_bandwidth_khz > freq_diff)
722 freq_range->max_bandwidth_khz = freq_diff;
723
724 power_rule->max_eirp = min(power_rule1->max_eirp,
725 power_rule2->max_eirp);
726 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
727 power_rule2->max_antenna_gain);
728
729 intersected_rule->flags = (rule1->flags | rule2->flags);
730
731 if (!is_valid_reg_rule(intersected_rule))
732 return -EINVAL;
733
734 return 0;
735}
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750static struct ieee80211_regdomain *regdom_intersect(
751 const struct ieee80211_regdomain *rd1,
752 const struct ieee80211_regdomain *rd2)
753{
754 int r, size_of_regd;
755 unsigned int x, y;
756 unsigned int num_rules = 0, rule_idx = 0;
757 const struct ieee80211_reg_rule *rule1, *rule2;
758 struct ieee80211_reg_rule *intersected_rule;
759 struct ieee80211_regdomain *rd;
760
761 struct ieee80211_reg_rule irule;
762
763
764 intersected_rule = &irule;
765
766 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
767
768 if (!rd1 || !rd2)
769 return NULL;
770
771
772
773
774
775
776
777
778
779 for (x = 0; x < rd1->n_reg_rules; x++) {
780 rule1 = &rd1->reg_rules[x];
781 for (y = 0; y < rd2->n_reg_rules; y++) {
782 rule2 = &rd2->reg_rules[y];
783 if (!reg_rules_intersect(rule1, rule2,
784 intersected_rule))
785 num_rules++;
786 memset(intersected_rule, 0,
787 sizeof(struct ieee80211_reg_rule));
788 }
789 }
790
791 if (!num_rules)
792 return NULL;
793
794 size_of_regd = sizeof(struct ieee80211_regdomain) +
795 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
796
797 rd = kzalloc(size_of_regd, GFP_KERNEL);
798 if (!rd)
799 return NULL;
800
801 for (x = 0; x < rd1->n_reg_rules; x++) {
802 rule1 = &rd1->reg_rules[x];
803 for (y = 0; y < rd2->n_reg_rules; y++) {
804 rule2 = &rd2->reg_rules[y];
805
806
807
808
809
810 intersected_rule = &rd->reg_rules[rule_idx];
811 r = reg_rules_intersect(rule1, rule2,
812 intersected_rule);
813
814
815
816
817 if (r)
818 continue;
819 rule_idx++;
820 }
821 }
822
823 if (rule_idx != num_rules) {
824 kfree(rd);
825 return NULL;
826 }
827
828 rd->n_reg_rules = num_rules;
829 rd->alpha2[0] = '9';
830 rd->alpha2[1] = '8';
831
832 return rd;
833}
834
835
836
837
838
839static u32 map_regdom_flags(u32 rd_flags)
840{
841 u32 channel_flags = 0;
842 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
843 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
844 if (rd_flags & NL80211_RRF_NO_IBSS)
845 channel_flags |= IEEE80211_CHAN_NO_IBSS;
846 if (rd_flags & NL80211_RRF_DFS)
847 channel_flags |= IEEE80211_CHAN_RADAR;
848 return channel_flags;
849}
850
851static int freq_reg_info_regd(struct wiphy *wiphy,
852 u32 center_freq,
853 u32 desired_bw_khz,
854 const struct ieee80211_reg_rule **reg_rule,
855 const struct ieee80211_regdomain *custom_regd)
856{
857 int i;
858 bool band_rule_found = false;
859 const struct ieee80211_regdomain *regd;
860 bool bw_fits = false;
861
862 if (!desired_bw_khz)
863 desired_bw_khz = MHZ_TO_KHZ(20);
864
865 regd = custom_regd ? custom_regd : cfg80211_regdomain;
866
867
868
869
870
871 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
872 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
873 wiphy->regd)
874 regd = wiphy->regd;
875
876 if (!regd)
877 return -EINVAL;
878
879 for (i = 0; i < regd->n_reg_rules; i++) {
880 const struct ieee80211_reg_rule *rr;
881 const struct ieee80211_freq_range *fr = NULL;
882 const struct ieee80211_power_rule *pr = NULL;
883
884 rr = ®d->reg_rules[i];
885 fr = &rr->freq_range;
886 pr = &rr->power_rule;
887
888
889
890
891
892
893 if (!band_rule_found)
894 band_rule_found = freq_in_rule_band(fr, center_freq);
895
896 bw_fits = reg_does_bw_fit(fr,
897 center_freq,
898 desired_bw_khz);
899
900 if (band_rule_found && bw_fits) {
901 *reg_rule = rr;
902 return 0;
903 }
904 }
905
906 if (!band_rule_found)
907 return -ERANGE;
908
909 return -EINVAL;
910}
911EXPORT_SYMBOL(freq_reg_info);
912
913int freq_reg_info(struct wiphy *wiphy,
914 u32 center_freq,
915 u32 desired_bw_khz,
916 const struct ieee80211_reg_rule **reg_rule)
917{
918 assert_cfg80211_lock();
919 return freq_reg_info_regd(wiphy,
920 center_freq,
921 desired_bw_khz,
922 reg_rule,
923 NULL);
924}
925
926
927
928
929
930
931
932
933
934
935static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
936 unsigned int chan_idx)
937{
938 int r;
939 u32 flags, bw_flags = 0;
940 u32 desired_bw_khz = MHZ_TO_KHZ(20);
941 const struct ieee80211_reg_rule *reg_rule = NULL;
942 const struct ieee80211_power_rule *power_rule = NULL;
943 const struct ieee80211_freq_range *freq_range = NULL;
944 struct ieee80211_supported_band *sband;
945 struct ieee80211_channel *chan;
946 struct wiphy *request_wiphy = NULL;
947
948 assert_cfg80211_lock();
949
950 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
951
952 sband = wiphy->bands[band];
953 BUG_ON(chan_idx >= sband->n_channels);
954 chan = &sband->channels[chan_idx];
955
956 flags = chan->orig_flags;
957
958 r = freq_reg_info(wiphy,
959 MHZ_TO_KHZ(chan->center_freq),
960 desired_bw_khz,
961 ®_rule);
962
963 if (r) {
964
965
966
967
968
969
970
971
972
973
974
975 if (r == -ERANGE &&
976 last_request->initiator ==
977 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
978#ifdef CONFIG_CFG80211_REG_DEBUG
979 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
980 "intact on %s - no rule found in band on "
981 "Country IE\n",
982 chan->center_freq, wiphy_name(wiphy));
983#endif
984 } else {
985
986
987
988
989#ifdef CONFIG_CFG80211_REG_DEBUG
990 if (last_request->initiator ==
991 NL80211_REGDOM_SET_BY_COUNTRY_IE)
992 printk(KERN_DEBUG "cfg80211: Disabling "
993 "channel %d MHz on %s due to "
994 "Country IE\n",
995 chan->center_freq, wiphy_name(wiphy));
996#endif
997 flags |= IEEE80211_CHAN_DISABLED;
998 chan->flags = flags;
999 }
1000 return;
1001 }
1002
1003 power_rule = ®_rule->power_rule;
1004 freq_range = ®_rule->freq_range;
1005
1006 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1007 bw_flags = IEEE80211_CHAN_NO_HT40;
1008
1009 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1010 request_wiphy && request_wiphy == wiphy &&
1011 request_wiphy->strict_regulatory) {
1012
1013
1014
1015
1016
1017 chan->flags = chan->orig_flags =
1018 map_regdom_flags(reg_rule->flags) | bw_flags;
1019 chan->max_antenna_gain = chan->orig_mag =
1020 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1021 chan->max_power = chan->orig_mpwr =
1022 (int) MBM_TO_DBM(power_rule->max_eirp);
1023 return;
1024 }
1025
1026 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1027 chan->max_antenna_gain = min(chan->orig_mag,
1028 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
1029 if (chan->orig_mpwr)
1030 chan->max_power = min(chan->orig_mpwr,
1031 (int) MBM_TO_DBM(power_rule->max_eirp));
1032 else
1033 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1034}
1035
1036static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
1037{
1038 unsigned int i;
1039 struct ieee80211_supported_band *sband;
1040
1041 BUG_ON(!wiphy->bands[band]);
1042 sband = wiphy->bands[band];
1043
1044 for (i = 0; i < sband->n_channels; i++)
1045 handle_channel(wiphy, band, i);
1046}
1047
1048static bool ignore_reg_update(struct wiphy *wiphy,
1049 enum nl80211_reg_initiator initiator)
1050{
1051 if (!last_request)
1052 return true;
1053 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
1054 wiphy->custom_regulatory)
1055 return true;
1056
1057
1058
1059
1060 if (wiphy->strict_regulatory && !wiphy->regd &&
1061 !is_world_regdom(last_request->alpha2))
1062 return true;
1063 return false;
1064}
1065
1066static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
1067{
1068 struct cfg80211_registered_device *rdev;
1069
1070 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1071 wiphy_update_regulatory(&rdev->wiphy, initiator);
1072}
1073
1074static void handle_reg_beacon(struct wiphy *wiphy,
1075 unsigned int chan_idx,
1076 struct reg_beacon *reg_beacon)
1077{
1078 struct ieee80211_supported_band *sband;
1079 struct ieee80211_channel *chan;
1080 bool channel_changed = false;
1081 struct ieee80211_channel chan_before;
1082
1083 assert_cfg80211_lock();
1084
1085 sband = wiphy->bands[reg_beacon->chan.band];
1086 chan = &sband->channels[chan_idx];
1087
1088 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1089 return;
1090
1091 if (chan->beacon_found)
1092 return;
1093
1094 chan->beacon_found = true;
1095
1096 if (wiphy->disable_beacon_hints)
1097 return;
1098
1099 chan_before.center_freq = chan->center_freq;
1100 chan_before.flags = chan->flags;
1101
1102 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
1103 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
1104 channel_changed = true;
1105 }
1106
1107 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
1108 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
1109 channel_changed = true;
1110 }
1111
1112 if (channel_changed)
1113 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
1114}
1115
1116
1117
1118
1119
1120static void wiphy_update_new_beacon(struct wiphy *wiphy,
1121 struct reg_beacon *reg_beacon)
1122{
1123 unsigned int i;
1124 struct ieee80211_supported_band *sband;
1125
1126 assert_cfg80211_lock();
1127
1128 if (!wiphy->bands[reg_beacon->chan.band])
1129 return;
1130
1131 sband = wiphy->bands[reg_beacon->chan.band];
1132
1133 for (i = 0; i < sband->n_channels; i++)
1134 handle_reg_beacon(wiphy, i, reg_beacon);
1135}
1136
1137
1138
1139
1140static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1141{
1142 unsigned int i;
1143 struct ieee80211_supported_band *sband;
1144 struct reg_beacon *reg_beacon;
1145
1146 assert_cfg80211_lock();
1147
1148 if (list_empty(®_beacon_list))
1149 return;
1150
1151 list_for_each_entry(reg_beacon, ®_beacon_list, list) {
1152 if (!wiphy->bands[reg_beacon->chan.band])
1153 continue;
1154 sband = wiphy->bands[reg_beacon->chan.band];
1155 for (i = 0; i < sband->n_channels; i++)
1156 handle_reg_beacon(wiphy, i, reg_beacon);
1157 }
1158}
1159
1160static bool reg_is_world_roaming(struct wiphy *wiphy)
1161{
1162 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1163 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1164 return true;
1165 if (last_request &&
1166 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1167 wiphy->custom_regulatory)
1168 return true;
1169 return false;
1170}
1171
1172
1173static void reg_process_beacons(struct wiphy *wiphy)
1174{
1175
1176
1177
1178
1179 if (!last_request)
1180 return;
1181 if (!reg_is_world_roaming(wiphy))
1182 return;
1183 wiphy_update_beacon_reg(wiphy);
1184}
1185
1186static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1187{
1188 if (!chan)
1189 return true;
1190 if (chan->flags & IEEE80211_CHAN_DISABLED)
1191 return true;
1192
1193 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1194 return true;
1195 return false;
1196}
1197
1198static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1199 enum ieee80211_band band,
1200 unsigned int chan_idx)
1201{
1202 struct ieee80211_supported_band *sband;
1203 struct ieee80211_channel *channel;
1204 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1205 unsigned int i;
1206
1207 assert_cfg80211_lock();
1208
1209 sband = wiphy->bands[band];
1210 BUG_ON(chan_idx >= sband->n_channels);
1211 channel = &sband->channels[chan_idx];
1212
1213 if (is_ht40_not_allowed(channel)) {
1214 channel->flags |= IEEE80211_CHAN_NO_HT40;
1215 return;
1216 }
1217
1218
1219
1220
1221
1222 for (i = 0; i < sband->n_channels; i++) {
1223 struct ieee80211_channel *c = &sband->channels[i];
1224 if (c->center_freq == (channel->center_freq - 20))
1225 channel_before = c;
1226 if (c->center_freq == (channel->center_freq + 20))
1227 channel_after = c;
1228 }
1229
1230
1231
1232
1233
1234
1235 if (is_ht40_not_allowed(channel_before))
1236 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1237 else
1238 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
1239
1240 if (is_ht40_not_allowed(channel_after))
1241 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1242 else
1243 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
1244}
1245
1246static void reg_process_ht_flags_band(struct wiphy *wiphy,
1247 enum ieee80211_band band)
1248{
1249 unsigned int i;
1250 struct ieee80211_supported_band *sband;
1251
1252 BUG_ON(!wiphy->bands[band]);
1253 sband = wiphy->bands[band];
1254
1255 for (i = 0; i < sband->n_channels; i++)
1256 reg_process_ht_flags_channel(wiphy, band, i);
1257}
1258
1259static void reg_process_ht_flags(struct wiphy *wiphy)
1260{
1261 enum ieee80211_band band;
1262
1263 if (!wiphy)
1264 return;
1265
1266 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1267 if (wiphy->bands[band])
1268 reg_process_ht_flags_band(wiphy, band);
1269 }
1270
1271}
1272
1273void wiphy_update_regulatory(struct wiphy *wiphy,
1274 enum nl80211_reg_initiator initiator)
1275{
1276 enum ieee80211_band band;
1277
1278 if (ignore_reg_update(wiphy, initiator))
1279 goto out;
1280 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1281 if (wiphy->bands[band])
1282 handle_band(wiphy, band);
1283 }
1284out:
1285 reg_process_beacons(wiphy);
1286 reg_process_ht_flags(wiphy);
1287 if (wiphy->reg_notifier)
1288 wiphy->reg_notifier(wiphy, last_request);
1289}
1290
1291static void handle_channel_custom(struct wiphy *wiphy,
1292 enum ieee80211_band band,
1293 unsigned int chan_idx,
1294 const struct ieee80211_regdomain *regd)
1295{
1296 int r;
1297 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1298 u32 bw_flags = 0;
1299 const struct ieee80211_reg_rule *reg_rule = NULL;
1300 const struct ieee80211_power_rule *power_rule = NULL;
1301 const struct ieee80211_freq_range *freq_range = NULL;
1302 struct ieee80211_supported_band *sband;
1303 struct ieee80211_channel *chan;
1304
1305 assert_reg_lock();
1306
1307 sband = wiphy->bands[band];
1308 BUG_ON(chan_idx >= sband->n_channels);
1309 chan = &sband->channels[chan_idx];
1310
1311 r = freq_reg_info_regd(wiphy,
1312 MHZ_TO_KHZ(chan->center_freq),
1313 desired_bw_khz,
1314 ®_rule,
1315 regd);
1316
1317 if (r) {
1318 chan->flags = IEEE80211_CHAN_DISABLED;
1319 return;
1320 }
1321
1322 power_rule = ®_rule->power_rule;
1323 freq_range = ®_rule->freq_range;
1324
1325 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1326 bw_flags = IEEE80211_CHAN_NO_HT40;
1327
1328 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1329 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1330 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1331}
1332
1333static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1334 const struct ieee80211_regdomain *regd)
1335{
1336 unsigned int i;
1337 struct ieee80211_supported_band *sband;
1338
1339 BUG_ON(!wiphy->bands[band]);
1340 sband = wiphy->bands[band];
1341
1342 for (i = 0; i < sband->n_channels; i++)
1343 handle_channel_custom(wiphy, band, i, regd);
1344}
1345
1346
1347void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1348 const struct ieee80211_regdomain *regd)
1349{
1350 enum ieee80211_band band;
1351 unsigned int bands_set = 0;
1352
1353 mutex_lock(®_mutex);
1354 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1355 if (!wiphy->bands[band])
1356 continue;
1357 handle_band_custom(wiphy, band, regd);
1358 bands_set++;
1359 }
1360 mutex_unlock(®_mutex);
1361
1362
1363
1364
1365
1366 WARN_ON(!bands_set);
1367}
1368EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1369
1370static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1371 const struct ieee80211_regdomain *src_regd)
1372{
1373 struct ieee80211_regdomain *regd;
1374 int size_of_regd = 0;
1375 unsigned int i;
1376
1377 size_of_regd = sizeof(struct ieee80211_regdomain) +
1378 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1379
1380 regd = kzalloc(size_of_regd, GFP_KERNEL);
1381 if (!regd)
1382 return -ENOMEM;
1383
1384 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1385
1386 for (i = 0; i < src_regd->n_reg_rules; i++)
1387 memcpy(®d->reg_rules[i], &src_regd->reg_rules[i],
1388 sizeof(struct ieee80211_reg_rule));
1389
1390 *dst_regd = regd;
1391 return 0;
1392}
1393
1394
1395
1396
1397
1398#define REG_INTERSECT 1
1399
1400
1401
1402static int ignore_request(struct wiphy *wiphy,
1403 struct regulatory_request *pending_request)
1404{
1405 struct wiphy *last_wiphy = NULL;
1406
1407 assert_cfg80211_lock();
1408
1409
1410 if (!last_request)
1411 return 0;
1412
1413 switch (pending_request->initiator) {
1414 case NL80211_REGDOM_SET_BY_CORE:
1415 return -EINVAL;
1416 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
1417
1418 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1419
1420 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
1421 return -EINVAL;
1422 if (last_request->initiator ==
1423 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1424 if (last_wiphy != wiphy) {
1425
1426
1427
1428
1429
1430
1431 if (regdom_changes(pending_request->alpha2))
1432 return -EOPNOTSUPP;
1433 return -EALREADY;
1434 }
1435
1436
1437
1438
1439 if (WARN_ON(regdom_changes(pending_request->alpha2)))
1440 return 0;
1441 return -EALREADY;
1442 }
1443 return REG_INTERSECT;
1444 case NL80211_REGDOM_SET_BY_DRIVER:
1445 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
1446 if (is_old_static_regdom(cfg80211_regdomain))
1447 return 0;
1448 if (regdom_changes(pending_request->alpha2))
1449 return 0;
1450 return -EALREADY;
1451 }
1452
1453
1454
1455
1456
1457
1458 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1459 !regdom_changes(pending_request->alpha2))
1460 return -EALREADY;
1461
1462 return REG_INTERSECT;
1463 case NL80211_REGDOM_SET_BY_USER:
1464 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
1465 return REG_INTERSECT;
1466
1467
1468
1469
1470 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
1471 last_request->intersect)
1472 return -EOPNOTSUPP;
1473
1474
1475
1476
1477 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1478 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1479 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
1480 if (regdom_changes(last_request->alpha2))
1481 return -EAGAIN;
1482 }
1483
1484 if (!is_old_static_regdom(cfg80211_regdomain) &&
1485 !regdom_changes(pending_request->alpha2))
1486 return -EALREADY;
1487
1488 return 0;
1489 }
1490
1491 return -EINVAL;
1492}
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508static int __regulatory_hint(struct wiphy *wiphy,
1509 struct regulatory_request *pending_request)
1510{
1511 bool intersect = false;
1512 int r = 0;
1513
1514 assert_cfg80211_lock();
1515
1516 r = ignore_request(wiphy, pending_request);
1517
1518 if (r == REG_INTERSECT) {
1519 if (pending_request->initiator ==
1520 NL80211_REGDOM_SET_BY_DRIVER) {
1521 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1522 if (r) {
1523 kfree(pending_request);
1524 return r;
1525 }
1526 }
1527 intersect = true;
1528 } else if (r) {
1529
1530
1531
1532
1533
1534 if (r == -EALREADY &&
1535 pending_request->initiator ==
1536 NL80211_REGDOM_SET_BY_DRIVER) {
1537 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1538 if (r) {
1539 kfree(pending_request);
1540 return r;
1541 }
1542 r = -EALREADY;
1543 goto new_request;
1544 }
1545 kfree(pending_request);
1546 return r;
1547 }
1548
1549new_request:
1550 kfree(last_request);
1551
1552 last_request = pending_request;
1553 last_request->intersect = intersect;
1554
1555 pending_request = NULL;
1556
1557
1558 if (r < 0) {
1559
1560
1561
1562
1563
1564 if (r == -EALREADY)
1565 nl80211_send_reg_change_event(last_request);
1566 return r;
1567 }
1568
1569 return call_crda(last_request->alpha2);
1570}
1571
1572
1573static void reg_process_hint(struct regulatory_request *reg_request)
1574{
1575 int r = 0;
1576 struct wiphy *wiphy = NULL;
1577
1578 BUG_ON(!reg_request->alpha2);
1579
1580 mutex_lock(&cfg80211_mutex);
1581 mutex_lock(®_mutex);
1582
1583 if (wiphy_idx_valid(reg_request->wiphy_idx))
1584 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1585
1586 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
1587 !wiphy) {
1588 kfree(reg_request);
1589 goto out;
1590 }
1591
1592 r = __regulatory_hint(wiphy, reg_request);
1593
1594 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1595 wiphy_update_regulatory(wiphy, reg_request->initiator);
1596out:
1597 mutex_unlock(®_mutex);
1598 mutex_unlock(&cfg80211_mutex);
1599}
1600
1601
1602static void reg_process_pending_hints(void)
1603 {
1604 struct regulatory_request *reg_request;
1605
1606 spin_lock(®_requests_lock);
1607 while (!list_empty(®_requests_list)) {
1608 reg_request = list_first_entry(®_requests_list,
1609 struct regulatory_request,
1610 list);
1611 list_del_init(®_request->list);
1612
1613 spin_unlock(®_requests_lock);
1614 reg_process_hint(reg_request);
1615 spin_lock(®_requests_lock);
1616 }
1617 spin_unlock(®_requests_lock);
1618}
1619
1620
1621static void reg_process_pending_beacon_hints(void)
1622{
1623 struct cfg80211_registered_device *rdev;
1624 struct reg_beacon *pending_beacon, *tmp;
1625
1626
1627
1628
1629
1630 mutex_lock(&cfg80211_mutex);
1631
1632
1633 spin_lock_bh(®_pending_beacons_lock);
1634
1635 if (list_empty(®_pending_beacons)) {
1636 spin_unlock_bh(®_pending_beacons_lock);
1637 goto out;
1638 }
1639
1640 list_for_each_entry_safe(pending_beacon, tmp,
1641 ®_pending_beacons, list) {
1642
1643 list_del_init(&pending_beacon->list);
1644
1645
1646 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1647 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
1648
1649
1650 list_add_tail(&pending_beacon->list, ®_beacon_list);
1651 }
1652
1653 spin_unlock_bh(®_pending_beacons_lock);
1654out:
1655 mutex_unlock(&cfg80211_mutex);
1656}
1657
1658static void reg_todo(struct work_struct *work)
1659{
1660 reg_process_pending_hints();
1661 reg_process_pending_beacon_hints();
1662}
1663
1664static DECLARE_WORK(reg_work, reg_todo);
1665
1666static void queue_regulatory_request(struct regulatory_request *request)
1667{
1668 spin_lock(®_requests_lock);
1669 list_add_tail(&request->list, ®_requests_list);
1670 spin_unlock(®_requests_lock);
1671
1672 schedule_work(®_work);
1673}
1674
1675
1676static int regulatory_hint_core(const char *alpha2)
1677{
1678 struct regulatory_request *request;
1679
1680 BUG_ON(last_request);
1681
1682 request = kzalloc(sizeof(struct regulatory_request),
1683 GFP_KERNEL);
1684 if (!request)
1685 return -ENOMEM;
1686
1687 request->alpha2[0] = alpha2[0];
1688 request->alpha2[1] = alpha2[1];
1689 request->initiator = NL80211_REGDOM_SET_BY_CORE;
1690
1691 queue_regulatory_request(request);
1692
1693
1694
1695
1696
1697
1698 flush_scheduled_work();
1699
1700 return 0;
1701}
1702
1703
1704int regulatory_hint_user(const char *alpha2)
1705{
1706 struct regulatory_request *request;
1707
1708 BUG_ON(!alpha2);
1709
1710 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1711 if (!request)
1712 return -ENOMEM;
1713
1714 request->wiphy_idx = WIPHY_IDX_STALE;
1715 request->alpha2[0] = alpha2[0];
1716 request->alpha2[1] = alpha2[1];
1717 request->initiator = NL80211_REGDOM_SET_BY_USER,
1718
1719 queue_regulatory_request(request);
1720
1721 return 0;
1722}
1723
1724
1725int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1726{
1727 struct regulatory_request *request;
1728
1729 BUG_ON(!alpha2);
1730 BUG_ON(!wiphy);
1731
1732 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1733 if (!request)
1734 return -ENOMEM;
1735
1736 request->wiphy_idx = get_wiphy_idx(wiphy);
1737
1738
1739 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1740
1741 request->alpha2[0] = alpha2[0];
1742 request->alpha2[1] = alpha2[1];
1743 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
1744
1745 queue_regulatory_request(request);
1746
1747 return 0;
1748}
1749EXPORT_SYMBOL(regulatory_hint);
1750
1751
1752static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1753 u32 country_ie_checksum)
1754{
1755 struct wiphy *request_wiphy;
1756
1757 assert_reg_lock();
1758
1759 if (unlikely(last_request->initiator !=
1760 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1761 return false;
1762
1763 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1764
1765 if (!request_wiphy)
1766 return false;
1767
1768 if (likely(request_wiphy != wiphy))
1769 return !country_ie_integrity_changes(country_ie_checksum);
1770
1771
1772
1773
1774
1775 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1776 return true;
1777 return false;
1778}
1779
1780
1781
1782
1783
1784void regulatory_hint_11d(struct wiphy *wiphy,
1785 u8 *country_ie,
1786 u8 country_ie_len)
1787{
1788 struct ieee80211_regdomain *rd = NULL;
1789 char alpha2[2];
1790 u32 checksum = 0;
1791 enum environment_cap env = ENVIRON_ANY;
1792 struct regulatory_request *request;
1793
1794 mutex_lock(®_mutex);
1795
1796 if (unlikely(!last_request))
1797 goto out;
1798
1799
1800 if (country_ie_len & 0x01)
1801 goto out;
1802
1803 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1804 goto out;
1805
1806
1807
1808
1809
1810
1811 if (country_ie_regdomain)
1812 goto out;
1813
1814 alpha2[0] = country_ie[0];
1815 alpha2[1] = country_ie[1];
1816
1817 if (country_ie[2] == 'I')
1818 env = ENVIRON_INDOOR;
1819 else if (country_ie[2] == 'O')
1820 env = ENVIRON_OUTDOOR;
1821
1822
1823
1824
1825
1826
1827 if (likely(last_request->initiator ==
1828 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1829 wiphy_idx_valid(last_request->wiphy_idx)))
1830 goto out;
1831
1832 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1833 if (!rd)
1834 goto out;
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
1846 goto free_rd_out;
1847
1848 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1849 if (!request)
1850 goto free_rd_out;
1851
1852
1853
1854
1855
1856 country_ie_regdomain = rd;
1857
1858 request->wiphy_idx = get_wiphy_idx(wiphy);
1859 request->alpha2[0] = rd->alpha2[0];
1860 request->alpha2[1] = rd->alpha2[1];
1861 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
1862 request->country_ie_checksum = checksum;
1863 request->country_ie_env = env;
1864
1865 mutex_unlock(®_mutex);
1866
1867 queue_regulatory_request(request);
1868
1869 return;
1870
1871free_rd_out:
1872 kfree(rd);
1873out:
1874 mutex_unlock(®_mutex);
1875}
1876
1877static bool freq_is_chan_12_13_14(u16 freq)
1878{
1879 if (freq == ieee80211_channel_to_frequency(12) ||
1880 freq == ieee80211_channel_to_frequency(13) ||
1881 freq == ieee80211_channel_to_frequency(14))
1882 return true;
1883 return false;
1884}
1885
1886int regulatory_hint_found_beacon(struct wiphy *wiphy,
1887 struct ieee80211_channel *beacon_chan,
1888 gfp_t gfp)
1889{
1890 struct reg_beacon *reg_beacon;
1891
1892 if (likely((beacon_chan->beacon_found ||
1893 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1894 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1895 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1896 return 0;
1897
1898 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1899 if (!reg_beacon)
1900 return -ENOMEM;
1901
1902#ifdef CONFIG_CFG80211_REG_DEBUG
1903 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1904 "frequency: %d MHz (Ch %d) on %s\n",
1905 beacon_chan->center_freq,
1906 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1907 wiphy_name(wiphy));
1908#endif
1909 memcpy(®_beacon->chan, beacon_chan,
1910 sizeof(struct ieee80211_channel));
1911
1912
1913
1914
1915
1916
1917 spin_lock_bh(®_pending_beacons_lock);
1918 list_add_tail(®_beacon->list, ®_pending_beacons);
1919 spin_unlock_bh(®_pending_beacons_lock);
1920
1921 schedule_work(®_work);
1922
1923 return 0;
1924}
1925
1926static void print_rd_rules(const struct ieee80211_regdomain *rd)
1927{
1928 unsigned int i;
1929 const struct ieee80211_reg_rule *reg_rule = NULL;
1930 const struct ieee80211_freq_range *freq_range = NULL;
1931 const struct ieee80211_power_rule *power_rule = NULL;
1932
1933 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1934 "(max_antenna_gain, max_eirp)\n");
1935
1936 for (i = 0; i < rd->n_reg_rules; i++) {
1937 reg_rule = &rd->reg_rules[i];
1938 freq_range = ®_rule->freq_range;
1939 power_rule = ®_rule->power_rule;
1940
1941
1942
1943
1944
1945 if (power_rule->max_antenna_gain)
1946 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1947 "(%d mBi, %d mBm)\n",
1948 freq_range->start_freq_khz,
1949 freq_range->end_freq_khz,
1950 freq_range->max_bandwidth_khz,
1951 power_rule->max_antenna_gain,
1952 power_rule->max_eirp);
1953 else
1954 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1955 "(N/A, %d mBm)\n",
1956 freq_range->start_freq_khz,
1957 freq_range->end_freq_khz,
1958 freq_range->max_bandwidth_khz,
1959 power_rule->max_eirp);
1960 }
1961}
1962
1963static void print_regdomain(const struct ieee80211_regdomain *rd)
1964{
1965
1966 if (is_intersected_alpha2(rd->alpha2)) {
1967
1968 if (last_request->initiator ==
1969 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
1970 struct cfg80211_registered_device *rdev;
1971 rdev = cfg80211_rdev_by_wiphy_idx(
1972 last_request->wiphy_idx);
1973 if (rdev) {
1974 printk(KERN_INFO "cfg80211: Current regulatory "
1975 "domain updated by AP to: %c%c\n",
1976 rdev->country_ie_alpha2[0],
1977 rdev->country_ie_alpha2[1]);
1978 } else
1979 printk(KERN_INFO "cfg80211: Current regulatory "
1980 "domain intersected: \n");
1981 } else
1982 printk(KERN_INFO "cfg80211: Current regulatory "
1983 "domain intersected: \n");
1984 } else if (is_world_regdom(rd->alpha2))
1985 printk(KERN_INFO "cfg80211: World regulatory "
1986 "domain updated:\n");
1987 else {
1988 if (is_unknown_alpha2(rd->alpha2))
1989 printk(KERN_INFO "cfg80211: Regulatory domain "
1990 "changed to driver built-in settings "
1991 "(unknown country)\n");
1992 else
1993 printk(KERN_INFO "cfg80211: Regulatory domain "
1994 "changed to country: %c%c\n",
1995 rd->alpha2[0], rd->alpha2[1]);
1996 }
1997 print_rd_rules(rd);
1998}
1999
2000static void print_regdomain_info(const struct ieee80211_regdomain *rd)
2001{
2002 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2003 rd->alpha2[0], rd->alpha2[1]);
2004 print_rd_rules(rd);
2005}
2006
2007#ifdef CONFIG_CFG80211_REG_DEBUG
2008static void reg_country_ie_process_debug(
2009 const struct ieee80211_regdomain *rd,
2010 const struct ieee80211_regdomain *country_ie_regdomain,
2011 const struct ieee80211_regdomain *intersected_rd)
2012{
2013 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2014 print_regdomain_info(country_ie_regdomain);
2015 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2016 print_regdomain_info(rd);
2017 if (intersected_rd) {
2018 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2019 "and get:\n");
2020 print_regdomain_info(intersected_rd);
2021 return;
2022 }
2023 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2024}
2025#else
2026static inline void reg_country_ie_process_debug(
2027 const struct ieee80211_regdomain *rd,
2028 const struct ieee80211_regdomain *country_ie_regdomain,
2029 const struct ieee80211_regdomain *intersected_rd)
2030{
2031}
2032#endif
2033
2034
2035static int __set_regdom(const struct ieee80211_regdomain *rd)
2036{
2037 const struct ieee80211_regdomain *intersected_rd = NULL;
2038 struct cfg80211_registered_device *rdev = NULL;
2039 struct wiphy *request_wiphy;
2040
2041
2042 if (is_world_regdom(rd->alpha2)) {
2043 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2044 return -EINVAL;
2045 update_world_regdomain(rd);
2046 return 0;
2047 }
2048
2049 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2050 !is_unknown_alpha2(rd->alpha2))
2051 return -EINVAL;
2052
2053 if (!last_request)
2054 return -EINVAL;
2055
2056
2057
2058
2059
2060
2061 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2062
2063
2064
2065
2066 if (!is_old_static_regdom(cfg80211_regdomain) &&
2067 !regdom_changes(rd->alpha2))
2068 return -EINVAL;
2069 }
2070
2071
2072
2073
2074
2075
2076
2077
2078 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
2079 return -EINVAL;
2080
2081 if (!is_valid_rd(rd)) {
2082 printk(KERN_ERR "cfg80211: Invalid "
2083 "regulatory domain detected:\n");
2084 print_regdomain_info(rd);
2085 return -EINVAL;
2086 }
2087
2088 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2089
2090 if (!last_request->intersect) {
2091 int r;
2092
2093 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
2094 reset_regdomains();
2095 cfg80211_regdomain = rd;
2096 return 0;
2097 }
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108 if (request_wiphy->regd)
2109 return -EALREADY;
2110
2111 r = reg_copy_regd(&request_wiphy->regd, rd);
2112 if (r)
2113 return r;
2114
2115 reset_regdomains();
2116 cfg80211_regdomain = rd;
2117 return 0;
2118 }
2119
2120
2121
2122 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
2123
2124 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2125 if (!intersected_rd)
2126 return -EINVAL;
2127
2128
2129
2130
2131
2132
2133 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
2134 request_wiphy->regd = rd;
2135 else
2136 kfree(rd);
2137
2138 rd = NULL;
2139
2140 reset_regdomains();
2141 cfg80211_regdomain = intersected_rd;
2142
2143 return 0;
2144 }
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156 if (!country_ie_regdomain)
2157 return -EALREADY;
2158 BUG_ON(rd == country_ie_regdomain);
2159
2160
2161
2162
2163
2164
2165 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
2166
2167 reg_country_ie_process_debug(rd,
2168 country_ie_regdomain,
2169 intersected_rd);
2170
2171 kfree(country_ie_regdomain);
2172 country_ie_regdomain = NULL;
2173
2174 if (!intersected_rd)
2175 return -EINVAL;
2176
2177 rdev = wiphy_to_dev(request_wiphy);
2178
2179 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2180 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2181 rdev->env = last_request->country_ie_env;
2182
2183 BUG_ON(intersected_rd == rd);
2184
2185 kfree(rd);
2186 rd = NULL;
2187
2188 reset_regdomains();
2189 cfg80211_regdomain = intersected_rd;
2190
2191 return 0;
2192}
2193
2194
2195
2196
2197
2198
2199
2200int set_regdom(const struct ieee80211_regdomain *rd)
2201{
2202 int r;
2203
2204 assert_cfg80211_lock();
2205
2206 mutex_lock(®_mutex);
2207
2208
2209 r = __set_regdom(rd);
2210 if (r) {
2211 kfree(rd);
2212 mutex_unlock(®_mutex);
2213 return r;
2214 }
2215
2216
2217 if (!last_request->intersect)
2218 BUG_ON(rd != cfg80211_regdomain);
2219
2220
2221 update_all_wiphy_regulatory(last_request->initiator);
2222
2223 print_regdomain(cfg80211_regdomain);
2224
2225 nl80211_send_reg_change_event(last_request);
2226
2227 mutex_unlock(®_mutex);
2228
2229 return r;
2230}
2231
2232
2233void reg_device_remove(struct wiphy *wiphy)
2234{
2235 struct wiphy *request_wiphy = NULL;
2236
2237 assert_cfg80211_lock();
2238
2239 mutex_lock(®_mutex);
2240
2241 kfree(wiphy->regd);
2242
2243 if (last_request)
2244 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2245
2246 if (!request_wiphy || request_wiphy != wiphy)
2247 goto out;
2248
2249 last_request->wiphy_idx = WIPHY_IDX_STALE;
2250 last_request->country_ie_env = ENVIRON_ANY;
2251out:
2252 mutex_unlock(®_mutex);
2253}
2254
2255int regulatory_init(void)
2256{
2257 int err = 0;
2258
2259 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2260 if (IS_ERR(reg_pdev))
2261 return PTR_ERR(reg_pdev);
2262
2263 spin_lock_init(®_requests_lock);
2264 spin_lock_init(®_pending_beacons_lock);
2265
2266#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2267 cfg80211_regdomain = static_regdom(ieee80211_regdom);
2268
2269 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
2270 print_regdomain_info(cfg80211_regdomain);
2271#else
2272 cfg80211_regdomain = cfg80211_world_regdom;
2273
2274#endif
2275
2276 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
2277 if (err) {
2278 if (err == -ENOMEM)
2279 return err;
2280
2281
2282
2283
2284
2285
2286
2287 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2288 "to call CRDA during init");
2289#ifdef CONFIG_CFG80211_REG_DEBUG
2290
2291 WARN_ON(err);
2292#endif
2293 }
2294
2295
2296
2297
2298
2299 if (!is_world_regdom(ieee80211_regdom))
2300 regulatory_hint_user(ieee80211_regdom);
2301
2302 return 0;
2303}
2304
2305void regulatory_exit(void)
2306{
2307 struct regulatory_request *reg_request, *tmp;
2308 struct reg_beacon *reg_beacon, *btmp;
2309
2310 cancel_work_sync(®_work);
2311
2312 mutex_lock(&cfg80211_mutex);
2313 mutex_lock(®_mutex);
2314
2315 reset_regdomains();
2316
2317 kfree(country_ie_regdomain);
2318 country_ie_regdomain = NULL;
2319
2320 kfree(last_request);
2321
2322 platform_device_unregister(reg_pdev);
2323
2324 spin_lock_bh(®_pending_beacons_lock);
2325 if (!list_empty(®_pending_beacons)) {
2326 list_for_each_entry_safe(reg_beacon, btmp,
2327 ®_pending_beacons, list) {
2328 list_del(®_beacon->list);
2329 kfree(reg_beacon);
2330 }
2331 }
2332 spin_unlock_bh(®_pending_beacons_lock);
2333
2334 if (!list_empty(®_beacon_list)) {
2335 list_for_each_entry_safe(reg_beacon, btmp,
2336 ®_beacon_list, list) {
2337 list_del(®_beacon->list);
2338 kfree(reg_beacon);
2339 }
2340 }
2341
2342 spin_lock(®_requests_lock);
2343 if (!list_empty(®_requests_list)) {
2344 list_for_each_entry_safe(reg_request, tmp,
2345 ®_requests_list, list) {
2346 list_del(®_request->list);
2347 kfree(reg_request);
2348 }
2349 }
2350 spin_unlock(®_requests_lock);
2351
2352 mutex_unlock(®_mutex);
2353 mutex_unlock(&cfg80211_mutex);
2354}
2355