]>
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> | |
3b77d5ec | 5 | * Copyright 2008-2011 Luis R. Rodriguez <mcgrof@qca.qualcomm.com> |
8318d78a | 6 | * |
3b77d5ec LR |
7 | * Permission to use, copy, modify, and/or distribute this software for any |
8 | * purpose with or without fee is hereby granted, provided that the above | |
9 | * copyright notice and this permission notice appear in all copies. | |
10 | * | |
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
8318d78a JB |
18 | */ |
19 | ||
3b77d5ec | 20 | |
b2e1b302 LR |
21 | /** |
22 | * DOC: Wireless regulatory infrastructure | |
8318d78a JB |
23 | * |
24 | * The usual implementation is for a driver to read a device EEPROM to | |
25 | * determine which regulatory domain it should be operating under, then | |
26 | * looking up the allowable channels in a driver-local table and finally | |
27 | * registering those channels in the wiphy structure. | |
28 | * | |
b2e1b302 LR |
29 | * Another set of compliance enforcement is for drivers to use their |
30 | * own compliance limits which can be stored on the EEPROM. The host | |
31 | * driver or firmware may ensure these are used. | |
32 | * | |
33 | * In addition to all this we provide an extra layer of regulatory | |
34 | * conformance. For drivers which do not have any regulatory | |
35 | * information CRDA provides the complete regulatory solution. | |
36 | * For others it provides a community effort on further restrictions | |
37 | * to enhance compliance. | |
38 | * | |
39 | * Note: When number of rules --> infinity we will not be able to | |
40 | * index on alpha2 any more, instead we'll probably have to | |
41 | * rely on some SHA1 checksum of the regdomain for example. | |
42 | * | |
8318d78a | 43 | */ |
e9c0268f JP |
44 | |
45 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
46 | ||
8318d78a | 47 | #include <linux/kernel.h> |
bc3b2d7f | 48 | #include <linux/export.h> |
5a0e3ad6 | 49 | #include <linux/slab.h> |
b2e1b302 | 50 | #include <linux/list.h> |
c61029c7 | 51 | #include <linux/ctype.h> |
b2e1b302 LR |
52 | #include <linux/nl80211.h> |
53 | #include <linux/platform_device.h> | |
d9b93842 | 54 | #include <linux/moduleparam.h> |
b2e1b302 | 55 | #include <net/cfg80211.h> |
8318d78a | 56 | #include "core.h" |
b2e1b302 | 57 | #include "reg.h" |
3b377ea9 | 58 | #include "regdb.h" |
73d54c9e | 59 | #include "nl80211.h" |
8318d78a | 60 | |
4113f751 | 61 | #ifdef CONFIG_CFG80211_REG_DEBUG |
12c5ffb5 JP |
62 | #define REG_DBG_PRINT(format, args...) \ |
63 | printk(KERN_DEBUG pr_fmt(format), ##args) | |
4113f751 | 64 | #else |
8271195e | 65 | #define REG_DBG_PRINT(args...) |
4113f751 LR |
66 | #endif |
67 | ||
2f92212b JB |
68 | enum reg_request_treatment { |
69 | REG_REQ_OK, | |
70 | REG_REQ_IGNORE, | |
71 | REG_REQ_INTERSECT, | |
72 | REG_REQ_ALREADY_SET, | |
73 | }; | |
74 | ||
a042994d LR |
75 | static struct regulatory_request core_request_world = { |
76 | .initiator = NL80211_REGDOM_SET_BY_CORE, | |
77 | .alpha2[0] = '0', | |
78 | .alpha2[1] = '0', | |
79 | .intersect = false, | |
80 | .processed = true, | |
81 | .country_ie_env = ENVIRON_ANY, | |
82 | }; | |
83 | ||
38fd2143 JB |
84 | /* |
85 | * Receipt of information from last regulatory request, | |
86 | * protected by RTNL (and can be accessed with RCU protection) | |
87 | */ | |
c492db37 JB |
88 | static struct regulatory_request __rcu *last_request = |
89 | (void __rcu *)&core_request_world; | |
734366de | 90 | |
b2e1b302 LR |
91 | /* To trigger userspace events */ |
92 | static struct platform_device *reg_pdev; | |
8318d78a | 93 | |
4d9d88d1 SJR |
94 | static struct device_type reg_device_type = { |
95 | .uevent = reg_device_uevent, | |
96 | }; | |
97 | ||
fb1fc7ad LR |
98 | /* |
99 | * Central wireless core regulatory domains, we only need two, | |
734366de | 100 | * the current one and a world regulatory domain in case we have no |
e8da2bb4 | 101 | * information to give us an alpha2. |
38fd2143 | 102 | * (protected by RTNL, can be read under RCU) |
fb1fc7ad | 103 | */ |
458f4f9e | 104 | const struct ieee80211_regdomain __rcu *cfg80211_regdomain; |
734366de | 105 | |
57b5ce07 LR |
106 | /* |
107 | * Number of devices that registered to the core | |
108 | * that support cellular base station regulatory hints | |
38fd2143 | 109 | * (protected by RTNL) |
57b5ce07 LR |
110 | */ |
111 | static int reg_num_devs_support_basehint; | |
112 | ||
458f4f9e JB |
113 | static const struct ieee80211_regdomain *get_cfg80211_regdom(void) |
114 | { | |
38fd2143 | 115 | return rtnl_dereference(cfg80211_regdomain); |
458f4f9e JB |
116 | } |
117 | ||
118 | static const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy) | |
119 | { | |
38fd2143 | 120 | return rtnl_dereference(wiphy->regd); |
458f4f9e JB |
121 | } |
122 | ||
123 | static void rcu_free_regdom(const struct ieee80211_regdomain *r) | |
124 | { | |
125 | if (!r) | |
126 | return; | |
127 | kfree_rcu((struct ieee80211_regdomain *)r, rcu_head); | |
128 | } | |
129 | ||
c492db37 JB |
130 | static struct regulatory_request *get_last_request(void) |
131 | { | |
38fd2143 | 132 | return rcu_dereference_rtnl(last_request); |
c492db37 JB |
133 | } |
134 | ||
e38f8a7a | 135 | /* Used to queue up regulatory hints */ |
fe33eb39 LR |
136 | static LIST_HEAD(reg_requests_list); |
137 | static spinlock_t reg_requests_lock; | |
138 | ||
e38f8a7a LR |
139 | /* Used to queue up beacon hints for review */ |
140 | static LIST_HEAD(reg_pending_beacons); | |
141 | static spinlock_t reg_pending_beacons_lock; | |
142 | ||
143 | /* Used to keep track of processed beacon hints */ | |
144 | static LIST_HEAD(reg_beacon_list); | |
145 | ||
146 | struct reg_beacon { | |
147 | struct list_head list; | |
148 | struct ieee80211_channel chan; | |
149 | }; | |
150 | ||
f333a7a2 LR |
151 | static void reg_todo(struct work_struct *work); |
152 | static DECLARE_WORK(reg_work, reg_todo); | |
153 | ||
a90c7a31 LR |
154 | static void reg_timeout_work(struct work_struct *work); |
155 | static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work); | |
156 | ||
734366de JB |
157 | /* We keep a static world regulatory domain in case of the absence of CRDA */ |
158 | static const struct ieee80211_regdomain world_regdom = { | |
90cdc6df | 159 | .n_reg_rules = 6, |
734366de JB |
160 | .alpha2 = "00", |
161 | .reg_rules = { | |
68798a62 LR |
162 | /* IEEE 802.11b/g, channels 1..11 */ |
163 | REG_RULE(2412-10, 2462+10, 40, 6, 20, 0), | |
43c771a1 JB |
164 | /* IEEE 802.11b/g, channels 12..13. */ |
165 | REG_RULE(2467-10, 2472+10, 40, 6, 20, | |
8fe02e16 | 166 | NL80211_RRF_NO_IR), |
611b6a82 LR |
167 | /* IEEE 802.11 channel 14 - Only JP enables |
168 | * this and for 802.11b only */ | |
169 | REG_RULE(2484-10, 2484+10, 20, 6, 20, | |
8fe02e16 | 170 | NL80211_RRF_NO_IR | |
611b6a82 LR |
171 | NL80211_RRF_NO_OFDM), |
172 | /* IEEE 802.11a, channel 36..48 */ | |
131a19bc | 173 | REG_RULE(5180-10, 5240+10, 160, 6, 20, |
8fe02e16 | 174 | NL80211_RRF_NO_IR), |
3fc71f77 | 175 | |
131a19bc JB |
176 | /* IEEE 802.11a, channel 52..64 - DFS required */ |
177 | REG_RULE(5260-10, 5320+10, 160, 6, 20, | |
8fe02e16 | 178 | NL80211_RRF_NO_IR | |
131a19bc JB |
179 | NL80211_RRF_DFS), |
180 | ||
181 | /* IEEE 802.11a, channel 100..144 - DFS required */ | |
182 | REG_RULE(5500-10, 5720+10, 160, 6, 20, | |
8fe02e16 | 183 | NL80211_RRF_NO_IR | |
131a19bc | 184 | NL80211_RRF_DFS), |
3fc71f77 LR |
185 | |
186 | /* IEEE 802.11a, channel 149..165 */ | |
8ab9d85c | 187 | REG_RULE(5745-10, 5825+10, 80, 6, 20, |
8fe02e16 | 188 | NL80211_RRF_NO_IR), |
90cdc6df VK |
189 | |
190 | /* IEEE 802.11ad (60gHz), channels 1..3 */ | |
191 | REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0), | |
734366de JB |
192 | } |
193 | }; | |
194 | ||
38fd2143 | 195 | /* protected by RTNL */ |
a3d2eaf0 JB |
196 | static const struct ieee80211_regdomain *cfg80211_world_regdom = |
197 | &world_regdom; | |
734366de | 198 | |
6ee7d330 | 199 | static char *ieee80211_regdom = "00"; |
09d989d1 | 200 | static char user_alpha2[2]; |
6ee7d330 | 201 | |
734366de JB |
202 | module_param(ieee80211_regdom, charp, 0444); |
203 | MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); | |
204 | ||
5ad6ef5e LR |
205 | static void reg_kfree_last_request(void) |
206 | { | |
207 | struct regulatory_request *lr; | |
208 | ||
209 | lr = get_last_request(); | |
210 | ||
211 | if (lr != &core_request_world && lr) | |
212 | kfree_rcu(lr, rcu_head); | |
213 | } | |
214 | ||
05f1a3ea LR |
215 | static void reg_update_last_request(struct regulatory_request *request) |
216 | { | |
217 | reg_kfree_last_request(); | |
218 | rcu_assign_pointer(last_request, request); | |
219 | } | |
220 | ||
379b82f4 JB |
221 | static void reset_regdomains(bool full_reset, |
222 | const struct ieee80211_regdomain *new_regdom) | |
734366de | 223 | { |
458f4f9e JB |
224 | const struct ieee80211_regdomain *r; |
225 | ||
38fd2143 | 226 | ASSERT_RTNL(); |
e8da2bb4 | 227 | |
458f4f9e JB |
228 | r = get_cfg80211_regdom(); |
229 | ||
942b25cf | 230 | /* avoid freeing static information or freeing something twice */ |
458f4f9e JB |
231 | if (r == cfg80211_world_regdom) |
232 | r = NULL; | |
942b25cf JB |
233 | if (cfg80211_world_regdom == &world_regdom) |
234 | cfg80211_world_regdom = NULL; | |
458f4f9e JB |
235 | if (r == &world_regdom) |
236 | r = NULL; | |
942b25cf | 237 | |
458f4f9e JB |
238 | rcu_free_regdom(r); |
239 | rcu_free_regdom(cfg80211_world_regdom); | |
734366de | 240 | |
a3d2eaf0 | 241 | cfg80211_world_regdom = &world_regdom; |
458f4f9e | 242 | rcu_assign_pointer(cfg80211_regdomain, new_regdom); |
a042994d LR |
243 | |
244 | if (!full_reset) | |
245 | return; | |
246 | ||
05f1a3ea | 247 | reg_update_last_request(&core_request_world); |
734366de JB |
248 | } |
249 | ||
fb1fc7ad LR |
250 | /* |
251 | * Dynamic world regulatory domain requested by the wireless | |
252 | * core upon initialization | |
253 | */ | |
a3d2eaf0 | 254 | static void update_world_regdomain(const struct ieee80211_regdomain *rd) |
734366de | 255 | { |
c492db37 | 256 | struct regulatory_request *lr; |
734366de | 257 | |
c492db37 JB |
258 | lr = get_last_request(); |
259 | ||
260 | WARN_ON(!lr); | |
734366de | 261 | |
379b82f4 | 262 | reset_regdomains(false, rd); |
734366de JB |
263 | |
264 | cfg80211_world_regdom = rd; | |
734366de | 265 | } |
734366de | 266 | |
a3d2eaf0 | 267 | bool is_world_regdom(const char *alpha2) |
b2e1b302 LR |
268 | { |
269 | if (!alpha2) | |
270 | return false; | |
1a919318 | 271 | return alpha2[0] == '0' && alpha2[1] == '0'; |
b2e1b302 | 272 | } |
8318d78a | 273 | |
a3d2eaf0 | 274 | static bool is_alpha2_set(const char *alpha2) |
b2e1b302 LR |
275 | { |
276 | if (!alpha2) | |
277 | return false; | |
1a919318 | 278 | return alpha2[0] && alpha2[1]; |
b2e1b302 | 279 | } |
8318d78a | 280 | |
a3d2eaf0 | 281 | static bool is_unknown_alpha2(const char *alpha2) |
b2e1b302 LR |
282 | { |
283 | if (!alpha2) | |
284 | return false; | |
fb1fc7ad LR |
285 | /* |
286 | * Special case where regulatory domain was built by driver | |
287 | * but a specific alpha2 cannot be determined | |
288 | */ | |
1a919318 | 289 | return alpha2[0] == '9' && alpha2[1] == '9'; |
b2e1b302 | 290 | } |
8318d78a | 291 | |
3f2355cb LR |
292 | static bool is_intersected_alpha2(const char *alpha2) |
293 | { | |
294 | if (!alpha2) | |
295 | return false; | |
fb1fc7ad LR |
296 | /* |
297 | * Special case where regulatory domain is the | |
3f2355cb | 298 | * result of an intersection between two regulatory domain |
fb1fc7ad LR |
299 | * structures |
300 | */ | |
1a919318 | 301 | return alpha2[0] == '9' && alpha2[1] == '8'; |
3f2355cb LR |
302 | } |
303 | ||
a3d2eaf0 | 304 | static bool is_an_alpha2(const char *alpha2) |
b2e1b302 LR |
305 | { |
306 | if (!alpha2) | |
307 | return false; | |
1a919318 | 308 | return isalpha(alpha2[0]) && isalpha(alpha2[1]); |
b2e1b302 | 309 | } |
8318d78a | 310 | |
a3d2eaf0 | 311 | static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y) |
b2e1b302 LR |
312 | { |
313 | if (!alpha2_x || !alpha2_y) | |
314 | return false; | |
1a919318 | 315 | return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1]; |
b2e1b302 LR |
316 | } |
317 | ||
69b1572b | 318 | static bool regdom_changes(const char *alpha2) |
b2e1b302 | 319 | { |
458f4f9e | 320 | const struct ieee80211_regdomain *r = get_cfg80211_regdom(); |
761cf7ec | 321 | |
458f4f9e | 322 | if (!r) |
b2e1b302 | 323 | return true; |
458f4f9e | 324 | return !alpha2_equal(r->alpha2, alpha2); |
b2e1b302 LR |
325 | } |
326 | ||
09d989d1 LR |
327 | /* |
328 | * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets | |
329 | * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER | |
330 | * has ever been issued. | |
331 | */ | |
332 | static bool is_user_regdom_saved(void) | |
333 | { | |
334 | if (user_alpha2[0] == '9' && user_alpha2[1] == '7') | |
335 | return false; | |
336 | ||
337 | /* This would indicate a mistake on the design */ | |
1a919318 | 338 | if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2), |
09d989d1 | 339 | "Unexpected user alpha2: %c%c\n", |
1a919318 | 340 | user_alpha2[0], user_alpha2[1])) |
09d989d1 LR |
341 | return false; |
342 | ||
343 | return true; | |
344 | } | |
345 | ||
e9763c3c JB |
346 | static const struct ieee80211_regdomain * |
347 | reg_copy_regd(const struct ieee80211_regdomain *src_regd) | |
3b377ea9 JL |
348 | { |
349 | struct ieee80211_regdomain *regd; | |
e9763c3c | 350 | int size_of_regd; |
3b377ea9 JL |
351 | unsigned int i; |
352 | ||
82f20856 JB |
353 | size_of_regd = |
354 | sizeof(struct ieee80211_regdomain) + | |
355 | src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule); | |
3b377ea9 JL |
356 | |
357 | regd = kzalloc(size_of_regd, GFP_KERNEL); | |
358 | if (!regd) | |
e9763c3c | 359 | return ERR_PTR(-ENOMEM); |
3b377ea9 JL |
360 | |
361 | memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain)); | |
362 | ||
363 | for (i = 0; i < src_regd->n_reg_rules; i++) | |
364 | memcpy(®d->reg_rules[i], &src_regd->reg_rules[i], | |
e9763c3c | 365 | sizeof(struct ieee80211_reg_rule)); |
3b377ea9 | 366 | |
e9763c3c | 367 | return regd; |
3b377ea9 JL |
368 | } |
369 | ||
370 | #ifdef CONFIG_CFG80211_INTERNAL_REGDB | |
371 | struct reg_regdb_search_request { | |
372 | char alpha2[2]; | |
373 | struct list_head list; | |
374 | }; | |
375 | ||
376 | static LIST_HEAD(reg_regdb_search_list); | |
368d06f5 | 377 | static DEFINE_MUTEX(reg_regdb_search_mutex); |
3b377ea9 JL |
378 | |
379 | static void reg_regdb_search(struct work_struct *work) | |
380 | { | |
381 | struct reg_regdb_search_request *request; | |
e9763c3c JB |
382 | const struct ieee80211_regdomain *curdom, *regdom = NULL; |
383 | int i; | |
a85d0d7f | 384 | |
5fe231e8 | 385 | rtnl_lock(); |
3b377ea9 | 386 | |
368d06f5 | 387 | mutex_lock(®_regdb_search_mutex); |
3b377ea9 JL |
388 | while (!list_empty(®_regdb_search_list)) { |
389 | request = list_first_entry(®_regdb_search_list, | |
390 | struct reg_regdb_search_request, | |
391 | list); | |
392 | list_del(&request->list); | |
393 | ||
1a919318 | 394 | for (i = 0; i < reg_regdb_size; i++) { |
3b377ea9 JL |
395 | curdom = reg_regdb[i]; |
396 | ||
1a919318 | 397 | if (alpha2_equal(request->alpha2, curdom->alpha2)) { |
e9763c3c | 398 | regdom = reg_copy_regd(curdom); |
3b377ea9 JL |
399 | break; |
400 | } | |
401 | } | |
402 | ||
403 | kfree(request); | |
404 | } | |
368d06f5 | 405 | mutex_unlock(®_regdb_search_mutex); |
a85d0d7f | 406 | |
e9763c3c | 407 | if (!IS_ERR_OR_NULL(regdom)) |
a85d0d7f LR |
408 | set_regdom(regdom); |
409 | ||
5fe231e8 | 410 | rtnl_unlock(); |
3b377ea9 JL |
411 | } |
412 | ||
413 | static DECLARE_WORK(reg_regdb_work, reg_regdb_search); | |
414 | ||
415 | static void reg_regdb_query(const char *alpha2) | |
416 | { | |
417 | struct reg_regdb_search_request *request; | |
418 | ||
419 | if (!alpha2) | |
420 | return; | |
421 | ||
422 | request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL); | |
423 | if (!request) | |
424 | return; | |
425 | ||
426 | memcpy(request->alpha2, alpha2, 2); | |
427 | ||
368d06f5 | 428 | mutex_lock(®_regdb_search_mutex); |
3b377ea9 | 429 | list_add_tail(&request->list, ®_regdb_search_list); |
368d06f5 | 430 | mutex_unlock(®_regdb_search_mutex); |
3b377ea9 JL |
431 | |
432 | schedule_work(®_regdb_work); | |
433 | } | |
80007efe LR |
434 | |
435 | /* Feel free to add any other sanity checks here */ | |
436 | static void reg_regdb_size_check(void) | |
437 | { | |
438 | /* We should ideally BUILD_BUG_ON() but then random builds would fail */ | |
439 | WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it..."); | |
440 | } | |
3b377ea9 | 441 | #else |
80007efe | 442 | static inline void reg_regdb_size_check(void) {} |
3b377ea9 JL |
443 | static inline void reg_regdb_query(const char *alpha2) {} |
444 | #endif /* CONFIG_CFG80211_INTERNAL_REGDB */ | |
445 | ||
fb1fc7ad LR |
446 | /* |
447 | * This lets us keep regulatory code which is updated on a regulatory | |
4d9d88d1 SJR |
448 | * basis in userspace. Country information is filled in by |
449 | * reg_device_uevent | |
fb1fc7ad | 450 | */ |
b2e1b302 LR |
451 | static int call_crda(const char *alpha2) |
452 | { | |
b2e1b302 | 453 | if (!is_world_regdom((char *) alpha2)) |
e9c0268f | 454 | pr_info("Calling CRDA for country: %c%c\n", |
b2e1b302 LR |
455 | alpha2[0], alpha2[1]); |
456 | else | |
e9c0268f | 457 | pr_info("Calling CRDA to update world regulatory domain\n"); |
b2e1b302 | 458 | |
3b377ea9 JL |
459 | /* query internal regulatory database (if it exists) */ |
460 | reg_regdb_query(alpha2); | |
461 | ||
4d9d88d1 | 462 | return kobject_uevent(®_pdev->dev.kobj, KOBJ_CHANGE); |
b2e1b302 LR |
463 | } |
464 | ||
fe6631ff LR |
465 | static enum reg_request_treatment |
466 | reg_call_crda(struct regulatory_request *request) | |
467 | { | |
468 | if (call_crda(request->alpha2)) | |
469 | return REG_REQ_IGNORE; | |
470 | return REG_REQ_OK; | |
471 | } | |
472 | ||
e438768f | 473 | bool reg_is_valid_request(const char *alpha2) |
b2e1b302 | 474 | { |
c492db37 | 475 | struct regulatory_request *lr = get_last_request(); |
61405e97 | 476 | |
c492db37 | 477 | if (!lr || lr->processed) |
f6037d09 JB |
478 | return false; |
479 | ||
c492db37 | 480 | return alpha2_equal(lr->alpha2, alpha2); |
b2e1b302 | 481 | } |
8318d78a | 482 | |
b2e1b302 | 483 | /* Sanity check on a regulatory rule */ |
a3d2eaf0 | 484 | static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule) |
8318d78a | 485 | { |
a3d2eaf0 | 486 | const struct ieee80211_freq_range *freq_range = &rule->freq_range; |
b2e1b302 LR |
487 | u32 freq_diff; |
488 | ||
91e99004 | 489 | if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0) |
b2e1b302 LR |
490 | return false; |
491 | ||
492 | if (freq_range->start_freq_khz > freq_range->end_freq_khz) | |
493 | return false; | |
494 | ||
495 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; | |
496 | ||
bd05f28e | 497 | if (freq_range->end_freq_khz <= freq_range->start_freq_khz || |
1a919318 | 498 | freq_range->max_bandwidth_khz > freq_diff) |
b2e1b302 LR |
499 | return false; |
500 | ||
501 | return true; | |
502 | } | |
503 | ||
a3d2eaf0 | 504 | static bool is_valid_rd(const struct ieee80211_regdomain *rd) |
b2e1b302 | 505 | { |
a3d2eaf0 | 506 | const struct ieee80211_reg_rule *reg_rule = NULL; |
b2e1b302 | 507 | unsigned int i; |
8318d78a | 508 | |
b2e1b302 LR |
509 | if (!rd->n_reg_rules) |
510 | return false; | |
8318d78a | 511 | |
88dc1c3f LR |
512 | if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES)) |
513 | return false; | |
514 | ||
b2e1b302 LR |
515 | for (i = 0; i < rd->n_reg_rules; i++) { |
516 | reg_rule = &rd->reg_rules[i]; | |
517 | if (!is_valid_reg_rule(reg_rule)) | |
518 | return false; | |
519 | } | |
520 | ||
521 | return true; | |
8318d78a JB |
522 | } |
523 | ||
038659e7 | 524 | static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range, |
fe7ef5e9 | 525 | u32 center_freq_khz, u32 bw_khz) |
b2e1b302 | 526 | { |
038659e7 LR |
527 | u32 start_freq_khz, end_freq_khz; |
528 | ||
529 | start_freq_khz = center_freq_khz - (bw_khz/2); | |
530 | end_freq_khz = center_freq_khz + (bw_khz/2); | |
531 | ||
532 | if (start_freq_khz >= freq_range->start_freq_khz && | |
533 | end_freq_khz <= freq_range->end_freq_khz) | |
534 | return true; | |
535 | ||
536 | return false; | |
b2e1b302 | 537 | } |
8318d78a | 538 | |
0c7dc45d LR |
539 | /** |
540 | * freq_in_rule_band - tells us if a frequency is in a frequency band | |
541 | * @freq_range: frequency rule we want to query | |
542 | * @freq_khz: frequency we are inquiring about | |
543 | * | |
544 | * This lets us know if a specific frequency rule is or is not relevant to | |
545 | * a specific frequency's band. Bands are device specific and artificial | |
64629b9d VK |
546 | * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"), |
547 | * however it is safe for now to assume that a frequency rule should not be | |
548 | * part of a frequency's band if the start freq or end freq are off by more | |
549 | * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the | |
550 | * 60 GHz band. | |
0c7dc45d LR |
551 | * This resolution can be lowered and should be considered as we add |
552 | * regulatory rule support for other "bands". | |
553 | **/ | |
554 | static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range, | |
1a919318 | 555 | u32 freq_khz) |
0c7dc45d LR |
556 | { |
557 | #define ONE_GHZ_IN_KHZ 1000000 | |
64629b9d VK |
558 | /* |
559 | * From 802.11ad: directional multi-gigabit (DMG): | |
560 | * Pertaining to operation in a frequency band containing a channel | |
561 | * with the Channel starting frequency above 45 GHz. | |
562 | */ | |
563 | u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ? | |
564 | 10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ; | |
565 | if (abs(freq_khz - freq_range->start_freq_khz) <= limit) | |
0c7dc45d | 566 | return true; |
64629b9d | 567 | if (abs(freq_khz - freq_range->end_freq_khz) <= limit) |
0c7dc45d LR |
568 | return true; |
569 | return false; | |
570 | #undef ONE_GHZ_IN_KHZ | |
571 | } | |
572 | ||
adbfb058 LR |
573 | /* |
574 | * Later on we can perhaps use the more restrictive DFS | |
575 | * region but we don't have information for that yet so | |
576 | * for now simply disallow conflicts. | |
577 | */ | |
578 | static enum nl80211_dfs_regions | |
579 | reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1, | |
580 | const enum nl80211_dfs_regions dfs_region2) | |
581 | { | |
582 | if (dfs_region1 != dfs_region2) | |
583 | return NL80211_DFS_UNSET; | |
584 | return dfs_region1; | |
585 | } | |
586 | ||
fb1fc7ad LR |
587 | /* |
588 | * Helper for regdom_intersect(), this does the real | |
589 | * mathematical intersection fun | |
590 | */ | |
1a919318 JB |
591 | static int reg_rules_intersect(const struct ieee80211_reg_rule *rule1, |
592 | const struct ieee80211_reg_rule *rule2, | |
593 | struct ieee80211_reg_rule *intersected_rule) | |
9c96477d LR |
594 | { |
595 | const struct ieee80211_freq_range *freq_range1, *freq_range2; | |
596 | struct ieee80211_freq_range *freq_range; | |
597 | const struct ieee80211_power_rule *power_rule1, *power_rule2; | |
598 | struct ieee80211_power_rule *power_rule; | |
599 | u32 freq_diff; | |
600 | ||
601 | freq_range1 = &rule1->freq_range; | |
602 | freq_range2 = &rule2->freq_range; | |
603 | freq_range = &intersected_rule->freq_range; | |
604 | ||
605 | power_rule1 = &rule1->power_rule; | |
606 | power_rule2 = &rule2->power_rule; | |
607 | power_rule = &intersected_rule->power_rule; | |
608 | ||
609 | freq_range->start_freq_khz = max(freq_range1->start_freq_khz, | |
1a919318 | 610 | freq_range2->start_freq_khz); |
9c96477d | 611 | freq_range->end_freq_khz = min(freq_range1->end_freq_khz, |
1a919318 | 612 | freq_range2->end_freq_khz); |
9c96477d | 613 | freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz, |
1a919318 | 614 | freq_range2->max_bandwidth_khz); |
9c96477d LR |
615 | |
616 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; | |
617 | if (freq_range->max_bandwidth_khz > freq_diff) | |
618 | freq_range->max_bandwidth_khz = freq_diff; | |
619 | ||
620 | power_rule->max_eirp = min(power_rule1->max_eirp, | |
621 | power_rule2->max_eirp); | |
622 | power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain, | |
623 | power_rule2->max_antenna_gain); | |
624 | ||
1a919318 | 625 | intersected_rule->flags = rule1->flags | rule2->flags; |
9c96477d LR |
626 | |
627 | if (!is_valid_reg_rule(intersected_rule)) | |
628 | return -EINVAL; | |
629 | ||
630 | return 0; | |
631 | } | |
632 | ||
633 | /** | |
634 | * regdom_intersect - do the intersection between two regulatory domains | |
635 | * @rd1: first regulatory domain | |
636 | * @rd2: second regulatory domain | |
637 | * | |
638 | * Use this function to get the intersection between two regulatory domains. | |
639 | * Once completed we will mark the alpha2 for the rd as intersected, "98", | |
640 | * as no one single alpha2 can represent this regulatory domain. | |
641 | * | |
642 | * Returns a pointer to the regulatory domain structure which will hold the | |
643 | * resulting intersection of rules between rd1 and rd2. We will | |
644 | * kzalloc() this structure for you. | |
645 | */ | |
1a919318 JB |
646 | static struct ieee80211_regdomain * |
647 | regdom_intersect(const struct ieee80211_regdomain *rd1, | |
648 | const struct ieee80211_regdomain *rd2) | |
9c96477d LR |
649 | { |
650 | int r, size_of_regd; | |
651 | unsigned int x, y; | |
652 | unsigned int num_rules = 0, rule_idx = 0; | |
653 | const struct ieee80211_reg_rule *rule1, *rule2; | |
654 | struct ieee80211_reg_rule *intersected_rule; | |
655 | struct ieee80211_regdomain *rd; | |
656 | /* This is just a dummy holder to help us count */ | |
74f53cd8 | 657 | struct ieee80211_reg_rule dummy_rule; |
9c96477d LR |
658 | |
659 | if (!rd1 || !rd2) | |
660 | return NULL; | |
661 | ||
fb1fc7ad LR |
662 | /* |
663 | * First we get a count of the rules we'll need, then we actually | |
9c96477d LR |
664 | * build them. This is to so we can malloc() and free() a |
665 | * regdomain once. The reason we use reg_rules_intersect() here | |
666 | * is it will return -EINVAL if the rule computed makes no sense. | |
fb1fc7ad LR |
667 | * All rules that do check out OK are valid. |
668 | */ | |
9c96477d LR |
669 | |
670 | for (x = 0; x < rd1->n_reg_rules; x++) { | |
671 | rule1 = &rd1->reg_rules[x]; | |
672 | for (y = 0; y < rd2->n_reg_rules; y++) { | |
673 | rule2 = &rd2->reg_rules[y]; | |
74f53cd8 | 674 | if (!reg_rules_intersect(rule1, rule2, &dummy_rule)) |
9c96477d | 675 | num_rules++; |
9c96477d LR |
676 | } |
677 | } | |
678 | ||
679 | if (!num_rules) | |
680 | return NULL; | |
681 | ||
682 | size_of_regd = sizeof(struct ieee80211_regdomain) + | |
82f20856 | 683 | num_rules * sizeof(struct ieee80211_reg_rule); |
9c96477d LR |
684 | |
685 | rd = kzalloc(size_of_regd, GFP_KERNEL); | |
686 | if (!rd) | |
687 | return NULL; | |
688 | ||
8a57fff0 | 689 | for (x = 0; x < rd1->n_reg_rules && rule_idx < num_rules; x++) { |
9c96477d | 690 | rule1 = &rd1->reg_rules[x]; |
8a57fff0 | 691 | for (y = 0; y < rd2->n_reg_rules && rule_idx < num_rules; y++) { |
9c96477d | 692 | rule2 = &rd2->reg_rules[y]; |
fb1fc7ad LR |
693 | /* |
694 | * This time around instead of using the stack lets | |
9c96477d | 695 | * write to the target rule directly saving ourselves |
fb1fc7ad LR |
696 | * a memcpy() |
697 | */ | |
9c96477d | 698 | intersected_rule = &rd->reg_rules[rule_idx]; |
1a919318 | 699 | r = reg_rules_intersect(rule1, rule2, intersected_rule); |
fb1fc7ad LR |
700 | /* |
701 | * No need to memset here the intersected rule here as | |
702 | * we're not using the stack anymore | |
703 | */ | |
9c96477d LR |
704 | if (r) |
705 | continue; | |
706 | rule_idx++; | |
707 | } | |
708 | } | |
709 | ||
710 | if (rule_idx != num_rules) { | |
711 | kfree(rd); | |
712 | return NULL; | |
713 | } | |
714 | ||
715 | rd->n_reg_rules = num_rules; | |
716 | rd->alpha2[0] = '9'; | |
717 | rd->alpha2[1] = '8'; | |
adbfb058 LR |
718 | rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region, |
719 | rd2->dfs_region); | |
9c96477d LR |
720 | |
721 | return rd; | |
722 | } | |
723 | ||
fb1fc7ad LR |
724 | /* |
725 | * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may | |
726 | * want to just have the channel structure use these | |
727 | */ | |
b2e1b302 LR |
728 | static u32 map_regdom_flags(u32 rd_flags) |
729 | { | |
730 | u32 channel_flags = 0; | |
8fe02e16 LR |
731 | if (rd_flags & NL80211_RRF_NO_IR_ALL) |
732 | channel_flags |= IEEE80211_CHAN_NO_IR; | |
b2e1b302 LR |
733 | if (rd_flags & NL80211_RRF_DFS) |
734 | channel_flags |= IEEE80211_CHAN_RADAR; | |
03f6b084 SF |
735 | if (rd_flags & NL80211_RRF_NO_OFDM) |
736 | channel_flags |= IEEE80211_CHAN_NO_OFDM; | |
b2e1b302 LR |
737 | return channel_flags; |
738 | } | |
739 | ||
361c9c8b JB |
740 | static const struct ieee80211_reg_rule * |
741 | freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq, | |
742 | const struct ieee80211_regdomain *regd) | |
8318d78a JB |
743 | { |
744 | int i; | |
0c7dc45d | 745 | bool band_rule_found = false; |
038659e7 LR |
746 | bool bw_fits = false; |
747 | ||
3e0c3ff3 | 748 | if (!regd) |
361c9c8b | 749 | return ERR_PTR(-EINVAL); |
b2e1b302 | 750 | |
3e0c3ff3 | 751 | for (i = 0; i < regd->n_reg_rules; i++) { |
b2e1b302 LR |
752 | const struct ieee80211_reg_rule *rr; |
753 | const struct ieee80211_freq_range *fr = NULL; | |
b2e1b302 | 754 | |
3e0c3ff3 | 755 | rr = ®d->reg_rules[i]; |
b2e1b302 | 756 | fr = &rr->freq_range; |
0c7dc45d | 757 | |
fb1fc7ad LR |
758 | /* |
759 | * We only need to know if one frequency rule was | |
0c7dc45d | 760 | * was in center_freq's band, that's enough, so lets |
fb1fc7ad LR |
761 | * not overwrite it once found |
762 | */ | |
0c7dc45d LR |
763 | if (!band_rule_found) |
764 | band_rule_found = freq_in_rule_band(fr, center_freq); | |
765 | ||
fe7ef5e9 | 766 | bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(20)); |
0c7dc45d | 767 | |
361c9c8b JB |
768 | if (band_rule_found && bw_fits) |
769 | return rr; | |
8318d78a JB |
770 | } |
771 | ||
0c7dc45d | 772 | if (!band_rule_found) |
361c9c8b | 773 | return ERR_PTR(-ERANGE); |
0c7dc45d | 774 | |
361c9c8b | 775 | return ERR_PTR(-EINVAL); |
b2e1b302 LR |
776 | } |
777 | ||
361c9c8b JB |
778 | const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy, |
779 | u32 center_freq) | |
1fa25e41 | 780 | { |
5d885b99 | 781 | const struct ieee80211_regdomain *regd; |
c492db37 | 782 | struct regulatory_request *lr = get_last_request(); |
1a919318 | 783 | |
5d885b99 JB |
784 | /* |
785 | * Follow the driver's regulatory domain, if present, unless a country | |
786 | * IE has been processed or a user wants to help complaince further | |
787 | */ | |
c492db37 JB |
788 | if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && |
789 | lr->initiator != NL80211_REGDOM_SET_BY_USER && | |
5d885b99 | 790 | wiphy->regd) |
458f4f9e | 791 | regd = get_wiphy_regdom(wiphy); |
5d885b99 | 792 | else |
458f4f9e | 793 | regd = get_cfg80211_regdom(); |
5d885b99 | 794 | |
361c9c8b | 795 | return freq_reg_info_regd(wiphy, center_freq, regd); |
1fa25e41 | 796 | } |
4f366c5d | 797 | EXPORT_SYMBOL(freq_reg_info); |
b2e1b302 | 798 | |
034c6d6e | 799 | const char *reg_initiator_name(enum nl80211_reg_initiator initiator) |
926a0a09 LR |
800 | { |
801 | switch (initiator) { | |
802 | case NL80211_REGDOM_SET_BY_CORE: | |
034c6d6e | 803 | return "core"; |
926a0a09 | 804 | case NL80211_REGDOM_SET_BY_USER: |
034c6d6e | 805 | return "user"; |
926a0a09 | 806 | case NL80211_REGDOM_SET_BY_DRIVER: |
034c6d6e | 807 | return "driver"; |
926a0a09 | 808 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
034c6d6e | 809 | return "country IE"; |
926a0a09 LR |
810 | default: |
811 | WARN_ON(1); | |
034c6d6e | 812 | return "bug"; |
926a0a09 LR |
813 | } |
814 | } | |
034c6d6e | 815 | EXPORT_SYMBOL(reg_initiator_name); |
e702d3cf | 816 | |
034c6d6e | 817 | #ifdef CONFIG_CFG80211_REG_DEBUG |
e702d3cf | 818 | static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan, |
e702d3cf LR |
819 | const struct ieee80211_reg_rule *reg_rule) |
820 | { | |
821 | const struct ieee80211_power_rule *power_rule; | |
822 | const struct ieee80211_freq_range *freq_range; | |
823 | char max_antenna_gain[32]; | |
824 | ||
825 | power_rule = ®_rule->power_rule; | |
826 | freq_range = ®_rule->freq_range; | |
827 | ||
828 | if (!power_rule->max_antenna_gain) | |
829 | snprintf(max_antenna_gain, 32, "N/A"); | |
830 | else | |
831 | snprintf(max_antenna_gain, 32, "%d", power_rule->max_antenna_gain); | |
832 | ||
fe7ef5e9 JB |
833 | REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:\n", |
834 | chan->center_freq); | |
e702d3cf | 835 | |
56e6786e | 836 | REG_DBG_PRINT("%d KHz - %d KHz @ %d KHz), (%s mBi, %d mBm)\n", |
1a919318 JB |
837 | freq_range->start_freq_khz, freq_range->end_freq_khz, |
838 | freq_range->max_bandwidth_khz, max_antenna_gain, | |
e702d3cf LR |
839 | power_rule->max_eirp); |
840 | } | |
841 | #else | |
842 | static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan, | |
e702d3cf LR |
843 | const struct ieee80211_reg_rule *reg_rule) |
844 | { | |
845 | return; | |
846 | } | |
926a0a09 LR |
847 | #endif |
848 | ||
038659e7 LR |
849 | /* |
850 | * Note that right now we assume the desired channel bandwidth | |
851 | * is always 20 MHz for each individual channel (HT40 uses 20 MHz | |
fe7ef5e9 | 852 | * per channel, the primary and the extension channel). |
038659e7 | 853 | */ |
7ca43d03 LR |
854 | static void handle_channel(struct wiphy *wiphy, |
855 | enum nl80211_reg_initiator initiator, | |
fdc9d7b2 | 856 | struct ieee80211_channel *chan) |
b2e1b302 | 857 | { |
038659e7 | 858 | u32 flags, bw_flags = 0; |
b2e1b302 LR |
859 | const struct ieee80211_reg_rule *reg_rule = NULL; |
860 | const struct ieee80211_power_rule *power_rule = NULL; | |
038659e7 | 861 | const struct ieee80211_freq_range *freq_range = NULL; |
fe33eb39 | 862 | struct wiphy *request_wiphy = NULL; |
c492db37 | 863 | struct regulatory_request *lr = get_last_request(); |
a92a3ce7 | 864 | |
c492db37 | 865 | request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); |
a92a3ce7 LR |
866 | |
867 | flags = chan->orig_flags; | |
b2e1b302 | 868 | |
361c9c8b JB |
869 | reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq)); |
870 | if (IS_ERR(reg_rule)) { | |
ca4ffe8f LR |
871 | /* |
872 | * We will disable all channels that do not match our | |
25985edc | 873 | * received regulatory rule unless the hint is coming |
ca4ffe8f LR |
874 | * from a Country IE and the Country IE had no information |
875 | * about a band. The IEEE 802.11 spec allows for an AP | |
876 | * to send only a subset of the regulatory rules allowed, | |
877 | * so an AP in the US that only supports 2.4 GHz may only send | |
878 | * a country IE with information for the 2.4 GHz band | |
879 | * while 5 GHz is still supported. | |
880 | */ | |
881 | if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && | |
361c9c8b | 882 | PTR_ERR(reg_rule) == -ERANGE) |
ca4ffe8f LR |
883 | return; |
884 | ||
cc493e4f LR |
885 | if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && |
886 | request_wiphy && request_wiphy == wiphy && | |
a2f73b6c | 887 | request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) { |
cc493e4f LR |
888 | REG_DBG_PRINT("Disabling freq %d MHz for good\n", |
889 | chan->center_freq); | |
890 | chan->orig_flags |= IEEE80211_CHAN_DISABLED; | |
891 | chan->flags = chan->orig_flags; | |
892 | } else { | |
893 | REG_DBG_PRINT("Disabling freq %d MHz\n", | |
894 | chan->center_freq); | |
895 | chan->flags |= IEEE80211_CHAN_DISABLED; | |
896 | } | |
8318d78a | 897 | return; |
ca4ffe8f | 898 | } |
8318d78a | 899 | |
fe7ef5e9 | 900 | chan_reg_rule_print_dbg(chan, reg_rule); |
e702d3cf | 901 | |
b2e1b302 | 902 | power_rule = ®_rule->power_rule; |
038659e7 LR |
903 | freq_range = ®_rule->freq_range; |
904 | ||
905 | if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40)) | |
906 | bw_flags = IEEE80211_CHAN_NO_HT40; | |
c7a6ee27 JB |
907 | if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(80)) |
908 | bw_flags |= IEEE80211_CHAN_NO_80MHZ; | |
909 | if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(160)) | |
910 | bw_flags |= IEEE80211_CHAN_NO_160MHZ; | |
b2e1b302 | 911 | |
c492db37 | 912 | if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && |
806a9e39 | 913 | request_wiphy && request_wiphy == wiphy && |
a2f73b6c | 914 | request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) { |
fb1fc7ad | 915 | /* |
25985edc | 916 | * This guarantees the driver's requested regulatory domain |
f976376d | 917 | * will always be used as a base for further regulatory |
fb1fc7ad LR |
918 | * settings |
919 | */ | |
f976376d | 920 | chan->flags = chan->orig_flags = |
038659e7 | 921 | map_regdom_flags(reg_rule->flags) | bw_flags; |
f976376d LR |
922 | chan->max_antenna_gain = chan->orig_mag = |
923 | (int) MBI_TO_DBI(power_rule->max_antenna_gain); | |
279f0f55 | 924 | chan->max_reg_power = chan->max_power = chan->orig_mpwr = |
f976376d LR |
925 | (int) MBM_TO_DBM(power_rule->max_eirp); |
926 | return; | |
927 | } | |
928 | ||
04f39047 SW |
929 | chan->dfs_state = NL80211_DFS_USABLE; |
930 | chan->dfs_state_entered = jiffies; | |
931 | ||
aa3d7eef | 932 | chan->beacon_found = false; |
038659e7 | 933 | chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags); |
1a919318 JB |
934 | chan->max_antenna_gain = |
935 | min_t(int, chan->orig_mag, | |
936 | MBI_TO_DBI(power_rule->max_antenna_gain)); | |
eccc068e | 937 | chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp); |
5e31fc08 SG |
938 | if (chan->orig_mpwr) { |
939 | /* | |
a09a85a0 LR |
940 | * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER |
941 | * will always follow the passed country IE power settings. | |
5e31fc08 SG |
942 | */ |
943 | if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && | |
a09a85a0 | 944 | wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER) |
5e31fc08 SG |
945 | chan->max_power = chan->max_reg_power; |
946 | else | |
947 | chan->max_power = min(chan->orig_mpwr, | |
948 | chan->max_reg_power); | |
949 | } else | |
950 | chan->max_power = chan->max_reg_power; | |
8318d78a JB |
951 | } |
952 | ||
7ca43d03 | 953 | static void handle_band(struct wiphy *wiphy, |
fdc9d7b2 JB |
954 | enum nl80211_reg_initiator initiator, |
955 | struct ieee80211_supported_band *sband) | |
8318d78a | 956 | { |
a92a3ce7 | 957 | unsigned int i; |
a92a3ce7 | 958 | |
fdc9d7b2 JB |
959 | if (!sband) |
960 | return; | |
8318d78a JB |
961 | |
962 | for (i = 0; i < sband->n_channels; i++) | |
fdc9d7b2 | 963 | handle_channel(wiphy, initiator, &sband->channels[i]); |
8318d78a JB |
964 | } |
965 | ||
57b5ce07 LR |
966 | static bool reg_request_cell_base(struct regulatory_request *request) |
967 | { | |
968 | if (request->initiator != NL80211_REGDOM_SET_BY_USER) | |
969 | return false; | |
1a919318 | 970 | return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE; |
57b5ce07 LR |
971 | } |
972 | ||
973 | bool reg_last_request_cell_base(void) | |
974 | { | |
38fd2143 | 975 | return reg_request_cell_base(get_last_request()); |
57b5ce07 LR |
976 | } |
977 | ||
978 | #ifdef CONFIG_CFG80211_CERTIFICATION_ONUS | |
57b5ce07 | 979 | /* Core specific check */ |
2f92212b JB |
980 | static enum reg_request_treatment |
981 | reg_ignore_cell_hint(struct regulatory_request *pending_request) | |
57b5ce07 | 982 | { |
c492db37 JB |
983 | struct regulatory_request *lr = get_last_request(); |
984 | ||
57b5ce07 | 985 | if (!reg_num_devs_support_basehint) |
2f92212b | 986 | return REG_REQ_IGNORE; |
57b5ce07 | 987 | |
c492db37 | 988 | if (reg_request_cell_base(lr) && |
1a919318 | 989 | !regdom_changes(pending_request->alpha2)) |
2f92212b | 990 | return REG_REQ_ALREADY_SET; |
1a919318 | 991 | |
2f92212b | 992 | return REG_REQ_OK; |
57b5ce07 LR |
993 | } |
994 | ||
995 | /* Device specific check */ | |
996 | static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy) | |
997 | { | |
1a919318 | 998 | return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS); |
57b5ce07 LR |
999 | } |
1000 | #else | |
1001 | static int reg_ignore_cell_hint(struct regulatory_request *pending_request) | |
1002 | { | |
2f92212b | 1003 | return REG_REQ_IGNORE; |
57b5ce07 | 1004 | } |
1a919318 JB |
1005 | |
1006 | static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy) | |
57b5ce07 LR |
1007 | { |
1008 | return true; | |
1009 | } | |
1010 | #endif | |
1011 | ||
fa1fb9cb LR |
1012 | static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy) |
1013 | { | |
a2f73b6c LR |
1014 | if (wiphy->regulatory_flags & REGULATORY_STRICT_REG && |
1015 | !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)) | |
fa1fb9cb LR |
1016 | return true; |
1017 | return false; | |
1018 | } | |
57b5ce07 | 1019 | |
7db90f4a LR |
1020 | static bool ignore_reg_update(struct wiphy *wiphy, |
1021 | enum nl80211_reg_initiator initiator) | |
14b9815a | 1022 | { |
c492db37 JB |
1023 | struct regulatory_request *lr = get_last_request(); |
1024 | ||
1025 | if (!lr) { | |
034c6d6e LR |
1026 | REG_DBG_PRINT("Ignoring regulatory request set by %s " |
1027 | "since last_request is not set\n", | |
926a0a09 | 1028 | reg_initiator_name(initiator)); |
14b9815a | 1029 | return true; |
926a0a09 LR |
1030 | } |
1031 | ||
7db90f4a | 1032 | if (initiator == NL80211_REGDOM_SET_BY_CORE && |
a2f73b6c | 1033 | wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) { |
034c6d6e LR |
1034 | REG_DBG_PRINT("Ignoring regulatory request set by %s " |
1035 | "since the driver uses its own custom " | |
1036 | "regulatory domain\n", | |
926a0a09 | 1037 | reg_initiator_name(initiator)); |
14b9815a | 1038 | return true; |
926a0a09 LR |
1039 | } |
1040 | ||
fb1fc7ad LR |
1041 | /* |
1042 | * wiphy->regd will be set once the device has its own | |
1043 | * desired regulatory domain set | |
1044 | */ | |
fa1fb9cb | 1045 | if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd && |
749b527b | 1046 | initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && |
c492db37 | 1047 | !is_world_regdom(lr->alpha2)) { |
034c6d6e LR |
1048 | REG_DBG_PRINT("Ignoring regulatory request set by %s " |
1049 | "since the driver requires its own regulatory " | |
1050 | "domain to be set first\n", | |
926a0a09 | 1051 | reg_initiator_name(initiator)); |
14b9815a | 1052 | return true; |
926a0a09 LR |
1053 | } |
1054 | ||
c492db37 | 1055 | if (reg_request_cell_base(lr)) |
57b5ce07 LR |
1056 | return reg_dev_ignore_cell_hint(wiphy); |
1057 | ||
14b9815a LR |
1058 | return false; |
1059 | } | |
1060 | ||
3195e489 LR |
1061 | static bool reg_is_world_roaming(struct wiphy *wiphy) |
1062 | { | |
1063 | const struct ieee80211_regdomain *cr = get_cfg80211_regdom(); | |
1064 | const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy); | |
1065 | struct regulatory_request *lr = get_last_request(); | |
1066 | ||
1067 | if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2))) | |
1068 | return true; | |
1069 | ||
1070 | if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && | |
a2f73b6c | 1071 | wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) |
3195e489 LR |
1072 | return true; |
1073 | ||
1074 | return false; | |
1075 | } | |
1076 | ||
1a919318 | 1077 | static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx, |
e38f8a7a LR |
1078 | struct reg_beacon *reg_beacon) |
1079 | { | |
e38f8a7a LR |
1080 | struct ieee80211_supported_band *sband; |
1081 | struct ieee80211_channel *chan; | |
6bad8766 LR |
1082 | bool channel_changed = false; |
1083 | struct ieee80211_channel chan_before; | |
e38f8a7a | 1084 | |
e38f8a7a LR |
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 | ||
0f500a5f LR |
1096 | if (!reg_is_world_roaming(wiphy)) |
1097 | return; | |
1098 | ||
a2f73b6c | 1099 | if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS) |
37184244 LR |
1100 | return; |
1101 | ||
6bad8766 LR |
1102 | chan_before.center_freq = chan->center_freq; |
1103 | chan_before.flags = chan->flags; | |
1104 | ||
8fe02e16 LR |
1105 | if (chan->flags & IEEE80211_CHAN_NO_IR) { |
1106 | chan->flags &= ~IEEE80211_CHAN_NO_IR; | |
6bad8766 | 1107 | channel_changed = true; |
e38f8a7a LR |
1108 | } |
1109 | ||
6bad8766 LR |
1110 | if (channel_changed) |
1111 | nl80211_send_beacon_hint_event(wiphy, &chan_before, chan); | |
e38f8a7a LR |
1112 | } |
1113 | ||
1114 | /* | |
1115 | * Called when a scan on a wiphy finds a beacon on | |
1116 | * new channel | |
1117 | */ | |
1118 | static void wiphy_update_new_beacon(struct wiphy *wiphy, | |
1119 | struct reg_beacon *reg_beacon) | |
1120 | { | |
1121 | unsigned int i; | |
1122 | struct ieee80211_supported_band *sband; | |
1123 | ||
e38f8a7a LR |
1124 | if (!wiphy->bands[reg_beacon->chan.band]) |
1125 | return; | |
1126 | ||
1127 | sband = wiphy->bands[reg_beacon->chan.band]; | |
1128 | ||
1129 | for (i = 0; i < sband->n_channels; i++) | |
1130 | handle_reg_beacon(wiphy, i, reg_beacon); | |
1131 | } | |
1132 | ||
1133 | /* | |
1134 | * Called upon reg changes or a new wiphy is added | |
1135 | */ | |
1136 | static void wiphy_update_beacon_reg(struct wiphy *wiphy) | |
1137 | { | |
1138 | unsigned int i; | |
1139 | struct ieee80211_supported_band *sband; | |
1140 | struct reg_beacon *reg_beacon; | |
1141 | ||
e38f8a7a LR |
1142 | list_for_each_entry(reg_beacon, ®_beacon_list, list) { |
1143 | if (!wiphy->bands[reg_beacon->chan.band]) | |
1144 | continue; | |
1145 | sband = wiphy->bands[reg_beacon->chan.band]; | |
1146 | for (i = 0; i < sband->n_channels; i++) | |
1147 | handle_reg_beacon(wiphy, i, reg_beacon); | |
1148 | } | |
1149 | } | |
1150 | ||
e38f8a7a LR |
1151 | /* Reap the advantages of previously found beacons */ |
1152 | static void reg_process_beacons(struct wiphy *wiphy) | |
1153 | { | |
b1ed8ddd LR |
1154 | /* |
1155 | * Means we are just firing up cfg80211, so no beacons would | |
1156 | * have been processed yet. | |
1157 | */ | |
1158 | if (!last_request) | |
1159 | return; | |
e38f8a7a LR |
1160 | wiphy_update_beacon_reg(wiphy); |
1161 | } | |
1162 | ||
1a919318 | 1163 | static bool is_ht40_allowed(struct ieee80211_channel *chan) |
038659e7 LR |
1164 | { |
1165 | if (!chan) | |
1a919318 | 1166 | return false; |
038659e7 | 1167 | if (chan->flags & IEEE80211_CHAN_DISABLED) |
1a919318 | 1168 | return false; |
038659e7 | 1169 | /* This would happen when regulatory rules disallow HT40 completely */ |
55b183ad FF |
1170 | if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40) |
1171 | return false; | |
1172 | return true; | |
038659e7 LR |
1173 | } |
1174 | ||
1175 | static void reg_process_ht_flags_channel(struct wiphy *wiphy, | |
fdc9d7b2 | 1176 | struct ieee80211_channel *channel) |
038659e7 | 1177 | { |
fdc9d7b2 | 1178 | struct ieee80211_supported_band *sband = wiphy->bands[channel->band]; |
038659e7 LR |
1179 | struct ieee80211_channel *channel_before = NULL, *channel_after = NULL; |
1180 | unsigned int i; | |
1181 | ||
1a919318 | 1182 | if (!is_ht40_allowed(channel)) { |
038659e7 LR |
1183 | channel->flags |= IEEE80211_CHAN_NO_HT40; |
1184 | return; | |
1185 | } | |
1186 | ||
1187 | /* | |
1188 | * We need to ensure the extension channels exist to | |
1189 | * be able to use HT40- or HT40+, this finds them (or not) | |
1190 | */ | |
1191 | for (i = 0; i < sband->n_channels; i++) { | |
1192 | struct ieee80211_channel *c = &sband->channels[i]; | |
1a919318 | 1193 | |
038659e7 LR |
1194 | if (c->center_freq == (channel->center_freq - 20)) |
1195 | channel_before = c; | |
1196 | if (c->center_freq == (channel->center_freq + 20)) | |
1197 | channel_after = c; | |
1198 | } | |
1199 | ||
1200 | /* | |
1201 | * Please note that this assumes target bandwidth is 20 MHz, | |
1202 | * if that ever changes we also need to change the below logic | |
1203 | * to include that as well. | |
1204 | */ | |
1a919318 | 1205 | if (!is_ht40_allowed(channel_before)) |
689da1b3 | 1206 | channel->flags |= IEEE80211_CHAN_NO_HT40MINUS; |
038659e7 | 1207 | else |
689da1b3 | 1208 | channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS; |
038659e7 | 1209 | |
1a919318 | 1210 | if (!is_ht40_allowed(channel_after)) |
689da1b3 | 1211 | channel->flags |= IEEE80211_CHAN_NO_HT40PLUS; |
038659e7 | 1212 | else |
689da1b3 | 1213 | channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS; |
038659e7 LR |
1214 | } |
1215 | ||
1216 | static void reg_process_ht_flags_band(struct wiphy *wiphy, | |
fdc9d7b2 | 1217 | struct ieee80211_supported_band *sband) |
038659e7 LR |
1218 | { |
1219 | unsigned int i; | |
038659e7 | 1220 | |
fdc9d7b2 JB |
1221 | if (!sband) |
1222 | return; | |
038659e7 LR |
1223 | |
1224 | for (i = 0; i < sband->n_channels; i++) | |
fdc9d7b2 | 1225 | reg_process_ht_flags_channel(wiphy, &sband->channels[i]); |
038659e7 LR |
1226 | } |
1227 | ||
1228 | static void reg_process_ht_flags(struct wiphy *wiphy) | |
1229 | { | |
1230 | enum ieee80211_band band; | |
1231 | ||
1232 | if (!wiphy) | |
1233 | return; | |
1234 | ||
fdc9d7b2 JB |
1235 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) |
1236 | reg_process_ht_flags_band(wiphy, wiphy->bands[band]); | |
038659e7 LR |
1237 | } |
1238 | ||
0e3802db LR |
1239 | static void reg_call_notifier(struct wiphy *wiphy, |
1240 | struct regulatory_request *request) | |
1241 | { | |
1242 | if (wiphy->reg_notifier) | |
1243 | wiphy->reg_notifier(wiphy, request); | |
1244 | } | |
1245 | ||
eac03e38 SN |
1246 | static void wiphy_update_regulatory(struct wiphy *wiphy, |
1247 | enum nl80211_reg_initiator initiator) | |
b2e1b302 LR |
1248 | { |
1249 | enum ieee80211_band band; | |
c492db37 | 1250 | struct regulatory_request *lr = get_last_request(); |
eac03e38 | 1251 | |
0e3802db LR |
1252 | if (ignore_reg_update(wiphy, initiator)) { |
1253 | /* | |
1254 | * Regulatory updates set by CORE are ignored for custom | |
1255 | * regulatory cards. Let us notify the changes to the driver, | |
1256 | * as some drivers used this to restore its orig_* reg domain. | |
1257 | */ | |
1258 | if (initiator == NL80211_REGDOM_SET_BY_CORE && | |
a2f73b6c | 1259 | wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) |
0e3802db | 1260 | reg_call_notifier(wiphy, lr); |
a203c2aa | 1261 | return; |
0e3802db | 1262 | } |
a203c2aa | 1263 | |
c492db37 | 1264 | lr->dfs_region = get_cfg80211_regdom()->dfs_region; |
b68e6b3b | 1265 | |
fdc9d7b2 JB |
1266 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) |
1267 | handle_band(wiphy, initiator, wiphy->bands[band]); | |
a203c2aa | 1268 | |
e38f8a7a | 1269 | reg_process_beacons(wiphy); |
038659e7 | 1270 | reg_process_ht_flags(wiphy); |
0e3802db | 1271 | reg_call_notifier(wiphy, lr); |
b2e1b302 LR |
1272 | } |
1273 | ||
d7549cbb SN |
1274 | static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator) |
1275 | { | |
1276 | struct cfg80211_registered_device *rdev; | |
4a38994f | 1277 | struct wiphy *wiphy; |
d7549cbb | 1278 | |
5fe231e8 | 1279 | ASSERT_RTNL(); |
458f4f9e | 1280 | |
4a38994f RM |
1281 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
1282 | wiphy = &rdev->wiphy; | |
1283 | wiphy_update_regulatory(wiphy, initiator); | |
4a38994f | 1284 | } |
d7549cbb SN |
1285 | } |
1286 | ||
1fa25e41 | 1287 | static void handle_channel_custom(struct wiphy *wiphy, |
fdc9d7b2 | 1288 | struct ieee80211_channel *chan, |
1fa25e41 LR |
1289 | const struct ieee80211_regdomain *regd) |
1290 | { | |
038659e7 | 1291 | u32 bw_flags = 0; |
1fa25e41 LR |
1292 | const struct ieee80211_reg_rule *reg_rule = NULL; |
1293 | const struct ieee80211_power_rule *power_rule = NULL; | |
038659e7 | 1294 | const struct ieee80211_freq_range *freq_range = NULL; |
ac46d48e | 1295 | |
361c9c8b JB |
1296 | reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq), |
1297 | regd); | |
1fa25e41 | 1298 | |
361c9c8b | 1299 | if (IS_ERR(reg_rule)) { |
fe7ef5e9 JB |
1300 | REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it\n", |
1301 | chan->center_freq); | |
cc493e4f LR |
1302 | chan->orig_flags |= IEEE80211_CHAN_DISABLED; |
1303 | chan->flags = chan->orig_flags; | |
1fa25e41 LR |
1304 | return; |
1305 | } | |
1306 | ||
fe7ef5e9 | 1307 | chan_reg_rule_print_dbg(chan, reg_rule); |
e702d3cf | 1308 | |
1fa25e41 | 1309 | power_rule = ®_rule->power_rule; |
038659e7 LR |
1310 | freq_range = ®_rule->freq_range; |
1311 | ||
1312 | if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40)) | |
1313 | bw_flags = IEEE80211_CHAN_NO_HT40; | |
c7a6ee27 JB |
1314 | if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(80)) |
1315 | bw_flags |= IEEE80211_CHAN_NO_80MHZ; | |
1316 | if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(160)) | |
1317 | bw_flags |= IEEE80211_CHAN_NO_160MHZ; | |
1fa25e41 | 1318 | |
038659e7 | 1319 | chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags; |
1fa25e41 | 1320 | chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain); |
279f0f55 FF |
1321 | chan->max_reg_power = chan->max_power = |
1322 | (int) MBM_TO_DBM(power_rule->max_eirp); | |
1fa25e41 LR |
1323 | } |
1324 | ||
fdc9d7b2 JB |
1325 | static void handle_band_custom(struct wiphy *wiphy, |
1326 | struct ieee80211_supported_band *sband, | |
1fa25e41 LR |
1327 | const struct ieee80211_regdomain *regd) |
1328 | { | |
1329 | unsigned int i; | |
1fa25e41 | 1330 | |
fdc9d7b2 JB |
1331 | if (!sband) |
1332 | return; | |
1fa25e41 LR |
1333 | |
1334 | for (i = 0; i < sband->n_channels; i++) | |
fdc9d7b2 | 1335 | handle_channel_custom(wiphy, &sband->channels[i], regd); |
1fa25e41 LR |
1336 | } |
1337 | ||
1338 | /* Used by drivers prior to wiphy registration */ | |
1339 | void wiphy_apply_custom_regulatory(struct wiphy *wiphy, | |
1340 | const struct ieee80211_regdomain *regd) | |
1341 | { | |
1342 | enum ieee80211_band band; | |
bbcf3f02 | 1343 | unsigned int bands_set = 0; |
ac46d48e | 1344 | |
a2f73b6c LR |
1345 | WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG), |
1346 | "wiphy should have REGULATORY_CUSTOM_REG\n"); | |
1347 | wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG; | |
222ea581 | 1348 | |
1fa25e41 | 1349 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
bbcf3f02 LR |
1350 | if (!wiphy->bands[band]) |
1351 | continue; | |
fdc9d7b2 | 1352 | handle_band_custom(wiphy, wiphy->bands[band], regd); |
bbcf3f02 | 1353 | bands_set++; |
b2e1b302 | 1354 | } |
bbcf3f02 LR |
1355 | |
1356 | /* | |
1357 | * no point in calling this if it won't have any effect | |
1a919318 | 1358 | * on your device's supported bands. |
bbcf3f02 LR |
1359 | */ |
1360 | WARN_ON(!bands_set); | |
b2e1b302 | 1361 | } |
1fa25e41 LR |
1362 | EXPORT_SYMBOL(wiphy_apply_custom_regulatory); |
1363 | ||
b2e253cf LR |
1364 | static void reg_set_request_processed(void) |
1365 | { | |
1366 | bool need_more_processing = false; | |
c492db37 | 1367 | struct regulatory_request *lr = get_last_request(); |
b2e253cf | 1368 | |
c492db37 | 1369 | lr->processed = true; |
b2e253cf LR |
1370 | |
1371 | spin_lock(®_requests_lock); | |
1372 | if (!list_empty(®_requests_list)) | |
1373 | need_more_processing = true; | |
1374 | spin_unlock(®_requests_lock); | |
1375 | ||
c492db37 | 1376 | if (lr->initiator == NL80211_REGDOM_SET_BY_USER) |
fe20b39e | 1377 | cancel_delayed_work(®_timeout); |
a90c7a31 | 1378 | |
b2e253cf LR |
1379 | if (need_more_processing) |
1380 | schedule_work(®_work); | |
1381 | } | |
1382 | ||
b3eb7f3f LR |
1383 | /** |
1384 | * reg_process_hint_core - process core regulatory requests | |
1385 | * @pending_request: a pending core regulatory request | |
1386 | * | |
1387 | * The wireless subsystem can use this function to process | |
1388 | * a regulatory request issued by the regulatory core. | |
1389 | * | |
1390 | * Returns one of the different reg request treatment values. | |
1391 | */ | |
1392 | static enum reg_request_treatment | |
1393 | reg_process_hint_core(struct regulatory_request *core_request) | |
1394 | { | |
b3eb7f3f LR |
1395 | |
1396 | core_request->intersect = false; | |
1397 | core_request->processed = false; | |
5ad6ef5e | 1398 | |
05f1a3ea | 1399 | reg_update_last_request(core_request); |
b3eb7f3f | 1400 | |
fe6631ff | 1401 | return reg_call_crda(core_request); |
b3eb7f3f LR |
1402 | } |
1403 | ||
0d97a619 LR |
1404 | static enum reg_request_treatment |
1405 | __reg_process_hint_user(struct regulatory_request *user_request) | |
1406 | { | |
1407 | struct regulatory_request *lr = get_last_request(); | |
1408 | ||
1409 | if (reg_request_cell_base(user_request)) | |
1410 | return reg_ignore_cell_hint(user_request); | |
1411 | ||
1412 | if (reg_request_cell_base(lr)) | |
1413 | return REG_REQ_IGNORE; | |
1414 | ||
1415 | if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) | |
1416 | return REG_REQ_INTERSECT; | |
1417 | /* | |
1418 | * If the user knows better the user should set the regdom | |
1419 | * to their country before the IE is picked up | |
1420 | */ | |
1421 | if (lr->initiator == NL80211_REGDOM_SET_BY_USER && | |
1422 | lr->intersect) | |
1423 | return REG_REQ_IGNORE; | |
1424 | /* | |
1425 | * Process user requests only after previous user/driver/core | |
1426 | * requests have been processed | |
1427 | */ | |
1428 | if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE || | |
1429 | lr->initiator == NL80211_REGDOM_SET_BY_DRIVER || | |
1430 | lr->initiator == NL80211_REGDOM_SET_BY_USER) && | |
1431 | regdom_changes(lr->alpha2)) | |
1432 | return REG_REQ_IGNORE; | |
1433 | ||
1434 | if (!regdom_changes(user_request->alpha2)) | |
1435 | return REG_REQ_ALREADY_SET; | |
1436 | ||
1437 | return REG_REQ_OK; | |
1438 | } | |
1439 | ||
1440 | /** | |
1441 | * reg_process_hint_user - process user regulatory requests | |
1442 | * @user_request: a pending user regulatory request | |
1443 | * | |
1444 | * The wireless subsystem can use this function to process | |
1445 | * a regulatory request initiated by userspace. | |
1446 | * | |
1447 | * Returns one of the different reg request treatment values. | |
1448 | */ | |
1449 | static enum reg_request_treatment | |
1450 | reg_process_hint_user(struct regulatory_request *user_request) | |
1451 | { | |
1452 | enum reg_request_treatment treatment; | |
0d97a619 LR |
1453 | |
1454 | treatment = __reg_process_hint_user(user_request); | |
1455 | if (treatment == REG_REQ_IGNORE || | |
1456 | treatment == REG_REQ_ALREADY_SET) { | |
1457 | kfree(user_request); | |
1458 | return treatment; | |
1459 | } | |
1460 | ||
0d97a619 LR |
1461 | user_request->intersect = treatment == REG_REQ_INTERSECT; |
1462 | user_request->processed = false; | |
5ad6ef5e | 1463 | |
05f1a3ea | 1464 | reg_update_last_request(user_request); |
0d97a619 LR |
1465 | |
1466 | user_alpha2[0] = user_request->alpha2[0]; | |
1467 | user_alpha2[1] = user_request->alpha2[1]; | |
1468 | ||
fe6631ff | 1469 | return reg_call_crda(user_request); |
0d97a619 LR |
1470 | } |
1471 | ||
21636c7f LR |
1472 | static enum reg_request_treatment |
1473 | __reg_process_hint_driver(struct regulatory_request *driver_request) | |
1474 | { | |
1475 | struct regulatory_request *lr = get_last_request(); | |
1476 | ||
1477 | if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) { | |
1478 | if (regdom_changes(driver_request->alpha2)) | |
1479 | return REG_REQ_OK; | |
1480 | return REG_REQ_ALREADY_SET; | |
1481 | } | |
1482 | ||
1483 | /* | |
1484 | * This would happen if you unplug and plug your card | |
1485 | * back in or if you add a new device for which the previously | |
1486 | * loaded card also agrees on the regulatory domain. | |
1487 | */ | |
1488 | if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER && | |
1489 | !regdom_changes(driver_request->alpha2)) | |
1490 | return REG_REQ_ALREADY_SET; | |
1491 | ||
1492 | return REG_REQ_INTERSECT; | |
1493 | } | |
1494 | ||
1495 | /** | |
1496 | * reg_process_hint_driver - process driver regulatory requests | |
1497 | * @driver_request: a pending driver regulatory request | |
1498 | * | |
1499 | * The wireless subsystem can use this function to process | |
1500 | * a regulatory request issued by an 802.11 driver. | |
1501 | * | |
1502 | * Returns one of the different reg request treatment values. | |
1503 | */ | |
1504 | static enum reg_request_treatment | |
1505 | reg_process_hint_driver(struct wiphy *wiphy, | |
1506 | struct regulatory_request *driver_request) | |
1507 | { | |
1508 | const struct ieee80211_regdomain *regd; | |
1509 | enum reg_request_treatment treatment; | |
21636c7f LR |
1510 | |
1511 | treatment = __reg_process_hint_driver(driver_request); | |
1512 | ||
1513 | switch (treatment) { | |
1514 | case REG_REQ_OK: | |
1515 | break; | |
1516 | case REG_REQ_IGNORE: | |
1517 | kfree(driver_request); | |
1518 | return treatment; | |
1519 | case REG_REQ_INTERSECT: | |
1520 | /* fall through */ | |
1521 | case REG_REQ_ALREADY_SET: | |
1522 | regd = reg_copy_regd(get_cfg80211_regdom()); | |
1523 | if (IS_ERR(regd)) { | |
1524 | kfree(driver_request); | |
1525 | return REG_REQ_IGNORE; | |
1526 | } | |
1527 | rcu_assign_pointer(wiphy->regd, regd); | |
1528 | } | |
1529 | ||
21636c7f LR |
1530 | |
1531 | driver_request->intersect = treatment == REG_REQ_INTERSECT; | |
1532 | driver_request->processed = false; | |
5ad6ef5e | 1533 | |
05f1a3ea | 1534 | reg_update_last_request(driver_request); |
21636c7f LR |
1535 | |
1536 | /* | |
1537 | * Since CRDA will not be called in this case as we already | |
1538 | * have applied the requested regulatory domain before we just | |
1539 | * inform userspace we have processed the request | |
1540 | */ | |
1541 | if (treatment == REG_REQ_ALREADY_SET) { | |
1542 | nl80211_send_reg_change_event(driver_request); | |
1543 | reg_set_request_processed(); | |
1544 | return treatment; | |
1545 | } | |
1546 | ||
fe6631ff | 1547 | return reg_call_crda(driver_request); |
21636c7f LR |
1548 | } |
1549 | ||
b23e7a9e LR |
1550 | static enum reg_request_treatment |
1551 | __reg_process_hint_country_ie(struct wiphy *wiphy, | |
1552 | struct regulatory_request *country_ie_request) | |
1553 | { | |
1554 | struct wiphy *last_wiphy = NULL; | |
1555 | struct regulatory_request *lr = get_last_request(); | |
1556 | ||
1557 | if (reg_request_cell_base(lr)) { | |
1558 | /* Trust a Cell base station over the AP's country IE */ | |
1559 | if (regdom_changes(country_ie_request->alpha2)) | |
1560 | return REG_REQ_IGNORE; | |
1561 | return REG_REQ_ALREADY_SET; | |
2a901468 LR |
1562 | } else { |
1563 | if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE) | |
1564 | return REG_REQ_IGNORE; | |
b23e7a9e LR |
1565 | } |
1566 | ||
b23e7a9e LR |
1567 | if (unlikely(!is_an_alpha2(country_ie_request->alpha2))) |
1568 | return -EINVAL; | |
2f1c6c57 LR |
1569 | |
1570 | if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) | |
1571 | return REG_REQ_OK; | |
1572 | ||
1573 | last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); | |
1574 | ||
1575 | if (last_wiphy != wiphy) { | |
b23e7a9e | 1576 | /* |
2f1c6c57 LR |
1577 | * Two cards with two APs claiming different |
1578 | * Country IE alpha2s. We could | |
1579 | * intersect them, but that seems unlikely | |
1580 | * to be correct. Reject second one for now. | |
b23e7a9e | 1581 | */ |
2f1c6c57 LR |
1582 | if (regdom_changes(country_ie_request->alpha2)) |
1583 | return REG_REQ_IGNORE; | |
b23e7a9e LR |
1584 | return REG_REQ_ALREADY_SET; |
1585 | } | |
2f1c6c57 LR |
1586 | /* |
1587 | * Two consecutive Country IE hints on the same wiphy. | |
1588 | * This should be picked up early by the driver/stack | |
1589 | */ | |
1590 | if (WARN_ON(regdom_changes(country_ie_request->alpha2))) | |
1591 | return REG_REQ_OK; | |
1592 | return REG_REQ_ALREADY_SET; | |
b23e7a9e LR |
1593 | } |
1594 | ||
d1c96a9a | 1595 | /** |
b23e7a9e LR |
1596 | * reg_process_hint_country_ie - process regulatory requests from country IEs |
1597 | * @country_ie_request: a regulatory request from a country IE | |
d1c96a9a | 1598 | * |
b23e7a9e LR |
1599 | * The wireless subsystem can use this function to process |
1600 | * a regulatory request issued by a country Information Element. | |
d1c96a9a | 1601 | * |
2f92212b | 1602 | * Returns one of the different reg request treatment values. |
d1c96a9a | 1603 | */ |
2f92212b | 1604 | static enum reg_request_treatment |
b23e7a9e LR |
1605 | reg_process_hint_country_ie(struct wiphy *wiphy, |
1606 | struct regulatory_request *country_ie_request) | |
b2e1b302 | 1607 | { |
2f92212b | 1608 | enum reg_request_treatment treatment; |
761cf7ec | 1609 | |
b23e7a9e | 1610 | treatment = __reg_process_hint_country_ie(wiphy, country_ie_request); |
9c96477d | 1611 | |
2f92212b | 1612 | switch (treatment) { |
2f92212b JB |
1613 | case REG_REQ_OK: |
1614 | break; | |
b23e7a9e LR |
1615 | case REG_REQ_IGNORE: |
1616 | /* fall through */ | |
1617 | case REG_REQ_ALREADY_SET: | |
1618 | kfree(country_ie_request); | |
1619 | return treatment; | |
1620 | case REG_REQ_INTERSECT: | |
1621 | kfree(country_ie_request); | |
fb1fc7ad | 1622 | /* |
b23e7a9e LR |
1623 | * This doesn't happen yet, not sure we |
1624 | * ever want to support it for this case. | |
fb1fc7ad | 1625 | */ |
b23e7a9e LR |
1626 | WARN_ONCE(1, "Unexpected intersection for country IEs"); |
1627 | return REG_REQ_IGNORE; | |
3e0c3ff3 | 1628 | } |
b2e1b302 | 1629 | |
b23e7a9e LR |
1630 | country_ie_request->intersect = false; |
1631 | country_ie_request->processed = false; | |
5ad6ef5e | 1632 | |
05f1a3ea | 1633 | reg_update_last_request(country_ie_request); |
3e0c3ff3 | 1634 | |
fe6631ff | 1635 | return reg_call_crda(country_ie_request); |
b2e1b302 LR |
1636 | } |
1637 | ||
30a548c7 | 1638 | /* This processes *all* regulatory hints */ |
1daa37c7 | 1639 | static void reg_process_hint(struct regulatory_request *reg_request) |
fe33eb39 | 1640 | { |
fe33eb39 | 1641 | struct wiphy *wiphy = NULL; |
b3eb7f3f | 1642 | enum reg_request_treatment treatment; |
fe33eb39 | 1643 | |
fdc9d7b2 JB |
1644 | if (WARN_ON(!reg_request->alpha2)) |
1645 | return; | |
fe33eb39 | 1646 | |
f4173766 | 1647 | if (reg_request->wiphy_idx != WIPHY_IDX_INVALID) |
fe33eb39 LR |
1648 | wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx); |
1649 | ||
1daa37c7 | 1650 | if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER && !wiphy) { |
d951c1dd | 1651 | kfree(reg_request); |
b0e2880b | 1652 | return; |
fe33eb39 LR |
1653 | } |
1654 | ||
b3eb7f3f LR |
1655 | switch (reg_request->initiator) { |
1656 | case NL80211_REGDOM_SET_BY_CORE: | |
1657 | reg_process_hint_core(reg_request); | |
1658 | return; | |
1659 | case NL80211_REGDOM_SET_BY_USER: | |
0d97a619 LR |
1660 | treatment = reg_process_hint_user(reg_request); |
1661 | if (treatment == REG_REQ_OK || | |
1662 | treatment == REG_REQ_ALREADY_SET) | |
1663 | return; | |
1664 | schedule_delayed_work(®_timeout, msecs_to_jiffies(3142)); | |
1665 | return; | |
b3eb7f3f | 1666 | case NL80211_REGDOM_SET_BY_DRIVER: |
21636c7f LR |
1667 | treatment = reg_process_hint_driver(wiphy, reg_request); |
1668 | break; | |
b3eb7f3f | 1669 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
b23e7a9e | 1670 | treatment = reg_process_hint_country_ie(wiphy, reg_request); |
b3eb7f3f LR |
1671 | break; |
1672 | default: | |
1673 | WARN(1, "invalid initiator %d\n", reg_request->initiator); | |
1674 | return; | |
1675 | } | |
1676 | ||
b23e7a9e LR |
1677 | /* This is required so that the orig_* parameters are saved */ |
1678 | if (treatment == REG_REQ_ALREADY_SET && wiphy && | |
a2f73b6c | 1679 | wiphy->regulatory_flags & REGULATORY_STRICT_REG) |
b23e7a9e | 1680 | wiphy_update_regulatory(wiphy, reg_request->initiator); |
fe33eb39 LR |
1681 | } |
1682 | ||
b2e253cf LR |
1683 | /* |
1684 | * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* | |
1685 | * Regulatory hints come on a first come first serve basis and we | |
1686 | * must process each one atomically. | |
1687 | */ | |
fe33eb39 | 1688 | static void reg_process_pending_hints(void) |
b0e2880b | 1689 | { |
c492db37 | 1690 | struct regulatory_request *reg_request, *lr; |
fe33eb39 | 1691 | |
c492db37 | 1692 | lr = get_last_request(); |
b0e2880b | 1693 | |
b2e253cf | 1694 | /* When last_request->processed becomes true this will be rescheduled */ |
c492db37 | 1695 | if (lr && !lr->processed) { |
1a919318 | 1696 | REG_DBG_PRINT("Pending regulatory request, waiting for it to be processed...\n"); |
5fe231e8 | 1697 | return; |
b2e253cf LR |
1698 | } |
1699 | ||
fe33eb39 | 1700 | spin_lock(®_requests_lock); |
fe33eb39 | 1701 | |
b2e253cf | 1702 | if (list_empty(®_requests_list)) { |
d951c1dd | 1703 | spin_unlock(®_requests_lock); |
5fe231e8 | 1704 | return; |
fe33eb39 | 1705 | } |
b2e253cf LR |
1706 | |
1707 | reg_request = list_first_entry(®_requests_list, | |
1708 | struct regulatory_request, | |
1709 | list); | |
1710 | list_del_init(®_request->list); | |
1711 | ||
fe33eb39 | 1712 | spin_unlock(®_requests_lock); |
b0e2880b | 1713 | |
1daa37c7 | 1714 | reg_process_hint(reg_request); |
fe33eb39 LR |
1715 | } |
1716 | ||
e38f8a7a LR |
1717 | /* Processes beacon hints -- this has nothing to do with country IEs */ |
1718 | static void reg_process_pending_beacon_hints(void) | |
1719 | { | |
79c97e97 | 1720 | struct cfg80211_registered_device *rdev; |
e38f8a7a LR |
1721 | struct reg_beacon *pending_beacon, *tmp; |
1722 | ||
e38f8a7a LR |
1723 | /* This goes through the _pending_ beacon list */ |
1724 | spin_lock_bh(®_pending_beacons_lock); | |
1725 | ||
e38f8a7a LR |
1726 | list_for_each_entry_safe(pending_beacon, tmp, |
1727 | ®_pending_beacons, list) { | |
e38f8a7a LR |
1728 | list_del_init(&pending_beacon->list); |
1729 | ||
1730 | /* Applies the beacon hint to current wiphys */ | |
79c97e97 JB |
1731 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) |
1732 | wiphy_update_new_beacon(&rdev->wiphy, pending_beacon); | |
e38f8a7a LR |
1733 | |
1734 | /* Remembers the beacon hint for new wiphys or reg changes */ | |
1735 | list_add_tail(&pending_beacon->list, ®_beacon_list); | |
1736 | } | |
1737 | ||
1738 | spin_unlock_bh(®_pending_beacons_lock); | |
e38f8a7a LR |
1739 | } |
1740 | ||
fe33eb39 LR |
1741 | static void reg_todo(struct work_struct *work) |
1742 | { | |
5fe231e8 | 1743 | rtnl_lock(); |
fe33eb39 | 1744 | reg_process_pending_hints(); |
e38f8a7a | 1745 | reg_process_pending_beacon_hints(); |
5fe231e8 | 1746 | rtnl_unlock(); |
fe33eb39 LR |
1747 | } |
1748 | ||
fe33eb39 LR |
1749 | static void queue_regulatory_request(struct regulatory_request *request) |
1750 | { | |
d4f2c881 JB |
1751 | request->alpha2[0] = toupper(request->alpha2[0]); |
1752 | request->alpha2[1] = toupper(request->alpha2[1]); | |
c61029c7 | 1753 | |
fe33eb39 LR |
1754 | spin_lock(®_requests_lock); |
1755 | list_add_tail(&request->list, ®_requests_list); | |
1756 | spin_unlock(®_requests_lock); | |
1757 | ||
1758 | schedule_work(®_work); | |
1759 | } | |
1760 | ||
09d989d1 LR |
1761 | /* |
1762 | * Core regulatory hint -- happens during cfg80211_init() | |
1763 | * and when we restore regulatory settings. | |
1764 | */ | |
ba25c141 LR |
1765 | static int regulatory_hint_core(const char *alpha2) |
1766 | { | |
1767 | struct regulatory_request *request; | |
1768 | ||
1a919318 | 1769 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
ba25c141 LR |
1770 | if (!request) |
1771 | return -ENOMEM; | |
1772 | ||
1773 | request->alpha2[0] = alpha2[0]; | |
1774 | request->alpha2[1] = alpha2[1]; | |
7db90f4a | 1775 | request->initiator = NL80211_REGDOM_SET_BY_CORE; |
ba25c141 | 1776 | |
31e99729 | 1777 | queue_regulatory_request(request); |
5078b2e3 | 1778 | |
fe33eb39 | 1779 | return 0; |
ba25c141 LR |
1780 | } |
1781 | ||
fe33eb39 | 1782 | /* User hints */ |
57b5ce07 LR |
1783 | int regulatory_hint_user(const char *alpha2, |
1784 | enum nl80211_user_reg_hint_type user_reg_hint_type) | |
b2e1b302 | 1785 | { |
fe33eb39 LR |
1786 | struct regulatory_request *request; |
1787 | ||
fdc9d7b2 JB |
1788 | if (WARN_ON(!alpha2)) |
1789 | return -EINVAL; | |
b2e1b302 | 1790 | |
fe33eb39 LR |
1791 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
1792 | if (!request) | |
1793 | return -ENOMEM; | |
1794 | ||
f4173766 | 1795 | request->wiphy_idx = WIPHY_IDX_INVALID; |
fe33eb39 LR |
1796 | request->alpha2[0] = alpha2[0]; |
1797 | request->alpha2[1] = alpha2[1]; | |
e12822e1 | 1798 | request->initiator = NL80211_REGDOM_SET_BY_USER; |
57b5ce07 | 1799 | request->user_reg_hint_type = user_reg_hint_type; |
fe33eb39 LR |
1800 | |
1801 | queue_regulatory_request(request); | |
1802 | ||
1803 | return 0; | |
1804 | } | |
1805 | ||
1806 | /* Driver hints */ | |
1807 | int regulatory_hint(struct wiphy *wiphy, const char *alpha2) | |
1808 | { | |
1809 | struct regulatory_request *request; | |
1810 | ||
fdc9d7b2 JB |
1811 | if (WARN_ON(!alpha2 || !wiphy)) |
1812 | return -EINVAL; | |
fe33eb39 LR |
1813 | |
1814 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); | |
1815 | if (!request) | |
1816 | return -ENOMEM; | |
1817 | ||
1818 | request->wiphy_idx = get_wiphy_idx(wiphy); | |
1819 | ||
fe33eb39 LR |
1820 | request->alpha2[0] = alpha2[0]; |
1821 | request->alpha2[1] = alpha2[1]; | |
7db90f4a | 1822 | request->initiator = NL80211_REGDOM_SET_BY_DRIVER; |
fe33eb39 LR |
1823 | |
1824 | queue_regulatory_request(request); | |
1825 | ||
1826 | return 0; | |
b2e1b302 LR |
1827 | } |
1828 | EXPORT_SYMBOL(regulatory_hint); | |
1829 | ||
789fd033 LR |
1830 | void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band, |
1831 | const u8 *country_ie, u8 country_ie_len) | |
3f2355cb | 1832 | { |
3f2355cb | 1833 | char alpha2[2]; |
3f2355cb | 1834 | enum environment_cap env = ENVIRON_ANY; |
db2424c5 | 1835 | struct regulatory_request *request = NULL, *lr; |
d335fe63 | 1836 | |
3f2355cb LR |
1837 | /* IE len must be evenly divisible by 2 */ |
1838 | if (country_ie_len & 0x01) | |
db2424c5 | 1839 | return; |
3f2355cb LR |
1840 | |
1841 | if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) | |
db2424c5 JB |
1842 | return; |
1843 | ||
1844 | request = kzalloc(sizeof(*request), GFP_KERNEL); | |
1845 | if (!request) | |
1846 | return; | |
3f2355cb | 1847 | |
3f2355cb LR |
1848 | alpha2[0] = country_ie[0]; |
1849 | alpha2[1] = country_ie[1]; | |
1850 | ||
1851 | if (country_ie[2] == 'I') | |
1852 | env = ENVIRON_INDOOR; | |
1853 | else if (country_ie[2] == 'O') | |
1854 | env = ENVIRON_OUTDOOR; | |
1855 | ||
db2424c5 JB |
1856 | rcu_read_lock(); |
1857 | lr = get_last_request(); | |
1858 | ||
1859 | if (unlikely(!lr)) | |
1860 | goto out; | |
1861 | ||
fb1fc7ad | 1862 | /* |
8b19e6ca | 1863 | * We will run this only upon a successful connection on cfg80211. |
4b44c8bc | 1864 | * We leave conflict resolution to the workqueue, where can hold |
5fe231e8 | 1865 | * the RTNL. |
fb1fc7ad | 1866 | */ |
c492db37 JB |
1867 | if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE && |
1868 | lr->wiphy_idx != WIPHY_IDX_INVALID) | |
4b44c8bc | 1869 | goto out; |
3f2355cb | 1870 | |
fe33eb39 | 1871 | request->wiphy_idx = get_wiphy_idx(wiphy); |
4f366c5d JL |
1872 | request->alpha2[0] = alpha2[0]; |
1873 | request->alpha2[1] = alpha2[1]; | |
7db90f4a | 1874 | request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE; |
fe33eb39 LR |
1875 | request->country_ie_env = env; |
1876 | ||
fe33eb39 | 1877 | queue_regulatory_request(request); |
db2424c5 | 1878 | request = NULL; |
3f2355cb | 1879 | out: |
db2424c5 JB |
1880 | kfree(request); |
1881 | rcu_read_unlock(); | |
3f2355cb | 1882 | } |
b2e1b302 | 1883 | |
09d989d1 LR |
1884 | static void restore_alpha2(char *alpha2, bool reset_user) |
1885 | { | |
1886 | /* indicates there is no alpha2 to consider for restoration */ | |
1887 | alpha2[0] = '9'; | |
1888 | alpha2[1] = '7'; | |
1889 | ||
1890 | /* The user setting has precedence over the module parameter */ | |
1891 | if (is_user_regdom_saved()) { | |
1892 | /* Unless we're asked to ignore it and reset it */ | |
1893 | if (reset_user) { | |
1a919318 | 1894 | REG_DBG_PRINT("Restoring regulatory settings including user preference\n"); |
09d989d1 LR |
1895 | user_alpha2[0] = '9'; |
1896 | user_alpha2[1] = '7'; | |
1897 | ||
1898 | /* | |
1899 | * If we're ignoring user settings, we still need to | |
1900 | * check the module parameter to ensure we put things | |
1901 | * back as they were for a full restore. | |
1902 | */ | |
1903 | if (!is_world_regdom(ieee80211_regdom)) { | |
1a919318 JB |
1904 | REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n", |
1905 | ieee80211_regdom[0], ieee80211_regdom[1]); | |
09d989d1 LR |
1906 | alpha2[0] = ieee80211_regdom[0]; |
1907 | alpha2[1] = ieee80211_regdom[1]; | |
1908 | } | |
1909 | } else { | |
1a919318 JB |
1910 | REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c\n", |
1911 | user_alpha2[0], user_alpha2[1]); | |
09d989d1 LR |
1912 | alpha2[0] = user_alpha2[0]; |
1913 | alpha2[1] = user_alpha2[1]; | |
1914 | } | |
1915 | } else if (!is_world_regdom(ieee80211_regdom)) { | |
1a919318 JB |
1916 | REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c\n", |
1917 | ieee80211_regdom[0], ieee80211_regdom[1]); | |
09d989d1 LR |
1918 | alpha2[0] = ieee80211_regdom[0]; |
1919 | alpha2[1] = ieee80211_regdom[1]; | |
1920 | } else | |
d91e41b6 | 1921 | REG_DBG_PRINT("Restoring regulatory settings\n"); |
09d989d1 LR |
1922 | } |
1923 | ||
5ce543d1 RM |
1924 | static void restore_custom_reg_settings(struct wiphy *wiphy) |
1925 | { | |
1926 | struct ieee80211_supported_band *sband; | |
1927 | enum ieee80211_band band; | |
1928 | struct ieee80211_channel *chan; | |
1929 | int i; | |
1930 | ||
1931 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
1932 | sband = wiphy->bands[band]; | |
1933 | if (!sband) | |
1934 | continue; | |
1935 | for (i = 0; i < sband->n_channels; i++) { | |
1936 | chan = &sband->channels[i]; | |
1937 | chan->flags = chan->orig_flags; | |
1938 | chan->max_antenna_gain = chan->orig_mag; | |
1939 | chan->max_power = chan->orig_mpwr; | |
899852af | 1940 | chan->beacon_found = false; |
5ce543d1 RM |
1941 | } |
1942 | } | |
1943 | } | |
1944 | ||
09d989d1 LR |
1945 | /* |
1946 | * Restoring regulatory settings involves ingoring any | |
1947 | * possibly stale country IE information and user regulatory | |
1948 | * settings if so desired, this includes any beacon hints | |
1949 | * learned as we could have traveled outside to another country | |
1950 | * after disconnection. To restore regulatory settings we do | |
1951 | * exactly what we did at bootup: | |
1952 | * | |
1953 | * - send a core regulatory hint | |
1954 | * - send a user regulatory hint if applicable | |
1955 | * | |
1956 | * Device drivers that send a regulatory hint for a specific country | |
1957 | * keep their own regulatory domain on wiphy->regd so that does does | |
1958 | * not need to be remembered. | |
1959 | */ | |
1960 | static void restore_regulatory_settings(bool reset_user) | |
1961 | { | |
1962 | char alpha2[2]; | |
cee0bec5 | 1963 | char world_alpha2[2]; |
09d989d1 | 1964 | struct reg_beacon *reg_beacon, *btmp; |
14609555 LR |
1965 | struct regulatory_request *reg_request, *tmp; |
1966 | LIST_HEAD(tmp_reg_req_list); | |
5ce543d1 | 1967 | struct cfg80211_registered_device *rdev; |
09d989d1 | 1968 | |
5fe231e8 JB |
1969 | ASSERT_RTNL(); |
1970 | ||
2d319867 | 1971 | reset_regdomains(true, &world_regdom); |
09d989d1 LR |
1972 | restore_alpha2(alpha2, reset_user); |
1973 | ||
14609555 LR |
1974 | /* |
1975 | * If there's any pending requests we simply | |
1976 | * stash them to a temporary pending queue and | |
1977 | * add then after we've restored regulatory | |
1978 | * settings. | |
1979 | */ | |
1980 | spin_lock(®_requests_lock); | |
fea9bced JB |
1981 | list_for_each_entry_safe(reg_request, tmp, ®_requests_list, list) { |
1982 | if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER) | |
1983 | continue; | |
1984 | list_move_tail(®_request->list, &tmp_reg_req_list); | |
14609555 LR |
1985 | } |
1986 | spin_unlock(®_requests_lock); | |
1987 | ||
09d989d1 LR |
1988 | /* Clear beacon hints */ |
1989 | spin_lock_bh(®_pending_beacons_lock); | |
fea9bced JB |
1990 | list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) { |
1991 | list_del(®_beacon->list); | |
1992 | kfree(reg_beacon); | |
09d989d1 LR |
1993 | } |
1994 | spin_unlock_bh(®_pending_beacons_lock); | |
1995 | ||
fea9bced JB |
1996 | list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) { |
1997 | list_del(®_beacon->list); | |
1998 | kfree(reg_beacon); | |
09d989d1 LR |
1999 | } |
2000 | ||
2001 | /* First restore to the basic regulatory settings */ | |
379b82f4 JB |
2002 | world_alpha2[0] = cfg80211_world_regdom->alpha2[0]; |
2003 | world_alpha2[1] = cfg80211_world_regdom->alpha2[1]; | |
09d989d1 | 2004 | |
5ce543d1 | 2005 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) { |
a2f73b6c | 2006 | if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG) |
5ce543d1 RM |
2007 | restore_custom_reg_settings(&rdev->wiphy); |
2008 | } | |
2009 | ||
cee0bec5 | 2010 | regulatory_hint_core(world_alpha2); |
09d989d1 LR |
2011 | |
2012 | /* | |
2013 | * This restores the ieee80211_regdom module parameter | |
2014 | * preference or the last user requested regulatory | |
2015 | * settings, user regulatory settings takes precedence. | |
2016 | */ | |
2017 | if (is_an_alpha2(alpha2)) | |
57b5ce07 | 2018 | regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER); |
09d989d1 | 2019 | |
14609555 | 2020 | spin_lock(®_requests_lock); |
11cff96c | 2021 | list_splice_tail_init(&tmp_reg_req_list, ®_requests_list); |
14609555 LR |
2022 | spin_unlock(®_requests_lock); |
2023 | ||
14609555 LR |
2024 | REG_DBG_PRINT("Kicking the queue\n"); |
2025 | ||
2026 | schedule_work(®_work); | |
2027 | } | |
09d989d1 LR |
2028 | |
2029 | void regulatory_hint_disconnect(void) | |
2030 | { | |
1a919318 | 2031 | REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings\n"); |
09d989d1 LR |
2032 | restore_regulatory_settings(false); |
2033 | } | |
2034 | ||
e38f8a7a LR |
2035 | static bool freq_is_chan_12_13_14(u16 freq) |
2036 | { | |
59eb21a6 BR |
2037 | if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) || |
2038 | freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) || | |
2039 | freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ)) | |
e38f8a7a LR |
2040 | return true; |
2041 | return false; | |
2042 | } | |
2043 | ||
3ebfa6e7 LR |
2044 | static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan) |
2045 | { | |
2046 | struct reg_beacon *pending_beacon; | |
2047 | ||
2048 | list_for_each_entry(pending_beacon, ®_pending_beacons, list) | |
2049 | if (beacon_chan->center_freq == | |
2050 | pending_beacon->chan.center_freq) | |
2051 | return true; | |
2052 | return false; | |
2053 | } | |
2054 | ||
e38f8a7a LR |
2055 | int regulatory_hint_found_beacon(struct wiphy *wiphy, |
2056 | struct ieee80211_channel *beacon_chan, | |
2057 | gfp_t gfp) | |
2058 | { | |
2059 | struct reg_beacon *reg_beacon; | |
3ebfa6e7 | 2060 | bool processing; |
e38f8a7a | 2061 | |
1a919318 JB |
2062 | if (beacon_chan->beacon_found || |
2063 | beacon_chan->flags & IEEE80211_CHAN_RADAR || | |
e38f8a7a | 2064 | (beacon_chan->band == IEEE80211_BAND_2GHZ && |
1a919318 | 2065 | !freq_is_chan_12_13_14(beacon_chan->center_freq))) |
e38f8a7a LR |
2066 | return 0; |
2067 | ||
3ebfa6e7 LR |
2068 | spin_lock_bh(®_pending_beacons_lock); |
2069 | processing = pending_reg_beacon(beacon_chan); | |
2070 | spin_unlock_bh(®_pending_beacons_lock); | |
2071 | ||
2072 | if (processing) | |
e38f8a7a LR |
2073 | return 0; |
2074 | ||
2075 | reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp); | |
2076 | if (!reg_beacon) | |
2077 | return -ENOMEM; | |
2078 | ||
1a919318 | 2079 | REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s\n", |
4113f751 LR |
2080 | beacon_chan->center_freq, |
2081 | ieee80211_frequency_to_channel(beacon_chan->center_freq), | |
2082 | wiphy_name(wiphy)); | |
2083 | ||
e38f8a7a | 2084 | memcpy(®_beacon->chan, beacon_chan, |
1a919318 | 2085 | sizeof(struct ieee80211_channel)); |
e38f8a7a LR |
2086 | |
2087 | /* | |
2088 | * Since we can be called from BH or and non-BH context | |
2089 | * we must use spin_lock_bh() | |
2090 | */ | |
2091 | spin_lock_bh(®_pending_beacons_lock); | |
2092 | list_add_tail(®_beacon->list, ®_pending_beacons); | |
2093 | spin_unlock_bh(®_pending_beacons_lock); | |
2094 | ||
2095 | schedule_work(®_work); | |
2096 | ||
2097 | return 0; | |
2098 | } | |
2099 | ||
a3d2eaf0 | 2100 | static void print_rd_rules(const struct ieee80211_regdomain *rd) |
b2e1b302 LR |
2101 | { |
2102 | unsigned int i; | |
a3d2eaf0 JB |
2103 | const struct ieee80211_reg_rule *reg_rule = NULL; |
2104 | const struct ieee80211_freq_range *freq_range = NULL; | |
2105 | const struct ieee80211_power_rule *power_rule = NULL; | |
b2e1b302 | 2106 | |
6653325a | 2107 | pr_info(" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n"); |
b2e1b302 LR |
2108 | |
2109 | for (i = 0; i < rd->n_reg_rules; i++) { | |
2110 | reg_rule = &rd->reg_rules[i]; | |
2111 | freq_range = ®_rule->freq_range; | |
2112 | power_rule = ®_rule->power_rule; | |
2113 | ||
fb1fc7ad LR |
2114 | /* |
2115 | * There may not be documentation for max antenna gain | |
2116 | * in certain regions | |
2117 | */ | |
b2e1b302 | 2118 | if (power_rule->max_antenna_gain) |
6653325a | 2119 | pr_info(" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n", |
b2e1b302 LR |
2120 | freq_range->start_freq_khz, |
2121 | freq_range->end_freq_khz, | |
2122 | freq_range->max_bandwidth_khz, | |
2123 | power_rule->max_antenna_gain, | |
2124 | power_rule->max_eirp); | |
2125 | else | |
6653325a | 2126 | pr_info(" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n", |
b2e1b302 LR |
2127 | freq_range->start_freq_khz, |
2128 | freq_range->end_freq_khz, | |
2129 | freq_range->max_bandwidth_khz, | |
2130 | power_rule->max_eirp); | |
2131 | } | |
2132 | } | |
2133 | ||
4c7d3982 | 2134 | bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region) |
8b60b078 LR |
2135 | { |
2136 | switch (dfs_region) { | |
2137 | case NL80211_DFS_UNSET: | |
2138 | case NL80211_DFS_FCC: | |
2139 | case NL80211_DFS_ETSI: | |
2140 | case NL80211_DFS_JP: | |
2141 | return true; | |
2142 | default: | |
2143 | REG_DBG_PRINT("Ignoring uknown DFS master region: %d\n", | |
2144 | dfs_region); | |
2145 | return false; | |
2146 | } | |
2147 | } | |
2148 | ||
4c7d3982 | 2149 | static void print_dfs_region(enum nl80211_dfs_regions dfs_region) |
8b60b078 LR |
2150 | { |
2151 | if (!dfs_region) | |
2152 | return; | |
2153 | ||
2154 | switch (dfs_region) { | |
d7a4b05a LR |
2155 | case NL80211_DFS_UNSET: |
2156 | pr_info(" DFS Master region unset"); | |
2157 | break; | |
8b60b078 LR |
2158 | case NL80211_DFS_FCC: |
2159 | pr_info(" DFS Master region FCC"); | |
2160 | break; | |
2161 | case NL80211_DFS_ETSI: | |
2162 | pr_info(" DFS Master region ETSI"); | |
2163 | break; | |
2164 | case NL80211_DFS_JP: | |
2165 | pr_info(" DFS Master region JP"); | |
2166 | break; | |
2167 | default: | |
1a919318 | 2168 | pr_info(" DFS Master region Unknown"); |
8b60b078 LR |
2169 | break; |
2170 | } | |
2171 | } | |
2172 | ||
a3d2eaf0 | 2173 | static void print_regdomain(const struct ieee80211_regdomain *rd) |
b2e1b302 | 2174 | { |
c492db37 | 2175 | struct regulatory_request *lr = get_last_request(); |
b2e1b302 | 2176 | |
3f2355cb | 2177 | if (is_intersected_alpha2(rd->alpha2)) { |
c492db37 | 2178 | if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) { |
79c97e97 | 2179 | struct cfg80211_registered_device *rdev; |
c492db37 | 2180 | rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx); |
79c97e97 | 2181 | if (rdev) { |
e9c0268f | 2182 | pr_info("Current regulatory domain updated by AP to: %c%c\n", |
79c97e97 JB |
2183 | rdev->country_ie_alpha2[0], |
2184 | rdev->country_ie_alpha2[1]); | |
3f2355cb | 2185 | } else |
e9c0268f | 2186 | pr_info("Current regulatory domain intersected:\n"); |
3f2355cb | 2187 | } else |
e9c0268f | 2188 | pr_info("Current regulatory domain intersected:\n"); |
1a919318 | 2189 | } else if (is_world_regdom(rd->alpha2)) { |
e9c0268f | 2190 | pr_info("World regulatory domain updated:\n"); |
1a919318 | 2191 | } else { |
b2e1b302 | 2192 | if (is_unknown_alpha2(rd->alpha2)) |
e9c0268f | 2193 | pr_info("Regulatory domain changed to driver built-in settings (unknown country)\n"); |
57b5ce07 | 2194 | else { |
c492db37 | 2195 | if (reg_request_cell_base(lr)) |
1a919318 | 2196 | pr_info("Regulatory domain changed to country: %c%c by Cell Station\n", |
57b5ce07 LR |
2197 | rd->alpha2[0], rd->alpha2[1]); |
2198 | else | |
1a919318 | 2199 | pr_info("Regulatory domain changed to country: %c%c\n", |
57b5ce07 LR |
2200 | rd->alpha2[0], rd->alpha2[1]); |
2201 | } | |
b2e1b302 | 2202 | } |
1a919318 | 2203 | |
8b60b078 | 2204 | print_dfs_region(rd->dfs_region); |
b2e1b302 LR |
2205 | print_rd_rules(rd); |
2206 | } | |
2207 | ||
2df78167 | 2208 | static void print_regdomain_info(const struct ieee80211_regdomain *rd) |
b2e1b302 | 2209 | { |
e9c0268f | 2210 | pr_info("Regulatory domain: %c%c\n", rd->alpha2[0], rd->alpha2[1]); |
b2e1b302 LR |
2211 | print_rd_rules(rd); |
2212 | } | |
2213 | ||
3b9e5aca LR |
2214 | static int reg_set_rd_core(const struct ieee80211_regdomain *rd) |
2215 | { | |
2216 | if (!is_world_regdom(rd->alpha2)) | |
2217 | return -EINVAL; | |
2218 | update_world_regdomain(rd); | |
2219 | return 0; | |
2220 | } | |
2221 | ||
84721d44 LR |
2222 | static int reg_set_rd_user(const struct ieee80211_regdomain *rd, |
2223 | struct regulatory_request *user_request) | |
2224 | { | |
2225 | const struct ieee80211_regdomain *intersected_rd = NULL; | |
2226 | ||
2227 | if (is_world_regdom(rd->alpha2)) | |
2228 | return -EINVAL; | |
2229 | ||
2230 | if (!regdom_changes(rd->alpha2)) | |
2231 | return -EALREADY; | |
2232 | ||
2233 | if (!is_valid_rd(rd)) { | |
2234 | pr_err("Invalid regulatory domain detected:\n"); | |
2235 | print_regdomain_info(rd); | |
2236 | return -EINVAL; | |
2237 | } | |
2238 | ||
2239 | if (!user_request->intersect) { | |
2240 | reset_regdomains(false, rd); | |
2241 | return 0; | |
2242 | } | |
2243 | ||
2244 | intersected_rd = regdom_intersect(rd, get_cfg80211_regdom()); | |
2245 | if (!intersected_rd) | |
2246 | return -EINVAL; | |
2247 | ||
2248 | kfree(rd); | |
2249 | rd = NULL; | |
2250 | reset_regdomains(false, intersected_rd); | |
2251 | ||
2252 | return 0; | |
2253 | } | |
2254 | ||
f5fe3247 LR |
2255 | static int reg_set_rd_driver(const struct ieee80211_regdomain *rd, |
2256 | struct regulatory_request *driver_request) | |
b2e1b302 | 2257 | { |
e9763c3c | 2258 | const struct ieee80211_regdomain *regd; |
9c96477d | 2259 | const struct ieee80211_regdomain *intersected_rd = NULL; |
f5fe3247 | 2260 | const struct ieee80211_regdomain *tmp; |
806a9e39 | 2261 | struct wiphy *request_wiphy; |
6913b49a | 2262 | |
f5fe3247 | 2263 | if (is_world_regdom(rd->alpha2)) |
b2e1b302 LR |
2264 | return -EINVAL; |
2265 | ||
f5fe3247 LR |
2266 | if (!regdom_changes(rd->alpha2)) |
2267 | return -EALREADY; | |
b2e1b302 | 2268 | |
8375af3b | 2269 | if (!is_valid_rd(rd)) { |
e9c0268f | 2270 | pr_err("Invalid regulatory domain detected:\n"); |
8375af3b LR |
2271 | print_regdomain_info(rd); |
2272 | return -EINVAL; | |
b2e1b302 LR |
2273 | } |
2274 | ||
f5fe3247 LR |
2275 | request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx); |
2276 | if (!request_wiphy) { | |
0bac71af | 2277 | schedule_delayed_work(®_timeout, 0); |
de3584bd JB |
2278 | return -ENODEV; |
2279 | } | |
806a9e39 | 2280 | |
f5fe3247 | 2281 | if (!driver_request->intersect) { |
558f6d32 LR |
2282 | if (request_wiphy->regd) |
2283 | return -EALREADY; | |
3e0c3ff3 | 2284 | |
e9763c3c JB |
2285 | regd = reg_copy_regd(rd); |
2286 | if (IS_ERR(regd)) | |
2287 | return PTR_ERR(regd); | |
3e0c3ff3 | 2288 | |
458f4f9e | 2289 | rcu_assign_pointer(request_wiphy->regd, regd); |
379b82f4 | 2290 | reset_regdomains(false, rd); |
b8295acd LR |
2291 | return 0; |
2292 | } | |
2293 | ||
f5fe3247 LR |
2294 | intersected_rd = regdom_intersect(rd, get_cfg80211_regdom()); |
2295 | if (!intersected_rd) | |
2296 | return -EINVAL; | |
b8295acd | 2297 | |
f5fe3247 LR |
2298 | /* |
2299 | * We can trash what CRDA provided now. | |
2300 | * However if a driver requested this specific regulatory | |
2301 | * domain we keep it for its private use | |
2302 | */ | |
2303 | tmp = get_wiphy_regdom(request_wiphy); | |
2304 | rcu_assign_pointer(request_wiphy->regd, rd); | |
2305 | rcu_free_regdom(tmp); | |
b8295acd | 2306 | |
f5fe3247 | 2307 | rd = NULL; |
b7566fc3 | 2308 | |
f5fe3247 | 2309 | reset_regdomains(false, intersected_rd); |
3e0c3ff3 | 2310 | |
f5fe3247 LR |
2311 | return 0; |
2312 | } | |
2313 | ||
01992406 LR |
2314 | static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd, |
2315 | struct regulatory_request *country_ie_request) | |
f5fe3247 LR |
2316 | { |
2317 | struct wiphy *request_wiphy; | |
b8295acd | 2318 | |
f5fe3247 LR |
2319 | if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && |
2320 | !is_unknown_alpha2(rd->alpha2)) | |
2321 | return -EINVAL; | |
b8295acd | 2322 | |
f5fe3247 LR |
2323 | /* |
2324 | * Lets only bother proceeding on the same alpha2 if the current | |
2325 | * rd is non static (it means CRDA was present and was used last) | |
2326 | * and the pending request came in from a country IE | |
2327 | */ | |
2328 | ||
2329 | if (!is_valid_rd(rd)) { | |
2330 | pr_err("Invalid regulatory domain detected:\n"); | |
2331 | print_regdomain_info(rd); | |
2332 | return -EINVAL; | |
9c96477d LR |
2333 | } |
2334 | ||
01992406 | 2335 | request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx); |
f5fe3247 LR |
2336 | if (!request_wiphy) { |
2337 | schedule_delayed_work(®_timeout, 0); | |
2338 | return -ENODEV; | |
2339 | } | |
b2e1b302 | 2340 | |
01992406 | 2341 | if (country_ie_request->intersect) |
f5fe3247 LR |
2342 | return -EINVAL; |
2343 | ||
2344 | reset_regdomains(false, rd); | |
2345 | return 0; | |
2346 | } | |
b2e1b302 | 2347 | |
fb1fc7ad LR |
2348 | /* |
2349 | * Use this call to set the current regulatory domain. Conflicts with | |
b2e1b302 | 2350 | * multiple drivers can be ironed out later. Caller must've already |
458f4f9e | 2351 | * kmalloc'd the rd structure. |
fb1fc7ad | 2352 | */ |
a3d2eaf0 | 2353 | int set_regdom(const struct ieee80211_regdomain *rd) |
b2e1b302 | 2354 | { |
c492db37 | 2355 | struct regulatory_request *lr; |
b2e1b302 LR |
2356 | int r; |
2357 | ||
3b9e5aca LR |
2358 | if (!reg_is_valid_request(rd->alpha2)) { |
2359 | kfree(rd); | |
2360 | return -EINVAL; | |
2361 | } | |
2362 | ||
c492db37 | 2363 | lr = get_last_request(); |
abc7381b | 2364 | |
b2e1b302 | 2365 | /* Note that this doesn't update the wiphys, this is done below */ |
3b9e5aca LR |
2366 | switch (lr->initiator) { |
2367 | case NL80211_REGDOM_SET_BY_CORE: | |
2368 | r = reg_set_rd_core(rd); | |
2369 | break; | |
2370 | case NL80211_REGDOM_SET_BY_USER: | |
84721d44 LR |
2371 | r = reg_set_rd_user(rd, lr); |
2372 | break; | |
3b9e5aca | 2373 | case NL80211_REGDOM_SET_BY_DRIVER: |
f5fe3247 LR |
2374 | r = reg_set_rd_driver(rd, lr); |
2375 | break; | |
3b9e5aca | 2376 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
01992406 | 2377 | r = reg_set_rd_country_ie(rd, lr); |
3b9e5aca LR |
2378 | break; |
2379 | default: | |
2380 | WARN(1, "invalid initiator %d\n", lr->initiator); | |
2381 | return -EINVAL; | |
2382 | } | |
2383 | ||
d2372b31 | 2384 | if (r) { |
95908535 KV |
2385 | if (r == -EALREADY) |
2386 | reg_set_request_processed(); | |
2387 | ||
d2372b31 | 2388 | kfree(rd); |
38fd2143 | 2389 | return r; |
d2372b31 | 2390 | } |
b2e1b302 | 2391 | |
b2e1b302 | 2392 | /* This would make this whole thing pointless */ |
38fd2143 JB |
2393 | if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom())) |
2394 | return -EINVAL; | |
b2e1b302 LR |
2395 | |
2396 | /* update all wiphys now with the new established regulatory domain */ | |
c492db37 | 2397 | update_all_wiphy_regulatory(lr->initiator); |
b2e1b302 | 2398 | |
458f4f9e | 2399 | print_regdomain(get_cfg80211_regdom()); |
b2e1b302 | 2400 | |
c492db37 | 2401 | nl80211_send_reg_change_event(lr); |
73d54c9e | 2402 | |
b2e253cf LR |
2403 | reg_set_request_processed(); |
2404 | ||
38fd2143 | 2405 | return 0; |
b2e1b302 LR |
2406 | } |
2407 | ||
4d9d88d1 SJR |
2408 | int reg_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
2409 | { | |
4a484cff JB |
2410 | struct regulatory_request *lr; |
2411 | u8 alpha2[2]; | |
2412 | bool add = false; | |
c492db37 | 2413 | |
4a484cff JB |
2414 | rcu_read_lock(); |
2415 | lr = get_last_request(); | |
c492db37 | 2416 | if (lr && !lr->processed) { |
4a484cff JB |
2417 | memcpy(alpha2, lr->alpha2, 2); |
2418 | add = true; | |
4d9d88d1 | 2419 | } |
4a484cff | 2420 | rcu_read_unlock(); |
4d9d88d1 | 2421 | |
4a484cff JB |
2422 | if (add) |
2423 | return add_uevent_var(env, "COUNTRY=%c%c", | |
2424 | alpha2[0], alpha2[1]); | |
4d9d88d1 SJR |
2425 | return 0; |
2426 | } | |
4d9d88d1 | 2427 | |
57b5ce07 LR |
2428 | void wiphy_regulatory_register(struct wiphy *wiphy) |
2429 | { | |
23df0b73 AN |
2430 | struct regulatory_request *lr; |
2431 | ||
57b5ce07 LR |
2432 | if (!reg_dev_ignore_cell_hint(wiphy)) |
2433 | reg_num_devs_support_basehint++; | |
2434 | ||
23df0b73 AN |
2435 | lr = get_last_request(); |
2436 | wiphy_update_regulatory(wiphy, lr->initiator); | |
57b5ce07 LR |
2437 | } |
2438 | ||
bfead080 | 2439 | void wiphy_regulatory_deregister(struct wiphy *wiphy) |
3f2355cb | 2440 | { |
0ad8acaf | 2441 | struct wiphy *request_wiphy = NULL; |
c492db37 | 2442 | struct regulatory_request *lr; |
761cf7ec | 2443 | |
c492db37 | 2444 | lr = get_last_request(); |
abc7381b | 2445 | |
57b5ce07 LR |
2446 | if (!reg_dev_ignore_cell_hint(wiphy)) |
2447 | reg_num_devs_support_basehint--; | |
2448 | ||
458f4f9e JB |
2449 | rcu_free_regdom(get_wiphy_regdom(wiphy)); |
2450 | rcu_assign_pointer(wiphy->regd, NULL); | |
0ef9ccdd | 2451 | |
c492db37 JB |
2452 | if (lr) |
2453 | request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx); | |
806a9e39 | 2454 | |
0ef9ccdd | 2455 | if (!request_wiphy || request_wiphy != wiphy) |
38fd2143 | 2456 | return; |
0ef9ccdd | 2457 | |
c492db37 JB |
2458 | lr->wiphy_idx = WIPHY_IDX_INVALID; |
2459 | lr->country_ie_env = ENVIRON_ANY; | |
3f2355cb LR |
2460 | } |
2461 | ||
a90c7a31 LR |
2462 | static void reg_timeout_work(struct work_struct *work) |
2463 | { | |
1a919318 | 2464 | REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n"); |
f77b86d7 | 2465 | rtnl_lock(); |
a90c7a31 | 2466 | restore_regulatory_settings(true); |
f77b86d7 | 2467 | rtnl_unlock(); |
a90c7a31 LR |
2468 | } |
2469 | ||
2fcc9f73 | 2470 | int __init regulatory_init(void) |
b2e1b302 | 2471 | { |
bcf4f99b | 2472 | int err = 0; |
734366de | 2473 | |
b2e1b302 LR |
2474 | reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0); |
2475 | if (IS_ERR(reg_pdev)) | |
2476 | return PTR_ERR(reg_pdev); | |
734366de | 2477 | |
4d9d88d1 SJR |
2478 | reg_pdev->dev.type = ®_device_type; |
2479 | ||
fe33eb39 | 2480 | spin_lock_init(®_requests_lock); |
e38f8a7a | 2481 | spin_lock_init(®_pending_beacons_lock); |
fe33eb39 | 2482 | |
80007efe LR |
2483 | reg_regdb_size_check(); |
2484 | ||
458f4f9e | 2485 | rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom); |
734366de | 2486 | |
09d989d1 LR |
2487 | user_alpha2[0] = '9'; |
2488 | user_alpha2[1] = '7'; | |
2489 | ||
ae9e4b0d | 2490 | /* We always try to get an update for the static regdomain */ |
458f4f9e | 2491 | err = regulatory_hint_core(cfg80211_world_regdom->alpha2); |
ba25c141 | 2492 | if (err) { |
bcf4f99b LR |
2493 | if (err == -ENOMEM) |
2494 | return err; | |
2495 | /* | |
2496 | * N.B. kobject_uevent_env() can fail mainly for when we're out | |
2497 | * memory which is handled and propagated appropriately above | |
2498 | * but it can also fail during a netlink_broadcast() or during | |
2499 | * early boot for call_usermodehelper(). For now treat these | |
2500 | * errors as non-fatal. | |
2501 | */ | |
e9c0268f | 2502 | pr_err("kobject_uevent_env() was unable to call CRDA during init\n"); |
bcf4f99b | 2503 | } |
734366de | 2504 | |
ae9e4b0d LR |
2505 | /* |
2506 | * Finally, if the user set the module parameter treat it | |
2507 | * as a user hint. | |
2508 | */ | |
2509 | if (!is_world_regdom(ieee80211_regdom)) | |
57b5ce07 LR |
2510 | regulatory_hint_user(ieee80211_regdom, |
2511 | NL80211_USER_REG_HINT_USER); | |
ae9e4b0d | 2512 | |
b2e1b302 LR |
2513 | return 0; |
2514 | } | |
2515 | ||
1a919318 | 2516 | void regulatory_exit(void) |
b2e1b302 | 2517 | { |
fe33eb39 | 2518 | struct regulatory_request *reg_request, *tmp; |
e38f8a7a | 2519 | struct reg_beacon *reg_beacon, *btmp; |
fe33eb39 LR |
2520 | |
2521 | cancel_work_sync(®_work); | |
a90c7a31 | 2522 | cancel_delayed_work_sync(®_timeout); |
fe33eb39 | 2523 | |
9027b149 | 2524 | /* Lock to suppress warnings */ |
38fd2143 | 2525 | rtnl_lock(); |
379b82f4 | 2526 | reset_regdomains(true, NULL); |
38fd2143 | 2527 | rtnl_unlock(); |
734366de | 2528 | |
58ebacc6 | 2529 | dev_set_uevent_suppress(®_pdev->dev, true); |
f6037d09 | 2530 | |
b2e1b302 | 2531 | platform_device_unregister(reg_pdev); |
734366de | 2532 | |
fea9bced JB |
2533 | list_for_each_entry_safe(reg_beacon, btmp, ®_pending_beacons, list) { |
2534 | list_del(®_beacon->list); | |
2535 | kfree(reg_beacon); | |
e38f8a7a | 2536 | } |
e38f8a7a | 2537 | |
fea9bced JB |
2538 | list_for_each_entry_safe(reg_beacon, btmp, ®_beacon_list, list) { |
2539 | list_del(®_beacon->list); | |
2540 | kfree(reg_beacon); | |
e38f8a7a LR |
2541 | } |
2542 | ||
fea9bced JB |
2543 | list_for_each_entry_safe(reg_request, tmp, ®_requests_list, list) { |
2544 | list_del(®_request->list); | |
2545 | kfree(reg_request); | |
fe33eb39 | 2546 | } |
8318d78a | 2547 | } |