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