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