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