cfg80211: allow users to help a driver's compliance
[safe/jmp/linux-2.6] / net / wireless / reg.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
5  * Copyright 2008       Luis R. Rodriguez <lrodriguz@atheros.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 /**
13  * DOC: Wireless regulatory infrastructure
14  *
15  * The usual implementation is for a driver to read a device EEPROM to
16  * determine which regulatory domain it should be operating under, then
17  * looking up the allowable channels in a driver-local table and finally
18  * registering those channels in the wiphy structure.
19  *
20  * Another set of compliance enforcement is for drivers to use their
21  * own compliance limits which can be stored on the EEPROM. The host
22  * driver or firmware may ensure these are used.
23  *
24  * In addition to all this we provide an extra layer of regulatory
25  * conformance. For drivers which do not have any regulatory
26  * information CRDA provides the complete regulatory solution.
27  * For others it provides a community effort on further restrictions
28  * to enhance compliance.
29  *
30  * Note: When number of rules --> infinity we will not be able to
31  * index on alpha2 any more, instead we'll probably have to
32  * rely on some SHA1 checksum of the regdomain for example.
33  *
34  */
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/random.h>
38 #include <linux/nl80211.h>
39 #include <linux/platform_device.h>
40 #include <net/wireless.h>
41 #include <net/cfg80211.h>
42 #include "core.h"
43 #include "reg.h"
44
45 /* Receipt of information from last regulatory request */
46 static struct regulatory_request *last_request;
47
48 /* To trigger userspace events */
49 static struct platform_device *reg_pdev;
50
51 /* Keep the ordering from large to small */
52 static u32 supported_bandwidths[] = {
53         MHZ_TO_KHZ(40),
54         MHZ_TO_KHZ(20),
55 };
56
57 /* Central wireless core regulatory domains, we only need two,
58  * the current one and a world regulatory domain in case we have no
59  * information to give us an alpha2 */
60 static const struct ieee80211_regdomain *cfg80211_regdomain;
61
62 /* We use this as a place for the rd structure built from the
63  * last parsed country IE to rest until CRDA gets back to us with
64  * what it thinks should apply for the same country */
65 static const struct ieee80211_regdomain *country_ie_regdomain;
66
67 /* We keep a static world regulatory domain in case of the absence of CRDA */
68 static const struct ieee80211_regdomain world_regdom = {
69         .n_reg_rules = 1,
70         .alpha2 =  "00",
71         .reg_rules = {
72                 REG_RULE(2412-10, 2462+10, 40, 6, 20,
73                         NL80211_RRF_PASSIVE_SCAN |
74                         NL80211_RRF_NO_IBSS),
75         }
76 };
77
78 static const struct ieee80211_regdomain *cfg80211_world_regdom =
79         &world_regdom;
80
81 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
82 static char *ieee80211_regdom = "US";
83 module_param(ieee80211_regdom, charp, 0444);
84 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
85
86 /* We assume 40 MHz bandwidth for the old regulatory work.
87  * We make emphasis we are using the exact same frequencies
88  * as before */
89
90 static const struct ieee80211_regdomain us_regdom = {
91         .n_reg_rules = 6,
92         .alpha2 =  "US",
93         .reg_rules = {
94                 /* IEEE 802.11b/g, channels 1..11 */
95                 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
96                 /* IEEE 802.11a, channel 36 */
97                 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
98                 /* IEEE 802.11a, channel 40 */
99                 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
100                 /* IEEE 802.11a, channel 44 */
101                 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
102                 /* IEEE 802.11a, channels 48..64 */
103                 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
104                 /* IEEE 802.11a, channels 149..165, outdoor */
105                 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
106         }
107 };
108
109 static const struct ieee80211_regdomain jp_regdom = {
110         .n_reg_rules = 3,
111         .alpha2 =  "JP",
112         .reg_rules = {
113                 /* IEEE 802.11b/g, channels 1..14 */
114                 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
115                 /* IEEE 802.11a, channels 34..48 */
116                 REG_RULE(5170-10, 5240+10, 40, 6, 20,
117                         NL80211_RRF_PASSIVE_SCAN),
118                 /* IEEE 802.11a, channels 52..64 */
119                 REG_RULE(5260-10, 5320+10, 40, 6, 20,
120                         NL80211_RRF_NO_IBSS |
121                         NL80211_RRF_DFS),
122         }
123 };
124
125 static const struct ieee80211_regdomain eu_regdom = {
126         .n_reg_rules = 6,
127         /* This alpha2 is bogus, we leave it here just for stupid
128          * backward compatibility */
129         .alpha2 =  "EU",
130         .reg_rules = {
131                 /* IEEE 802.11b/g, channels 1..13 */
132                 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
133                 /* IEEE 802.11a, channel 36 */
134                 REG_RULE(5180-10, 5180+10, 40, 6, 23,
135                         NL80211_RRF_PASSIVE_SCAN),
136                 /* IEEE 802.11a, channel 40 */
137                 REG_RULE(5200-10, 5200+10, 40, 6, 23,
138                         NL80211_RRF_PASSIVE_SCAN),
139                 /* IEEE 802.11a, channel 44 */
140                 REG_RULE(5220-10, 5220+10, 40, 6, 23,
141                         NL80211_RRF_PASSIVE_SCAN),
142                 /* IEEE 802.11a, channels 48..64 */
143                 REG_RULE(5240-10, 5320+10, 40, 6, 20,
144                         NL80211_RRF_NO_IBSS |
145                         NL80211_RRF_DFS),
146                 /* IEEE 802.11a, channels 100..140 */
147                 REG_RULE(5500-10, 5700+10, 40, 6, 30,
148                         NL80211_RRF_NO_IBSS |
149                         NL80211_RRF_DFS),
150         }
151 };
152
153 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
154 {
155         if (alpha2[0] == 'U' && alpha2[1] == 'S')
156                 return &us_regdom;
157         if (alpha2[0] == 'J' && alpha2[1] == 'P')
158                 return &jp_regdom;
159         if (alpha2[0] == 'E' && alpha2[1] == 'U')
160                 return &eu_regdom;
161         /* Default, as per the old rules */
162         return &us_regdom;
163 }
164
165 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
166 {
167         if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
168                 return true;
169         return false;
170 }
171 #else
172 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
173 {
174         return false;
175 }
176 #endif
177
178 static void reset_regdomains(void)
179 {
180         /* avoid freeing static information or freeing something twice */
181         if (cfg80211_regdomain == cfg80211_world_regdom)
182                 cfg80211_regdomain = NULL;
183         if (cfg80211_world_regdom == &world_regdom)
184                 cfg80211_world_regdom = NULL;
185         if (cfg80211_regdomain == &world_regdom)
186                 cfg80211_regdomain = NULL;
187         if (is_old_static_regdom(cfg80211_regdomain))
188                 cfg80211_regdomain = NULL;
189
190         kfree(cfg80211_regdomain);
191         kfree(cfg80211_world_regdom);
192
193         cfg80211_world_regdom = &world_regdom;
194         cfg80211_regdomain = NULL;
195 }
196
197 /* Dynamic world regulatory domain requested by the wireless
198  * core upon initialization */
199 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
200 {
201         BUG_ON(!last_request);
202
203         reset_regdomains();
204
205         cfg80211_world_regdom = rd;
206         cfg80211_regdomain = rd;
207 }
208
209 bool is_world_regdom(const char *alpha2)
210 {
211         if (!alpha2)
212                 return false;
213         if (alpha2[0] == '0' && alpha2[1] == '0')
214                 return true;
215         return false;
216 }
217
218 static bool is_alpha2_set(const char *alpha2)
219 {
220         if (!alpha2)
221                 return false;
222         if (alpha2[0] != 0 && alpha2[1] != 0)
223                 return true;
224         return false;
225 }
226
227 static bool is_alpha_upper(char letter)
228 {
229         /* ASCII A - Z */
230         if (letter >= 65 && letter <= 90)
231                 return true;
232         return false;
233 }
234
235 static bool is_unknown_alpha2(const char *alpha2)
236 {
237         if (!alpha2)
238                 return false;
239         /* Special case where regulatory domain was built by driver
240          * but a specific alpha2 cannot be determined */
241         if (alpha2[0] == '9' && alpha2[1] == '9')
242                 return true;
243         return false;
244 }
245
246 static bool is_intersected_alpha2(const char *alpha2)
247 {
248         if (!alpha2)
249                 return false;
250         /* Special case where regulatory domain is the
251          * result of an intersection between two regulatory domain
252          * structures */
253         if (alpha2[0] == '9' && alpha2[1] == '8')
254                 return true;
255         return false;
256 }
257
258 static bool is_an_alpha2(const char *alpha2)
259 {
260         if (!alpha2)
261                 return false;
262         if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
263                 return true;
264         return false;
265 }
266
267 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
268 {
269         if (!alpha2_x || !alpha2_y)
270                 return false;
271         if (alpha2_x[0] == alpha2_y[0] &&
272                 alpha2_x[1] == alpha2_y[1])
273                 return true;
274         return false;
275 }
276
277 static bool regdom_changed(const char *alpha2)
278 {
279         if (!cfg80211_regdomain)
280                 return true;
281         if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
282                 return false;
283         return true;
284 }
285
286 /**
287  * country_ie_integrity_changes - tells us if the country IE has changed
288  * @checksum: checksum of country IE of fields we are interested in
289  *
290  * If the country IE has not changed you can ignore it safely. This is
291  * useful to determine if two devices are seeing two different country IEs
292  * even on the same alpha2. Note that this will return false if no IE has
293  * been set on the wireless core yet.
294  */
295 static bool country_ie_integrity_changes(u32 checksum)
296 {
297         /* If no IE has been set then the checksum doesn't change */
298         if (unlikely(!last_request->country_ie_checksum))
299                 return false;
300         if (unlikely(last_request->country_ie_checksum != checksum))
301                 return true;
302         return false;
303 }
304
305 /* This lets us keep regulatory code which is updated on a regulatory
306  * basis in userspace. */
307 static int call_crda(const char *alpha2)
308 {
309         char country_env[9 + 2] = "COUNTRY=";
310         char *envp[] = {
311                 country_env,
312                 NULL
313         };
314
315         if (!is_world_regdom((char *) alpha2))
316                 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
317                         alpha2[0], alpha2[1]);
318         else
319                 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
320                         "regulatory domain\n");
321
322         country_env[8] = alpha2[0];
323         country_env[9] = alpha2[1];
324
325         return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
326 }
327
328 /* Used by nl80211 before kmalloc'ing our regulatory domain */
329 bool reg_is_valid_request(const char *alpha2)
330 {
331         if (!last_request)
332                 return false;
333
334         return alpha2_equal(last_request->alpha2, alpha2);
335 }
336
337 /* Sanity check on a regulatory rule */
338 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
339 {
340         const struct ieee80211_freq_range *freq_range = &rule->freq_range;
341         u32 freq_diff;
342
343         if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
344                 return false;
345
346         if (freq_range->start_freq_khz > freq_range->end_freq_khz)
347                 return false;
348
349         freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
350
351         if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff)
352                 return false;
353
354         return true;
355 }
356
357 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
358 {
359         const struct ieee80211_reg_rule *reg_rule = NULL;
360         unsigned int i;
361
362         if (!rd->n_reg_rules)
363                 return false;
364
365         if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
366                 return false;
367
368         for (i = 0; i < rd->n_reg_rules; i++) {
369                 reg_rule = &rd->reg_rules[i];
370                 if (!is_valid_reg_rule(reg_rule))
371                         return false;
372         }
373
374         return true;
375 }
376
377 /* Returns value in KHz */
378 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
379         u32 freq)
380 {
381         unsigned int i;
382         for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
383                 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
384                 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
385                 if (start_freq_khz >= freq_range->start_freq_khz &&
386                         end_freq_khz <= freq_range->end_freq_khz)
387                         return supported_bandwidths[i];
388         }
389         return 0;
390 }
391
392 /**
393  * freq_in_rule_band - tells us if a frequency is in a frequency band
394  * @freq_range: frequency rule we want to query
395  * @freq_khz: frequency we are inquiring about
396  *
397  * This lets us know if a specific frequency rule is or is not relevant to
398  * a specific frequency's band. Bands are device specific and artificial
399  * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
400  * safe for now to assume that a frequency rule should not be part of a
401  * frequency's band if the start freq or end freq are off by more than 2 GHz.
402  * This resolution can be lowered and should be considered as we add
403  * regulatory rule support for other "bands".
404  **/
405 static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
406         u32 freq_khz)
407 {
408 #define ONE_GHZ_IN_KHZ  1000000
409         if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
410                 return true;
411         if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
412                 return true;
413         return false;
414 #undef ONE_GHZ_IN_KHZ
415 }
416
417 /* Converts a country IE to a regulatory domain. A regulatory domain
418  * structure has a lot of information which the IE doesn't yet have,
419  * so for the other values we use upper max values as we will intersect
420  * with our userspace regulatory agent to get lower bounds. */
421 static struct ieee80211_regdomain *country_ie_2_rd(
422                                 u8 *country_ie,
423                                 u8 country_ie_len,
424                                 u32 *checksum)
425 {
426         struct ieee80211_regdomain *rd = NULL;
427         unsigned int i = 0;
428         char alpha2[2];
429         u32 flags = 0;
430         u32 num_rules = 0, size_of_regd = 0;
431         u8 *triplets_start = NULL;
432         u8 len_at_triplet = 0;
433         /* the last channel we have registered in a subband (triplet) */
434         int last_sub_max_channel = 0;
435
436         *checksum = 0xDEADBEEF;
437
438         /* Country IE requirements */
439         BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
440                 country_ie_len & 0x01);
441
442         alpha2[0] = country_ie[0];
443         alpha2[1] = country_ie[1];
444
445         /*
446          * Third octet can be:
447          *    'I' - Indoor
448          *    'O' - Outdoor
449          *
450          *  anything else we assume is no restrictions
451          */
452         if (country_ie[2] == 'I')
453                 flags = NL80211_RRF_NO_OUTDOOR;
454         else if (country_ie[2] == 'O')
455                 flags = NL80211_RRF_NO_INDOOR;
456
457         country_ie += 3;
458         country_ie_len -= 3;
459
460         triplets_start = country_ie;
461         len_at_triplet = country_ie_len;
462
463         *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
464
465         /* We need to build a reg rule for each triplet, but first we must
466          * calculate the number of reg rules we will need. We will need one
467          * for each channel subband */
468         while (country_ie_len >= 3) {
469                 struct ieee80211_country_ie_triplet *triplet =
470                         (struct ieee80211_country_ie_triplet *) country_ie;
471                 int cur_sub_max_channel = 0, cur_channel = 0;
472
473                 if (triplet->ext.reg_extension_id >=
474                                 IEEE80211_COUNTRY_EXTENSION_ID) {
475                         country_ie += 3;
476                         country_ie_len -= 3;
477                         continue;
478                 }
479
480                 cur_channel = triplet->chans.first_channel;
481                 cur_sub_max_channel = ieee80211_channel_to_frequency(
482                         cur_channel + triplet->chans.num_channels);
483
484                 /* Basic sanity check */
485                 if (cur_sub_max_channel < cur_channel)
486                         return NULL;
487
488                 /* Do not allow overlapping channels. Also channels
489                  * passed in each subband must be monotonically
490                  * increasing */
491                 if (last_sub_max_channel) {
492                         if (cur_channel <= last_sub_max_channel)
493                                 return NULL;
494                         if (cur_sub_max_channel <= last_sub_max_channel)
495                                 return NULL;
496                 }
497
498                 /* When dot11RegulatoryClassesRequired is supported
499                  * we can throw ext triplets as part of this soup,
500                  * for now we don't care when those change as we
501                  * don't support them */
502                 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
503                   ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
504                   ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
505
506                 last_sub_max_channel = cur_sub_max_channel;
507
508                 country_ie += 3;
509                 country_ie_len -= 3;
510                 num_rules++;
511
512                 /* Note: this is not a IEEE requirement but
513                  * simply a memory requirement */
514                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
515                         return NULL;
516         }
517
518         country_ie = triplets_start;
519         country_ie_len = len_at_triplet;
520
521         size_of_regd = sizeof(struct ieee80211_regdomain) +
522                 (num_rules * sizeof(struct ieee80211_reg_rule));
523
524         rd = kzalloc(size_of_regd, GFP_KERNEL);
525         if (!rd)
526                 return NULL;
527
528         rd->n_reg_rules = num_rules;
529         rd->alpha2[0] = alpha2[0];
530         rd->alpha2[1] = alpha2[1];
531
532         /* This time around we fill in the rd */
533         while (country_ie_len >= 3) {
534                 int end_channel = 0;
535                 struct ieee80211_country_ie_triplet *triplet =
536                         (struct ieee80211_country_ie_triplet *) country_ie;
537                 struct ieee80211_reg_rule *reg_rule = NULL;
538                 struct ieee80211_freq_range *freq_range = NULL;
539                 struct ieee80211_power_rule *power_rule = NULL;
540
541                 /* Must parse if dot11RegulatoryClassesRequired is true,
542                  * we don't support this yet */
543                 if (triplet->ext.reg_extension_id >=
544                                 IEEE80211_COUNTRY_EXTENSION_ID) {
545                         country_ie += 3;
546                         country_ie_len -= 3;
547                         continue;
548                 }
549
550                 reg_rule = &rd->reg_rules[i];
551                 freq_range = &reg_rule->freq_range;
552                 power_rule = &reg_rule->power_rule;
553
554                 reg_rule->flags = flags;
555
556                 /* 2 GHz */
557                 if (triplet->chans.first_channel <= 14)
558                         end_channel = triplet->chans.first_channel +
559                                 triplet->chans.num_channels;
560                 else
561                         /*
562                          * 5 GHz -- For example in country IEs if the first
563                          * channel given is 36 and the number of channels is 4
564                          * then the individual channel numbers defined for the
565                          * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
566                          * and not 36, 37, 38, 39.
567                          *
568                          * See: http://tinyurl.com/11d-clarification
569                          */
570                         end_channel =  triplet->chans.first_channel +
571                                 (4 * (triplet->chans.num_channels - 1));
572
573                 /* The +10 is since the regulatory domain expects
574                  * the actual band edge, not the center of freq for
575                  * its start and end freqs, assuming 20 MHz bandwidth on
576                  * the channels passed */
577                 freq_range->start_freq_khz =
578                         MHZ_TO_KHZ(ieee80211_channel_to_frequency(
579                                 triplet->chans.first_channel) - 10);
580                 freq_range->end_freq_khz =
581                         MHZ_TO_KHZ(ieee80211_channel_to_frequency(
582                                 end_channel) + 10);
583
584                 /* Large arbitrary values, we intersect later */
585                 /* Increment this if we ever support >= 40 MHz channels
586                  * in IEEE 802.11 */
587                 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
588                 power_rule->max_antenna_gain = DBI_TO_MBI(100);
589                 power_rule->max_eirp = DBM_TO_MBM(100);
590
591                 country_ie += 3;
592                 country_ie_len -= 3;
593                 i++;
594
595                 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
596         }
597
598         return rd;
599 }
600
601
602 /* Helper for regdom_intersect(), this does the real
603  * mathematical intersection fun */
604 static int reg_rules_intersect(
605         const struct ieee80211_reg_rule *rule1,
606         const struct ieee80211_reg_rule *rule2,
607         struct ieee80211_reg_rule *intersected_rule)
608 {
609         const struct ieee80211_freq_range *freq_range1, *freq_range2;
610         struct ieee80211_freq_range *freq_range;
611         const struct ieee80211_power_rule *power_rule1, *power_rule2;
612         struct ieee80211_power_rule *power_rule;
613         u32 freq_diff;
614
615         freq_range1 = &rule1->freq_range;
616         freq_range2 = &rule2->freq_range;
617         freq_range = &intersected_rule->freq_range;
618
619         power_rule1 = &rule1->power_rule;
620         power_rule2 = &rule2->power_rule;
621         power_rule = &intersected_rule->power_rule;
622
623         freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
624                 freq_range2->start_freq_khz);
625         freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
626                 freq_range2->end_freq_khz);
627         freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
628                 freq_range2->max_bandwidth_khz);
629
630         freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
631         if (freq_range->max_bandwidth_khz > freq_diff)
632                 freq_range->max_bandwidth_khz = freq_diff;
633
634         power_rule->max_eirp = min(power_rule1->max_eirp,
635                 power_rule2->max_eirp);
636         power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
637                 power_rule2->max_antenna_gain);
638
639         intersected_rule->flags = (rule1->flags | rule2->flags);
640
641         if (!is_valid_reg_rule(intersected_rule))
642                 return -EINVAL;
643
644         return 0;
645 }
646
647 /**
648  * regdom_intersect - do the intersection between two regulatory domains
649  * @rd1: first regulatory domain
650  * @rd2: second regulatory domain
651  *
652  * Use this function to get the intersection between two regulatory domains.
653  * Once completed we will mark the alpha2 for the rd as intersected, "98",
654  * as no one single alpha2 can represent this regulatory domain.
655  *
656  * Returns a pointer to the regulatory domain structure which will hold the
657  * resulting intersection of rules between rd1 and rd2. We will
658  * kzalloc() this structure for you.
659  */
660 static struct ieee80211_regdomain *regdom_intersect(
661         const struct ieee80211_regdomain *rd1,
662         const struct ieee80211_regdomain *rd2)
663 {
664         int r, size_of_regd;
665         unsigned int x, y;
666         unsigned int num_rules = 0, rule_idx = 0;
667         const struct ieee80211_reg_rule *rule1, *rule2;
668         struct ieee80211_reg_rule *intersected_rule;
669         struct ieee80211_regdomain *rd;
670         /* This is just a dummy holder to help us count */
671         struct ieee80211_reg_rule irule;
672
673         /* Uses the stack temporarily for counter arithmetic */
674         intersected_rule = &irule;
675
676         memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
677
678         if (!rd1 || !rd2)
679                 return NULL;
680
681         /* First we get a count of the rules we'll need, then we actually
682          * build them. This is to so we can malloc() and free() a
683          * regdomain once. The reason we use reg_rules_intersect() here
684          * is it will return -EINVAL if the rule computed makes no sense.
685          * All rules that do check out OK are valid. */
686
687         for (x = 0; x < rd1->n_reg_rules; x++) {
688                 rule1 = &rd1->reg_rules[x];
689                 for (y = 0; y < rd2->n_reg_rules; y++) {
690                         rule2 = &rd2->reg_rules[y];
691                         if (!reg_rules_intersect(rule1, rule2,
692                                         intersected_rule))
693                                 num_rules++;
694                         memset(intersected_rule, 0,
695                                         sizeof(struct ieee80211_reg_rule));
696                 }
697         }
698
699         if (!num_rules)
700                 return NULL;
701
702         size_of_regd = sizeof(struct ieee80211_regdomain) +
703                 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
704
705         rd = kzalloc(size_of_regd, GFP_KERNEL);
706         if (!rd)
707                 return NULL;
708
709         for (x = 0; x < rd1->n_reg_rules; x++) {
710                 rule1 = &rd1->reg_rules[x];
711                 for (y = 0; y < rd2->n_reg_rules; y++) {
712                         rule2 = &rd2->reg_rules[y];
713                         /* This time around instead of using the stack lets
714                          * write to the target rule directly saving ourselves
715                          * a memcpy() */
716                         intersected_rule = &rd->reg_rules[rule_idx];
717                         r = reg_rules_intersect(rule1, rule2,
718                                 intersected_rule);
719                         /* No need to memset here the intersected rule here as
720                          * we're not using the stack anymore */
721                         if (r)
722                                 continue;
723                         rule_idx++;
724                 }
725         }
726
727         if (rule_idx != num_rules) {
728                 kfree(rd);
729                 return NULL;
730         }
731
732         rd->n_reg_rules = num_rules;
733         rd->alpha2[0] = '9';
734         rd->alpha2[1] = '8';
735
736         return rd;
737 }
738
739 /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
740  * want to just have the channel structure use these */
741 static u32 map_regdom_flags(u32 rd_flags)
742 {
743         u32 channel_flags = 0;
744         if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
745                 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
746         if (rd_flags & NL80211_RRF_NO_IBSS)
747                 channel_flags |= IEEE80211_CHAN_NO_IBSS;
748         if (rd_flags & NL80211_RRF_DFS)
749                 channel_flags |= IEEE80211_CHAN_RADAR;
750         return channel_flags;
751 }
752
753 static int freq_reg_info_regd(struct wiphy *wiphy,
754                               u32 center_freq,
755                               u32 *bandwidth,
756                               const struct ieee80211_reg_rule **reg_rule,
757                               const struct ieee80211_regdomain *custom_regd)
758 {
759         int i;
760         bool band_rule_found = false;
761         const struct ieee80211_regdomain *regd;
762         u32 max_bandwidth = 0;
763
764         regd = custom_regd ? custom_regd : cfg80211_regdomain;
765
766         /* Follow the driver's regulatory domain, if present, unless a country
767          * IE has been processed or a user wants to help complaince further */
768         if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
769             last_request->initiator != REGDOM_SET_BY_USER &&
770             wiphy->regd)
771                 regd = wiphy->regd;
772
773         if (!regd)
774                 return -EINVAL;
775
776         for (i = 0; i < regd->n_reg_rules; i++) {
777                 const struct ieee80211_reg_rule *rr;
778                 const struct ieee80211_freq_range *fr = NULL;
779                 const struct ieee80211_power_rule *pr = NULL;
780
781                 rr = &regd->reg_rules[i];
782                 fr = &rr->freq_range;
783                 pr = &rr->power_rule;
784
785                 /* We only need to know if one frequency rule was
786                  * was in center_freq's band, that's enough, so lets
787                  * not overwrite it once found */
788                 if (!band_rule_found)
789                         band_rule_found = freq_in_rule_band(fr, center_freq);
790
791                 max_bandwidth = freq_max_bandwidth(fr, center_freq);
792
793                 if (max_bandwidth && *bandwidth <= max_bandwidth) {
794                         *reg_rule = rr;
795                         *bandwidth = max_bandwidth;
796                         break;
797                 }
798         }
799
800         if (!band_rule_found)
801                 return -ERANGE;
802
803         return !max_bandwidth;
804 }
805 EXPORT_SYMBOL(freq_reg_info);
806
807 int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
808                          const struct ieee80211_reg_rule **reg_rule)
809 {
810         return freq_reg_info_regd(wiphy, center_freq,
811                 bandwidth, reg_rule, NULL);
812 }
813
814 static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
815                            unsigned int chan_idx)
816 {
817         int r;
818         u32 flags;
819         u32 max_bandwidth = 0;
820         const struct ieee80211_reg_rule *reg_rule = NULL;
821         const struct ieee80211_power_rule *power_rule = NULL;
822         struct ieee80211_supported_band *sband;
823         struct ieee80211_channel *chan;
824
825         sband = wiphy->bands[band];
826         BUG_ON(chan_idx >= sband->n_channels);
827         chan = &sband->channels[chan_idx];
828
829         flags = chan->orig_flags;
830
831         r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
832                 &max_bandwidth, &reg_rule);
833
834         if (r) {
835                 /* This means no regulatory rule was found in the country IE
836                  * with a frequency range on the center_freq's band, since
837                  * IEEE-802.11 allows for a country IE to have a subset of the
838                  * regulatory information provided in a country we ignore
839                  * disabling the channel unless at least one reg rule was
840                  * found on the center_freq's band. For details see this
841                  * clarification:
842                  *
843                  * http://tinyurl.com/11d-clarification
844                  */
845                 if (r == -ERANGE &&
846                     last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
847 #ifdef CONFIG_CFG80211_REG_DEBUG
848                         printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
849                                 "intact on %s - no rule found in band on "
850                                 "Country IE\n",
851                                 chan->center_freq, wiphy_name(wiphy));
852 #endif
853                 } else {
854                 /* In this case we know the country IE has at least one reg rule
855                  * for the band so we respect its band definitions */
856 #ifdef CONFIG_CFG80211_REG_DEBUG
857                         if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
858                                 printk(KERN_DEBUG "cfg80211: Disabling "
859                                         "channel %d MHz on %s due to "
860                                         "Country IE\n",
861                                         chan->center_freq, wiphy_name(wiphy));
862 #endif
863                         flags |= IEEE80211_CHAN_DISABLED;
864                         chan->flags = flags;
865                 }
866                 return;
867         }
868
869         power_rule = &reg_rule->power_rule;
870
871         if (last_request->initiator == REGDOM_SET_BY_DRIVER &&
872             last_request->wiphy && last_request->wiphy == wiphy &&
873             last_request->wiphy->strict_regulatory) {
874                 /* This gaurantees the driver's requested regulatory domain
875                  * will always be used as a base for further regulatory
876                  * settings */
877                 chan->flags = chan->orig_flags =
878                         map_regdom_flags(reg_rule->flags);
879                 chan->max_antenna_gain = chan->orig_mag =
880                         (int) MBI_TO_DBI(power_rule->max_antenna_gain);
881                 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
882                 chan->max_power = chan->orig_mpwr =
883                         (int) MBM_TO_DBM(power_rule->max_eirp);
884                 return;
885         }
886
887         chan->flags = flags | map_regdom_flags(reg_rule->flags);
888         chan->max_antenna_gain = min(chan->orig_mag,
889                 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
890         chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
891         if (chan->orig_mpwr)
892                 chan->max_power = min(chan->orig_mpwr,
893                         (int) MBM_TO_DBM(power_rule->max_eirp));
894         else
895                 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
896 }
897
898 static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
899 {
900         unsigned int i;
901         struct ieee80211_supported_band *sband;
902
903         BUG_ON(!wiphy->bands[band]);
904         sband = wiphy->bands[band];
905
906         for (i = 0; i < sband->n_channels; i++)
907                 handle_channel(wiphy, band, i);
908 }
909
910 static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
911 {
912         if (!last_request)
913                 return true;
914         if (setby == REGDOM_SET_BY_CORE &&
915                   wiphy->custom_regulatory)
916                 return true;
917         /* wiphy->regd will be set once the device has its own
918          * desired regulatory domain set */
919         if (wiphy->strict_regulatory && !wiphy->regd &&
920             !is_world_regdom(last_request->alpha2))
921                 return true;
922         return false;
923 }
924
925 static void update_all_wiphy_regulatory(enum reg_set_by setby)
926 {
927         struct cfg80211_registered_device *drv;
928
929         list_for_each_entry(drv, &cfg80211_drv_list, list)
930                 wiphy_update_regulatory(&drv->wiphy, setby);
931 }
932
933 void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
934 {
935         enum ieee80211_band band;
936
937         if (ignore_reg_update(wiphy, setby))
938                 return;
939         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
940                 if (wiphy->bands[band])
941                         handle_band(wiphy, band);
942         }
943         if (wiphy->reg_notifier)
944                 wiphy->reg_notifier(wiphy, last_request);
945 }
946
947 static void handle_channel_custom(struct wiphy *wiphy,
948                                   enum ieee80211_band band,
949                                   unsigned int chan_idx,
950                                   const struct ieee80211_regdomain *regd)
951 {
952         int r;
953         u32 max_bandwidth = 0;
954         const struct ieee80211_reg_rule *reg_rule = NULL;
955         const struct ieee80211_power_rule *power_rule = NULL;
956         struct ieee80211_supported_band *sband;
957         struct ieee80211_channel *chan;
958
959         sband = wiphy->bands[band];
960         BUG_ON(chan_idx >= sband->n_channels);
961         chan = &sband->channels[chan_idx];
962
963         r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
964                 &max_bandwidth, &reg_rule, regd);
965
966         if (r) {
967                 chan->flags = IEEE80211_CHAN_DISABLED;
968                 return;
969         }
970
971         power_rule = &reg_rule->power_rule;
972
973         chan->flags |= map_regdom_flags(reg_rule->flags);
974         chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
975         chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
976         chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
977 }
978
979 static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
980                                const struct ieee80211_regdomain *regd)
981 {
982         unsigned int i;
983         struct ieee80211_supported_band *sband;
984
985         BUG_ON(!wiphy->bands[band]);
986         sband = wiphy->bands[band];
987
988         for (i = 0; i < sband->n_channels; i++)
989                 handle_channel_custom(wiphy, band, i, regd);
990 }
991
992 /* Used by drivers prior to wiphy registration */
993 void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
994                                    const struct ieee80211_regdomain *regd)
995 {
996         enum ieee80211_band band;
997         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
998                 if (wiphy->bands[band])
999                         handle_band_custom(wiphy, band, regd);
1000         }
1001 }
1002 EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1003
1004 static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1005                          const struct ieee80211_regdomain *src_regd)
1006 {
1007         struct ieee80211_regdomain *regd;
1008         int size_of_regd = 0;
1009         unsigned int i;
1010
1011         size_of_regd = sizeof(struct ieee80211_regdomain) +
1012           ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1013
1014         regd = kzalloc(size_of_regd, GFP_KERNEL);
1015         if (!regd)
1016                 return -ENOMEM;
1017
1018         memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1019
1020         for (i = 0; i < src_regd->n_reg_rules; i++)
1021                 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1022                         sizeof(struct ieee80211_reg_rule));
1023
1024         *dst_regd = regd;
1025         return 0;
1026 }
1027
1028 /* Return value which can be used by ignore_request() to indicate
1029  * it has been determined we should intersect two regulatory domains */
1030 #define REG_INTERSECT   1
1031
1032 /* This has the logic which determines when a new request
1033  * should be ignored. */
1034 static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
1035                           const char *alpha2)
1036 {
1037         /* All initial requests are respected */
1038         if (!last_request)
1039                 return 0;
1040
1041         switch (set_by) {
1042         case REGDOM_SET_BY_INIT:
1043                 return -EINVAL;
1044         case REGDOM_SET_BY_CORE:
1045                 /*
1046                  * Always respect new wireless core hints, should only happen
1047                  * when updating the world regulatory domain at init.
1048                  */
1049                 return 0;
1050         case REGDOM_SET_BY_COUNTRY_IE:
1051                 if (unlikely(!is_an_alpha2(alpha2)))
1052                         return -EINVAL;
1053                 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1054                         if (last_request->wiphy != wiphy) {
1055                                 /*
1056                                  * Two cards with two APs claiming different
1057                                  * different Country IE alpha2s. We could
1058                                  * intersect them, but that seems unlikely
1059                                  * to be correct. Reject second one for now.
1060                                  */
1061                                 if (!alpha2_equal(alpha2,
1062                                                   cfg80211_regdomain->alpha2))
1063                                         return -EOPNOTSUPP;
1064                                 return -EALREADY;
1065                         }
1066                         /* Two consecutive Country IE hints on the same wiphy.
1067                          * This should be picked up early by the driver/stack */
1068                         if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
1069                                   alpha2)))
1070                                 return 0;
1071                         return -EALREADY;
1072                 }
1073                 return REG_INTERSECT;
1074         case REGDOM_SET_BY_DRIVER:
1075                 if (last_request->initiator == REGDOM_SET_BY_CORE) {
1076                         if (is_old_static_regdom(cfg80211_regdomain))
1077                                 return 0;
1078                         if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
1079                                 return 0;
1080                         return -EALREADY;
1081                 }
1082                 return REG_INTERSECT;
1083         case REGDOM_SET_BY_USER:
1084                 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
1085                         return REG_INTERSECT;
1086                 /* If the user knows better the user should set the regdom
1087                  * to their country before the IE is picked up */
1088                 if (last_request->initiator == REGDOM_SET_BY_USER &&
1089                           last_request->intersect)
1090                         return -EOPNOTSUPP;
1091                 /* Process user requests only after previous user/driver/core
1092                  * requests have been processed */
1093                 if (last_request->initiator == REGDOM_SET_BY_CORE ||
1094                     last_request->initiator == REGDOM_SET_BY_DRIVER ||
1095                     last_request->initiator == REGDOM_SET_BY_USER) {
1096                         if (!alpha2_equal(last_request->alpha2,
1097                             cfg80211_regdomain->alpha2))
1098                                 return -EAGAIN;
1099                 }
1100
1101                 if (!is_old_static_regdom(cfg80211_regdomain) &&
1102                     alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
1103                         return -EALREADY;
1104
1105                 return 0;
1106         }
1107
1108         return -EINVAL;
1109 }
1110
1111 /* Caller must hold &cfg80211_drv_mutex */
1112 int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
1113                         const char *alpha2,
1114                         u32 country_ie_checksum,
1115                         enum environment_cap env)
1116 {
1117         struct regulatory_request *request;
1118         bool intersect = false;
1119         int r = 0;
1120
1121         r = ignore_request(wiphy, set_by, alpha2);
1122
1123         if (r == REG_INTERSECT) {
1124                 if (set_by == REGDOM_SET_BY_DRIVER) {
1125                         r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1126                         if (r)
1127                                 return r;
1128                 }
1129                 intersect = true;
1130         } else if (r) {
1131                 /* If the regulatory domain being requested by the
1132                  * driver has already been set just copy it to the
1133                  * wiphy */
1134                 if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
1135                         r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1136                         if (r)
1137                                 return r;
1138                         r = -EALREADY;
1139                         goto new_request;
1140                 }
1141                 return r;
1142         }
1143
1144 new_request:
1145         request = kzalloc(sizeof(struct regulatory_request),
1146                           GFP_KERNEL);
1147         if (!request)
1148                 return -ENOMEM;
1149
1150         request->alpha2[0] = alpha2[0];
1151         request->alpha2[1] = alpha2[1];
1152         request->initiator = set_by;
1153         request->wiphy = wiphy;
1154         request->intersect = intersect;
1155         request->country_ie_checksum = country_ie_checksum;
1156         request->country_ie_env = env;
1157
1158         kfree(last_request);
1159         last_request = request;
1160
1161         /* When r == REG_INTERSECT we do need to call CRDA */
1162         if (r < 0)
1163                 return r;
1164
1165         /*
1166          * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1167          * AND if CRDA is NOT present nothing will happen, if someone
1168          * wants to bother with 11d with OLD_REG you can add a timer.
1169          * If after x amount of time nothing happens you can call:
1170          *
1171          * return set_regdom(country_ie_regdomain);
1172          *
1173          * to intersect with the static rd
1174          */
1175         return call_crda(alpha2);
1176 }
1177
1178 void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1179 {
1180         int r;
1181         BUG_ON(!alpha2);
1182
1183         mutex_lock(&cfg80211_drv_mutex);
1184         r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER,
1185                 alpha2, 0, ENVIRON_ANY);
1186         /* This is required so that the orig_* parameters are saved */
1187         if (r == -EALREADY && wiphy->strict_regulatory)
1188                 wiphy_update_regulatory(wiphy, REGDOM_SET_BY_DRIVER);
1189         mutex_unlock(&cfg80211_drv_mutex);
1190 }
1191 EXPORT_SYMBOL(regulatory_hint);
1192
1193 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1194                         u32 country_ie_checksum)
1195 {
1196         if (!last_request->wiphy)
1197                 return false;
1198         if (likely(last_request->wiphy != wiphy))
1199                 return !country_ie_integrity_changes(country_ie_checksum);
1200         /* We should not have let these through at this point, they
1201          * should have been picked up earlier by the first alpha2 check
1202          * on the device */
1203         if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1204                 return true;
1205         return false;
1206 }
1207
1208 void regulatory_hint_11d(struct wiphy *wiphy,
1209                         u8 *country_ie,
1210                         u8 country_ie_len)
1211 {
1212         struct ieee80211_regdomain *rd = NULL;
1213         char alpha2[2];
1214         u32 checksum = 0;
1215         enum environment_cap env = ENVIRON_ANY;
1216
1217         if (!last_request)
1218                 return;
1219
1220         mutex_lock(&cfg80211_drv_mutex);
1221
1222         /* IE len must be evenly divisible by 2 */
1223         if (country_ie_len & 0x01)
1224                 goto out;
1225
1226         if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1227                 goto out;
1228
1229         /* Pending country IE processing, this can happen after we
1230          * call CRDA and wait for a response if a beacon was received before
1231          * we were able to process the last regulatory_hint_11d() call */
1232         if (country_ie_regdomain)
1233                 goto out;
1234
1235         alpha2[0] = country_ie[0];
1236         alpha2[1] = country_ie[1];
1237
1238         if (country_ie[2] == 'I')
1239                 env = ENVIRON_INDOOR;
1240         else if (country_ie[2] == 'O')
1241                 env = ENVIRON_OUTDOOR;
1242
1243         /* We will run this for *every* beacon processed for the BSSID, so
1244          * we optimize an early check to exit out early if we don't have to
1245          * do anything */
1246         if (likely(last_request->wiphy)) {
1247                 struct cfg80211_registered_device *drv_last_ie;
1248
1249                 drv_last_ie = wiphy_to_dev(last_request->wiphy);
1250
1251                 /* Lets keep this simple -- we trust the first AP
1252                  * after we intersect with CRDA */
1253                 if (likely(last_request->wiphy == wiphy)) {
1254                         /* Ignore IEs coming in on this wiphy with
1255                          * the same alpha2 and environment cap */
1256                         if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1257                                   alpha2) &&
1258                                   env == drv_last_ie->env)) {
1259                                 goto out;
1260                         }
1261                         /* the wiphy moved on to another BSSID or the AP
1262                          * was reconfigured. XXX: We need to deal with the
1263                          * case where the user suspends and goes to goes
1264                          * to another country, and then gets IEs from an
1265                          * AP with different settings */
1266                         goto out;
1267                 } else {
1268                         /* Ignore IEs coming in on two separate wiphys with
1269                          * the same alpha2 and environment cap */
1270                         if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1271                                   alpha2) &&
1272                                   env == drv_last_ie->env)) {
1273                                 goto out;
1274                         }
1275                         /* We could potentially intersect though */
1276                         goto out;
1277                 }
1278         }
1279
1280         rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1281         if (!rd)
1282                 goto out;
1283
1284         /* This will not happen right now but we leave it here for the
1285          * the future when we want to add suspend/resume support and having
1286          * the user move to another country after doing so, or having the user
1287          * move to another AP. Right now we just trust the first AP. This is why
1288          * this is marked as likley(). If we hit this before we add this support
1289          * we want to be informed of it as it would indicate a mistake in the
1290          * current design  */
1291         if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
1292                 goto out;
1293
1294         /* We keep this around for when CRDA comes back with a response so
1295          * we can intersect with that */
1296         country_ie_regdomain = rd;
1297
1298         __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
1299                 country_ie_regdomain->alpha2, checksum, env);
1300
1301 out:
1302         mutex_unlock(&cfg80211_drv_mutex);
1303 }
1304 EXPORT_SYMBOL(regulatory_hint_11d);
1305
1306 static void print_rd_rules(const struct ieee80211_regdomain *rd)
1307 {
1308         unsigned int i;
1309         const struct ieee80211_reg_rule *reg_rule = NULL;
1310         const struct ieee80211_freq_range *freq_range = NULL;
1311         const struct ieee80211_power_rule *power_rule = NULL;
1312
1313         printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1314                 "(max_antenna_gain, max_eirp)\n");
1315
1316         for (i = 0; i < rd->n_reg_rules; i++) {
1317                 reg_rule = &rd->reg_rules[i];
1318                 freq_range = &reg_rule->freq_range;
1319                 power_rule = &reg_rule->power_rule;
1320
1321                 /* There may not be documentation for max antenna gain
1322                  * in certain regions */
1323                 if (power_rule->max_antenna_gain)
1324                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1325                                 "(%d mBi, %d mBm)\n",
1326                                 freq_range->start_freq_khz,
1327                                 freq_range->end_freq_khz,
1328                                 freq_range->max_bandwidth_khz,
1329                                 power_rule->max_antenna_gain,
1330                                 power_rule->max_eirp);
1331                 else
1332                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1333                                 "(N/A, %d mBm)\n",
1334                                 freq_range->start_freq_khz,
1335                                 freq_range->end_freq_khz,
1336                                 freq_range->max_bandwidth_khz,
1337                                 power_rule->max_eirp);
1338         }
1339 }
1340
1341 static void print_regdomain(const struct ieee80211_regdomain *rd)
1342 {
1343
1344         if (is_intersected_alpha2(rd->alpha2)) {
1345                 struct wiphy *wiphy = NULL;
1346                 struct cfg80211_registered_device *drv;
1347
1348                 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1349                         if (last_request->wiphy) {
1350                                 wiphy = last_request->wiphy;
1351                                 drv = wiphy_to_dev(wiphy);
1352                                 printk(KERN_INFO "cfg80211: Current regulatory "
1353                                         "domain updated by AP to: %c%c\n",
1354                                         drv->country_ie_alpha2[0],
1355                                         drv->country_ie_alpha2[1]);
1356                         } else
1357                                 printk(KERN_INFO "cfg80211: Current regulatory "
1358                                         "domain intersected: \n");
1359                 } else
1360                                 printk(KERN_INFO "cfg80211: Current regulatory "
1361                                         "domain intersected: \n");
1362         } else if (is_world_regdom(rd->alpha2))
1363                 printk(KERN_INFO "cfg80211: World regulatory "
1364                         "domain updated:\n");
1365         else {
1366                 if (is_unknown_alpha2(rd->alpha2))
1367                         printk(KERN_INFO "cfg80211: Regulatory domain "
1368                                 "changed to driver built-in settings "
1369                                 "(unknown country)\n");
1370                 else
1371                         printk(KERN_INFO "cfg80211: Regulatory domain "
1372                                 "changed to country: %c%c\n",
1373                                 rd->alpha2[0], rd->alpha2[1]);
1374         }
1375         print_rd_rules(rd);
1376 }
1377
1378 static void print_regdomain_info(const struct ieee80211_regdomain *rd)
1379 {
1380         printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1381                 rd->alpha2[0], rd->alpha2[1]);
1382         print_rd_rules(rd);
1383 }
1384
1385 #ifdef CONFIG_CFG80211_REG_DEBUG
1386 static void reg_country_ie_process_debug(
1387         const struct ieee80211_regdomain *rd,
1388         const struct ieee80211_regdomain *country_ie_regdomain,
1389         const struct ieee80211_regdomain *intersected_rd)
1390 {
1391         printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1392         print_regdomain_info(country_ie_regdomain);
1393         printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1394         print_regdomain_info(rd);
1395         if (intersected_rd) {
1396                 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1397                         "and get:\n");
1398                 print_regdomain_info(rd);
1399                 return;
1400         }
1401         printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1402 }
1403 #else
1404 static inline void reg_country_ie_process_debug(
1405         const struct ieee80211_regdomain *rd,
1406         const struct ieee80211_regdomain *country_ie_regdomain,
1407         const struct ieee80211_regdomain *intersected_rd)
1408 {
1409 }
1410 #endif
1411
1412 /* Takes ownership of rd only if it doesn't fail */
1413 static int __set_regdom(const struct ieee80211_regdomain *rd)
1414 {
1415         const struct ieee80211_regdomain *intersected_rd = NULL;
1416         struct cfg80211_registered_device *drv = NULL;
1417         struct wiphy *wiphy = NULL;
1418         /* Some basic sanity checks first */
1419
1420         if (is_world_regdom(rd->alpha2)) {
1421                 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1422                         return -EINVAL;
1423                 update_world_regdomain(rd);
1424                 return 0;
1425         }
1426
1427         if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1428                         !is_unknown_alpha2(rd->alpha2))
1429                 return -EINVAL;
1430
1431         if (!last_request)
1432                 return -EINVAL;
1433
1434         /* Lets only bother proceeding on the same alpha2 if the current
1435          * rd is non static (it means CRDA was present and was used last)
1436          * and the pending request came in from a country IE */
1437         if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1438                 /* If someone else asked us to change the rd lets only bother
1439                  * checking if the alpha2 changes if CRDA was already called */
1440                 if (!is_old_static_regdom(cfg80211_regdomain) &&
1441                     !regdom_changed(rd->alpha2))
1442                         return -EINVAL;
1443         }
1444
1445         wiphy = last_request->wiphy;
1446
1447         /* Now lets set the regulatory domain, update all driver channels
1448          * and finally inform them of what we have done, in case they want
1449          * to review or adjust their own settings based on their own
1450          * internal EEPROM data */
1451
1452         if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
1453                 return -EINVAL;
1454
1455         if (!is_valid_rd(rd)) {
1456                 printk(KERN_ERR "cfg80211: Invalid "
1457                         "regulatory domain detected:\n");
1458                 print_regdomain_info(rd);
1459                 return -EINVAL;
1460         }
1461
1462         if (!last_request->intersect) {
1463                 int r;
1464
1465                 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1466                         reset_regdomains();
1467                         cfg80211_regdomain = rd;
1468                         return 0;
1469                 }
1470
1471                 /* For a driver hint, lets copy the regulatory domain the
1472                  * driver wanted to the wiphy to deal with conflicts */
1473
1474                 BUG_ON(last_request->wiphy->regd);
1475
1476                 r = reg_copy_regd(&last_request->wiphy->regd, rd);
1477                 if (r)
1478                         return r;
1479
1480                 reset_regdomains();
1481                 cfg80211_regdomain = rd;
1482                 return 0;
1483         }
1484
1485         /* Intersection requires a bit more work */
1486
1487         if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1488
1489                 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1490                 if (!intersected_rd)
1491                         return -EINVAL;
1492
1493                 /* We can trash what CRDA provided now.
1494                  * However if a driver requested this specific regulatory
1495                  * domain we keep it for its private use */
1496                 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
1497                         last_request->wiphy->regd = rd;
1498                 else
1499                         kfree(rd);
1500
1501                 rd = NULL;
1502
1503                 reset_regdomains();
1504                 cfg80211_regdomain = intersected_rd;
1505
1506                 return 0;
1507         }
1508
1509         /*
1510          * Country IE requests are handled a bit differently, we intersect
1511          * the country IE rd with what CRDA believes that country should have
1512          */
1513
1514         BUG_ON(!country_ie_regdomain);
1515
1516         if (rd != country_ie_regdomain) {
1517                 /* Intersect what CRDA returned and our what we
1518                  * had built from the Country IE received */
1519
1520                 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1521
1522                 reg_country_ie_process_debug(rd, country_ie_regdomain,
1523                         intersected_rd);
1524
1525                 kfree(country_ie_regdomain);
1526                 country_ie_regdomain = NULL;
1527         } else {
1528                 /* This would happen when CRDA was not present and
1529                  * OLD_REGULATORY was enabled. We intersect our Country
1530                  * IE rd and what was set on cfg80211 originally */
1531                 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1532         }
1533
1534         if (!intersected_rd)
1535                 return -EINVAL;
1536
1537         drv = wiphy_to_dev(wiphy);
1538
1539         drv->country_ie_alpha2[0] = rd->alpha2[0];
1540         drv->country_ie_alpha2[1] = rd->alpha2[1];
1541         drv->env = last_request->country_ie_env;
1542
1543         BUG_ON(intersected_rd == rd);
1544
1545         kfree(rd);
1546         rd = NULL;
1547
1548         reset_regdomains();
1549         cfg80211_regdomain = intersected_rd;
1550
1551         return 0;
1552 }
1553
1554
1555 /* Use this call to set the current regulatory domain. Conflicts with
1556  * multiple drivers can be ironed out later. Caller must've already
1557  * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
1558 int set_regdom(const struct ieee80211_regdomain *rd)
1559 {
1560         int r;
1561
1562         /* Note that this doesn't update the wiphys, this is done below */
1563         r = __set_regdom(rd);
1564         if (r) {
1565                 kfree(rd);
1566                 return r;
1567         }
1568
1569         /* This would make this whole thing pointless */
1570         if (!last_request->intersect)
1571                 BUG_ON(rd != cfg80211_regdomain);
1572
1573         /* update all wiphys now with the new established regulatory domain */
1574         update_all_wiphy_regulatory(last_request->initiator);
1575
1576         print_regdomain(cfg80211_regdomain);
1577
1578         return r;
1579 }
1580
1581 /* Caller must hold cfg80211_drv_mutex */
1582 void reg_device_remove(struct wiphy *wiphy)
1583 {
1584         kfree(wiphy->regd);
1585         if (!last_request || !last_request->wiphy)
1586                 return;
1587         if (last_request->wiphy != wiphy)
1588                 return;
1589         last_request->wiphy = NULL;
1590         last_request->country_ie_env = ENVIRON_ANY;
1591 }
1592
1593 int regulatory_init(void)
1594 {
1595         int err;
1596
1597         reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1598         if (IS_ERR(reg_pdev))
1599                 return PTR_ERR(reg_pdev);
1600
1601 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
1602         cfg80211_regdomain = static_regdom(ieee80211_regdom);
1603
1604         printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
1605         print_regdomain_info(cfg80211_regdomain);
1606         /* The old code still requests for a new regdomain and if
1607          * you have CRDA you get it updated, otherwise you get
1608          * stuck with the static values. We ignore "EU" code as
1609          * that is not a valid ISO / IEC 3166 alpha2 */
1610         if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
1611                 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
1612                                         ieee80211_regdom, 0, ENVIRON_ANY);
1613 #else
1614         cfg80211_regdomain = cfg80211_world_regdom;
1615
1616         err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
1617         if (err)
1618                 printk(KERN_ERR "cfg80211: calling CRDA failed - "
1619                        "unable to update world regulatory domain, "
1620                        "using static definition\n");
1621 #endif
1622
1623         return 0;
1624 }
1625
1626 void regulatory_exit(void)
1627 {
1628         mutex_lock(&cfg80211_drv_mutex);
1629
1630         reset_regdomains();
1631
1632         kfree(country_ie_regdomain);
1633         country_ie_regdomain = NULL;
1634
1635         kfree(last_request);
1636
1637         platform_device_unregister(reg_pdev);
1638
1639         mutex_unlock(&cfg80211_drv_mutex);
1640 }