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