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