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