]>
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 | |
5166ccd2 | 45 | /* Receipt of information from last regulatory request */ |
f6037d09 | 46 | static struct regulatory_request *last_request; |
734366de | 47 | |
b2e1b302 LR |
48 | /* To trigger userspace events */ |
49 | static struct platform_device *reg_pdev; | |
8318d78a | 50 | |
b2e1b302 LR |
51 | /* Keep the ordering from large to small */ |
52 | static u32 supported_bandwidths[] = { | |
53 | MHZ_TO_KHZ(40), | |
54 | MHZ_TO_KHZ(20), | |
8318d78a JB |
55 | }; |
56 | ||
fb1fc7ad LR |
57 | /* |
58 | * Central wireless core regulatory domains, we only need two, | |
734366de | 59 | * the current one and a world regulatory domain in case we have no |
fb1fc7ad LR |
60 | * information to give us an alpha2 |
61 | */ | |
f130347c | 62 | const struct ieee80211_regdomain *cfg80211_regdomain; |
734366de | 63 | |
fb1fc7ad LR |
64 | /* |
65 | * We use this as a place for the rd structure built from the | |
3f2355cb | 66 | * last parsed country IE to rest until CRDA gets back to us with |
fb1fc7ad LR |
67 | * what it thinks should apply for the same country |
68 | */ | |
3f2355cb LR |
69 | static const struct ieee80211_regdomain *country_ie_regdomain; |
70 | ||
e38f8a7a | 71 | /* Used to queue up regulatory hints */ |
fe33eb39 LR |
72 | static LIST_HEAD(reg_requests_list); |
73 | static spinlock_t reg_requests_lock; | |
74 | ||
e38f8a7a LR |
75 | /* Used to queue up beacon hints for review */ |
76 | static LIST_HEAD(reg_pending_beacons); | |
77 | static spinlock_t reg_pending_beacons_lock; | |
78 | ||
79 | /* Used to keep track of processed beacon hints */ | |
80 | static LIST_HEAD(reg_beacon_list); | |
81 | ||
82 | struct reg_beacon { | |
83 | struct list_head list; | |
84 | struct ieee80211_channel chan; | |
85 | }; | |
86 | ||
734366de JB |
87 | /* We keep a static world regulatory domain in case of the absence of CRDA */ |
88 | static const struct ieee80211_regdomain world_regdom = { | |
3fc71f77 | 89 | .n_reg_rules = 3, |
734366de JB |
90 | .alpha2 = "00", |
91 | .reg_rules = { | |
68798a62 LR |
92 | /* IEEE 802.11b/g, channels 1..11 */ |
93 | REG_RULE(2412-10, 2462+10, 40, 6, 20, 0), | |
3fc71f77 LR |
94 | /* IEEE 802.11a, channel 36..48 */ |
95 | REG_RULE(5180-10, 5240+10, 40, 6, 23, | |
96 | NL80211_RRF_PASSIVE_SCAN | | |
97 | NL80211_RRF_NO_IBSS), | |
98 | ||
99 | /* NB: 5260 MHz - 5700 MHz requies DFS */ | |
100 | ||
101 | /* IEEE 802.11a, channel 149..165 */ | |
102 | REG_RULE(5745-10, 5825+10, 40, 6, 23, | |
103 | NL80211_RRF_PASSIVE_SCAN | | |
104 | NL80211_RRF_NO_IBSS), | |
734366de JB |
105 | } |
106 | }; | |
107 | ||
a3d2eaf0 JB |
108 | static const struct ieee80211_regdomain *cfg80211_world_regdom = |
109 | &world_regdom; | |
734366de JB |
110 | |
111 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY | |
112 | static char *ieee80211_regdom = "US"; | |
113 | module_param(ieee80211_regdom, charp, 0444); | |
114 | MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); | |
115 | ||
fb1fc7ad LR |
116 | /* |
117 | * We assume 40 MHz bandwidth for the old regulatory work. | |
734366de | 118 | * We make emphasis we are using the exact same frequencies |
fb1fc7ad LR |
119 | * as before |
120 | */ | |
734366de JB |
121 | |
122 | static 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 | ||
141 | static 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 | ||
157 | static const struct ieee80211_regdomain eu_regdom = { | |
158 | .n_reg_rules = 6, | |
fb1fc7ad LR |
159 | /* |
160 | * This alpha2 is bogus, we leave it here just for stupid | |
161 | * backward compatibility | |
162 | */ | |
734366de JB |
163 | .alpha2 = "EU", |
164 | .reg_rules = { | |
165 | /* IEEE 802.11b/g, channels 1..13 */ | |
166 | REG_RULE(2412-10, 2472+10, 40, 6, 20, 0), | |
167 | /* IEEE 802.11a, channel 36 */ | |
168 | REG_RULE(5180-10, 5180+10, 40, 6, 23, | |
169 | NL80211_RRF_PASSIVE_SCAN), | |
170 | /* IEEE 802.11a, channel 40 */ | |
171 | REG_RULE(5200-10, 5200+10, 40, 6, 23, | |
172 | NL80211_RRF_PASSIVE_SCAN), | |
173 | /* IEEE 802.11a, channel 44 */ | |
174 | REG_RULE(5220-10, 5220+10, 40, 6, 23, | |
175 | NL80211_RRF_PASSIVE_SCAN), | |
176 | /* IEEE 802.11a, channels 48..64 */ | |
177 | REG_RULE(5240-10, 5320+10, 40, 6, 20, | |
178 | NL80211_RRF_NO_IBSS | | |
179 | NL80211_RRF_DFS), | |
180 | /* IEEE 802.11a, channels 100..140 */ | |
181 | REG_RULE(5500-10, 5700+10, 40, 6, 30, | |
182 | NL80211_RRF_NO_IBSS | | |
183 | NL80211_RRF_DFS), | |
184 | } | |
185 | }; | |
186 | ||
187 | static const struct ieee80211_regdomain *static_regdom(char *alpha2) | |
188 | { | |
189 | if (alpha2[0] == 'U' && alpha2[1] == 'S') | |
190 | return &us_regdom; | |
191 | if (alpha2[0] == 'J' && alpha2[1] == 'P') | |
192 | return &jp_regdom; | |
193 | if (alpha2[0] == 'E' && alpha2[1] == 'U') | |
194 | return &eu_regdom; | |
195 | /* Default, as per the old rules */ | |
196 | return &us_regdom; | |
197 | } | |
198 | ||
a3d2eaf0 | 199 | static bool is_old_static_regdom(const struct ieee80211_regdomain *rd) |
734366de JB |
200 | { |
201 | if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom) | |
202 | return true; | |
203 | return false; | |
204 | } | |
942b25cf JB |
205 | #else |
206 | static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd) | |
734366de | 207 | { |
942b25cf | 208 | return false; |
734366de | 209 | } |
942b25cf JB |
210 | #endif |
211 | ||
734366de JB |
212 | static void reset_regdomains(void) |
213 | { | |
942b25cf JB |
214 | /* avoid freeing static information or freeing something twice */ |
215 | if (cfg80211_regdomain == cfg80211_world_regdom) | |
216 | cfg80211_regdomain = NULL; | |
217 | if (cfg80211_world_regdom == &world_regdom) | |
218 | cfg80211_world_regdom = NULL; | |
219 | if (cfg80211_regdomain == &world_regdom) | |
220 | cfg80211_regdomain = NULL; | |
221 | if (is_old_static_regdom(cfg80211_regdomain)) | |
222 | cfg80211_regdomain = NULL; | |
223 | ||
224 | kfree(cfg80211_regdomain); | |
225 | kfree(cfg80211_world_regdom); | |
734366de | 226 | |
a3d2eaf0 | 227 | cfg80211_world_regdom = &world_regdom; |
734366de JB |
228 | cfg80211_regdomain = NULL; |
229 | } | |
230 | ||
fb1fc7ad LR |
231 | /* |
232 | * Dynamic world regulatory domain requested by the wireless | |
233 | * core upon initialization | |
234 | */ | |
a3d2eaf0 | 235 | static void update_world_regdomain(const struct ieee80211_regdomain *rd) |
734366de | 236 | { |
f6037d09 | 237 | BUG_ON(!last_request); |
734366de JB |
238 | |
239 | reset_regdomains(); | |
240 | ||
241 | cfg80211_world_regdom = rd; | |
242 | cfg80211_regdomain = rd; | |
243 | } | |
734366de | 244 | |
a3d2eaf0 | 245 | bool is_world_regdom(const char *alpha2) |
b2e1b302 LR |
246 | { |
247 | if (!alpha2) | |
248 | return false; | |
249 | if (alpha2[0] == '0' && alpha2[1] == '0') | |
250 | return true; | |
251 | return false; | |
252 | } | |
8318d78a | 253 | |
a3d2eaf0 | 254 | static bool is_alpha2_set(const char *alpha2) |
b2e1b302 LR |
255 | { |
256 | if (!alpha2) | |
257 | return false; | |
258 | if (alpha2[0] != 0 && alpha2[1] != 0) | |
259 | return true; | |
260 | return false; | |
261 | } | |
8318d78a | 262 | |
b2e1b302 LR |
263 | static bool is_alpha_upper(char letter) |
264 | { | |
265 | /* ASCII A - Z */ | |
266 | if (letter >= 65 && letter <= 90) | |
267 | return true; | |
268 | return false; | |
269 | } | |
8318d78a | 270 | |
a3d2eaf0 | 271 | static bool is_unknown_alpha2(const char *alpha2) |
b2e1b302 LR |
272 | { |
273 | if (!alpha2) | |
274 | return false; | |
fb1fc7ad LR |
275 | /* |
276 | * Special case where regulatory domain was built by driver | |
277 | * but a specific alpha2 cannot be determined | |
278 | */ | |
b2e1b302 LR |
279 | if (alpha2[0] == '9' && alpha2[1] == '9') |
280 | return true; | |
281 | return false; | |
282 | } | |
8318d78a | 283 | |
3f2355cb LR |
284 | static bool is_intersected_alpha2(const char *alpha2) |
285 | { | |
286 | if (!alpha2) | |
287 | return false; | |
fb1fc7ad LR |
288 | /* |
289 | * Special case where regulatory domain is the | |
3f2355cb | 290 | * result of an intersection between two regulatory domain |
fb1fc7ad LR |
291 | * structures |
292 | */ | |
3f2355cb LR |
293 | if (alpha2[0] == '9' && alpha2[1] == '8') |
294 | return true; | |
295 | return false; | |
296 | } | |
297 | ||
a3d2eaf0 | 298 | static bool is_an_alpha2(const char *alpha2) |
b2e1b302 LR |
299 | { |
300 | if (!alpha2) | |
301 | return false; | |
302 | if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1])) | |
303 | return true; | |
304 | return false; | |
305 | } | |
8318d78a | 306 | |
a3d2eaf0 | 307 | static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y) |
b2e1b302 LR |
308 | { |
309 | if (!alpha2_x || !alpha2_y) | |
310 | return false; | |
311 | if (alpha2_x[0] == alpha2_y[0] && | |
312 | alpha2_x[1] == alpha2_y[1]) | |
313 | return true; | |
314 | return false; | |
315 | } | |
316 | ||
69b1572b | 317 | static bool regdom_changes(const char *alpha2) |
b2e1b302 | 318 | { |
761cf7ec LR |
319 | assert_cfg80211_lock(); |
320 | ||
b2e1b302 LR |
321 | if (!cfg80211_regdomain) |
322 | return true; | |
323 | if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2)) | |
324 | return false; | |
325 | return true; | |
326 | } | |
327 | ||
3f2355cb LR |
328 | /** |
329 | * country_ie_integrity_changes - tells us if the country IE has changed | |
330 | * @checksum: checksum of country IE of fields we are interested in | |
331 | * | |
332 | * If the country IE has not changed you can ignore it safely. This is | |
333 | * useful to determine if two devices are seeing two different country IEs | |
334 | * even on the same alpha2. Note that this will return false if no IE has | |
335 | * been set on the wireless core yet. | |
336 | */ | |
337 | static bool country_ie_integrity_changes(u32 checksum) | |
338 | { | |
339 | /* If no IE has been set then the checksum doesn't change */ | |
340 | if (unlikely(!last_request->country_ie_checksum)) | |
341 | return false; | |
342 | if (unlikely(last_request->country_ie_checksum != checksum)) | |
343 | return true; | |
344 | return false; | |
345 | } | |
346 | ||
fb1fc7ad LR |
347 | /* |
348 | * This lets us keep regulatory code which is updated on a regulatory | |
349 | * basis in userspace. | |
350 | */ | |
b2e1b302 LR |
351 | static int call_crda(const char *alpha2) |
352 | { | |
353 | char country_env[9 + 2] = "COUNTRY="; | |
354 | char *envp[] = { | |
355 | country_env, | |
356 | NULL | |
357 | }; | |
358 | ||
359 | if (!is_world_regdom((char *) alpha2)) | |
360 | printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n", | |
361 | alpha2[0], alpha2[1]); | |
362 | else | |
b2e1b302 LR |
363 | printk(KERN_INFO "cfg80211: Calling CRDA to update world " |
364 | "regulatory domain\n"); | |
b2e1b302 LR |
365 | |
366 | country_env[8] = alpha2[0]; | |
367 | country_env[9] = alpha2[1]; | |
368 | ||
369 | return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp); | |
370 | } | |
371 | ||
b2e1b302 | 372 | /* Used by nl80211 before kmalloc'ing our regulatory domain */ |
a3d2eaf0 | 373 | bool reg_is_valid_request(const char *alpha2) |
b2e1b302 | 374 | { |
f6037d09 JB |
375 | if (!last_request) |
376 | return false; | |
377 | ||
378 | return alpha2_equal(last_request->alpha2, alpha2); | |
b2e1b302 | 379 | } |
8318d78a | 380 | |
b2e1b302 | 381 | /* Sanity check on a regulatory rule */ |
a3d2eaf0 | 382 | static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule) |
8318d78a | 383 | { |
a3d2eaf0 | 384 | const struct ieee80211_freq_range *freq_range = &rule->freq_range; |
b2e1b302 LR |
385 | u32 freq_diff; |
386 | ||
91e99004 | 387 | if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0) |
b2e1b302 LR |
388 | return false; |
389 | ||
390 | if (freq_range->start_freq_khz > freq_range->end_freq_khz) | |
391 | return false; | |
392 | ||
393 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; | |
394 | ||
d71aaf60 | 395 | if (freq_diff <= 0 || freq_range->max_bandwidth_khz > freq_diff) |
b2e1b302 LR |
396 | return false; |
397 | ||
398 | return true; | |
399 | } | |
400 | ||
a3d2eaf0 | 401 | static bool is_valid_rd(const struct ieee80211_regdomain *rd) |
b2e1b302 | 402 | { |
a3d2eaf0 | 403 | const struct ieee80211_reg_rule *reg_rule = NULL; |
b2e1b302 | 404 | unsigned int i; |
8318d78a | 405 | |
b2e1b302 LR |
406 | if (!rd->n_reg_rules) |
407 | return false; | |
8318d78a | 408 | |
88dc1c3f LR |
409 | if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES)) |
410 | return false; | |
411 | ||
b2e1b302 LR |
412 | for (i = 0; i < rd->n_reg_rules; i++) { |
413 | reg_rule = &rd->reg_rules[i]; | |
414 | if (!is_valid_reg_rule(reg_rule)) | |
415 | return false; | |
416 | } | |
417 | ||
418 | return true; | |
8318d78a JB |
419 | } |
420 | ||
b2e1b302 LR |
421 | /* Returns value in KHz */ |
422 | static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range, | |
423 | u32 freq) | |
424 | { | |
425 | unsigned int i; | |
426 | for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) { | |
427 | u32 start_freq_khz = freq - supported_bandwidths[i]/2; | |
428 | u32 end_freq_khz = freq + supported_bandwidths[i]/2; | |
429 | if (start_freq_khz >= freq_range->start_freq_khz && | |
430 | end_freq_khz <= freq_range->end_freq_khz) | |
431 | return supported_bandwidths[i]; | |
432 | } | |
433 | return 0; | |
434 | } | |
8318d78a | 435 | |
0c7dc45d LR |
436 | /** |
437 | * freq_in_rule_band - tells us if a frequency is in a frequency band | |
438 | * @freq_range: frequency rule we want to query | |
439 | * @freq_khz: frequency we are inquiring about | |
440 | * | |
441 | * This lets us know if a specific frequency rule is or is not relevant to | |
442 | * a specific frequency's band. Bands are device specific and artificial | |
443 | * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is | |
444 | * safe for now to assume that a frequency rule should not be part of a | |
445 | * frequency's band if the start freq or end freq are off by more than 2 GHz. | |
446 | * This resolution can be lowered and should be considered as we add | |
447 | * regulatory rule support for other "bands". | |
448 | **/ | |
449 | static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range, | |
450 | u32 freq_khz) | |
451 | { | |
452 | #define ONE_GHZ_IN_KHZ 1000000 | |
453 | if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ)) | |
454 | return true; | |
455 | if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ)) | |
456 | return true; | |
457 | return false; | |
458 | #undef ONE_GHZ_IN_KHZ | |
459 | } | |
460 | ||
fb1fc7ad LR |
461 | /* |
462 | * Converts a country IE to a regulatory domain. A regulatory domain | |
3f2355cb LR |
463 | * structure has a lot of information which the IE doesn't yet have, |
464 | * so for the other values we use upper max values as we will intersect | |
fb1fc7ad LR |
465 | * with our userspace regulatory agent to get lower bounds. |
466 | */ | |
3f2355cb LR |
467 | static struct ieee80211_regdomain *country_ie_2_rd( |
468 | u8 *country_ie, | |
469 | u8 country_ie_len, | |
470 | u32 *checksum) | |
471 | { | |
472 | struct ieee80211_regdomain *rd = NULL; | |
473 | unsigned int i = 0; | |
474 | char alpha2[2]; | |
475 | u32 flags = 0; | |
476 | u32 num_rules = 0, size_of_regd = 0; | |
477 | u8 *triplets_start = NULL; | |
478 | u8 len_at_triplet = 0; | |
479 | /* the last channel we have registered in a subband (triplet) */ | |
480 | int last_sub_max_channel = 0; | |
481 | ||
482 | *checksum = 0xDEADBEEF; | |
483 | ||
484 | /* Country IE requirements */ | |
485 | BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN || | |
486 | country_ie_len & 0x01); | |
487 | ||
488 | alpha2[0] = country_ie[0]; | |
489 | alpha2[1] = country_ie[1]; | |
490 | ||
491 | /* | |
492 | * Third octet can be: | |
493 | * 'I' - Indoor | |
494 | * 'O' - Outdoor | |
495 | * | |
496 | * anything else we assume is no restrictions | |
497 | */ | |
498 | if (country_ie[2] == 'I') | |
499 | flags = NL80211_RRF_NO_OUTDOOR; | |
500 | else if (country_ie[2] == 'O') | |
501 | flags = NL80211_RRF_NO_INDOOR; | |
502 | ||
503 | country_ie += 3; | |
504 | country_ie_len -= 3; | |
505 | ||
506 | triplets_start = country_ie; | |
507 | len_at_triplet = country_ie_len; | |
508 | ||
509 | *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8); | |
510 | ||
fb1fc7ad LR |
511 | /* |
512 | * We need to build a reg rule for each triplet, but first we must | |
3f2355cb | 513 | * calculate the number of reg rules we will need. We will need one |
fb1fc7ad LR |
514 | * for each channel subband |
515 | */ | |
3f2355cb | 516 | while (country_ie_len >= 3) { |
615aab4b | 517 | int end_channel = 0; |
3f2355cb LR |
518 | struct ieee80211_country_ie_triplet *triplet = |
519 | (struct ieee80211_country_ie_triplet *) country_ie; | |
520 | int cur_sub_max_channel = 0, cur_channel = 0; | |
521 | ||
522 | if (triplet->ext.reg_extension_id >= | |
523 | IEEE80211_COUNTRY_EXTENSION_ID) { | |
524 | country_ie += 3; | |
525 | country_ie_len -= 3; | |
526 | continue; | |
527 | } | |
528 | ||
615aab4b LR |
529 | /* 2 GHz */ |
530 | if (triplet->chans.first_channel <= 14) | |
531 | end_channel = triplet->chans.first_channel + | |
532 | triplet->chans.num_channels; | |
533 | else | |
534 | /* | |
535 | * 5 GHz -- For example in country IEs if the first | |
536 | * channel given is 36 and the number of channels is 4 | |
537 | * then the individual channel numbers defined for the | |
538 | * 5 GHz PHY by these parameters are: 36, 40, 44, and 48 | |
539 | * and not 36, 37, 38, 39. | |
540 | * | |
541 | * See: http://tinyurl.com/11d-clarification | |
542 | */ | |
543 | end_channel = triplet->chans.first_channel + | |
544 | (4 * (triplet->chans.num_channels - 1)); | |
545 | ||
3f2355cb | 546 | cur_channel = triplet->chans.first_channel; |
615aab4b | 547 | cur_sub_max_channel = end_channel; |
3f2355cb LR |
548 | |
549 | /* Basic sanity check */ | |
550 | if (cur_sub_max_channel < cur_channel) | |
551 | return NULL; | |
552 | ||
fb1fc7ad LR |
553 | /* |
554 | * Do not allow overlapping channels. Also channels | |
3f2355cb | 555 | * passed in each subband must be monotonically |
fb1fc7ad LR |
556 | * increasing |
557 | */ | |
3f2355cb LR |
558 | if (last_sub_max_channel) { |
559 | if (cur_channel <= last_sub_max_channel) | |
560 | return NULL; | |
561 | if (cur_sub_max_channel <= last_sub_max_channel) | |
562 | return NULL; | |
563 | } | |
564 | ||
fb1fc7ad LR |
565 | /* |
566 | * When dot11RegulatoryClassesRequired is supported | |
3f2355cb LR |
567 | * we can throw ext triplets as part of this soup, |
568 | * for now we don't care when those change as we | |
fb1fc7ad LR |
569 | * don't support them |
570 | */ | |
3f2355cb LR |
571 | *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) | |
572 | ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) | | |
573 | ((triplet->chans.max_power ^ cur_sub_max_channel) << 24); | |
574 | ||
575 | last_sub_max_channel = cur_sub_max_channel; | |
576 | ||
577 | country_ie += 3; | |
578 | country_ie_len -= 3; | |
579 | num_rules++; | |
580 | ||
fb1fc7ad LR |
581 | /* |
582 | * Note: this is not a IEEE requirement but | |
583 | * simply a memory requirement | |
584 | */ | |
3f2355cb LR |
585 | if (num_rules > NL80211_MAX_SUPP_REG_RULES) |
586 | return NULL; | |
587 | } | |
588 | ||
589 | country_ie = triplets_start; | |
590 | country_ie_len = len_at_triplet; | |
591 | ||
592 | size_of_regd = sizeof(struct ieee80211_regdomain) + | |
593 | (num_rules * sizeof(struct ieee80211_reg_rule)); | |
594 | ||
595 | rd = kzalloc(size_of_regd, GFP_KERNEL); | |
596 | if (!rd) | |
597 | return NULL; | |
598 | ||
599 | rd->n_reg_rules = num_rules; | |
600 | rd->alpha2[0] = alpha2[0]; | |
601 | rd->alpha2[1] = alpha2[1]; | |
602 | ||
603 | /* This time around we fill in the rd */ | |
604 | while (country_ie_len >= 3) { | |
02e68a3d | 605 | int end_channel = 0; |
3f2355cb LR |
606 | struct ieee80211_country_ie_triplet *triplet = |
607 | (struct ieee80211_country_ie_triplet *) country_ie; | |
608 | struct ieee80211_reg_rule *reg_rule = NULL; | |
609 | struct ieee80211_freq_range *freq_range = NULL; | |
610 | struct ieee80211_power_rule *power_rule = NULL; | |
611 | ||
fb1fc7ad LR |
612 | /* |
613 | * Must parse if dot11RegulatoryClassesRequired is true, | |
614 | * we don't support this yet | |
615 | */ | |
3f2355cb LR |
616 | if (triplet->ext.reg_extension_id >= |
617 | IEEE80211_COUNTRY_EXTENSION_ID) { | |
618 | country_ie += 3; | |
619 | country_ie_len -= 3; | |
620 | continue; | |
621 | } | |
622 | ||
623 | reg_rule = &rd->reg_rules[i]; | |
624 | freq_range = ®_rule->freq_range; | |
625 | power_rule = ®_rule->power_rule; | |
626 | ||
627 | reg_rule->flags = flags; | |
628 | ||
02e68a3d LR |
629 | /* 2 GHz */ |
630 | if (triplet->chans.first_channel <= 14) | |
631 | end_channel = triplet->chans.first_channel + | |
632 | triplet->chans.num_channels; | |
633 | else | |
02e68a3d LR |
634 | end_channel = triplet->chans.first_channel + |
635 | (4 * (triplet->chans.num_channels - 1)); | |
636 | ||
fb1fc7ad LR |
637 | /* |
638 | * The +10 is since the regulatory domain expects | |
3f2355cb LR |
639 | * the actual band edge, not the center of freq for |
640 | * its start and end freqs, assuming 20 MHz bandwidth on | |
fb1fc7ad LR |
641 | * the channels passed |
642 | */ | |
3f2355cb LR |
643 | freq_range->start_freq_khz = |
644 | MHZ_TO_KHZ(ieee80211_channel_to_frequency( | |
645 | triplet->chans.first_channel) - 10); | |
646 | freq_range->end_freq_khz = | |
647 | MHZ_TO_KHZ(ieee80211_channel_to_frequency( | |
02e68a3d | 648 | end_channel) + 10); |
3f2355cb | 649 | |
fb1fc7ad LR |
650 | /* |
651 | * These are large arbitrary values we use to intersect later. | |
652 | * Increment this if we ever support >= 40 MHz channels | |
653 | * in IEEE 802.11 | |
654 | */ | |
3f2355cb LR |
655 | freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40); |
656 | power_rule->max_antenna_gain = DBI_TO_MBI(100); | |
657 | power_rule->max_eirp = DBM_TO_MBM(100); | |
658 | ||
659 | country_ie += 3; | |
660 | country_ie_len -= 3; | |
661 | i++; | |
662 | ||
663 | BUG_ON(i > NL80211_MAX_SUPP_REG_RULES); | |
664 | } | |
665 | ||
666 | return rd; | |
667 | } | |
668 | ||
669 | ||
fb1fc7ad LR |
670 | /* |
671 | * Helper for regdom_intersect(), this does the real | |
672 | * mathematical intersection fun | |
673 | */ | |
9c96477d LR |
674 | static int reg_rules_intersect( |
675 | const struct ieee80211_reg_rule *rule1, | |
676 | const struct ieee80211_reg_rule *rule2, | |
677 | struct ieee80211_reg_rule *intersected_rule) | |
678 | { | |
679 | const struct ieee80211_freq_range *freq_range1, *freq_range2; | |
680 | struct ieee80211_freq_range *freq_range; | |
681 | const struct ieee80211_power_rule *power_rule1, *power_rule2; | |
682 | struct ieee80211_power_rule *power_rule; | |
683 | u32 freq_diff; | |
684 | ||
685 | freq_range1 = &rule1->freq_range; | |
686 | freq_range2 = &rule2->freq_range; | |
687 | freq_range = &intersected_rule->freq_range; | |
688 | ||
689 | power_rule1 = &rule1->power_rule; | |
690 | power_rule2 = &rule2->power_rule; | |
691 | power_rule = &intersected_rule->power_rule; | |
692 | ||
693 | freq_range->start_freq_khz = max(freq_range1->start_freq_khz, | |
694 | freq_range2->start_freq_khz); | |
695 | freq_range->end_freq_khz = min(freq_range1->end_freq_khz, | |
696 | freq_range2->end_freq_khz); | |
697 | freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz, | |
698 | freq_range2->max_bandwidth_khz); | |
699 | ||
700 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; | |
701 | if (freq_range->max_bandwidth_khz > freq_diff) | |
702 | freq_range->max_bandwidth_khz = freq_diff; | |
703 | ||
704 | power_rule->max_eirp = min(power_rule1->max_eirp, | |
705 | power_rule2->max_eirp); | |
706 | power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain, | |
707 | power_rule2->max_antenna_gain); | |
708 | ||
709 | intersected_rule->flags = (rule1->flags | rule2->flags); | |
710 | ||
711 | if (!is_valid_reg_rule(intersected_rule)) | |
712 | return -EINVAL; | |
713 | ||
714 | return 0; | |
715 | } | |
716 | ||
717 | /** | |
718 | * regdom_intersect - do the intersection between two regulatory domains | |
719 | * @rd1: first regulatory domain | |
720 | * @rd2: second regulatory domain | |
721 | * | |
722 | * Use this function to get the intersection between two regulatory domains. | |
723 | * Once completed we will mark the alpha2 for the rd as intersected, "98", | |
724 | * as no one single alpha2 can represent this regulatory domain. | |
725 | * | |
726 | * Returns a pointer to the regulatory domain structure which will hold the | |
727 | * resulting intersection of rules between rd1 and rd2. We will | |
728 | * kzalloc() this structure for you. | |
729 | */ | |
730 | static struct ieee80211_regdomain *regdom_intersect( | |
731 | const struct ieee80211_regdomain *rd1, | |
732 | const struct ieee80211_regdomain *rd2) | |
733 | { | |
734 | int r, size_of_regd; | |
735 | unsigned int x, y; | |
736 | unsigned int num_rules = 0, rule_idx = 0; | |
737 | const struct ieee80211_reg_rule *rule1, *rule2; | |
738 | struct ieee80211_reg_rule *intersected_rule; | |
739 | struct ieee80211_regdomain *rd; | |
740 | /* This is just a dummy holder to help us count */ | |
741 | struct ieee80211_reg_rule irule; | |
742 | ||
743 | /* Uses the stack temporarily for counter arithmetic */ | |
744 | intersected_rule = &irule; | |
745 | ||
746 | memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule)); | |
747 | ||
748 | if (!rd1 || !rd2) | |
749 | return NULL; | |
750 | ||
fb1fc7ad LR |
751 | /* |
752 | * First we get a count of the rules we'll need, then we actually | |
9c96477d LR |
753 | * build them. This is to so we can malloc() and free() a |
754 | * regdomain once. The reason we use reg_rules_intersect() here | |
755 | * is it will return -EINVAL if the rule computed makes no sense. | |
fb1fc7ad LR |
756 | * All rules that do check out OK are valid. |
757 | */ | |
9c96477d LR |
758 | |
759 | for (x = 0; x < rd1->n_reg_rules; x++) { | |
760 | rule1 = &rd1->reg_rules[x]; | |
761 | for (y = 0; y < rd2->n_reg_rules; y++) { | |
762 | rule2 = &rd2->reg_rules[y]; | |
763 | if (!reg_rules_intersect(rule1, rule2, | |
764 | intersected_rule)) | |
765 | num_rules++; | |
766 | memset(intersected_rule, 0, | |
767 | sizeof(struct ieee80211_reg_rule)); | |
768 | } | |
769 | } | |
770 | ||
771 | if (!num_rules) | |
772 | return NULL; | |
773 | ||
774 | size_of_regd = sizeof(struct ieee80211_regdomain) + | |
775 | ((num_rules + 1) * sizeof(struct ieee80211_reg_rule)); | |
776 | ||
777 | rd = kzalloc(size_of_regd, GFP_KERNEL); | |
778 | if (!rd) | |
779 | return NULL; | |
780 | ||
781 | for (x = 0; x < rd1->n_reg_rules; x++) { | |
782 | rule1 = &rd1->reg_rules[x]; | |
783 | for (y = 0; y < rd2->n_reg_rules; y++) { | |
784 | rule2 = &rd2->reg_rules[y]; | |
fb1fc7ad LR |
785 | /* |
786 | * This time around instead of using the stack lets | |
9c96477d | 787 | * write to the target rule directly saving ourselves |
fb1fc7ad LR |
788 | * a memcpy() |
789 | */ | |
9c96477d LR |
790 | intersected_rule = &rd->reg_rules[rule_idx]; |
791 | r = reg_rules_intersect(rule1, rule2, | |
792 | intersected_rule); | |
fb1fc7ad LR |
793 | /* |
794 | * No need to memset here the intersected rule here as | |
795 | * we're not using the stack anymore | |
796 | */ | |
9c96477d LR |
797 | if (r) |
798 | continue; | |
799 | rule_idx++; | |
800 | } | |
801 | } | |
802 | ||
803 | if (rule_idx != num_rules) { | |
804 | kfree(rd); | |
805 | return NULL; | |
806 | } | |
807 | ||
808 | rd->n_reg_rules = num_rules; | |
809 | rd->alpha2[0] = '9'; | |
810 | rd->alpha2[1] = '8'; | |
811 | ||
812 | return rd; | |
813 | } | |
814 | ||
fb1fc7ad LR |
815 | /* |
816 | * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may | |
817 | * want to just have the channel structure use these | |
818 | */ | |
b2e1b302 LR |
819 | static u32 map_regdom_flags(u32 rd_flags) |
820 | { | |
821 | u32 channel_flags = 0; | |
822 | if (rd_flags & NL80211_RRF_PASSIVE_SCAN) | |
823 | channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN; | |
824 | if (rd_flags & NL80211_RRF_NO_IBSS) | |
825 | channel_flags |= IEEE80211_CHAN_NO_IBSS; | |
826 | if (rd_flags & NL80211_RRF_DFS) | |
827 | channel_flags |= IEEE80211_CHAN_RADAR; | |
828 | return channel_flags; | |
829 | } | |
830 | ||
1fa25e41 LR |
831 | static int freq_reg_info_regd(struct wiphy *wiphy, |
832 | u32 center_freq, | |
833 | u32 *bandwidth, | |
834 | const struct ieee80211_reg_rule **reg_rule, | |
835 | const struct ieee80211_regdomain *custom_regd) | |
8318d78a JB |
836 | { |
837 | int i; | |
0c7dc45d | 838 | bool band_rule_found = false; |
3e0c3ff3 | 839 | const struct ieee80211_regdomain *regd; |
b2e1b302 | 840 | u32 max_bandwidth = 0; |
8318d78a | 841 | |
1fa25e41 | 842 | regd = custom_regd ? custom_regd : cfg80211_regdomain; |
3e0c3ff3 | 843 | |
fb1fc7ad LR |
844 | /* |
845 | * Follow the driver's regulatory domain, if present, unless a country | |
846 | * IE has been processed or a user wants to help complaince further | |
847 | */ | |
3e0c3ff3 | 848 | if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE && |
24ed1da1 | 849 | last_request->initiator != REGDOM_SET_BY_USER && |
3e0c3ff3 LR |
850 | wiphy->regd) |
851 | regd = wiphy->regd; | |
852 | ||
853 | if (!regd) | |
b2e1b302 LR |
854 | return -EINVAL; |
855 | ||
3e0c3ff3 | 856 | for (i = 0; i < regd->n_reg_rules; i++) { |
b2e1b302 LR |
857 | const struct ieee80211_reg_rule *rr; |
858 | const struct ieee80211_freq_range *fr = NULL; | |
859 | const struct ieee80211_power_rule *pr = NULL; | |
860 | ||
3e0c3ff3 | 861 | rr = ®d->reg_rules[i]; |
b2e1b302 LR |
862 | fr = &rr->freq_range; |
863 | pr = &rr->power_rule; | |
0c7dc45d | 864 | |
fb1fc7ad LR |
865 | /* |
866 | * We only need to know if one frequency rule was | |
0c7dc45d | 867 | * was in center_freq's band, that's enough, so lets |
fb1fc7ad LR |
868 | * not overwrite it once found |
869 | */ | |
0c7dc45d LR |
870 | if (!band_rule_found) |
871 | band_rule_found = freq_in_rule_band(fr, center_freq); | |
872 | ||
b2e1b302 | 873 | max_bandwidth = freq_max_bandwidth(fr, center_freq); |
0c7dc45d | 874 | |
b2e1b302 LR |
875 | if (max_bandwidth && *bandwidth <= max_bandwidth) { |
876 | *reg_rule = rr; | |
877 | *bandwidth = max_bandwidth; | |
8318d78a JB |
878 | break; |
879 | } | |
880 | } | |
881 | ||
0c7dc45d LR |
882 | if (!band_rule_found) |
883 | return -ERANGE; | |
884 | ||
b2e1b302 LR |
885 | return !max_bandwidth; |
886 | } | |
34f57347 | 887 | EXPORT_SYMBOL(freq_reg_info); |
b2e1b302 | 888 | |
34f57347 | 889 | int freq_reg_info(struct wiphy *wiphy, u32 center_freq, u32 *bandwidth, |
1fa25e41 LR |
890 | const struct ieee80211_reg_rule **reg_rule) |
891 | { | |
892 | return freq_reg_info_regd(wiphy, center_freq, | |
893 | bandwidth, reg_rule, NULL); | |
894 | } | |
b2e1b302 | 895 | |
a92a3ce7 LR |
896 | static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band, |
897 | unsigned int chan_idx) | |
b2e1b302 LR |
898 | { |
899 | int r; | |
a92a3ce7 | 900 | u32 flags; |
b2e1b302 LR |
901 | u32 max_bandwidth = 0; |
902 | const struct ieee80211_reg_rule *reg_rule = NULL; | |
903 | const struct ieee80211_power_rule *power_rule = NULL; | |
a92a3ce7 LR |
904 | struct ieee80211_supported_band *sband; |
905 | struct ieee80211_channel *chan; | |
fe33eb39 | 906 | struct wiphy *request_wiphy = NULL; |
a92a3ce7 | 907 | |
761cf7ec LR |
908 | assert_cfg80211_lock(); |
909 | ||
806a9e39 LR |
910 | request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
911 | ||
a92a3ce7 LR |
912 | sband = wiphy->bands[band]; |
913 | BUG_ON(chan_idx >= sband->n_channels); | |
914 | chan = &sband->channels[chan_idx]; | |
915 | ||
916 | flags = chan->orig_flags; | |
b2e1b302 | 917 | |
3e0c3ff3 | 918 | r = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq), |
b2e1b302 LR |
919 | &max_bandwidth, ®_rule); |
920 | ||
921 | if (r) { | |
fb1fc7ad LR |
922 | /* |
923 | * This means no regulatory rule was found in the country IE | |
0c7dc45d LR |
924 | * with a frequency range on the center_freq's band, since |
925 | * IEEE-802.11 allows for a country IE to have a subset of the | |
926 | * regulatory information provided in a country we ignore | |
927 | * disabling the channel unless at least one reg rule was | |
928 | * found on the center_freq's band. For details see this | |
929 | * clarification: | |
930 | * | |
931 | * http://tinyurl.com/11d-clarification | |
932 | */ | |
933 | if (r == -ERANGE && | |
934 | last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { | |
935 | #ifdef CONFIG_CFG80211_REG_DEBUG | |
936 | printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz " | |
937 | "intact on %s - no rule found in band on " | |
938 | "Country IE\n", | |
939 | chan->center_freq, wiphy_name(wiphy)); | |
940 | #endif | |
941 | } else { | |
fb1fc7ad LR |
942 | /* |
943 | * In this case we know the country IE has at least one reg rule | |
944 | * for the band so we respect its band definitions | |
945 | */ | |
0c7dc45d LR |
946 | #ifdef CONFIG_CFG80211_REG_DEBUG |
947 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) | |
948 | printk(KERN_DEBUG "cfg80211: Disabling " | |
949 | "channel %d MHz on %s due to " | |
950 | "Country IE\n", | |
951 | chan->center_freq, wiphy_name(wiphy)); | |
952 | #endif | |
953 | flags |= IEEE80211_CHAN_DISABLED; | |
954 | chan->flags = flags; | |
955 | } | |
8318d78a JB |
956 | return; |
957 | } | |
958 | ||
b2e1b302 LR |
959 | power_rule = ®_rule->power_rule; |
960 | ||
f976376d | 961 | if (last_request->initiator == REGDOM_SET_BY_DRIVER && |
806a9e39 LR |
962 | request_wiphy && request_wiphy == wiphy && |
963 | request_wiphy->strict_regulatory) { | |
fb1fc7ad LR |
964 | /* |
965 | * This gaurantees the driver's requested regulatory domain | |
f976376d | 966 | * will always be used as a base for further regulatory |
fb1fc7ad LR |
967 | * settings |
968 | */ | |
f976376d LR |
969 | chan->flags = chan->orig_flags = |
970 | map_regdom_flags(reg_rule->flags); | |
971 | chan->max_antenna_gain = chan->orig_mag = | |
972 | (int) MBI_TO_DBI(power_rule->max_antenna_gain); | |
973 | chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth); | |
974 | chan->max_power = chan->orig_mpwr = | |
975 | (int) MBM_TO_DBM(power_rule->max_eirp); | |
976 | return; | |
977 | } | |
978 | ||
b2e1b302 | 979 | chan->flags = flags | map_regdom_flags(reg_rule->flags); |
8318d78a | 980 | chan->max_antenna_gain = min(chan->orig_mag, |
b2e1b302 LR |
981 | (int) MBI_TO_DBI(power_rule->max_antenna_gain)); |
982 | chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth); | |
253898c4 | 983 | if (chan->orig_mpwr) |
b2e1b302 LR |
984 | chan->max_power = min(chan->orig_mpwr, |
985 | (int) MBM_TO_DBM(power_rule->max_eirp)); | |
253898c4 | 986 | else |
b2e1b302 | 987 | chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); |
8318d78a JB |
988 | } |
989 | ||
a92a3ce7 | 990 | static void handle_band(struct wiphy *wiphy, enum ieee80211_band band) |
8318d78a | 991 | { |
a92a3ce7 LR |
992 | unsigned int i; |
993 | struct ieee80211_supported_band *sband; | |
994 | ||
995 | BUG_ON(!wiphy->bands[band]); | |
996 | sband = wiphy->bands[band]; | |
8318d78a JB |
997 | |
998 | for (i = 0; i < sband->n_channels; i++) | |
a92a3ce7 | 999 | handle_channel(wiphy, band, i); |
8318d78a JB |
1000 | } |
1001 | ||
14b9815a LR |
1002 | static bool ignore_reg_update(struct wiphy *wiphy, enum reg_set_by setby) |
1003 | { | |
1004 | if (!last_request) | |
1005 | return true; | |
1006 | if (setby == REGDOM_SET_BY_CORE && | |
2a44f911 | 1007 | wiphy->custom_regulatory) |
14b9815a | 1008 | return true; |
fb1fc7ad LR |
1009 | /* |
1010 | * wiphy->regd will be set once the device has its own | |
1011 | * desired regulatory domain set | |
1012 | */ | |
f976376d LR |
1013 | if (wiphy->strict_regulatory && !wiphy->regd && |
1014 | !is_world_regdom(last_request->alpha2)) | |
14b9815a LR |
1015 | return true; |
1016 | return false; | |
1017 | } | |
1018 | ||
b2e1b302 | 1019 | static void update_all_wiphy_regulatory(enum reg_set_by setby) |
8318d78a | 1020 | { |
b2e1b302 | 1021 | struct cfg80211_registered_device *drv; |
8318d78a | 1022 | |
b2e1b302 | 1023 | list_for_each_entry(drv, &cfg80211_drv_list, list) |
d46e5b1d | 1024 | wiphy_update_regulatory(&drv->wiphy, setby); |
b2e1b302 LR |
1025 | } |
1026 | ||
e38f8a7a LR |
1027 | static void handle_reg_beacon(struct wiphy *wiphy, |
1028 | unsigned int chan_idx, | |
1029 | struct reg_beacon *reg_beacon) | |
1030 | { | |
1031 | #ifdef CONFIG_CFG80211_REG_DEBUG | |
1032 | #define REG_DEBUG_BEACON_FLAG(desc) \ | |
1033 | printk(KERN_DEBUG "cfg80211: Enabling " desc " on " \ | |
1034 | "frequency: %d MHz (Ch %d) on %s\n", \ | |
1035 | reg_beacon->chan.center_freq, \ | |
1036 | ieee80211_frequency_to_channel(reg_beacon->chan.center_freq), \ | |
1037 | wiphy_name(wiphy)); | |
1038 | #else | |
1039 | #define REG_DEBUG_BEACON_FLAG(desc) do {} while (0) | |
1040 | #endif | |
1041 | struct ieee80211_supported_band *sband; | |
1042 | struct ieee80211_channel *chan; | |
1043 | ||
1044 | assert_cfg80211_lock(); | |
1045 | ||
1046 | sband = wiphy->bands[reg_beacon->chan.band]; | |
1047 | chan = &sband->channels[chan_idx]; | |
1048 | ||
1049 | if (likely(chan->center_freq != reg_beacon->chan.center_freq)) | |
1050 | return; | |
1051 | ||
1052 | if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) { | |
1053 | chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN; | |
1054 | REG_DEBUG_BEACON_FLAG("active scanning"); | |
1055 | } | |
1056 | ||
1057 | if (chan->flags & IEEE80211_CHAN_NO_IBSS) { | |
1058 | chan->flags &= ~IEEE80211_CHAN_NO_IBSS; | |
1059 | REG_DEBUG_BEACON_FLAG("beaconing"); | |
1060 | } | |
1061 | ||
1062 | chan->beacon_found = true; | |
1063 | #undef REG_DEBUG_BEACON_FLAG | |
1064 | } | |
1065 | ||
1066 | /* | |
1067 | * Called when a scan on a wiphy finds a beacon on | |
1068 | * new channel | |
1069 | */ | |
1070 | static void wiphy_update_new_beacon(struct wiphy *wiphy, | |
1071 | struct reg_beacon *reg_beacon) | |
1072 | { | |
1073 | unsigned int i; | |
1074 | struct ieee80211_supported_band *sband; | |
1075 | ||
1076 | assert_cfg80211_lock(); | |
1077 | ||
1078 | if (!wiphy->bands[reg_beacon->chan.band]) | |
1079 | return; | |
1080 | ||
1081 | sband = wiphy->bands[reg_beacon->chan.band]; | |
1082 | ||
1083 | for (i = 0; i < sband->n_channels; i++) | |
1084 | handle_reg_beacon(wiphy, i, reg_beacon); | |
1085 | } | |
1086 | ||
1087 | /* | |
1088 | * Called upon reg changes or a new wiphy is added | |
1089 | */ | |
1090 | static void wiphy_update_beacon_reg(struct wiphy *wiphy) | |
1091 | { | |
1092 | unsigned int i; | |
1093 | struct ieee80211_supported_band *sband; | |
1094 | struct reg_beacon *reg_beacon; | |
1095 | ||
1096 | assert_cfg80211_lock(); | |
1097 | ||
1098 | if (list_empty(®_beacon_list)) | |
1099 | return; | |
1100 | ||
1101 | list_for_each_entry(reg_beacon, ®_beacon_list, list) { | |
1102 | if (!wiphy->bands[reg_beacon->chan.band]) | |
1103 | continue; | |
1104 | sband = wiphy->bands[reg_beacon->chan.band]; | |
1105 | for (i = 0; i < sband->n_channels; i++) | |
1106 | handle_reg_beacon(wiphy, i, reg_beacon); | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | static bool reg_is_world_roaming(struct wiphy *wiphy) | |
1111 | { | |
1112 | if (is_world_regdom(cfg80211_regdomain->alpha2) || | |
1113 | (wiphy->regd && is_world_regdom(wiphy->regd->alpha2))) | |
1114 | return true; | |
1115 | if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE && | |
1116 | wiphy->custom_regulatory) | |
1117 | return true; | |
1118 | return false; | |
1119 | } | |
1120 | ||
1121 | /* Reap the advantages of previously found beacons */ | |
1122 | static void reg_process_beacons(struct wiphy *wiphy) | |
1123 | { | |
1124 | if (!reg_is_world_roaming(wiphy)) | |
1125 | return; | |
1126 | wiphy_update_beacon_reg(wiphy); | |
1127 | } | |
1128 | ||
b2e1b302 LR |
1129 | void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby) |
1130 | { | |
1131 | enum ieee80211_band band; | |
d46e5b1d LR |
1132 | |
1133 | if (ignore_reg_update(wiphy, setby)) | |
e38f8a7a | 1134 | goto out; |
b2e1b302 | 1135 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
8318d78a | 1136 | if (wiphy->bands[band]) |
a92a3ce7 | 1137 | handle_band(wiphy, band); |
b2e1b302 | 1138 | } |
e38f8a7a LR |
1139 | out: |
1140 | reg_process_beacons(wiphy); | |
560e28e1 | 1141 | if (wiphy->reg_notifier) |
716f9392 | 1142 | wiphy->reg_notifier(wiphy, last_request); |
b2e1b302 LR |
1143 | } |
1144 | ||
1fa25e41 LR |
1145 | static void handle_channel_custom(struct wiphy *wiphy, |
1146 | enum ieee80211_band band, | |
1147 | unsigned int chan_idx, | |
1148 | const struct ieee80211_regdomain *regd) | |
1149 | { | |
1150 | int r; | |
1151 | u32 max_bandwidth = 0; | |
1152 | const struct ieee80211_reg_rule *reg_rule = NULL; | |
1153 | const struct ieee80211_power_rule *power_rule = NULL; | |
1154 | struct ieee80211_supported_band *sband; | |
1155 | struct ieee80211_channel *chan; | |
1156 | ||
1157 | sband = wiphy->bands[band]; | |
1158 | BUG_ON(chan_idx >= sband->n_channels); | |
1159 | chan = &sband->channels[chan_idx]; | |
1160 | ||
1161 | r = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq), | |
1162 | &max_bandwidth, ®_rule, regd); | |
1163 | ||
1164 | if (r) { | |
1165 | chan->flags = IEEE80211_CHAN_DISABLED; | |
1166 | return; | |
1167 | } | |
1168 | ||
1169 | power_rule = ®_rule->power_rule; | |
1170 | ||
1171 | chan->flags |= map_regdom_flags(reg_rule->flags); | |
1172 | chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain); | |
1173 | chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth); | |
1174 | chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); | |
1175 | } | |
1176 | ||
1177 | static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band, | |
1178 | const struct ieee80211_regdomain *regd) | |
1179 | { | |
1180 | unsigned int i; | |
1181 | struct ieee80211_supported_band *sband; | |
1182 | ||
1183 | BUG_ON(!wiphy->bands[band]); | |
1184 | sband = wiphy->bands[band]; | |
1185 | ||
1186 | for (i = 0; i < sband->n_channels; i++) | |
1187 | handle_channel_custom(wiphy, band, i, regd); | |
1188 | } | |
1189 | ||
1190 | /* Used by drivers prior to wiphy registration */ | |
1191 | void wiphy_apply_custom_regulatory(struct wiphy *wiphy, | |
1192 | const struct ieee80211_regdomain *regd) | |
1193 | { | |
1194 | enum ieee80211_band band; | |
1195 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
1196 | if (wiphy->bands[band]) | |
1197 | handle_band_custom(wiphy, band, regd); | |
b2e1b302 LR |
1198 | } |
1199 | } | |
1fa25e41 LR |
1200 | EXPORT_SYMBOL(wiphy_apply_custom_regulatory); |
1201 | ||
3e0c3ff3 LR |
1202 | static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd, |
1203 | const struct ieee80211_regdomain *src_regd) | |
1204 | { | |
1205 | struct ieee80211_regdomain *regd; | |
1206 | int size_of_regd = 0; | |
1207 | unsigned int i; | |
1208 | ||
1209 | size_of_regd = sizeof(struct ieee80211_regdomain) + | |
1210 | ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule)); | |
1211 | ||
1212 | regd = kzalloc(size_of_regd, GFP_KERNEL); | |
1213 | if (!regd) | |
1214 | return -ENOMEM; | |
1215 | ||
1216 | memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain)); | |
1217 | ||
1218 | for (i = 0; i < src_regd->n_reg_rules; i++) | |
1219 | memcpy(®d->reg_rules[i], &src_regd->reg_rules[i], | |
1220 | sizeof(struct ieee80211_reg_rule)); | |
1221 | ||
1222 | *dst_regd = regd; | |
1223 | return 0; | |
1224 | } | |
b2e1b302 | 1225 | |
fb1fc7ad LR |
1226 | /* |
1227 | * Return value which can be used by ignore_request() to indicate | |
1228 | * it has been determined we should intersect two regulatory domains | |
1229 | */ | |
9c96477d LR |
1230 | #define REG_INTERSECT 1 |
1231 | ||
84fa4f43 JB |
1232 | /* This has the logic which determines when a new request |
1233 | * should be ignored. */ | |
1234 | static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by, | |
1235 | const char *alpha2) | |
1236 | { | |
806a9e39 | 1237 | struct wiphy *last_wiphy = NULL; |
761cf7ec LR |
1238 | |
1239 | assert_cfg80211_lock(); | |
1240 | ||
84fa4f43 JB |
1241 | /* All initial requests are respected */ |
1242 | if (!last_request) | |
1243 | return 0; | |
1244 | ||
1245 | switch (set_by) { | |
1246 | case REGDOM_SET_BY_INIT: | |
1247 | return -EINVAL; | |
1248 | case REGDOM_SET_BY_CORE: | |
ba25c141 | 1249 | return -EINVAL; |
84fa4f43 | 1250 | case REGDOM_SET_BY_COUNTRY_IE: |
806a9e39 LR |
1251 | |
1252 | last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); | |
1253 | ||
84fa4f43 JB |
1254 | if (unlikely(!is_an_alpha2(alpha2))) |
1255 | return -EINVAL; | |
1256 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { | |
806a9e39 | 1257 | if (last_wiphy != wiphy) { |
84fa4f43 JB |
1258 | /* |
1259 | * Two cards with two APs claiming different | |
1260 | * different Country IE alpha2s. We could | |
1261 | * intersect them, but that seems unlikely | |
1262 | * to be correct. Reject second one for now. | |
1263 | */ | |
69b1572b | 1264 | if (regdom_changes(alpha2)) |
84fa4f43 JB |
1265 | return -EOPNOTSUPP; |
1266 | return -EALREADY; | |
1267 | } | |
fb1fc7ad LR |
1268 | /* |
1269 | * Two consecutive Country IE hints on the same wiphy. | |
1270 | * This should be picked up early by the driver/stack | |
1271 | */ | |
69b1572b | 1272 | if (WARN_ON(regdom_changes(alpha2))) |
84fa4f43 JB |
1273 | return 0; |
1274 | return -EALREADY; | |
1275 | } | |
3f2355cb | 1276 | return REG_INTERSECT; |
84fa4f43 | 1277 | case REGDOM_SET_BY_DRIVER: |
e74b1e7f LR |
1278 | if (last_request->initiator == REGDOM_SET_BY_CORE) { |
1279 | if (is_old_static_regdom(cfg80211_regdomain)) | |
1280 | return 0; | |
69b1572b | 1281 | if (regdom_changes(alpha2)) |
e74b1e7f | 1282 | return 0; |
84fa4f43 | 1283 | return -EALREADY; |
e74b1e7f | 1284 | } |
fff32c04 LR |
1285 | |
1286 | /* | |
1287 | * This would happen if you unplug and plug your card | |
1288 | * back in or if you add a new device for which the previously | |
1289 | * loaded card also agrees on the regulatory domain. | |
1290 | */ | |
1291 | if (last_request->initiator == REGDOM_SET_BY_DRIVER && | |
69b1572b | 1292 | !regdom_changes(alpha2)) |
fff32c04 LR |
1293 | return -EALREADY; |
1294 | ||
3e0c3ff3 | 1295 | return REG_INTERSECT; |
84fa4f43 | 1296 | case REGDOM_SET_BY_USER: |
84fa4f43 | 1297 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) |
9c96477d | 1298 | return REG_INTERSECT; |
fb1fc7ad LR |
1299 | /* |
1300 | * If the user knows better the user should set the regdom | |
1301 | * to their country before the IE is picked up | |
1302 | */ | |
3f2355cb LR |
1303 | if (last_request->initiator == REGDOM_SET_BY_USER && |
1304 | last_request->intersect) | |
1305 | return -EOPNOTSUPP; | |
fb1fc7ad LR |
1306 | /* |
1307 | * Process user requests only after previous user/driver/core | |
1308 | * requests have been processed | |
1309 | */ | |
5eebade6 LR |
1310 | if (last_request->initiator == REGDOM_SET_BY_CORE || |
1311 | last_request->initiator == REGDOM_SET_BY_DRIVER || | |
1312 | last_request->initiator == REGDOM_SET_BY_USER) { | |
69b1572b | 1313 | if (regdom_changes(last_request->alpha2)) |
5eebade6 LR |
1314 | return -EAGAIN; |
1315 | } | |
1316 | ||
e74b1e7f | 1317 | if (!is_old_static_regdom(cfg80211_regdomain) && |
69b1572b | 1318 | !regdom_changes(alpha2)) |
e74b1e7f LR |
1319 | return -EALREADY; |
1320 | ||
84fa4f43 JB |
1321 | return 0; |
1322 | } | |
1323 | ||
1324 | return -EINVAL; | |
1325 | } | |
1326 | ||
d1c96a9a LR |
1327 | /** |
1328 | * __regulatory_hint - hint to the wireless core a regulatory domain | |
1329 | * @wiphy: if the hint comes from country information from an AP, this | |
1330 | * is required to be set to the wiphy that received the information | |
28da32d7 | 1331 | * @pending_request: the regulatory request currently being processed |
d1c96a9a LR |
1332 | * |
1333 | * The Wireless subsystem can use this function to hint to the wireless core | |
28da32d7 | 1334 | * what it believes should be the current regulatory domain. |
d1c96a9a LR |
1335 | * |
1336 | * Returns zero if all went fine, %-EALREADY if a regulatory domain had | |
1337 | * already been set or other standard error codes. | |
1338 | * | |
1339 | * Caller must hold &cfg80211_mutex | |
1340 | */ | |
28da32d7 LR |
1341 | static int __regulatory_hint(struct wiphy *wiphy, |
1342 | struct regulatory_request *pending_request) | |
b2e1b302 | 1343 | { |
9c96477d | 1344 | bool intersect = false; |
b2e1b302 LR |
1345 | int r = 0; |
1346 | ||
761cf7ec LR |
1347 | assert_cfg80211_lock(); |
1348 | ||
28da32d7 LR |
1349 | r = ignore_request(wiphy, |
1350 | pending_request->initiator, | |
1351 | pending_request->alpha2); | |
9c96477d | 1352 | |
3e0c3ff3 | 1353 | if (r == REG_INTERSECT) { |
28da32d7 | 1354 | if (pending_request->initiator == REGDOM_SET_BY_DRIVER) { |
3e0c3ff3 | 1355 | r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain); |
d951c1dd LR |
1356 | if (r) { |
1357 | kfree(pending_request); | |
3e0c3ff3 | 1358 | return r; |
d951c1dd | 1359 | } |
3e0c3ff3 | 1360 | } |
9c96477d | 1361 | intersect = true; |
3e0c3ff3 | 1362 | } else if (r) { |
fb1fc7ad LR |
1363 | /* |
1364 | * If the regulatory domain being requested by the | |
3e0c3ff3 | 1365 | * driver has already been set just copy it to the |
fb1fc7ad LR |
1366 | * wiphy |
1367 | */ | |
28da32d7 LR |
1368 | if (r == -EALREADY && |
1369 | pending_request->initiator == REGDOM_SET_BY_DRIVER) { | |
3e0c3ff3 | 1370 | r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain); |
d951c1dd LR |
1371 | if (r) { |
1372 | kfree(pending_request); | |
3e0c3ff3 | 1373 | return r; |
d951c1dd | 1374 | } |
3e0c3ff3 LR |
1375 | r = -EALREADY; |
1376 | goto new_request; | |
1377 | } | |
d951c1dd | 1378 | kfree(pending_request); |
b2e1b302 | 1379 | return r; |
3e0c3ff3 | 1380 | } |
b2e1b302 | 1381 | |
3e0c3ff3 | 1382 | new_request: |
d951c1dd | 1383 | kfree(last_request); |
5203cdb6 | 1384 | |
d951c1dd LR |
1385 | last_request = pending_request; |
1386 | last_request->intersect = intersect; | |
5203cdb6 | 1387 | |
d951c1dd | 1388 | pending_request = NULL; |
3e0c3ff3 LR |
1389 | |
1390 | /* When r == REG_INTERSECT we do need to call CRDA */ | |
1391 | if (r < 0) | |
1392 | return r; | |
1393 | ||
3f2355cb LR |
1394 | /* |
1395 | * Note: When CONFIG_WIRELESS_OLD_REGULATORY is enabled | |
1396 | * AND if CRDA is NOT present nothing will happen, if someone | |
1397 | * wants to bother with 11d with OLD_REG you can add a timer. | |
1398 | * If after x amount of time nothing happens you can call: | |
1399 | * | |
1400 | * return set_regdom(country_ie_regdomain); | |
1401 | * | |
1402 | * to intersect with the static rd | |
1403 | */ | |
d951c1dd | 1404 | return call_crda(last_request->alpha2); |
b2e1b302 LR |
1405 | } |
1406 | ||
fe33eb39 | 1407 | /* This currently only processes user and driver regulatory hints */ |
d951c1dd | 1408 | static void reg_process_hint(struct regulatory_request *reg_request) |
fe33eb39 LR |
1409 | { |
1410 | int r = 0; | |
1411 | struct wiphy *wiphy = NULL; | |
1412 | ||
1413 | BUG_ON(!reg_request->alpha2); | |
1414 | ||
1415 | mutex_lock(&cfg80211_mutex); | |
1416 | ||
1417 | if (wiphy_idx_valid(reg_request->wiphy_idx)) | |
1418 | wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx); | |
1419 | ||
1420 | if (reg_request->initiator == REGDOM_SET_BY_DRIVER && | |
1421 | !wiphy) { | |
d951c1dd | 1422 | kfree(reg_request); |
fe33eb39 LR |
1423 | goto out; |
1424 | } | |
1425 | ||
28da32d7 | 1426 | r = __regulatory_hint(wiphy, reg_request); |
fe33eb39 LR |
1427 | /* This is required so that the orig_* parameters are saved */ |
1428 | if (r == -EALREADY && wiphy && wiphy->strict_regulatory) | |
1429 | wiphy_update_regulatory(wiphy, reg_request->initiator); | |
1430 | out: | |
1431 | mutex_unlock(&cfg80211_mutex); | |
fe33eb39 LR |
1432 | } |
1433 | ||
e38f8a7a | 1434 | /* Processes regulatory hints, this is all the REGDOM_SET_BY_* */ |
fe33eb39 LR |
1435 | static void reg_process_pending_hints(void) |
1436 | { | |
1437 | struct regulatory_request *reg_request; | |
fe33eb39 LR |
1438 | |
1439 | spin_lock(®_requests_lock); | |
1440 | while (!list_empty(®_requests_list)) { | |
1441 | reg_request = list_first_entry(®_requests_list, | |
1442 | struct regulatory_request, | |
1443 | list); | |
1444 | list_del_init(®_request->list); | |
fe33eb39 | 1445 | |
d951c1dd LR |
1446 | spin_unlock(®_requests_lock); |
1447 | reg_process_hint(reg_request); | |
fe33eb39 LR |
1448 | spin_lock(®_requests_lock); |
1449 | } | |
1450 | spin_unlock(®_requests_lock); | |
1451 | } | |
1452 | ||
e38f8a7a LR |
1453 | /* Processes beacon hints -- this has nothing to do with country IEs */ |
1454 | static void reg_process_pending_beacon_hints(void) | |
1455 | { | |
1456 | struct cfg80211_registered_device *drv; | |
1457 | struct reg_beacon *pending_beacon, *tmp; | |
1458 | ||
1459 | mutex_lock(&cfg80211_mutex); | |
1460 | ||
1461 | /* This goes through the _pending_ beacon list */ | |
1462 | spin_lock_bh(®_pending_beacons_lock); | |
1463 | ||
1464 | if (list_empty(®_pending_beacons)) { | |
1465 | spin_unlock_bh(®_pending_beacons_lock); | |
1466 | goto out; | |
1467 | } | |
1468 | ||
1469 | list_for_each_entry_safe(pending_beacon, tmp, | |
1470 | ®_pending_beacons, list) { | |
1471 | ||
1472 | list_del_init(&pending_beacon->list); | |
1473 | ||
1474 | /* Applies the beacon hint to current wiphys */ | |
1475 | list_for_each_entry(drv, &cfg80211_drv_list, list) | |
1476 | wiphy_update_new_beacon(&drv->wiphy, pending_beacon); | |
1477 | ||
1478 | /* Remembers the beacon hint for new wiphys or reg changes */ | |
1479 | list_add_tail(&pending_beacon->list, ®_beacon_list); | |
1480 | } | |
1481 | ||
1482 | spin_unlock_bh(®_pending_beacons_lock); | |
1483 | out: | |
1484 | mutex_unlock(&cfg80211_mutex); | |
1485 | } | |
1486 | ||
fe33eb39 LR |
1487 | static void reg_todo(struct work_struct *work) |
1488 | { | |
1489 | reg_process_pending_hints(); | |
e38f8a7a | 1490 | reg_process_pending_beacon_hints(); |
fe33eb39 LR |
1491 | } |
1492 | ||
1493 | static DECLARE_WORK(reg_work, reg_todo); | |
1494 | ||
1495 | static void queue_regulatory_request(struct regulatory_request *request) | |
1496 | { | |
1497 | spin_lock(®_requests_lock); | |
1498 | list_add_tail(&request->list, ®_requests_list); | |
1499 | spin_unlock(®_requests_lock); | |
1500 | ||
1501 | schedule_work(®_work); | |
1502 | } | |
1503 | ||
1504 | /* Core regulatory hint -- happens once during cfg80211_init() */ | |
ba25c141 LR |
1505 | static int regulatory_hint_core(const char *alpha2) |
1506 | { | |
1507 | struct regulatory_request *request; | |
1508 | ||
1509 | BUG_ON(last_request); | |
1510 | ||
1511 | request = kzalloc(sizeof(struct regulatory_request), | |
1512 | GFP_KERNEL); | |
1513 | if (!request) | |
1514 | return -ENOMEM; | |
1515 | ||
1516 | request->alpha2[0] = alpha2[0]; | |
1517 | request->alpha2[1] = alpha2[1]; | |
1518 | request->initiator = REGDOM_SET_BY_CORE; | |
1519 | ||
fe33eb39 | 1520 | queue_regulatory_request(request); |
ba25c141 | 1521 | |
fe33eb39 | 1522 | return 0; |
ba25c141 LR |
1523 | } |
1524 | ||
fe33eb39 LR |
1525 | /* User hints */ |
1526 | int regulatory_hint_user(const char *alpha2) | |
b2e1b302 | 1527 | { |
fe33eb39 LR |
1528 | struct regulatory_request *request; |
1529 | ||
be3d4810 | 1530 | BUG_ON(!alpha2); |
b2e1b302 | 1531 | |
fe33eb39 LR |
1532 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
1533 | if (!request) | |
1534 | return -ENOMEM; | |
1535 | ||
1536 | request->wiphy_idx = WIPHY_IDX_STALE; | |
1537 | request->alpha2[0] = alpha2[0]; | |
1538 | request->alpha2[1] = alpha2[1]; | |
1539 | request->initiator = REGDOM_SET_BY_USER, | |
1540 | ||
1541 | queue_regulatory_request(request); | |
1542 | ||
1543 | return 0; | |
1544 | } | |
1545 | ||
1546 | /* Driver hints */ | |
1547 | int regulatory_hint(struct wiphy *wiphy, const char *alpha2) | |
1548 | { | |
1549 | struct regulatory_request *request; | |
1550 | ||
1551 | BUG_ON(!alpha2); | |
1552 | BUG_ON(!wiphy); | |
1553 | ||
1554 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); | |
1555 | if (!request) | |
1556 | return -ENOMEM; | |
1557 | ||
1558 | request->wiphy_idx = get_wiphy_idx(wiphy); | |
1559 | ||
1560 | /* Must have registered wiphy first */ | |
1561 | BUG_ON(!wiphy_idx_valid(request->wiphy_idx)); | |
1562 | ||
1563 | request->alpha2[0] = alpha2[0]; | |
1564 | request->alpha2[1] = alpha2[1]; | |
1565 | request->initiator = REGDOM_SET_BY_DRIVER; | |
1566 | ||
1567 | queue_regulatory_request(request); | |
1568 | ||
1569 | return 0; | |
b2e1b302 LR |
1570 | } |
1571 | EXPORT_SYMBOL(regulatory_hint); | |
1572 | ||
3f2355cb LR |
1573 | static bool reg_same_country_ie_hint(struct wiphy *wiphy, |
1574 | u32 country_ie_checksum) | |
1575 | { | |
806a9e39 LR |
1576 | struct wiphy *request_wiphy; |
1577 | ||
761cf7ec LR |
1578 | assert_cfg80211_lock(); |
1579 | ||
806a9e39 LR |
1580 | request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
1581 | ||
1582 | if (!request_wiphy) | |
3f2355cb | 1583 | return false; |
806a9e39 LR |
1584 | |
1585 | if (likely(request_wiphy != wiphy)) | |
3f2355cb | 1586 | return !country_ie_integrity_changes(country_ie_checksum); |
fb1fc7ad LR |
1587 | /* |
1588 | * We should not have let these through at this point, they | |
3f2355cb | 1589 | * should have been picked up earlier by the first alpha2 check |
fb1fc7ad LR |
1590 | * on the device |
1591 | */ | |
3f2355cb LR |
1592 | if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum))) |
1593 | return true; | |
1594 | return false; | |
1595 | } | |
1596 | ||
1597 | void regulatory_hint_11d(struct wiphy *wiphy, | |
1598 | u8 *country_ie, | |
1599 | u8 country_ie_len) | |
1600 | { | |
1601 | struct ieee80211_regdomain *rd = NULL; | |
1602 | char alpha2[2]; | |
1603 | u32 checksum = 0; | |
1604 | enum environment_cap env = ENVIRON_ANY; | |
fe33eb39 | 1605 | struct regulatory_request *request; |
3f2355cb | 1606 | |
a1794390 | 1607 | mutex_lock(&cfg80211_mutex); |
3f2355cb | 1608 | |
d335fe63 LR |
1609 | if (unlikely(!last_request)) { |
1610 | mutex_unlock(&cfg80211_mutex); | |
1611 | return; | |
1612 | } | |
1613 | ||
3f2355cb LR |
1614 | /* IE len must be evenly divisible by 2 */ |
1615 | if (country_ie_len & 0x01) | |
1616 | goto out; | |
1617 | ||
1618 | if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) | |
1619 | goto out; | |
1620 | ||
fb1fc7ad LR |
1621 | /* |
1622 | * Pending country IE processing, this can happen after we | |
3f2355cb | 1623 | * call CRDA and wait for a response if a beacon was received before |
fb1fc7ad LR |
1624 | * we were able to process the last regulatory_hint_11d() call |
1625 | */ | |
3f2355cb LR |
1626 | if (country_ie_regdomain) |
1627 | goto out; | |
1628 | ||
1629 | alpha2[0] = country_ie[0]; | |
1630 | alpha2[1] = country_ie[1]; | |
1631 | ||
1632 | if (country_ie[2] == 'I') | |
1633 | env = ENVIRON_INDOOR; | |
1634 | else if (country_ie[2] == 'O') | |
1635 | env = ENVIRON_OUTDOOR; | |
1636 | ||
fb1fc7ad LR |
1637 | /* |
1638 | * We will run this for *every* beacon processed for the BSSID, so | |
3f2355cb | 1639 | * we optimize an early check to exit out early if we don't have to |
fb1fc7ad LR |
1640 | * do anything |
1641 | */ | |
806a9e39 | 1642 | if (likely(wiphy_idx_valid(last_request->wiphy_idx))) { |
3f2355cb LR |
1643 | struct cfg80211_registered_device *drv_last_ie; |
1644 | ||
806a9e39 LR |
1645 | drv_last_ie = |
1646 | cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx); | |
3f2355cb | 1647 | |
fb1fc7ad LR |
1648 | /* |
1649 | * Lets keep this simple -- we trust the first AP | |
1650 | * after we intersect with CRDA | |
1651 | */ | |
806a9e39 | 1652 | if (likely(&drv_last_ie->wiphy == wiphy)) { |
fb1fc7ad LR |
1653 | /* |
1654 | * Ignore IEs coming in on this wiphy with | |
1655 | * the same alpha2 and environment cap | |
1656 | */ | |
3f2355cb LR |
1657 | if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2, |
1658 | alpha2) && | |
1659 | env == drv_last_ie->env)) { | |
1660 | goto out; | |
1661 | } | |
fb1fc7ad LR |
1662 | /* |
1663 | * the wiphy moved on to another BSSID or the AP | |
3f2355cb LR |
1664 | * was reconfigured. XXX: We need to deal with the |
1665 | * case where the user suspends and goes to goes | |
1666 | * to another country, and then gets IEs from an | |
fb1fc7ad LR |
1667 | * AP with different settings |
1668 | */ | |
3f2355cb LR |
1669 | goto out; |
1670 | } else { | |
fb1fc7ad LR |
1671 | /* |
1672 | * Ignore IEs coming in on two separate wiphys with | |
1673 | * the same alpha2 and environment cap | |
1674 | */ | |
3f2355cb LR |
1675 | if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2, |
1676 | alpha2) && | |
1677 | env == drv_last_ie->env)) { | |
1678 | goto out; | |
1679 | } | |
1680 | /* We could potentially intersect though */ | |
1681 | goto out; | |
1682 | } | |
1683 | } | |
1684 | ||
1685 | rd = country_ie_2_rd(country_ie, country_ie_len, &checksum); | |
1686 | if (!rd) | |
1687 | goto out; | |
1688 | ||
915278e0 LR |
1689 | /* |
1690 | * This will not happen right now but we leave it here for the | |
3f2355cb LR |
1691 | * the future when we want to add suspend/resume support and having |
1692 | * the user move to another country after doing so, or having the user | |
915278e0 LR |
1693 | * move to another AP. Right now we just trust the first AP. |
1694 | * | |
1695 | * If we hit this before we add this support we want to be informed of | |
1696 | * it as it would indicate a mistake in the current design | |
1697 | */ | |
1698 | if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum))) | |
0441d6ff | 1699 | goto free_rd_out; |
3f2355cb | 1700 | |
fe33eb39 LR |
1701 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
1702 | if (!request) | |
1703 | goto free_rd_out; | |
1704 | ||
fb1fc7ad LR |
1705 | /* |
1706 | * We keep this around for when CRDA comes back with a response so | |
1707 | * we can intersect with that | |
1708 | */ | |
3f2355cb LR |
1709 | country_ie_regdomain = rd; |
1710 | ||
fe33eb39 LR |
1711 | request->wiphy_idx = get_wiphy_idx(wiphy); |
1712 | request->alpha2[0] = rd->alpha2[0]; | |
1713 | request->alpha2[1] = rd->alpha2[1]; | |
1714 | request->initiator = REGDOM_SET_BY_COUNTRY_IE; | |
1715 | request->country_ie_checksum = checksum; | |
1716 | request->country_ie_env = env; | |
1717 | ||
1718 | mutex_unlock(&cfg80211_mutex); | |
3f2355cb | 1719 | |
fe33eb39 LR |
1720 | queue_regulatory_request(request); |
1721 | ||
1722 | return; | |
0441d6ff LR |
1723 | |
1724 | free_rd_out: | |
1725 | kfree(rd); | |
3f2355cb | 1726 | out: |
a1794390 | 1727 | mutex_unlock(&cfg80211_mutex); |
3f2355cb LR |
1728 | } |
1729 | EXPORT_SYMBOL(regulatory_hint_11d); | |
b2e1b302 | 1730 | |
e38f8a7a LR |
1731 | static bool freq_is_chan_12_13_14(u16 freq) |
1732 | { | |
1733 | if (freq == ieee80211_channel_to_frequency(12) || | |
1734 | freq == ieee80211_channel_to_frequency(13) || | |
1735 | freq == ieee80211_channel_to_frequency(14)) | |
1736 | return true; | |
1737 | return false; | |
1738 | } | |
1739 | ||
1740 | int regulatory_hint_found_beacon(struct wiphy *wiphy, | |
1741 | struct ieee80211_channel *beacon_chan, | |
1742 | gfp_t gfp) | |
1743 | { | |
1744 | struct reg_beacon *reg_beacon; | |
1745 | ||
1746 | if (likely((beacon_chan->beacon_found || | |
1747 | (beacon_chan->flags & IEEE80211_CHAN_RADAR) || | |
1748 | (beacon_chan->band == IEEE80211_BAND_2GHZ && | |
1749 | !freq_is_chan_12_13_14(beacon_chan->center_freq))))) | |
1750 | return 0; | |
1751 | ||
1752 | reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp); | |
1753 | if (!reg_beacon) | |
1754 | return -ENOMEM; | |
1755 | ||
1756 | #ifdef CONFIG_CFG80211_REG_DEBUG | |
1757 | printk(KERN_DEBUG "cfg80211: Found new beacon on " | |
1758 | "frequency: %d MHz (Ch %d) on %s\n", | |
1759 | beacon_chan->center_freq, | |
1760 | ieee80211_frequency_to_channel(beacon_chan->center_freq), | |
1761 | wiphy_name(wiphy)); | |
1762 | #endif | |
1763 | memcpy(®_beacon->chan, beacon_chan, | |
1764 | sizeof(struct ieee80211_channel)); | |
1765 | ||
1766 | ||
1767 | /* | |
1768 | * Since we can be called from BH or and non-BH context | |
1769 | * we must use spin_lock_bh() | |
1770 | */ | |
1771 | spin_lock_bh(®_pending_beacons_lock); | |
1772 | list_add_tail(®_beacon->list, ®_pending_beacons); | |
1773 | spin_unlock_bh(®_pending_beacons_lock); | |
1774 | ||
1775 | schedule_work(®_work); | |
1776 | ||
1777 | return 0; | |
1778 | } | |
1779 | ||
a3d2eaf0 | 1780 | static void print_rd_rules(const struct ieee80211_regdomain *rd) |
b2e1b302 LR |
1781 | { |
1782 | unsigned int i; | |
a3d2eaf0 JB |
1783 | const struct ieee80211_reg_rule *reg_rule = NULL; |
1784 | const struct ieee80211_freq_range *freq_range = NULL; | |
1785 | const struct ieee80211_power_rule *power_rule = NULL; | |
b2e1b302 LR |
1786 | |
1787 | printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), " | |
1788 | "(max_antenna_gain, max_eirp)\n"); | |
1789 | ||
1790 | for (i = 0; i < rd->n_reg_rules; i++) { | |
1791 | reg_rule = &rd->reg_rules[i]; | |
1792 | freq_range = ®_rule->freq_range; | |
1793 | power_rule = ®_rule->power_rule; | |
1794 | ||
fb1fc7ad LR |
1795 | /* |
1796 | * There may not be documentation for max antenna gain | |
1797 | * in certain regions | |
1798 | */ | |
b2e1b302 LR |
1799 | if (power_rule->max_antenna_gain) |
1800 | printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), " | |
1801 | "(%d mBi, %d mBm)\n", | |
1802 | freq_range->start_freq_khz, | |
1803 | freq_range->end_freq_khz, | |
1804 | freq_range->max_bandwidth_khz, | |
1805 | power_rule->max_antenna_gain, | |
1806 | power_rule->max_eirp); | |
1807 | else | |
1808 | printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), " | |
1809 | "(N/A, %d mBm)\n", | |
1810 | freq_range->start_freq_khz, | |
1811 | freq_range->end_freq_khz, | |
1812 | freq_range->max_bandwidth_khz, | |
1813 | power_rule->max_eirp); | |
1814 | } | |
1815 | } | |
1816 | ||
a3d2eaf0 | 1817 | static void print_regdomain(const struct ieee80211_regdomain *rd) |
b2e1b302 LR |
1818 | { |
1819 | ||
3f2355cb | 1820 | if (is_intersected_alpha2(rd->alpha2)) { |
3f2355cb LR |
1821 | |
1822 | if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) { | |
806a9e39 LR |
1823 | struct cfg80211_registered_device *drv; |
1824 | drv = cfg80211_drv_by_wiphy_idx( | |
1825 | last_request->wiphy_idx); | |
1826 | if (drv) { | |
3f2355cb LR |
1827 | printk(KERN_INFO "cfg80211: Current regulatory " |
1828 | "domain updated by AP to: %c%c\n", | |
1829 | drv->country_ie_alpha2[0], | |
1830 | drv->country_ie_alpha2[1]); | |
1831 | } else | |
1832 | printk(KERN_INFO "cfg80211: Current regulatory " | |
1833 | "domain intersected: \n"); | |
1834 | } else | |
1835 | printk(KERN_INFO "cfg80211: Current regulatory " | |
039498c6 | 1836 | "domain intersected: \n"); |
3f2355cb | 1837 | } else if (is_world_regdom(rd->alpha2)) |
b2e1b302 LR |
1838 | printk(KERN_INFO "cfg80211: World regulatory " |
1839 | "domain updated:\n"); | |
1840 | else { | |
1841 | if (is_unknown_alpha2(rd->alpha2)) | |
1842 | printk(KERN_INFO "cfg80211: Regulatory domain " | |
1843 | "changed to driver built-in settings " | |
1844 | "(unknown country)\n"); | |
1845 | else | |
1846 | printk(KERN_INFO "cfg80211: Regulatory domain " | |
1847 | "changed to country: %c%c\n", | |
1848 | rd->alpha2[0], rd->alpha2[1]); | |
1849 | } | |
1850 | print_rd_rules(rd); | |
1851 | } | |
1852 | ||
2df78167 | 1853 | static void print_regdomain_info(const struct ieee80211_regdomain *rd) |
b2e1b302 LR |
1854 | { |
1855 | printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n", | |
1856 | rd->alpha2[0], rd->alpha2[1]); | |
1857 | print_rd_rules(rd); | |
1858 | } | |
1859 | ||
3f2355cb LR |
1860 | #ifdef CONFIG_CFG80211_REG_DEBUG |
1861 | static void reg_country_ie_process_debug( | |
1862 | const struct ieee80211_regdomain *rd, | |
1863 | const struct ieee80211_regdomain *country_ie_regdomain, | |
1864 | const struct ieee80211_regdomain *intersected_rd) | |
1865 | { | |
1866 | printk(KERN_DEBUG "cfg80211: Received country IE:\n"); | |
1867 | print_regdomain_info(country_ie_regdomain); | |
1868 | printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n"); | |
1869 | print_regdomain_info(rd); | |
1870 | if (intersected_rd) { | |
1871 | printk(KERN_DEBUG "cfg80211: We intersect both of these " | |
1872 | "and get:\n"); | |
667ecd01 | 1873 | print_regdomain_info(intersected_rd); |
3f2355cb LR |
1874 | return; |
1875 | } | |
1876 | printk(KERN_DEBUG "cfg80211: Intersection between both failed\n"); | |
1877 | } | |
1878 | #else | |
1879 | static inline void reg_country_ie_process_debug( | |
1880 | const struct ieee80211_regdomain *rd, | |
1881 | const struct ieee80211_regdomain *country_ie_regdomain, | |
1882 | const struct ieee80211_regdomain *intersected_rd) | |
1883 | { | |
1884 | } | |
1885 | #endif | |
1886 | ||
d2372b31 | 1887 | /* Takes ownership of rd only if it doesn't fail */ |
a3d2eaf0 | 1888 | static int __set_regdom(const struct ieee80211_regdomain *rd) |
b2e1b302 | 1889 | { |
9c96477d | 1890 | const struct ieee80211_regdomain *intersected_rd = NULL; |
3f2355cb | 1891 | struct cfg80211_registered_device *drv = NULL; |
806a9e39 | 1892 | struct wiphy *request_wiphy; |
b2e1b302 LR |
1893 | /* Some basic sanity checks first */ |
1894 | ||
b2e1b302 | 1895 | if (is_world_regdom(rd->alpha2)) { |
f6037d09 | 1896 | if (WARN_ON(!reg_is_valid_request(rd->alpha2))) |
b2e1b302 LR |
1897 | return -EINVAL; |
1898 | update_world_regdomain(rd); | |
1899 | return 0; | |
1900 | } | |
b2e1b302 LR |
1901 | |
1902 | if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && | |
1903 | !is_unknown_alpha2(rd->alpha2)) | |
1904 | return -EINVAL; | |
1905 | ||
f6037d09 | 1906 | if (!last_request) |
b2e1b302 LR |
1907 | return -EINVAL; |
1908 | ||
fb1fc7ad LR |
1909 | /* |
1910 | * Lets only bother proceeding on the same alpha2 if the current | |
3f2355cb | 1911 | * rd is non static (it means CRDA was present and was used last) |
fb1fc7ad LR |
1912 | * and the pending request came in from a country IE |
1913 | */ | |
3f2355cb | 1914 | if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) { |
fb1fc7ad LR |
1915 | /* |
1916 | * If someone else asked us to change the rd lets only bother | |
1917 | * checking if the alpha2 changes if CRDA was already called | |
1918 | */ | |
3f2355cb | 1919 | if (!is_old_static_regdom(cfg80211_regdomain) && |
69b1572b | 1920 | !regdom_changes(rd->alpha2)) |
3f2355cb LR |
1921 | return -EINVAL; |
1922 | } | |
1923 | ||
fb1fc7ad LR |
1924 | /* |
1925 | * Now lets set the regulatory domain, update all driver channels | |
b2e1b302 LR |
1926 | * and finally inform them of what we have done, in case they want |
1927 | * to review or adjust their own settings based on their own | |
fb1fc7ad LR |
1928 | * internal EEPROM data |
1929 | */ | |
b2e1b302 | 1930 | |
f6037d09 | 1931 | if (WARN_ON(!reg_is_valid_request(rd->alpha2))) |
b2e1b302 LR |
1932 | return -EINVAL; |
1933 | ||
8375af3b LR |
1934 | if (!is_valid_rd(rd)) { |
1935 | printk(KERN_ERR "cfg80211: Invalid " | |
1936 | "regulatory domain detected:\n"); | |
1937 | print_regdomain_info(rd); | |
1938 | return -EINVAL; | |
b2e1b302 LR |
1939 | } |
1940 | ||
806a9e39 LR |
1941 | request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
1942 | ||
b8295acd | 1943 | if (!last_request->intersect) { |
3e0c3ff3 LR |
1944 | int r; |
1945 | ||
1946 | if (last_request->initiator != REGDOM_SET_BY_DRIVER) { | |
1947 | reset_regdomains(); | |
1948 | cfg80211_regdomain = rd; | |
1949 | return 0; | |
1950 | } | |
1951 | ||
fb1fc7ad LR |
1952 | /* |
1953 | * For a driver hint, lets copy the regulatory domain the | |
1954 | * driver wanted to the wiphy to deal with conflicts | |
1955 | */ | |
3e0c3ff3 | 1956 | |
806a9e39 | 1957 | BUG_ON(request_wiphy->regd); |
3e0c3ff3 | 1958 | |
806a9e39 | 1959 | r = reg_copy_regd(&request_wiphy->regd, rd); |
3e0c3ff3 LR |
1960 | if (r) |
1961 | return r; | |
1962 | ||
b8295acd LR |
1963 | reset_regdomains(); |
1964 | cfg80211_regdomain = rd; | |
1965 | return 0; | |
1966 | } | |
1967 | ||
1968 | /* Intersection requires a bit more work */ | |
1969 | ||
1970 | if (last_request->initiator != REGDOM_SET_BY_COUNTRY_IE) { | |
1971 | ||
9c96477d LR |
1972 | intersected_rd = regdom_intersect(rd, cfg80211_regdomain); |
1973 | if (!intersected_rd) | |
1974 | return -EINVAL; | |
b8295acd | 1975 | |
fb1fc7ad LR |
1976 | /* |
1977 | * We can trash what CRDA provided now. | |
3e0c3ff3 | 1978 | * However if a driver requested this specific regulatory |
fb1fc7ad LR |
1979 | * domain we keep it for its private use |
1980 | */ | |
3e0c3ff3 | 1981 | if (last_request->initiator == REGDOM_SET_BY_DRIVER) |
806a9e39 | 1982 | request_wiphy->regd = rd; |
3e0c3ff3 LR |
1983 | else |
1984 | kfree(rd); | |
1985 | ||
b8295acd LR |
1986 | rd = NULL; |
1987 | ||
1988 | reset_regdomains(); | |
1989 | cfg80211_regdomain = intersected_rd; | |
1990 | ||
1991 | return 0; | |
9c96477d LR |
1992 | } |
1993 | ||
3f2355cb LR |
1994 | /* |
1995 | * Country IE requests are handled a bit differently, we intersect | |
1996 | * the country IE rd with what CRDA believes that country should have | |
1997 | */ | |
1998 | ||
1999 | BUG_ON(!country_ie_regdomain); | |
2000 | ||
2001 | if (rd != country_ie_regdomain) { | |
fb1fc7ad LR |
2002 | /* |
2003 | * Intersect what CRDA returned and our what we | |
2004 | * had built from the Country IE received | |
2005 | */ | |
3f2355cb LR |
2006 | |
2007 | intersected_rd = regdom_intersect(rd, country_ie_regdomain); | |
2008 | ||
2009 | reg_country_ie_process_debug(rd, country_ie_regdomain, | |
2010 | intersected_rd); | |
2011 | ||
2012 | kfree(country_ie_regdomain); | |
2013 | country_ie_regdomain = NULL; | |
2014 | } else { | |
fb1fc7ad LR |
2015 | /* |
2016 | * This would happen when CRDA was not present and | |
3f2355cb | 2017 | * OLD_REGULATORY was enabled. We intersect our Country |
fb1fc7ad LR |
2018 | * IE rd and what was set on cfg80211 originally |
2019 | */ | |
3f2355cb LR |
2020 | intersected_rd = regdom_intersect(rd, cfg80211_regdomain); |
2021 | } | |
2022 | ||
2023 | if (!intersected_rd) | |
2024 | return -EINVAL; | |
2025 | ||
806a9e39 | 2026 | drv = wiphy_to_dev(request_wiphy); |
3f2355cb LR |
2027 | |
2028 | drv->country_ie_alpha2[0] = rd->alpha2[0]; | |
2029 | drv->country_ie_alpha2[1] = rd->alpha2[1]; | |
2030 | drv->env = last_request->country_ie_env; | |
2031 | ||
2032 | BUG_ON(intersected_rd == rd); | |
2033 | ||
2034 | kfree(rd); | |
2035 | rd = NULL; | |
2036 | ||
b8295acd | 2037 | reset_regdomains(); |
3f2355cb | 2038 | cfg80211_regdomain = intersected_rd; |
b2e1b302 LR |
2039 | |
2040 | return 0; | |
2041 | } | |
2042 | ||
2043 | ||
fb1fc7ad LR |
2044 | /* |
2045 | * Use this call to set the current regulatory domain. Conflicts with | |
b2e1b302 | 2046 | * multiple drivers can be ironed out later. Caller must've already |
fb1fc7ad LR |
2047 | * kmalloc'd the rd structure. Caller must hold cfg80211_mutex |
2048 | */ | |
a3d2eaf0 | 2049 | int set_regdom(const struct ieee80211_regdomain *rd) |
b2e1b302 | 2050 | { |
b2e1b302 LR |
2051 | int r; |
2052 | ||
761cf7ec LR |
2053 | assert_cfg80211_lock(); |
2054 | ||
b2e1b302 LR |
2055 | /* Note that this doesn't update the wiphys, this is done below */ |
2056 | r = __set_regdom(rd); | |
d2372b31 JB |
2057 | if (r) { |
2058 | kfree(rd); | |
b2e1b302 | 2059 | return r; |
d2372b31 | 2060 | } |
b2e1b302 | 2061 | |
b2e1b302 | 2062 | /* This would make this whole thing pointless */ |
a01ddafd LR |
2063 | if (!last_request->intersect) |
2064 | BUG_ON(rd != cfg80211_regdomain); | |
b2e1b302 LR |
2065 | |
2066 | /* update all wiphys now with the new established regulatory domain */ | |
f6037d09 | 2067 | update_all_wiphy_regulatory(last_request->initiator); |
b2e1b302 | 2068 | |
a01ddafd | 2069 | print_regdomain(cfg80211_regdomain); |
b2e1b302 LR |
2070 | |
2071 | return r; | |
2072 | } | |
2073 | ||
a1794390 | 2074 | /* Caller must hold cfg80211_mutex */ |
3f2355cb LR |
2075 | void reg_device_remove(struct wiphy *wiphy) |
2076 | { | |
806a9e39 LR |
2077 | struct wiphy *request_wiphy; |
2078 | ||
761cf7ec LR |
2079 | assert_cfg80211_lock(); |
2080 | ||
806a9e39 LR |
2081 | request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
2082 | ||
3e0c3ff3 | 2083 | kfree(wiphy->regd); |
806a9e39 | 2084 | if (!last_request || !request_wiphy) |
3f2355cb | 2085 | return; |
806a9e39 | 2086 | if (request_wiphy != wiphy) |
3f2355cb | 2087 | return; |
806a9e39 | 2088 | last_request->wiphy_idx = WIPHY_IDX_STALE; |
3f2355cb LR |
2089 | last_request->country_ie_env = ENVIRON_ANY; |
2090 | } | |
2091 | ||
b2e1b302 LR |
2092 | int regulatory_init(void) |
2093 | { | |
bcf4f99b | 2094 | int err = 0; |
734366de | 2095 | |
b2e1b302 LR |
2096 | reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0); |
2097 | if (IS_ERR(reg_pdev)) | |
2098 | return PTR_ERR(reg_pdev); | |
734366de | 2099 | |
fe33eb39 | 2100 | spin_lock_init(®_requests_lock); |
e38f8a7a | 2101 | spin_lock_init(®_pending_beacons_lock); |
fe33eb39 | 2102 | |
734366de | 2103 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY |
a3d2eaf0 | 2104 | cfg80211_regdomain = static_regdom(ieee80211_regdom); |
734366de | 2105 | |
942b25cf | 2106 | printk(KERN_INFO "cfg80211: Using static regulatory domain info\n"); |
734366de | 2107 | print_regdomain_info(cfg80211_regdomain); |
fb1fc7ad LR |
2108 | /* |
2109 | * The old code still requests for a new regdomain and if | |
734366de JB |
2110 | * you have CRDA you get it updated, otherwise you get |
2111 | * stuck with the static values. We ignore "EU" code as | |
fb1fc7ad LR |
2112 | * that is not a valid ISO / IEC 3166 alpha2 |
2113 | */ | |
ac9440a4 | 2114 | if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U') |
ba25c141 | 2115 | err = regulatory_hint_core(ieee80211_regdom); |
734366de | 2116 | #else |
a3d2eaf0 | 2117 | cfg80211_regdomain = cfg80211_world_regdom; |
734366de | 2118 | |
ba25c141 | 2119 | err = regulatory_hint_core("00"); |
bcf4f99b | 2120 | #endif |
ba25c141 | 2121 | if (err) { |
bcf4f99b LR |
2122 | if (err == -ENOMEM) |
2123 | return err; | |
2124 | /* | |
2125 | * N.B. kobject_uevent_env() can fail mainly for when we're out | |
2126 | * memory which is handled and propagated appropriately above | |
2127 | * but it can also fail during a netlink_broadcast() or during | |
2128 | * early boot for call_usermodehelper(). For now treat these | |
2129 | * errors as non-fatal. | |
2130 | */ | |
2131 | printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable " | |
2132 | "to call CRDA during init"); | |
2133 | #ifdef CONFIG_CFG80211_REG_DEBUG | |
2134 | /* We want to find out exactly why when debugging */ | |
2135 | WARN_ON(err); | |
734366de | 2136 | #endif |
bcf4f99b | 2137 | } |
734366de | 2138 | |
b2e1b302 LR |
2139 | return 0; |
2140 | } | |
2141 | ||
2142 | void regulatory_exit(void) | |
2143 | { | |
fe33eb39 | 2144 | struct regulatory_request *reg_request, *tmp; |
e38f8a7a | 2145 | struct reg_beacon *reg_beacon, *btmp; |
fe33eb39 LR |
2146 | |
2147 | cancel_work_sync(®_work); | |
2148 | ||
a1794390 | 2149 | mutex_lock(&cfg80211_mutex); |
734366de | 2150 | |
b2e1b302 | 2151 | reset_regdomains(); |
734366de | 2152 | |
3f2355cb LR |
2153 | kfree(country_ie_regdomain); |
2154 | country_ie_regdomain = NULL; | |
2155 | ||
f6037d09 JB |
2156 | kfree(last_request); |
2157 | ||
b2e1b302 | 2158 | platform_device_unregister(reg_pdev); |
734366de | 2159 | |
e38f8a7a LR |
2160 | spin_lock_bh(®_pending_beacons_lock); |
2161 | if (!list_empty(®_pending_beacons)) { | |
2162 | list_for_each_entry_safe(reg_beacon, btmp, | |
2163 | ®_pending_beacons, list) { | |
2164 | list_del(®_beacon->list); | |
2165 | kfree(reg_beacon); | |
2166 | } | |
2167 | } | |
2168 | spin_unlock_bh(®_pending_beacons_lock); | |
2169 | ||
2170 | if (!list_empty(®_beacon_list)) { | |
2171 | list_for_each_entry_safe(reg_beacon, btmp, | |
2172 | ®_beacon_list, list) { | |
2173 | list_del(®_beacon->list); | |
2174 | kfree(reg_beacon); | |
2175 | } | |
2176 | } | |
2177 | ||
fe33eb39 LR |
2178 | spin_lock(®_requests_lock); |
2179 | if (!list_empty(®_requests_list)) { | |
2180 | list_for_each_entry_safe(reg_request, tmp, | |
2181 | ®_requests_list, list) { | |
2182 | list_del(®_request->list); | |
2183 | kfree(reg_request); | |
2184 | } | |
2185 | } | |
2186 | spin_unlock(®_requests_lock); | |
2187 | ||
a1794390 | 2188 | mutex_unlock(&cfg80211_mutex); |
8318d78a | 2189 | } |