blob: 7a0754c92df4702ed1c72c2cdb10945dd1eb56fd [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"
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040043#include "nl80211.h"
Johannes Berg8318d782008-01-24 19:38:38 +010044
Luis R. Rodriguez5166ccd2008-10-30 13:33:56 -070045/* Receipt of information from last regulatory request */
Johannes Bergf6037d02008-10-21 11:01:33 +020046static struct regulatory_request *last_request;
Johannes Berg734366d2008-09-15 10:56:48 +020047
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070048/* To trigger userspace events */
49static struct platform_device *reg_pdev;
Johannes Berg8318d782008-01-24 19:38:38 +010050
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050051/*
52 * Central wireless core regulatory domains, we only need two,
Johannes Berg734366d2008-09-15 10:56:48 +020053 * the current one and a world regulatory domain in case we have no
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050054 * information to give us an alpha2
55 */
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080056const struct ieee80211_regdomain *cfg80211_regdomain;
Johannes Berg734366d2008-09-15 10:56:48 +020057
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050058/*
59 * We use this as a place for the rd structure built from the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080060 * last parsed country IE to rest until CRDA gets back to us with
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -050061 * what it thinks should apply for the same country
62 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -080063static const struct ieee80211_regdomain *country_ie_regdomain;
64
Luis R. Rodriguezabc73812009-07-30 17:38:08 -070065/*
66 * Protects static reg.c components:
67 * - cfg80211_world_regdom
68 * - cfg80211_regdom
69 * - country_ie_regdomain
70 * - last_request
71 */
72DEFINE_MUTEX(reg_mutex);
73#define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
74
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050075/* Used to queue up regulatory hints */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -050076static LIST_HEAD(reg_requests_list);
77static spinlock_t reg_requests_lock;
78
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -050079/* Used to queue up beacon hints for review */
80static LIST_HEAD(reg_pending_beacons);
81static spinlock_t reg_pending_beacons_lock;
82
83/* Used to keep track of processed beacon hints */
84static LIST_HEAD(reg_beacon_list);
85
86struct reg_beacon {
87 struct list_head list;
88 struct ieee80211_channel chan;
89};
90
Johannes Berg734366d2008-09-15 10:56:48 +020091/* We keep a static world regulatory domain in case of the absence of CRDA */
92static const struct ieee80211_regdomain world_regdom = {
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080093 .n_reg_rules = 5,
Johannes Berg734366d2008-09-15 10:56:48 +020094 .alpha2 = "00",
95 .reg_rules = {
Luis R. Rodriguez68798a62009-02-21 00:20:37 -050096 /* IEEE 802.11b/g, channels 1..11 */
97 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -080098 /* IEEE 802.11b/g, channels 12..13. No HT40
99 * channel fits here. */
100 REG_RULE(2467-10, 2472+10, 20, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500101 NL80211_RRF_PASSIVE_SCAN |
102 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800103 /* IEEE 802.11 channel 14 - Only JP enables
104 * this and for 802.11b only */
105 REG_RULE(2484-10, 2484+10, 20, 6, 20,
106 NL80211_RRF_PASSIVE_SCAN |
107 NL80211_RRF_NO_IBSS |
108 NL80211_RRF_NO_OFDM),
109 /* IEEE 802.11a, channel 36..48 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800110 REG_RULE(5180-10, 5240+10, 40, 6, 20,
Luis R. Rodriguez611b6a82009-03-05 21:19:21 -0800111 NL80211_RRF_PASSIVE_SCAN |
112 NL80211_RRF_NO_IBSS),
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500113
114 /* NB: 5260 MHz - 5700 MHz requies DFS */
115
116 /* IEEE 802.11a, channel 149..165 */
Luis R. Rodriguezec329ac2009-03-05 21:19:22 -0800117 REG_RULE(5745-10, 5825+10, 40, 6, 20,
Luis R. Rodriguez3fc71f72009-02-21 00:20:38 -0500118 NL80211_RRF_PASSIVE_SCAN |
119 NL80211_RRF_NO_IBSS),
Johannes Berg734366d2008-09-15 10:56:48 +0200120 }
121};
122
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200123static const struct ieee80211_regdomain *cfg80211_world_regdom =
124 &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200125
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400126static char *ieee80211_regdom = "00";
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400127
Johannes Berg734366d2008-09-15 10:56:48 +0200128module_param(ieee80211_regdom, charp, 0444);
129MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
130
Luis R. Rodriguez6ee7d332009-03-20 23:53:06 -0400131#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500132/*
133 * We assume 40 MHz bandwidth for the old regulatory work.
Johannes Berg734366d2008-09-15 10:56:48 +0200134 * We make emphasis we are using the exact same frequencies
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500135 * as before
136 */
Johannes Berg734366d2008-09-15 10:56:48 +0200137
138static const struct ieee80211_regdomain us_regdom = {
139 .n_reg_rules = 6,
140 .alpha2 = "US",
141 .reg_rules = {
142 /* IEEE 802.11b/g, channels 1..11 */
143 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
John W. Linville65182b92009-12-09 15:11:22 -0500144 /* IEEE 802.11a, channel 36..48 */
145 REG_RULE(5180-10, 5240+10, 40, 6, 17, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200146 /* IEEE 802.11a, channels 48..64 */
John W. Linville65182b92009-12-09 15:11:22 -0500147 REG_RULE(5260-10, 5320+10, 40, 6, 20, NL80211_RRF_DFS),
148 /* IEEE 802.11a, channels 100..124 */
149 REG_RULE(5500-10, 5590+10, 40, 6, 20, NL80211_RRF_DFS),
150 /* IEEE 802.11a, channels 132..144 */
151 REG_RULE(5660-10, 5700+10, 40, 6, 20, NL80211_RRF_DFS),
Johannes Berg734366d2008-09-15 10:56:48 +0200152 /* IEEE 802.11a, channels 149..165, outdoor */
153 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
154 }
155};
156
157static const struct ieee80211_regdomain jp_regdom = {
John W. Linville65182b92009-12-09 15:11:22 -0500158 .n_reg_rules = 6,
Johannes Berg734366d2008-09-15 10:56:48 +0200159 .alpha2 = "JP",
160 .reg_rules = {
John W. Linville65182b92009-12-09 15:11:22 -0500161 /* IEEE 802.11b/g, channels 1..11 */
162 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
163 /* IEEE 802.11b/g, channels 12..13 */
164 REG_RULE(2467-10, 2472+10, 20, 6, 20, 0),
165 /* IEEE 802.11b/g, channel 14 */
166 REG_RULE(2484-10, 2484+10, 20, 6, 20, NL80211_RRF_NO_OFDM),
167 /* IEEE 802.11a, channels 36..48 */
168 REG_RULE(5180-10, 5240+10, 40, 6, 20, 0),
Johannes Berg734366d2008-09-15 10:56:48 +0200169 /* IEEE 802.11a, channels 52..64 */
John W. Linville65182b92009-12-09 15:11:22 -0500170 REG_RULE(5260-10, 5320+10, 40, 6, 20, NL80211_RRF_DFS),
171 /* IEEE 802.11a, channels 100..144 */
172 REG_RULE(5500-10, 5700+10, 40, 6, 23, NL80211_RRF_DFS),
Johannes Berg734366d2008-09-15 10:56:48 +0200173 }
174};
175
176static const struct ieee80211_regdomain *static_regdom(char *alpha2)
177{
178 if (alpha2[0] == 'U' && alpha2[1] == 'S')
179 return &us_regdom;
180 if (alpha2[0] == 'J' && alpha2[1] == 'P')
181 return &jp_regdom;
John W. Linville65182b92009-12-09 15:11:22 -0500182 /* Use world roaming rules for "EU", since it was a pseudo
183 domain anyway... */
Johannes Berg734366d2008-09-15 10:56:48 +0200184 if (alpha2[0] == 'E' && alpha2[1] == 'U')
John W. Linville65182b92009-12-09 15:11:22 -0500185 return &world_regdom;
186 /* Default, world roaming rules */
187 return &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200188}
189
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200190static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200191{
John W. Linville65182b92009-12-09 15:11:22 -0500192 if (rd == &us_regdom || rd == &jp_regdom || rd == &world_regdom)
Johannes Berg734366d2008-09-15 10:56:48 +0200193 return true;
194 return false;
195}
Johannes Berg734366d2008-09-15 10:56:48 +0200196#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200197static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
198{
199 return false;
200}
201#endif
202
Johannes Berg734366d2008-09-15 10:56:48 +0200203static void reset_regdomains(void)
204{
Johannes Berg942b25c2008-09-15 11:26:47 +0200205 /* avoid freeing static information or freeing something twice */
206 if (cfg80211_regdomain == cfg80211_world_regdom)
207 cfg80211_regdomain = NULL;
208 if (cfg80211_world_regdom == &world_regdom)
209 cfg80211_world_regdom = NULL;
210 if (cfg80211_regdomain == &world_regdom)
211 cfg80211_regdomain = NULL;
212 if (is_old_static_regdom(cfg80211_regdomain))
213 cfg80211_regdomain = NULL;
214
215 kfree(cfg80211_regdomain);
216 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200217
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200218 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200219 cfg80211_regdomain = NULL;
220}
221
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500222/*
223 * Dynamic world regulatory domain requested by the wireless
224 * core upon initialization
225 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200226static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200227{
Johannes Bergf6037d02008-10-21 11:01:33 +0200228 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200229
230 reset_regdomains();
231
232 cfg80211_world_regdom = rd;
233 cfg80211_regdomain = rd;
234}
Johannes Berg734366d2008-09-15 10:56:48 +0200235
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200236bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100237{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700238 if (!alpha2)
239 return false;
240 if (alpha2[0] == '0' && alpha2[1] == '0')
241 return true;
242 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100243}
244
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200245static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700246{
247 if (!alpha2)
248 return false;
249 if (alpha2[0] != 0 && alpha2[1] != 0)
250 return true;
251 return false;
252}
Johannes Berg8318d782008-01-24 19:38:38 +0100253
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700254static bool is_alpha_upper(char letter)
255{
256 /* ASCII A - Z */
257 if (letter >= 65 && letter <= 90)
258 return true;
259 return false;
260}
261
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200262static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263{
264 if (!alpha2)
265 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500266 /*
267 * Special case where regulatory domain was built by driver
268 * but a specific alpha2 cannot be determined
269 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700270 if (alpha2[0] == '9' && alpha2[1] == '9')
271 return true;
272 return false;
273}
274
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800275static bool is_intersected_alpha2(const char *alpha2)
276{
277 if (!alpha2)
278 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500279 /*
280 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800281 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500282 * structures
283 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800284 if (alpha2[0] == '9' && alpha2[1] == '8')
285 return true;
286 return false;
287}
288
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200289static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700290{
291 if (!alpha2)
292 return false;
293 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
294 return true;
295 return false;
296}
297
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200298static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700299{
300 if (!alpha2_x || !alpha2_y)
301 return false;
302 if (alpha2_x[0] == alpha2_y[0] &&
303 alpha2_x[1] == alpha2_y[1])
304 return true;
305 return false;
306}
307
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500308static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700309{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500310 assert_cfg80211_lock();
311
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700312 if (!cfg80211_regdomain)
313 return true;
314 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
315 return false;
316 return true;
317}
318
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800319/**
320 * country_ie_integrity_changes - tells us if the country IE has changed
321 * @checksum: checksum of country IE of fields we are interested in
322 *
323 * If the country IE has not changed you can ignore it safely. This is
324 * useful to determine if two devices are seeing two different country IEs
325 * even on the same alpha2. Note that this will return false if no IE has
326 * been set on the wireless core yet.
327 */
328static bool country_ie_integrity_changes(u32 checksum)
329{
330 /* If no IE has been set then the checksum doesn't change */
331 if (unlikely(!last_request->country_ie_checksum))
332 return false;
333 if (unlikely(last_request->country_ie_checksum != checksum))
334 return true;
335 return false;
336}
337
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500338/*
339 * This lets us keep regulatory code which is updated on a regulatory
340 * basis in userspace.
341 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700342static int call_crda(const char *alpha2)
343{
344 char country_env[9 + 2] = "COUNTRY=";
345 char *envp[] = {
346 country_env,
347 NULL
348 };
349
350 if (!is_world_regdom((char *) alpha2))
351 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
352 alpha2[0], alpha2[1]);
353 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700354 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
355 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700356
357 country_env[8] = alpha2[0];
358 country_env[9] = alpha2[1];
359
360 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
361}
362
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700363/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200364bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700365{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400366 assert_cfg80211_lock();
367
Johannes Bergf6037d02008-10-21 11:01:33 +0200368 if (!last_request)
369 return false;
370
371 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700372}
373
374/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200375static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700376{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200377 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700378 u32 freq_diff;
379
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800380 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700381 return false;
382
383 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
384 return false;
385
386 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
387
Roel Kluinbd05f282009-03-03 22:55:21 +0100388 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
389 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700390 return false;
391
392 return true;
393}
394
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200395static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700396{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200397 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700398 unsigned int i;
399
400 if (!rd->n_reg_rules)
401 return false;
402
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800403 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
404 return false;
405
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700406 for (i = 0; i < rd->n_reg_rules; i++) {
407 reg_rule = &rd->reg_rules[i];
408 if (!is_valid_reg_rule(reg_rule))
409 return false;
410 }
411
412 return true;
413}
414
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400415static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
416 u32 center_freq_khz,
417 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700418{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400419 u32 start_freq_khz, end_freq_khz;
420
421 start_freq_khz = center_freq_khz - (bw_khz/2);
422 end_freq_khz = center_freq_khz + (bw_khz/2);
423
424 if (start_freq_khz >= freq_range->start_freq_khz &&
425 end_freq_khz <= freq_range->end_freq_khz)
426 return true;
427
428 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700429}
430
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800431/**
432 * freq_in_rule_band - tells us if a frequency is in a frequency band
433 * @freq_range: frequency rule we want to query
434 * @freq_khz: frequency we are inquiring about
435 *
436 * This lets us know if a specific frequency rule is or is not relevant to
437 * a specific frequency's band. Bands are device specific and artificial
438 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
439 * safe for now to assume that a frequency rule should not be part of a
440 * frequency's band if the start freq or end freq are off by more than 2 GHz.
441 * This resolution can be lowered and should be considered as we add
442 * regulatory rule support for other "bands".
443 **/
444static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
445 u32 freq_khz)
446{
447#define ONE_GHZ_IN_KHZ 1000000
448 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
449 return true;
450 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
451 return true;
452 return false;
453#undef ONE_GHZ_IN_KHZ
454}
455
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500456/*
457 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800458 * structure has a lot of information which the IE doesn't yet have,
459 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500460 * with our userspace regulatory agent to get lower bounds.
461 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800462static struct ieee80211_regdomain *country_ie_2_rd(
463 u8 *country_ie,
464 u8 country_ie_len,
465 u32 *checksum)
466{
467 struct ieee80211_regdomain *rd = NULL;
468 unsigned int i = 0;
469 char alpha2[2];
470 u32 flags = 0;
471 u32 num_rules = 0, size_of_regd = 0;
472 u8 *triplets_start = NULL;
473 u8 len_at_triplet = 0;
474 /* the last channel we have registered in a subband (triplet) */
475 int last_sub_max_channel = 0;
476
477 *checksum = 0xDEADBEEF;
478
479 /* Country IE requirements */
480 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
481 country_ie_len & 0x01);
482
483 alpha2[0] = country_ie[0];
484 alpha2[1] = country_ie[1];
485
486 /*
487 * Third octet can be:
488 * 'I' - Indoor
489 * 'O' - Outdoor
490 *
491 * anything else we assume is no restrictions
492 */
493 if (country_ie[2] == 'I')
494 flags = NL80211_RRF_NO_OUTDOOR;
495 else if (country_ie[2] == 'O')
496 flags = NL80211_RRF_NO_INDOOR;
497
498 country_ie += 3;
499 country_ie_len -= 3;
500
501 triplets_start = country_ie;
502 len_at_triplet = country_ie_len;
503
504 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
505
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500506 /*
507 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800508 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500509 * for each channel subband
510 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800511 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800512 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800513 struct ieee80211_country_ie_triplet *triplet =
514 (struct ieee80211_country_ie_triplet *) country_ie;
515 int cur_sub_max_channel = 0, cur_channel = 0;
516
517 if (triplet->ext.reg_extension_id >=
518 IEEE80211_COUNTRY_EXTENSION_ID) {
519 country_ie += 3;
520 country_ie_len -= 3;
521 continue;
522 }
523
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800524 /* 2 GHz */
525 if (triplet->chans.first_channel <= 14)
526 end_channel = triplet->chans.first_channel +
527 triplet->chans.num_channels;
528 else
529 /*
530 * 5 GHz -- For example in country IEs if the first
531 * channel given is 36 and the number of channels is 4
532 * then the individual channel numbers defined for the
533 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
534 * and not 36, 37, 38, 39.
535 *
536 * See: http://tinyurl.com/11d-clarification
537 */
538 end_channel = triplet->chans.first_channel +
539 (4 * (triplet->chans.num_channels - 1));
540
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800541 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800542 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800543
544 /* Basic sanity check */
545 if (cur_sub_max_channel < cur_channel)
546 return NULL;
547
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500548 /*
549 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800550 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500551 * increasing
552 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800553 if (last_sub_max_channel) {
554 if (cur_channel <= last_sub_max_channel)
555 return NULL;
556 if (cur_sub_max_channel <= last_sub_max_channel)
557 return NULL;
558 }
559
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500560 /*
561 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800562 * we can throw ext triplets as part of this soup,
563 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500564 * don't support them
565 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800566 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
567 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
568 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
569
570 last_sub_max_channel = cur_sub_max_channel;
571
572 country_ie += 3;
573 country_ie_len -= 3;
574 num_rules++;
575
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500576 /*
577 * Note: this is not a IEEE requirement but
578 * simply a memory requirement
579 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800580 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
581 return NULL;
582 }
583
584 country_ie = triplets_start;
585 country_ie_len = len_at_triplet;
586
587 size_of_regd = sizeof(struct ieee80211_regdomain) +
588 (num_rules * sizeof(struct ieee80211_reg_rule));
589
590 rd = kzalloc(size_of_regd, GFP_KERNEL);
591 if (!rd)
592 return NULL;
593
594 rd->n_reg_rules = num_rules;
595 rd->alpha2[0] = alpha2[0];
596 rd->alpha2[1] = alpha2[1];
597
598 /* This time around we fill in the rd */
599 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800600 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800601 struct ieee80211_country_ie_triplet *triplet =
602 (struct ieee80211_country_ie_triplet *) country_ie;
603 struct ieee80211_reg_rule *reg_rule = NULL;
604 struct ieee80211_freq_range *freq_range = NULL;
605 struct ieee80211_power_rule *power_rule = NULL;
606
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500607 /*
608 * Must parse if dot11RegulatoryClassesRequired is true,
609 * we don't support this yet
610 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800611 if (triplet->ext.reg_extension_id >=
612 IEEE80211_COUNTRY_EXTENSION_ID) {
613 country_ie += 3;
614 country_ie_len -= 3;
615 continue;
616 }
617
618 reg_rule = &rd->reg_rules[i];
619 freq_range = &reg_rule->freq_range;
620 power_rule = &reg_rule->power_rule;
621
622 reg_rule->flags = flags;
623
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800624 /* 2 GHz */
625 if (triplet->chans.first_channel <= 14)
626 end_channel = triplet->chans.first_channel +
627 triplet->chans.num_channels;
628 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800629 end_channel = triplet->chans.first_channel +
630 (4 * (triplet->chans.num_channels - 1));
631
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500632 /*
633 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800634 * the actual band edge, not the center of freq for
635 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500636 * the channels passed
637 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800638 freq_range->start_freq_khz =
639 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
640 triplet->chans.first_channel) - 10);
641 freq_range->end_freq_khz =
642 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800643 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800644
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500645 /*
646 * These are large arbitrary values we use to intersect later.
647 * Increment this if we ever support >= 40 MHz channels
648 * in IEEE 802.11
649 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800650 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
651 power_rule->max_antenna_gain = DBI_TO_MBI(100);
652 power_rule->max_eirp = DBM_TO_MBM(100);
653
654 country_ie += 3;
655 country_ie_len -= 3;
656 i++;
657
658 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
659 }
660
661 return rd;
662}
663
664
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500665/*
666 * Helper for regdom_intersect(), this does the real
667 * mathematical intersection fun
668 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700669static int reg_rules_intersect(
670 const struct ieee80211_reg_rule *rule1,
671 const struct ieee80211_reg_rule *rule2,
672 struct ieee80211_reg_rule *intersected_rule)
673{
674 const struct ieee80211_freq_range *freq_range1, *freq_range2;
675 struct ieee80211_freq_range *freq_range;
676 const struct ieee80211_power_rule *power_rule1, *power_rule2;
677 struct ieee80211_power_rule *power_rule;
678 u32 freq_diff;
679
680 freq_range1 = &rule1->freq_range;
681 freq_range2 = &rule2->freq_range;
682 freq_range = &intersected_rule->freq_range;
683
684 power_rule1 = &rule1->power_rule;
685 power_rule2 = &rule2->power_rule;
686 power_rule = &intersected_rule->power_rule;
687
688 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
689 freq_range2->start_freq_khz);
690 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
691 freq_range2->end_freq_khz);
692 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
693 freq_range2->max_bandwidth_khz);
694
695 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
696 if (freq_range->max_bandwidth_khz > freq_diff)
697 freq_range->max_bandwidth_khz = freq_diff;
698
699 power_rule->max_eirp = min(power_rule1->max_eirp,
700 power_rule2->max_eirp);
701 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
702 power_rule2->max_antenna_gain);
703
704 intersected_rule->flags = (rule1->flags | rule2->flags);
705
706 if (!is_valid_reg_rule(intersected_rule))
707 return -EINVAL;
708
709 return 0;
710}
711
712/**
713 * regdom_intersect - do the intersection between two regulatory domains
714 * @rd1: first regulatory domain
715 * @rd2: second regulatory domain
716 *
717 * Use this function to get the intersection between two regulatory domains.
718 * Once completed we will mark the alpha2 for the rd as intersected, "98",
719 * as no one single alpha2 can represent this regulatory domain.
720 *
721 * Returns a pointer to the regulatory domain structure which will hold the
722 * resulting intersection of rules between rd1 and rd2. We will
723 * kzalloc() this structure for you.
724 */
725static struct ieee80211_regdomain *regdom_intersect(
726 const struct ieee80211_regdomain *rd1,
727 const struct ieee80211_regdomain *rd2)
728{
729 int r, size_of_regd;
730 unsigned int x, y;
731 unsigned int num_rules = 0, rule_idx = 0;
732 const struct ieee80211_reg_rule *rule1, *rule2;
733 struct ieee80211_reg_rule *intersected_rule;
734 struct ieee80211_regdomain *rd;
735 /* This is just a dummy holder to help us count */
736 struct ieee80211_reg_rule irule;
737
738 /* Uses the stack temporarily for counter arithmetic */
739 intersected_rule = &irule;
740
741 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
742
743 if (!rd1 || !rd2)
744 return NULL;
745
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500746 /*
747 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700748 * build them. This is to so we can malloc() and free() a
749 * regdomain once. The reason we use reg_rules_intersect() here
750 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500751 * All rules that do check out OK are valid.
752 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700753
754 for (x = 0; x < rd1->n_reg_rules; x++) {
755 rule1 = &rd1->reg_rules[x];
756 for (y = 0; y < rd2->n_reg_rules; y++) {
757 rule2 = &rd2->reg_rules[y];
758 if (!reg_rules_intersect(rule1, rule2,
759 intersected_rule))
760 num_rules++;
761 memset(intersected_rule, 0,
762 sizeof(struct ieee80211_reg_rule));
763 }
764 }
765
766 if (!num_rules)
767 return NULL;
768
769 size_of_regd = sizeof(struct ieee80211_regdomain) +
770 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
771
772 rd = kzalloc(size_of_regd, GFP_KERNEL);
773 if (!rd)
774 return NULL;
775
776 for (x = 0; x < rd1->n_reg_rules; x++) {
777 rule1 = &rd1->reg_rules[x];
778 for (y = 0; y < rd2->n_reg_rules; y++) {
779 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500780 /*
781 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700782 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500783 * a memcpy()
784 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700785 intersected_rule = &rd->reg_rules[rule_idx];
786 r = reg_rules_intersect(rule1, rule2,
787 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500788 /*
789 * No need to memset here the intersected rule here as
790 * we're not using the stack anymore
791 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700792 if (r)
793 continue;
794 rule_idx++;
795 }
796 }
797
798 if (rule_idx != num_rules) {
799 kfree(rd);
800 return NULL;
801 }
802
803 rd->n_reg_rules = num_rules;
804 rd->alpha2[0] = '9';
805 rd->alpha2[1] = '8';
806
807 return rd;
808}
809
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500810/*
811 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
812 * want to just have the channel structure use these
813 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700814static u32 map_regdom_flags(u32 rd_flags)
815{
816 u32 channel_flags = 0;
817 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
818 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
819 if (rd_flags & NL80211_RRF_NO_IBSS)
820 channel_flags |= IEEE80211_CHAN_NO_IBSS;
821 if (rd_flags & NL80211_RRF_DFS)
822 channel_flags |= IEEE80211_CHAN_RADAR;
823 return channel_flags;
824}
825
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800826static int freq_reg_info_regd(struct wiphy *wiphy,
827 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400828 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800829 const struct ieee80211_reg_rule **reg_rule,
830 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100831{
832 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800833 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800834 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400835 bool bw_fits = false;
836
837 if (!desired_bw_khz)
838 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +0100839
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800840 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800841
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500842 /*
843 * Follow the driver's regulatory domain, if present, unless a country
844 * IE has been processed or a user wants to help complaince further
845 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400846 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
847 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800848 wiphy->regd)
849 regd = wiphy->regd;
850
851 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700852 return -EINVAL;
853
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800854 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700855 const struct ieee80211_reg_rule *rr;
856 const struct ieee80211_freq_range *fr = NULL;
857 const struct ieee80211_power_rule *pr = NULL;
858
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800859 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700860 fr = &rr->freq_range;
861 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800862
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500863 /*
864 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800865 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500866 * not overwrite it once found
867 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800868 if (!band_rule_found)
869 band_rule_found = freq_in_rule_band(fr, center_freq);
870
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400871 bw_fits = reg_does_bw_fit(fr,
872 center_freq,
873 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800874
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400875 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700876 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400877 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100878 }
879 }
880
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800881 if (!band_rule_found)
882 return -ERANGE;
883
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400884 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700885}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800886EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700887
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400888int freq_reg_info(struct wiphy *wiphy,
889 u32 center_freq,
890 u32 desired_bw_khz,
891 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800892{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400893 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400894 return freq_reg_info_regd(wiphy,
895 center_freq,
896 desired_bw_khz,
897 reg_rule,
898 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800899}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700900
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400901/*
902 * Note that right now we assume the desired channel bandwidth
903 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
904 * per channel, the primary and the extension channel). To support
905 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
906 * new ieee80211_channel.target_bw and re run the regulatory check
907 * on the wiphy with the target_bw specified. Then we can simply use
908 * that below for the desired_bw_khz below.
909 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800910static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
911 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700912{
913 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400914 u32 flags, bw_flags = 0;
915 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700916 const struct ieee80211_reg_rule *reg_rule = NULL;
917 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400918 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800919 struct ieee80211_supported_band *sband;
920 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500921 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800922
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500923 assert_cfg80211_lock();
924
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500925 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
926
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800927 sband = wiphy->bands[band];
928 BUG_ON(chan_idx >= sband->n_channels);
929 chan = &sband->channels[chan_idx];
930
931 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700932
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400933 r = freq_reg_info(wiphy,
934 MHZ_TO_KHZ(chan->center_freq),
935 desired_bw_khz,
936 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700937
938 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500939 /*
940 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800941 * with a frequency range on the center_freq's band, since
942 * IEEE-802.11 allows for a country IE to have a subset of the
943 * regulatory information provided in a country we ignore
944 * disabling the channel unless at least one reg rule was
945 * found on the center_freq's band. For details see this
946 * clarification:
947 *
948 * http://tinyurl.com/11d-clarification
949 */
950 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400951 last_request->initiator ==
952 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800953#ifdef CONFIG_CFG80211_REG_DEBUG
954 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
955 "intact on %s - no rule found in band on "
956 "Country IE\n",
957 chan->center_freq, wiphy_name(wiphy));
958#endif
959 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500960 /*
961 * In this case we know the country IE has at least one reg rule
962 * for the band so we respect its band definitions
963 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800964#ifdef CONFIG_CFG80211_REG_DEBUG
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400965 if (last_request->initiator ==
966 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800967 printk(KERN_DEBUG "cfg80211: Disabling "
968 "channel %d MHz on %s due to "
969 "Country IE\n",
970 chan->center_freq, wiphy_name(wiphy));
971#endif
972 flags |= IEEE80211_CHAN_DISABLED;
973 chan->flags = flags;
974 }
Johannes Berg8318d782008-01-24 19:38:38 +0100975 return;
976 }
977
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700978 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400979 freq_range = &reg_rule->freq_range;
980
981 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
982 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700983
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400984 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500985 request_wiphy && request_wiphy == wiphy &&
Johannes Berg5be83de2009-11-19 00:56:28 +0100986 request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500987 /*
988 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800989 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500990 * settings
991 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800992 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400993 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800994 chan->max_antenna_gain = chan->orig_mag =
995 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -0800996 chan->max_power = chan->orig_mpwr =
997 (int) MBM_TO_DBM(power_rule->max_eirp);
998 return;
999 }
1000
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001001 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001002 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001003 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
John W. Linville253898c2008-04-03 15:32:54 -04001004 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001005 chan->max_power = min(chan->orig_mpwr,
1006 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001007 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001008 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001009}
1010
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001011static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001012{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001013 unsigned int i;
1014 struct ieee80211_supported_band *sband;
1015
1016 BUG_ON(!wiphy->bands[band]);
1017 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001018
1019 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001020 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001021}
1022
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001023static bool ignore_reg_update(struct wiphy *wiphy,
1024 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001025{
1026 if (!last_request)
1027 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001028 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001029 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001030 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001031 /*
1032 * wiphy->regd will be set once the device has its own
1033 * desired regulatory domain set
1034 */
Johannes Berg5be83de2009-11-19 00:56:28 +01001035 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001036 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001037 return true;
1038 return false;
1039}
1040
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001041static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001042{
Johannes Berg79c97e92009-07-07 03:56:12 +02001043 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001044
Johannes Berg79c97e92009-07-07 03:56:12 +02001045 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1046 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001047}
1048
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001049static void handle_reg_beacon(struct wiphy *wiphy,
1050 unsigned int chan_idx,
1051 struct reg_beacon *reg_beacon)
1052{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001053 struct ieee80211_supported_band *sband;
1054 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001055 bool channel_changed = false;
1056 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001057
1058 assert_cfg80211_lock();
1059
1060 sband = wiphy->bands[reg_beacon->chan.band];
1061 chan = &sband->channels[chan_idx];
1062
1063 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1064 return;
1065
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001066 if (chan->beacon_found)
1067 return;
1068
1069 chan->beacon_found = true;
1070
Johannes Berg5be83de2009-11-19 00:56:28 +01001071 if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001072 return;
1073
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001074 chan_before.center_freq = chan->center_freq;
1075 chan_before.flags = chan->flags;
1076
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001077 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001078 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001079 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001080 }
1081
Luis R. Rodriguez37184242009-07-30 17:43:48 -07001082 if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001083 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001084 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001085 }
1086
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001087 if (channel_changed)
1088 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001089}
1090
1091/*
1092 * Called when a scan on a wiphy finds a beacon on
1093 * new channel
1094 */
1095static void wiphy_update_new_beacon(struct wiphy *wiphy,
1096 struct reg_beacon *reg_beacon)
1097{
1098 unsigned int i;
1099 struct ieee80211_supported_band *sband;
1100
1101 assert_cfg80211_lock();
1102
1103 if (!wiphy->bands[reg_beacon->chan.band])
1104 return;
1105
1106 sband = wiphy->bands[reg_beacon->chan.band];
1107
1108 for (i = 0; i < sband->n_channels; i++)
1109 handle_reg_beacon(wiphy, i, reg_beacon);
1110}
1111
1112/*
1113 * Called upon reg changes or a new wiphy is added
1114 */
1115static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1116{
1117 unsigned int i;
1118 struct ieee80211_supported_band *sband;
1119 struct reg_beacon *reg_beacon;
1120
1121 assert_cfg80211_lock();
1122
1123 if (list_empty(&reg_beacon_list))
1124 return;
1125
1126 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1127 if (!wiphy->bands[reg_beacon->chan.band])
1128 continue;
1129 sband = wiphy->bands[reg_beacon->chan.band];
1130 for (i = 0; i < sband->n_channels; i++)
1131 handle_reg_beacon(wiphy, i, reg_beacon);
1132 }
1133}
1134
1135static bool reg_is_world_roaming(struct wiphy *wiphy)
1136{
1137 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1138 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1139 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001140 if (last_request &&
1141 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Johannes Berg5be83de2009-11-19 00:56:28 +01001142 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001143 return true;
1144 return false;
1145}
1146
1147/* Reap the advantages of previously found beacons */
1148static void reg_process_beacons(struct wiphy *wiphy)
1149{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001150 /*
1151 * Means we are just firing up cfg80211, so no beacons would
1152 * have been processed yet.
1153 */
1154 if (!last_request)
1155 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001156 if (!reg_is_world_roaming(wiphy))
1157 return;
1158 wiphy_update_beacon_reg(wiphy);
1159}
1160
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001161static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1162{
1163 if (!chan)
1164 return true;
1165 if (chan->flags & IEEE80211_CHAN_DISABLED)
1166 return true;
1167 /* This would happen when regulatory rules disallow HT40 completely */
1168 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1169 return true;
1170 return false;
1171}
1172
1173static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1174 enum ieee80211_band band,
1175 unsigned int chan_idx)
1176{
1177 struct ieee80211_supported_band *sband;
1178 struct ieee80211_channel *channel;
1179 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1180 unsigned int i;
1181
1182 assert_cfg80211_lock();
1183
1184 sband = wiphy->bands[band];
1185 BUG_ON(chan_idx >= sband->n_channels);
1186 channel = &sband->channels[chan_idx];
1187
1188 if (is_ht40_not_allowed(channel)) {
1189 channel->flags |= IEEE80211_CHAN_NO_HT40;
1190 return;
1191 }
1192
1193 /*
1194 * We need to ensure the extension channels exist to
1195 * be able to use HT40- or HT40+, this finds them (or not)
1196 */
1197 for (i = 0; i < sband->n_channels; i++) {
1198 struct ieee80211_channel *c = &sband->channels[i];
1199 if (c->center_freq == (channel->center_freq - 20))
1200 channel_before = c;
1201 if (c->center_freq == (channel->center_freq + 20))
1202 channel_after = c;
1203 }
1204
1205 /*
1206 * Please note that this assumes target bandwidth is 20 MHz,
1207 * if that ever changes we also need to change the below logic
1208 * to include that as well.
1209 */
1210 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001211 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001212 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001213 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001214
1215 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001216 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001217 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001218 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001219}
1220
1221static void reg_process_ht_flags_band(struct wiphy *wiphy,
1222 enum ieee80211_band band)
1223{
1224 unsigned int i;
1225 struct ieee80211_supported_band *sband;
1226
1227 BUG_ON(!wiphy->bands[band]);
1228 sband = wiphy->bands[band];
1229
1230 for (i = 0; i < sband->n_channels; i++)
1231 reg_process_ht_flags_channel(wiphy, band, i);
1232}
1233
1234static void reg_process_ht_flags(struct wiphy *wiphy)
1235{
1236 enum ieee80211_band band;
1237
1238 if (!wiphy)
1239 return;
1240
1241 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1242 if (wiphy->bands[band])
1243 reg_process_ht_flags_band(wiphy, band);
1244 }
1245
1246}
1247
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001248void wiphy_update_regulatory(struct wiphy *wiphy,
1249 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001250{
1251 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001252
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001253 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001254 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001255 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001256 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001257 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001258 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001259out:
1260 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001261 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001262 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001263 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001264}
1265
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001266static void handle_channel_custom(struct wiphy *wiphy,
1267 enum ieee80211_band band,
1268 unsigned int chan_idx,
1269 const struct ieee80211_regdomain *regd)
1270{
1271 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001272 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1273 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001274 const struct ieee80211_reg_rule *reg_rule = NULL;
1275 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001276 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001277 struct ieee80211_supported_band *sband;
1278 struct ieee80211_channel *chan;
1279
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001280 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001281
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001282 sband = wiphy->bands[band];
1283 BUG_ON(chan_idx >= sband->n_channels);
1284 chan = &sband->channels[chan_idx];
1285
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001286 r = freq_reg_info_regd(wiphy,
1287 MHZ_TO_KHZ(chan->center_freq),
1288 desired_bw_khz,
1289 &reg_rule,
1290 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001291
1292 if (r) {
1293 chan->flags = IEEE80211_CHAN_DISABLED;
1294 return;
1295 }
1296
1297 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001298 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001299
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001300 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1301 bw_flags = IEEE80211_CHAN_NO_HT40;
1302
1303 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001304 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001305 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1306}
1307
1308static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1309 const struct ieee80211_regdomain *regd)
1310{
1311 unsigned int i;
1312 struct ieee80211_supported_band *sband;
1313
1314 BUG_ON(!wiphy->bands[band]);
1315 sband = wiphy->bands[band];
1316
1317 for (i = 0; i < sband->n_channels; i++)
1318 handle_channel_custom(wiphy, band, i, regd);
1319}
1320
1321/* Used by drivers prior to wiphy registration */
1322void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1323 const struct ieee80211_regdomain *regd)
1324{
1325 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001326 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001327
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001328 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001329 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001330 if (!wiphy->bands[band])
1331 continue;
1332 handle_band_custom(wiphy, band, regd);
1333 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001334 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001335 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001336
1337 /*
1338 * no point in calling this if it won't have any effect
1339 * on your device's supportd bands.
1340 */
1341 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001342}
1343EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1344
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001345static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1346 const struct ieee80211_regdomain *src_regd)
1347{
1348 struct ieee80211_regdomain *regd;
1349 int size_of_regd = 0;
1350 unsigned int i;
1351
1352 size_of_regd = sizeof(struct ieee80211_regdomain) +
1353 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1354
1355 regd = kzalloc(size_of_regd, GFP_KERNEL);
1356 if (!regd)
1357 return -ENOMEM;
1358
1359 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1360
1361 for (i = 0; i < src_regd->n_reg_rules; i++)
1362 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1363 sizeof(struct ieee80211_reg_rule));
1364
1365 *dst_regd = regd;
1366 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001367}
1368
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001369/*
1370 * Return value which can be used by ignore_request() to indicate
1371 * it has been determined we should intersect two regulatory domains
1372 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001373#define REG_INTERSECT 1
1374
Johannes Berg84fa4f42008-10-24 20:32:23 +02001375/* This has the logic which determines when a new request
1376 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001377static int ignore_request(struct wiphy *wiphy,
1378 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001379{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001380 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001381
1382 assert_cfg80211_lock();
1383
Johannes Berg84fa4f42008-10-24 20:32:23 +02001384 /* All initial requests are respected */
1385 if (!last_request)
1386 return 0;
1387
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001388 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001389 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001390 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001391 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001392
1393 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1394
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001395 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001396 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001397 if (last_request->initiator ==
1398 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001399 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001400 /*
1401 * Two cards with two APs claiming different
Thadeu Lima de Souza Cascardo1fe90b02009-08-11 11:18:42 -03001402 * Country IE alpha2s. We could
Johannes Berg84fa4f42008-10-24 20:32:23 +02001403 * intersect them, but that seems unlikely
1404 * to be correct. Reject second one for now.
1405 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001406 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001407 return -EOPNOTSUPP;
1408 return -EALREADY;
1409 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001410 /*
1411 * Two consecutive Country IE hints on the same wiphy.
1412 * This should be picked up early by the driver/stack
1413 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001414 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001415 return 0;
1416 return -EALREADY;
1417 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001418 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001419 case NL80211_REGDOM_SET_BY_DRIVER:
1420 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001421 if (is_old_static_regdom(cfg80211_regdomain))
1422 return 0;
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001423 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001424 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001425 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001426 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001427
1428 /*
1429 * This would happen if you unplug and plug your card
1430 * back in or if you add a new device for which the previously
1431 * loaded card also agrees on the regulatory domain.
1432 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001433 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001434 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001435 return -EALREADY;
1436
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001437 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001438 case NL80211_REGDOM_SET_BY_USER:
1439 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001440 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001441 /*
1442 * If the user knows better the user should set the regdom
1443 * to their country before the IE is picked up
1444 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001445 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001446 last_request->intersect)
1447 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001448 /*
1449 * Process user requests only after previous user/driver/core
1450 * requests have been processed
1451 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001452 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1453 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1454 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001455 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001456 return -EAGAIN;
1457 }
1458
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001459 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001460 !regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001461 return -EALREADY;
1462
Johannes Berg84fa4f42008-10-24 20:32:23 +02001463 return 0;
1464 }
1465
1466 return -EINVAL;
1467}
1468
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001469/**
1470 * __regulatory_hint - hint to the wireless core a regulatory domain
1471 * @wiphy: if the hint comes from country information from an AP, this
1472 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001473 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001474 *
1475 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001476 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001477 *
1478 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1479 * already been set or other standard error codes.
1480 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001481 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001482 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001483static int __regulatory_hint(struct wiphy *wiphy,
1484 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001485{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001486 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001487 int r = 0;
1488
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001489 assert_cfg80211_lock();
1490
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001491 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001492
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001493 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001494 if (pending_request->initiator ==
1495 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001496 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001497 if (r) {
1498 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001499 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001500 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001501 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001502 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001503 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001504 /*
1505 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001506 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001507 * wiphy
1508 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001509 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001510 pending_request->initiator ==
1511 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001512 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001513 if (r) {
1514 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001515 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001516 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001517 r = -EALREADY;
1518 goto new_request;
1519 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001520 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001521 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001522 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001523
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001524new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001525 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001526
1527 last_request = pending_request;
1528 last_request->intersect = intersect;
1529
1530 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001531
1532 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001533 if (r < 0) {
1534 /*
1535 * Since CRDA will not be called in this case as we already
1536 * have applied the requested regulatory domain before we just
1537 * inform userspace we have processed the request
1538 */
1539 if (r == -EALREADY)
1540 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001541 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001542 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001543
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001544 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001545}
1546
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001547/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001548static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001549{
1550 int r = 0;
1551 struct wiphy *wiphy = NULL;
1552
1553 BUG_ON(!reg_request->alpha2);
1554
1555 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001556 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001557
1558 if (wiphy_idx_valid(reg_request->wiphy_idx))
1559 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1560
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001561 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001562 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001563 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001564 goto out;
1565 }
1566
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001567 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001568 /* This is required so that the orig_* parameters are saved */
Johannes Berg5be83de2009-11-19 00:56:28 +01001569 if (r == -EALREADY && wiphy &&
1570 wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001571 wiphy_update_regulatory(wiphy, reg_request->initiator);
1572out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001573 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001574 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001575}
1576
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001577/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001578static void reg_process_pending_hints(void)
1579 {
1580 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001581
1582 spin_lock(&reg_requests_lock);
1583 while (!list_empty(&reg_requests_list)) {
1584 reg_request = list_first_entry(&reg_requests_list,
1585 struct regulatory_request,
1586 list);
1587 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001588
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001589 spin_unlock(&reg_requests_lock);
1590 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001591 spin_lock(&reg_requests_lock);
1592 }
1593 spin_unlock(&reg_requests_lock);
1594}
1595
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001596/* Processes beacon hints -- this has nothing to do with country IEs */
1597static void reg_process_pending_beacon_hints(void)
1598{
Johannes Berg79c97e92009-07-07 03:56:12 +02001599 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001600 struct reg_beacon *pending_beacon, *tmp;
1601
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001602 /*
1603 * No need to hold the reg_mutex here as we just touch wiphys
1604 * and do not read or access regulatory variables.
1605 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001606 mutex_lock(&cfg80211_mutex);
1607
1608 /* This goes through the _pending_ beacon list */
1609 spin_lock_bh(&reg_pending_beacons_lock);
1610
1611 if (list_empty(&reg_pending_beacons)) {
1612 spin_unlock_bh(&reg_pending_beacons_lock);
1613 goto out;
1614 }
1615
1616 list_for_each_entry_safe(pending_beacon, tmp,
1617 &reg_pending_beacons, list) {
1618
1619 list_del_init(&pending_beacon->list);
1620
1621 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001622 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1623 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001624
1625 /* Remembers the beacon hint for new wiphys or reg changes */
1626 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1627 }
1628
1629 spin_unlock_bh(&reg_pending_beacons_lock);
1630out:
1631 mutex_unlock(&cfg80211_mutex);
1632}
1633
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001634static void reg_todo(struct work_struct *work)
1635{
1636 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001637 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001638}
1639
1640static DECLARE_WORK(reg_work, reg_todo);
1641
1642static void queue_regulatory_request(struct regulatory_request *request)
1643{
1644 spin_lock(&reg_requests_lock);
1645 list_add_tail(&request->list, &reg_requests_list);
1646 spin_unlock(&reg_requests_lock);
1647
1648 schedule_work(&reg_work);
1649}
1650
1651/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001652static int regulatory_hint_core(const char *alpha2)
1653{
1654 struct regulatory_request *request;
1655
1656 BUG_ON(last_request);
1657
1658 request = kzalloc(sizeof(struct regulatory_request),
1659 GFP_KERNEL);
1660 if (!request)
1661 return -ENOMEM;
1662
1663 request->alpha2[0] = alpha2[0];
1664 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001665 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001666
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001667 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001668
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001669 /*
1670 * This ensures last_request is populated once modules
1671 * come swinging in and calling regulatory hints and
1672 * wiphy_apply_custom_regulatory().
1673 */
1674 flush_scheduled_work();
1675
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001676 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001677}
1678
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001679/* User hints */
1680int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001681{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001682 struct regulatory_request *request;
1683
Johannes Bergbe3d4812008-10-24 20:32:21 +02001684 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001685
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001686 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1687 if (!request)
1688 return -ENOMEM;
1689
1690 request->wiphy_idx = WIPHY_IDX_STALE;
1691 request->alpha2[0] = alpha2[0];
1692 request->alpha2[1] = alpha2[1];
Luis R. Rodrigueze12822e2010-01-04 11:37:39 -05001693 request->initiator = NL80211_REGDOM_SET_BY_USER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001694
1695 queue_regulatory_request(request);
1696
1697 return 0;
1698}
1699
1700/* Driver hints */
1701int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1702{
1703 struct regulatory_request *request;
1704
1705 BUG_ON(!alpha2);
1706 BUG_ON(!wiphy);
1707
1708 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1709 if (!request)
1710 return -ENOMEM;
1711
1712 request->wiphy_idx = get_wiphy_idx(wiphy);
1713
1714 /* Must have registered wiphy first */
1715 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1716
1717 request->alpha2[0] = alpha2[0];
1718 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001719 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001720
1721 queue_regulatory_request(request);
1722
1723 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001724}
1725EXPORT_SYMBOL(regulatory_hint);
1726
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001727/* Caller must hold reg_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001728static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1729 u32 country_ie_checksum)
1730{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001731 struct wiphy *request_wiphy;
1732
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001733 assert_reg_lock();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001734
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001735 if (unlikely(last_request->initiator !=
1736 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1737 return false;
1738
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001739 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1740
1741 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001742 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001743
1744 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001745 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001746 /*
1747 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001748 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001749 * on the device
1750 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001751 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1752 return true;
1753 return false;
1754}
1755
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001756/*
1757 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1758 * therefore cannot iterate over the rdev list here.
1759 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001760void regulatory_hint_11d(struct wiphy *wiphy,
1761 u8 *country_ie,
1762 u8 country_ie_len)
1763{
1764 struct ieee80211_regdomain *rd = NULL;
1765 char alpha2[2];
1766 u32 checksum = 0;
1767 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001768 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001769
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001770 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001771
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001772 if (unlikely(!last_request))
1773 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001774
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001775 /* IE len must be evenly divisible by 2 */
1776 if (country_ie_len & 0x01)
1777 goto out;
1778
1779 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1780 goto out;
1781
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001782 /*
1783 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001784 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001785 * we were able to process the last regulatory_hint_11d() call
1786 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001787 if (country_ie_regdomain)
1788 goto out;
1789
1790 alpha2[0] = country_ie[0];
1791 alpha2[1] = country_ie[1];
1792
1793 if (country_ie[2] == 'I')
1794 env = ENVIRON_INDOOR;
1795 else if (country_ie[2] == 'O')
1796 env = ENVIRON_OUTDOOR;
1797
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001798 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001799 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001800 * We leave conflict resolution to the workqueue, where can hold
1801 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001802 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001803 if (likely(last_request->initiator ==
1804 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001805 wiphy_idx_valid(last_request->wiphy_idx)))
1806 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001807
1808 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1809 if (!rd)
1810 goto out;
1811
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001812 /*
1813 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001814 * the future when we want to add suspend/resume support and having
1815 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001816 * move to another AP. Right now we just trust the first AP.
1817 *
1818 * If we hit this before we add this support we want to be informed of
1819 * it as it would indicate a mistake in the current design
1820 */
1821 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001822 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001823
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001824 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1825 if (!request)
1826 goto free_rd_out;
1827
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001828 /*
1829 * We keep this around for when CRDA comes back with a response so
1830 * we can intersect with that
1831 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001832 country_ie_regdomain = rd;
1833
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001834 request->wiphy_idx = get_wiphy_idx(wiphy);
1835 request->alpha2[0] = rd->alpha2[0];
1836 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001837 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001838 request->country_ie_checksum = checksum;
1839 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001840
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001841 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001842
1843 queue_regulatory_request(request);
1844
1845 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001846
1847free_rd_out:
1848 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001849out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001850 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001851}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001852
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001853static bool freq_is_chan_12_13_14(u16 freq)
1854{
1855 if (freq == ieee80211_channel_to_frequency(12) ||
1856 freq == ieee80211_channel_to_frequency(13) ||
1857 freq == ieee80211_channel_to_frequency(14))
1858 return true;
1859 return false;
1860}
1861
1862int regulatory_hint_found_beacon(struct wiphy *wiphy,
1863 struct ieee80211_channel *beacon_chan,
1864 gfp_t gfp)
1865{
1866 struct reg_beacon *reg_beacon;
1867
1868 if (likely((beacon_chan->beacon_found ||
1869 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1870 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1871 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1872 return 0;
1873
1874 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1875 if (!reg_beacon)
1876 return -ENOMEM;
1877
1878#ifdef CONFIG_CFG80211_REG_DEBUG
1879 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1880 "frequency: %d MHz (Ch %d) on %s\n",
1881 beacon_chan->center_freq,
1882 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1883 wiphy_name(wiphy));
1884#endif
1885 memcpy(&reg_beacon->chan, beacon_chan,
1886 sizeof(struct ieee80211_channel));
1887
1888
1889 /*
1890 * Since we can be called from BH or and non-BH context
1891 * we must use spin_lock_bh()
1892 */
1893 spin_lock_bh(&reg_pending_beacons_lock);
1894 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1895 spin_unlock_bh(&reg_pending_beacons_lock);
1896
1897 schedule_work(&reg_work);
1898
1899 return 0;
1900}
1901
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001902static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001903{
1904 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001905 const struct ieee80211_reg_rule *reg_rule = NULL;
1906 const struct ieee80211_freq_range *freq_range = NULL;
1907 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001908
Kalle Valo269ac5f2009-12-01 10:47:15 +02001909 printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001910 "(max_antenna_gain, max_eirp)\n");
1911
1912 for (i = 0; i < rd->n_reg_rules; i++) {
1913 reg_rule = &rd->reg_rules[i];
1914 freq_range = &reg_rule->freq_range;
1915 power_rule = &reg_rule->power_rule;
1916
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001917 /*
1918 * There may not be documentation for max antenna gain
1919 * in certain regions
1920 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001921 if (power_rule->max_antenna_gain)
Kalle Valo269ac5f2009-12-01 10:47:15 +02001922 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001923 "(%d mBi, %d mBm)\n",
1924 freq_range->start_freq_khz,
1925 freq_range->end_freq_khz,
1926 freq_range->max_bandwidth_khz,
1927 power_rule->max_antenna_gain,
1928 power_rule->max_eirp);
1929 else
Kalle Valo269ac5f2009-12-01 10:47:15 +02001930 printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001931 "(N/A, %d mBm)\n",
1932 freq_range->start_freq_khz,
1933 freq_range->end_freq_khz,
1934 freq_range->max_bandwidth_khz,
1935 power_rule->max_eirp);
1936 }
1937}
1938
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001939static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001940{
1941
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001942 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001943
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001944 if (last_request->initiator ==
1945 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001946 struct cfg80211_registered_device *rdev;
1947 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001948 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02001949 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001950 printk(KERN_INFO "cfg80211: Current regulatory "
1951 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02001952 rdev->country_ie_alpha2[0],
1953 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001954 } else
1955 printk(KERN_INFO "cfg80211: Current regulatory "
1956 "domain intersected: \n");
1957 } else
1958 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001959 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001960 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001961 printk(KERN_INFO "cfg80211: World regulatory "
1962 "domain updated:\n");
1963 else {
1964 if (is_unknown_alpha2(rd->alpha2))
1965 printk(KERN_INFO "cfg80211: Regulatory domain "
1966 "changed to driver built-in settings "
1967 "(unknown country)\n");
1968 else
1969 printk(KERN_INFO "cfg80211: Regulatory domain "
1970 "changed to country: %c%c\n",
1971 rd->alpha2[0], rd->alpha2[1]);
1972 }
1973 print_rd_rules(rd);
1974}
1975
Johannes Berg2df78162008-10-28 16:49:41 +01001976static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001977{
1978 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
1979 rd->alpha2[0], rd->alpha2[1]);
1980 print_rd_rules(rd);
1981}
1982
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001983#ifdef CONFIG_CFG80211_REG_DEBUG
1984static void reg_country_ie_process_debug(
1985 const struct ieee80211_regdomain *rd,
1986 const struct ieee80211_regdomain *country_ie_regdomain,
1987 const struct ieee80211_regdomain *intersected_rd)
1988{
1989 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
1990 print_regdomain_info(country_ie_regdomain);
1991 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
1992 print_regdomain_info(rd);
1993 if (intersected_rd) {
1994 printk(KERN_DEBUG "cfg80211: We intersect both of these "
1995 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08001996 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001997 return;
1998 }
1999 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2000}
2001#else
2002static inline void reg_country_ie_process_debug(
2003 const struct ieee80211_regdomain *rd,
2004 const struct ieee80211_regdomain *country_ie_regdomain,
2005 const struct ieee80211_regdomain *intersected_rd)
2006{
2007}
2008#endif
2009
Johannes Bergd2372b32008-10-24 20:32:20 +02002010/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002011static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002012{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002013 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002014 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002015 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002016 /* Some basic sanity checks first */
2017
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002018 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002019 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002020 return -EINVAL;
2021 update_world_regdomain(rd);
2022 return 0;
2023 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002024
2025 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2026 !is_unknown_alpha2(rd->alpha2))
2027 return -EINVAL;
2028
Johannes Bergf6037d02008-10-21 11:01:33 +02002029 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002030 return -EINVAL;
2031
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002032 /*
2033 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002034 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002035 * and the pending request came in from a country IE
2036 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002037 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002038 /*
2039 * If someone else asked us to change the rd lets only bother
2040 * checking if the alpha2 changes if CRDA was already called
2041 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002042 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05002043 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002044 return -EINVAL;
2045 }
2046
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002047 /*
2048 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002049 * and finally inform them of what we have done, in case they want
2050 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002051 * internal EEPROM data
2052 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002053
Johannes Bergf6037d02008-10-21 11:01:33 +02002054 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002055 return -EINVAL;
2056
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002057 if (!is_valid_rd(rd)) {
2058 printk(KERN_ERR "cfg80211: Invalid "
2059 "regulatory domain detected:\n");
2060 print_regdomain_info(rd);
2061 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002062 }
2063
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002064 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2065
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002066 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002067 int r;
2068
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002069 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002070 reset_regdomains();
2071 cfg80211_regdomain = rd;
2072 return 0;
2073 }
2074
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002075 /*
2076 * For a driver hint, lets copy the regulatory domain the
2077 * driver wanted to the wiphy to deal with conflicts
2078 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002079
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002080 /*
2081 * Userspace could have sent two replies with only
2082 * one kernel request.
2083 */
2084 if (request_wiphy->regd)
2085 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002086
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002087 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002088 if (r)
2089 return r;
2090
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002091 reset_regdomains();
2092 cfg80211_regdomain = rd;
2093 return 0;
2094 }
2095
2096 /* Intersection requires a bit more work */
2097
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002098 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002099
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002100 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2101 if (!intersected_rd)
2102 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002103
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002104 /*
2105 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002106 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002107 * domain we keep it for its private use
2108 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002109 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002110 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002111 else
2112 kfree(rd);
2113
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002114 rd = NULL;
2115
2116 reset_regdomains();
2117 cfg80211_regdomain = intersected_rd;
2118
2119 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002120 }
2121
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002122 /*
2123 * Country IE requests are handled a bit differently, we intersect
2124 * the country IE rd with what CRDA believes that country should have
2125 */
2126
Luis R. Rodriguez729e9c72009-05-31 18:24:34 -04002127 /*
2128 * Userspace could have sent two replies with only
2129 * one kernel request. By the second reply we would have
2130 * already processed and consumed the country_ie_regdomain.
2131 */
2132 if (!country_ie_regdomain)
2133 return -EALREADY;
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002134 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002135
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002136 /*
2137 * Intersect what CRDA returned and our what we
2138 * had built from the Country IE received
2139 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002140
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002141 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002142
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002143 reg_country_ie_process_debug(rd,
2144 country_ie_regdomain,
2145 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002146
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002147 kfree(country_ie_regdomain);
2148 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002149
2150 if (!intersected_rd)
2151 return -EINVAL;
2152
Johannes Berg79c97e92009-07-07 03:56:12 +02002153 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002154
Johannes Berg79c97e92009-07-07 03:56:12 +02002155 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2156 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2157 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002158
2159 BUG_ON(intersected_rd == rd);
2160
2161 kfree(rd);
2162 rd = NULL;
2163
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002164 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002165 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002166
2167 return 0;
2168}
2169
2170
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002171/*
2172 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002173 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002174 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2175 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002176int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002177{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002178 int r;
2179
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002180 assert_cfg80211_lock();
2181
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002182 mutex_lock(&reg_mutex);
2183
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002184 /* Note that this doesn't update the wiphys, this is done below */
2185 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002186 if (r) {
2187 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002188 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002189 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002190 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002191
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002192 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002193 if (!last_request->intersect)
2194 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002195
2196 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002197 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002198
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002199 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002200
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002201 nl80211_send_reg_change_event(last_request);
2202
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002203 mutex_unlock(&reg_mutex);
2204
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002205 return r;
2206}
2207
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002208/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002209void reg_device_remove(struct wiphy *wiphy)
2210{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002211 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002212
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002213 assert_cfg80211_lock();
2214
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002215 mutex_lock(&reg_mutex);
2216
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002217 kfree(wiphy->regd);
2218
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002219 if (last_request)
2220 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002221
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002222 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002223 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002224
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002225 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002226 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002227out:
2228 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002229}
2230
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002231int regulatory_init(void)
2232{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002233 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002234
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002235 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2236 if (IS_ERR(reg_pdev))
2237 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002238
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002239 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002240 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002241
Johannes Berg734366d2008-09-15 10:56:48 +02002242#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002243 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002244
Johannes Berg942b25c2008-09-15 11:26:47 +02002245 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02002246 print_regdomain_info(cfg80211_regdomain);
Johannes Berg734366d2008-09-15 10:56:48 +02002247#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002248 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002249
Johannes Berg734366d2008-09-15 10:56:48 +02002250#endif
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002251 /* We always try to get an update for the static regdomain */
2252 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002253 if (err) {
2254 if (err == -ENOMEM)
2255 return err;
2256 /*
2257 * N.B. kobject_uevent_env() can fail mainly for when we're out
2258 * memory which is handled and propagated appropriately above
2259 * but it can also fail during a netlink_broadcast() or during
2260 * early boot for call_usermodehelper(). For now treat these
2261 * errors as non-fatal.
2262 */
2263 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2264 "to call CRDA during init");
2265#ifdef CONFIG_CFG80211_REG_DEBUG
2266 /* We want to find out exactly why when debugging */
2267 WARN_ON(err);
2268#endif
2269 }
Johannes Berg734366d2008-09-15 10:56:48 +02002270
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002271 /*
2272 * Finally, if the user set the module parameter treat it
2273 * as a user hint.
2274 */
2275 if (!is_world_regdom(ieee80211_regdom))
2276 regulatory_hint_user(ieee80211_regdom);
2277
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002278 return 0;
2279}
2280
2281void regulatory_exit(void)
2282{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002283 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002284 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002285
2286 cancel_work_sync(&reg_work);
2287
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002288 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002289 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002290
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002291 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002292
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002293 kfree(country_ie_regdomain);
2294 country_ie_regdomain = NULL;
2295
Johannes Bergf6037d02008-10-21 11:01:33 +02002296 kfree(last_request);
2297
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002298 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002299
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002300 spin_lock_bh(&reg_pending_beacons_lock);
2301 if (!list_empty(&reg_pending_beacons)) {
2302 list_for_each_entry_safe(reg_beacon, btmp,
2303 &reg_pending_beacons, list) {
2304 list_del(&reg_beacon->list);
2305 kfree(reg_beacon);
2306 }
2307 }
2308 spin_unlock_bh(&reg_pending_beacons_lock);
2309
2310 if (!list_empty(&reg_beacon_list)) {
2311 list_for_each_entry_safe(reg_beacon, btmp,
2312 &reg_beacon_list, list) {
2313 list_del(&reg_beacon->list);
2314 kfree(reg_beacon);
2315 }
2316 }
2317
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002318 spin_lock(&reg_requests_lock);
2319 if (!list_empty(&reg_requests_list)) {
2320 list_for_each_entry_safe(reg_request, tmp,
2321 &reg_requests_list, list) {
2322 list_del(&reg_request->list);
2323 kfree(reg_request);
2324 }
2325 }
2326 spin_unlock(&reg_requests_lock);
2327
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002328 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002329 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002330}