blob: f3b77f7b8e3d2aa4303b87f916b2ffeb882a7d8f [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
Luis R. Rodrigueza0f2e0f2010-01-14 13:27:46 -0500588 if (triplet->chans.num_channels == 0)
589 return 0;
590
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500591 /* Monitonically increasing channel order */
592 if (triplet->chans.first_channel <= end_subband_chan)
593 return 0;
594
595 /* 2 GHz */
596 if (triplet->chans.first_channel <= 14) {
597 end_channel = triplet->chans.first_channel +
598 triplet->chans.num_channels - 1;
599 }
600 else {
601 end_channel = triplet->chans.first_channel +
602 (4 * (triplet->chans.num_channels - 1));
603 next_band = IEEE80211_BAND_5GHZ;
604 }
605
606 if (band != next_band) {
607 *country_ie -= 3;
608 *country_ie_len += 3;
609 break;
610 }
611
612 if (orig_max_power != triplet->chans.max_power) {
613 *country_ie -= 3;
614 *country_ie_len += 3;
615 break;
616 }
617
618 cur_channel = triplet->chans.first_channel;
619
620 /* The key is finding the right next expected channel */
621 if (band == IEEE80211_BAND_2GHZ)
622 next_expected_chan = end_subband_chan + 1;
623 else
624 next_expected_chan = end_subband_chan + 4;
625
626 if (cur_channel != next_expected_chan) {
627 *country_ie -= 3;
628 *country_ie_len += 3;
629 break;
630 }
631
632 end_subband_chan = end_channel;
633
634 /* Move to the next one */
635 *country_ie += 3;
636 *country_ie_len -= 3;
637
638 /*
639 * Padding needs to be dealt with if we processed
640 * some channels.
641 */
642 if (*country_ie_len == 1) {
643 *country_ie += 1;
644 *country_ie_len -= 1;
645 break;
646 }
647
648 /* If seen, the IE is invalid */
649 if (*country_ie_len == 2)
650 return 0;
651 }
652
653 if (end_subband_chan == orig_end_channel) {
654 *country_ie = triplets_start;
655 *country_ie_len = len_at_triplet;
656 return orig_end_channel;
657 }
658
659 return end_subband_chan;
660}
661
662/*
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500663 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800664 * structure has a lot of information which the IE doesn't yet have,
665 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500666 * with our userspace regulatory agent to get lower bounds.
667 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800668static struct ieee80211_regdomain *country_ie_2_rd(
669 u8 *country_ie,
670 u8 country_ie_len,
671 u32 *checksum)
672{
673 struct ieee80211_regdomain *rd = NULL;
674 unsigned int i = 0;
675 char alpha2[2];
676 u32 flags = 0;
677 u32 num_rules = 0, size_of_regd = 0;
678 u8 *triplets_start = NULL;
679 u8 len_at_triplet = 0;
680 /* the last channel we have registered in a subband (triplet) */
681 int last_sub_max_channel = 0;
682
683 *checksum = 0xDEADBEEF;
684
685 /* Country IE requirements */
686 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
687 country_ie_len & 0x01);
688
689 alpha2[0] = country_ie[0];
690 alpha2[1] = country_ie[1];
691
692 /*
693 * Third octet can be:
694 * 'I' - Indoor
695 * 'O' - Outdoor
696 *
697 * anything else we assume is no restrictions
698 */
699 if (country_ie[2] == 'I')
700 flags = NL80211_RRF_NO_OUTDOOR;
701 else if (country_ie[2] == 'O')
702 flags = NL80211_RRF_NO_INDOOR;
703
704 country_ie += 3;
705 country_ie_len -= 3;
706
707 triplets_start = country_ie;
708 len_at_triplet = country_ie_len;
709
710 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
711
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500712 /*
713 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800714 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500715 * for each channel subband
716 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800717 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800718 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800719 struct ieee80211_country_ie_triplet *triplet =
720 (struct ieee80211_country_ie_triplet *) country_ie;
721 int cur_sub_max_channel = 0, cur_channel = 0;
722
723 if (triplet->ext.reg_extension_id >=
724 IEEE80211_COUNTRY_EXTENSION_ID) {
725 country_ie += 3;
726 country_ie_len -= 3;
727 continue;
728 }
729
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500730 /*
731 * APs can add padding to make length divisible
732 * by two, required by the spec.
733 */
734 if (triplet->chans.first_channel == 0) {
735 country_ie++;
736 country_ie_len--;
737 /* This is expected to be at the very end only */
738 if (country_ie_len != 0)
739 return NULL;
740 break;
741 }
742
Luis R. Rodrigueza0f2e0f2010-01-14 13:27:46 -0500743 if (triplet->chans.num_channels == 0)
744 return NULL;
745
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800746 /* 2 GHz */
747 if (triplet->chans.first_channel <= 14)
748 end_channel = triplet->chans.first_channel +
Luis R. Rodrigueze99c7cd2010-01-07 17:24:55 -0500749 triplet->chans.num_channels - 1;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800750 else
751 /*
752 * 5 GHz -- For example in country IEs if the first
753 * channel given is 36 and the number of channels is 4
754 * then the individual channel numbers defined for the
755 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
756 * and not 36, 37, 38, 39.
757 *
758 * See: http://tinyurl.com/11d-clarification
759 */
760 end_channel = triplet->chans.first_channel +
761 (4 * (triplet->chans.num_channels - 1));
762
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800763 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500764
765 /*
766 * Enhancement for APs that send a triplet for every channel
767 * or for whatever reason sends triplets with multiple channels
768 * separated when in fact they should be together.
769 */
770 end_channel = max_subband_chan(cur_channel,
771 end_channel,
772 triplet->chans.max_power,
773 &country_ie,
774 &country_ie_len);
775 if (!end_channel)
776 return NULL;
777
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800778 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800779
780 /* Basic sanity check */
781 if (cur_sub_max_channel < cur_channel)
782 return NULL;
783
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500784 /*
785 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800786 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500787 * increasing
788 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800789 if (last_sub_max_channel) {
790 if (cur_channel <= last_sub_max_channel)
791 return NULL;
792 if (cur_sub_max_channel <= last_sub_max_channel)
793 return NULL;
794 }
795
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500796 /*
797 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800798 * we can throw ext triplets as part of this soup,
799 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500800 * don't support them
801 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800802 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
803 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
804 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
805
806 last_sub_max_channel = cur_sub_max_channel;
807
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800808 num_rules++;
809
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500810 if (country_ie_len >= 3) {
811 country_ie += 3;
812 country_ie_len -= 3;
813 }
814
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500815 /*
816 * Note: this is not a IEEE requirement but
817 * simply a memory requirement
818 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800819 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
820 return NULL;
821 }
822
823 country_ie = triplets_start;
824 country_ie_len = len_at_triplet;
825
826 size_of_regd = sizeof(struct ieee80211_regdomain) +
827 (num_rules * sizeof(struct ieee80211_reg_rule));
828
829 rd = kzalloc(size_of_regd, GFP_KERNEL);
830 if (!rd)
831 return NULL;
832
833 rd->n_reg_rules = num_rules;
834 rd->alpha2[0] = alpha2[0];
835 rd->alpha2[1] = alpha2[1];
836
837 /* This time around we fill in the rd */
838 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800839 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800840 struct ieee80211_country_ie_triplet *triplet =
841 (struct ieee80211_country_ie_triplet *) country_ie;
842 struct ieee80211_reg_rule *reg_rule = NULL;
843 struct ieee80211_freq_range *freq_range = NULL;
844 struct ieee80211_power_rule *power_rule = NULL;
845
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500846 /*
847 * Must parse if dot11RegulatoryClassesRequired is true,
848 * we don't support this yet
849 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800850 if (triplet->ext.reg_extension_id >=
851 IEEE80211_COUNTRY_EXTENSION_ID) {
852 country_ie += 3;
853 country_ie_len -= 3;
854 continue;
855 }
856
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500857 if (triplet->chans.first_channel == 0) {
858 country_ie++;
859 country_ie_len--;
860 break;
861 }
862
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800863 reg_rule = &rd->reg_rules[i];
864 freq_range = &reg_rule->freq_range;
865 power_rule = &reg_rule->power_rule;
866
867 reg_rule->flags = flags;
868
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800869 /* 2 GHz */
870 if (triplet->chans.first_channel <= 14)
871 end_channel = triplet->chans.first_channel +
Luis R. Rodrigueze99c7cd2010-01-07 17:24:55 -0500872 triplet->chans.num_channels -1;
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800873 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800874 end_channel = triplet->chans.first_channel +
875 (4 * (triplet->chans.num_channels - 1));
876
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500877 end_channel = max_subband_chan(triplet->chans.first_channel,
878 end_channel,
879 triplet->chans.max_power,
880 &country_ie,
881 &country_ie_len);
882
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500883 /*
884 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800885 * the actual band edge, not the center of freq for
886 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500887 * the channels passed
888 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800889 freq_range->start_freq_khz =
890 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
891 triplet->chans.first_channel) - 10);
892 freq_range->end_freq_khz =
893 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800894 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800895
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500896 /*
897 * These are large arbitrary values we use to intersect later.
898 * Increment this if we ever support >= 40 MHz channels
899 * in IEEE 802.11
900 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800901 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
902 power_rule->max_antenna_gain = DBI_TO_MBI(100);
Luis R. Rodriguez08030db2010-01-07 17:24:56 -0500903 power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800904
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800905 i++;
906
Luis R. Rodriguezcc5d8a32010-01-07 17:24:57 -0500907 if (country_ie_len >= 3) {
908 country_ie += 3;
909 country_ie_len -= 3;
910 }
911
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800912 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
913 }
914
915 return rd;
916}
917
918
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500919/*
920 * Helper for regdom_intersect(), this does the real
921 * mathematical intersection fun
922 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700923static int reg_rules_intersect(
924 const struct ieee80211_reg_rule *rule1,
925 const struct ieee80211_reg_rule *rule2,
926 struct ieee80211_reg_rule *intersected_rule)
927{
928 const struct ieee80211_freq_range *freq_range1, *freq_range2;
929 struct ieee80211_freq_range *freq_range;
930 const struct ieee80211_power_rule *power_rule1, *power_rule2;
931 struct ieee80211_power_rule *power_rule;
932 u32 freq_diff;
933
934 freq_range1 = &rule1->freq_range;
935 freq_range2 = &rule2->freq_range;
936 freq_range = &intersected_rule->freq_range;
937
938 power_rule1 = &rule1->power_rule;
939 power_rule2 = &rule2->power_rule;
940 power_rule = &intersected_rule->power_rule;
941
942 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
943 freq_range2->start_freq_khz);
944 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
945 freq_range2->end_freq_khz);
946 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
947 freq_range2->max_bandwidth_khz);
948
949 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
950 if (freq_range->max_bandwidth_khz > freq_diff)
951 freq_range->max_bandwidth_khz = freq_diff;
952
953 power_rule->max_eirp = min(power_rule1->max_eirp,
954 power_rule2->max_eirp);
955 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
956 power_rule2->max_antenna_gain);
957
958 intersected_rule->flags = (rule1->flags | rule2->flags);
959
960 if (!is_valid_reg_rule(intersected_rule))
961 return -EINVAL;
962
963 return 0;
964}
965
966/**
967 * regdom_intersect - do the intersection between two regulatory domains
968 * @rd1: first regulatory domain
969 * @rd2: second regulatory domain
970 *
971 * Use this function to get the intersection between two regulatory domains.
972 * Once completed we will mark the alpha2 for the rd as intersected, "98",
973 * as no one single alpha2 can represent this regulatory domain.
974 *
975 * Returns a pointer to the regulatory domain structure which will hold the
976 * resulting intersection of rules between rd1 and rd2. We will
977 * kzalloc() this structure for you.
978 */
979static struct ieee80211_regdomain *regdom_intersect(
980 const struct ieee80211_regdomain *rd1,
981 const struct ieee80211_regdomain *rd2)
982{
983 int r, size_of_regd;
984 unsigned int x, y;
985 unsigned int num_rules = 0, rule_idx = 0;
986 const struct ieee80211_reg_rule *rule1, *rule2;
987 struct ieee80211_reg_rule *intersected_rule;
988 struct ieee80211_regdomain *rd;
989 /* This is just a dummy holder to help us count */
990 struct ieee80211_reg_rule irule;
991
992 /* Uses the stack temporarily for counter arithmetic */
993 intersected_rule = &irule;
994
995 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
996
997 if (!rd1 || !rd2)
998 return NULL;
999
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001000 /*
1001 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001002 * build them. This is to so we can malloc() and free() a
1003 * regdomain once. The reason we use reg_rules_intersect() here
1004 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001005 * All rules that do check out OK are valid.
1006 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001007
1008 for (x = 0; x < rd1->n_reg_rules; x++) {
1009 rule1 = &rd1->reg_rules[x];
1010 for (y = 0; y < rd2->n_reg_rules; y++) {
1011 rule2 = &rd2->reg_rules[y];
1012 if (!reg_rules_intersect(rule1, rule2,
1013 intersected_rule))
1014 num_rules++;
1015 memset(intersected_rule, 0,
1016 sizeof(struct ieee80211_reg_rule));
1017 }
1018 }
1019
1020 if (!num_rules)
1021 return NULL;
1022
1023 size_of_regd = sizeof(struct ieee80211_regdomain) +
1024 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
1025
1026 rd = kzalloc(size_of_regd, GFP_KERNEL);
1027 if (!rd)
1028 return NULL;
1029
1030 for (x = 0; x < rd1->n_reg_rules; x++) {
1031 rule1 = &rd1->reg_rules[x];
1032 for (y = 0; y < rd2->n_reg_rules; y++) {
1033 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001034 /*
1035 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001036 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001037 * a memcpy()
1038 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001039 intersected_rule = &rd->reg_rules[rule_idx];
1040 r = reg_rules_intersect(rule1, rule2,
1041 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001042 /*
1043 * No need to memset here the intersected rule here as
1044 * we're not using the stack anymore
1045 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001046 if (r)
1047 continue;
1048 rule_idx++;
1049 }
1050 }
1051
1052 if (rule_idx != num_rules) {
1053 kfree(rd);
1054 return NULL;
1055 }
1056
1057 rd->n_reg_rules = num_rules;
1058 rd->alpha2[0] = '9';
1059 rd->alpha2[1] = '8';
1060
1061 return rd;
1062}
1063
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001064/*
1065 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
1066 * want to just have the channel structure use these
1067 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001068static u32 map_regdom_flags(u32 rd_flags)
1069{
1070 u32 channel_flags = 0;
1071 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
1072 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1073 if (rd_flags & NL80211_RRF_NO_IBSS)
1074 channel_flags |= IEEE80211_CHAN_NO_IBSS;
1075 if (rd_flags & NL80211_RRF_DFS)
1076 channel_flags |= IEEE80211_CHAN_RADAR;
1077 return channel_flags;
1078}
1079
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001080static int freq_reg_info_regd(struct wiphy *wiphy,
1081 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001082 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001083 const struct ieee80211_reg_rule **reg_rule,
1084 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +01001085{
1086 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001087 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001088 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001089 bool bw_fits = false;
1090
1091 if (!desired_bw_khz)
1092 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +01001093
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001094 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001095
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001096 /*
1097 * Follow the driver's regulatory domain, if present, unless a country
1098 * IE has been processed or a user wants to help complaince further
1099 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001100 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
1101 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001102 wiphy->regd)
1103 regd = wiphy->regd;
1104
1105 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001106 return -EINVAL;
1107
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001108 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001109 const struct ieee80211_reg_rule *rr;
1110 const struct ieee80211_freq_range *fr = NULL;
1111 const struct ieee80211_power_rule *pr = NULL;
1112
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001113 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001114 fr = &rr->freq_range;
1115 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001116
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001117 /*
1118 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001119 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001120 * not overwrite it once found
1121 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001122 if (!band_rule_found)
1123 band_rule_found = freq_in_rule_band(fr, center_freq);
1124
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001125 bw_fits = reg_does_bw_fit(fr,
1126 center_freq,
1127 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001128
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001129 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001130 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001131 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001132 }
1133 }
1134
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001135 if (!band_rule_found)
1136 return -ERANGE;
1137
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001138 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001139}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -08001140EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001141
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001142int freq_reg_info(struct wiphy *wiphy,
1143 u32 center_freq,
1144 u32 desired_bw_khz,
1145 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001146{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001147 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001148 return freq_reg_info_regd(wiphy,
1149 center_freq,
1150 desired_bw_khz,
1151 reg_rule,
1152 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001153}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001154
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001155/*
1156 * Note that right now we assume the desired channel bandwidth
1157 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
1158 * per channel, the primary and the extension channel). To support
1159 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
1160 * new ieee80211_channel.target_bw and re run the regulatory check
1161 * on the wiphy with the target_bw specified. Then we can simply use
1162 * that below for the desired_bw_khz below.
1163 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001164static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
1165 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001166{
1167 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001168 u32 flags, bw_flags = 0;
1169 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001170 const struct ieee80211_reg_rule *reg_rule = NULL;
1171 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001172 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001173 struct ieee80211_supported_band *sband;
1174 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001175 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001176
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001177 assert_cfg80211_lock();
1178
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001179 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1180
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001181 sband = wiphy->bands[band];
1182 BUG_ON(chan_idx >= sband->n_channels);
1183 chan = &sband->channels[chan_idx];
1184
1185 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001186
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001187 r = freq_reg_info(wiphy,
1188 MHZ_TO_KHZ(chan->center_freq),
1189 desired_bw_khz,
1190 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001191
1192 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001193 /*
1194 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001195 * with a frequency range on the center_freq's band, since
1196 * IEEE-802.11 allows for a country IE to have a subset of the
1197 * regulatory information provided in a country we ignore
1198 * disabling the channel unless at least one reg rule was
1199 * found on the center_freq's band. For details see this
1200 * clarification:
1201 *
1202 * http://tinyurl.com/11d-clarification
1203 */
1204 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001205 last_request->initiator ==
1206 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001207 REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001208 "intact on %s - no rule found in band on "
1209 "Country IE\n",
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001210 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001211 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001212 /*
1213 * In this case we know the country IE has at least one reg rule
1214 * for the band so we respect its band definitions
1215 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001216 if (last_request->initiator ==
1217 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05001218 REG_DBG_PRINT("cfg80211: Disabling "
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001219 "channel %d MHz on %s due to "
1220 "Country IE\n",
1221 chan->center_freq, wiphy_name(wiphy));
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -08001222 flags |= IEEE80211_CHAN_DISABLED;
1223 chan->flags = flags;
1224 }
Johannes Berg8318d782008-01-24 19:38:38 +01001225 return;
1226 }
1227
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001228 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001229 freq_range = &reg_rule->freq_range;
1230
1231 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1232 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001233
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001234 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001235 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001236 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001237 /*
1238 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001239 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001240 * settings
1241 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001242 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001243 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001244 chan->max_antenna_gain = chan->orig_mag =
1245 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001246 chan->max_power = chan->orig_mpwr =
1247 (int) MBM_TO_DBM(power_rule->max_eirp);
1248 return;
1249 }
1250
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001251 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001252 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001253 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -04001254 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001255 chan->max_power = min(chan->orig_mpwr,
1256 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001257 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001258 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001259}
1260
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001261static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001262{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001263 unsigned int i;
1264 struct ieee80211_supported_band *sband;
1265
1266 BUG_ON(!wiphy->bands[band]);
1267 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001268
1269 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001270 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001271}
1272
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001273static bool ignore_reg_update(struct wiphy *wiphy,
1274 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001275{
1276 if (!last_request)
1277 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001278 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001279 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001280 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001281 /*
1282 * wiphy->regd will be set once the device has its own
1283 * desired regulatory domain set
1284 */
Johannes Berg5be83de2009-11-19 00:56:28 +01001285 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001286 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001287 return true;
1288 return false;
1289}
1290
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001291static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001292{
Johannes Berg79c97e92009-07-07 03:56:12 +02001293 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001294
Johannes Berg79c97e92009-07-07 03:56:12 +02001295 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1296 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001297}
1298
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001299static void handle_reg_beacon(struct wiphy *wiphy,
1300 unsigned int chan_idx,
1301 struct reg_beacon *reg_beacon)
1302{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001303 struct ieee80211_supported_band *sband;
1304 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001305 bool channel_changed = false;
1306 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001307
1308 assert_cfg80211_lock();
1309
1310 sband = wiphy->bands[reg_beacon->chan.band];
1311 chan = &sband->channels[chan_idx];
1312
1313 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1314 return;
1315
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001316 if (chan->beacon_found)
1317 return;
1318
1319 chan->beacon_found = true;
1320
Johannes Berg5be83de2009-11-19 00:56:28 +01001321 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001322 return;
1323
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001324 chan_before.center_freq = chan->center_freq;
1325 chan_before.flags = chan->flags;
1326
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001327 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001328 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001329 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001330 }
1331
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001332 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001333 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001334 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001335 }
1336
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001337 if (channel_changed)
1338 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001339}
1340
1341/*
1342 * Called when a scan on a wiphy finds a beacon on
1343 * new channel
1344 */
1345static void wiphy_update_new_beacon(struct wiphy *wiphy,
1346 struct reg_beacon *reg_beacon)
1347{
1348 unsigned int i;
1349 struct ieee80211_supported_band *sband;
1350
1351 assert_cfg80211_lock();
1352
1353 if (!wiphy->bands[reg_beacon->chan.band])
1354 return;
1355
1356 sband = wiphy->bands[reg_beacon->chan.band];
1357
1358 for (i = 0; i < sband->n_channels; i++)
1359 handle_reg_beacon(wiphy, i, reg_beacon);
1360}
1361
1362/*
1363 * Called upon reg changes or a new wiphy is added
1364 */
1365static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1366{
1367 unsigned int i;
1368 struct ieee80211_supported_band *sband;
1369 struct reg_beacon *reg_beacon;
1370
1371 assert_cfg80211_lock();
1372
1373 if (list_empty(&reg_beacon_list))
1374 return;
1375
1376 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1377 if (!wiphy->bands[reg_beacon->chan.band])
1378 continue;
1379 sband = wiphy->bands[reg_beacon->chan.band];
1380 for (i = 0; i < sband->n_channels; i++)
1381 handle_reg_beacon(wiphy, i, reg_beacon);
1382 }
1383}
1384
1385static bool reg_is_world_roaming(struct wiphy *wiphy)
1386{
1387 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1388 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1389 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001390 if (last_request &&
1391 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001392 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001393 return true;
1394 return false;
1395}
1396
1397/* Reap the advantages of previously found beacons */
1398static void reg_process_beacons(struct wiphy *wiphy)
1399{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001400 /*
1401 * Means we are just firing up cfg80211, so no beacons would
1402 * have been processed yet.
1403 */
1404 if (!last_request)
1405 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001406 if (!reg_is_world_roaming(wiphy))
1407 return;
1408 wiphy_update_beacon_reg(wiphy);
1409}
1410
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001411static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1412{
1413 if (!chan)
1414 return true;
1415 if (chan->flags & IEEE80211_CHAN_DISABLED)
1416 return true;
1417 /* This would happen when regulatory rules disallow HT40 completely */
1418 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1419 return true;
1420 return false;
1421}
1422
1423static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1424 enum ieee80211_band band,
1425 unsigned int chan_idx)
1426{
1427 struct ieee80211_supported_band *sband;
1428 struct ieee80211_channel *channel;
1429 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1430 unsigned int i;
1431
1432 assert_cfg80211_lock();
1433
1434 sband = wiphy->bands[band];
1435 BUG_ON(chan_idx >= sband->n_channels);
1436 channel = &sband->channels[chan_idx];
1437
1438 if (is_ht40_not_allowed(channel)) {
1439 channel->flags |= IEEE80211_CHAN_NO_HT40;
1440 return;
1441 }
1442
1443 /*
1444 * We need to ensure the extension channels exist to
1445 * be able to use HT40- or HT40+, this finds them (or not)
1446 */
1447 for (i = 0; i < sband->n_channels; i++) {
1448 struct ieee80211_channel *c = &sband->channels[i];
1449 if (c->center_freq == (channel->center_freq - 20))
1450 channel_before = c;
1451 if (c->center_freq == (channel->center_freq + 20))
1452 channel_after = c;
1453 }
1454
1455 /*
1456 * Please note that this assumes target bandwidth is 20 MHz,
1457 * if that ever changes we also need to change the below logic
1458 * to include that as well.
1459 */
1460 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001461 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001462 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001463 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001464
1465 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001466 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001467 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001468 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001469}
1470
1471static void reg_process_ht_flags_band(struct wiphy *wiphy,
1472 enum ieee80211_band band)
1473{
1474 unsigned int i;
1475 struct ieee80211_supported_band *sband;
1476
1477 BUG_ON(!wiphy->bands[band]);
1478 sband = wiphy->bands[band];
1479
1480 for (i = 0; i < sband->n_channels; i++)
1481 reg_process_ht_flags_channel(wiphy, band, i);
1482}
1483
1484static void reg_process_ht_flags(struct wiphy *wiphy)
1485{
1486 enum ieee80211_band band;
1487
1488 if (!wiphy)
1489 return;
1490
1491 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1492 if (wiphy->bands[band])
1493 reg_process_ht_flags_band(wiphy, band);
1494 }
1495
1496}
1497
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001498void wiphy_update_regulatory(struct wiphy *wiphy,
1499 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001500{
1501 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001502
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001503 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001504 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001505 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001506 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001507 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001508 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001509out:
1510 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001511 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001512 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001513 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001514}
1515
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001516static void handle_channel_custom(struct wiphy *wiphy,
1517 enum ieee80211_band band,
1518 unsigned int chan_idx,
1519 const struct ieee80211_regdomain *regd)
1520{
1521 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001522 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1523 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001524 const struct ieee80211_reg_rule *reg_rule = NULL;
1525 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001526 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001527 struct ieee80211_supported_band *sband;
1528 struct ieee80211_channel *chan;
1529
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001530 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001531
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001532 sband = wiphy->bands[band];
1533 BUG_ON(chan_idx >= sband->n_channels);
1534 chan = &sband->channels[chan_idx];
1535
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001536 r = freq_reg_info_regd(wiphy,
1537 MHZ_TO_KHZ(chan->center_freq),
1538 desired_bw_khz,
1539 &reg_rule,
1540 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001541
1542 if (r) {
1543 chan->flags = IEEE80211_CHAN_DISABLED;
1544 return;
1545 }
1546
1547 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001548 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001549
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001550 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1551 bw_flags = IEEE80211_CHAN_NO_HT40;
1552
1553 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001554 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001555 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1556}
1557
1558static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1559 const struct ieee80211_regdomain *regd)
1560{
1561 unsigned int i;
1562 struct ieee80211_supported_band *sband;
1563
1564 BUG_ON(!wiphy->bands[band]);
1565 sband = wiphy->bands[band];
1566
1567 for (i = 0; i < sband->n_channels; i++)
1568 handle_channel_custom(wiphy, band, i, regd);
1569}
1570
1571/* Used by drivers prior to wiphy registration */
1572void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1573 const struct ieee80211_regdomain *regd)
1574{
1575 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001576 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001577
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001578 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001579 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001580 if (!wiphy->bands[band])
1581 continue;
1582 handle_band_custom(wiphy, band, regd);
1583 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001584 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001585 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001586
1587 /*
1588 * no point in calling this if it won't have any effect
1589 * on your device's supportd bands.
1590 */
1591 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001592}
1593EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1594
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001595/*
1596 * Return value which can be used by ignore_request() to indicate
1597 * it has been determined we should intersect two regulatory domains
1598 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001599#define REG_INTERSECT 1
1600
Johannes Berg84fa4f42008-10-24 20:32:23 +02001601/* This has the logic which determines when a new request
1602 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001603static int ignore_request(struct wiphy *wiphy,
1604 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001605{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001606 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001607
1608 assert_cfg80211_lock();
1609
Johannes Berg84fa4f42008-10-24 20:32:23 +02001610 /* All initial requests are respected */
1611 if (!last_request)
1612 return 0;
1613
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001614 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001615 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001616 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001617 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001618
1619 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1620
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001621 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001622 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001623 if (last_request->initiator ==
1624 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001625 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001626 /*
1627 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001628 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001629 * intersect them, but that seems unlikely
1630 * to be correct. Reject second one for now.
1631 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001632 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001633 return -EOPNOTSUPP;
1634 return -EALREADY;
1635 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001636 /*
1637 * Two consecutive Country IE hints on the same wiphy.
1638 * This should be picked up early by the driver/stack
1639 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001640 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001641 return 0;
1642 return -EALREADY;
1643 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001644 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001645 case NL80211_REGDOM_SET_BY_DRIVER:
1646 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001647 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001648 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001649 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001650 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001651
1652 /*
1653 * This would happen if you unplug and plug your card
1654 * back in or if you add a new device for which the previously
1655 * loaded card also agrees on the regulatory domain.
1656 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001657 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001658 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001659 return -EALREADY;
1660
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001661 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001662 case NL80211_REGDOM_SET_BY_USER:
1663 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001664 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001665 /*
1666 * If the user knows better the user should set the regdom
1667 * to their country before the IE is picked up
1668 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001669 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001670 last_request->intersect)
1671 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001672 /*
1673 * Process user requests only after previous user/driver/core
1674 * requests have been processed
1675 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001676 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1677 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1678 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001679 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001680 return -EAGAIN;
1681 }
1682
John W. Linvillebaeb66f2009-12-18 17:59:02 -05001683 if (!regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001684 return -EALREADY;
1685
Johannes Berg84fa4f42008-10-24 20:32:23 +02001686 return 0;
1687 }
1688
1689 return -EINVAL;
1690}
1691
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001692/**
1693 * __regulatory_hint - hint to the wireless core a regulatory domain
1694 * @wiphy: if the hint comes from country information from an AP, this
1695 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001696 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001697 *
1698 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001699 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001700 *
1701 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1702 * already been set or other standard error codes.
1703 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001704 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001705 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001706static int __regulatory_hint(struct wiphy *wiphy,
1707 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001708{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001709 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001710 int r = 0;
1711
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001712 assert_cfg80211_lock();
1713
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001714 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001715
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001716 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001717 if (pending_request->initiator ==
1718 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001719 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001720 if (r) {
1721 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001722 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001723 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001724 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001725 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001726 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001727 /*
1728 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001729 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001730 * wiphy
1731 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001732 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001733 pending_request->initiator ==
1734 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001735 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001736 if (r) {
1737 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001738 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001739 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001740 r = -EALREADY;
1741 goto new_request;
1742 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001743 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001744 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001745 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001746
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001747new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001748 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001749
1750 last_request = pending_request;
1751 last_request->intersect = intersect;
1752
1753 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001754
1755 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001756 if (r < 0) {
1757 /*
1758 * Since CRDA will not be called in this case as we already
1759 * have applied the requested regulatory domain before we just
1760 * inform userspace we have processed the request
1761 */
1762 if (r == -EALREADY)
1763 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001764 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001765 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001766
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001767 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001768}
1769
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001770/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001771static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001772{
1773 int r = 0;
1774 struct wiphy *wiphy = NULL;
1775
1776 BUG_ON(!reg_request->alpha2);
1777
1778 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001779 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001780
1781 if (wiphy_idx_valid(reg_request->wiphy_idx))
1782 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1783
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001784 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001785 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001786 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001787 goto out;
1788 }
1789
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001790 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001791 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001792 if (r == -EALREADY && wiphy &&
1793 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001794 wiphy_update_regulatory(wiphy, reg_request->initiator);
1795out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001796 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001797 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001798}
1799
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001800/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001801static void reg_process_pending_hints(void)
1802 {
1803 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001804
1805 spin_lock(&reg_requests_lock);
1806 while (!list_empty(&reg_requests_list)) {
1807 reg_request = list_first_entry(&reg_requests_list,
1808 struct regulatory_request,
1809 list);
1810 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001811
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001812 spin_unlock(&reg_requests_lock);
1813 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001814 spin_lock(&reg_requests_lock);
1815 }
1816 spin_unlock(&reg_requests_lock);
1817}
1818
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001819/* Processes beacon hints -- this has nothing to do with country IEs */
1820static void reg_process_pending_beacon_hints(void)
1821{
Johannes Berg79c97e92009-07-07 03:56:12 +02001822 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001823 struct reg_beacon *pending_beacon, *tmp;
1824
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001825 /*
1826 * No need to hold the reg_mutex here as we just touch wiphys
1827 * and do not read or access regulatory variables.
1828 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001829 mutex_lock(&cfg80211_mutex);
1830
1831 /* This goes through the _pending_ beacon list */
1832 spin_lock_bh(&reg_pending_beacons_lock);
1833
1834 if (list_empty(&reg_pending_beacons)) {
1835 spin_unlock_bh(&reg_pending_beacons_lock);
1836 goto out;
1837 }
1838
1839 list_for_each_entry_safe(pending_beacon, tmp,
1840 &reg_pending_beacons, list) {
1841
1842 list_del_init(&pending_beacon->list);
1843
1844 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001845 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1846 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001847
1848 /* Remembers the beacon hint for new wiphys or reg changes */
1849 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1850 }
1851
1852 spin_unlock_bh(&reg_pending_beacons_lock);
1853out:
1854 mutex_unlock(&cfg80211_mutex);
1855}
1856
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001857static void reg_todo(struct work_struct *work)
1858{
1859 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001860 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001861}
1862
1863static DECLARE_WORK(reg_work, reg_todo);
1864
1865static void queue_regulatory_request(struct regulatory_request *request)
1866{
1867 spin_lock(&reg_requests_lock);
1868 list_add_tail(&request->list, &reg_requests_list);
1869 spin_unlock(&reg_requests_lock);
1870
1871 schedule_work(&reg_work);
1872}
1873
1874/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001875static int regulatory_hint_core(const char *alpha2)
1876{
1877 struct regulatory_request *request;
1878
1879 BUG_ON(last_request);
1880
1881 request = kzalloc(sizeof(struct regulatory_request),
1882 GFP_KERNEL);
1883 if (!request)
1884 return -ENOMEM;
1885
1886 request->alpha2[0] = alpha2[0];
1887 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001888 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001889
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001890 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001891
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001892 /*
1893 * This ensures last_request is populated once modules
1894 * come swinging in and calling regulatory hints and
1895 * wiphy_apply_custom_regulatory().
1896 */
1897 flush_scheduled_work();
1898
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001899 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001900}
1901
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001902/* User hints */
1903int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001904{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001905 struct regulatory_request *request;
1906
Johannes Bergbe3d4812008-10-24 20:32:21 +02001907 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001908
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001909 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1910 if (!request)
1911 return -ENOMEM;
1912
1913 request->wiphy_idx = WIPHY_IDX_STALE;
1914 request->alpha2[0] = alpha2[0];
1915 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001916 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001917
1918 queue_regulatory_request(request);
1919
1920 return 0;
1921}
1922
1923/* Driver hints */
1924int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1925{
1926 struct regulatory_request *request;
1927
1928 BUG_ON(!alpha2);
1929 BUG_ON(!wiphy);
1930
1931 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1932 if (!request)
1933 return -ENOMEM;
1934
1935 request->wiphy_idx = get_wiphy_idx(wiphy);
1936
1937 /* Must have registered wiphy first */
1938 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1939
1940 request->alpha2[0] = alpha2[0];
1941 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001942 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001943
1944 queue_regulatory_request(request);
1945
1946 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001947}
1948EXPORT_SYMBOL(regulatory_hint);
1949
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001950/* Caller must hold reg_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001951static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1952 u32 country_ie_checksum)
1953{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001954 struct wiphy *request_wiphy;
1955
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001956 assert_reg_lock();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001957
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001958 if (unlikely(last_request->initiator !=
1959 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1960 return false;
1961
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001962 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1963
1964 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001965 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001966
1967 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001968 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001969 /*
1970 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001971 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001972 * on the device
1973 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001974 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1975 return true;
1976 return false;
1977}
1978
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001979/*
1980 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1981 * therefore cannot iterate over the rdev list here.
1982 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001983void regulatory_hint_11d(struct wiphy *wiphy,
1984 u8 *country_ie,
1985 u8 country_ie_len)
1986{
1987 struct ieee80211_regdomain *rd = NULL;
1988 char alpha2[2];
1989 u32 checksum = 0;
1990 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001991 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001992
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001993 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001994
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001995 if (unlikely(!last_request))
1996 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001997
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001998 /* IE len must be evenly divisible by 2 */
1999 if (country_ie_len & 0x01)
2000 goto out;
2001
2002 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
2003 goto out;
2004
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002005 /*
2006 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002007 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002008 * we were able to process the last regulatory_hint_11d() call
2009 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002010 if (country_ie_regdomain)
2011 goto out;
2012
2013 alpha2[0] = country_ie[0];
2014 alpha2[1] = country_ie[1];
2015
2016 if (country_ie[2] == 'I')
2017 env = ENVIRON_INDOOR;
2018 else if (country_ie[2] == 'O')
2019 env = ENVIRON_OUTDOOR;
2020
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002021 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002022 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002023 * We leave conflict resolution to the workqueue, where can hold
2024 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002025 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04002026 if (likely(last_request->initiator ==
2027 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07002028 wiphy_idx_valid(last_request->wiphy_idx)))
2029 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002030
2031 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002032 if (!rd) {
2033 REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002034 goto out;
Luis R. Rodriguezb74d12e2010-01-07 17:24:54 -05002035 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002036
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002037 /*
2038 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002039 * the future when we want to add suspend/resume support and having
2040 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05002041 * move to another AP. Right now we just trust the first AP.
2042 *
2043 * If we hit this before we add this support we want to be informed of
2044 * it as it would indicate a mistake in the current design
2045 */
2046 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002047 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002048
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002049 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
2050 if (!request)
2051 goto free_rd_out;
2052
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002053 /*
2054 * We keep this around for when CRDA comes back with a response so
2055 * we can intersect with that
2056 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002057 country_ie_regdomain = rd;
2058
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002059 request->wiphy_idx = get_wiphy_idx(wiphy);
2060 request->alpha2[0] = rd->alpha2[0];
2061 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002062 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002063 request->country_ie_checksum = checksum;
2064 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002065
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002066 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002067
2068 queue_regulatory_request(request);
2069
2070 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05002071
2072free_rd_out:
2073 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002074out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002075 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002076}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002077
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002078static bool freq_is_chan_12_13_14(u16 freq)
2079{
2080 if (freq == ieee80211_channel_to_frequency(12) ||
2081 freq == ieee80211_channel_to_frequency(13) ||
2082 freq == ieee80211_channel_to_frequency(14))
2083 return true;
2084 return false;
2085}
2086
2087int regulatory_hint_found_beacon(struct wiphy *wiphy,
2088 struct ieee80211_channel *beacon_chan,
2089 gfp_t gfp)
2090{
2091 struct reg_beacon *reg_beacon;
2092
2093 if (likely((beacon_chan->beacon_found ||
2094 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
2095 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
2096 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
2097 return 0;
2098
2099 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
2100 if (!reg_beacon)
2101 return -ENOMEM;
2102
Luis R. Rodriguez4113f752010-01-04 11:50:11 -05002103 REG_DBG_PRINT("cfg80211: Found new beacon on "
2104 "frequency: %d MHz (Ch %d) on %s\n",
2105 beacon_chan->center_freq,
2106 ieee80211_frequency_to_channel(beacon_chan->center_freq),
2107 wiphy_name(wiphy));
2108
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002109 memcpy(&reg_beacon->chan, beacon_chan,
2110 sizeof(struct ieee80211_channel));
2111
2112
2113 /*
2114 * Since we can be called from BH or and non-BH context
2115 * we must use spin_lock_bh()
2116 */
2117 spin_lock_bh(&reg_pending_beacons_lock);
2118 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
2119 spin_unlock_bh(&reg_pending_beacons_lock);
2120
2121 schedule_work(&reg_work);
2122
2123 return 0;
2124}
2125
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002126static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002127{
2128 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002129 const struct ieee80211_reg_rule *reg_rule = NULL;
2130 const struct ieee80211_freq_range *freq_range = NULL;
2131 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002132
Kalle Valo269ac5f2009-12-01 10:47:15 +02002133 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002134 "(max_antenna_gain, max_eirp)\n");
2135
2136 for (i = 0; i < rd->n_reg_rules; i++) {
2137 reg_rule = &rd->reg_rules[i];
2138 freq_range = &reg_rule->freq_range;
2139 power_rule = &reg_rule->power_rule;
2140
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002141 /*
2142 * There may not be documentation for max antenna gain
2143 * in certain regions
2144 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002145 if (power_rule->max_antenna_gain)
Kalle Valo269ac5f2009-12-01 10:47:15 +02002146 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002147 "(%d mBi, %d mBm)\n",
2148 freq_range->start_freq_khz,
2149 freq_range->end_freq_khz,
2150 freq_range->max_bandwidth_khz,
2151 power_rule->max_antenna_gain,
2152 power_rule->max_eirp);
2153 else
Kalle Valo269ac5f2009-12-01 10:47:15 +02002154 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002155 "(N/A, %d mBm)\n",
2156 freq_range->start_freq_khz,
2157 freq_range->end_freq_khz,
2158 freq_range->max_bandwidth_khz,
2159 power_rule->max_eirp);
2160 }
2161}
2162
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002163static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002164{
2165
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002166 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002167
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002168 if (last_request->initiator ==
2169 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02002170 struct cfg80211_registered_device *rdev;
2171 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002172 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02002173 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002174 printk(KERN_INFO "cfg80211: Current regulatory "
2175 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02002176 rdev->country_ie_alpha2[0],
2177 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002178 } else
2179 printk(KERN_INFO "cfg80211: Current regulatory "
2180 "domain intersected: \n");
2181 } else
2182 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08002183 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002184 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002185 printk(KERN_INFO "cfg80211: World regulatory "
2186 "domain updated:\n");
2187 else {
2188 if (is_unknown_alpha2(rd->alpha2))
2189 printk(KERN_INFO "cfg80211: Regulatory domain "
2190 "changed to driver built-in settings "
2191 "(unknown country)\n");
2192 else
2193 printk(KERN_INFO "cfg80211: Regulatory domain "
2194 "changed to country: %c%c\n",
2195 rd->alpha2[0], rd->alpha2[1]);
2196 }
2197 print_rd_rules(rd);
2198}
2199
Johannes Berg2df78162008-10-28 16:49:41 +01002200static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002201{
2202 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2203 rd->alpha2[0], rd->alpha2[1]);
2204 print_rd_rules(rd);
2205}
2206
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002207#ifdef CONFIG_CFG80211_REG_DEBUG
2208static void reg_country_ie_process_debug(
2209 const struct ieee80211_regdomain *rd,
2210 const struct ieee80211_regdomain *country_ie_regdomain,
2211 const struct ieee80211_regdomain *intersected_rd)
2212{
2213 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2214 print_regdomain_info(country_ie_regdomain);
2215 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2216 print_regdomain_info(rd);
2217 if (intersected_rd) {
2218 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2219 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08002220 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002221 return;
2222 }
2223 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2224}
2225#else
2226static inline void reg_country_ie_process_debug(
2227 const struct ieee80211_regdomain *rd,
2228 const struct ieee80211_regdomain *country_ie_regdomain,
2229 const struct ieee80211_regdomain *intersected_rd)
2230{
2231}
2232#endif
2233
Johannes Bergd2372b32008-10-24 20:32:20 +02002234/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002235static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002236{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002237 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002238 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002239 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002240 /* Some basic sanity checks first */
2241
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002242 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002243 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002244 return -EINVAL;
2245 update_world_regdomain(rd);
2246 return 0;
2247 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002248
2249 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2250 !is_unknown_alpha2(rd->alpha2))
2251 return -EINVAL;
2252
Johannes Bergf6037d02008-10-21 11:01:33 +02002253 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002254 return -EINVAL;
2255
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002256 /*
2257 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002258 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002259 * and the pending request came in from a country IE
2260 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002261 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002262 /*
2263 * If someone else asked us to change the rd lets only bother
2264 * checking if the alpha2 changes if CRDA was already called
2265 */
John W. Linvillebaeb66f2009-12-18 17:59:02 -05002266 if (!regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002267 return -EINVAL;
2268 }
2269
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002270 /*
2271 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002272 * and finally inform them of what we have done, in case they want
2273 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002274 * internal EEPROM data
2275 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002276
Johannes Bergf6037d02008-10-21 11:01:33 +02002277 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002278 return -EINVAL;
2279
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002280 if (!is_valid_rd(rd)) {
2281 printk(KERN_ERR "cfg80211: Invalid "
2282 "regulatory domain detected:\n");
2283 print_regdomain_info(rd);
2284 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002285 }
2286
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002287 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2288
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002289 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002290 int r;
2291
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002292 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002293 reset_regdomains();
2294 cfg80211_regdomain = rd;
2295 return 0;
2296 }
2297
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002298 /*
2299 * For a driver hint, lets copy the regulatory domain the
2300 * driver wanted to the wiphy to deal with conflicts
2301 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002302
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002303 /*
2304 * Userspace could have sent two replies with only
2305 * one kernel request.
2306 */
2307 if (request_wiphy->regd)
2308 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002309
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002310 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002311 if (r)
2312 return r;
2313
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002314 reset_regdomains();
2315 cfg80211_regdomain = rd;
2316 return 0;
2317 }
2318
2319 /* Intersection requires a bit more work */
2320
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002321 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002322
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002323 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2324 if (!intersected_rd)
2325 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002326
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002327 /*
2328 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002329 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002330 * domain we keep it for its private use
2331 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002332 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002333 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002334 else
2335 kfree(rd);
2336
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002337 rd = NULL;
2338
2339 reset_regdomains();
2340 cfg80211_regdomain = intersected_rd;
2341
2342 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002343 }
2344
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002345 /*
2346 * Country IE requests are handled a bit differently, we intersect
2347 * the country IE rd with what CRDA believes that country should have
2348 */
2349
Luis R. Rodriguez729e9c72009-05-31 18:24:34 -04002350 /*
2351 * Userspace could have sent two replies with only
2352 * one kernel request. By the second reply we would have
2353 * already processed and consumed the country_ie_regdomain.
2354 */
2355 if (!country_ie_regdomain)
2356 return -EALREADY;
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002357 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002358
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002359 /*
2360 * Intersect what CRDA returned and our what we
2361 * had built from the Country IE received
2362 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002363
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002364 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002365
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002366 reg_country_ie_process_debug(rd,
2367 country_ie_regdomain,
2368 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002369
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002370 kfree(country_ie_regdomain);
2371 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002372
2373 if (!intersected_rd)
2374 return -EINVAL;
2375
Johannes Berg79c97e92009-07-07 03:56:12 +02002376 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002377
Johannes Berg79c97e92009-07-07 03:56:12 +02002378 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2379 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2380 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002381
2382 BUG_ON(intersected_rd == rd);
2383
2384 kfree(rd);
2385 rd = NULL;
2386
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002387 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002388 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002389
2390 return 0;
2391}
2392
2393
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002394/*
2395 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002396 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002397 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2398 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002399int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002400{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002401 int r;
2402
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002403 assert_cfg80211_lock();
2404
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002405 mutex_lock(&reg_mutex);
2406
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002407 /* Note that this doesn't update the wiphys, this is done below */
2408 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002409 if (r) {
2410 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002411 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002412 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002413 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002414
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002415 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002416 if (!last_request->intersect)
2417 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002418
2419 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002420 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002421
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002422 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002423
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002424 nl80211_send_reg_change_event(last_request);
2425
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002426 mutex_unlock(&reg_mutex);
2427
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002428 return r;
2429}
2430
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002431/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002432void reg_device_remove(struct wiphy *wiphy)
2433{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002434 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002435
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002436 assert_cfg80211_lock();
2437
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002438 mutex_lock(&reg_mutex);
2439
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002440 kfree(wiphy->regd);
2441
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002442 if (last_request)
2443 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002444
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002445 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002446 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002447
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002448 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002449 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002450out:
2451 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002452}
2453
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002454int regulatory_init(void)
2455{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002456 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002457
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002458 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2459 if (IS_ERR(reg_pdev))
2460 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002461
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002462 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002463 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002464
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002465 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002466
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002467 /* We always try to get an update for the static regdomain */
2468 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002469 if (err) {
2470 if (err == -ENOMEM)
2471 return err;
2472 /*
2473 * N.B. kobject_uevent_env() can fail mainly for when we're out
2474 * memory which is handled and propagated appropriately above
2475 * but it can also fail during a netlink_broadcast() or during
2476 * early boot for call_usermodehelper(). For now treat these
2477 * errors as non-fatal.
2478 */
2479 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2480 "to call CRDA during init");
2481#ifdef CONFIG_CFG80211_REG_DEBUG
2482 /* We want to find out exactly why when debugging */
2483 WARN_ON(err);
2484#endif
2485 }
Johannes Berg734366d2008-09-15 10:56:48 +02002486
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002487 /*
2488 * Finally, if the user set the module parameter treat it
2489 * as a user hint.
2490 */
2491 if (!is_world_regdom(ieee80211_regdom))
2492 regulatory_hint_user(ieee80211_regdom);
2493
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002494 return 0;
2495}
2496
2497void regulatory_exit(void)
2498{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002499 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002500 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002501
2502 cancel_work_sync(&reg_work);
2503
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002504 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002505 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002506
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002507 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002508
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002509 kfree(country_ie_regdomain);
2510 country_ie_regdomain = NULL;
2511
Johannes Bergf6037d02008-10-21 11:01:33 +02002512 kfree(last_request);
2513
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002514 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002515
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002516 spin_lock_bh(&reg_pending_beacons_lock);
2517 if (!list_empty(&reg_pending_beacons)) {
2518 list_for_each_entry_safe(reg_beacon, btmp,
2519 &reg_pending_beacons, list) {
2520 list_del(&reg_beacon->list);
2521 kfree(reg_beacon);
2522 }
2523 }
2524 spin_unlock_bh(&reg_pending_beacons_lock);
2525
2526 if (!list_empty(&reg_beacon_list)) {
2527 list_for_each_entry_safe(reg_beacon, btmp,
2528 &reg_beacon_list, list) {
2529 list_del(&reg_beacon->list);
2530 kfree(reg_beacon);
2531 }
2532 }
2533
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002534 spin_lock(&reg_requests_lock);
2535 if (!list_empty(&reg_requests_list)) {
2536 list_for_each_entry_safe(reg_request, tmp,
2537 &reg_requests_list, list) {
2538 list_del(&reg_request->list);
2539 kfree(reg_request);
2540 }
2541 }
2542 spin_unlock(&reg_requests_lock);
2543
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002544 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002545 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002546}