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