blob: b3ac0aace0e5daf80563caadf67a1473813ffc16 [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),
144 /* IEEE 802.11a, channel 36 */
145 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
146 /* IEEE 802.11a, channel 40 */
147 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
148 /* IEEE 802.11a, channel 44 */
149 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
150 /* IEEE 802.11a, channels 48..64 */
151 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
152 /* 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 = {
158 .n_reg_rules = 3,
159 .alpha2 = "JP",
160 .reg_rules = {
161 /* IEEE 802.11b/g, channels 1..14 */
162 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
163 /* IEEE 802.11a, channels 34..48 */
164 REG_RULE(5170-10, 5240+10, 40, 6, 20,
165 NL80211_RRF_PASSIVE_SCAN),
166 /* IEEE 802.11a, channels 52..64 */
167 REG_RULE(5260-10, 5320+10, 40, 6, 20,
168 NL80211_RRF_NO_IBSS |
169 NL80211_RRF_DFS),
170 }
171};
172
173static const struct ieee80211_regdomain eu_regdom = {
174 .n_reg_rules = 6,
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500175 /*
176 * This alpha2 is bogus, we leave it here just for stupid
177 * backward compatibility
178 */
Johannes Berg734366d2008-09-15 10:56:48 +0200179 .alpha2 = "EU",
180 .reg_rules = {
181 /* IEEE 802.11b/g, channels 1..13 */
182 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
183 /* IEEE 802.11a, channel 36 */
184 REG_RULE(5180-10, 5180+10, 40, 6, 23,
185 NL80211_RRF_PASSIVE_SCAN),
186 /* IEEE 802.11a, channel 40 */
187 REG_RULE(5200-10, 5200+10, 40, 6, 23,
188 NL80211_RRF_PASSIVE_SCAN),
189 /* IEEE 802.11a, channel 44 */
190 REG_RULE(5220-10, 5220+10, 40, 6, 23,
191 NL80211_RRF_PASSIVE_SCAN),
192 /* IEEE 802.11a, channels 48..64 */
193 REG_RULE(5240-10, 5320+10, 40, 6, 20,
194 NL80211_RRF_NO_IBSS |
195 NL80211_RRF_DFS),
196 /* IEEE 802.11a, channels 100..140 */
197 REG_RULE(5500-10, 5700+10, 40, 6, 30,
198 NL80211_RRF_NO_IBSS |
199 NL80211_RRF_DFS),
200 }
201};
202
203static const struct ieee80211_regdomain *static_regdom(char *alpha2)
204{
205 if (alpha2[0] == 'U' && alpha2[1] == 'S')
206 return &us_regdom;
207 if (alpha2[0] == 'J' && alpha2[1] == 'P')
208 return &jp_regdom;
209 if (alpha2[0] == 'E' && alpha2[1] == 'U')
210 return &eu_regdom;
211 /* Default, as per the old rules */
212 return &us_regdom;
213}
214
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200215static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200216{
217 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
218 return true;
219 return false;
220}
Johannes Berg734366d2008-09-15 10:56:48 +0200221#else
Johannes Berg942b25c2008-09-15 11:26:47 +0200222static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
223{
224 return false;
225}
226#endif
227
Johannes Berg734366d2008-09-15 10:56:48 +0200228static void reset_regdomains(void)
229{
Johannes Berg942b25c2008-09-15 11:26:47 +0200230 /* avoid freeing static information or freeing something twice */
231 if (cfg80211_regdomain == cfg80211_world_regdom)
232 cfg80211_regdomain = NULL;
233 if (cfg80211_world_regdom == &world_regdom)
234 cfg80211_world_regdom = NULL;
235 if (cfg80211_regdomain == &world_regdom)
236 cfg80211_regdomain = NULL;
237 if (is_old_static_regdom(cfg80211_regdomain))
238 cfg80211_regdomain = NULL;
239
240 kfree(cfg80211_regdomain);
241 kfree(cfg80211_world_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +0200242
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200243 cfg80211_world_regdom = &world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +0200244 cfg80211_regdomain = NULL;
245}
246
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500247/*
248 * Dynamic world regulatory domain requested by the wireless
249 * core upon initialization
250 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200251static void update_world_regdomain(const struct ieee80211_regdomain *rd)
Johannes Berg734366d2008-09-15 10:56:48 +0200252{
Johannes Bergf6037d02008-10-21 11:01:33 +0200253 BUG_ON(!last_request);
Johannes Berg734366d2008-09-15 10:56:48 +0200254
255 reset_regdomains();
256
257 cfg80211_world_regdom = rd;
258 cfg80211_regdomain = rd;
259}
Johannes Berg734366d2008-09-15 10:56:48 +0200260
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200261bool is_world_regdom(const char *alpha2)
Johannes Berg8318d782008-01-24 19:38:38 +0100262{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263 if (!alpha2)
264 return false;
265 if (alpha2[0] == '0' && alpha2[1] == '0')
266 return true;
267 return false;
Johannes Berg8318d782008-01-24 19:38:38 +0100268}
269
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200270static bool is_alpha2_set(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700271{
272 if (!alpha2)
273 return false;
274 if (alpha2[0] != 0 && alpha2[1] != 0)
275 return true;
276 return false;
277}
Johannes Berg8318d782008-01-24 19:38:38 +0100278
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700279static bool is_alpha_upper(char letter)
280{
281 /* ASCII A - Z */
282 if (letter >= 65 && letter <= 90)
283 return true;
284 return false;
285}
286
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200287static bool is_unknown_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700288{
289 if (!alpha2)
290 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500291 /*
292 * Special case where regulatory domain was built by driver
293 * but a specific alpha2 cannot be determined
294 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700295 if (alpha2[0] == '9' && alpha2[1] == '9')
296 return true;
297 return false;
298}
299
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800300static bool is_intersected_alpha2(const char *alpha2)
301{
302 if (!alpha2)
303 return false;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500304 /*
305 * Special case where regulatory domain is the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800306 * result of an intersection between two regulatory domain
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500307 * structures
308 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800309 if (alpha2[0] == '9' && alpha2[1] == '8')
310 return true;
311 return false;
312}
313
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200314static bool is_an_alpha2(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700315{
316 if (!alpha2)
317 return false;
318 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
319 return true;
320 return false;
321}
322
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200323static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700324{
325 if (!alpha2_x || !alpha2_y)
326 return false;
327 if (alpha2_x[0] == alpha2_y[0] &&
328 alpha2_x[1] == alpha2_y[1])
329 return true;
330 return false;
331}
332
Luis R. Rodriguez69b15722009-02-21 00:04:33 -0500333static bool regdom_changes(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700334{
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500335 assert_cfg80211_lock();
336
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700337 if (!cfg80211_regdomain)
338 return true;
339 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
340 return false;
341 return true;
342}
343
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800344/**
345 * country_ie_integrity_changes - tells us if the country IE has changed
346 * @checksum: checksum of country IE of fields we are interested in
347 *
348 * If the country IE has not changed you can ignore it safely. This is
349 * useful to determine if two devices are seeing two different country IEs
350 * even on the same alpha2. Note that this will return false if no IE has
351 * been set on the wireless core yet.
352 */
353static bool country_ie_integrity_changes(u32 checksum)
354{
355 /* If no IE has been set then the checksum doesn't change */
356 if (unlikely(!last_request->country_ie_checksum))
357 return false;
358 if (unlikely(last_request->country_ie_checksum != checksum))
359 return true;
360 return false;
361}
362
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500363/*
364 * This lets us keep regulatory code which is updated on a regulatory
365 * basis in userspace.
366 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700367static int call_crda(const char *alpha2)
368{
369 char country_env[9 + 2] = "COUNTRY=";
370 char *envp[] = {
371 country_env,
372 NULL
373 };
374
375 if (!is_world_regdom((char *) alpha2))
376 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
377 alpha2[0], alpha2[1]);
378 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700379 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
380 "regulatory domain\n");
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700381
382 country_env[8] = alpha2[0];
383 country_env[9] = alpha2[1];
384
385 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
386}
387
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700388/* Used by nl80211 before kmalloc'ing our regulatory domain */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200389bool reg_is_valid_request(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700390{
Luis R. Rodriguez61405e92009-05-13 17:04:41 -0400391 assert_cfg80211_lock();
392
Johannes Bergf6037d02008-10-21 11:01:33 +0200393 if (!last_request)
394 return false;
395
396 return alpha2_equal(last_request->alpha2, alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700397}
398
399/* Sanity check on a regulatory rule */
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200400static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700401{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200402 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700403 u32 freq_diff;
404
Luis R. Rodriguez91e99002008-11-12 14:21:55 -0800405 if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700406 return false;
407
408 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
409 return false;
410
411 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
412
Roel Kluinbd05f282009-03-03 22:55:21 +0100413 if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
414 freq_range->max_bandwidth_khz > freq_diff)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700415 return false;
416
417 return true;
418}
419
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200420static bool is_valid_rd(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700421{
Johannes Berga3d2eaf2008-09-15 11:10:52 +0200422 const struct ieee80211_reg_rule *reg_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700423 unsigned int i;
424
425 if (!rd->n_reg_rules)
426 return false;
427
Luis R. Rodriguez88dc1c32008-11-12 14:22:01 -0800428 if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
429 return false;
430
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700431 for (i = 0; i < rd->n_reg_rules; i++) {
432 reg_rule = &rd->reg_rules[i];
433 if (!is_valid_reg_rule(reg_rule))
434 return false;
435 }
436
437 return true;
438}
439
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400440static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
441 u32 center_freq_khz,
442 u32 bw_khz)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700443{
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400444 u32 start_freq_khz, end_freq_khz;
445
446 start_freq_khz = center_freq_khz - (bw_khz/2);
447 end_freq_khz = center_freq_khz + (bw_khz/2);
448
449 if (start_freq_khz >= freq_range->start_freq_khz &&
450 end_freq_khz <= freq_range->end_freq_khz)
451 return true;
452
453 return false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700454}
455
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800456/**
457 * freq_in_rule_band - tells us if a frequency is in a frequency band
458 * @freq_range: frequency rule we want to query
459 * @freq_khz: frequency we are inquiring about
460 *
461 * This lets us know if a specific frequency rule is or is not relevant to
462 * a specific frequency's band. Bands are device specific and artificial
463 * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
464 * safe for now to assume that a frequency rule should not be part of a
465 * frequency's band if the start freq or end freq are off by more than 2 GHz.
466 * This resolution can be lowered and should be considered as we add
467 * regulatory rule support for other "bands".
468 **/
469static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
470 u32 freq_khz)
471{
472#define ONE_GHZ_IN_KHZ 1000000
473 if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
474 return true;
475 if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
476 return true;
477 return false;
478#undef ONE_GHZ_IN_KHZ
479}
480
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500481/*
482 * Converts a country IE to a regulatory domain. A regulatory domain
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800483 * structure has a lot of information which the IE doesn't yet have,
484 * so for the other values we use upper max values as we will intersect
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500485 * with our userspace regulatory agent to get lower bounds.
486 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800487static struct ieee80211_regdomain *country_ie_2_rd(
488 u8 *country_ie,
489 u8 country_ie_len,
490 u32 *checksum)
491{
492 struct ieee80211_regdomain *rd = NULL;
493 unsigned int i = 0;
494 char alpha2[2];
495 u32 flags = 0;
496 u32 num_rules = 0, size_of_regd = 0;
497 u8 *triplets_start = NULL;
498 u8 len_at_triplet = 0;
499 /* the last channel we have registered in a subband (triplet) */
500 int last_sub_max_channel = 0;
501
502 *checksum = 0xDEADBEEF;
503
504 /* Country IE requirements */
505 BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
506 country_ie_len & 0x01);
507
508 alpha2[0] = country_ie[0];
509 alpha2[1] = country_ie[1];
510
511 /*
512 * Third octet can be:
513 * 'I' - Indoor
514 * 'O' - Outdoor
515 *
516 * anything else we assume is no restrictions
517 */
518 if (country_ie[2] == 'I')
519 flags = NL80211_RRF_NO_OUTDOOR;
520 else if (country_ie[2] == 'O')
521 flags = NL80211_RRF_NO_INDOOR;
522
523 country_ie += 3;
524 country_ie_len -= 3;
525
526 triplets_start = country_ie;
527 len_at_triplet = country_ie_len;
528
529 *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
530
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500531 /*
532 * We need to build a reg rule for each triplet, but first we must
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800533 * calculate the number of reg rules we will need. We will need one
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500534 * for each channel subband
535 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800536 while (country_ie_len >= 3) {
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800537 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800538 struct ieee80211_country_ie_triplet *triplet =
539 (struct ieee80211_country_ie_triplet *) country_ie;
540 int cur_sub_max_channel = 0, cur_channel = 0;
541
542 if (triplet->ext.reg_extension_id >=
543 IEEE80211_COUNTRY_EXTENSION_ID) {
544 country_ie += 3;
545 country_ie_len -= 3;
546 continue;
547 }
548
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800549 /* 2 GHz */
550 if (triplet->chans.first_channel <= 14)
551 end_channel = triplet->chans.first_channel +
552 triplet->chans.num_channels;
553 else
554 /*
555 * 5 GHz -- For example in country IEs if the first
556 * channel given is 36 and the number of channels is 4
557 * then the individual channel numbers defined for the
558 * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
559 * and not 36, 37, 38, 39.
560 *
561 * See: http://tinyurl.com/11d-clarification
562 */
563 end_channel = triplet->chans.first_channel +
564 (4 * (triplet->chans.num_channels - 1));
565
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800566 cur_channel = triplet->chans.first_channel;
Luis R. Rodriguez615aab42009-01-22 15:05:46 -0800567 cur_sub_max_channel = end_channel;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800568
569 /* Basic sanity check */
570 if (cur_sub_max_channel < cur_channel)
571 return NULL;
572
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500573 /*
574 * Do not allow overlapping channels. Also channels
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800575 * passed in each subband must be monotonically
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500576 * increasing
577 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800578 if (last_sub_max_channel) {
579 if (cur_channel <= last_sub_max_channel)
580 return NULL;
581 if (cur_sub_max_channel <= last_sub_max_channel)
582 return NULL;
583 }
584
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500585 /*
586 * When dot11RegulatoryClassesRequired is supported
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800587 * we can throw ext triplets as part of this soup,
588 * for now we don't care when those change as we
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500589 * don't support them
590 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800591 *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
592 ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
593 ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
594
595 last_sub_max_channel = cur_sub_max_channel;
596
597 country_ie += 3;
598 country_ie_len -= 3;
599 num_rules++;
600
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500601 /*
602 * Note: this is not a IEEE requirement but
603 * simply a memory requirement
604 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800605 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
606 return NULL;
607 }
608
609 country_ie = triplets_start;
610 country_ie_len = len_at_triplet;
611
612 size_of_regd = sizeof(struct ieee80211_regdomain) +
613 (num_rules * sizeof(struct ieee80211_reg_rule));
614
615 rd = kzalloc(size_of_regd, GFP_KERNEL);
616 if (!rd)
617 return NULL;
618
619 rd->n_reg_rules = num_rules;
620 rd->alpha2[0] = alpha2[0];
621 rd->alpha2[1] = alpha2[1];
622
623 /* This time around we fill in the rd */
624 while (country_ie_len >= 3) {
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800625 int end_channel = 0;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800626 struct ieee80211_country_ie_triplet *triplet =
627 (struct ieee80211_country_ie_triplet *) country_ie;
628 struct ieee80211_reg_rule *reg_rule = NULL;
629 struct ieee80211_freq_range *freq_range = NULL;
630 struct ieee80211_power_rule *power_rule = NULL;
631
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500632 /*
633 * Must parse if dot11RegulatoryClassesRequired is true,
634 * we don't support this yet
635 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800636 if (triplet->ext.reg_extension_id >=
637 IEEE80211_COUNTRY_EXTENSION_ID) {
638 country_ie += 3;
639 country_ie_len -= 3;
640 continue;
641 }
642
643 reg_rule = &rd->reg_rules[i];
644 freq_range = &reg_rule->freq_range;
645 power_rule = &reg_rule->power_rule;
646
647 reg_rule->flags = flags;
648
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800649 /* 2 GHz */
650 if (triplet->chans.first_channel <= 14)
651 end_channel = triplet->chans.first_channel +
652 triplet->chans.num_channels;
653 else
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800654 end_channel = triplet->chans.first_channel +
655 (4 * (triplet->chans.num_channels - 1));
656
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500657 /*
658 * The +10 is since the regulatory domain expects
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800659 * the actual band edge, not the center of freq for
660 * its start and end freqs, assuming 20 MHz bandwidth on
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500661 * the channels passed
662 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800663 freq_range->start_freq_khz =
664 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
665 triplet->chans.first_channel) - 10);
666 freq_range->end_freq_khz =
667 MHZ_TO_KHZ(ieee80211_channel_to_frequency(
Luis R. Rodriguez02e68a32009-01-07 17:43:37 -0800668 end_channel) + 10);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800669
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500670 /*
671 * These are large arbitrary values we use to intersect later.
672 * Increment this if we ever support >= 40 MHz channels
673 * in IEEE 802.11
674 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -0800675 freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
676 power_rule->max_antenna_gain = DBI_TO_MBI(100);
677 power_rule->max_eirp = DBM_TO_MBM(100);
678
679 country_ie += 3;
680 country_ie_len -= 3;
681 i++;
682
683 BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
684 }
685
686 return rd;
687}
688
689
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500690/*
691 * Helper for regdom_intersect(), this does the real
692 * mathematical intersection fun
693 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700694static int reg_rules_intersect(
695 const struct ieee80211_reg_rule *rule1,
696 const struct ieee80211_reg_rule *rule2,
697 struct ieee80211_reg_rule *intersected_rule)
698{
699 const struct ieee80211_freq_range *freq_range1, *freq_range2;
700 struct ieee80211_freq_range *freq_range;
701 const struct ieee80211_power_rule *power_rule1, *power_rule2;
702 struct ieee80211_power_rule *power_rule;
703 u32 freq_diff;
704
705 freq_range1 = &rule1->freq_range;
706 freq_range2 = &rule2->freq_range;
707 freq_range = &intersected_rule->freq_range;
708
709 power_rule1 = &rule1->power_rule;
710 power_rule2 = &rule2->power_rule;
711 power_rule = &intersected_rule->power_rule;
712
713 freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
714 freq_range2->start_freq_khz);
715 freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
716 freq_range2->end_freq_khz);
717 freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
718 freq_range2->max_bandwidth_khz);
719
720 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
721 if (freq_range->max_bandwidth_khz > freq_diff)
722 freq_range->max_bandwidth_khz = freq_diff;
723
724 power_rule->max_eirp = min(power_rule1->max_eirp,
725 power_rule2->max_eirp);
726 power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
727 power_rule2->max_antenna_gain);
728
729 intersected_rule->flags = (rule1->flags | rule2->flags);
730
731 if (!is_valid_reg_rule(intersected_rule))
732 return -EINVAL;
733
734 return 0;
735}
736
737/**
738 * regdom_intersect - do the intersection between two regulatory domains
739 * @rd1: first regulatory domain
740 * @rd2: second regulatory domain
741 *
742 * Use this function to get the intersection between two regulatory domains.
743 * Once completed we will mark the alpha2 for the rd as intersected, "98",
744 * as no one single alpha2 can represent this regulatory domain.
745 *
746 * Returns a pointer to the regulatory domain structure which will hold the
747 * resulting intersection of rules between rd1 and rd2. We will
748 * kzalloc() this structure for you.
749 */
750static struct ieee80211_regdomain *regdom_intersect(
751 const struct ieee80211_regdomain *rd1,
752 const struct ieee80211_regdomain *rd2)
753{
754 int r, size_of_regd;
755 unsigned int x, y;
756 unsigned int num_rules = 0, rule_idx = 0;
757 const struct ieee80211_reg_rule *rule1, *rule2;
758 struct ieee80211_reg_rule *intersected_rule;
759 struct ieee80211_regdomain *rd;
760 /* This is just a dummy holder to help us count */
761 struct ieee80211_reg_rule irule;
762
763 /* Uses the stack temporarily for counter arithmetic */
764 intersected_rule = &irule;
765
766 memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
767
768 if (!rd1 || !rd2)
769 return NULL;
770
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500771 /*
772 * First we get a count of the rules we'll need, then we actually
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700773 * build them. This is to so we can malloc() and free() a
774 * regdomain once. The reason we use reg_rules_intersect() here
775 * is it will return -EINVAL if the rule computed makes no sense.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500776 * All rules that do check out OK are valid.
777 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700778
779 for (x = 0; x < rd1->n_reg_rules; x++) {
780 rule1 = &rd1->reg_rules[x];
781 for (y = 0; y < rd2->n_reg_rules; y++) {
782 rule2 = &rd2->reg_rules[y];
783 if (!reg_rules_intersect(rule1, rule2,
784 intersected_rule))
785 num_rules++;
786 memset(intersected_rule, 0,
787 sizeof(struct ieee80211_reg_rule));
788 }
789 }
790
791 if (!num_rules)
792 return NULL;
793
794 size_of_regd = sizeof(struct ieee80211_regdomain) +
795 ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
796
797 rd = kzalloc(size_of_regd, GFP_KERNEL);
798 if (!rd)
799 return NULL;
800
801 for (x = 0; x < rd1->n_reg_rules; x++) {
802 rule1 = &rd1->reg_rules[x];
803 for (y = 0; y < rd2->n_reg_rules; y++) {
804 rule2 = &rd2->reg_rules[y];
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500805 /*
806 * This time around instead of using the stack lets
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700807 * write to the target rule directly saving ourselves
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500808 * a memcpy()
809 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700810 intersected_rule = &rd->reg_rules[rule_idx];
811 r = reg_rules_intersect(rule1, rule2,
812 intersected_rule);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500813 /*
814 * No need to memset here the intersected rule here as
815 * we're not using the stack anymore
816 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -0700817 if (r)
818 continue;
819 rule_idx++;
820 }
821 }
822
823 if (rule_idx != num_rules) {
824 kfree(rd);
825 return NULL;
826 }
827
828 rd->n_reg_rules = num_rules;
829 rd->alpha2[0] = '9';
830 rd->alpha2[1] = '8';
831
832 return rd;
833}
834
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500835/*
836 * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
837 * want to just have the channel structure use these
838 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700839static u32 map_regdom_flags(u32 rd_flags)
840{
841 u32 channel_flags = 0;
842 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
843 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
844 if (rd_flags & NL80211_RRF_NO_IBSS)
845 channel_flags |= IEEE80211_CHAN_NO_IBSS;
846 if (rd_flags & NL80211_RRF_DFS)
847 channel_flags |= IEEE80211_CHAN_RADAR;
848 return channel_flags;
849}
850
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800851static int freq_reg_info_regd(struct wiphy *wiphy,
852 u32 center_freq,
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400853 u32 desired_bw_khz,
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800854 const struct ieee80211_reg_rule **reg_rule,
855 const struct ieee80211_regdomain *custom_regd)
Johannes Berg8318d782008-01-24 19:38:38 +0100856{
857 int i;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800858 bool band_rule_found = false;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800859 const struct ieee80211_regdomain *regd;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400860 bool bw_fits = false;
861
862 if (!desired_bw_khz)
863 desired_bw_khz = MHZ_TO_KHZ(20);
Johannes Berg8318d782008-01-24 19:38:38 +0100864
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800865 regd = custom_regd ? custom_regd : cfg80211_regdomain;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800866
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500867 /*
868 * Follow the driver's regulatory domain, if present, unless a country
869 * IE has been processed or a user wants to help complaince further
870 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400871 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
872 last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800873 wiphy->regd)
874 regd = wiphy->regd;
875
876 if (!regd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700877 return -EINVAL;
878
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800879 for (i = 0; i < regd->n_reg_rules; i++) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700880 const struct ieee80211_reg_rule *rr;
881 const struct ieee80211_freq_range *fr = NULL;
882 const struct ieee80211_power_rule *pr = NULL;
883
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -0800884 rr = &regd->reg_rules[i];
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700885 fr = &rr->freq_range;
886 pr = &rr->power_rule;
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800887
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500888 /*
889 * We only need to know if one frequency rule was
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800890 * was in center_freq's band, that's enough, so lets
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500891 * not overwrite it once found
892 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800893 if (!band_rule_found)
894 band_rule_found = freq_in_rule_band(fr, center_freq);
895
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400896 bw_fits = reg_does_bw_fit(fr,
897 center_freq,
898 desired_bw_khz);
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800899
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400900 if (band_rule_found && bw_fits) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700901 *reg_rule = rr;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400902 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100903 }
904 }
905
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800906 if (!band_rule_found)
907 return -ERANGE;
908
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400909 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700910}
Luis R. Rodriguez34f57342009-01-22 15:05:45 -0800911EXPORT_SYMBOL(freq_reg_info);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700912
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400913int freq_reg_info(struct wiphy *wiphy,
914 u32 center_freq,
915 u32 desired_bw_khz,
916 const struct ieee80211_reg_rule **reg_rule)
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800917{
Luis R. Rodriguezac46d482009-05-01 18:44:50 -0400918 assert_cfg80211_lock();
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400919 return freq_reg_info_regd(wiphy,
920 center_freq,
921 desired_bw_khz,
922 reg_rule,
923 NULL);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -0800924}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700925
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400926/*
927 * Note that right now we assume the desired channel bandwidth
928 * is always 20 MHz for each individual channel (HT40 uses 20 MHz
929 * per channel, the primary and the extension channel). To support
930 * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
931 * new ieee80211_channel.target_bw and re run the regulatory check
932 * on the wiphy with the target_bw specified. Then we can simply use
933 * that below for the desired_bw_khz below.
934 */
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800935static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
936 unsigned int chan_idx)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700937{
938 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400939 u32 flags, bw_flags = 0;
940 u32 desired_bw_khz = MHZ_TO_KHZ(20);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700941 const struct ieee80211_reg_rule *reg_rule = NULL;
942 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400943 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800944 struct ieee80211_supported_band *sband;
945 struct ieee80211_channel *chan;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -0500946 struct wiphy *request_wiphy = NULL;
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800947
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -0500948 assert_cfg80211_lock();
949
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -0500950 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
951
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -0800952 sband = wiphy->bands[band];
953 BUG_ON(chan_idx >= sband->n_channels);
954 chan = &sband->channels[chan_idx];
955
956 flags = chan->orig_flags;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700957
Luis R. Rodriguez038659e2009-05-02 00:37:17 -0400958 r = freq_reg_info(wiphy,
959 MHZ_TO_KHZ(chan->center_freq),
960 desired_bw_khz,
961 &reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700962
963 if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500964 /*
965 * This means no regulatory rule was found in the country IE
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800966 * with a frequency range on the center_freq's band, since
967 * IEEE-802.11 allows for a country IE to have a subset of the
968 * regulatory information provided in a country we ignore
969 * disabling the channel unless at least one reg rule was
970 * found on the center_freq's band. For details see this
971 * clarification:
972 *
973 * http://tinyurl.com/11d-clarification
974 */
975 if (r == -ERANGE &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400976 last_request->initiator ==
977 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800978#ifdef CONFIG_CFG80211_REG_DEBUG
979 printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz "
980 "intact on %s - no rule found in band on "
981 "Country IE\n",
982 chan->center_freq, wiphy_name(wiphy));
983#endif
984 } else {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -0500985 /*
986 * In this case we know the country IE has at least one reg rule
987 * for the band so we respect its band definitions
988 */
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800989#ifdef CONFIG_CFG80211_REG_DEBUG
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -0400990 if (last_request->initiator ==
991 NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez0c7dc452009-01-07 17:43:36 -0800992 printk(KERN_DEBUG "cfg80211: Disabling "
993 "channel %d MHz on %s due to "
994 "Country IE\n",
995 chan->center_freq, wiphy_name(wiphy));
996#endif
997 flags |= IEEE80211_CHAN_DISABLED;
998 chan->flags = flags;
999 }
Johannes Berg8318d782008-01-24 19:38:38 +01001000 return;
1001 }
1002
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001003 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001004 freq_range = &reg_rule->freq_range;
1005
1006 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1007 bw_flags = IEEE80211_CHAN_NO_HT40;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001008
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001009 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001010 request_wiphy && request_wiphy == wiphy &&
1011 request_wiphy->strict_regulatory) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001012 /*
1013 * This gaurantees the driver's requested regulatory domain
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001014 * will always be used as a base for further regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001015 * settings
1016 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001017 chan->flags = chan->orig_flags =
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001018 map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001019 chan->max_antenna_gain = chan->orig_mag =
1020 (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001021 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001022 chan->max_power = chan->orig_mpwr =
1023 (int) MBM_TO_DBM(power_rule->max_eirp);
1024 return;
1025 }
1026
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001027 chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
Johannes Berg8318d782008-01-24 19:38:38 +01001028 chan->max_antenna_gain = min(chan->orig_mag,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001029 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001030 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
John W. Linville253898c2008-04-03 15:32:54 -04001031 if (chan->orig_mpwr)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001032 chan->max_power = min(chan->orig_mpwr,
1033 (int) MBM_TO_DBM(power_rule->max_eirp));
John W. Linville253898c2008-04-03 15:32:54 -04001034 else
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001035 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
Johannes Berg8318d782008-01-24 19:38:38 +01001036}
1037
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001038static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +01001039{
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001040 unsigned int i;
1041 struct ieee80211_supported_band *sband;
1042
1043 BUG_ON(!wiphy->bands[band]);
1044 sband = wiphy->bands[band];
Johannes Berg8318d782008-01-24 19:38:38 +01001045
1046 for (i = 0; i < sband->n_channels; i++)
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001047 handle_channel(wiphy, band, i);
Johannes Berg8318d782008-01-24 19:38:38 +01001048}
1049
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001050static bool ignore_reg_update(struct wiphy *wiphy,
1051 enum nl80211_reg_initiator initiator)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001052{
1053 if (!last_request)
1054 return true;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001055 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
Luis R. Rodriguez2a44f912009-01-22 15:05:49 -08001056 wiphy->custom_regulatory)
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001057 return true;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001058 /*
1059 * wiphy->regd will be set once the device has its own
1060 * desired regulatory domain set
1061 */
Luis R. Rodriguezf9763762009-01-22 15:05:52 -08001062 if (wiphy->strict_regulatory && !wiphy->regd &&
1063 !is_world_regdom(last_request->alpha2))
Luis R. Rodriguez14b98152008-11-12 14:22:03 -08001064 return true;
1065 return false;
1066}
1067
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001068static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001069{
Johannes Berg79c97e92009-07-07 03:56:12 +02001070 struct cfg80211_registered_device *rdev;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001071
Johannes Berg79c97e92009-07-07 03:56:12 +02001072 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1073 wiphy_update_regulatory(&rdev->wiphy, initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001074}
1075
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001076static void handle_reg_beacon(struct wiphy *wiphy,
1077 unsigned int chan_idx,
1078 struct reg_beacon *reg_beacon)
1079{
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001080 struct ieee80211_supported_band *sband;
1081 struct ieee80211_channel *chan;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001082 bool channel_changed = false;
1083 struct ieee80211_channel chan_before;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001084
1085 assert_cfg80211_lock();
1086
1087 sband = wiphy->bands[reg_beacon->chan.band];
1088 chan = &sband->channels[chan_idx];
1089
1090 if (likely(chan->center_freq != reg_beacon->chan.center_freq))
1091 return;
1092
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001093 if (chan->beacon_found)
1094 return;
1095
1096 chan->beacon_found = true;
1097
1098 chan_before.center_freq = chan->center_freq;
1099 chan_before.flags = chan->flags;
1100
Luis R. Rodrigueza4ed90d2009-04-02 14:08:07 -04001101 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
1102 !(chan->orig_flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001103 chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001104 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001105 }
1106
Luis R. Rodrigueza4ed90d2009-04-02 14:08:07 -04001107 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
1108 !(chan->orig_flags & IEEE80211_CHAN_NO_IBSS)) {
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001109 chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001110 channel_changed = true;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001111 }
1112
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04001113 if (channel_changed)
1114 nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001115}
1116
1117/*
1118 * Called when a scan on a wiphy finds a beacon on
1119 * new channel
1120 */
1121static void wiphy_update_new_beacon(struct wiphy *wiphy,
1122 struct reg_beacon *reg_beacon)
1123{
1124 unsigned int i;
1125 struct ieee80211_supported_band *sband;
1126
1127 assert_cfg80211_lock();
1128
1129 if (!wiphy->bands[reg_beacon->chan.band])
1130 return;
1131
1132 sband = wiphy->bands[reg_beacon->chan.band];
1133
1134 for (i = 0; i < sband->n_channels; i++)
1135 handle_reg_beacon(wiphy, i, reg_beacon);
1136}
1137
1138/*
1139 * Called upon reg changes or a new wiphy is added
1140 */
1141static void wiphy_update_beacon_reg(struct wiphy *wiphy)
1142{
1143 unsigned int i;
1144 struct ieee80211_supported_band *sband;
1145 struct reg_beacon *reg_beacon;
1146
1147 assert_cfg80211_lock();
1148
1149 if (list_empty(&reg_beacon_list))
1150 return;
1151
1152 list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
1153 if (!wiphy->bands[reg_beacon->chan.band])
1154 continue;
1155 sband = wiphy->bands[reg_beacon->chan.band];
1156 for (i = 0; i < sband->n_channels; i++)
1157 handle_reg_beacon(wiphy, i, reg_beacon);
1158 }
1159}
1160
1161static bool reg_is_world_roaming(struct wiphy *wiphy)
1162{
1163 if (is_world_regdom(cfg80211_regdomain->alpha2) ||
1164 (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
1165 return true;
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001166 if (last_request &&
1167 last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001168 wiphy->custom_regulatory)
1169 return true;
1170 return false;
1171}
1172
1173/* Reap the advantages of previously found beacons */
1174static void reg_process_beacons(struct wiphy *wiphy)
1175{
Luis R. Rodriguezb1ed8dd2009-05-02 00:34:15 -04001176 /*
1177 * Means we are just firing up cfg80211, so no beacons would
1178 * have been processed yet.
1179 */
1180 if (!last_request)
1181 return;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001182 if (!reg_is_world_roaming(wiphy))
1183 return;
1184 wiphy_update_beacon_reg(wiphy);
1185}
1186
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001187static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
1188{
1189 if (!chan)
1190 return true;
1191 if (chan->flags & IEEE80211_CHAN_DISABLED)
1192 return true;
1193 /* This would happen when regulatory rules disallow HT40 completely */
1194 if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
1195 return true;
1196 return false;
1197}
1198
1199static void reg_process_ht_flags_channel(struct wiphy *wiphy,
1200 enum ieee80211_band band,
1201 unsigned int chan_idx)
1202{
1203 struct ieee80211_supported_band *sband;
1204 struct ieee80211_channel *channel;
1205 struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
1206 unsigned int i;
1207
1208 assert_cfg80211_lock();
1209
1210 sband = wiphy->bands[band];
1211 BUG_ON(chan_idx >= sband->n_channels);
1212 channel = &sband->channels[chan_idx];
1213
1214 if (is_ht40_not_allowed(channel)) {
1215 channel->flags |= IEEE80211_CHAN_NO_HT40;
1216 return;
1217 }
1218
1219 /*
1220 * We need to ensure the extension channels exist to
1221 * be able to use HT40- or HT40+, this finds them (or not)
1222 */
1223 for (i = 0; i < sband->n_channels; i++) {
1224 struct ieee80211_channel *c = &sband->channels[i];
1225 if (c->center_freq == (channel->center_freq - 20))
1226 channel_before = c;
1227 if (c->center_freq == (channel->center_freq + 20))
1228 channel_after = c;
1229 }
1230
1231 /*
1232 * Please note that this assumes target bandwidth is 20 MHz,
1233 * if that ever changes we also need to change the below logic
1234 * to include that as well.
1235 */
1236 if (is_ht40_not_allowed(channel_before))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001237 channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001238 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001239 channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001240
1241 if (is_ht40_not_allowed(channel_after))
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001242 channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001243 else
Luis R. Rodriguez689da1b2009-05-02 00:37:18 -04001244 channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001245}
1246
1247static void reg_process_ht_flags_band(struct wiphy *wiphy,
1248 enum ieee80211_band band)
1249{
1250 unsigned int i;
1251 struct ieee80211_supported_band *sband;
1252
1253 BUG_ON(!wiphy->bands[band]);
1254 sband = wiphy->bands[band];
1255
1256 for (i = 0; i < sband->n_channels; i++)
1257 reg_process_ht_flags_channel(wiphy, band, i);
1258}
1259
1260static void reg_process_ht_flags(struct wiphy *wiphy)
1261{
1262 enum ieee80211_band band;
1263
1264 if (!wiphy)
1265 return;
1266
1267 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1268 if (wiphy->bands[band])
1269 reg_process_ht_flags_band(wiphy, band);
1270 }
1271
1272}
1273
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001274void wiphy_update_regulatory(struct wiphy *wiphy,
1275 enum nl80211_reg_initiator initiator)
Johannes Berg8318d782008-01-24 19:38:38 +01001276{
1277 enum ieee80211_band band;
Luis R. Rodriguezd46e5b12009-01-22 15:05:50 -08001278
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001279 if (ignore_reg_update(wiphy, initiator))
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001280 goto out;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001281 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01001282 if (wiphy->bands[band])
Luis R. Rodrigueza92a3ce2009-01-07 17:43:33 -08001283 handle_band(wiphy, band);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001284 }
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001285out:
1286 reg_process_beacons(wiphy);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001287 reg_process_ht_flags(wiphy);
Luis R. Rodriguez560e28e2009-01-07 17:43:32 -08001288 if (wiphy->reg_notifier)
Luis R. Rodriguez716f9392009-01-22 15:05:51 -08001289 wiphy->reg_notifier(wiphy, last_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001290}
1291
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001292static void handle_channel_custom(struct wiphy *wiphy,
1293 enum ieee80211_band band,
1294 unsigned int chan_idx,
1295 const struct ieee80211_regdomain *regd)
1296{
1297 int r;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001298 u32 desired_bw_khz = MHZ_TO_KHZ(20);
1299 u32 bw_flags = 0;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001300 const struct ieee80211_reg_rule *reg_rule = NULL;
1301 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001302 const struct ieee80211_freq_range *freq_range = NULL;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001303 struct ieee80211_supported_band *sband;
1304 struct ieee80211_channel *chan;
1305
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001306 assert_reg_lock();
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001307
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001308 sband = wiphy->bands[band];
1309 BUG_ON(chan_idx >= sband->n_channels);
1310 chan = &sband->channels[chan_idx];
1311
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001312 r = freq_reg_info_regd(wiphy,
1313 MHZ_TO_KHZ(chan->center_freq),
1314 desired_bw_khz,
1315 &reg_rule,
1316 regd);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001317
1318 if (r) {
1319 chan->flags = IEEE80211_CHAN_DISABLED;
1320 return;
1321 }
1322
1323 power_rule = &reg_rule->power_rule;
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001324 freq_range = &reg_rule->freq_range;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001325
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001326 if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
1327 bw_flags = IEEE80211_CHAN_NO_HT40;
1328
1329 chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001330 chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
Luis R. Rodriguez038659e2009-05-02 00:37:17 -04001331 chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001332 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
1333}
1334
1335static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
1336 const struct ieee80211_regdomain *regd)
1337{
1338 unsigned int i;
1339 struct ieee80211_supported_band *sband;
1340
1341 BUG_ON(!wiphy->bands[band]);
1342 sband = wiphy->bands[band];
1343
1344 for (i = 0; i < sband->n_channels; i++)
1345 handle_channel_custom(wiphy, band, i, regd);
1346}
1347
1348/* Used by drivers prior to wiphy registration */
1349void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
1350 const struct ieee80211_regdomain *regd)
1351{
1352 enum ieee80211_band band;
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001353 unsigned int bands_set = 0;
Luis R. Rodriguezac46d482009-05-01 18:44:50 -04001354
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001355 mutex_lock(&reg_mutex);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001356 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001357 if (!wiphy->bands[band])
1358 continue;
1359 handle_band_custom(wiphy, band, regd);
1360 bands_set++;
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001361 }
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001362 mutex_unlock(&reg_mutex);
Luis R. Rodriguezbbcf3f02009-05-19 17:49:47 -04001363
1364 /*
1365 * no point in calling this if it won't have any effect
1366 * on your device's supportd bands.
1367 */
1368 WARN_ON(!bands_set);
Luis R. Rodriguez1fa25e42009-01-22 15:05:44 -08001369}
1370EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
1371
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001372static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
1373 const struct ieee80211_regdomain *src_regd)
1374{
1375 struct ieee80211_regdomain *regd;
1376 int size_of_regd = 0;
1377 unsigned int i;
1378
1379 size_of_regd = sizeof(struct ieee80211_regdomain) +
1380 ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
1381
1382 regd = kzalloc(size_of_regd, GFP_KERNEL);
1383 if (!regd)
1384 return -ENOMEM;
1385
1386 memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
1387
1388 for (i = 0; i < src_regd->n_reg_rules; i++)
1389 memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
1390 sizeof(struct ieee80211_reg_rule));
1391
1392 *dst_regd = regd;
1393 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001394}
1395
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001396/*
1397 * Return value which can be used by ignore_request() to indicate
1398 * it has been determined we should intersect two regulatory domains
1399 */
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001400#define REG_INTERSECT 1
1401
Johannes Berg84fa4f42008-10-24 20:32:23 +02001402/* This has the logic which determines when a new request
1403 * should be ignored. */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001404static int ignore_request(struct wiphy *wiphy,
1405 struct regulatory_request *pending_request)
Johannes Berg84fa4f42008-10-24 20:32:23 +02001406{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001407 struct wiphy *last_wiphy = NULL;
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001408
1409 assert_cfg80211_lock();
1410
Johannes Berg84fa4f42008-10-24 20:32:23 +02001411 /* All initial requests are respected */
1412 if (!last_request)
1413 return 0;
1414
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001415 switch (pending_request->initiator) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001416 case NL80211_REGDOM_SET_BY_CORE:
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001417 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001418 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001419
1420 last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1421
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001422 if (unlikely(!is_an_alpha2(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001423 return -EINVAL;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001424 if (last_request->initiator ==
1425 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001426 if (last_wiphy != wiphy) {
Johannes Berg84fa4f42008-10-24 20:32:23 +02001427 /*
1428 * Two cards with two APs claiming different
1429 * different Country IE alpha2s. We could
1430 * intersect them, but that seems unlikely
1431 * to be correct. Reject second one for now.
1432 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001433 if (regdom_changes(pending_request->alpha2))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001434 return -EOPNOTSUPP;
1435 return -EALREADY;
1436 }
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001437 /*
1438 * Two consecutive Country IE hints on the same wiphy.
1439 * This should be picked up early by the driver/stack
1440 */
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001441 if (WARN_ON(regdom_changes(pending_request->alpha2)))
Johannes Berg84fa4f42008-10-24 20:32:23 +02001442 return 0;
1443 return -EALREADY;
1444 }
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001445 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001446 case NL80211_REGDOM_SET_BY_DRIVER:
1447 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001448 if (is_old_static_regdom(cfg80211_regdomain))
1449 return 0;
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001450 if (regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001451 return 0;
Johannes Berg84fa4f42008-10-24 20:32:23 +02001452 return -EALREADY;
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001453 }
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001454
1455 /*
1456 * This would happen if you unplug and plug your card
1457 * back in or if you add a new device for which the previously
1458 * loaded card also agrees on the regulatory domain.
1459 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001460 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001461 !regdom_changes(pending_request->alpha2))
Luis R. Rodriguezfff32c02009-02-21 00:04:32 -05001462 return -EALREADY;
1463
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001464 return REG_INTERSECT;
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001465 case NL80211_REGDOM_SET_BY_USER:
1466 if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001467 return REG_INTERSECT;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001468 /*
1469 * If the user knows better the user should set the regdom
1470 * to their country before the IE is picked up
1471 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001472 if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001473 last_request->intersect)
1474 return -EOPNOTSUPP;
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001475 /*
1476 * Process user requests only after previous user/driver/core
1477 * requests have been processed
1478 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001479 if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
1480 last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
1481 last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05001482 if (regdom_changes(last_request->alpha2))
Luis R. Rodriguez5eebade2009-01-22 15:05:47 -08001483 return -EAGAIN;
1484 }
1485
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001486 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001487 !regdom_changes(pending_request->alpha2))
Luis R. Rodrigueze74b1e72009-01-22 15:05:48 -08001488 return -EALREADY;
1489
Johannes Berg84fa4f42008-10-24 20:32:23 +02001490 return 0;
1491 }
1492
1493 return -EINVAL;
1494}
1495
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001496/**
1497 * __regulatory_hint - hint to the wireless core a regulatory domain
1498 * @wiphy: if the hint comes from country information from an AP, this
1499 * is required to be set to the wiphy that received the information
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001500 * @pending_request: the regulatory request currently being processed
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001501 *
1502 * The Wireless subsystem can use this function to hint to the wireless core
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001503 * what it believes should be the current regulatory domain.
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001504 *
1505 * Returns zero if all went fine, %-EALREADY if a regulatory domain had
1506 * already been set or other standard error codes.
1507 *
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001508 * Caller must hold &cfg80211_mutex and &reg_mutex
Luis R. Rodriguezd1c96a92009-02-21 00:24:13 -05001509 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001510static int __regulatory_hint(struct wiphy *wiphy,
1511 struct regulatory_request *pending_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001512{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001513 bool intersect = false;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001514 int r = 0;
1515
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001516 assert_cfg80211_lock();
1517
Luis R. Rodriguez2f92cd22009-02-21 00:24:16 -05001518 r = ignore_request(wiphy, pending_request);
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001519
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001520 if (r == REG_INTERSECT) {
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001521 if (pending_request->initiator ==
1522 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001523 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001524 if (r) {
1525 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001526 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001527 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001528 }
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07001529 intersect = true;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001530 } else if (r) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001531 /*
1532 * If the regulatory domain being requested by the
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001533 * driver has already been set just copy it to the
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001534 * wiphy
1535 */
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001536 if (r == -EALREADY &&
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001537 pending_request->initiator ==
1538 NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001539 r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001540 if (r) {
1541 kfree(pending_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001542 return r;
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001543 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001544 r = -EALREADY;
1545 goto new_request;
1546 }
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001547 kfree(pending_request);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001548 return r;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001549 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001550
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001551new_request:
Luis R. Rodriguez5203cdb2008-11-12 14:21:56 -08001552 kfree(last_request);
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001553
1554 last_request = pending_request;
1555 last_request->intersect = intersect;
1556
1557 pending_request = NULL;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001558
1559 /* When r == REG_INTERSECT we do need to call CRDA */
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001560 if (r < 0) {
1561 /*
1562 * Since CRDA will not be called in this case as we already
1563 * have applied the requested regulatory domain before we just
1564 * inform userspace we have processed the request
1565 */
1566 if (r == -EALREADY)
1567 nl80211_send_reg_change_event(last_request);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001568 return r;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04001569 }
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08001570
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001571 return call_crda(last_request->alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001572}
1573
Luis R. Rodriguez30a548c2009-05-02 01:17:27 -04001574/* This processes *all* regulatory hints */
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001575static void reg_process_hint(struct regulatory_request *reg_request)
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001576{
1577 int r = 0;
1578 struct wiphy *wiphy = NULL;
1579
1580 BUG_ON(!reg_request->alpha2);
1581
1582 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001583 mutex_lock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001584
1585 if (wiphy_idx_valid(reg_request->wiphy_idx))
1586 wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
1587
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001588 if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001589 !wiphy) {
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001590 kfree(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001591 goto out;
1592 }
1593
Luis R. Rodriguez28da32d2009-02-21 00:24:14 -05001594 r = __regulatory_hint(wiphy, reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001595 /* This is required so that the orig_* parameters are saved */
1596 if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
1597 wiphy_update_regulatory(wiphy, reg_request->initiator);
1598out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001599 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001600 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001601}
1602
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001603/* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001604static void reg_process_pending_hints(void)
1605 {
1606 struct regulatory_request *reg_request;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001607
1608 spin_lock(&reg_requests_lock);
1609 while (!list_empty(&reg_requests_list)) {
1610 reg_request = list_first_entry(&reg_requests_list,
1611 struct regulatory_request,
1612 list);
1613 list_del_init(&reg_request->list);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001614
Luis R. Rodriguezd951c1d2009-02-21 00:24:15 -05001615 spin_unlock(&reg_requests_lock);
1616 reg_process_hint(reg_request);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001617 spin_lock(&reg_requests_lock);
1618 }
1619 spin_unlock(&reg_requests_lock);
1620}
1621
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001622/* Processes beacon hints -- this has nothing to do with country IEs */
1623static void reg_process_pending_beacon_hints(void)
1624{
Johannes Berg79c97e92009-07-07 03:56:12 +02001625 struct cfg80211_registered_device *rdev;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001626 struct reg_beacon *pending_beacon, *tmp;
1627
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001628 /*
1629 * No need to hold the reg_mutex here as we just touch wiphys
1630 * and do not read or access regulatory variables.
1631 */
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001632 mutex_lock(&cfg80211_mutex);
1633
1634 /* This goes through the _pending_ beacon list */
1635 spin_lock_bh(&reg_pending_beacons_lock);
1636
1637 if (list_empty(&reg_pending_beacons)) {
1638 spin_unlock_bh(&reg_pending_beacons_lock);
1639 goto out;
1640 }
1641
1642 list_for_each_entry_safe(pending_beacon, tmp,
1643 &reg_pending_beacons, list) {
1644
1645 list_del_init(&pending_beacon->list);
1646
1647 /* Applies the beacon hint to current wiphys */
Johannes Berg79c97e92009-07-07 03:56:12 +02001648 list_for_each_entry(rdev, &cfg80211_rdev_list, list)
1649 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001650
1651 /* Remembers the beacon hint for new wiphys or reg changes */
1652 list_add_tail(&pending_beacon->list, &reg_beacon_list);
1653 }
1654
1655 spin_unlock_bh(&reg_pending_beacons_lock);
1656out:
1657 mutex_unlock(&cfg80211_mutex);
1658}
1659
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001660static void reg_todo(struct work_struct *work)
1661{
1662 reg_process_pending_hints();
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001663 reg_process_pending_beacon_hints();
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001664}
1665
1666static DECLARE_WORK(reg_work, reg_todo);
1667
1668static void queue_regulatory_request(struct regulatory_request *request)
1669{
1670 spin_lock(&reg_requests_lock);
1671 list_add_tail(&request->list, &reg_requests_list);
1672 spin_unlock(&reg_requests_lock);
1673
1674 schedule_work(&reg_work);
1675}
1676
1677/* Core regulatory hint -- happens once during cfg80211_init() */
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001678static int regulatory_hint_core(const char *alpha2)
1679{
1680 struct regulatory_request *request;
1681
1682 BUG_ON(last_request);
1683
1684 request = kzalloc(sizeof(struct regulatory_request),
1685 GFP_KERNEL);
1686 if (!request)
1687 return -ENOMEM;
1688
1689 request->alpha2[0] = alpha2[0];
1690 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001691 request->initiator = NL80211_REGDOM_SET_BY_CORE;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001692
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001693 queue_regulatory_request(request);
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001694
Luis R. Rodriguez5078b2e2009-05-13 17:04:42 -04001695 /*
1696 * This ensures last_request is populated once modules
1697 * come swinging in and calling regulatory hints and
1698 * wiphy_apply_custom_regulatory().
1699 */
1700 flush_scheduled_work();
1701
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001702 return 0;
Luis R. Rodriguezba25c142009-02-21 00:04:23 -05001703}
1704
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001705/* User hints */
1706int regulatory_hint_user(const char *alpha2)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001707{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001708 struct regulatory_request *request;
1709
Johannes Bergbe3d4812008-10-24 20:32:21 +02001710 BUG_ON(!alpha2);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001711
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001712 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1713 if (!request)
1714 return -ENOMEM;
1715
1716 request->wiphy_idx = WIPHY_IDX_STALE;
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_USER,
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001720
1721 queue_regulatory_request(request);
1722
1723 return 0;
1724}
1725
1726/* Driver hints */
1727int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1728{
1729 struct regulatory_request *request;
1730
1731 BUG_ON(!alpha2);
1732 BUG_ON(!wiphy);
1733
1734 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1735 if (!request)
1736 return -ENOMEM;
1737
1738 request->wiphy_idx = get_wiphy_idx(wiphy);
1739
1740 /* Must have registered wiphy first */
1741 BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
1742
1743 request->alpha2[0] = alpha2[0];
1744 request->alpha2[1] = alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001745 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001746
1747 queue_regulatory_request(request);
1748
1749 return 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001750}
1751EXPORT_SYMBOL(regulatory_hint);
1752
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001753/* Caller must hold reg_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001754static bool reg_same_country_ie_hint(struct wiphy *wiphy,
1755 u32 country_ie_checksum)
1756{
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001757 struct wiphy *request_wiphy;
1758
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001759 assert_reg_lock();
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05001760
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001761 if (unlikely(last_request->initiator !=
1762 NL80211_REGDOM_SET_BY_COUNTRY_IE))
1763 return false;
1764
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001765 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
1766
1767 if (!request_wiphy)
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001768 return false;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001769
1770 if (likely(request_wiphy != wiphy))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001771 return !country_ie_integrity_changes(country_ie_checksum);
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001772 /*
1773 * We should not have let these through at this point, they
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001774 * should have been picked up earlier by the first alpha2 check
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001775 * on the device
1776 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001777 if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
1778 return true;
1779 return false;
1780}
1781
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001782/*
1783 * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
1784 * therefore cannot iterate over the rdev list here.
1785 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001786void regulatory_hint_11d(struct wiphy *wiphy,
1787 u8 *country_ie,
1788 u8 country_ie_len)
1789{
1790 struct ieee80211_regdomain *rd = NULL;
1791 char alpha2[2];
1792 u32 checksum = 0;
1793 enum environment_cap env = ENVIRON_ANY;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001794 struct regulatory_request *request;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001795
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001796 mutex_lock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001797
Luis R. Rodriguez9828b012009-07-30 17:38:06 -07001798 if (unlikely(!last_request))
1799 goto out;
Luis R. Rodriguezd335fe62009-02-21 00:04:27 -05001800
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001801 /* IE len must be evenly divisible by 2 */
1802 if (country_ie_len & 0x01)
1803 goto out;
1804
1805 if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1806 goto out;
1807
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001808 /*
1809 * Pending country IE processing, this can happen after we
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001810 * call CRDA and wait for a response if a beacon was received before
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001811 * we were able to process the last regulatory_hint_11d() call
1812 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001813 if (country_ie_regdomain)
1814 goto out;
1815
1816 alpha2[0] = country_ie[0];
1817 alpha2[1] = country_ie[1];
1818
1819 if (country_ie[2] == 'I')
1820 env = ENVIRON_INDOOR;
1821 else if (country_ie[2] == 'O')
1822 env = ENVIRON_OUTDOOR;
1823
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001824 /*
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07001825 * We will run this only upon a successful connection on cfg80211.
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001826 * We leave conflict resolution to the workqueue, where can hold
1827 * cfg80211_mutex.
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001828 */
Luis R. Rodriguezcc0b6fe2009-03-20 23:53:05 -04001829 if (likely(last_request->initiator ==
1830 NL80211_REGDOM_SET_BY_COUNTRY_IE &&
Luis R. Rodriguez4b44c8b2009-07-30 17:38:07 -07001831 wiphy_idx_valid(last_request->wiphy_idx)))
1832 goto out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001833
1834 rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
1835 if (!rd)
1836 goto out;
1837
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001838 /*
1839 * This will not happen right now but we leave it here for the
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001840 * the future when we want to add suspend/resume support and having
1841 * the user move to another country after doing so, or having the user
Luis R. Rodriguez915278e2009-02-21 00:04:28 -05001842 * move to another AP. Right now we just trust the first AP.
1843 *
1844 * If we hit this before we add this support we want to be informed of
1845 * it as it would indicate a mistake in the current design
1846 */
1847 if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001848 goto free_rd_out;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001849
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001850 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
1851 if (!request)
1852 goto free_rd_out;
1853
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001854 /*
1855 * We keep this around for when CRDA comes back with a response so
1856 * we can intersect with that
1857 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001858 country_ie_regdomain = rd;
1859
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001860 request->wiphy_idx = get_wiphy_idx(wiphy);
1861 request->alpha2[0] = rd->alpha2[0];
1862 request->alpha2[1] = rd->alpha2[1];
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001863 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001864 request->country_ie_checksum = checksum;
1865 request->country_ie_env = env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001866
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001867 mutex_unlock(&reg_mutex);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05001868
1869 queue_regulatory_request(request);
1870
1871 return;
Luis R. Rodriguez0441d6f2009-02-21 00:04:29 -05001872
1873free_rd_out:
1874 kfree(rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001875out:
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07001876 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001877}
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001878
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001879static bool freq_is_chan_12_13_14(u16 freq)
1880{
1881 if (freq == ieee80211_channel_to_frequency(12) ||
1882 freq == ieee80211_channel_to_frequency(13) ||
1883 freq == ieee80211_channel_to_frequency(14))
1884 return true;
1885 return false;
1886}
1887
1888int regulatory_hint_found_beacon(struct wiphy *wiphy,
1889 struct ieee80211_channel *beacon_chan,
1890 gfp_t gfp)
1891{
1892 struct reg_beacon *reg_beacon;
1893
1894 if (likely((beacon_chan->beacon_found ||
1895 (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
1896 (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1897 !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
1898 return 0;
1899
1900 reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
1901 if (!reg_beacon)
1902 return -ENOMEM;
1903
1904#ifdef CONFIG_CFG80211_REG_DEBUG
1905 printk(KERN_DEBUG "cfg80211: Found new beacon on "
1906 "frequency: %d MHz (Ch %d) on %s\n",
1907 beacon_chan->center_freq,
1908 ieee80211_frequency_to_channel(beacon_chan->center_freq),
1909 wiphy_name(wiphy));
1910#endif
1911 memcpy(&reg_beacon->chan, beacon_chan,
1912 sizeof(struct ieee80211_channel));
1913
1914
1915 /*
1916 * Since we can be called from BH or and non-BH context
1917 * we must use spin_lock_bh()
1918 */
1919 spin_lock_bh(&reg_pending_beacons_lock);
1920 list_add_tail(&reg_beacon->list, &reg_pending_beacons);
1921 spin_unlock_bh(&reg_pending_beacons_lock);
1922
1923 schedule_work(&reg_work);
1924
1925 return 0;
1926}
1927
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001928static void print_rd_rules(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001929{
1930 unsigned int i;
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001931 const struct ieee80211_reg_rule *reg_rule = NULL;
1932 const struct ieee80211_freq_range *freq_range = NULL;
1933 const struct ieee80211_power_rule *power_rule = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001934
1935 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
1936 "(max_antenna_gain, max_eirp)\n");
1937
1938 for (i = 0; i < rd->n_reg_rules; i++) {
1939 reg_rule = &rd->reg_rules[i];
1940 freq_range = &reg_rule->freq_range;
1941 power_rule = &reg_rule->power_rule;
1942
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05001943 /*
1944 * There may not be documentation for max antenna gain
1945 * in certain regions
1946 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001947 if (power_rule->max_antenna_gain)
1948 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1949 "(%d mBi, %d mBm)\n",
1950 freq_range->start_freq_khz,
1951 freq_range->end_freq_khz,
1952 freq_range->max_bandwidth_khz,
1953 power_rule->max_antenna_gain,
1954 power_rule->max_eirp);
1955 else
1956 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
1957 "(N/A, %d mBm)\n",
1958 freq_range->start_freq_khz,
1959 freq_range->end_freq_khz,
1960 freq_range->max_bandwidth_khz,
1961 power_rule->max_eirp);
1962 }
1963}
1964
Johannes Berga3d2eaf2008-09-15 11:10:52 +02001965static void print_regdomain(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001966{
1967
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001968 if (is_intersected_alpha2(rd->alpha2)) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001969
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04001970 if (last_request->initiator ==
1971 NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001972 struct cfg80211_registered_device *rdev;
1973 rdev = cfg80211_rdev_by_wiphy_idx(
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05001974 last_request->wiphy_idx);
Johannes Berg79c97e92009-07-07 03:56:12 +02001975 if (rdev) {
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001976 printk(KERN_INFO "cfg80211: Current regulatory "
1977 "domain updated by AP to: %c%c\n",
Johannes Berg79c97e92009-07-07 03:56:12 +02001978 rdev->country_ie_alpha2[0],
1979 rdev->country_ie_alpha2[1]);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001980 } else
1981 printk(KERN_INFO "cfg80211: Current regulatory "
1982 "domain intersected: \n");
1983 } else
1984 printk(KERN_INFO "cfg80211: Current regulatory "
Luis R. Rodriguez039498c2009-01-07 17:43:35 -08001985 "domain intersected: \n");
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001986 } else if (is_world_regdom(rd->alpha2))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001987 printk(KERN_INFO "cfg80211: World regulatory "
1988 "domain updated:\n");
1989 else {
1990 if (is_unknown_alpha2(rd->alpha2))
1991 printk(KERN_INFO "cfg80211: Regulatory domain "
1992 "changed to driver built-in settings "
1993 "(unknown country)\n");
1994 else
1995 printk(KERN_INFO "cfg80211: Regulatory domain "
1996 "changed to country: %c%c\n",
1997 rd->alpha2[0], rd->alpha2[1]);
1998 }
1999 print_rd_rules(rd);
2000}
2001
Johannes Berg2df78162008-10-28 16:49:41 +01002002static void print_regdomain_info(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002003{
2004 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
2005 rd->alpha2[0], rd->alpha2[1]);
2006 print_rd_rules(rd);
2007}
2008
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002009#ifdef CONFIG_CFG80211_REG_DEBUG
2010static void reg_country_ie_process_debug(
2011 const struct ieee80211_regdomain *rd,
2012 const struct ieee80211_regdomain *country_ie_regdomain,
2013 const struct ieee80211_regdomain *intersected_rd)
2014{
2015 printk(KERN_DEBUG "cfg80211: Received country IE:\n");
2016 print_regdomain_info(country_ie_regdomain);
2017 printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
2018 print_regdomain_info(rd);
2019 if (intersected_rd) {
2020 printk(KERN_DEBUG "cfg80211: We intersect both of these "
2021 "and get:\n");
Luis R. Rodriguez667ecd02009-01-22 15:05:43 -08002022 print_regdomain_info(intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002023 return;
2024 }
2025 printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
2026}
2027#else
2028static inline void reg_country_ie_process_debug(
2029 const struct ieee80211_regdomain *rd,
2030 const struct ieee80211_regdomain *country_ie_regdomain,
2031 const struct ieee80211_regdomain *intersected_rd)
2032{
2033}
2034#endif
2035
Johannes Bergd2372b32008-10-24 20:32:20 +02002036/* Takes ownership of rd only if it doesn't fail */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002037static int __set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002038{
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002039 const struct ieee80211_regdomain *intersected_rd = NULL;
Johannes Berg79c97e92009-07-07 03:56:12 +02002040 struct cfg80211_registered_device *rdev = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002041 struct wiphy *request_wiphy;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002042 /* Some basic sanity checks first */
2043
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002044 if (is_world_regdom(rd->alpha2)) {
Johannes Bergf6037d02008-10-21 11:01:33 +02002045 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002046 return -EINVAL;
2047 update_world_regdomain(rd);
2048 return 0;
2049 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002050
2051 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
2052 !is_unknown_alpha2(rd->alpha2))
2053 return -EINVAL;
2054
Johannes Bergf6037d02008-10-21 11:01:33 +02002055 if (!last_request)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002056 return -EINVAL;
2057
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002058 /*
2059 * Lets only bother proceeding on the same alpha2 if the current
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002060 * rd is non static (it means CRDA was present and was used last)
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002061 * and the pending request came in from a country IE
2062 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002063 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002064 /*
2065 * If someone else asked us to change the rd lets only bother
2066 * checking if the alpha2 changes if CRDA was already called
2067 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002068 if (!is_old_static_regdom(cfg80211_regdomain) &&
Luis R. Rodriguez69b15722009-02-21 00:04:33 -05002069 !regdom_changes(rd->alpha2))
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002070 return -EINVAL;
2071 }
2072
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002073 /*
2074 * Now lets set the regulatory domain, update all driver channels
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002075 * and finally inform them of what we have done, in case they want
2076 * to review or adjust their own settings based on their own
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002077 * internal EEPROM data
2078 */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002079
Johannes Bergf6037d02008-10-21 11:01:33 +02002080 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002081 return -EINVAL;
2082
Luis R. Rodriguez8375af32008-11-12 14:21:57 -08002083 if (!is_valid_rd(rd)) {
2084 printk(KERN_ERR "cfg80211: Invalid "
2085 "regulatory domain detected:\n");
2086 print_regdomain_info(rd);
2087 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002088 }
2089
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002090 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
2091
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002092 if (!last_request->intersect) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002093 int r;
2094
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002095 if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002096 reset_regdomains();
2097 cfg80211_regdomain = rd;
2098 return 0;
2099 }
2100
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002101 /*
2102 * For a driver hint, lets copy the regulatory domain the
2103 * driver wanted to the wiphy to deal with conflicts
2104 */
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002105
Luis R. Rodriguez558f6d32009-06-08 18:54:37 -07002106 /*
2107 * Userspace could have sent two replies with only
2108 * one kernel request.
2109 */
2110 if (request_wiphy->regd)
2111 return -EALREADY;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002112
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002113 r = reg_copy_regd(&request_wiphy->regd, rd);
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002114 if (r)
2115 return r;
2116
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002117 reset_regdomains();
2118 cfg80211_regdomain = rd;
2119 return 0;
2120 }
2121
2122 /* Intersection requires a bit more work */
2123
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002124 if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002125
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002126 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2127 if (!intersected_rd)
2128 return -EINVAL;
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002129
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002130 /*
2131 * We can trash what CRDA provided now.
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002132 * However if a driver requested this specific regulatory
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002133 * domain we keep it for its private use
2134 */
Luis R. Rodriguez7db90f42009-03-09 22:07:41 -04002135 if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002136 request_wiphy->regd = rd;
Luis R. Rodriguez3e0c3ff2009-01-07 17:43:34 -08002137 else
2138 kfree(rd);
2139
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002140 rd = NULL;
2141
2142 reset_regdomains();
2143 cfg80211_regdomain = intersected_rd;
2144
2145 return 0;
Luis R. Rodriguez9c964772008-10-30 13:33:53 -07002146 }
2147
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002148 /*
2149 * Country IE requests are handled a bit differently, we intersect
2150 * the country IE rd with what CRDA believes that country should have
2151 */
2152
Luis R. Rodriguez729e9c72009-05-31 18:24:34 -04002153 /*
2154 * Userspace could have sent two replies with only
2155 * one kernel request. By the second reply we would have
2156 * already processed and consumed the country_ie_regdomain.
2157 */
2158 if (!country_ie_regdomain)
2159 return -EALREADY;
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002160 BUG_ON(rd == country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002161
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002162 /*
2163 * Intersect what CRDA returned and our what we
2164 * had built from the Country IE received
2165 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002166
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002167 intersected_rd = regdom_intersect(rd, country_ie_regdomain);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002168
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002169 reg_country_ie_process_debug(rd,
2170 country_ie_regdomain,
2171 intersected_rd);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002172
Luis R. Rodriguez86f04682009-03-20 23:53:07 -04002173 kfree(country_ie_regdomain);
2174 country_ie_regdomain = NULL;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002175
2176 if (!intersected_rd)
2177 return -EINVAL;
2178
Johannes Berg79c97e92009-07-07 03:56:12 +02002179 rdev = wiphy_to_dev(request_wiphy);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002180
Johannes Berg79c97e92009-07-07 03:56:12 +02002181 rdev->country_ie_alpha2[0] = rd->alpha2[0];
2182 rdev->country_ie_alpha2[1] = rd->alpha2[1];
2183 rdev->env = last_request->country_ie_env;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002184
2185 BUG_ON(intersected_rd == rd);
2186
2187 kfree(rd);
2188 rd = NULL;
2189
Luis R. Rodriguezb8295ac2008-11-12 14:21:58 -08002190 reset_regdomains();
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002191 cfg80211_regdomain = intersected_rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002192
2193 return 0;
2194}
2195
2196
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002197/*
2198 * Use this call to set the current regulatory domain. Conflicts with
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002199 * multiple drivers can be ironed out later. Caller must've already
Luis R. Rodriguezfb1fc7a2009-02-21 00:04:31 -05002200 * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
2201 */
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002202int set_regdom(const struct ieee80211_regdomain *rd)
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002203{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002204 int r;
2205
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002206 assert_cfg80211_lock();
2207
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002208 mutex_lock(&reg_mutex);
2209
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002210 /* Note that this doesn't update the wiphys, this is done below */
2211 r = __set_regdom(rd);
Johannes Bergd2372b32008-10-24 20:32:20 +02002212 if (r) {
2213 kfree(rd);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002214 mutex_unlock(&reg_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002215 return r;
Johannes Bergd2372b32008-10-24 20:32:20 +02002216 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002217
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002218 /* This would make this whole thing pointless */
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002219 if (!last_request->intersect)
2220 BUG_ON(rd != cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002221
2222 /* update all wiphys now with the new established regulatory domain */
Johannes Bergf6037d02008-10-21 11:01:33 +02002223 update_all_wiphy_regulatory(last_request->initiator);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002224
Luis R. Rodrigueza01ddaf2008-11-12 14:21:59 -08002225 print_regdomain(cfg80211_regdomain);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002226
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04002227 nl80211_send_reg_change_event(last_request);
2228
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002229 mutex_unlock(&reg_mutex);
2230
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002231 return r;
2232}
2233
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002234/* Caller must hold cfg80211_mutex */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002235void reg_device_remove(struct wiphy *wiphy)
2236{
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002237 struct wiphy *request_wiphy = NULL;
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002238
Luis R. Rodriguez761cf7e2009-02-21 00:04:25 -05002239 assert_cfg80211_lock();
2240
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002241 mutex_lock(&reg_mutex);
2242
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002243 kfree(wiphy->regd);
2244
Luis R. Rodriguez0ad8aca2009-03-24 21:21:08 -04002245 if (last_request)
2246 request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002247
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002248 if (!request_wiphy || request_wiphy != wiphy)
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002249 goto out;
Chris Wright0ef9ccd2009-04-24 14:09:31 -07002250
Luis R. Rodriguez806a9e32009-02-21 00:04:26 -05002251 last_request->wiphy_idx = WIPHY_IDX_STALE;
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002252 last_request->country_ie_env = ENVIRON_ANY;
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002253out:
2254 mutex_unlock(&reg_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002255}
2256
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002257int regulatory_init(void)
2258{
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002259 int err = 0;
Johannes Berg734366d2008-09-15 10:56:48 +02002260
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002261 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
2262 if (IS_ERR(reg_pdev))
2263 return PTR_ERR(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002264
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002265 spin_lock_init(&reg_requests_lock);
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002266 spin_lock_init(&reg_pending_beacons_lock);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002267
Johannes Berg734366d2008-09-15 10:56:48 +02002268#ifdef CONFIG_WIRELESS_OLD_REGULATORY
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002269 cfg80211_regdomain = static_regdom(ieee80211_regdom);
Johannes Berg734366d2008-09-15 10:56:48 +02002270
Johannes Berg942b25c2008-09-15 11:26:47 +02002271 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
Johannes Berg734366d2008-09-15 10:56:48 +02002272 print_regdomain_info(cfg80211_regdomain);
Johannes Berg734366d2008-09-15 10:56:48 +02002273#else
Johannes Berga3d2eaf2008-09-15 11:10:52 +02002274 cfg80211_regdomain = cfg80211_world_regdom;
Johannes Berg734366d2008-09-15 10:56:48 +02002275
Johannes Berg734366d2008-09-15 10:56:48 +02002276#endif
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002277 /* We always try to get an update for the static regdomain */
2278 err = regulatory_hint_core(cfg80211_regdomain->alpha2);
Luis R. Rodriguezbcf4f992009-02-21 00:04:24 -05002279 if (err) {
2280 if (err == -ENOMEM)
2281 return err;
2282 /*
2283 * N.B. kobject_uevent_env() can fail mainly for when we're out
2284 * memory which is handled and propagated appropriately above
2285 * but it can also fail during a netlink_broadcast() or during
2286 * early boot for call_usermodehelper(). For now treat these
2287 * errors as non-fatal.
2288 */
2289 printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
2290 "to call CRDA during init");
2291#ifdef CONFIG_CFG80211_REG_DEBUG
2292 /* We want to find out exactly why when debugging */
2293 WARN_ON(err);
2294#endif
2295 }
Johannes Berg734366d2008-09-15 10:56:48 +02002296
Luis R. Rodriguezae9e4b02009-07-14 20:23:15 -04002297 /*
2298 * Finally, if the user set the module parameter treat it
2299 * as a user hint.
2300 */
2301 if (!is_world_regdom(ieee80211_regdom))
2302 regulatory_hint_user(ieee80211_regdom);
2303
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002304 return 0;
2305}
2306
2307void regulatory_exit(void)
2308{
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002309 struct regulatory_request *reg_request, *tmp;
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002310 struct reg_beacon *reg_beacon, *btmp;
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002311
2312 cancel_work_sync(&reg_work);
2313
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002314 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002315 mutex_lock(&reg_mutex);
Johannes Berg734366d2008-09-15 10:56:48 +02002316
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002317 reset_regdomains();
Johannes Berg734366d2008-09-15 10:56:48 +02002318
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002319 kfree(country_ie_regdomain);
2320 country_ie_regdomain = NULL;
2321
Johannes Bergf6037d02008-10-21 11:01:33 +02002322 kfree(last_request);
2323
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002324 platform_device_unregister(reg_pdev);
Johannes Berg734366d2008-09-15 10:56:48 +02002325
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05002326 spin_lock_bh(&reg_pending_beacons_lock);
2327 if (!list_empty(&reg_pending_beacons)) {
2328 list_for_each_entry_safe(reg_beacon, btmp,
2329 &reg_pending_beacons, list) {
2330 list_del(&reg_beacon->list);
2331 kfree(reg_beacon);
2332 }
2333 }
2334 spin_unlock_bh(&reg_pending_beacons_lock);
2335
2336 if (!list_empty(&reg_beacon_list)) {
2337 list_for_each_entry_safe(reg_beacon, btmp,
2338 &reg_beacon_list, list) {
2339 list_del(&reg_beacon->list);
2340 kfree(reg_beacon);
2341 }
2342 }
2343
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002344 spin_lock(&reg_requests_lock);
2345 if (!list_empty(&reg_requests_list)) {
2346 list_for_each_entry_safe(reg_request, tmp,
2347 &reg_requests_list, list) {
2348 list_del(&reg_request->list);
2349 kfree(reg_request);
2350 }
2351 }
2352 spin_unlock(&reg_requests_lock);
2353
Luis R. Rodriguezabc73812009-07-30 17:38:08 -07002354 mutex_unlock(&reg_mutex);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002355 mutex_unlock(&cfg80211_mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002356}