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