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