blob: 87ea60d84c3c88fbe66058c1e17e71e203e141e9 [file] [log] [blame]
Johannes Berg8318d782008-01-24 19:38:38 +01001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
Johannes Berg8318d782008-01-24 19:38:38 +01006 *
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
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012/**
13 * DOC: Wireless regulatory infrastructure
Johannes Berg8318d782008-01-24 19:38:38 +010014 *
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 *
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070020 * 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 *
Johannes Berg8318d782008-01-24 19:38:38 +010034 */
35#include <linux/kernel.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070036#include <linux/list.h>
37#include <linux/random.h>
38#include <linux/nl80211.h>
39#include <linux/platform_device.h>
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070040#include <net/cfg80211.h>
Johannes Berg8318d782008-01-24 19:38:38 +010041#include "core.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070042#include "reg.h"
John W. Linville3b377ea2009-12-18 17:59:01 -050043#include "regdb.h"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040044#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010045
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070046/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020047static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020048
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070049/* To trigger userspace events */
50static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010051
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050052/*
53 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020054 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050055 * information to give us an alpha2
56 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080057const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020058
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050059/*
60 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080061 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050062 * what it thinks should apply for the same country
63 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080064static const struct ieee80211_regdomain *country_ie_regdomain;
65
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070066/*
67 * Protects static reg.c components:
68 * - cfg80211_world_regdom
69 * - cfg80211_regdom
70 * - country_ie_regdomain
71 * - last_request
72 */
73DEFINE_MUTEX(reg_mutex);
74#define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
75
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050076/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050077static LIST_HEAD(reg_requests_list);
78static spinlock_t reg_requests_lock;
79
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050080/* Used to queue up beacon hints for review */
81static LIST_HEAD(reg_pending_beacons);
82static spinlock_t reg_pending_beacons_lock;
83
84/* Used to keep track of processed beacon hints */
85static LIST_HEAD(reg_beacon_list);
86
87struct reg_beacon {
88 struct list_head list;
89 struct ieee80211_channel chan;
90};
91
Johannes Berg734366d2008-09-15 10:56:48 +020092/* We keep a static world regulatory domain in case of the absence of CRDA */
93static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080094 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +020095 .alpha2 = "00",
96 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -050097 /* IEEE 802.11b/g, channels 1..11 */
98 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080099 /* IEEE 802.11b/g, channels 12..13. No HT40
100 * channel fits here. */
101 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500102 NL80211_RRF_PASSIVE_SCAN |
103 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800104 /* 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 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800111 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800112 NL80211_RRF_PASSIVE_SCAN |
113 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500114
115 /* NB: 5260 MHz - 5700 MHz requies DFS */
116
117 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800118 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500119 NL80211_RRF_PASSIVE_SCAN |
120 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200121 }
122};
123
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200124static const struct ieee80211_regdomain *cfg80211_world_regdom =
125 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200126
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400127static char *ieee80211_regdom = "00";
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400128
Johannes Berg734366d2008-09-15 10:56:48 +0200129module_param(ieee80211_regdom, charp, 0444);
130MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
131
Johannes Berg734366d2008-09-15 10:56:48 +0200132static void reset_regdomains(void)
133{
Johannes Berg942b25c2008-09-15 11:26:47 +0200134 /* avoid freeing static information or freeing something twice */
135 if (cfg80211_regdomain == cfg80211_world_regdom)
136 cfg80211_regdomain = NULL;
137 if (cfg80211_world_regdom == &world_regdom)
138 cfg80211_world_regdom = NULL;
139 if (cfg80211_regdomain == &world_regdom)
140 cfg80211_regdomain = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200141
142 kfree(cfg80211_regdomain);
143 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200144
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200145 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200146 cfg80211_regdomain = NULL;
147}
148
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500149/*
150 * Dynamic world regulatory domain requested by the wireless
151 * core upon initialization
152 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200153static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200154{
Johannes Bergf6037d02008-10-21 11:01:33 +0200155 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200156
157 reset_regdomains();
158
159 cfg80211_world_regdom = rd;
160 cfg80211_regdomain = rd;
161}
Johannes Berg734366d2008-09-15 10:56:48 +0200162
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200163bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100164{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700165 if (!alpha2)
166 return false;
167 if (alpha2[0] == '0' && alpha2[1] == '0')
168 return true;
169 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100170}
171
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200172static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700173{
174 if (!alpha2)
175 return false;
176 if (alpha2[0] != 0 && alpha2[1] != 0)
177 return true;
178 return false;
179}
Johannes Berg8318d782008-01-24 19:38:38 +0100180
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700181static bool is_alpha_upper(char letter)
182{
183 /* ASCII A - Z */
184 if (letter >= 65 && letter <= 90)
185 return true;
186 return false;
187}
188
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200189static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700190{
191 if (!alpha2)
192 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500193 /*
194 * Special case where regulatory domain was built by driver
195 * but a specific alpha2 cannot be determined
196 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700197 if (alpha2[0] == '9' && alpha2[1] == '9')
198 return true;
199 return false;
200}
201
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800202static bool is_intersected_alpha2(const char *alpha2)
203{
204 if (!alpha2)
205 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500206 /*
207 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800208 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500209 * structures
210 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800211 if (alpha2[0] == '9' && alpha2[1] == '8')
212 return true;
213 return false;
214}
215
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200216static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700217{
218 if (!alpha2)
219 return false;
220 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
221 return true;
222 return false;
223}
224
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200225static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700226{
227 if (!alpha2_x || !alpha2_y)
228 return false;
229 if (alpha2_x[0] == alpha2_y[0] &&
230 alpha2_x[1] == alpha2_y[1])
231 return true;
232 return false;
233}
234
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500235static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700236{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500237 assert_cfg80211_lock();
238
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700239 if (!cfg80211_regdomain)
240 return true;
241 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
242 return false;
243 return true;
244}
245
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800246/**
247 * country_ie_integrity_changes - tells us if the country IE has changed
248 * @checksum: checksum of country IE of fields we are interested in
249 *
250 * If the country IE has not changed you can ignore it safely. This is
251 * useful to determine if two devices are seeing two different country IEs
252 * even on the same alpha2. Note that this will return false if no IE has
253 * been set on the wireless core yet.
254 */
255static bool country_ie_integrity_changes(u32 checksum)
256{
257 /* If no IE has been set then the checksum doesn't change */
258 if (unlikely(!last_request->country_ie_checksum))
259 return false;
260 if (unlikely(last_request->country_ie_checksum != checksum))
261 return true;
262 return false;
263}
264
John W. Linville3b377ea2009-12-18 17:59:01 -0500265static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
266 const struct ieee80211_regdomain *src_regd)
267{
268 struct ieee80211_regdomain *regd;
269 int size_of_regd = 0;
270 unsigned int i;
271
272 size_of_regd = sizeof(struct ieee80211_regdomain) +
273 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
274
275 regd = kzalloc(size_of_regd, GFP_KERNEL);
276 if (!regd)
277 return -ENOMEM;
278
279 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
280
281 for (i = 0; i < src_regd->n_reg_rules; i++)
282 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
283 sizeof(struct ieee80211_reg_rule));
284
285 *dst_regd = regd;
286 return 0;
287}
288
289#ifdef CONFIG_CFG80211_INTERNAL_REGDB
290struct reg_regdb_search_request {
291 char alpha2[2];
292 struct list_head list;
293};
294
295static LIST_HEAD(reg_regdb_search_list);
296static DEFINE_SPINLOCK(reg_regdb_search_lock);
297
298static void reg_regdb_search(struct work_struct *work)
299{
300 struct reg_regdb_search_request *request;
301 const struct ieee80211_regdomain *curdom, *regdom;
302 int i, r;
303
304 spin_lock(&reg_regdb_search_lock);
305 while (!list_empty(&reg_regdb_search_list)) {
306 request = list_first_entry(&reg_regdb_search_list,
307 struct reg_regdb_search_request,
308 list);
309 list_del(&request->list);
310
311 for (i=0; i<reg_regdb_size; i++) {
312 curdom = reg_regdb[i];
313
314 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
315 r = reg_copy_regd(&regdom, curdom);
316 if (r)
317 break;
318 spin_unlock(&reg_regdb_search_lock);
319 mutex_lock(&cfg80211_mutex);
320 set_regdom(regdom);
321 mutex_unlock(&cfg80211_mutex);
322 spin_lock(&reg_regdb_search_lock);
323 break;
324 }
325 }
326
327 kfree(request);
328 }
329 spin_unlock(&reg_regdb_search_lock);
330}
331
332static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
333
334static void reg_regdb_query(const char *alpha2)
335{
336 struct reg_regdb_search_request *request;
337
338 if (!alpha2)
339 return;
340
341 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
342 if (!request)
343 return;
344
345 memcpy(request->alpha2, alpha2, 2);
346
347 spin_lock(&reg_regdb_search_lock);
348 list_add_tail(&request->list, &reg_regdb_search_list);
349 spin_unlock(&reg_regdb_search_lock);
350
351 schedule_work(&reg_regdb_work);
352}
353#else
354static inline void reg_regdb_query(const char *alpha2) {}
355#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
356
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500357/*
358 * This lets us keep regulatory code which is updated on a regulatory
359 * basis in userspace.
360 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700361static int call_crda(const char *alpha2)
362{
363 char country_env[9 + 2] = "COUNTRY=";
364 char *envp[] = {
365 country_env,
366 NULL
367 };
368
369 if (!is_world_regdom((char *) alpha2))
370 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
371 alpha2[0], alpha2[1]);
372 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700373 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
374 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700375
John W. Linville3b377ea2009-12-18 17:59:01 -0500376 /* query internal regulatory database (if it exists) */
377 reg_regdb_query(alpha2);
378
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700379 country_env[8] = alpha2[0];
380 country_env[9] = alpha2[1];
381
382 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
383}
384
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700385/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200386bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700387{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400388 assert_cfg80211_lock();
389
Johannes Bergf6037d02008-10-21 11:01:33 +0200390 if (!last_request)
391 return false;
392
393 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700394}
395
396/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200397static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700398{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200399 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700400 u32 freq_diff;
401
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800402 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700403 return false;
404
405 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
406 return false;
407
408 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
409
Roel Kluinbd05f282009-03-03 22:55:21 +0100410 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
411 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700412 return false;
413
414 return true;
415}
416
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200417static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700418{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200419 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700420 unsigned int i;
421
422 if (!rd->n_reg_rules)
423 return false;
424
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800425 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
426 return false;
427
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700428 for (i = 0; i < rd->n_reg_rules; i++) {
429 reg_rule = &rd->reg_rules[i];
430 if (!is_valid_reg_rule(reg_rule))
431 return false;
432 }
433
434 return true;
435}
436
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400437static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
438 u32 center_freq_khz,
439 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700440{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400441 u32 start_freq_khz, end_freq_khz;
442
443 start_freq_khz = center_freq_khz - (bw_khz/2);
444 end_freq_khz = center_freq_khz + (bw_khz/2);
445
446 if (start_freq_khz >= freq_range->start_freq_khz &&
447 end_freq_khz <= freq_range->end_freq_khz)
448 return true;
449
450 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700451}
452
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800453/**
454 * freq_in_rule_band - tells us if a frequency is in a frequency band
455 * @freq_range: frequency rule we want to query
456 * @freq_khz: frequency we are inquiring about
457 *
458 * This lets us know if a specific frequency rule is or is not relevant to
459 * a specific frequency's band. Bands are device specific and artificial
460 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
461 * safe for now to assume that a frequency rule should not be part of a
462 * frequency's band if the start freq or end freq are off by more than 2 GHz.
463 * This resolution can be lowered and should be considered as we add
464 * regulatory rule support for other "bands".
465 **/
466static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
467 u32 freq_khz)
468{
469#define ONE_GHZ_IN_KHZ 1000000
470 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
471 return true;
472 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
473 return true;
474 return false;
475#undef ONE_GHZ_IN_KHZ
476}
477
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500478/*
479 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800480 * structure has a lot of information which the IE doesn't yet have,
481 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500482 * with our userspace regulatory agent to get lower bounds.
483 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800484static struct ieee80211_regdomain *country_ie_2_rd(
485 u8 *country_ie,
486 u8 country_ie_len,
487 u32 *checksum)
488{
489 struct ieee80211_regdomain *rd = NULL;
490 unsigned int i = 0;
491 char alpha2[2];
492 u32 flags = 0;
493 u32 num_rules = 0, size_of_regd = 0;
494 u8 *triplets_start = NULL;
495 u8 len_at_triplet = 0;
496 /* the last channel we have registered in a subband (triplet) */
497 int last_sub_max_channel = 0;
498
499 *checksum = 0xDEADBEEF;
500
501 /* Country IE requirements */
502 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
503 country_ie_len & 0x01);
504
505 alpha2[0] = country_ie[0];
506 alpha2[1] = country_ie[1];
507
508 /*
509 * Third octet can be:
510 * 'I' - Indoor
511 * 'O' - Outdoor
512 *
513 * anything else we assume is no restrictions
514 */
515 if (country_ie[2] == 'I')
516 flags = NL80211_RRF_NO_OUTDOOR;
517 else if (country_ie[2] == 'O')
518 flags = NL80211_RRF_NO_INDOOR;
519
520 country_ie += 3;
521 country_ie_len -= 3;
522
523 triplets_start = country_ie;
524 len_at_triplet = country_ie_len;
525
526 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
527
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500528 /*
529 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800530 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500531 * for each channel subband
532 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800533 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800534 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800535 struct ieee80211_country_ie_triplet *triplet =
536 (struct ieee80211_country_ie_triplet *) country_ie;
537 int cur_sub_max_channel = 0, cur_channel = 0;
538
539 if (triplet->ext.reg_extension_id >=
540 IEEE80211_COUNTRY_EXTENSION_ID) {
541 country_ie += 3;
542 country_ie_len -= 3;
543 continue;
544 }
545
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800546 /* 2 GHz */
547 if (triplet->chans.first_channel <= 14)
548 end_channel = triplet->chans.first_channel +
549 triplet->chans.num_channels;
550 else
551 /*
552 * 5 GHz -- For example in country IEs if the first
553 * channel given is 36 and the number of channels is 4
554 * then the individual channel numbers defined for the
555 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
556 * and not 36, 37, 38, 39.
557 *
558 * See: http://tinyurl.com/11d-clarification
559 */
560 end_channel = triplet->chans.first_channel +
561 (4 * (triplet->chans.num_channels - 1));
562
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800563 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800564 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800565
566 /* Basic sanity check */
567 if (cur_sub_max_channel < cur_channel)
568 return NULL;
569
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500570 /*
571 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800572 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500573 * increasing
574 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800575 if (last_sub_max_channel) {
576 if (cur_channel <= last_sub_max_channel)
577 return NULL;
578 if (cur_sub_max_channel <= last_sub_max_channel)
579 return NULL;
580 }
581
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500582 /*
583 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800584 * we can throw ext triplets as part of this soup,
585 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500586 * don't support them
587 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800588 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
589 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
590 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
591
592 last_sub_max_channel = cur_sub_max_channel;
593
594 country_ie += 3;
595 country_ie_len -= 3;
596 num_rules++;
597
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500598 /*
599 * Note: this is not a IEEE requirement but
600 * simply a memory requirement
601 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800602 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
603 return NULL;
604 }
605
606 country_ie = triplets_start;
607 country_ie_len = len_at_triplet;
608
609 size_of_regd = sizeof(struct ieee80211_regdomain) +
610 (num_rules * sizeof(struct ieee80211_reg_rule));
611
612 rd = kzalloc(size_of_regd, GFP_KERNEL);
613 if (!rd)
614 return NULL;
615
616 rd->n_reg_rules = num_rules;
617 rd->alpha2[0] = alpha2[0];
618 rd->alpha2[1] = alpha2[1];
619
620 /* This time around we fill in the rd */
621 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800622 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800623 struct ieee80211_country_ie_triplet *triplet =
624 (struct ieee80211_country_ie_triplet *) country_ie;
625 struct ieee80211_reg_rule *reg_rule = NULL;
626 struct ieee80211_freq_range *freq_range = NULL;
627 struct ieee80211_power_rule *power_rule = NULL;
628
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500629 /*
630 * Must parse if dot11RegulatoryClassesRequired is true,
631 * we don't support this yet
632 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800633 if (triplet->ext.reg_extension_id >=
634 IEEE80211_COUNTRY_EXTENSION_ID) {
635 country_ie += 3;
636 country_ie_len -= 3;
637 continue;
638 }
639
640 reg_rule = &rd->reg_rules[i];
641 freq_range = &reg_rule->freq_range;
642 power_rule = &reg_rule->power_rule;
643
644 reg_rule->flags = flags;
645
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800646 /* 2 GHz */
647 if (triplet->chans.first_channel <= 14)
648 end_channel = triplet->chans.first_channel +
649 triplet->chans.num_channels;
650 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800651 end_channel = triplet->chans.first_channel +
652 (4 * (triplet->chans.num_channels - 1));
653
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500654 /*
655 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800656 * the actual band edge, not the center of freq for
657 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500658 * the channels passed
659 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800660 freq_range->start_freq_khz =
661 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
662 triplet->chans.first_channel) - 10);
663 freq_range->end_freq_khz =
664 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800665 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800666
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500667 /*
668 * These are large arbitrary values we use to intersect later.
669 * Increment this if we ever support >= 40 MHz channels
670 * in IEEE 802.11
671 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800672 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
673 power_rule->max_antenna_gain = DBI_TO_MBI(100);
674 power_rule->max_eirp = DBM_TO_MBM(100);
675
676 country_ie += 3;
677 country_ie_len -= 3;
678 i++;
679
680 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
681 }
682
683 return rd;
684}
685
686
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500687/*
688 * Helper for regdom_intersect(), this does the real
689 * mathematical intersection fun
690 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700691static int reg_rules_intersect(
692 const struct ieee80211_reg_rule *rule1,
693 const struct ieee80211_reg_rule *rule2,
694 struct ieee80211_reg_rule *intersected_rule)
695{
696 const struct ieee80211_freq_range *freq_range1, *freq_range2;
697 struct ieee80211_freq_range *freq_range;
698 const struct ieee80211_power_rule *power_rule1, *power_rule2;
699 struct ieee80211_power_rule *power_rule;
700 u32 freq_diff;
701
702 freq_range1 = &rule1->freq_range;
703 freq_range2 = &rule2->freq_range;
704 freq_range = &intersected_rule->freq_range;
705
706 power_rule1 = &rule1->power_rule;
707 power_rule2 = &rule2->power_rule;
708 power_rule = &intersected_rule->power_rule;
709
710 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
711 freq_range2->start_freq_khz);
712 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
713 freq_range2->end_freq_khz);
714 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
715 freq_range2->max_bandwidth_khz);
716
717 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
718 if (freq_range->max_bandwidth_khz > freq_diff)
719 freq_range->max_bandwidth_khz = freq_diff;
720
721 power_rule->max_eirp = min(power_rule1->max_eirp,
722 power_rule2->max_eirp);
723 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
724 power_rule2->max_antenna_gain);
725
726 intersected_rule->flags = (rule1->flags | rule2->flags);
727
728 if (!is_valid_reg_rule(intersected_rule))
729 return -EINVAL;
730
731 return 0;
732}
733
734/**
735 * regdom_intersect - do the intersection between two regulatory domains
736 * @rd1: first regulatory domain
737 * @rd2: second regulatory domain
738 *
739 * Use this function to get the intersection between two regulatory domains.
740 * Once completed we will mark the alpha2 for the rd as intersected, "98",
741 * as no one single alpha2 can represent this regulatory domain.
742 *
743 * Returns a pointer to the regulatory domain structure which will hold the
744 * resulting intersection of rules between rd1 and rd2. We will
745 * kzalloc() this structure for you.
746 */
747static struct ieee80211_regdomain *regdom_intersect(
748 const struct ieee80211_regdomain *rd1,
749 const struct ieee80211_regdomain *rd2)
750{
751 int r, size_of_regd;
752 unsigned int x, y;
753 unsigned int num_rules = 0, rule_idx = 0;
754 const struct ieee80211_reg_rule *rule1, *rule2;
755 struct ieee80211_reg_rule *intersected_rule;
756 struct ieee80211_regdomain *rd;
757 /* This is just a dummy holder to help us count */
758 struct ieee80211_reg_rule irule;
759
760 /* Uses the stack temporarily for counter arithmetic */
761 intersected_rule = &irule;
762
763 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
764
765 if (!rd1 || !rd2)
766 return NULL;
767
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500768 /*
769 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700770 * build them. This is to so we can malloc() and free() a
771 * regdomain once. The reason we use reg_rules_intersect() here
772 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500773 * All rules that do check out OK are valid.
774 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700775
776 for (x = 0; x < rd1->n_reg_rules; x++) {
777 rule1 = &rd1->reg_rules[x];
778 for (y = 0; y < rd2->n_reg_rules; y++) {
779 rule2 = &rd2->reg_rules[y];
780 if (!reg_rules_intersect(rule1, rule2,
781 intersected_rule))
782 num_rules++;
783 memset(intersected_rule, 0,
784 sizeof(struct ieee80211_reg_rule));
785 }
786 }
787
788 if (!num_rules)
789 return NULL;
790
791 size_of_regd = sizeof(struct ieee80211_regdomain) +
792 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
793
794 rd = kzalloc(size_of_regd, GFP_KERNEL);
795 if (!rd)
796 return NULL;
797
798 for (x = 0; x < rd1->n_reg_rules; x++) {
799 rule1 = &rd1->reg_rules[x];
800 for (y = 0; y < rd2->n_reg_rules; y++) {
801 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500802 /*
803 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700804 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500805 * a memcpy()
806 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700807 intersected_rule = &rd->reg_rules[rule_idx];
808 r = reg_rules_intersect(rule1, rule2,
809 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500810 /*
811 * No need to memset here the intersected rule here as
812 * we're not using the stack anymore
813 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700814 if (r)
815 continue;
816 rule_idx++;
817 }
818 }
819
820 if (rule_idx != num_rules) {
821 kfree(rd);
822 return NULL;
823 }
824
825 rd->n_reg_rules = num_rules;
826 rd->alpha2[0] = '9';
827 rd->alpha2[1] = '8';
828
829 return rd;
830}
831
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500832/*
833 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
834 * want to just have the channel structure use these
835 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700836static u32 map_regdom_flags(u32 rd_flags)
837{
838 u32 channel_flags = 0;
839 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
840 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
841 if (rd_flags & NL80211_RRF_NO_IBSS)
842 channel_flags |= IEEE80211_CHAN_NO_IBSS;
843 if (rd_flags & NL80211_RRF_DFS)
844 channel_flags |= IEEE80211_CHAN_RADAR;
845 return channel_flags;
846}
847
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800848static int freq_reg_info_regd(struct wiphy *wiphy,
849 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400850 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800851 const struct ieee80211_reg_rule **reg_rule,
852 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100853{
854 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800855 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800856 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400857 bool bw_fits = false;
858
859 if (!desired_bw_khz)
860 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +0100861
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800862 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800863
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500864 /*
865 * Follow the driver's regulatory domain, if present, unless a country
866 * IE has been processed or a user wants to help complaince further
867 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400868 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
869 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800870 wiphy->regd)
871 regd = wiphy->regd;
872
873 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700874 return -EINVAL;
875
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800876 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700877 const struct ieee80211_reg_rule *rr;
878 const struct ieee80211_freq_range *fr = NULL;
879 const struct ieee80211_power_rule *pr = NULL;
880
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800881 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700882 fr = &rr->freq_range;
883 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800884
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500885 /*
886 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800887 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500888 * not overwrite it once found
889 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800890 if (!band_rule_found)
891 band_rule_found = freq_in_rule_band(fr, center_freq);
892
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400893 bw_fits = reg_does_bw_fit(fr,
894 center_freq,
895 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800896
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400897 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700898 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400899 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100900 }
901 }
902
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800903 if (!band_rule_found)
904 return -ERANGE;
905
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400906 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700907}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800908EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700909
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400910int freq_reg_info(struct wiphy *wiphy,
911 u32 center_freq,
912 u32 desired_bw_khz,
913 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800914{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400915 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400916 return freq_reg_info_regd(wiphy,
917 center_freq,
918 desired_bw_khz,
919 reg_rule,
920 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800921}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700922
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400923/*
924 * Note that right now we assume the desired channel bandwidth
925 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
926 * per channel, the primary and the extension channel). To support
927 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
928 * new ieee80211_channel.target_bw and re run the regulatory check
929 * on the wiphy with the target_bw specified. Then we can simply use
930 * that below for the desired_bw_khz below.
931 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800932static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
933 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700934{
935 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400936 u32 flags, bw_flags = 0;
937 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700938 const struct ieee80211_reg_rule *reg_rule = NULL;
939 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400940 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800941 struct ieee80211_supported_band *sband;
942 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500943 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800944
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500945 assert_cfg80211_lock();
946
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500947 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
948
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800949 sband = wiphy->bands[band];
950 BUG_ON(chan_idx >= sband->n_channels);
951 chan = &sband->channels[chan_idx];
952
953 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700954
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400955 r = freq_reg_info(wiphy,
956 MHZ_TO_KHZ(chan->center_freq),
957 desired_bw_khz,
958 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700959
960 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500961 /*
962 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800963 * with a frequency range on the center_freq's band, since
964 * IEEE-802.11 allows for a country IE to have a subset of the
965 * regulatory information provided in a country we ignore
966 * disabling the channel unless at least one reg rule was
967 * found on the center_freq's band. For details see this
968 * clarification:
969 *
970 * http://tinyurl.com/11d-clarification
971 */
972 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400973 last_request->initiator ==
974 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800975#ifdef CONFIG_CFG80211_REG_DEBUG
976 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
977 "intact on %s - no rule found in band on "
978 "Country IE\n",
979 chan->center_freq, wiphy_name(wiphy));
980#endif
981 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500982 /*
983 * In this case we know the country IE has at least one reg rule
984 * for the band so we respect its band definitions
985 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800986#ifdef CONFIG_CFG80211_REG_DEBUG
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400987 if (last_request->initiator ==
988 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800989 printk(KERN_DEBUG "cfg80211: Disabling "
990 "channel %d MHz on %s due to "
991 "Country IE\n",
992 chan->center_freq, wiphy_name(wiphy));
993#endif
994 flags |= IEEE80211_CHAN_DISABLED;
995 chan->flags = flags;
996 }
Johannes Berg8318d782008-01-24 19:38:38 +0100997 return;
998 }
999
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001000 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001001 freq_range = &reg_rule->freq_range;
1002
1003 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1004 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001005
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001006 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001007 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001008 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001009 /*
1010 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001011 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001012 * settings
1013 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001014 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001015 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001016 chan->max_antenna_gain = chan->orig_mag =
1017 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001018 chan->max_power = chan->orig_mpwr =
1019 (int) MBM_TO_DBM(power_rule->max_eirp);
1020 return;
1021 }
1022
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001023 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001024 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001025 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -04001026 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001027 chan->max_power = min(chan->orig_mpwr,
1028 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001029 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001030 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001031}
1032
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001033static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001034{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001035 unsigned int i;
1036 struct ieee80211_supported_band *sband;
1037
1038 BUG_ON(!wiphy->bands[band]);
1039 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001040
1041 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001042 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001043}
1044
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001045static bool ignore_reg_update(struct wiphy *wiphy,
1046 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001047{
1048 if (!last_request)
1049 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001050 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001051 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001052 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001053 /*
1054 * wiphy->regd will be set once the device has its own
1055 * desired regulatory domain set
1056 */
Johannes Berg5be83de2009-11-19 00:56:28 +01001057 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001058 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001059 return true;
1060 return false;
1061}
1062
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001063static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001064{
Johannes Berg79c97e92009-07-07 03:56:12 +02001065 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001066
Johannes Berg79c97e92009-07-07 03:56:12 +02001067 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1068 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001069}
1070
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001071static void handle_reg_beacon(struct wiphy *wiphy,
1072 unsigned int chan_idx,
1073 struct reg_beacon *reg_beacon)
1074{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001075 struct ieee80211_supported_band *sband;
1076 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001077 bool channel_changed = false;
1078 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001079
1080 assert_cfg80211_lock();
1081
1082 sband = wiphy->bands[reg_beacon->chan.band];
1083 chan = &sband->channels[chan_idx];
1084
1085 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1086 return;
1087
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001088 if (chan->beacon_found)
1089 return;
1090
1091 chan->beacon_found = true;
1092
Johannes Berg5be83de2009-11-19 00:56:28 +01001093 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001094 return;
1095
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001096 chan_before.center_freq = chan->center_freq;
1097 chan_before.flags = chan->flags;
1098
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001099 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001100 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001101 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001102 }
1103
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001104 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001105 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001106 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001107 }
1108
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001109 if (channel_changed)
1110 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001111}
1112
1113/*
1114 * Called when a scan on a wiphy finds a beacon on
1115 * new channel
1116 */
1117static void wiphy_update_new_beacon(struct wiphy *wiphy,
1118 struct reg_beacon *reg_beacon)
1119{
1120 unsigned int i;
1121 struct ieee80211_supported_band *sband;
1122
1123 assert_cfg80211_lock();
1124
1125 if (!wiphy->bands[reg_beacon->chan.band])
1126 return;
1127
1128 sband = wiphy->bands[reg_beacon->chan.band];
1129
1130 for (i = 0; i < sband->n_channels; i++)
1131 handle_reg_beacon(wiphy, i, reg_beacon);
1132}
1133
1134/*
1135 * Called upon reg changes or a new wiphy is added
1136 */
1137static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1138{
1139 unsigned int i;
1140 struct ieee80211_supported_band *sband;
1141 struct reg_beacon *reg_beacon;
1142
1143 assert_cfg80211_lock();
1144
1145 if (list_empty(&reg_beacon_list))
1146 return;
1147
1148 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1149 if (!wiphy->bands[reg_beacon->chan.band])
1150 continue;
1151 sband = wiphy->bands[reg_beacon->chan.band];
1152 for (i = 0; i < sband->n_channels; i++)
1153 handle_reg_beacon(wiphy, i, reg_beacon);
1154 }
1155}
1156
1157static bool reg_is_world_roaming(struct wiphy *wiphy)
1158{
1159 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1160 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1161 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001162 if (last_request &&
1163 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001164 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001165 return true;
1166 return false;
1167}
1168
1169/* Reap the advantages of previously found beacons */
1170static void reg_process_beacons(struct wiphy *wiphy)
1171{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001172 /*
1173 * Means we are just firing up cfg80211, so no beacons would
1174 * have been processed yet.
1175 */
1176 if (!last_request)
1177 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001178 if (!reg_is_world_roaming(wiphy))
1179 return;
1180 wiphy_update_beacon_reg(wiphy);
1181}
1182
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001183static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1184{
1185 if (!chan)
1186 return true;
1187 if (chan->flags & IEEE80211_CHAN_DISABLED)
1188 return true;
1189 /* This would happen when regulatory rules disallow HT40 completely */
1190 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1191 return true;
1192 return false;
1193}
1194
1195static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1196 enum ieee80211_band band,
1197 unsigned int chan_idx)
1198{
1199 struct ieee80211_supported_band *sband;
1200 struct ieee80211_channel *channel;
1201 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1202 unsigned int i;
1203
1204 assert_cfg80211_lock();
1205
1206 sband = wiphy->bands[band];
1207 BUG_ON(chan_idx >= sband->n_channels);
1208 channel = &sband->channels[chan_idx];
1209
1210 if (is_ht40_not_allowed(channel)) {
1211 channel->flags |= IEEE80211_CHAN_NO_HT40;
1212 return;
1213 }
1214
1215 /*
1216 * We need to ensure the extension channels exist to
1217 * be able to use HT40- or HT40+, this finds them (or not)
1218 */
1219 for (i = 0; i < sband->n_channels; i++) {
1220 struct ieee80211_channel *c = &sband->channels[i];
1221 if (c->center_freq == (channel->center_freq - 20))
1222 channel_before = c;
1223 if (c->center_freq == (channel->center_freq + 20))
1224 channel_after = c;
1225 }
1226
1227 /*
1228 * Please note that this assumes target bandwidth is 20 MHz,
1229 * if that ever changes we also need to change the below logic
1230 * to include that as well.
1231 */
1232 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001233 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001234 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001235 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001236
1237 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001238 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001239 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001240 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001241}
1242
1243static void reg_process_ht_flags_band(struct wiphy *wiphy,
1244 enum ieee80211_band band)
1245{
1246 unsigned int i;
1247 struct ieee80211_supported_band *sband;
1248
1249 BUG_ON(!wiphy->bands[band]);
1250 sband = wiphy->bands[band];
1251
1252 for (i = 0; i < sband->n_channels; i++)
1253 reg_process_ht_flags_channel(wiphy, band, i);
1254}
1255
1256static void reg_process_ht_flags(struct wiphy *wiphy)
1257{
1258 enum ieee80211_band band;
1259
1260 if (!wiphy)
1261 return;
1262
1263 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1264 if (wiphy->bands[band])
1265 reg_process_ht_flags_band(wiphy, band);
1266 }
1267
1268}
1269
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001270void wiphy_update_regulatory(struct wiphy *wiphy,
1271 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001272{
1273 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001274
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001275 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001276 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001277 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001278 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001279 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001280 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001281out:
1282 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001283 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001284 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001285 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001286}
1287
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001288static void handle_channel_custom(struct wiphy *wiphy,
1289 enum ieee80211_band band,
1290 unsigned int chan_idx,
1291 const struct ieee80211_regdomain *regd)
1292{
1293 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001294 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1295 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001296 const struct ieee80211_reg_rule *reg_rule = NULL;
1297 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001298 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001299 struct ieee80211_supported_band *sband;
1300 struct ieee80211_channel *chan;
1301
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001302 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001303
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001304 sband = wiphy->bands[band];
1305 BUG_ON(chan_idx >= sband->n_channels);
1306 chan = &sband->channels[chan_idx];
1307
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001308 r = freq_reg_info_regd(wiphy,
1309 MHZ_TO_KHZ(chan->center_freq),
1310 desired_bw_khz,
1311 &reg_rule,
1312 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001313
1314 if (r) {
1315 chan->flags = IEEE80211_CHAN_DISABLED;
1316 return;
1317 }
1318
1319 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001320 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001321
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001322 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1323 bw_flags = IEEE80211_CHAN_NO_HT40;
1324
1325 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001326 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001327 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1328}
1329
1330static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1331 const struct ieee80211_regdomain *regd)
1332{
1333 unsigned int i;
1334 struct ieee80211_supported_band *sband;
1335
1336 BUG_ON(!wiphy->bands[band]);
1337 sband = wiphy->bands[band];
1338
1339 for (i = 0; i < sband->n_channels; i++)
1340 handle_channel_custom(wiphy, band, i, regd);
1341}
1342
1343/* Used by drivers prior to wiphy registration */
1344void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1345 const struct ieee80211_regdomain *regd)
1346{
1347 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001348 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001349
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001350 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001351 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001352 if (!wiphy->bands[band])
1353 continue;
1354 handle_band_custom(wiphy, band, regd);
1355 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001356 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001357 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001358
1359 /*
1360 * no point in calling this if it won't have any effect
1361 * on your device's supportd bands.
1362 */
1363 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001364}
1365EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1366
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001367/*
1368 * Return value which can be used by ignore_request() to indicate
1369 * it has been determined we should intersect two regulatory domains
1370 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001371#define REG_INTERSECT 1
1372
Johannes Berg84fa4f42008-10-24 20:32:23 +02001373/* This has the logic which determines when a new request
1374 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001375static int ignore_request(struct wiphy *wiphy,
1376 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001377{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001378 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001379
1380 assert_cfg80211_lock();
1381
Johannes Berg84fa4f42008-10-24 20:32:23 +02001382 /* All initial requests are respected */
1383 if (!last_request)
1384 return 0;
1385
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001386 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001387 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001388 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001389 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001390
1391 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1392
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001393 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001394 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001395 if (last_request->initiator ==
1396 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001397 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001398 /*
1399 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001400 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001401 * intersect them, but that seems unlikely
1402 * to be correct. Reject second one for now.
1403 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001404 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001405 return -EOPNOTSUPP;
1406 return -EALREADY;
1407 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001408 /*
1409 * Two consecutive Country IE hints on the same wiphy.
1410 * This should be picked up early by the driver/stack
1411 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001412 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001413 return 0;
1414 return -EALREADY;
1415 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001416 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001417 case NL80211_REGDOM_SET_BY_DRIVER:
1418 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001419 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001420 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001421 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001422 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001423
1424 /*
1425 * This would happen if you unplug and plug your card
1426 * back in or if you add a new device for which the previously
1427 * loaded card also agrees on the regulatory domain.
1428 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001429 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001430 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001431 return -EALREADY;
1432
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001433 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001434 case NL80211_REGDOM_SET_BY_USER:
1435 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001436 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001437 /*
1438 * If the user knows better the user should set the regdom
1439 * to their country before the IE is picked up
1440 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001441 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001442 last_request->intersect)
1443 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001444 /*
1445 * Process user requests only after previous user/driver/core
1446 * requests have been processed
1447 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001448 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1449 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1450 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001451 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001452 return -EAGAIN;
1453 }
1454
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001455 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001456 return -EALREADY;
1457
Johannes Berg84fa4f42008-10-24 20:32:23 +02001458 return 0;
1459 }
1460
1461 return -EINVAL;
1462}
1463
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001464/**
1465 * __regulatory_hint - hint to the wireless core a regulatory domain
1466 * @wiphy: if the hint comes from country information from an AP, this
1467 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001468 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001469 *
1470 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001471 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001472 *
1473 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1474 * already been set or other standard error codes.
1475 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001476 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001477 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001478static int __regulatory_hint(struct wiphy *wiphy,
1479 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001480{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001481 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001482 int r = 0;
1483
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001484 assert_cfg80211_lock();
1485
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001486 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001487
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001488 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001489 if (pending_request->initiator ==
1490 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001491 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001492 if (r) {
1493 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001494 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001495 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001496 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001497 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001498 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001499 /*
1500 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001501 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001502 * wiphy
1503 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001504 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001505 pending_request->initiator ==
1506 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001507 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001508 if (r) {
1509 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001510 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001511 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001512 r = -EALREADY;
1513 goto new_request;
1514 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001515 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001516 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001517 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001518
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001519new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001520 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001521
1522 last_request = pending_request;
1523 last_request->intersect = intersect;
1524
1525 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001526
1527 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001528 if (r < 0) {
1529 /*
1530 * Since CRDA will not be called in this case as we already
1531 * have applied the requested regulatory domain before we just
1532 * inform userspace we have processed the request
1533 */
1534 if (r == -EALREADY)
1535 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001536 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001537 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001538
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001539 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001540}
1541
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001542/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001543static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001544{
1545 int r = 0;
1546 struct wiphy *wiphy = NULL;
1547
1548 BUG_ON(!reg_request->alpha2);
1549
1550 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001551 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001552
1553 if (wiphy_idx_valid(reg_request->wiphy_idx))
1554 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1555
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001556 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001557 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001558 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001559 goto out;
1560 }
1561
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001562 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001563 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001564 if (r == -EALREADY && wiphy &&
1565 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001566 wiphy_update_regulatory(wiphy, reg_request->initiator);
1567out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001568 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001569 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001570}
1571
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001572/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001573static void reg_process_pending_hints(void)
1574 {
1575 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001576
1577 spin_lock(&reg_requests_lock);
1578 while (!list_empty(&reg_requests_list)) {
1579 reg_request = list_first_entry(&reg_requests_list,
1580 struct regulatory_request,
1581 list);
1582 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001583
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001584 spin_unlock(&reg_requests_lock);
1585 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001586 spin_lock(&reg_requests_lock);
1587 }
1588 spin_unlock(&reg_requests_lock);
1589}
1590
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001591/* Processes beacon hints -- this has nothing to do with country IEs */
1592static void reg_process_pending_beacon_hints(void)
1593{
Johannes Berg79c97e92009-07-07 03:56:12 +02001594 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001595 struct reg_beacon *pending_beacon, *tmp;
1596
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001597 /*
1598 * No need to hold the reg_mutex here as we just touch wiphys
1599 * and do not read or access regulatory variables.
1600 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001601 mutex_lock(&cfg80211_mutex);
1602
1603 /* This goes through the _pending_ beacon list */
1604 spin_lock_bh(&reg_pending_beacons_lock);
1605
1606 if (list_empty(&reg_pending_beacons)) {
1607 spin_unlock_bh(&reg_pending_beacons_lock);
1608 goto out;
1609 }
1610
1611 list_for_each_entry_safe(pending_beacon, tmp,
1612 &reg_pending_beacons, list) {
1613
1614 list_del_init(&pending_beacon->list);
1615
1616 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001617 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1618 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001619
1620 /* Remembers the beacon hint for new wiphys or reg changes */
1621 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1622 }
1623
1624 spin_unlock_bh(&reg_pending_beacons_lock);
1625out:
1626 mutex_unlock(&cfg80211_mutex);
1627}
1628
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001629static void reg_todo(struct work_struct *work)
1630{
1631 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001632 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001633}
1634
1635static DECLARE_WORK(reg_work, reg_todo);
1636
1637static void queue_regulatory_request(struct regulatory_request *request)
1638{
1639 spin_lock(&reg_requests_lock);
1640 list_add_tail(&request->list, &reg_requests_list);
1641 spin_unlock(&reg_requests_lock);
1642
1643 schedule_work(&reg_work);
1644}
1645
1646/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001647static int regulatory_hint_core(const char *alpha2)
1648{
1649 struct regulatory_request *request;
1650
1651 BUG_ON(last_request);
1652
1653 request = kzalloc(sizeof(struct regulatory_request),
1654 GFP_KERNEL);
1655 if (!request)
1656 return -ENOMEM;
1657
1658 request->alpha2[0] = alpha2[0];
1659 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001660 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001661
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001662 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001663
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001664 /*
1665 * This ensures last_request is populated once modules
1666 * come swinging in and calling regulatory hints and
1667 * wiphy_apply_custom_regulatory().
1668 */
1669 flush_scheduled_work();
1670
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001671 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001672}
1673
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001674/* User hints */
1675int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001676{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001677 struct regulatory_request *request;
1678
Johannes Bergbe3d4812008-10-24 20:32:21 +02001679 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001680
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001681 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1682 if (!request)
1683 return -ENOMEM;
1684
1685 request->wiphy_idx = WIPHY_IDX_STALE;
1686 request->alpha2[0] = alpha2[0];
1687 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001688 request->initiator = NL80211_REGDOM_SET_BY_USER,
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001689
1690 queue_regulatory_request(request);
1691
1692 return 0;
1693}
1694
1695/* Driver hints */
1696int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1697{
1698 struct regulatory_request *request;
1699
1700 BUG_ON(!alpha2);
1701 BUG_ON(!wiphy);
1702
1703 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1704 if (!request)
1705 return -ENOMEM;
1706
1707 request->wiphy_idx = get_wiphy_idx(wiphy);
1708
1709 /* Must have registered wiphy first */
1710 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1711
1712 request->alpha2[0] = alpha2[0];
1713 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001714 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001715
1716 queue_regulatory_request(request);
1717
1718 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001719}
1720EXPORT_SYMBOL(regulatory_hint);
1721
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001722/* Caller must hold reg_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001723static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1724 u32 country_ie_checksum)
1725{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001726 struct wiphy *request_wiphy;
1727
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001728 assert_reg_lock();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001729
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001730 if (unlikely(last_request->initiator !=
1731 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1732 return false;
1733
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001734 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1735
1736 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001737 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001738
1739 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001740 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001741 /*
1742 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001743 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001744 * on the device
1745 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001746 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1747 return true;
1748 return false;
1749}
1750
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001751/*
1752 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1753 * therefore cannot iterate over the rdev list here.
1754 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001755void regulatory_hint_11d(struct wiphy *wiphy,
1756 u8 *country_ie,
1757 u8 country_ie_len)
1758{
1759 struct ieee80211_regdomain *rd = NULL;
1760 char alpha2[2];
1761 u32 checksum = 0;
1762 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001763 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001764
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001765 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001766
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001767 if (unlikely(!last_request))
1768 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001769
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001770 /* IE len must be evenly divisible by 2 */
1771 if (country_ie_len & 0x01)
1772 goto out;
1773
1774 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1775 goto out;
1776
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001777 /*
1778 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001779 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001780 * we were able to process the last regulatory_hint_11d() call
1781 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001782 if (country_ie_regdomain)
1783 goto out;
1784
1785 alpha2[0] = country_ie[0];
1786 alpha2[1] = country_ie[1];
1787
1788 if (country_ie[2] == 'I')
1789 env = ENVIRON_INDOOR;
1790 else if (country_ie[2] == 'O')
1791 env = ENVIRON_OUTDOOR;
1792
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001793 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001794 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001795 * We leave conflict resolution to the workqueue, where can hold
1796 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001797 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001798 if (likely(last_request->initiator ==
1799 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001800 wiphy_idx_valid(last_request->wiphy_idx)))
1801 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001802
1803 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1804 if (!rd)
1805 goto out;
1806
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001807 /*
1808 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001809 * the future when we want to add suspend/resume support and having
1810 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001811 * move to another AP. Right now we just trust the first AP.
1812 *
1813 * If we hit this before we add this support we want to be informed of
1814 * it as it would indicate a mistake in the current design
1815 */
1816 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001817 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001818
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001819 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1820 if (!request)
1821 goto free_rd_out;
1822
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001823 /*
1824 * We keep this around for when CRDA comes back with a response so
1825 * we can intersect with that
1826 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001827 country_ie_regdomain = rd;
1828
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001829 request->wiphy_idx = get_wiphy_idx(wiphy);
1830 request->alpha2[0] = rd->alpha2[0];
1831 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001832 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001833 request->country_ie_checksum = checksum;
1834 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001835
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001836 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001837
1838 queue_regulatory_request(request);
1839
1840 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001841
1842free_rd_out:
1843 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001844out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001845 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001846}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001847
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001848static bool freq_is_chan_12_13_14(u16 freq)
1849{
1850 if (freq == ieee80211_channel_to_frequency(12) ||
1851 freq == ieee80211_channel_to_frequency(13) ||
1852 freq == ieee80211_channel_to_frequency(14))
1853 return true;
1854 return false;
1855}
1856
1857int regulatory_hint_found_beacon(struct wiphy *wiphy,
1858 struct ieee80211_channel *beacon_chan,
1859 gfp_t gfp)
1860{
1861 struct reg_beacon *reg_beacon;
1862
1863 if (likely((beacon_chan->beacon_found ||
1864 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1865 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1866 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1867 return 0;
1868
1869 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1870 if (!reg_beacon)
1871 return -ENOMEM;
1872
1873#ifdef CONFIG_CFG80211_REG_DEBUG
1874 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1875 "frequency: %d MHz (Ch %d) on %s\n",
1876 beacon_chan->center_freq,
1877 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1878 wiphy_name(wiphy));
1879#endif
1880 memcpy(&reg_beacon->chan, beacon_chan,
1881 sizeof(struct ieee80211_channel));
1882
1883
1884 /*
1885 * Since we can be called from BH or and non-BH context
1886 * we must use spin_lock_bh()
1887 */
1888 spin_lock_bh(&reg_pending_beacons_lock);
1889 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1890 spin_unlock_bh(&reg_pending_beacons_lock);
1891
1892 schedule_work(&reg_work);
1893
1894 return 0;
1895}
1896
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001897static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001898{
1899 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001900 const struct ieee80211_reg_rule *reg_rule = NULL;
1901 const struct ieee80211_freq_range *freq_range = NULL;
1902 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001903
Kalle Valo269ac5f2009-12-01 10:47:15 +02001904 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001905 "(max_antenna_gain, max_eirp)\n");
1906
1907 for (i = 0; i < rd->n_reg_rules; i++) {
1908 reg_rule = &rd->reg_rules[i];
1909 freq_range = &reg_rule->freq_range;
1910 power_rule = &reg_rule->power_rule;
1911
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001912 /*
1913 * There may not be documentation for max antenna gain
1914 * in certain regions
1915 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001916 if (power_rule->max_antenna_gain)
Kalle Valo269ac5f2009-12-01 10:47:15 +02001917 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001918 "(%d mBi, %d mBm)\n",
1919 freq_range->start_freq_khz,
1920 freq_range->end_freq_khz,
1921 freq_range->max_bandwidth_khz,
1922 power_rule->max_antenna_gain,
1923 power_rule->max_eirp);
1924 else
Kalle Valo269ac5f2009-12-01 10:47:15 +02001925 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001926 "(N/A, %d mBm)\n",
1927 freq_range->start_freq_khz,
1928 freq_range->end_freq_khz,
1929 freq_range->max_bandwidth_khz,
1930 power_rule->max_eirp);
1931 }
1932}
1933
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001934static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001935{
1936
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001937 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001938
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001939 if (last_request->initiator ==
1940 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001941 struct cfg80211_registered_device *rdev;
1942 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001943 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02001944 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001945 printk(KERN_INFO "cfg80211: Current regulatory "
1946 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02001947 rdev->country_ie_alpha2[0],
1948 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001949 } else
1950 printk(KERN_INFO "cfg80211: Current regulatory "
1951 "domain intersected: \n");
1952 } else
1953 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001954 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001955 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001956 printk(KERN_INFO "cfg80211: World regulatory "
1957 "domain updated:\n");
1958 else {
1959 if (is_unknown_alpha2(rd->alpha2))
1960 printk(KERN_INFO "cfg80211: Regulatory domain "
1961 "changed to driver built-in settings "
1962 "(unknown country)\n");
1963 else
1964 printk(KERN_INFO "cfg80211: Regulatory domain "
1965 "changed to country: %c%c\n",
1966 rd->alpha2[0], rd->alpha2[1]);
1967 }
1968 print_rd_rules(rd);
1969}
1970
Johannes Berg2df78162008-10-28 16:49:41 +01001971static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001972{
1973 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1974 rd->alpha2[0], rd->alpha2[1]);
1975 print_rd_rules(rd);
1976}
1977
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001978#ifdef CONFIG_CFG80211_REG_DEBUG
1979static void reg_country_ie_process_debug(
1980 const struct ieee80211_regdomain *rd,
1981 const struct ieee80211_regdomain *country_ie_regdomain,
1982 const struct ieee80211_regdomain *intersected_rd)
1983{
1984 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1985 print_regdomain_info(country_ie_regdomain);
1986 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1987 print_regdomain_info(rd);
1988 if (intersected_rd) {
1989 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1990 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08001991 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001992 return;
1993 }
1994 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
1995}
1996#else
1997static inline void reg_country_ie_process_debug(
1998 const struct ieee80211_regdomain *rd,
1999 const struct ieee80211_regdomain *country_ie_regdomain,
2000 const struct ieee80211_regdomain *intersected_rd)
2001{
2002}
2003#endif
2004
Johannes Bergd2372b32008-10-24 20:32:20 +02002005/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002006static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002007{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002008 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002009 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002010 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002011 /* Some basic sanity checks first */
2012
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002013 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002014 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002015 return -EINVAL;
2016 update_world_regdomain(rd);
2017 return 0;
2018 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002019
2020 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2021 !is_unknown_alpha2(rd->alpha2))
2022 return -EINVAL;
2023
Johannes Bergf6037d02008-10-21 11:01:33 +02002024 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002025 return -EINVAL;
2026
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002027 /*
2028 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002029 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002030 * and the pending request came in from a country IE
2031 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002032 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002033 /*
2034 * If someone else asked us to change the rd lets only bother
2035 * checking if the alpha2 changes if CRDA was already called
2036 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002037 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002038 return -EINVAL;
2039 }
2040
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002041 /*
2042 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002043 * and finally inform them of what we have done, in case they want
2044 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002045 * internal EEPROM data
2046 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002047
Johannes Bergf6037d02008-10-21 11:01:33 +02002048 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002049 return -EINVAL;
2050
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002051 if (!is_valid_rd(rd)) {
2052 printk(KERN_ERR "cfg80211: Invalid "
2053 "regulatory domain detected:\n");
2054 print_regdomain_info(rd);
2055 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002056 }
2057
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002058 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2059
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002060 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002061 int r;
2062
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002063 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002064 reset_regdomains();
2065 cfg80211_regdomain = rd;
2066 return 0;
2067 }
2068
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002069 /*
2070 * For a driver hint, lets copy the regulatory domain the
2071 * driver wanted to the wiphy to deal with conflicts
2072 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002073
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002074 /*
2075 * Userspace could have sent two replies with only
2076 * one kernel request.
2077 */
2078 if (request_wiphy->regd)
2079 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002080
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002081 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002082 if (r)
2083 return r;
2084
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002085 reset_regdomains();
2086 cfg80211_regdomain = rd;
2087 return 0;
2088 }
2089
2090 /* Intersection requires a bit more work */
2091
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002092 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002093
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002094 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2095 if (!intersected_rd)
2096 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002097
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002098 /*
2099 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002100 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002101 * domain we keep it for its private use
2102 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002103 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002104 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002105 else
2106 kfree(rd);
2107
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002108 rd = NULL;
2109
2110 reset_regdomains();
2111 cfg80211_regdomain = intersected_rd;
2112
2113 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002114 }
2115
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002116 /*
2117 * Country IE requests are handled a bit differently, we intersect
2118 * the country IE rd with what CRDA believes that country should have
2119 */
2120
Luis R. Rodriguez729e9c72009-05-31 18:24:34 -04002121 /*
2122 * Userspace could have sent two replies with only
2123 * one kernel request. By the second reply we would have
2124 * already processed and consumed the country_ie_regdomain.
2125 */
2126 if (!country_ie_regdomain)
2127 return -EALREADY;
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002128 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002129
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002130 /*
2131 * Intersect what CRDA returned and our what we
2132 * had built from the Country IE received
2133 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002134
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002135 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002136
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002137 reg_country_ie_process_debug(rd,
2138 country_ie_regdomain,
2139 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002140
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002141 kfree(country_ie_regdomain);
2142 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002143
2144 if (!intersected_rd)
2145 return -EINVAL;
2146
Johannes Berg79c97e92009-07-07 03:56:12 +02002147 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002148
Johannes Berg79c97e92009-07-07 03:56:12 +02002149 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2150 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2151 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002152
2153 BUG_ON(intersected_rd == rd);
2154
2155 kfree(rd);
2156 rd = NULL;
2157
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002158 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002159 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002160
2161 return 0;
2162}
2163
2164
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002165/*
2166 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002167 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002168 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2169 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002170int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002171{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002172 int r;
2173
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002174 assert_cfg80211_lock();
2175
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002176 mutex_lock(&reg_mutex);
2177
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002178 /* Note that this doesn't update the wiphys, this is done below */
2179 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002180 if (r) {
2181 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002182 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002183 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002184 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002185
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002186 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002187 if (!last_request->intersect)
2188 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002189
2190 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002191 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002192
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002193 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002194
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002195 nl80211_send_reg_change_event(last_request);
2196
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002197 mutex_unlock(&reg_mutex);
2198
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002199 return r;
2200}
2201
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002202/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002203void reg_device_remove(struct wiphy *wiphy)
2204{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002205 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002206
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002207 assert_cfg80211_lock();
2208
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002209 mutex_lock(&reg_mutex);
2210
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002211 kfree(wiphy->regd);
2212
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002213 if (last_request)
2214 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002215
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002216 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002217 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002218
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002219 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002220 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002221out:
2222 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002223}
2224
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002225int regulatory_init(void)
2226{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002227 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002228
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002229 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2230 if (IS_ERR(reg_pdev))
2231 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002232
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002233 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002234 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002235
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002236 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002237
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002238 /* We always try to get an update for the static regdomain */
2239 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002240 if (err) {
2241 if (err == -ENOMEM)
2242 return err;
2243 /*
2244 * N.B. kobject_uevent_env() can fail mainly for when we're out
2245 * memory which is handled and propagated appropriately above
2246 * but it can also fail during a netlink_broadcast() or during
2247 * early boot for call_usermodehelper(). For now treat these
2248 * errors as non-fatal.
2249 */
2250 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2251 "to call CRDA during init");
2252#ifdef CONFIG_CFG80211_REG_DEBUG
2253 /* We want to find out exactly why when debugging */
2254 WARN_ON(err);
2255#endif
2256 }
Johannes Berg734366d2008-09-15 10:56:48 +02002257
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002258 /*
2259 * Finally, if the user set the module parameter treat it
2260 * as a user hint.
2261 */
2262 if (!is_world_regdom(ieee80211_regdom))
2263 regulatory_hint_user(ieee80211_regdom);
2264
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002265 return 0;
2266}
2267
2268void regulatory_exit(void)
2269{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002270 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002271 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002272
2273 cancel_work_sync(&reg_work);
2274
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002275 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002276 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002277
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002278 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002279
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002280 kfree(country_ie_regdomain);
2281 country_ie_regdomain = NULL;
2282
Johannes Bergf6037d02008-10-21 11:01:33 +02002283 kfree(last_request);
2284
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002285 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002286
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002287 spin_lock_bh(&reg_pending_beacons_lock);
2288 if (!list_empty(&reg_pending_beacons)) {
2289 list_for_each_entry_safe(reg_beacon, btmp,
2290 &reg_pending_beacons, list) {
2291 list_del(&reg_beacon->list);
2292 kfree(reg_beacon);
2293 }
2294 }
2295 spin_unlock_bh(&reg_pending_beacons_lock);
2296
2297 if (!list_empty(&reg_beacon_list)) {
2298 list_for_each_entry_safe(reg_beacon, btmp,
2299 &reg_beacon_list, list) {
2300 list_del(&reg_beacon->list);
2301 kfree(reg_beacon);
2302 }
2303 }
2304
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002305 spin_lock(&reg_requests_lock);
2306 if (!list_empty(&reg_requests_list)) {
2307 list_for_each_entry_safe(reg_request, tmp,
2308 &reg_requests_list, list) {
2309 list_del(&reg_request->list);
2310 kfree(reg_request);
2311 }
2312 }
2313 spin_unlock(&reg_requests_lock);
2314
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002315 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002316 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002317}