blob: a5c2d3a6cbb2a30c96c46dba7c7595e256f8d418 [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. Rodriguez4113f752010-01-04 11:50:11 -050046#ifdef CONFIG_CFG80211_REG_DEBUG
John W. Linville82711952010-01-05 17:57:20 -050047#define REG_DBG_PRINT(format, args...) \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050048 do { \
John W. Linville82711952010-01-05 17:57:20 -050049 printk(KERN_DEBUG format , ## args); \
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050050 } while (0)
51#else
John W. Linville82711952010-01-05 17:57:20 -050052#define REG_DBG_PRINT(args...)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -050053#endif
54
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070055/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020056static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020057
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070058/* To trigger userspace events */
59static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010060
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050061/*
62 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020063 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050064 * information to give us an alpha2
65 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080066const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020067
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050068/*
69 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080070 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050071 * what it thinks should apply for the same country
72 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080073static const struct ieee80211_regdomain *country_ie_regdomain;
74
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070075/*
76 * Protects static reg.c components:
77 * - cfg80211_world_regdom
78 * - cfg80211_regdom
79 * - country_ie_regdomain
80 * - last_request
81 */
82DEFINE_MUTEX(reg_mutex);
83#define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
84
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050085/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050086static LIST_HEAD(reg_requests_list);
87static spinlock_t reg_requests_lock;
88
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050089/* Used to queue up beacon hints for review */
90static LIST_HEAD(reg_pending_beacons);
91static spinlock_t reg_pending_beacons_lock;
92
93/* Used to keep track of processed beacon hints */
94static LIST_HEAD(reg_beacon_list);
95
96struct reg_beacon {
97 struct list_head list;
98 struct ieee80211_channel chan;
99};
100
Johannes Berg734366d2008-09-15 10:56:48 +0200101/* We keep a static world regulatory domain in case of the absence of CRDA */
102static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800103 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +0200104 .alpha2 = "00",
105 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -0500106 /* IEEE 802.11b/g, channels 1..11 */
107 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800108 /* IEEE 802.11b/g, channels 12..13. No HT40
109 * channel fits here. */
110 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500111 NL80211_RRF_PASSIVE_SCAN |
112 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800113 /* IEEE 802.11 channel 14 - Only JP enables
114 * this and for 802.11b only */
115 REG_RULE(2484-10, 2484+10, 20, 6, 20,
116 NL80211_RRF_PASSIVE_SCAN |
117 NL80211_RRF_NO_IBSS |
118 NL80211_RRF_NO_OFDM),
119 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800120 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800121 NL80211_RRF_PASSIVE_SCAN |
122 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500123
124 /* NB: 5260 MHz - 5700 MHz requies DFS */
125
126 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800127 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500128 NL80211_RRF_PASSIVE_SCAN |
129 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200130 }
131};
132
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200133static const struct ieee80211_regdomain *cfg80211_world_regdom =
134 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200135
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400136static char *ieee80211_regdom = "00";
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400137
Johannes Berg734366d2008-09-15 10:56:48 +0200138module_param(ieee80211_regdom, charp, 0444);
139MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
140
Johannes Berg734366d2008-09-15 10:56:48 +0200141static void reset_regdomains(void)
142{
Johannes Berg942b25c2008-09-15 11:26:47 +0200143 /* avoid freeing static information or freeing something twice */
144 if (cfg80211_regdomain == cfg80211_world_regdom)
145 cfg80211_regdomain = NULL;
146 if (cfg80211_world_regdom == &world_regdom)
147 cfg80211_world_regdom = NULL;
148 if (cfg80211_regdomain == &world_regdom)
149 cfg80211_regdomain = NULL;
Johannes Berg942b25c2008-09-15 11:26:47 +0200150
151 kfree(cfg80211_regdomain);
152 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200153
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200154 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200155 cfg80211_regdomain = NULL;
156}
157
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500158/*
159 * Dynamic world regulatory domain requested by the wireless
160 * core upon initialization
161 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200162static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200163{
Johannes Bergf6037d02008-10-21 11:01:33 +0200164 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200165
166 reset_regdomains();
167
168 cfg80211_world_regdom = rd;
169 cfg80211_regdomain = rd;
170}
Johannes Berg734366d2008-09-15 10:56:48 +0200171
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200172bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100173{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700174 if (!alpha2)
175 return false;
176 if (alpha2[0] == '0' && alpha2[1] == '0')
177 return true;
178 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100179}
180
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200181static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700182{
183 if (!alpha2)
184 return false;
185 if (alpha2[0] != 0 && alpha2[1] != 0)
186 return true;
187 return false;
188}
Johannes Berg8318d782008-01-24 19:38:38 +0100189
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700190static bool is_alpha_upper(char letter)
191{
192 /* ASCII A - Z */
193 if (letter >= 65 && letter <= 90)
194 return true;
195 return false;
196}
197
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200198static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700199{
200 if (!alpha2)
201 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500202 /*
203 * Special case where regulatory domain was built by driver
204 * but a specific alpha2 cannot be determined
205 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700206 if (alpha2[0] == '9' && alpha2[1] == '9')
207 return true;
208 return false;
209}
210
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800211static bool is_intersected_alpha2(const char *alpha2)
212{
213 if (!alpha2)
214 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500215 /*
216 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800217 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500218 * structures
219 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800220 if (alpha2[0] == '9' && alpha2[1] == '8')
221 return true;
222 return false;
223}
224
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200225static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700226{
227 if (!alpha2)
228 return false;
229 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
230 return true;
231 return false;
232}
233
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200234static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700235{
236 if (!alpha2_x || !alpha2_y)
237 return false;
238 if (alpha2_x[0] == alpha2_y[0] &&
239 alpha2_x[1] == alpha2_y[1])
240 return true;
241 return false;
242}
243
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500244static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700245{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500246 assert_cfg80211_lock();
247
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700248 if (!cfg80211_regdomain)
249 return true;
250 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
251 return false;
252 return true;
253}
254
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800255/**
256 * country_ie_integrity_changes - tells us if the country IE has changed
257 * @checksum: checksum of country IE of fields we are interested in
258 *
259 * If the country IE has not changed you can ignore it safely. This is
260 * useful to determine if two devices are seeing two different country IEs
261 * even on the same alpha2. Note that this will return false if no IE has
262 * been set on the wireless core yet.
263 */
264static bool country_ie_integrity_changes(u32 checksum)
265{
266 /* If no IE has been set then the checksum doesn't change */
267 if (unlikely(!last_request->country_ie_checksum))
268 return false;
269 if (unlikely(last_request->country_ie_checksum != checksum))
270 return true;
271 return false;
272}
273
John W. Linville3b377ea2009-12-18 17:59:01 -0500274static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
275 const struct ieee80211_regdomain *src_regd)
276{
277 struct ieee80211_regdomain *regd;
278 int size_of_regd = 0;
279 unsigned int i;
280
281 size_of_regd = sizeof(struct ieee80211_regdomain) +
282 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
283
284 regd = kzalloc(size_of_regd, GFP_KERNEL);
285 if (!regd)
286 return -ENOMEM;
287
288 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
289
290 for (i = 0; i < src_regd->n_reg_rules; i++)
291 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
292 sizeof(struct ieee80211_reg_rule));
293
294 *dst_regd = regd;
295 return 0;
296}
297
298#ifdef CONFIG_CFG80211_INTERNAL_REGDB
299struct reg_regdb_search_request {
300 char alpha2[2];
301 struct list_head list;
302};
303
304static LIST_HEAD(reg_regdb_search_list);
305static DEFINE_SPINLOCK(reg_regdb_search_lock);
306
307static void reg_regdb_search(struct work_struct *work)
308{
309 struct reg_regdb_search_request *request;
310 const struct ieee80211_regdomain *curdom, *regdom;
311 int i, r;
312
313 spin_lock(&reg_regdb_search_lock);
314 while (!list_empty(&reg_regdb_search_list)) {
315 request = list_first_entry(&reg_regdb_search_list,
316 struct reg_regdb_search_request,
317 list);
318 list_del(&request->list);
319
320 for (i=0; i<reg_regdb_size; i++) {
321 curdom = reg_regdb[i];
322
323 if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
324 r = reg_copy_regd(&regdom, curdom);
325 if (r)
326 break;
327 spin_unlock(&reg_regdb_search_lock);
328 mutex_lock(&cfg80211_mutex);
329 set_regdom(regdom);
330 mutex_unlock(&cfg80211_mutex);
331 spin_lock(&reg_regdb_search_lock);
332 break;
333 }
334 }
335
336 kfree(request);
337 }
338 spin_unlock(&reg_regdb_search_lock);
339}
340
341static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
342
343static void reg_regdb_query(const char *alpha2)
344{
345 struct reg_regdb_search_request *request;
346
347 if (!alpha2)
348 return;
349
350 request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
351 if (!request)
352 return;
353
354 memcpy(request->alpha2, alpha2, 2);
355
356 spin_lock(&reg_regdb_search_lock);
357 list_add_tail(&request->list, &reg_regdb_search_list);
358 spin_unlock(&reg_regdb_search_lock);
359
360 schedule_work(&reg_regdb_work);
361}
362#else
363static inline void reg_regdb_query(const char *alpha2) {}
364#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
365
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500366/*
367 * This lets us keep regulatory code which is updated on a regulatory
368 * basis in userspace.
369 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700370static int call_crda(const char *alpha2)
371{
372 char country_env[9 + 2] = "COUNTRY=";
373 char *envp[] = {
374 country_env,
375 NULL
376 };
377
378 if (!is_world_regdom((char *) alpha2))
379 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
380 alpha2[0], alpha2[1]);
381 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700382 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
383 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700384
John W. Linville3b377ea2009-12-18 17:59:01 -0500385 /* query internal regulatory database (if it exists) */
386 reg_regdb_query(alpha2);
387
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388 country_env[8] = alpha2[0];
389 country_env[9] = alpha2[1];
390
391 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
392}
393
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700394/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200395bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700396{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400397 assert_cfg80211_lock();
398
Johannes Bergf6037d02008-10-21 11:01:33 +0200399 if (!last_request)
400 return false;
401
402 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700403}
404
405/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200406static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700407{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200408 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700409 u32 freq_diff;
410
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800411 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700412 return false;
413
414 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
415 return false;
416
417 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
418
Roel Kluinbd05f282009-03-03 22:55:21 +0100419 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
420 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700421 return false;
422
423 return true;
424}
425
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200426static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700427{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200428 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700429 unsigned int i;
430
431 if (!rd->n_reg_rules)
432 return false;
433
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800434 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
435 return false;
436
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700437 for (i = 0; i < rd->n_reg_rules; i++) {
438 reg_rule = &rd->reg_rules[i];
439 if (!is_valid_reg_rule(reg_rule))
440 return false;
441 }
442
443 return true;
444}
445
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400446static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
447 u32 center_freq_khz,
448 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700449{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400450 u32 start_freq_khz, end_freq_khz;
451
452 start_freq_khz = center_freq_khz - (bw_khz/2);
453 end_freq_khz = center_freq_khz + (bw_khz/2);
454
455 if (start_freq_khz >= freq_range->start_freq_khz &&
456 end_freq_khz <= freq_range->end_freq_khz)
457 return true;
458
459 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700460}
461
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800462/**
463 * freq_in_rule_band - tells us if a frequency is in a frequency band
464 * @freq_range: frequency rule we want to query
465 * @freq_khz: frequency we are inquiring about
466 *
467 * This lets us know if a specific frequency rule is or is not relevant to
468 * a specific frequency's band. Bands are device specific and artificial
469 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
470 * safe for now to assume that a frequency rule should not be part of a
471 * frequency's band if the start freq or end freq are off by more than 2 GHz.
472 * This resolution can be lowered and should be considered as we add
473 * regulatory rule support for other "bands".
474 **/
475static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
476 u32 freq_khz)
477{
478#define ONE_GHZ_IN_KHZ 1000000
479 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
480 return true;
481 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
482 return true;
483 return false;
484#undef ONE_GHZ_IN_KHZ
485}
486
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500487/*
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500488 * Some APs may send a country IE triplet for each channel they
489 * support and while this is completely overkill and silly we still
490 * need to support it. We avoid making a single rule for each channel
491 * though and to help us with this we use this helper to find the
492 * actual subband end channel. These type of country IE triplet
493 * scenerios are handled then, all yielding two regulaotry rules from
494 * parsing a country IE:
495 *
496 * [1]
497 * [2]
498 * [36]
499 * [40]
500 *
501 * [1]
502 * [2-4]
503 * [5-12]
504 * [36]
505 * [40-44]
506 *
507 * [1-4]
508 * [5-7]
509 * [36-44]
510 * [48-64]
511 *
512 * [36-36]
513 * [40-40]
514 * [44-44]
515 * [48-48]
516 * [52-52]
517 * [56-56]
518 * [60-60]
519 * [64-64]
520 * [100-100]
521 * [104-104]
522 * [108-108]
523 * [112-112]
524 * [116-116]
525 * [120-120]
526 * [124-124]
527 * [128-128]
528 * [132-132]
529 * [136-136]
530 * [140-140]
531 *
532 * Returns 0 if the IE has been found to be invalid in the middle
533 * somewhere.
534 */
535static int max_subband_chan(int orig_cur_chan,
536 int orig_end_channel,
537 s8 orig_max_power,
538 u8 **country_ie,
539 u8 *country_ie_len)
540{
541 u8 *triplets_start = *country_ie;
542 u8 len_at_triplet = *country_ie_len;
543 int end_subband_chan = orig_end_channel;
544 enum ieee80211_band band;
545
546 /*
547 * We'll deal with padding for the caller unless
548 * its not immediate and we don't process any channels
549 */
550 if (*country_ie_len == 1) {
551 *country_ie += 1;
552 *country_ie_len -= 1;
553 return orig_end_channel;
554 }
555
556 /* Move to the next triplet and then start search */
557 *country_ie += 3;
558 *country_ie_len -= 3;
559
560 if (orig_cur_chan <= 14)
561 band = IEEE80211_BAND_2GHZ;
562 else
563 band = IEEE80211_BAND_5GHZ;
564
565 while (*country_ie_len >= 3) {
566 int end_channel = 0;
567 struct ieee80211_country_ie_triplet *triplet =
568 (struct ieee80211_country_ie_triplet *) *country_ie;
569 int cur_channel = 0, next_expected_chan;
570 enum ieee80211_band next_band = IEEE80211_BAND_2GHZ;
571
572 /* means last triplet is completely unrelated to this one */
573 if (triplet->ext.reg_extension_id >=
574 IEEE80211_COUNTRY_EXTENSION_ID) {
575 *country_ie -= 3;
576 *country_ie_len += 3;
577 break;
578 }
579
580 if (triplet->chans.first_channel == 0) {
581 *country_ie += 1;
582 *country_ie_len -= 1;
583 if (*country_ie_len != 0)
584 return 0;
585 break;
586 }
587
588 /* Monitonically increasing channel order */
589 if (triplet->chans.first_channel <= end_subband_chan)
590 return 0;
591
592 /* 2 GHz */
593 if (triplet->chans.first_channel <= 14) {
594 end_channel = triplet->chans.first_channel +
595 triplet->chans.num_channels - 1;
596 }
597 else {
598 end_channel = triplet->chans.first_channel +
599 (4 * (triplet->chans.num_channels - 1));
600 next_band = IEEE80211_BAND_5GHZ;
601 }
602
603 if (band != next_band) {
604 *country_ie -= 3;
605 *country_ie_len += 3;
606 break;
607 }
608
609 if (orig_max_power != triplet->chans.max_power) {
610 *country_ie -= 3;
611 *country_ie_len += 3;
612 break;
613 }
614
615 cur_channel = triplet->chans.first_channel;
616
617 /* The key is finding the right next expected channel */
618 if (band == IEEE80211_BAND_2GHZ)
619 next_expected_chan = end_subband_chan + 1;
620 else
621 next_expected_chan = end_subband_chan + 4;
622
623 if (cur_channel != next_expected_chan) {
624 *country_ie -= 3;
625 *country_ie_len += 3;
626 break;
627 }
628
629 end_subband_chan = end_channel;
630
631 /* Move to the next one */
632 *country_ie += 3;
633 *country_ie_len -= 3;
634
635 /*
636 * Padding needs to be dealt with if we processed
637 * some channels.
638 */
639 if (*country_ie_len == 1) {
640 *country_ie += 1;
641 *country_ie_len -= 1;
642 break;
643 }
644
645 /* If seen, the IE is invalid */
646 if (*country_ie_len == 2)
647 return 0;
648 }
649
650 if (end_subband_chan == orig_end_channel) {
651 *country_ie = triplets_start;
652 *country_ie_len = len_at_triplet;
653 return orig_end_channel;
654 }
655
656 return end_subband_chan;
657}
658
659/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500660 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800661 * structure has a lot of information which the IE doesn't yet have,
662 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500663 * with our userspace regulatory agent to get lower bounds.
664 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800665static struct ieee80211_regdomain *country_ie_2_rd(
666 u8 *country_ie,
667 u8 country_ie_len,
668 u32 *checksum)
669{
670 struct ieee80211_regdomain *rd = NULL;
671 unsigned int i = 0;
672 char alpha2[2];
673 u32 flags = 0;
674 u32 num_rules = 0, size_of_regd = 0;
675 u8 *triplets_start = NULL;
676 u8 len_at_triplet = 0;
677 /* the last channel we have registered in a subband (triplet) */
678 int last_sub_max_channel = 0;
679
680 *checksum = 0xDEADBEEF;
681
682 /* Country IE requirements */
683 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
684 country_ie_len & 0x01);
685
686 alpha2[0] = country_ie[0];
687 alpha2[1] = country_ie[1];
688
689 /*
690 * Third octet can be:
691 * 'I' - Indoor
692 * 'O' - Outdoor
693 *
694 * anything else we assume is no restrictions
695 */
696 if (country_ie[2] == 'I')
697 flags = NL80211_RRF_NO_OUTDOOR;
698 else if (country_ie[2] == 'O')
699 flags = NL80211_RRF_NO_INDOOR;
700
701 country_ie += 3;
702 country_ie_len -= 3;
703
704 triplets_start = country_ie;
705 len_at_triplet = country_ie_len;
706
707 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
708
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500709 /*
710 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800711 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500712 * for each channel subband
713 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800714 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800715 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800716 struct ieee80211_country_ie_triplet *triplet =
717 (struct ieee80211_country_ie_triplet *) country_ie;
718 int cur_sub_max_channel = 0, cur_channel = 0;
719
720 if (triplet->ext.reg_extension_id >=
721 IEEE80211_COUNTRY_EXTENSION_ID) {
722 country_ie += 3;
723 country_ie_len -= 3;
724 continue;
725 }
726
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500727 /*
728 * APs can add padding to make length divisible
729 * by two, required by the spec.
730 */
731 if (triplet->chans.first_channel == 0) {
732 country_ie++;
733 country_ie_len--;
734 /* This is expected to be at the very end only */
735 if (country_ie_len != 0)
736 return NULL;
737 break;
738 }
739
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800740 /* 2 GHz */
741 if (triplet->chans.first_channel <= 14)
742 end_channel = triplet->chans.first_channel +
743 triplet->chans.num_channels;
744 else
745 /*
746 * 5 GHz -- For example in country IEs if the first
747 * channel given is 36 and the number of channels is 4
748 * then the individual channel numbers defined for the
749 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
750 * and not 36, 37, 38, 39.
751 *
752 * See: http://tinyurl.com/11d-clarification
753 */
754 end_channel = triplet->chans.first_channel +
755 (4 * (triplet->chans.num_channels - 1));
756
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800757 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500758
759 /*
760 * Enhancement for APs that send a triplet for every channel
761 * or for whatever reason sends triplets with multiple channels
762 * separated when in fact they should be together.
763 */
764 end_channel = max_subband_chan(cur_channel,
765 end_channel,
766 triplet->chans.max_power,
767 &country_ie,
768 &country_ie_len);
769 if (!end_channel)
770 return NULL;
771
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800772 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800773
774 /* Basic sanity check */
775 if (cur_sub_max_channel < cur_channel)
776 return NULL;
777
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500778 /*
779 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800780 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500781 * increasing
782 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800783 if (last_sub_max_channel) {
784 if (cur_channel <= last_sub_max_channel)
785 return NULL;
786 if (cur_sub_max_channel <= last_sub_max_channel)
787 return NULL;
788 }
789
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500790 /*
791 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800792 * we can throw ext triplets as part of this soup,
793 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500794 * don't support them
795 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800796 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
797 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
798 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
799
800 last_sub_max_channel = cur_sub_max_channel;
801
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800802 num_rules++;
803
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500804 if (country_ie_len >= 3) {
805 country_ie += 3;
806 country_ie_len -= 3;
807 }
808
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500809 /*
810 * Note: this is not a IEEE requirement but
811 * simply a memory requirement
812 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800813 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
814 return NULL;
815 }
816
817 country_ie = triplets_start;
818 country_ie_len = len_at_triplet;
819
820 size_of_regd = sizeof(struct ieee80211_regdomain) +
821 (num_rules * sizeof(struct ieee80211_reg_rule));
822
823 rd = kzalloc(size_of_regd, GFP_KERNEL);
824 if (!rd)
825 return NULL;
826
827 rd->n_reg_rules = num_rules;
828 rd->alpha2[0] = alpha2[0];
829 rd->alpha2[1] = alpha2[1];
830
831 /* This time around we fill in the rd */
832 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800833 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800834 struct ieee80211_country_ie_triplet *triplet =
835 (struct ieee80211_country_ie_triplet *) country_ie;
836 struct ieee80211_reg_rule *reg_rule = NULL;
837 struct ieee80211_freq_range *freq_range = NULL;
838 struct ieee80211_power_rule *power_rule = NULL;
839
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500840 /*
841 * Must parse if dot11RegulatoryClassesRequired is true,
842 * we don't support this yet
843 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800844 if (triplet->ext.reg_extension_id >=
845 IEEE80211_COUNTRY_EXTENSION_ID) {
846 country_ie += 3;
847 country_ie_len -= 3;
848 continue;
849 }
850
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500851 if (triplet->chans.first_channel == 0) {
852 country_ie++;
853 country_ie_len--;
854 break;
855 }
856
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800857 reg_rule = &rd->reg_rules[i];
858 freq_range = &reg_rule->freq_range;
859 power_rule = &reg_rule->power_rule;
860
861 reg_rule->flags = flags;
862
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800863 /* 2 GHz */
864 if (triplet->chans.first_channel <= 14)
865 end_channel = triplet->chans.first_channel +
866 triplet->chans.num_channels;
867 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800868 end_channel = triplet->chans.first_channel +
869 (4 * (triplet->chans.num_channels - 1));
870
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500871 end_channel = max_subband_chan(triplet->chans.first_channel,
872 end_channel,
873 triplet->chans.max_power,
874 &country_ie,
875 &country_ie_len);
876
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500877 /*
878 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800879 * the actual band edge, not the center of freq for
880 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500881 * the channels passed
882 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800883 freq_range->start_freq_khz =
884 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
885 triplet->chans.first_channel) - 10);
886 freq_range->end_freq_khz =
887 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800888 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800889
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500890 /*
891 * These are large arbitrary values we use to intersect later.
892 * Increment this if we ever support >= 40 MHz channels
893 * in IEEE 802.11
894 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800895 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
896 power_rule->max_antenna_gain = DBI_TO_MBI(100);
Luis R. Rodriguez08030db2010-01-07 17:24:56 -0500897 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800898
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800899 i++;
900
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500901 if (country_ie_len >= 3) {
902 country_ie += 3;
903 country_ie_len -= 3;
904 }
905
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800906 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
907 }
908
909 return rd;
910}
911
912
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500913/*
914 * Helper for regdom_intersect(), this does the real
915 * mathematical intersection fun
916 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700917static int reg_rules_intersect(
918 const struct ieee80211_reg_rule *rule1,
919 const struct ieee80211_reg_rule *rule2,
920 struct ieee80211_reg_rule *intersected_rule)
921{
922 const struct ieee80211_freq_range *freq_range1, *freq_range2;
923 struct ieee80211_freq_range *freq_range;
924 const struct ieee80211_power_rule *power_rule1, *power_rule2;
925 struct ieee80211_power_rule *power_rule;
926 u32 freq_diff;
927
928 freq_range1 = &rule1->freq_range;
929 freq_range2 = &rule2->freq_range;
930 freq_range = &intersected_rule->freq_range;
931
932 power_rule1 = &rule1->power_rule;
933 power_rule2 = &rule2->power_rule;
934 power_rule = &intersected_rule->power_rule;
935
936 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
937 freq_range2->start_freq_khz);
938 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
939 freq_range2->end_freq_khz);
940 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
941 freq_range2->max_bandwidth_khz);
942
943 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
944 if (freq_range->max_bandwidth_khz > freq_diff)
945 freq_range->max_bandwidth_khz = freq_diff;
946
947 power_rule->max_eirp = min(power_rule1->max_eirp,
948 power_rule2->max_eirp);
949 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
950 power_rule2->max_antenna_gain);
951
952 intersected_rule->flags = (rule1->flags | rule2->flags);
953
954 if (!is_valid_reg_rule(intersected_rule))
955 return -EINVAL;
956
957 return 0;
958}
959
960/**
961 * regdom_intersect - do the intersection between two regulatory domains
962 * @rd1: first regulatory domain
963 * @rd2: second regulatory domain
964 *
965 * Use this function to get the intersection between two regulatory domains.
966 * Once completed we will mark the alpha2 for the rd as intersected, "98",
967 * as no one single alpha2 can represent this regulatory domain.
968 *
969 * Returns a pointer to the regulatory domain structure which will hold the
970 * resulting intersection of rules between rd1 and rd2. We will
971 * kzalloc() this structure for you.
972 */
973static struct ieee80211_regdomain *regdom_intersect(
974 const struct ieee80211_regdomain *rd1,
975 const struct ieee80211_regdomain *rd2)
976{
977 int r, size_of_regd;
978 unsigned int x, y;
979 unsigned int num_rules = 0, rule_idx = 0;
980 const struct ieee80211_reg_rule *rule1, *rule2;
981 struct ieee80211_reg_rule *intersected_rule;
982 struct ieee80211_regdomain *rd;
983 /* This is just a dummy holder to help us count */
984 struct ieee80211_reg_rule irule;
985
986 /* Uses the stack temporarily for counter arithmetic */
987 intersected_rule = &irule;
988
989 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
990
991 if (!rd1 || !rd2)
992 return NULL;
993
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500994 /*
995 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700996 * build them. This is to so we can malloc() and free() a
997 * regdomain once. The reason we use reg_rules_intersect() here
998 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500999 * All rules that do check out OK are valid.
1000 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001001
1002 for (x = 0; x < rd1->n_reg_rules; x++) {
1003 rule1 = &rd1->reg_rules[x];
1004 for (y = 0; y < rd2->n_reg_rules; y++) {
1005 rule2 = &rd2->reg_rules[y];
1006 if (!reg_rules_intersect(rule1, rule2,
1007 intersected_rule))
1008 num_rules++;
1009 memset(intersected_rule, 0,
1010 sizeof(struct ieee80211_reg_rule));
1011 }
1012 }
1013
1014 if (!num_rules)
1015 return NULL;
1016
1017 size_of_regd = sizeof(struct ieee80211_regdomain) +
1018 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
1019
1020 rd = kzalloc(size_of_regd, GFP_KERNEL);
1021 if (!rd)
1022 return NULL;
1023
1024 for (x = 0; x < rd1->n_reg_rules; x++) {
1025 rule1 = &rd1->reg_rules[x];
1026 for (y = 0; y < rd2->n_reg_rules; y++) {
1027 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001028 /*
1029 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001030 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001031 * a memcpy()
1032 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001033 intersected_rule = &rd->reg_rules[rule_idx];
1034 r = reg_rules_intersect(rule1, rule2,
1035 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001036 /*
1037 * No need to memset here the intersected rule here as
1038 * we're not using the stack anymore
1039 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001040 if (r)
1041 continue;
1042 rule_idx++;
1043 }
1044 }
1045
1046 if (rule_idx != num_rules) {
1047 kfree(rd);
1048 return NULL;
1049 }
1050
1051 rd->n_reg_rules = num_rules;
1052 rd->alpha2[0] = '9';
1053 rd->alpha2[1] = '8';
1054
1055 return rd;
1056}
1057
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001058/*
1059 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1060 * want to just have the channel structure use these
1061 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001062static u32 map_regdom_flags(u32 rd_flags)
1063{
1064 u32 channel_flags = 0;
1065 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
1066 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1067 if (rd_flags & NL80211_RRF_NO_IBSS)
1068 channel_flags |= IEEE80211_CHAN_NO_IBSS;
1069 if (rd_flags & NL80211_RRF_DFS)
1070 channel_flags |= IEEE80211_CHAN_RADAR;
1071 return channel_flags;
1072}
1073
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001074static int freq_reg_info_regd(struct wiphy *wiphy,
1075 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001076 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001077 const struct ieee80211_reg_rule **reg_rule,
1078 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +01001079{
1080 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001081 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001082 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001083 bool bw_fits = false;
1084
1085 if (!desired_bw_khz)
1086 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +01001087
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001088 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001089
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001090 /*
1091 * Follow the driver's regulatory domain, if present, unless a country
1092 * IE has been processed or a user wants to help complaince further
1093 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001094 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1095 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001096 wiphy->regd)
1097 regd = wiphy->regd;
1098
1099 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001100 return -EINVAL;
1101
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001102 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001103 const struct ieee80211_reg_rule *rr;
1104 const struct ieee80211_freq_range *fr = NULL;
1105 const struct ieee80211_power_rule *pr = NULL;
1106
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001107 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001108 fr = &rr->freq_range;
1109 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001110
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001111 /*
1112 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001113 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001114 * not overwrite it once found
1115 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001116 if (!band_rule_found)
1117 band_rule_found = freq_in_rule_band(fr, center_freq);
1118
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001119 bw_fits = reg_does_bw_fit(fr,
1120 center_freq,
1121 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001122
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001123 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001124 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001125 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001126 }
1127 }
1128
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001129 if (!band_rule_found)
1130 return -ERANGE;
1131
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001132 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001133}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -08001134EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001135
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001136int freq_reg_info(struct wiphy *wiphy,
1137 u32 center_freq,
1138 u32 desired_bw_khz,
1139 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001140{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001141 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001142 return freq_reg_info_regd(wiphy,
1143 center_freq,
1144 desired_bw_khz,
1145 reg_rule,
1146 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001147}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001148
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001149/*
1150 * Note that right now we assume the desired channel bandwidth
1151 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1152 * per channel, the primary and the extension channel). To support
1153 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1154 * new ieee80211_channel.target_bw and re run the regulatory check
1155 * on the wiphy with the target_bw specified. Then we can simply use
1156 * that below for the desired_bw_khz below.
1157 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001158static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1159 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001160{
1161 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001162 u32 flags, bw_flags = 0;
1163 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001164 const struct ieee80211_reg_rule *reg_rule = NULL;
1165 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001166 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001167 struct ieee80211_supported_band *sband;
1168 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001169 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001170
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001171 assert_cfg80211_lock();
1172
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001173 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1174
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001175 sband = wiphy->bands[band];
1176 BUG_ON(chan_idx >= sband->n_channels);
1177 chan = &sband->channels[chan_idx];
1178
1179 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001180
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001181 r = freq_reg_info(wiphy,
1182 MHZ_TO_KHZ(chan->center_freq),
1183 desired_bw_khz,
1184 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001185
1186 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001187 /*
1188 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001189 * with a frequency range on the center_freq's band, since
1190 * IEEE-802.11 allows for a country IE to have a subset of the
1191 * regulatory information provided in a country we ignore
1192 * disabling the channel unless at least one reg rule was
1193 * found on the center_freq's band. For details see this
1194 * clarification:
1195 *
1196 * http://tinyurl.com/11d-clarification
1197 */
1198 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001199 last_request->initiator ==
1200 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001201 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001202 "intact on %s - no rule found in band on "
1203 "Country IE\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001204 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001205 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001206 /*
1207 * In this case we know the country IE has at least one reg rule
1208 * for the band so we respect its band definitions
1209 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001210 if (last_request->initiator ==
1211 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001212 REG_DBG_PRINT("cfg80211: Disabling "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001213 "channel %d MHz on %s due to "
1214 "Country IE\n",
1215 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001216 flags |= IEEE80211_CHAN_DISABLED;
1217 chan->flags = flags;
1218 }
Johannes Berg8318d782008-01-24 19:38:38 +01001219 return;
1220 }
1221
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001222 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001223 freq_range = &reg_rule->freq_range;
1224
1225 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1226 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001227
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001228 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001229 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001230 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001231 /*
1232 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001233 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001234 * settings
1235 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001236 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001237 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001238 chan->max_antenna_gain = chan->orig_mag =
1239 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001240 chan->max_power = chan->orig_mpwr =
1241 (int) MBM_TO_DBM(power_rule->max_eirp);
1242 return;
1243 }
1244
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001245 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001246 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001247 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -04001248 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001249 chan->max_power = min(chan->orig_mpwr,
1250 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001251 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001252 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001253}
1254
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001255static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001256{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001257 unsigned int i;
1258 struct ieee80211_supported_band *sband;
1259
1260 BUG_ON(!wiphy->bands[band]);
1261 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001262
1263 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001264 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001265}
1266
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001267static bool ignore_reg_update(struct wiphy *wiphy,
1268 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001269{
1270 if (!last_request)
1271 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001272 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001273 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001274 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001275 /*
1276 * wiphy->regd will be set once the device has its own
1277 * desired regulatory domain set
1278 */
Johannes Berg5be83de2009-11-19 00:56:28 +01001279 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001280 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001281 return true;
1282 return false;
1283}
1284
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001285static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001286{
Johannes Berg79c97e92009-07-07 03:56:12 +02001287 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001288
Johannes Berg79c97e92009-07-07 03:56:12 +02001289 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1290 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001291}
1292
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001293static void handle_reg_beacon(struct wiphy *wiphy,
1294 unsigned int chan_idx,
1295 struct reg_beacon *reg_beacon)
1296{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001297 struct ieee80211_supported_band *sband;
1298 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001299 bool channel_changed = false;
1300 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001301
1302 assert_cfg80211_lock();
1303
1304 sband = wiphy->bands[reg_beacon->chan.band];
1305 chan = &sband->channels[chan_idx];
1306
1307 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1308 return;
1309
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001310 if (chan->beacon_found)
1311 return;
1312
1313 chan->beacon_found = true;
1314
Johannes Berg5be83de2009-11-19 00:56:28 +01001315 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001316 return;
1317
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001318 chan_before.center_freq = chan->center_freq;
1319 chan_before.flags = chan->flags;
1320
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001321 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001322 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001323 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001324 }
1325
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001326 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001327 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001328 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001329 }
1330
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001331 if (channel_changed)
1332 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001333}
1334
1335/*
1336 * Called when a scan on a wiphy finds a beacon on
1337 * new channel
1338 */
1339static void wiphy_update_new_beacon(struct wiphy *wiphy,
1340 struct reg_beacon *reg_beacon)
1341{
1342 unsigned int i;
1343 struct ieee80211_supported_band *sband;
1344
1345 assert_cfg80211_lock();
1346
1347 if (!wiphy->bands[reg_beacon->chan.band])
1348 return;
1349
1350 sband = wiphy->bands[reg_beacon->chan.band];
1351
1352 for (i = 0; i < sband->n_channels; i++)
1353 handle_reg_beacon(wiphy, i, reg_beacon);
1354}
1355
1356/*
1357 * Called upon reg changes or a new wiphy is added
1358 */
1359static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1360{
1361 unsigned int i;
1362 struct ieee80211_supported_band *sband;
1363 struct reg_beacon *reg_beacon;
1364
1365 assert_cfg80211_lock();
1366
1367 if (list_empty(&reg_beacon_list))
1368 return;
1369
1370 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1371 if (!wiphy->bands[reg_beacon->chan.band])
1372 continue;
1373 sband = wiphy->bands[reg_beacon->chan.band];
1374 for (i = 0; i < sband->n_channels; i++)
1375 handle_reg_beacon(wiphy, i, reg_beacon);
1376 }
1377}
1378
1379static bool reg_is_world_roaming(struct wiphy *wiphy)
1380{
1381 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1382 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1383 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001384 if (last_request &&
1385 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001386 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001387 return true;
1388 return false;
1389}
1390
1391/* Reap the advantages of previously found beacons */
1392static void reg_process_beacons(struct wiphy *wiphy)
1393{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001394 /*
1395 * Means we are just firing up cfg80211, so no beacons would
1396 * have been processed yet.
1397 */
1398 if (!last_request)
1399 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001400 if (!reg_is_world_roaming(wiphy))
1401 return;
1402 wiphy_update_beacon_reg(wiphy);
1403}
1404
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001405static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1406{
1407 if (!chan)
1408 return true;
1409 if (chan->flags & IEEE80211_CHAN_DISABLED)
1410 return true;
1411 /* This would happen when regulatory rules disallow HT40 completely */
1412 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1413 return true;
1414 return false;
1415}
1416
1417static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1418 enum ieee80211_band band,
1419 unsigned int chan_idx)
1420{
1421 struct ieee80211_supported_band *sband;
1422 struct ieee80211_channel *channel;
1423 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1424 unsigned int i;
1425
1426 assert_cfg80211_lock();
1427
1428 sband = wiphy->bands[band];
1429 BUG_ON(chan_idx >= sband->n_channels);
1430 channel = &sband->channels[chan_idx];
1431
1432 if (is_ht40_not_allowed(channel)) {
1433 channel->flags |= IEEE80211_CHAN_NO_HT40;
1434 return;
1435 }
1436
1437 /*
1438 * We need to ensure the extension channels exist to
1439 * be able to use HT40- or HT40+, this finds them (or not)
1440 */
1441 for (i = 0; i < sband->n_channels; i++) {
1442 struct ieee80211_channel *c = &sband->channels[i];
1443 if (c->center_freq == (channel->center_freq - 20))
1444 channel_before = c;
1445 if (c->center_freq == (channel->center_freq + 20))
1446 channel_after = c;
1447 }
1448
1449 /*
1450 * Please note that this assumes target bandwidth is 20 MHz,
1451 * if that ever changes we also need to change the below logic
1452 * to include that as well.
1453 */
1454 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001455 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001456 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001457 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001458
1459 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001460 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001461 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001462 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001463}
1464
1465static void reg_process_ht_flags_band(struct wiphy *wiphy,
1466 enum ieee80211_band band)
1467{
1468 unsigned int i;
1469 struct ieee80211_supported_band *sband;
1470
1471 BUG_ON(!wiphy->bands[band]);
1472 sband = wiphy->bands[band];
1473
1474 for (i = 0; i < sband->n_channels; i++)
1475 reg_process_ht_flags_channel(wiphy, band, i);
1476}
1477
1478static void reg_process_ht_flags(struct wiphy *wiphy)
1479{
1480 enum ieee80211_band band;
1481
1482 if (!wiphy)
1483 return;
1484
1485 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1486 if (wiphy->bands[band])
1487 reg_process_ht_flags_band(wiphy, band);
1488 }
1489
1490}
1491
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001492void wiphy_update_regulatory(struct wiphy *wiphy,
1493 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001494{
1495 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001496
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001497 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001498 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001499 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001500 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001501 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001502 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001503out:
1504 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001505 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001506 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001507 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001508}
1509
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001510static void handle_channel_custom(struct wiphy *wiphy,
1511 enum ieee80211_band band,
1512 unsigned int chan_idx,
1513 const struct ieee80211_regdomain *regd)
1514{
1515 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001516 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1517 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001518 const struct ieee80211_reg_rule *reg_rule = NULL;
1519 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001520 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001521 struct ieee80211_supported_band *sband;
1522 struct ieee80211_channel *chan;
1523
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001524 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001525
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001526 sband = wiphy->bands[band];
1527 BUG_ON(chan_idx >= sband->n_channels);
1528 chan = &sband->channels[chan_idx];
1529
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001530 r = freq_reg_info_regd(wiphy,
1531 MHZ_TO_KHZ(chan->center_freq),
1532 desired_bw_khz,
1533 &reg_rule,
1534 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001535
1536 if (r) {
1537 chan->flags = IEEE80211_CHAN_DISABLED;
1538 return;
1539 }
1540
1541 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001542 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001543
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001544 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1545 bw_flags = IEEE80211_CHAN_NO_HT40;
1546
1547 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001548 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001549 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1550}
1551
1552static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1553 const struct ieee80211_regdomain *regd)
1554{
1555 unsigned int i;
1556 struct ieee80211_supported_band *sband;
1557
1558 BUG_ON(!wiphy->bands[band]);
1559 sband = wiphy->bands[band];
1560
1561 for (i = 0; i < sband->n_channels; i++)
1562 handle_channel_custom(wiphy, band, i, regd);
1563}
1564
1565/* Used by drivers prior to wiphy registration */
1566void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1567 const struct ieee80211_regdomain *regd)
1568{
1569 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001570 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001571
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001572 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001573 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001574 if (!wiphy->bands[band])
1575 continue;
1576 handle_band_custom(wiphy, band, regd);
1577 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001578 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001579 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001580
1581 /*
1582 * no point in calling this if it won't have any effect
1583 * on your device's supportd bands.
1584 */
1585 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001586}
1587EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1588
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001589/*
1590 * Return value which can be used by ignore_request() to indicate
1591 * it has been determined we should intersect two regulatory domains
1592 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001593#define REG_INTERSECT 1
1594
Johannes Berg84fa4f42008-10-24 20:32:23 +02001595/* This has the logic which determines when a new request
1596 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001597static int ignore_request(struct wiphy *wiphy,
1598 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001599{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001600 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001601
1602 assert_cfg80211_lock();
1603
Johannes Berg84fa4f42008-10-24 20:32:23 +02001604 /* All initial requests are respected */
1605 if (!last_request)
1606 return 0;
1607
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001608 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001609 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001610 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001611 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001612
1613 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1614
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001615 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001616 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001617 if (last_request->initiator ==
1618 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001619 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001620 /*
1621 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001622 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001623 * intersect them, but that seems unlikely
1624 * to be correct. Reject second one for now.
1625 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001626 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001627 return -EOPNOTSUPP;
1628 return -EALREADY;
1629 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001630 /*
1631 * Two consecutive Country IE hints on the same wiphy.
1632 * This should be picked up early by the driver/stack
1633 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001634 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001635 return 0;
1636 return -EALREADY;
1637 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001638 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001639 case NL80211_REGDOM_SET_BY_DRIVER:
1640 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001641 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001642 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001643 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001644 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001645
1646 /*
1647 * This would happen if you unplug and plug your card
1648 * back in or if you add a new device for which the previously
1649 * loaded card also agrees on the regulatory domain.
1650 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001651 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001652 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001653 return -EALREADY;
1654
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001655 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001656 case NL80211_REGDOM_SET_BY_USER:
1657 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001658 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001659 /*
1660 * If the user knows better the user should set the regdom
1661 * to their country before the IE is picked up
1662 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001663 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001664 last_request->intersect)
1665 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001666 /*
1667 * Process user requests only after previous user/driver/core
1668 * requests have been processed
1669 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001670 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1671 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1672 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001673 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001674 return -EAGAIN;
1675 }
1676
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001677 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001678 return -EALREADY;
1679
Johannes Berg84fa4f42008-10-24 20:32:23 +02001680 return 0;
1681 }
1682
1683 return -EINVAL;
1684}
1685
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001686/**
1687 * __regulatory_hint - hint to the wireless core a regulatory domain
1688 * @wiphy: if the hint comes from country information from an AP, this
1689 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001690 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001691 *
1692 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001693 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001694 *
1695 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1696 * already been set or other standard error codes.
1697 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001698 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001699 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001700static int __regulatory_hint(struct wiphy *wiphy,
1701 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001702{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001703 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001704 int r = 0;
1705
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001706 assert_cfg80211_lock();
1707
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001708 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001709
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001710 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001711 if (pending_request->initiator ==
1712 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001713 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001714 if (r) {
1715 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001716 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001717 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001718 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001719 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001720 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001721 /*
1722 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001723 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001724 * wiphy
1725 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001726 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001727 pending_request->initiator ==
1728 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001729 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001730 if (r) {
1731 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001732 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001733 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001734 r = -EALREADY;
1735 goto new_request;
1736 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001737 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001738 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001739 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001740
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001741new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001742 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001743
1744 last_request = pending_request;
1745 last_request->intersect = intersect;
1746
1747 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001748
1749 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001750 if (r < 0) {
1751 /*
1752 * Since CRDA will not be called in this case as we already
1753 * have applied the requested regulatory domain before we just
1754 * inform userspace we have processed the request
1755 */
1756 if (r == -EALREADY)
1757 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001758 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001759 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001760
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001761 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001762}
1763
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001764/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001765static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001766{
1767 int r = 0;
1768 struct wiphy *wiphy = NULL;
1769
1770 BUG_ON(!reg_request->alpha2);
1771
1772 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001773 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001774
1775 if (wiphy_idx_valid(reg_request->wiphy_idx))
1776 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1777
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001778 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001779 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001780 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001781 goto out;
1782 }
1783
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001784 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001785 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001786 if (r == -EALREADY && wiphy &&
1787 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001788 wiphy_update_regulatory(wiphy, reg_request->initiator);
1789out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001790 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001791 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001792}
1793
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001794/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001795static void reg_process_pending_hints(void)
1796 {
1797 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001798
1799 spin_lock(&reg_requests_lock);
1800 while (!list_empty(&reg_requests_list)) {
1801 reg_request = list_first_entry(&reg_requests_list,
1802 struct regulatory_request,
1803 list);
1804 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001805
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001806 spin_unlock(&reg_requests_lock);
1807 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001808 spin_lock(&reg_requests_lock);
1809 }
1810 spin_unlock(&reg_requests_lock);
1811}
1812
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001813/* Processes beacon hints -- this has nothing to do with country IEs */
1814static void reg_process_pending_beacon_hints(void)
1815{
Johannes Berg79c97e92009-07-07 03:56:12 +02001816 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001817 struct reg_beacon *pending_beacon, *tmp;
1818
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001819 /*
1820 * No need to hold the reg_mutex here as we just touch wiphys
1821 * and do not read or access regulatory variables.
1822 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001823 mutex_lock(&cfg80211_mutex);
1824
1825 /* This goes through the _pending_ beacon list */
1826 spin_lock_bh(&reg_pending_beacons_lock);
1827
1828 if (list_empty(&reg_pending_beacons)) {
1829 spin_unlock_bh(&reg_pending_beacons_lock);
1830 goto out;
1831 }
1832
1833 list_for_each_entry_safe(pending_beacon, tmp,
1834 &reg_pending_beacons, list) {
1835
1836 list_del_init(&pending_beacon->list);
1837
1838 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001839 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1840 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001841
1842 /* Remembers the beacon hint for new wiphys or reg changes */
1843 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1844 }
1845
1846 spin_unlock_bh(&reg_pending_beacons_lock);
1847out:
1848 mutex_unlock(&cfg80211_mutex);
1849}
1850
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001851static void reg_todo(struct work_struct *work)
1852{
1853 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001854 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001855}
1856
1857static DECLARE_WORK(reg_work, reg_todo);
1858
1859static void queue_regulatory_request(struct regulatory_request *request)
1860{
1861 spin_lock(&reg_requests_lock);
1862 list_add_tail(&request->list, &reg_requests_list);
1863 spin_unlock(&reg_requests_lock);
1864
1865 schedule_work(&reg_work);
1866}
1867
1868/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001869static int regulatory_hint_core(const char *alpha2)
1870{
1871 struct regulatory_request *request;
1872
1873 BUG_ON(last_request);
1874
1875 request = kzalloc(sizeof(struct regulatory_request),
1876 GFP_KERNEL);
1877 if (!request)
1878 return -ENOMEM;
1879
1880 request->alpha2[0] = alpha2[0];
1881 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001882 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001883
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001884 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001885
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001886 /*
1887 * This ensures last_request is populated once modules
1888 * come swinging in and calling regulatory hints and
1889 * wiphy_apply_custom_regulatory().
1890 */
1891 flush_scheduled_work();
1892
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001893 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001894}
1895
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001896/* User hints */
1897int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001898{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001899 struct regulatory_request *request;
1900
Johannes Bergbe3d4812008-10-24 20:32:21 +02001901 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001902
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001903 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1904 if (!request)
1905 return -ENOMEM;
1906
1907 request->wiphy_idx = WIPHY_IDX_STALE;
1908 request->alpha2[0] = alpha2[0];
1909 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001910 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001911
1912 queue_regulatory_request(request);
1913
1914 return 0;
1915}
1916
1917/* Driver hints */
1918int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1919{
1920 struct regulatory_request *request;
1921
1922 BUG_ON(!alpha2);
1923 BUG_ON(!wiphy);
1924
1925 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1926 if (!request)
1927 return -ENOMEM;
1928
1929 request->wiphy_idx = get_wiphy_idx(wiphy);
1930
1931 /* Must have registered wiphy first */
1932 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1933
1934 request->alpha2[0] = alpha2[0];
1935 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001936 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001937
1938 queue_regulatory_request(request);
1939
1940 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001941}
1942EXPORT_SYMBOL(regulatory_hint);
1943
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001944/* Caller must hold reg_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001945static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1946 u32 country_ie_checksum)
1947{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001948 struct wiphy *request_wiphy;
1949
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001950 assert_reg_lock();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001951
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001952 if (unlikely(last_request->initiator !=
1953 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1954 return false;
1955
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001956 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1957
1958 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001959 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001960
1961 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001962 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001963 /*
1964 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001965 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001966 * on the device
1967 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001968 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1969 return true;
1970 return false;
1971}
1972
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001973/*
1974 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1975 * therefore cannot iterate over the rdev list here.
1976 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001977void regulatory_hint_11d(struct wiphy *wiphy,
1978 u8 *country_ie,
1979 u8 country_ie_len)
1980{
1981 struct ieee80211_regdomain *rd = NULL;
1982 char alpha2[2];
1983 u32 checksum = 0;
1984 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001985 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001986
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001987 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001988
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001989 if (unlikely(!last_request))
1990 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001991
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001992 /* IE len must be evenly divisible by 2 */
1993 if (country_ie_len & 0x01)
1994 goto out;
1995
1996 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1997 goto out;
1998
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001999 /*
2000 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002001 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002002 * we were able to process the last regulatory_hint_11d() call
2003 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002004 if (country_ie_regdomain)
2005 goto out;
2006
2007 alpha2[0] = country_ie[0];
2008 alpha2[1] = country_ie[1];
2009
2010 if (country_ie[2] == 'I')
2011 env = ENVIRON_INDOOR;
2012 else if (country_ie[2] == 'O')
2013 env = ENVIRON_OUTDOOR;
2014
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002015 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002016 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002017 * We leave conflict resolution to the workqueue, where can hold
2018 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002019 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04002020 if (likely(last_request->initiator ==
2021 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002022 wiphy_idx_valid(last_request->wiphy_idx)))
2023 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002024
2025 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002026 if (!rd) {
2027 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002028 goto out;
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002029 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002030
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002031 /*
2032 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002033 * the future when we want to add suspend/resume support and having
2034 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002035 * move to another AP. Right now we just trust the first AP.
2036 *
2037 * If we hit this before we add this support we want to be informed of
2038 * it as it would indicate a mistake in the current design
2039 */
2040 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002041 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002042
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002043 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2044 if (!request)
2045 goto free_rd_out;
2046
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002047 /*
2048 * We keep this around for when CRDA comes back with a response so
2049 * we can intersect with that
2050 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002051 country_ie_regdomain = rd;
2052
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002053 request->wiphy_idx = get_wiphy_idx(wiphy);
2054 request->alpha2[0] = rd->alpha2[0];
2055 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002056 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002057 request->country_ie_checksum = checksum;
2058 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002059
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002060 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002061
2062 queue_regulatory_request(request);
2063
2064 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002065
2066free_rd_out:
2067 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002068out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002069 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002070}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002071
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002072static bool freq_is_chan_12_13_14(u16 freq)
2073{
2074 if (freq == ieee80211_channel_to_frequency(12) ||
2075 freq == ieee80211_channel_to_frequency(13) ||
2076 freq == ieee80211_channel_to_frequency(14))
2077 return true;
2078 return false;
2079}
2080
2081int regulatory_hint_found_beacon(struct wiphy *wiphy,
2082 struct ieee80211_channel *beacon_chan,
2083 gfp_t gfp)
2084{
2085 struct reg_beacon *reg_beacon;
2086
2087 if (likely((beacon_chan->beacon_found ||
2088 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
2089 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
2090 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
2091 return 0;
2092
2093 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2094 if (!reg_beacon)
2095 return -ENOMEM;
2096
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002097 REG_DBG_PRINT("cfg80211: Found new beacon on "
2098 "frequency: %d MHz (Ch %d) on %s\n",
2099 beacon_chan->center_freq,
2100 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2101 wiphy_name(wiphy));
2102
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002103 memcpy(&reg_beacon->chan, beacon_chan,
2104 sizeof(struct ieee80211_channel));
2105
2106
2107 /*
2108 * Since we can be called from BH or and non-BH context
2109 * we must use spin_lock_bh()
2110 */
2111 spin_lock_bh(&reg_pending_beacons_lock);
2112 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2113 spin_unlock_bh(&reg_pending_beacons_lock);
2114
2115 schedule_work(&reg_work);
2116
2117 return 0;
2118}
2119
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002120static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002121{
2122 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002123 const struct ieee80211_reg_rule *reg_rule = NULL;
2124 const struct ieee80211_freq_range *freq_range = NULL;
2125 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002126
Kalle Valo269ac5f2009-12-01 10:47:15 +02002127 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002128 "(max_antenna_gain, max_eirp)\n");
2129
2130 for (i = 0; i < rd->n_reg_rules; i++) {
2131 reg_rule = &rd->reg_rules[i];
2132 freq_range = &reg_rule->freq_range;
2133 power_rule = &reg_rule->power_rule;
2134
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002135 /*
2136 * There may not be documentation for max antenna gain
2137 * in certain regions
2138 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002139 if (power_rule->max_antenna_gain)
Kalle Valo269ac5f2009-12-01 10:47:15 +02002140 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002141 "(%d mBi, %d mBm)\n",
2142 freq_range->start_freq_khz,
2143 freq_range->end_freq_khz,
2144 freq_range->max_bandwidth_khz,
2145 power_rule->max_antenna_gain,
2146 power_rule->max_eirp);
2147 else
Kalle Valo269ac5f2009-12-01 10:47:15 +02002148 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002149 "(N/A, %d mBm)\n",
2150 freq_range->start_freq_khz,
2151 freq_range->end_freq_khz,
2152 freq_range->max_bandwidth_khz,
2153 power_rule->max_eirp);
2154 }
2155}
2156
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002157static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002158{
2159
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002160 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002161
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002162 if (last_request->initiator ==
2163 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002164 struct cfg80211_registered_device *rdev;
2165 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002166 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002167 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002168 printk(KERN_INFO "cfg80211: Current regulatory "
2169 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002170 rdev->country_ie_alpha2[0],
2171 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002172 } else
2173 printk(KERN_INFO "cfg80211: Current regulatory "
2174 "domain intersected: \n");
2175 } else
2176 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08002177 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002178 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002179 printk(KERN_INFO "cfg80211: World regulatory "
2180 "domain updated:\n");
2181 else {
2182 if (is_unknown_alpha2(rd->alpha2))
2183 printk(KERN_INFO "cfg80211: Regulatory domain "
2184 "changed to driver built-in settings "
2185 "(unknown country)\n");
2186 else
2187 printk(KERN_INFO "cfg80211: Regulatory domain "
2188 "changed to country: %c%c\n",
2189 rd->alpha2[0], rd->alpha2[1]);
2190 }
2191 print_rd_rules(rd);
2192}
2193
Johannes Berg2df78162008-10-28 16:49:41 +01002194static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002195{
2196 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2197 rd->alpha2[0], rd->alpha2[1]);
2198 print_rd_rules(rd);
2199}
2200
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002201#ifdef CONFIG_CFG80211_REG_DEBUG
2202static void reg_country_ie_process_debug(
2203 const struct ieee80211_regdomain *rd,
2204 const struct ieee80211_regdomain *country_ie_regdomain,
2205 const struct ieee80211_regdomain *intersected_rd)
2206{
2207 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2208 print_regdomain_info(country_ie_regdomain);
2209 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2210 print_regdomain_info(rd);
2211 if (intersected_rd) {
2212 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2213 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08002214 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002215 return;
2216 }
2217 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2218}
2219#else
2220static inline void reg_country_ie_process_debug(
2221 const struct ieee80211_regdomain *rd,
2222 const struct ieee80211_regdomain *country_ie_regdomain,
2223 const struct ieee80211_regdomain *intersected_rd)
2224{
2225}
2226#endif
2227
Johannes Bergd2372b32008-10-24 20:32:20 +02002228/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002229static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002230{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002231 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002232 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002233 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002234 /* Some basic sanity checks first */
2235
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002236 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002237 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002238 return -EINVAL;
2239 update_world_regdomain(rd);
2240 return 0;
2241 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002242
2243 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2244 !is_unknown_alpha2(rd->alpha2))
2245 return -EINVAL;
2246
Johannes Bergf6037d02008-10-21 11:01:33 +02002247 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002248 return -EINVAL;
2249
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002250 /*
2251 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002252 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002253 * and the pending request came in from a country IE
2254 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002255 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002256 /*
2257 * If someone else asked us to change the rd lets only bother
2258 * checking if the alpha2 changes if CRDA was already called
2259 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002260 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002261 return -EINVAL;
2262 }
2263
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002264 /*
2265 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002266 * and finally inform them of what we have done, in case they want
2267 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002268 * internal EEPROM data
2269 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002270
Johannes Bergf6037d02008-10-21 11:01:33 +02002271 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002272 return -EINVAL;
2273
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002274 if (!is_valid_rd(rd)) {
2275 printk(KERN_ERR "cfg80211: Invalid "
2276 "regulatory domain detected:\n");
2277 print_regdomain_info(rd);
2278 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002279 }
2280
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002281 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2282
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002283 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002284 int r;
2285
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002286 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002287 reset_regdomains();
2288 cfg80211_regdomain = rd;
2289 return 0;
2290 }
2291
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002292 /*
2293 * For a driver hint, lets copy the regulatory domain the
2294 * driver wanted to the wiphy to deal with conflicts
2295 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002296
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002297 /*
2298 * Userspace could have sent two replies with only
2299 * one kernel request.
2300 */
2301 if (request_wiphy->regd)
2302 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002303
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002304 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002305 if (r)
2306 return r;
2307
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002308 reset_regdomains();
2309 cfg80211_regdomain = rd;
2310 return 0;
2311 }
2312
2313 /* Intersection requires a bit more work */
2314
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002315 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002316
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002317 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2318 if (!intersected_rd)
2319 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002320
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002321 /*
2322 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002323 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002324 * domain we keep it for its private use
2325 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002326 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002327 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002328 else
2329 kfree(rd);
2330
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002331 rd = NULL;
2332
2333 reset_regdomains();
2334 cfg80211_regdomain = intersected_rd;
2335
2336 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002337 }
2338
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002339 /*
2340 * Country IE requests are handled a bit differently, we intersect
2341 * the country IE rd with what CRDA believes that country should have
2342 */
2343
Luis R. Rodriguez729e9c72009-05-31 18:24:34 -04002344 /*
2345 * Userspace could have sent two replies with only
2346 * one kernel request. By the second reply we would have
2347 * already processed and consumed the country_ie_regdomain.
2348 */
2349 if (!country_ie_regdomain)
2350 return -EALREADY;
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002351 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002352
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002353 /*
2354 * Intersect what CRDA returned and our what we
2355 * had built from the Country IE received
2356 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002357
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002358 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002359
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002360 reg_country_ie_process_debug(rd,
2361 country_ie_regdomain,
2362 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002363
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002364 kfree(country_ie_regdomain);
2365 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002366
2367 if (!intersected_rd)
2368 return -EINVAL;
2369
Johannes Berg79c97e92009-07-07 03:56:12 +02002370 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002371
Johannes Berg79c97e92009-07-07 03:56:12 +02002372 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2373 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2374 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002375
2376 BUG_ON(intersected_rd == rd);
2377
2378 kfree(rd);
2379 rd = NULL;
2380
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002381 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002382 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002383
2384 return 0;
2385}
2386
2387
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002388/*
2389 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002390 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002391 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2392 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002393int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002394{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002395 int r;
2396
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002397 assert_cfg80211_lock();
2398
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002399 mutex_lock(&reg_mutex);
2400
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002401 /* Note that this doesn't update the wiphys, this is done below */
2402 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002403 if (r) {
2404 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002405 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002406 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002407 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002408
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002409 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002410 if (!last_request->intersect)
2411 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002412
2413 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002414 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002415
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002416 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002417
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002418 nl80211_send_reg_change_event(last_request);
2419
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002420 mutex_unlock(&reg_mutex);
2421
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002422 return r;
2423}
2424
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002425/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002426void reg_device_remove(struct wiphy *wiphy)
2427{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002428 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002429
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002430 assert_cfg80211_lock();
2431
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002432 mutex_lock(&reg_mutex);
2433
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002434 kfree(wiphy->regd);
2435
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002436 if (last_request)
2437 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002438
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002439 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002440 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002441
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002442 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002443 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002444out:
2445 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002446}
2447
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002448int regulatory_init(void)
2449{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002450 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002451
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002452 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2453 if (IS_ERR(reg_pdev))
2454 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002455
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002456 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002457 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002458
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002459 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002460
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002461 /* We always try to get an update for the static regdomain */
2462 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002463 if (err) {
2464 if (err == -ENOMEM)
2465 return err;
2466 /*
2467 * N.B. kobject_uevent_env() can fail mainly for when we're out
2468 * memory which is handled and propagated appropriately above
2469 * but it can also fail during a netlink_broadcast() or during
2470 * early boot for call_usermodehelper(). For now treat these
2471 * errors as non-fatal.
2472 */
2473 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2474 "to call CRDA during init");
2475#ifdef CONFIG_CFG80211_REG_DEBUG
2476 /* We want to find out exactly why when debugging */
2477 WARN_ON(err);
2478#endif
2479 }
Johannes Berg734366d2008-09-15 10:56:48 +02002480
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002481 /*
2482 * Finally, if the user set the module parameter treat it
2483 * as a user hint.
2484 */
2485 if (!is_world_regdom(ieee80211_regdom))
2486 regulatory_hint_user(ieee80211_regdom);
2487
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002488 return 0;
2489}
2490
2491void regulatory_exit(void)
2492{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002493 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002494 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002495
2496 cancel_work_sync(&reg_work);
2497
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002498 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002499 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002500
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002501 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002502
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002503 kfree(country_ie_regdomain);
2504 country_ie_regdomain = NULL;
2505
Johannes Bergf6037d02008-10-21 11:01:33 +02002506 kfree(last_request);
2507
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002508 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002509
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002510 spin_lock_bh(&reg_pending_beacons_lock);
2511 if (!list_empty(&reg_pending_beacons)) {
2512 list_for_each_entry_safe(reg_beacon, btmp,
2513 &reg_pending_beacons, list) {
2514 list_del(&reg_beacon->list);
2515 kfree(reg_beacon);
2516 }
2517 }
2518 spin_unlock_bh(&reg_pending_beacons_lock);
2519
2520 if (!list_empty(&reg_beacon_list)) {
2521 list_for_each_entry_safe(reg_beacon, btmp,
2522 &reg_beacon_list, list) {
2523 list_del(&reg_beacon->list);
2524 kfree(reg_beacon);
2525 }
2526 }
2527
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002528 spin_lock(&reg_requests_lock);
2529 if (!list_empty(&reg_requests_list)) {
2530 list_for_each_entry_safe(reg_request, tmp,
2531 &reg_requests_list, list) {
2532 list_del(&reg_request->list);
2533 kfree(reg_request);
2534 }
2535 }
2536 spin_unlock(&reg_requests_lock);
2537
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002538 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002539 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002540}