]>
Commit | Line | Data |
---|---|---|
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 | |
be3d4810 JB |
45 | /* |
46 | * wiphy is set if this request's initiator is | |
47 | * REGDOM_SET_BY_COUNTRY_IE or _DRIVER | |
48 | */ | |
734366de | 49 | struct regulatory_request { |
734366de | 50 | struct wiphy *wiphy; |
734366de JB |
51 | enum reg_set_by initiator; |
52 | char alpha2[2]; | |
9c96477d | 53 | bool intersect; |
734366de JB |
54 | }; |
55 | ||
f6037d09 | 56 | static struct regulatory_request *last_request; |
734366de | 57 | |
b2e1b302 LR |
58 | /* To trigger userspace events */ |
59 | static struct platform_device *reg_pdev; | |
8318d78a | 60 | |
b2e1b302 LR |
61 | /* Keep the ordering from large to small */ |
62 | static u32 supported_bandwidths[] = { | |
63 | MHZ_TO_KHZ(40), | |
64 | MHZ_TO_KHZ(20), | |
8318d78a JB |
65 | }; |
66 | ||
734366de JB |
67 | /* Central wireless core regulatory domains, we only need two, |
68 | * the current one and a world regulatory domain in case we have no | |
69 | * information to give us an alpha2 */ | |
a3d2eaf0 | 70 | static const struct ieee80211_regdomain *cfg80211_regdomain; |
734366de JB |
71 | |
72 | /* We keep a static world regulatory domain in case of the absence of CRDA */ | |
73 | static const struct ieee80211_regdomain world_regdom = { | |
74 | .n_reg_rules = 1, | |
75 | .alpha2 = "00", | |
76 | .reg_rules = { | |
77 | REG_RULE(2412-10, 2462+10, 40, 6, 20, | |
78 | NL80211_RRF_PASSIVE_SCAN | | |
79 | NL80211_RRF_NO_IBSS), | |
80 | } | |
81 | }; | |
82 | ||
a3d2eaf0 JB |
83 | static const struct ieee80211_regdomain *cfg80211_world_regdom = |
84 | &world_regdom; | |
734366de JB |
85 | |
86 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY | |
87 | static char *ieee80211_regdom = "US"; | |
88 | module_param(ieee80211_regdom, charp, 0444); | |
89 | MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); | |
90 | ||
91 | /* We assume 40 MHz bandwidth for the old regulatory work. | |
92 | * We make emphasis we are using the exact same frequencies | |
93 | * as before */ | |
94 | ||
95 | static const struct ieee80211_regdomain us_regdom = { | |
96 | .n_reg_rules = 6, | |
97 | .alpha2 = "US", | |
98 | .reg_rules = { | |
99 | /* IEEE 802.11b/g, channels 1..11 */ | |
100 | REG_RULE(2412-10, 2462+10, 40, 6, 27, 0), | |
101 | /* IEEE 802.11a, channel 36 */ | |
102 | REG_RULE(5180-10, 5180+10, 40, 6, 23, 0), | |
103 | /* IEEE 802.11a, channel 40 */ | |
104 | REG_RULE(5200-10, 5200+10, 40, 6, 23, 0), | |
105 | /* IEEE 802.11a, channel 44 */ | |
106 | REG_RULE(5220-10, 5220+10, 40, 6, 23, 0), | |
107 | /* IEEE 802.11a, channels 48..64 */ | |
108 | REG_RULE(5240-10, 5320+10, 40, 6, 23, 0), | |
109 | /* IEEE 802.11a, channels 149..165, outdoor */ | |
110 | REG_RULE(5745-10, 5825+10, 40, 6, 30, 0), | |
111 | } | |
112 | }; | |
113 | ||
114 | static const struct ieee80211_regdomain jp_regdom = { | |
115 | .n_reg_rules = 3, | |
116 | .alpha2 = "JP", | |
117 | .reg_rules = { | |
118 | /* IEEE 802.11b/g, channels 1..14 */ | |
119 | REG_RULE(2412-10, 2484+10, 40, 6, 20, 0), | |
120 | /* IEEE 802.11a, channels 34..48 */ | |
121 | REG_RULE(5170-10, 5240+10, 40, 6, 20, | |
122 | NL80211_RRF_PASSIVE_SCAN), | |
123 | /* IEEE 802.11a, channels 52..64 */ | |
124 | REG_RULE(5260-10, 5320+10, 40, 6, 20, | |
125 | NL80211_RRF_NO_IBSS | | |
126 | NL80211_RRF_DFS), | |
127 | } | |
128 | }; | |
129 | ||
130 | static const struct ieee80211_regdomain eu_regdom = { | |
131 | .n_reg_rules = 6, | |
132 | /* This alpha2 is bogus, we leave it here just for stupid | |
133 | * backward compatibility */ | |
134 | .alpha2 = "EU", | |
135 | .reg_rules = { | |
136 | /* IEEE 802.11b/g, channels 1..13 */ | |
137 | REG_RULE(2412-10, 2472+10, 40, 6, 20, 0), | |
138 | /* IEEE 802.11a, channel 36 */ | |
139 | REG_RULE(5180-10, 5180+10, 40, 6, 23, | |
140 | NL80211_RRF_PASSIVE_SCAN), | |
141 | /* IEEE 802.11a, channel 40 */ | |
142 | REG_RULE(5200-10, 5200+10, 40, 6, 23, | |
143 | NL80211_RRF_PASSIVE_SCAN), | |
144 | /* IEEE 802.11a, channel 44 */ | |
145 | REG_RULE(5220-10, 5220+10, 40, 6, 23, | |
146 | NL80211_RRF_PASSIVE_SCAN), | |
147 | /* IEEE 802.11a, channels 48..64 */ | |
148 | REG_RULE(5240-10, 5320+10, 40, 6, 20, | |
149 | NL80211_RRF_NO_IBSS | | |
150 | NL80211_RRF_DFS), | |
151 | /* IEEE 802.11a, channels 100..140 */ | |
152 | REG_RULE(5500-10, 5700+10, 40, 6, 30, | |
153 | NL80211_RRF_NO_IBSS | | |
154 | NL80211_RRF_DFS), | |
155 | } | |
156 | }; | |
157 | ||
158 | static const struct ieee80211_regdomain *static_regdom(char *alpha2) | |
159 | { | |
160 | if (alpha2[0] == 'U' && alpha2[1] == 'S') | |
161 | return &us_regdom; | |
162 | if (alpha2[0] == 'J' && alpha2[1] == 'P') | |
163 | return &jp_regdom; | |
164 | if (alpha2[0] == 'E' && alpha2[1] == 'U') | |
165 | return &eu_regdom; | |
166 | /* Default, as per the old rules */ | |
167 | return &us_regdom; | |
168 | } | |
169 | ||
a3d2eaf0 | 170 | static bool is_old_static_regdom(const struct ieee80211_regdomain *rd) |
734366de JB |
171 | { |
172 | if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom) | |
173 | return true; | |
174 | return false; | |
175 | } | |
942b25cf JB |
176 | #else |
177 | static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd) | |
734366de | 178 | { |
942b25cf | 179 | return false; |
734366de | 180 | } |
942b25cf JB |
181 | #endif |
182 | ||
734366de JB |
183 | static void reset_regdomains(void) |
184 | { | |
942b25cf JB |
185 | /* avoid freeing static information or freeing something twice */ |
186 | if (cfg80211_regdomain == cfg80211_world_regdom) | |
187 | cfg80211_regdomain = NULL; | |
188 | if (cfg80211_world_regdom == &world_regdom) | |
189 | cfg80211_world_regdom = NULL; | |
190 | if (cfg80211_regdomain == &world_regdom) | |
191 | cfg80211_regdomain = NULL; | |
192 | if (is_old_static_regdom(cfg80211_regdomain)) | |
193 | cfg80211_regdomain = NULL; | |
194 | ||
195 | kfree(cfg80211_regdomain); | |
196 | kfree(cfg80211_world_regdom); | |
734366de | 197 | |
a3d2eaf0 | 198 | cfg80211_world_regdom = &world_regdom; |
734366de JB |
199 | cfg80211_regdomain = NULL; |
200 | } | |
201 | ||
202 | /* Dynamic world regulatory domain requested by the wireless | |
203 | * core upon initialization */ | |
a3d2eaf0 | 204 | static void update_world_regdomain(const struct ieee80211_regdomain *rd) |
734366de | 205 | { |
f6037d09 | 206 | BUG_ON(!last_request); |
734366de JB |
207 | |
208 | reset_regdomains(); | |
209 | ||
210 | cfg80211_world_regdom = rd; | |
211 | cfg80211_regdomain = rd; | |
212 | } | |
734366de | 213 | |
a3d2eaf0 | 214 | bool is_world_regdom(const char *alpha2) |
b2e1b302 LR |
215 | { |
216 | if (!alpha2) | |
217 | return false; | |
218 | if (alpha2[0] == '0' && alpha2[1] == '0') | |
219 | return true; | |
220 | return false; | |
221 | } | |
8318d78a | 222 | |
a3d2eaf0 | 223 | static bool is_alpha2_set(const char *alpha2) |
b2e1b302 LR |
224 | { |
225 | if (!alpha2) | |
226 | return false; | |
227 | if (alpha2[0] != 0 && alpha2[1] != 0) | |
228 | return true; | |
229 | return false; | |
230 | } | |
8318d78a | 231 | |
b2e1b302 LR |
232 | static bool is_alpha_upper(char letter) |
233 | { | |
234 | /* ASCII A - Z */ | |
235 | if (letter >= 65 && letter <= 90) | |
236 | return true; | |
237 | return false; | |
238 | } | |
8318d78a | 239 | |
a3d2eaf0 | 240 | static bool is_unknown_alpha2(const char *alpha2) |
b2e1b302 LR |
241 | { |
242 | if (!alpha2) | |
243 | return false; | |
244 | /* Special case where regulatory domain was built by driver | |
245 | * but a specific alpha2 cannot be determined */ | |
246 | if (alpha2[0] == '9' && alpha2[1] == '9') | |
247 | return true; | |
248 | return false; | |
249 | } | |
8318d78a | 250 | |
a3d2eaf0 | 251 | static bool is_an_alpha2(const char *alpha2) |
b2e1b302 LR |
252 | { |
253 | if (!alpha2) | |
254 | return false; | |
255 | if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1])) | |
256 | return true; | |
257 | return false; | |
258 | } | |
8318d78a | 259 | |
a3d2eaf0 | 260 | static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y) |
b2e1b302 LR |
261 | { |
262 | if (!alpha2_x || !alpha2_y) | |
263 | return false; | |
264 | if (alpha2_x[0] == alpha2_y[0] && | |
265 | alpha2_x[1] == alpha2_y[1]) | |
266 | return true; | |
267 | return false; | |
268 | } | |
269 | ||
a3d2eaf0 | 270 | static bool regdom_changed(const char *alpha2) |
b2e1b302 LR |
271 | { |
272 | if (!cfg80211_regdomain) | |
273 | return true; | |
274 | if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2)) | |
275 | return false; | |
276 | return true; | |
277 | } | |
278 | ||
279 | /* This lets us keep regulatory code which is updated on a regulatory | |
280 | * basis in userspace. */ | |
281 | static int call_crda(const char *alpha2) | |
282 | { | |
283 | char country_env[9 + 2] = "COUNTRY="; | |
284 | char *envp[] = { | |
285 | country_env, | |
286 | NULL | |
287 | }; | |
288 | ||
289 | if (!is_world_regdom((char *) alpha2)) | |
290 | printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n", | |
291 | alpha2[0], alpha2[1]); | |
292 | else | |
b2e1b302 LR |
293 | printk(KERN_INFO "cfg80211: Calling CRDA to update world " |
294 | "regulatory domain\n"); | |
b2e1b302 LR |
295 | |
296 | country_env[8] = alpha2[0]; | |
297 | country_env[9] = alpha2[1]; | |
298 | ||
299 | return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp); | |
300 | } | |
301 | ||
b2e1b302 | 302 | /* Used by nl80211 before kmalloc'ing our regulatory domain */ |
a3d2eaf0 | 303 | bool reg_is_valid_request(const char *alpha2) |
b2e1b302 | 304 | { |
f6037d09 JB |
305 | if (!last_request) |
306 | return false; | |
307 | ||
308 | return alpha2_equal(last_request->alpha2, alpha2); | |
b2e1b302 | 309 | } |
8318d78a | 310 | |
b2e1b302 | 311 | /* Sanity check on a regulatory rule */ |
a3d2eaf0 | 312 | static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule) |
8318d78a | 313 | { |
a3d2eaf0 | 314 | const struct ieee80211_freq_range *freq_range = &rule->freq_range; |
b2e1b302 LR |
315 | u32 freq_diff; |
316 | ||
317 | if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0) | |
318 | return false; | |
319 | ||
320 | if (freq_range->start_freq_khz > freq_range->end_freq_khz) | |
321 | return false; | |
322 | ||
323 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; | |
324 | ||
d71aaf60 | 325 | if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff) |
b2e1b302 LR |
326 | return false; |
327 | ||
328 | return true; | |
329 | } | |
330 | ||
a3d2eaf0 | 331 | static bool is_valid_rd(const struct ieee80211_regdomain *rd) |
b2e1b302 | 332 | { |
a3d2eaf0 | 333 | const struct ieee80211_reg_rule *reg_rule = NULL; |
b2e1b302 | 334 | unsigned int i; |
8318d78a | 335 | |
b2e1b302 LR |
336 | if (!rd->n_reg_rules) |
337 | return false; | |
8318d78a | 338 | |
b2e1b302 LR |
339 | for (i = 0; i < rd->n_reg_rules; i++) { |
340 | reg_rule = &rd->reg_rules[i]; | |
341 | if (!is_valid_reg_rule(reg_rule)) | |
342 | return false; | |
343 | } | |
344 | ||
345 | return true; | |
8318d78a JB |
346 | } |
347 | ||
b2e1b302 LR |
348 | /* Returns value in KHz */ |
349 | static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range, | |
350 | u32 freq) | |
351 | { | |
352 | unsigned int i; | |
353 | for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) { | |
354 | u32 start_freq_khz = freq - supported_bandwidths[i]/2; | |
355 | u32 end_freq_khz = freq + supported_bandwidths[i]/2; | |
356 | if (start_freq_khz >= freq_range->start_freq_khz && | |
357 | end_freq_khz <= freq_range->end_freq_khz) | |
358 | return supported_bandwidths[i]; | |
359 | } | |
360 | return 0; | |
361 | } | |
8318d78a | 362 | |
9c96477d LR |
363 | /* Helper for regdom_intersect(), this does the real |
364 | * mathematical intersection fun */ | |
365 | static int reg_rules_intersect( | |
366 | const struct ieee80211_reg_rule *rule1, | |
367 | const struct ieee80211_reg_rule *rule2, | |
368 | struct ieee80211_reg_rule *intersected_rule) | |
369 | { | |
370 | const struct ieee80211_freq_range *freq_range1, *freq_range2; | |
371 | struct ieee80211_freq_range *freq_range; | |
372 | const struct ieee80211_power_rule *power_rule1, *power_rule2; | |
373 | struct ieee80211_power_rule *power_rule; | |
374 | u32 freq_diff; | |
375 | ||
376 | freq_range1 = &rule1->freq_range; | |
377 | freq_range2 = &rule2->freq_range; | |
378 | freq_range = &intersected_rule->freq_range; | |
379 | ||
380 | power_rule1 = &rule1->power_rule; | |
381 | power_rule2 = &rule2->power_rule; | |
382 | power_rule = &intersected_rule->power_rule; | |
383 | ||
384 | freq_range->start_freq_khz = max(freq_range1->start_freq_khz, | |
385 | freq_range2->start_freq_khz); | |
386 | freq_range->end_freq_khz = min(freq_range1->end_freq_khz, | |
387 | freq_range2->end_freq_khz); | |
388 | freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz, | |
389 | freq_range2->max_bandwidth_khz); | |
390 | ||
391 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; | |
392 | if (freq_range->max_bandwidth_khz > freq_diff) | |
393 | freq_range->max_bandwidth_khz = freq_diff; | |
394 | ||
395 | power_rule->max_eirp = min(power_rule1->max_eirp, | |
396 | power_rule2->max_eirp); | |
397 | power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain, | |
398 | power_rule2->max_antenna_gain); | |
399 | ||
400 | intersected_rule->flags = (rule1->flags | rule2->flags); | |
401 | ||
402 | if (!is_valid_reg_rule(intersected_rule)) | |
403 | return -EINVAL; | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
408 | /** | |
409 | * regdom_intersect - do the intersection between two regulatory domains | |
410 | * @rd1: first regulatory domain | |
411 | * @rd2: second regulatory domain | |
412 | * | |
413 | * Use this function to get the intersection between two regulatory domains. | |
414 | * Once completed we will mark the alpha2 for the rd as intersected, "98", | |
415 | * as no one single alpha2 can represent this regulatory domain. | |
416 | * | |
417 | * Returns a pointer to the regulatory domain structure which will hold the | |
418 | * resulting intersection of rules between rd1 and rd2. We will | |
419 | * kzalloc() this structure for you. | |
420 | */ | |
421 | static struct ieee80211_regdomain *regdom_intersect( | |
422 | const struct ieee80211_regdomain *rd1, | |
423 | const struct ieee80211_regdomain *rd2) | |
424 | { | |
425 | int r, size_of_regd; | |
426 | unsigned int x, y; | |
427 | unsigned int num_rules = 0, rule_idx = 0; | |
428 | const struct ieee80211_reg_rule *rule1, *rule2; | |
429 | struct ieee80211_reg_rule *intersected_rule; | |
430 | struct ieee80211_regdomain *rd; | |
431 | /* This is just a dummy holder to help us count */ | |
432 | struct ieee80211_reg_rule irule; | |
433 | ||
434 | /* Uses the stack temporarily for counter arithmetic */ | |
435 | intersected_rule = &irule; | |
436 | ||
437 | memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule)); | |
438 | ||
439 | if (!rd1 || !rd2) | |
440 | return NULL; | |
441 | ||
442 | /* First we get a count of the rules we'll need, then we actually | |
443 | * build them. This is to so we can malloc() and free() a | |
444 | * regdomain once. The reason we use reg_rules_intersect() here | |
445 | * is it will return -EINVAL if the rule computed makes no sense. | |
446 | * All rules that do check out OK are valid. */ | |
447 | ||
448 | for (x = 0; x < rd1->n_reg_rules; x++) { | |
449 | rule1 = &rd1->reg_rules[x]; | |
450 | for (y = 0; y < rd2->n_reg_rules; y++) { | |
451 | rule2 = &rd2->reg_rules[y]; | |
452 | if (!reg_rules_intersect(rule1, rule2, | |
453 | intersected_rule)) | |
454 | num_rules++; | |
455 | memset(intersected_rule, 0, | |
456 | sizeof(struct ieee80211_reg_rule)); | |
457 | } | |
458 | } | |
459 | ||
460 | if (!num_rules) | |
461 | return NULL; | |
462 | ||
463 | size_of_regd = sizeof(struct ieee80211_regdomain) + | |
464 | ((num_rules + 1) * sizeof(struct ieee80211_reg_rule)); | |
465 | ||
466 | rd = kzalloc(size_of_regd, GFP_KERNEL); | |
467 | if (!rd) | |
468 | return NULL; | |
469 | ||
470 | for (x = 0; x < rd1->n_reg_rules; x++) { | |
471 | rule1 = &rd1->reg_rules[x]; | |
472 | for (y = 0; y < rd2->n_reg_rules; y++) { | |
473 | rule2 = &rd2->reg_rules[y]; | |
474 | /* This time around instead of using the stack lets | |
475 | * write to the target rule directly saving ourselves | |
476 | * a memcpy() */ | |
477 | intersected_rule = &rd->reg_rules[rule_idx]; | |
478 | r = reg_rules_intersect(rule1, rule2, | |
479 | intersected_rule); | |
480 | /* No need to memset here the intersected rule here as | |
481 | * we're not using the stack anymore */ | |
482 | if (r) | |
483 | continue; | |
484 | rule_idx++; | |
485 | } | |
486 | } | |
487 | ||
488 | if (rule_idx != num_rules) { | |
489 | kfree(rd); | |
490 | return NULL; | |
491 | } | |
492 | ||
493 | rd->n_reg_rules = num_rules; | |
494 | rd->alpha2[0] = '9'; | |
495 | rd->alpha2[1] = '8'; | |
496 | ||
497 | return rd; | |
498 | } | |
499 | ||
b2e1b302 LR |
500 | /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may |
501 | * want to just have the channel structure use these */ | |
502 | static u32 map_regdom_flags(u32 rd_flags) | |
503 | { | |
504 | u32 channel_flags = 0; | |
505 | if (rd_flags & NL80211_RRF_PASSIVE_SCAN) | |
506 | channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN; | |
507 | if (rd_flags & NL80211_RRF_NO_IBSS) | |
508 | channel_flags |= IEEE80211_CHAN_NO_IBSS; | |
509 | if (rd_flags & NL80211_RRF_DFS) | |
510 | channel_flags |= IEEE80211_CHAN_RADAR; | |
511 | return channel_flags; | |
512 | } | |
513 | ||
514 | /** | |
515 | * freq_reg_info - get regulatory information for the given frequency | |
516 | * @center_freq: Frequency in KHz for which we want regulatory information for | |
517 | * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one | |
518 | * you can set this to 0. If this frequency is allowed we then set | |
519 | * this value to the maximum allowed bandwidth. | |
520 | * @reg_rule: the regulatory rule which we have for this frequency | |
521 | * | |
522 | * Use this function to get the regulatory rule for a specific frequency. | |
523 | */ | |
524 | static int freq_reg_info(u32 center_freq, u32 *bandwidth, | |
525 | const struct ieee80211_reg_rule **reg_rule) | |
8318d78a JB |
526 | { |
527 | int i; | |
b2e1b302 | 528 | u32 max_bandwidth = 0; |
8318d78a | 529 | |
b2e1b302 LR |
530 | if (!cfg80211_regdomain) |
531 | return -EINVAL; | |
532 | ||
533 | for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) { | |
534 | const struct ieee80211_reg_rule *rr; | |
535 | const struct ieee80211_freq_range *fr = NULL; | |
536 | const struct ieee80211_power_rule *pr = NULL; | |
537 | ||
538 | rr = &cfg80211_regdomain->reg_rules[i]; | |
539 | fr = &rr->freq_range; | |
540 | pr = &rr->power_rule; | |
541 | max_bandwidth = freq_max_bandwidth(fr, center_freq); | |
542 | if (max_bandwidth && *bandwidth <= max_bandwidth) { | |
543 | *reg_rule = rr; | |
544 | *bandwidth = max_bandwidth; | |
8318d78a JB |
545 | break; |
546 | } | |
547 | } | |
548 | ||
b2e1b302 LR |
549 | return !max_bandwidth; |
550 | } | |
551 | ||
552 | static void handle_channel(struct ieee80211_channel *chan) | |
553 | { | |
554 | int r; | |
555 | u32 flags = chan->orig_flags; | |
556 | u32 max_bandwidth = 0; | |
557 | const struct ieee80211_reg_rule *reg_rule = NULL; | |
558 | const struct ieee80211_power_rule *power_rule = NULL; | |
559 | ||
560 | r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq), | |
561 | &max_bandwidth, ®_rule); | |
562 | ||
563 | if (r) { | |
8318d78a JB |
564 | flags |= IEEE80211_CHAN_DISABLED; |
565 | chan->flags = flags; | |
566 | return; | |
567 | } | |
568 | ||
b2e1b302 LR |
569 | power_rule = ®_rule->power_rule; |
570 | ||
571 | chan->flags = flags | map_regdom_flags(reg_rule->flags); | |
8318d78a | 572 | chan->max_antenna_gain = min(chan->orig_mag, |
b2e1b302 LR |
573 | (int) MBI_TO_DBI(power_rule->max_antenna_gain)); |
574 | chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth); | |
253898c4 | 575 | if (chan->orig_mpwr) |
b2e1b302 LR |
576 | chan->max_power = min(chan->orig_mpwr, |
577 | (int) MBM_TO_DBM(power_rule->max_eirp)); | |
253898c4 | 578 | else |
b2e1b302 | 579 | chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); |
8318d78a JB |
580 | } |
581 | ||
b2e1b302 | 582 | static void handle_band(struct ieee80211_supported_band *sband) |
8318d78a JB |
583 | { |
584 | int i; | |
585 | ||
586 | for (i = 0; i < sband->n_channels; i++) | |
b2e1b302 | 587 | handle_channel(&sband->channels[i]); |
8318d78a JB |
588 | } |
589 | ||
b2e1b302 | 590 | static void update_all_wiphy_regulatory(enum reg_set_by setby) |
8318d78a | 591 | { |
b2e1b302 | 592 | struct cfg80211_registered_device *drv; |
8318d78a | 593 | |
b2e1b302 LR |
594 | list_for_each_entry(drv, &cfg80211_drv_list, list) |
595 | wiphy_update_regulatory(&drv->wiphy, setby); | |
596 | } | |
597 | ||
598 | void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby) | |
599 | { | |
600 | enum ieee80211_band band; | |
601 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
8318d78a | 602 | if (wiphy->bands[band]) |
b2e1b302 LR |
603 | handle_band(wiphy->bands[band]); |
604 | if (wiphy->reg_notifier) | |
605 | wiphy->reg_notifier(wiphy, setby); | |
606 | } | |
607 | } | |
608 | ||
9c96477d LR |
609 | /* Return value which can be used by ignore_request() to indicate |
610 | * it has been determined we should intersect two regulatory domains */ | |
611 | #define REG_INTERSECT 1 | |
612 | ||
84fa4f43 JB |
613 | /* This has the logic which determines when a new request |
614 | * should be ignored. */ | |
615 | static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by, | |
616 | const char *alpha2) | |
617 | { | |
618 | /* All initial requests are respected */ | |
619 | if (!last_request) | |
620 | return 0; | |
621 | ||
622 | switch (set_by) { | |
623 | case REGDOM_SET_BY_INIT: | |
624 | return -EINVAL; | |
625 | case REGDOM_SET_BY_CORE: | |
626 | /* | |
627 | * Always respect new wireless core hints, should only happen | |
628 | * when updating the world regulatory domain at init. | |
629 | */ | |
630 | return 0; | |
631 | case REGDOM_SET_BY_COUNTRY_IE: | |
632 | if (unlikely(!is_an_alpha2(alpha2))) | |
633 | return -EINVAL; | |
634 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { | |
635 | if (last_request->wiphy != wiphy) { | |
636 | /* | |
637 | * Two cards with two APs claiming different | |
638 | * different Country IE alpha2s. We could | |
639 | * intersect them, but that seems unlikely | |
640 | * to be correct. Reject second one for now. | |
641 | */ | |
642 | if (!alpha2_equal(alpha2, | |
643 | cfg80211_regdomain->alpha2)) | |
644 | return -EOPNOTSUPP; | |
645 | return -EALREADY; | |
646 | } | |
647 | /* Two consecutive Country IE hints on the same wiphy */ | |
648 | if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2)) | |
649 | return 0; | |
650 | return -EALREADY; | |
651 | } | |
652 | /* | |
653 | * Ignore Country IE hints for now, need to think about | |
654 | * what we need to do to support multi-domain operation. | |
655 | */ | |
656 | return -EOPNOTSUPP; | |
657 | case REGDOM_SET_BY_DRIVER: | |
658 | if (last_request->initiator == REGDOM_SET_BY_DRIVER) | |
659 | return -EALREADY; | |
660 | return 0; | |
661 | case REGDOM_SET_BY_USER: | |
84fa4f43 | 662 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) |
9c96477d | 663 | return REG_INTERSECT; |
84fa4f43 JB |
664 | return 0; |
665 | } | |
666 | ||
667 | return -EINVAL; | |
668 | } | |
669 | ||
b2e1b302 LR |
670 | /* Caller must hold &cfg80211_drv_mutex */ |
671 | int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by, | |
be3d4810 | 672 | const char *alpha2) |
b2e1b302 LR |
673 | { |
674 | struct regulatory_request *request; | |
9c96477d | 675 | bool intersect = false; |
b2e1b302 LR |
676 | int r = 0; |
677 | ||
be3d4810 | 678 | r = ignore_request(wiphy, set_by, alpha2); |
9c96477d LR |
679 | |
680 | if (r == REG_INTERSECT) | |
681 | intersect = true; | |
682 | else if (r) | |
b2e1b302 LR |
683 | return r; |
684 | ||
b2e1b302 LR |
685 | switch (set_by) { |
686 | case REGDOM_SET_BY_CORE: | |
687 | case REGDOM_SET_BY_COUNTRY_IE: | |
688 | case REGDOM_SET_BY_DRIVER: | |
689 | case REGDOM_SET_BY_USER: | |
690 | request = kzalloc(sizeof(struct regulatory_request), | |
be3d4810 | 691 | GFP_KERNEL); |
b2e1b302 LR |
692 | if (!request) |
693 | return -ENOMEM; | |
694 | ||
be3d4810 JB |
695 | request->alpha2[0] = alpha2[0]; |
696 | request->alpha2[1] = alpha2[1]; | |
b2e1b302 LR |
697 | request->initiator = set_by; |
698 | request->wiphy = wiphy; | |
9c96477d | 699 | request->intersect = intersect; |
b2e1b302 | 700 | |
f6037d09 JB |
701 | kfree(last_request); |
702 | last_request = request; | |
b2e1b302 LR |
703 | r = call_crda(alpha2); |
704 | #ifndef CONFIG_WIRELESS_OLD_REGULATORY | |
705 | if (r) | |
706 | printk(KERN_ERR "cfg80211: Failed calling CRDA\n"); | |
707 | #endif | |
708 | break; | |
709 | default: | |
710 | r = -ENOTSUPP; | |
711 | break; | |
712 | } | |
713 | ||
714 | return r; | |
715 | } | |
716 | ||
be3d4810 | 717 | void regulatory_hint(struct wiphy *wiphy, const char *alpha2) |
b2e1b302 | 718 | { |
be3d4810 | 719 | BUG_ON(!alpha2); |
b2e1b302 LR |
720 | |
721 | mutex_lock(&cfg80211_drv_mutex); | |
be3d4810 | 722 | __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2); |
b2e1b302 | 723 | mutex_unlock(&cfg80211_drv_mutex); |
b2e1b302 LR |
724 | } |
725 | EXPORT_SYMBOL(regulatory_hint); | |
726 | ||
727 | ||
a3d2eaf0 | 728 | static void print_rd_rules(const struct ieee80211_regdomain *rd) |
b2e1b302 LR |
729 | { |
730 | unsigned int i; | |
a3d2eaf0 JB |
731 | const struct ieee80211_reg_rule *reg_rule = NULL; |
732 | const struct ieee80211_freq_range *freq_range = NULL; | |
733 | const struct ieee80211_power_rule *power_rule = NULL; | |
b2e1b302 LR |
734 | |
735 | printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), " | |
736 | "(max_antenna_gain, max_eirp)\n"); | |
737 | ||
738 | for (i = 0; i < rd->n_reg_rules; i++) { | |
739 | reg_rule = &rd->reg_rules[i]; | |
740 | freq_range = ®_rule->freq_range; | |
741 | power_rule = ®_rule->power_rule; | |
742 | ||
743 | /* There may not be documentation for max antenna gain | |
744 | * in certain regions */ | |
745 | if (power_rule->max_antenna_gain) | |
746 | printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), " | |
747 | "(%d mBi, %d mBm)\n", | |
748 | freq_range->start_freq_khz, | |
749 | freq_range->end_freq_khz, | |
750 | freq_range->max_bandwidth_khz, | |
751 | power_rule->max_antenna_gain, | |
752 | power_rule->max_eirp); | |
753 | else | |
754 | printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), " | |
755 | "(N/A, %d mBm)\n", | |
756 | freq_range->start_freq_khz, | |
757 | freq_range->end_freq_khz, | |
758 | freq_range->max_bandwidth_khz, | |
759 | power_rule->max_eirp); | |
760 | } | |
761 | } | |
762 | ||
a3d2eaf0 | 763 | static void print_regdomain(const struct ieee80211_regdomain *rd) |
b2e1b302 LR |
764 | { |
765 | ||
766 | if (is_world_regdom(rd->alpha2)) | |
767 | printk(KERN_INFO "cfg80211: World regulatory " | |
768 | "domain updated:\n"); | |
769 | else { | |
770 | if (is_unknown_alpha2(rd->alpha2)) | |
771 | printk(KERN_INFO "cfg80211: Regulatory domain " | |
772 | "changed to driver built-in settings " | |
773 | "(unknown country)\n"); | |
774 | else | |
775 | printk(KERN_INFO "cfg80211: Regulatory domain " | |
776 | "changed to country: %c%c\n", | |
777 | rd->alpha2[0], rd->alpha2[1]); | |
778 | } | |
779 | print_rd_rules(rd); | |
780 | } | |
781 | ||
2df78167 | 782 | static void print_regdomain_info(const struct ieee80211_regdomain *rd) |
b2e1b302 LR |
783 | { |
784 | printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n", | |
785 | rd->alpha2[0], rd->alpha2[1]); | |
786 | print_rd_rules(rd); | |
787 | } | |
788 | ||
d2372b31 | 789 | /* Takes ownership of rd only if it doesn't fail */ |
a3d2eaf0 | 790 | static int __set_regdom(const struct ieee80211_regdomain *rd) |
b2e1b302 | 791 | { |
9c96477d | 792 | const struct ieee80211_regdomain *intersected_rd = NULL; |
b2e1b302 LR |
793 | /* Some basic sanity checks first */ |
794 | ||
b2e1b302 | 795 | if (is_world_regdom(rd->alpha2)) { |
f6037d09 | 796 | if (WARN_ON(!reg_is_valid_request(rd->alpha2))) |
b2e1b302 LR |
797 | return -EINVAL; |
798 | update_world_regdomain(rd); | |
799 | return 0; | |
800 | } | |
b2e1b302 LR |
801 | |
802 | if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && | |
803 | !is_unknown_alpha2(rd->alpha2)) | |
804 | return -EINVAL; | |
805 | ||
f6037d09 | 806 | if (!last_request) |
b2e1b302 LR |
807 | return -EINVAL; |
808 | ||
942b25cf | 809 | /* allow overriding the static definitions if CRDA is present */ |
b2e1b302 | 810 | if (!is_old_static_regdom(cfg80211_regdomain) && |
942b25cf | 811 | !regdom_changed(rd->alpha2)) |
b2e1b302 | 812 | return -EINVAL; |
b2e1b302 LR |
813 | |
814 | /* Now lets set the regulatory domain, update all driver channels | |
815 | * and finally inform them of what we have done, in case they want | |
816 | * to review or adjust their own settings based on their own | |
817 | * internal EEPROM data */ | |
818 | ||
f6037d09 | 819 | if (WARN_ON(!reg_is_valid_request(rd->alpha2))) |
b2e1b302 LR |
820 | return -EINVAL; |
821 | ||
b2e1b302 | 822 | reset_regdomains(); |
b2e1b302 LR |
823 | |
824 | /* Country IE parsing coming soon */ | |
f6037d09 | 825 | switch (last_request->initiator) { |
b2e1b302 LR |
826 | case REGDOM_SET_BY_CORE: |
827 | case REGDOM_SET_BY_DRIVER: | |
828 | case REGDOM_SET_BY_USER: | |
829 | if (!is_valid_rd(rd)) { | |
830 | printk(KERN_ERR "cfg80211: Invalid " | |
831 | "regulatory domain detected:\n"); | |
832 | print_regdomain_info(rd); | |
833 | return -EINVAL; | |
834 | } | |
835 | break; | |
836 | case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */ | |
837 | WARN_ON(1); | |
838 | default: | |
839 | return -EOPNOTSUPP; | |
840 | } | |
841 | ||
9c96477d LR |
842 | if (unlikely(last_request->intersect)) { |
843 | intersected_rd = regdom_intersect(rd, cfg80211_regdomain); | |
844 | if (!intersected_rd) | |
845 | return -EINVAL; | |
846 | kfree(rd); | |
847 | rd = intersected_rd; | |
848 | } | |
849 | ||
b2e1b302 LR |
850 | /* Tada! */ |
851 | cfg80211_regdomain = rd; | |
b2e1b302 LR |
852 | |
853 | return 0; | |
854 | } | |
855 | ||
856 | ||
857 | /* Use this call to set the current regulatory domain. Conflicts with | |
858 | * multiple drivers can be ironed out later. Caller must've already | |
d2372b31 | 859 | * kmalloc'd the rd structure. Caller must hold cfg80211_drv_mutex */ |
a3d2eaf0 | 860 | int set_regdom(const struct ieee80211_regdomain *rd) |
b2e1b302 | 861 | { |
b2e1b302 LR |
862 | int r; |
863 | ||
b2e1b302 LR |
864 | /* Note that this doesn't update the wiphys, this is done below */ |
865 | r = __set_regdom(rd); | |
d2372b31 JB |
866 | if (r) { |
867 | kfree(rd); | |
b2e1b302 | 868 | return r; |
d2372b31 | 869 | } |
b2e1b302 | 870 | |
b2e1b302 LR |
871 | /* This would make this whole thing pointless */ |
872 | BUG_ON(rd != cfg80211_regdomain); | |
873 | ||
874 | /* update all wiphys now with the new established regulatory domain */ | |
f6037d09 | 875 | update_all_wiphy_regulatory(last_request->initiator); |
b2e1b302 LR |
876 | |
877 | print_regdomain(rd); | |
878 | ||
879 | return r; | |
880 | } | |
881 | ||
882 | int regulatory_init(void) | |
883 | { | |
734366de JB |
884 | int err; |
885 | ||
b2e1b302 LR |
886 | reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0); |
887 | if (IS_ERR(reg_pdev)) | |
888 | return PTR_ERR(reg_pdev); | |
734366de JB |
889 | |
890 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY | |
a3d2eaf0 | 891 | cfg80211_regdomain = static_regdom(ieee80211_regdom); |
734366de | 892 | |
942b25cf | 893 | printk(KERN_INFO "cfg80211: Using static regulatory domain info\n"); |
734366de JB |
894 | print_regdomain_info(cfg80211_regdomain); |
895 | /* The old code still requests for a new regdomain and if | |
896 | * you have CRDA you get it updated, otherwise you get | |
897 | * stuck with the static values. We ignore "EU" code as | |
898 | * that is not a valid ISO / IEC 3166 alpha2 */ | |
ac9440a4 | 899 | if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U') |
734366de | 900 | err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, |
be3d4810 | 901 | ieee80211_regdom); |
734366de | 902 | #else |
a3d2eaf0 | 903 | cfg80211_regdomain = cfg80211_world_regdom; |
734366de | 904 | |
be3d4810 | 905 | err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00"); |
734366de JB |
906 | if (err) |
907 | printk(KERN_ERR "cfg80211: calling CRDA failed - " | |
908 | "unable to update world regulatory domain, " | |
909 | "using static definition\n"); | |
910 | #endif | |
911 | ||
b2e1b302 LR |
912 | return 0; |
913 | } | |
914 | ||
915 | void regulatory_exit(void) | |
916 | { | |
b2e1b302 | 917 | mutex_lock(&cfg80211_drv_mutex); |
734366de | 918 | |
b2e1b302 | 919 | reset_regdomains(); |
734366de | 920 | |
f6037d09 JB |
921 | kfree(last_request); |
922 | ||
b2e1b302 | 923 | platform_device_unregister(reg_pdev); |
734366de | 924 | |
b2e1b302 | 925 | mutex_unlock(&cfg80211_drv_mutex); |
8318d78a | 926 | } |