cfg80211: add wiphy_apply_custom_regulatory()
[deliverable/linux.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
0c7dc45d
LR
424/**
425 * freq_in_rule_band - tells us if a frequency is in a frequency band
426 * @freq_range: frequency rule we want to query
427 * @freq_khz: frequency we are inquiring about
428 *
429 * This lets us know if a specific frequency rule is or is not relevant to
430 * a specific frequency's band. Bands are device specific and artificial
431 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
432 * safe for now to assume that a frequency rule should not be part of a
433 * frequency's band if the start freq or end freq are off by more than 2 GHz.
434 * This resolution can be lowered and should be considered as we add
435 * regulatory rule support for other "bands".
436 **/
437static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
438 u32 freq_khz)
439{
440#define ONE_GHZ_IN_KHZ 1000000
441 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
442 return true;
443 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
444 return true;
445 return false;
446#undef ONE_GHZ_IN_KHZ
447}
448
3f2355cb
LR
449/* Converts a country IE to a regulatory domain. A regulatory domain
450 * structure has a lot of information which the IE doesn't yet have,
451 * so for the other values we use upper max values as we will intersect
452 * with our userspace regulatory agent to get lower bounds. */
453static struct ieee80211_regdomain *country_ie_2_rd(
454 u8 *country_ie,
455 u8 country_ie_len,
456 u32 *checksum)
457{
458 struct ieee80211_regdomain *rd = NULL;
459 unsigned int i = 0;
460 char alpha2[2];
461 u32 flags = 0;
462 u32 num_rules = 0, size_of_regd = 0;
463 u8 *triplets_start = NULL;
464 u8 len_at_triplet = 0;
465 /* the last channel we have registered in a subband (triplet) */
466 int last_sub_max_channel = 0;
467
468 *checksum = 0xDEADBEEF;
469
470 /* Country IE requirements */
471 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
472 country_ie_len & 0x01);
473
474 alpha2[0] = country_ie[0];
475 alpha2[1] = country_ie[1];
476
477 /*
478 * Third octet can be:
479 * 'I' - Indoor
480 * 'O' - Outdoor
481 *
482 * anything else we assume is no restrictions
483 */
484 if (country_ie[2] == 'I')
485 flags = NL80211_RRF_NO_OUTDOOR;
486 else if (country_ie[2] == 'O')
487 flags = NL80211_RRF_NO_INDOOR;
488
489 country_ie += 3;
490 country_ie_len -= 3;
491
492 triplets_start = country_ie;
493 len_at_triplet = country_ie_len;
494
495 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
496
497 /* We need to build a reg rule for each triplet, but first we must
498 * calculate the number of reg rules we will need. We will need one
499 * for each channel subband */
500 while (country_ie_len >= 3) {
501 struct ieee80211_country_ie_triplet *triplet =
502 (struct ieee80211_country_ie_triplet *) country_ie;
503 int cur_sub_max_channel = 0, cur_channel = 0;
504
505 if (triplet->ext.reg_extension_id >=
506 IEEE80211_COUNTRY_EXTENSION_ID) {
507 country_ie += 3;
508 country_ie_len -= 3;
509 continue;
510 }
511
512 cur_channel = triplet->chans.first_channel;
513 cur_sub_max_channel = ieee80211_channel_to_frequency(
514 cur_channel + triplet->chans.num_channels);
515
516 /* Basic sanity check */
517 if (cur_sub_max_channel < cur_channel)
518 return NULL;
519
520 /* Do not allow overlapping channels. Also channels
521 * passed in each subband must be monotonically
522 * increasing */
523 if (last_sub_max_channel) {
524 if (cur_channel <= last_sub_max_channel)
525 return NULL;
526 if (cur_sub_max_channel <= last_sub_max_channel)
527 return NULL;
528 }
529
530 /* When dot11RegulatoryClassesRequired is supported
531 * we can throw ext triplets as part of this soup,
532 * for now we don't care when those change as we
533 * don't support them */
534 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
535 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
536 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
537
538 last_sub_max_channel = cur_sub_max_channel;
539
540 country_ie += 3;
541 country_ie_len -= 3;
542 num_rules++;
543
544 /* Note: this is not a IEEE requirement but
545 * simply a memory requirement */
546 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
547 return NULL;
548 }
549
550 country_ie = triplets_start;
551 country_ie_len = len_at_triplet;
552
553 size_of_regd = sizeof(struct ieee80211_regdomain) +
554 (num_rules * sizeof(struct ieee80211_reg_rule));
555
556 rd = kzalloc(size_of_regd, GFP_KERNEL);
557 if (!rd)
558 return NULL;
559
560 rd->n_reg_rules = num_rules;
561 rd->alpha2[0] = alpha2[0];
562 rd->alpha2[1] = alpha2[1];
563
564 /* This time around we fill in the rd */
565 while (country_ie_len >= 3) {
02e68a3d 566 int end_channel = 0;
3f2355cb
LR
567 struct ieee80211_country_ie_triplet *triplet =
568 (struct ieee80211_country_ie_triplet *) country_ie;
569 struct ieee80211_reg_rule *reg_rule = NULL;
570 struct ieee80211_freq_range *freq_range = NULL;
571 struct ieee80211_power_rule *power_rule = NULL;
572
573 /* Must parse if dot11RegulatoryClassesRequired is true,
574 * we don't support this yet */
575 if (triplet->ext.reg_extension_id >=
576 IEEE80211_COUNTRY_EXTENSION_ID) {
577 country_ie += 3;
578 country_ie_len -= 3;
579 continue;
580 }
581
582 reg_rule = &rd->reg_rules[i];
583 freq_range = &reg_rule->freq_range;
584 power_rule = &reg_rule->power_rule;
585
586 reg_rule->flags = flags;
587
02e68a3d
LR
588 /* 2 GHz */
589 if (triplet->chans.first_channel <= 14)
590 end_channel = triplet->chans.first_channel +
591 triplet->chans.num_channels;
592 else
593 /*
594 * 5 GHz -- For example in country IEs if the first
595 * channel given is 36 and the number of channels is 4
596 * then the individual channel numbers defined for the
597 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
598 * and not 36, 37, 38, 39.
599 *
600 * See: http://tinyurl.com/11d-clarification
601 */
602 end_channel = triplet->chans.first_channel +
603 (4 * (triplet->chans.num_channels - 1));
604
3f2355cb
LR
605 /* The +10 is since the regulatory domain expects
606 * the actual band edge, not the center of freq for
607 * its start and end freqs, assuming 20 MHz bandwidth on
608 * the channels passed */
609 freq_range->start_freq_khz =
610 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
611 triplet->chans.first_channel) - 10);
612 freq_range->end_freq_khz =
613 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
02e68a3d 614 end_channel) + 10);
3f2355cb
LR
615
616 /* Large arbitrary values, we intersect later */
617 /* Increment this if we ever support >= 40 MHz channels
618 * in IEEE 802.11 */
619 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
620 power_rule->max_antenna_gain = DBI_TO_MBI(100);
621 power_rule->max_eirp = DBM_TO_MBM(100);
622
623 country_ie += 3;
624 country_ie_len -= 3;
625 i++;
626
627 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
628 }
629
630 return rd;
631}
632
633
9c96477d
LR
634/* Helper for regdom_intersect(), this does the real
635 * mathematical intersection fun */
636static int reg_rules_intersect(
637 const struct ieee80211_reg_rule *rule1,
638 const struct ieee80211_reg_rule *rule2,
639 struct ieee80211_reg_rule *intersected_rule)
640{
641 const struct ieee80211_freq_range *freq_range1, *freq_range2;
642 struct ieee80211_freq_range *freq_range;
643 const struct ieee80211_power_rule *power_rule1, *power_rule2;
644 struct ieee80211_power_rule *power_rule;
645 u32 freq_diff;
646
647 freq_range1 = &rule1->freq_range;
648 freq_range2 = &rule2->freq_range;
649 freq_range = &intersected_rule->freq_range;
650
651 power_rule1 = &rule1->power_rule;
652 power_rule2 = &rule2->power_rule;
653 power_rule = &intersected_rule->power_rule;
654
655 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
656 freq_range2->start_freq_khz);
657 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
658 freq_range2->end_freq_khz);
659 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
660 freq_range2->max_bandwidth_khz);
661
662 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
663 if (freq_range->max_bandwidth_khz > freq_diff)
664 freq_range->max_bandwidth_khz = freq_diff;
665
666 power_rule->max_eirp = min(power_rule1->max_eirp,
667 power_rule2->max_eirp);
668 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
669 power_rule2->max_antenna_gain);
670
671 intersected_rule->flags = (rule1->flags | rule2->flags);
672
673 if (!is_valid_reg_rule(intersected_rule))
674 return -EINVAL;
675
676 return 0;
677}
678
679/**
680 * regdom_intersect - do the intersection between two regulatory domains
681 * @rd1: first regulatory domain
682 * @rd2: second regulatory domain
683 *
684 * Use this function to get the intersection between two regulatory domains.
685 * Once completed we will mark the alpha2 for the rd as intersected, "98",
686 * as no one single alpha2 can represent this regulatory domain.
687 *
688 * Returns a pointer to the regulatory domain structure which will hold the
689 * resulting intersection of rules between rd1 and rd2. We will
690 * kzalloc() this structure for you.
691 */
692static struct ieee80211_regdomain *regdom_intersect(
693 const struct ieee80211_regdomain *rd1,
694 const struct ieee80211_regdomain *rd2)
695{
696 int r, size_of_regd;
697 unsigned int x, y;
698 unsigned int num_rules = 0, rule_idx = 0;
699 const struct ieee80211_reg_rule *rule1, *rule2;
700 struct ieee80211_reg_rule *intersected_rule;
701 struct ieee80211_regdomain *rd;
702 /* This is just a dummy holder to help us count */
703 struct ieee80211_reg_rule irule;
704
705 /* Uses the stack temporarily for counter arithmetic */
706 intersected_rule = &irule;
707
708 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
709
710 if (!rd1 || !rd2)
711 return NULL;
712
713 /* First we get a count of the rules we'll need, then we actually
714 * build them. This is to so we can malloc() and free() a
715 * regdomain once. The reason we use reg_rules_intersect() here
716 * is it will return -EINVAL if the rule computed makes no sense.
717 * All rules that do check out OK are valid. */
718
719 for (x = 0; x < rd1->n_reg_rules; x++) {
720 rule1 = &rd1->reg_rules[x];
721 for (y = 0; y < rd2->n_reg_rules; y++) {
722 rule2 = &rd2->reg_rules[y];
723 if (!reg_rules_intersect(rule1, rule2,
724 intersected_rule))
725 num_rules++;
726 memset(intersected_rule, 0,
727 sizeof(struct ieee80211_reg_rule));
728 }
729 }
730
731 if (!num_rules)
732 return NULL;
733
734 size_of_regd = sizeof(struct ieee80211_regdomain) +
735 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
736
737 rd = kzalloc(size_of_regd, GFP_KERNEL);
738 if (!rd)
739 return NULL;
740
741 for (x = 0; x < rd1->n_reg_rules; x++) {
742 rule1 = &rd1->reg_rules[x];
743 for (y = 0; y < rd2->n_reg_rules; y++) {
744 rule2 = &rd2->reg_rules[y];
745 /* This time around instead of using the stack lets
746 * write to the target rule directly saving ourselves
747 * a memcpy() */
748 intersected_rule = &rd->reg_rules[rule_idx];
749 r = reg_rules_intersect(rule1, rule2,
750 intersected_rule);
751 /* No need to memset here the intersected rule here as
752 * we're not using the stack anymore */
753 if (r)
754 continue;
755 rule_idx++;
756 }
757 }
758
759 if (rule_idx != num_rules) {
760 kfree(rd);
761 return NULL;
762 }
763
764 rd->n_reg_rules = num_rules;
765 rd->alpha2[0] = '9';
766 rd->alpha2[1] = '8';
767
768 return rd;
769}
770
b2e1b302
LR
771/* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
772 * want to just have the channel structure use these */
773static u32 map_regdom_flags(u32 rd_flags)
774{
775 u32 channel_flags = 0;
776 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
777 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
778 if (rd_flags & NL80211_RRF_NO_IBSS)
779 channel_flags |= IEEE80211_CHAN_NO_IBSS;
780 if (rd_flags & NL80211_RRF_DFS)
781 channel_flags |= IEEE80211_CHAN_RADAR;
782 return channel_flags;
783}
784
1fa25e41
LR
785static int freq_reg_info_regd(struct wiphy *wiphy,
786 u32 center_freq,
787 u32 *bandwidth,
788 const struct ieee80211_reg_rule **reg_rule,
789 const struct ieee80211_regdomain *custom_regd)
8318d78a
JB
790{
791 int i;
0c7dc45d 792 bool band_rule_found = false;
3e0c3ff3 793 const struct ieee80211_regdomain *regd;
b2e1b302 794 u32 max_bandwidth = 0;
8318d78a 795
1fa25e41 796 regd = custom_regd ? custom_regd : cfg80211_regdomain;
3e0c3ff3
LR
797
798 /* Follow the driver's regulatory domain, if present, unless a country
799 * IE has been processed */
800 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE &&
801 wiphy->regd)
802 regd = wiphy->regd;
803
804 if (!regd)
b2e1b302
LR
805 return -EINVAL;
806
3e0c3ff3 807 for (i = 0; i < regd->n_reg_rules; i++) {
b2e1b302
LR
808 const struct ieee80211_reg_rule *rr;
809 const struct ieee80211_freq_range *fr = NULL;
810 const struct ieee80211_power_rule *pr = NULL;
811
3e0c3ff3 812 rr = &regd->reg_rules[i];
b2e1b302
LR
813 fr = &rr->freq_range;
814 pr = &rr->power_rule;
0c7dc45d
LR
815
816 /* We only need to know if one frequency rule was
817 * was in center_freq's band, that's enough, so lets
818 * not overwrite it once found */
819 if (!band_rule_found)
820 band_rule_found = freq_in_rule_band(fr, center_freq);
821
b2e1b302 822 max_bandwidth = freq_max_bandwidth(fr, center_freq);
0c7dc45d 823
b2e1b302
LR
824 if (max_bandwidth && *bandwidth <= max_bandwidth) {
825 *reg_rule = rr;
826 *bandwidth = max_bandwidth;
8318d78a
JB
827 break;
828 }
829 }
830
0c7dc45d
LR
831 if (!band_rule_found)
832 return -ERANGE;
833
b2e1b302
LR
834 return !max_bandwidth;
835}
836
1fa25e41
LR
837/**
838 * freq_reg_info - get regulatory information for the given frequency
839 * @wiphy: the wiphy for which we want to process this rule for
840 * @center_freq: Frequency in KHz for which we want regulatory information for
841 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
842 * you can set this to 0. If this frequency is allowed we then set
843 * this value to the maximum allowed bandwidth.
844 * @reg_rule: the regulatory rule which we have for this frequency
845 *
846 * Use this function to get the regulatory rule for a specific frequency on
847 * a given wireless device. If the device has a specific regulatory domain
848 * it wants to follow we respect that unless a country IE has been received
849 * and processed already.
850 *
851 * Returns 0 if it was able to find a valid regulatory rule which does
852 * apply to the given center_freq otherwise it returns non-zero. It will
853 * also return -ERANGE if we determine the given center_freq does not even have
854 * a regulatory rule for a frequency range in the center_freq's band. See
855 * freq_in_rule_band() for our current definition of a band -- this is purely
856 * subjective and right now its 802.11 specific.
857 */
858static int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth,
859 const struct ieee80211_reg_rule **reg_rule)
860{
861 return freq_reg_info_regd(wiphy, center_freq,
862 bandwidth, reg_rule, NULL);
863}
864
a92a3ce7
LR
865static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
866 unsigned int chan_idx)
b2e1b302
LR
867{
868 int r;
a92a3ce7 869 u32 flags;
b2e1b302
LR
870 u32 max_bandwidth = 0;
871 const struct ieee80211_reg_rule *reg_rule = NULL;
872 const struct ieee80211_power_rule *power_rule = NULL;
a92a3ce7
LR
873 struct ieee80211_supported_band *sband;
874 struct ieee80211_channel *chan;
875
876 sband = wiphy->bands[band];
877 BUG_ON(chan_idx >= sband->n_channels);
878 chan = &sband->channels[chan_idx];
879
880 flags = chan->orig_flags;
b2e1b302 881
3e0c3ff3 882 r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq),
b2e1b302
LR
883 &max_bandwidth, &reg_rule);
884
885 if (r) {
0c7dc45d
LR
886 /* This means no regulatory rule was found in the country IE
887 * with a frequency range on the center_freq's band, since
888 * IEEE-802.11 allows for a country IE to have a subset of the
889 * regulatory information provided in a country we ignore
890 * disabling the channel unless at least one reg rule was
891 * found on the center_freq's band. For details see this
892 * clarification:
893 *
894 * http://tinyurl.com/11d-clarification
895 */
896 if (r == -ERANGE &&
897 last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
898#ifdef CONFIG_CFG80211_REG_DEBUG
899 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
900 "intact on %s - no rule found in band on "
901 "Country IE\n",
902 chan->center_freq, wiphy_name(wiphy));
903#endif
904 } else {
905 /* In this case we know the country IE has at least one reg rule
906 * for the band so we respect its band definitions */
907#ifdef CONFIG_CFG80211_REG_DEBUG
908 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
909 printk(KERN_DEBUG "cfg80211: Disabling "
910 "channel %d MHz on %s due to "
911 "Country IE\n",
912 chan->center_freq, wiphy_name(wiphy));
913#endif
914 flags |= IEEE80211_CHAN_DISABLED;
915 chan->flags = flags;
916 }
8318d78a
JB
917 return;
918 }
919
b2e1b302
LR
920 power_rule = &reg_rule->power_rule;
921
922 chan->flags = flags | map_regdom_flags(reg_rule->flags);
8318d78a 923 chan->max_antenna_gain = min(chan->orig_mag,
b2e1b302
LR
924 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
925 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
253898c4 926 if (chan->orig_mpwr)
b2e1b302
LR
927 chan->max_power = min(chan->orig_mpwr,
928 (int) MBM_TO_DBM(power_rule->max_eirp));
253898c4 929 else
b2e1b302 930 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
8318d78a
JB
931}
932
a92a3ce7 933static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
8318d78a 934{
a92a3ce7
LR
935 unsigned int i;
936 struct ieee80211_supported_band *sband;
937
938 BUG_ON(!wiphy->bands[band]);
939 sband = wiphy->bands[band];
8318d78a
JB
940
941 for (i = 0; i < sband->n_channels; i++)
a92a3ce7 942 handle_channel(wiphy, band, i);
8318d78a
JB
943}
944
14b9815a
LR
945static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby)
946{
947 if (!last_request)
948 return true;
949 if (setby == REGDOM_SET_BY_CORE &&
950 wiphy->fw_handles_regulatory)
951 return true;
952 return false;
953}
954
b2e1b302 955static void update_all_wiphy_regulatory(enum reg_set_by setby)
8318d78a 956{
b2e1b302 957 struct cfg80211_registered_device *drv;
8318d78a 958
b2e1b302 959 list_for_each_entry(drv, &cfg80211_drv_list, list)
14b9815a
LR
960 if (!ignore_reg_update(&drv->wiphy, setby))
961 wiphy_update_regulatory(&drv->wiphy, setby);
b2e1b302
LR
962}
963
964void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
965{
966 enum ieee80211_band band;
967 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
8318d78a 968 if (wiphy->bands[band])
a92a3ce7 969 handle_band(wiphy, band);
b2e1b302 970 }
560e28e1
LR
971 if (wiphy->reg_notifier)
972 wiphy->reg_notifier(wiphy, setby);
b2e1b302
LR
973}
974
1fa25e41
LR
975static void handle_channel_custom(struct wiphy *wiphy,
976 enum ieee80211_band band,
977 unsigned int chan_idx,
978 const struct ieee80211_regdomain *regd)
979{
980 int r;
981 u32 max_bandwidth = 0;
982 const struct ieee80211_reg_rule *reg_rule = NULL;
983 const struct ieee80211_power_rule *power_rule = NULL;
984 struct ieee80211_supported_band *sband;
985 struct ieee80211_channel *chan;
986
987 sband = wiphy->bands[band];
988 BUG_ON(chan_idx >= sband->n_channels);
989 chan = &sband->channels[chan_idx];
990
991 r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
992 &max_bandwidth, &reg_rule, regd);
993
994 if (r) {
995 chan->flags = IEEE80211_CHAN_DISABLED;
996 return;
997 }
998
999 power_rule = &reg_rule->power_rule;
1000
1001 chan->flags |= map_regdom_flags(reg_rule->flags);
1002 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
1003 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
1004 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1005}
1006
1007static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1008 const struct ieee80211_regdomain *regd)
1009{
1010 unsigned int i;
1011 struct ieee80211_supported_band *sband;
1012
1013 BUG_ON(!wiphy->bands[band]);
1014 sband = wiphy->bands[band];
1015
1016 for (i = 0; i < sband->n_channels; i++)
1017 handle_channel_custom(wiphy, band, i, regd);
1018}
1019
1020/* Used by drivers prior to wiphy registration */
1021void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1022 const struct ieee80211_regdomain *regd)
1023{
1024 enum ieee80211_band band;
1025 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1026 if (wiphy->bands[band])
1027 handle_band_custom(wiphy, band, regd);
1028 }
1029}
1030EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1031
3e0c3ff3
LR
1032static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1033 const struct ieee80211_regdomain *src_regd)
1034{
1035 struct ieee80211_regdomain *regd;
1036 int size_of_regd = 0;
1037 unsigned int i;
1038
1039 size_of_regd = sizeof(struct ieee80211_regdomain) +
1040 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1041
1042 regd = kzalloc(size_of_regd, GFP_KERNEL);
1043 if (!regd)
1044 return -ENOMEM;
1045
1046 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1047
1048 for (i = 0; i < src_regd->n_reg_rules; i++)
1049 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1050 sizeof(struct ieee80211_reg_rule));
1051
1052 *dst_regd = regd;
1053 return 0;
1054}
1055
9c96477d
LR
1056/* Return value which can be used by ignore_request() to indicate
1057 * it has been determined we should intersect two regulatory domains */
1058#define REG_INTERSECT 1
1059
84fa4f43
JB
1060/* This has the logic which determines when a new request
1061 * should be ignored. */
1062static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
1063 const char *alpha2)
1064{
1065 /* All initial requests are respected */
1066 if (!last_request)
1067 return 0;
1068
1069 switch (set_by) {
1070 case REGDOM_SET_BY_INIT:
1071 return -EINVAL;
1072 case REGDOM_SET_BY_CORE:
1073 /*
1074 * Always respect new wireless core hints, should only happen
1075 * when updating the world regulatory domain at init.
1076 */
1077 return 0;
1078 case REGDOM_SET_BY_COUNTRY_IE:
1079 if (unlikely(!is_an_alpha2(alpha2)))
1080 return -EINVAL;
1081 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1082 if (last_request->wiphy != wiphy) {
1083 /*
1084 * Two cards with two APs claiming different
1085 * different Country IE alpha2s. We could
1086 * intersect them, but that seems unlikely
1087 * to be correct. Reject second one for now.
1088 */
1089 if (!alpha2_equal(alpha2,
1090 cfg80211_regdomain->alpha2))
1091 return -EOPNOTSUPP;
1092 return -EALREADY;
1093 }
3f2355cb
LR
1094 /* Two consecutive Country IE hints on the same wiphy.
1095 * This should be picked up early by the driver/stack */
1096 if (WARN_ON(!alpha2_equal(cfg80211_regdomain->alpha2,
1097 alpha2)))
84fa4f43
JB
1098 return 0;
1099 return -EALREADY;
1100 }
3f2355cb 1101 return REG_INTERSECT;
84fa4f43 1102 case REGDOM_SET_BY_DRIVER:
3e0c3ff3
LR
1103 if (last_request->initiator == REGDOM_SET_BY_CORE)
1104 return 0;
1105 return REG_INTERSECT;
84fa4f43 1106 case REGDOM_SET_BY_USER:
84fa4f43 1107 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
9c96477d 1108 return REG_INTERSECT;
3f2355cb
LR
1109 /* If the user knows better the user should set the regdom
1110 * to their country before the IE is picked up */
1111 if (last_request->initiator == REGDOM_SET_BY_USER &&
1112 last_request->intersect)
1113 return -EOPNOTSUPP;
84fa4f43
JB
1114 return 0;
1115 }
1116
1117 return -EINVAL;
1118}
1119
b2e1b302
LR
1120/* Caller must hold &cfg80211_drv_mutex */
1121int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
3f2355cb
LR
1122 const char *alpha2,
1123 u32 country_ie_checksum,
1124 enum environment_cap env)
b2e1b302
LR
1125{
1126 struct regulatory_request *request;
9c96477d 1127 bool intersect = false;
b2e1b302
LR
1128 int r = 0;
1129
be3d4810 1130 r = ignore_request(wiphy, set_by, alpha2);
9c96477d 1131
3e0c3ff3
LR
1132 if (r == REG_INTERSECT) {
1133 if (set_by == REGDOM_SET_BY_DRIVER) {
1134 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1135 if (r)
1136 return r;
1137 }
9c96477d 1138 intersect = true;
3e0c3ff3
LR
1139 } else if (r) {
1140 /* If the regulatory domain being requested by the
1141 * driver has already been set just copy it to the
1142 * wiphy */
1143 if (r == -EALREADY && set_by == REGDOM_SET_BY_DRIVER) {
1144 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
1145 if (r)
1146 return r;
1147 r = -EALREADY;
1148 goto new_request;
1149 }
b2e1b302 1150 return r;
3e0c3ff3 1151 }
b2e1b302 1152
3e0c3ff3 1153new_request:
5203cdb6
LR
1154 request = kzalloc(sizeof(struct regulatory_request),
1155 GFP_KERNEL);
1156 if (!request)
1157 return -ENOMEM;
1158
1159 request->alpha2[0] = alpha2[0];
1160 request->alpha2[1] = alpha2[1];
1161 request->initiator = set_by;
1162 request->wiphy = wiphy;
1163 request->intersect = intersect;
3f2355cb
LR
1164 request->country_ie_checksum = country_ie_checksum;
1165 request->country_ie_env = env;
5203cdb6
LR
1166
1167 kfree(last_request);
1168 last_request = request;
3e0c3ff3
LR
1169
1170 /* When r == REG_INTERSECT we do need to call CRDA */
1171 if (r < 0)
1172 return r;
1173
3f2355cb
LR
1174 /*
1175 * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled
1176 * AND if CRDA is NOT present nothing will happen, if someone
1177 * wants to bother with 11d with OLD_REG you can add a timer.
1178 * If after x amount of time nothing happens you can call:
1179 *
1180 * return set_regdom(country_ie_regdomain);
1181 *
1182 * to intersect with the static rd
1183 */
02ba0b32 1184 return call_crda(alpha2);
b2e1b302
LR
1185}
1186
be3d4810 1187void regulatory_hint(struct wiphy *wiphy, const char *alpha2)
b2e1b302 1188{
be3d4810 1189 BUG_ON(!alpha2);
b2e1b302
LR
1190
1191 mutex_lock(&cfg80211_drv_mutex);
3f2355cb 1192 __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, 0, ENVIRON_ANY);
b2e1b302 1193 mutex_unlock(&cfg80211_drv_mutex);
b2e1b302
LR
1194}
1195EXPORT_SYMBOL(regulatory_hint);
1196
3f2355cb
LR
1197static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1198 u32 country_ie_checksum)
1199{
1200 if (!last_request->wiphy)
1201 return false;
1202 if (likely(last_request->wiphy != wiphy))
1203 return !country_ie_integrity_changes(country_ie_checksum);
1204 /* We should not have let these through at this point, they
1205 * should have been picked up earlier by the first alpha2 check
1206 * on the device */
1207 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1208 return true;
1209 return false;
1210}
1211
1212void regulatory_hint_11d(struct wiphy *wiphy,
1213 u8 *country_ie,
1214 u8 country_ie_len)
1215{
1216 struct ieee80211_regdomain *rd = NULL;
1217 char alpha2[2];
1218 u32 checksum = 0;
1219 enum environment_cap env = ENVIRON_ANY;
1220
0f70f398
JB
1221 if (!last_request)
1222 return;
1223
3f2355cb
LR
1224 mutex_lock(&cfg80211_drv_mutex);
1225
1226 /* IE len must be evenly divisible by 2 */
1227 if (country_ie_len & 0x01)
1228 goto out;
1229
1230 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1231 goto out;
1232
1233 /* Pending country IE processing, this can happen after we
1234 * call CRDA and wait for a response if a beacon was received before
1235 * we were able to process the last regulatory_hint_11d() call */
1236 if (country_ie_regdomain)
1237 goto out;
1238
1239 alpha2[0] = country_ie[0];
1240 alpha2[1] = country_ie[1];
1241
1242 if (country_ie[2] == 'I')
1243 env = ENVIRON_INDOOR;
1244 else if (country_ie[2] == 'O')
1245 env = ENVIRON_OUTDOOR;
1246
1247 /* We will run this for *every* beacon processed for the BSSID, so
1248 * we optimize an early check to exit out early if we don't have to
1249 * do anything */
1250 if (likely(last_request->wiphy)) {
1251 struct cfg80211_registered_device *drv_last_ie;
1252
1253 drv_last_ie = wiphy_to_dev(last_request->wiphy);
1254
1255 /* Lets keep this simple -- we trust the first AP
1256 * after we intersect with CRDA */
1257 if (likely(last_request->wiphy == wiphy)) {
1258 /* Ignore IEs coming in on this wiphy with
1259 * the same alpha2 and environment cap */
1260 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1261 alpha2) &&
1262 env == drv_last_ie->env)) {
1263 goto out;
1264 }
1265 /* the wiphy moved on to another BSSID or the AP
1266 * was reconfigured. XXX: We need to deal with the
1267 * case where the user suspends and goes to goes
1268 * to another country, and then gets IEs from an
1269 * AP with different settings */
1270 goto out;
1271 } else {
1272 /* Ignore IEs coming in on two separate wiphys with
1273 * the same alpha2 and environment cap */
1274 if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
1275 alpha2) &&
1276 env == drv_last_ie->env)) {
1277 goto out;
1278 }
1279 /* We could potentially intersect though */
1280 goto out;
1281 }
1282 }
1283
1284 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1285 if (!rd)
1286 goto out;
1287
1288 /* This will not happen right now but we leave it here for the
1289 * the future when we want to add suspend/resume support and having
1290 * the user move to another country after doing so, or having the user
1291 * move to another AP. Right now we just trust the first AP. This is why
1292 * this is marked as likley(). If we hit this before we add this support
1293 * we want to be informed of it as it would indicate a mistake in the
1294 * current design */
1295 if (likely(WARN_ON(reg_same_country_ie_hint(wiphy, checksum))))
1296 goto out;
1297
1298 /* We keep this around for when CRDA comes back with a response so
1299 * we can intersect with that */
1300 country_ie_regdomain = rd;
1301
1302 __regulatory_hint(wiphy, REGDOM_SET_BY_COUNTRY_IE,
1303 country_ie_regdomain->alpha2, checksum, env);
1304
1305out:
1306 mutex_unlock(&cfg80211_drv_mutex);
1307}
1308EXPORT_SYMBOL(regulatory_hint_11d);
b2e1b302 1309
a3d2eaf0 1310static void print_rd_rules(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1311{
1312 unsigned int i;
a3d2eaf0
JB
1313 const struct ieee80211_reg_rule *reg_rule = NULL;
1314 const struct ieee80211_freq_range *freq_range = NULL;
1315 const struct ieee80211_power_rule *power_rule = NULL;
b2e1b302
LR
1316
1317 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1318 "(max_antenna_gain, max_eirp)\n");
1319
1320 for (i = 0; i < rd->n_reg_rules; i++) {
1321 reg_rule = &rd->reg_rules[i];
1322 freq_range = &reg_rule->freq_range;
1323 power_rule = &reg_rule->power_rule;
1324
1325 /* There may not be documentation for max antenna gain
1326 * in certain regions */
1327 if (power_rule->max_antenna_gain)
1328 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1329 "(%d mBi, %d mBm)\n",
1330 freq_range->start_freq_khz,
1331 freq_range->end_freq_khz,
1332 freq_range->max_bandwidth_khz,
1333 power_rule->max_antenna_gain,
1334 power_rule->max_eirp);
1335 else
1336 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1337 "(N/A, %d mBm)\n",
1338 freq_range->start_freq_khz,
1339 freq_range->end_freq_khz,
1340 freq_range->max_bandwidth_khz,
1341 power_rule->max_eirp);
1342 }
1343}
1344
a3d2eaf0 1345static void print_regdomain(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1346{
1347
3f2355cb
LR
1348 if (is_intersected_alpha2(rd->alpha2)) {
1349 struct wiphy *wiphy = NULL;
1350 struct cfg80211_registered_device *drv;
1351
1352 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
1353 if (last_request->wiphy) {
1354 wiphy = last_request->wiphy;
1355 drv = wiphy_to_dev(wiphy);
1356 printk(KERN_INFO "cfg80211: Current regulatory "
1357 "domain updated by AP to: %c%c\n",
1358 drv->country_ie_alpha2[0],
1359 drv->country_ie_alpha2[1]);
1360 } else
1361 printk(KERN_INFO "cfg80211: Current regulatory "
1362 "domain intersected: \n");
1363 } else
1364 printk(KERN_INFO "cfg80211: Current regulatory "
039498c6 1365 "domain intersected: \n");
3f2355cb 1366 } else if (is_world_regdom(rd->alpha2))
b2e1b302
LR
1367 printk(KERN_INFO "cfg80211: World regulatory "
1368 "domain updated:\n");
1369 else {
1370 if (is_unknown_alpha2(rd->alpha2))
1371 printk(KERN_INFO "cfg80211: Regulatory domain "
1372 "changed to driver built-in settings "
1373 "(unknown country)\n");
1374 else
1375 printk(KERN_INFO "cfg80211: Regulatory domain "
1376 "changed to country: %c%c\n",
1377 rd->alpha2[0], rd->alpha2[1]);
1378 }
1379 print_rd_rules(rd);
1380}
1381
2df78167 1382static void print_regdomain_info(const struct ieee80211_regdomain *rd)
b2e1b302
LR
1383{
1384 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1385 rd->alpha2[0], rd->alpha2[1]);
1386 print_rd_rules(rd);
1387}
1388
3f2355cb
LR
1389#ifdef CONFIG_CFG80211_REG_DEBUG
1390static void reg_country_ie_process_debug(
1391 const struct ieee80211_regdomain *rd,
1392 const struct ieee80211_regdomain *country_ie_regdomain,
1393 const struct ieee80211_regdomain *intersected_rd)
1394{
1395 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1396 print_regdomain_info(country_ie_regdomain);
1397 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1398 print_regdomain_info(rd);
1399 if (intersected_rd) {
1400 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1401 "and get:\n");
1402 print_regdomain_info(rd);
1403 return;
1404 }
1405 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1406}
1407#else
1408static inline void reg_country_ie_process_debug(
1409 const struct ieee80211_regdomain *rd,
1410 const struct ieee80211_regdomain *country_ie_regdomain,
1411 const struct ieee80211_regdomain *intersected_rd)
1412{
1413}
1414#endif
1415
d2372b31 1416/* Takes ownership of rd only if it doesn't fail */
a3d2eaf0 1417static int __set_regdom(const struct ieee80211_regdomain *rd)
b2e1b302 1418{
9c96477d 1419 const struct ieee80211_regdomain *intersected_rd = NULL;
3f2355cb
LR
1420 struct cfg80211_registered_device *drv = NULL;
1421 struct wiphy *wiphy = NULL;
b2e1b302
LR
1422 /* Some basic sanity checks first */
1423
b2e1b302 1424 if (is_world_regdom(rd->alpha2)) {
f6037d09 1425 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
b2e1b302
LR
1426 return -EINVAL;
1427 update_world_regdomain(rd);
1428 return 0;
1429 }
b2e1b302
LR
1430
1431 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
1432 !is_unknown_alpha2(rd->alpha2))
1433 return -EINVAL;
1434
f6037d09 1435 if (!last_request)
b2e1b302
LR
1436 return -EINVAL;
1437
3f2355cb
LR
1438 /* Lets only bother proceeding on the same alpha2 if the current
1439 * rd is non static (it means CRDA was present and was used last)
1440 * and the pending request came in from a country IE */
1441 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1442 /* If someone else asked us to change the rd lets only bother
1443 * checking if the alpha2 changes if CRDA was already called */
1444 if (!is_old_static_regdom(cfg80211_regdomain) &&
1445 !regdom_changed(rd->alpha2))
1446 return -EINVAL;
1447 }
1448
1449 wiphy = last_request->wiphy;
b2e1b302
LR
1450
1451 /* Now lets set the regulatory domain, update all driver channels
1452 * and finally inform them of what we have done, in case they want
1453 * to review or adjust their own settings based on their own
1454 * internal EEPROM data */
1455
f6037d09 1456 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
b2e1b302
LR
1457 return -EINVAL;
1458
8375af3b
LR
1459 if (!is_valid_rd(rd)) {
1460 printk(KERN_ERR "cfg80211: Invalid "
1461 "regulatory domain detected:\n");
1462 print_regdomain_info(rd);
1463 return -EINVAL;
b2e1b302
LR
1464 }
1465
b8295acd 1466 if (!last_request->intersect) {
3e0c3ff3
LR
1467 int r;
1468
1469 if (last_request->initiator != REGDOM_SET_BY_DRIVER) {
1470 reset_regdomains();
1471 cfg80211_regdomain = rd;
1472 return 0;
1473 }
1474
1475 /* For a driver hint, lets copy the regulatory domain the
1476 * driver wanted to the wiphy to deal with conflicts */
1477
1478 BUG_ON(last_request->wiphy->regd);
1479
1480 r = reg_copy_regd(&last_request->wiphy->regd, rd);
1481 if (r)
1482 return r;
1483
b8295acd
LR
1484 reset_regdomains();
1485 cfg80211_regdomain = rd;
1486 return 0;
1487 }
1488
1489 /* Intersection requires a bit more work */
1490
1491 if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) {
1492
9c96477d
LR
1493 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1494 if (!intersected_rd)
1495 return -EINVAL;
b8295acd 1496
3e0c3ff3
LR
1497 /* We can trash what CRDA provided now.
1498 * However if a driver requested this specific regulatory
1499 * domain we keep it for its private use */
1500 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
1501 last_request->wiphy->regd = rd;
1502 else
1503 kfree(rd);
1504
b8295acd
LR
1505 rd = NULL;
1506
1507 reset_regdomains();
1508 cfg80211_regdomain = intersected_rd;
1509
1510 return 0;
9c96477d
LR
1511 }
1512
3f2355cb
LR
1513 /*
1514 * Country IE requests are handled a bit differently, we intersect
1515 * the country IE rd with what CRDA believes that country should have
1516 */
1517
1518 BUG_ON(!country_ie_regdomain);
1519
1520 if (rd != country_ie_regdomain) {
1521 /* Intersect what CRDA returned and our what we
1522 * had built from the Country IE received */
1523
1524 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
1525
1526 reg_country_ie_process_debug(rd, country_ie_regdomain,
1527 intersected_rd);
1528
1529 kfree(country_ie_regdomain);
1530 country_ie_regdomain = NULL;
1531 } else {
1532 /* This would happen when CRDA was not present and
1533 * OLD_REGULATORY was enabled. We intersect our Country
1534 * IE rd and what was set on cfg80211 originally */
1535 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
1536 }
1537
1538 if (!intersected_rd)
1539 return -EINVAL;
1540
1541 drv = wiphy_to_dev(wiphy);
1542
1543 drv->country_ie_alpha2[0] = rd->alpha2[0];
1544 drv->country_ie_alpha2[1] = rd->alpha2[1];
1545 drv->env = last_request->country_ie_env;
1546
1547 BUG_ON(intersected_rd == rd);
1548
1549 kfree(rd);
1550 rd = NULL;
1551
b8295acd 1552 reset_regdomains();
3f2355cb 1553 cfg80211_regdomain = intersected_rd;
b2e1b302
LR
1554
1555 return 0;
1556}
1557
1558
1559/* Use this call to set the current regulatory domain. Conflicts with
1560 * multiple drivers can be ironed out later. Caller must've already
d2372b31 1561 * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */
a3d2eaf0 1562int set_regdom(const struct ieee80211_regdomain *rd)
b2e1b302 1563{
b2e1b302
LR
1564 int r;
1565
b2e1b302
LR
1566 /* Note that this doesn't update the wiphys, this is done below */
1567 r = __set_regdom(rd);
d2372b31
JB
1568 if (r) {
1569 kfree(rd);
b2e1b302 1570 return r;
d2372b31 1571 }
b2e1b302 1572
b2e1b302 1573 /* This would make this whole thing pointless */
a01ddafd
LR
1574 if (!last_request->intersect)
1575 BUG_ON(rd != cfg80211_regdomain);
b2e1b302
LR
1576
1577 /* update all wiphys now with the new established regulatory domain */
f6037d09 1578 update_all_wiphy_regulatory(last_request->initiator);
b2e1b302 1579
a01ddafd 1580 print_regdomain(cfg80211_regdomain);
b2e1b302
LR
1581
1582 return r;
1583}
1584
3f2355cb
LR
1585/* Caller must hold cfg80211_drv_mutex */
1586void reg_device_remove(struct wiphy *wiphy)
1587{
3e0c3ff3 1588 kfree(wiphy->regd);
4a4f4d80 1589 if (!last_request || !last_request->wiphy)
3f2355cb
LR
1590 return;
1591 if (last_request->wiphy != wiphy)
1592 return;
1593 last_request->wiphy = NULL;
1594 last_request->country_ie_env = ENVIRON_ANY;
1595}
1596
b2e1b302
LR
1597int regulatory_init(void)
1598{
734366de
JB
1599 int err;
1600
b2e1b302
LR
1601 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
1602 if (IS_ERR(reg_pdev))
1603 return PTR_ERR(reg_pdev);
734366de
JB
1604
1605#ifdef CONFIG_WIRELESS_OLD_REGULATORY
a3d2eaf0 1606 cfg80211_regdomain = static_regdom(ieee80211_regdom);
734366de 1607
942b25cf 1608 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
734366de
JB
1609 print_regdomain_info(cfg80211_regdomain);
1610 /* The old code still requests for a new regdomain and if
1611 * you have CRDA you get it updated, otherwise you get
1612 * stuck with the static values. We ignore "EU" code as
1613 * that is not a valid ISO / IEC 3166 alpha2 */
ac9440a4 1614 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
734366de 1615 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
3f2355cb 1616 ieee80211_regdom, 0, ENVIRON_ANY);
734366de 1617#else
a3d2eaf0 1618 cfg80211_regdomain = cfg80211_world_regdom;
734366de 1619
3f2355cb 1620 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", 0, ENVIRON_ANY);
734366de
JB
1621 if (err)
1622 printk(KERN_ERR "cfg80211: calling CRDA failed - "
1623 "unable to update world regulatory domain, "
1624 "using static definition\n");
1625#endif
1626
b2e1b302
LR
1627 return 0;
1628}
1629
1630void regulatory_exit(void)
1631{
b2e1b302 1632 mutex_lock(&cfg80211_drv_mutex);
734366de 1633
b2e1b302 1634 reset_regdomains();
734366de 1635
3f2355cb
LR
1636 kfree(country_ie_regdomain);
1637 country_ie_regdomain = NULL;
1638
f6037d09
JB
1639 kfree(last_request);
1640
b2e1b302 1641 platform_device_unregister(reg_pdev);
734366de 1642
b2e1b302 1643 mutex_unlock(&cfg80211_drv_mutex);
8318d78a 1644}
This page took 0.191195 seconds and 5 git commands to generate.