Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002-2005, Instant802 Networks, Inc. |
| 3 | * Copyright 2005-2006, Devicescape Software, Inc. |
| 4 | * Copyright 2007 Johannes Berg <johannes@sipsolutions.net> |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 5 | * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com> |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 6 | * |
| 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. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 12 | /** |
| 13 | * DOC: Wireless regulatory infrastructure |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 14 | * |
| 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. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 20 | * 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 Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 34 | */ |
| 35 | #include <linux/kernel.h> |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 36 | #include <linux/list.h> |
| 37 | #include <linux/random.h> |
| 38 | #include <linux/nl80211.h> |
| 39 | #include <linux/platform_device.h> |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 40 | #include <net/cfg80211.h> |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 41 | #include "core.h" |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 42 | #include "reg.h" |
Luis R. Rodriguez | 73d54c9 | 2009-03-09 22:07:42 -0400 | [diff] [blame] | 43 | #include "nl80211.h" |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 44 | |
Luis R. Rodriguez | 5166ccd | 2008-10-30 13:33:56 -0700 | [diff] [blame] | 45 | /* Receipt of information from last regulatory request */ |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 46 | static struct regulatory_request *last_request; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 47 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 48 | /* To trigger userspace events */ |
| 49 | static struct platform_device *reg_pdev; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 50 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 51 | /* |
| 52 | * Central wireless core regulatory domains, we only need two, |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 53 | * the current one and a world regulatory domain in case we have no |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 54 | * information to give us an alpha2 |
| 55 | */ |
Luis R. Rodriguez | f130347 | 2009-01-30 09:26:42 -0800 | [diff] [blame] | 56 | const struct ieee80211_regdomain *cfg80211_regdomain; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 57 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 58 | /* |
| 59 | * We use this as a place for the rd structure built from the |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 60 | * last parsed country IE to rest until CRDA gets back to us with |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 61 | * what it thinks should apply for the same country |
| 62 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 63 | static const struct ieee80211_regdomain *country_ie_regdomain; |
| 64 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Protects static reg.c components: |
| 67 | * - cfg80211_world_regdom |
| 68 | * - cfg80211_regdom |
| 69 | * - country_ie_regdomain |
| 70 | * - last_request |
| 71 | */ |
| 72 | DEFINE_MUTEX(reg_mutex); |
| 73 | #define assert_reg_lock() WARN_ON(!mutex_is_locked(®_mutex)) |
| 74 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 75 | /* Used to queue up regulatory hints */ |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 76 | static LIST_HEAD(reg_requests_list); |
| 77 | static spinlock_t reg_requests_lock; |
| 78 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 79 | /* Used to queue up beacon hints for review */ |
| 80 | static LIST_HEAD(reg_pending_beacons); |
| 81 | static spinlock_t reg_pending_beacons_lock; |
| 82 | |
| 83 | /* Used to keep track of processed beacon hints */ |
| 84 | static LIST_HEAD(reg_beacon_list); |
| 85 | |
| 86 | struct reg_beacon { |
| 87 | struct list_head list; |
| 88 | struct ieee80211_channel chan; |
| 89 | }; |
| 90 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 91 | /* We keep a static world regulatory domain in case of the absence of CRDA */ |
| 92 | static const struct ieee80211_regdomain world_regdom = { |
Luis R. Rodriguez | 611b6a8 | 2009-03-05 21:19:21 -0800 | [diff] [blame] | 93 | .n_reg_rules = 5, |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 94 | .alpha2 = "00", |
| 95 | .reg_rules = { |
Luis R. Rodriguez | 68798a6 | 2009-02-21 00:20:37 -0500 | [diff] [blame] | 96 | /* IEEE 802.11b/g, channels 1..11 */ |
| 97 | REG_RULE(2412-10, 2462+10, 40, 6, 20, 0), |
Luis R. Rodriguez | 611b6a8 | 2009-03-05 21:19:21 -0800 | [diff] [blame] | 98 | /* 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. Rodriguez | 3fc71f7 | 2009-02-21 00:20:38 -0500 | [diff] [blame] | 101 | NL80211_RRF_PASSIVE_SCAN | |
| 102 | NL80211_RRF_NO_IBSS), |
Luis R. Rodriguez | 611b6a8 | 2009-03-05 21:19:21 -0800 | [diff] [blame] | 103 | /* 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. Rodriguez | ec329ac | 2009-03-05 21:19:22 -0800 | [diff] [blame] | 110 | REG_RULE(5180-10, 5240+10, 40, 6, 20, |
Luis R. Rodriguez | 611b6a8 | 2009-03-05 21:19:21 -0800 | [diff] [blame] | 111 | NL80211_RRF_PASSIVE_SCAN | |
| 112 | NL80211_RRF_NO_IBSS), |
Luis R. Rodriguez | 3fc71f7 | 2009-02-21 00:20:38 -0500 | [diff] [blame] | 113 | |
| 114 | /* NB: 5260 MHz - 5700 MHz requies DFS */ |
| 115 | |
| 116 | /* IEEE 802.11a, channel 149..165 */ |
Luis R. Rodriguez | ec329ac | 2009-03-05 21:19:22 -0800 | [diff] [blame] | 117 | REG_RULE(5745-10, 5825+10, 40, 6, 20, |
Luis R. Rodriguez | 3fc71f7 | 2009-02-21 00:20:38 -0500 | [diff] [blame] | 118 | NL80211_RRF_PASSIVE_SCAN | |
| 119 | NL80211_RRF_NO_IBSS), |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 120 | } |
| 121 | }; |
| 122 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 123 | static const struct ieee80211_regdomain *cfg80211_world_regdom = |
| 124 | &world_regdom; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 125 | |
Luis R. Rodriguez | 6ee7d33 | 2009-03-20 23:53:06 -0400 | [diff] [blame] | 126 | static char *ieee80211_regdom = "00"; |
Luis R. Rodriguez | 6ee7d33 | 2009-03-20 23:53:06 -0400 | [diff] [blame] | 127 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 128 | module_param(ieee80211_regdom, charp, 0444); |
| 129 | MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code"); |
| 130 | |
Luis R. Rodriguez | 6ee7d33 | 2009-03-20 23:53:06 -0400 | [diff] [blame] | 131 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 132 | /* |
| 133 | * We assume 40 MHz bandwidth for the old regulatory work. |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 134 | * We make emphasis we are using the exact same frequencies |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 135 | * as before |
| 136 | */ |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 137 | |
| 138 | static const struct ieee80211_regdomain us_regdom = { |
| 139 | .n_reg_rules = 6, |
| 140 | .alpha2 = "US", |
| 141 | .reg_rules = { |
| 142 | /* IEEE 802.11b/g, channels 1..11 */ |
| 143 | REG_RULE(2412-10, 2462+10, 40, 6, 27, 0), |
John W. Linville | 65182b9 | 2009-12-09 15:11:22 -0500 | [diff] [blame] | 144 | /* IEEE 802.11a, channel 36..48 */ |
| 145 | REG_RULE(5180-10, 5240+10, 40, 6, 17, 0), |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 146 | /* IEEE 802.11a, channels 48..64 */ |
John W. Linville | 65182b9 | 2009-12-09 15:11:22 -0500 | [diff] [blame] | 147 | REG_RULE(5260-10, 5320+10, 40, 6, 20, NL80211_RRF_DFS), |
| 148 | /* IEEE 802.11a, channels 100..124 */ |
| 149 | REG_RULE(5500-10, 5590+10, 40, 6, 20, NL80211_RRF_DFS), |
| 150 | /* IEEE 802.11a, channels 132..144 */ |
| 151 | REG_RULE(5660-10, 5700+10, 40, 6, 20, NL80211_RRF_DFS), |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 152 | /* IEEE 802.11a, channels 149..165, outdoor */ |
| 153 | REG_RULE(5745-10, 5825+10, 40, 6, 30, 0), |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | static const struct ieee80211_regdomain jp_regdom = { |
John W. Linville | 65182b9 | 2009-12-09 15:11:22 -0500 | [diff] [blame] | 158 | .n_reg_rules = 6, |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 159 | .alpha2 = "JP", |
| 160 | .reg_rules = { |
John W. Linville | 65182b9 | 2009-12-09 15:11:22 -0500 | [diff] [blame] | 161 | /* IEEE 802.11b/g, channels 1..11 */ |
| 162 | REG_RULE(2412-10, 2462+10, 40, 6, 20, 0), |
| 163 | /* IEEE 802.11b/g, channels 12..13 */ |
| 164 | REG_RULE(2467-10, 2472+10, 20, 6, 20, 0), |
| 165 | /* IEEE 802.11b/g, channel 14 */ |
| 166 | REG_RULE(2484-10, 2484+10, 20, 6, 20, NL80211_RRF_NO_OFDM), |
| 167 | /* IEEE 802.11a, channels 36..48 */ |
| 168 | REG_RULE(5180-10, 5240+10, 40, 6, 20, 0), |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 169 | /* IEEE 802.11a, channels 52..64 */ |
John W. Linville | 65182b9 | 2009-12-09 15:11:22 -0500 | [diff] [blame] | 170 | REG_RULE(5260-10, 5320+10, 40, 6, 20, NL80211_RRF_DFS), |
| 171 | /* IEEE 802.11a, channels 100..144 */ |
| 172 | REG_RULE(5500-10, 5700+10, 40, 6, 23, NL80211_RRF_DFS), |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 173 | } |
| 174 | }; |
| 175 | |
| 176 | static const struct ieee80211_regdomain *static_regdom(char *alpha2) |
| 177 | { |
| 178 | if (alpha2[0] == 'U' && alpha2[1] == 'S') |
| 179 | return &us_regdom; |
| 180 | if (alpha2[0] == 'J' && alpha2[1] == 'P') |
| 181 | return &jp_regdom; |
John W. Linville | 65182b9 | 2009-12-09 15:11:22 -0500 | [diff] [blame] | 182 | /* Use world roaming rules for "EU", since it was a pseudo |
| 183 | domain anyway... */ |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 184 | if (alpha2[0] == 'E' && alpha2[1] == 'U') |
John W. Linville | 65182b9 | 2009-12-09 15:11:22 -0500 | [diff] [blame] | 185 | return &world_regdom; |
| 186 | /* Default, world roaming rules */ |
| 187 | return &world_regdom; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 190 | static bool is_old_static_regdom(const struct ieee80211_regdomain *rd) |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 191 | { |
John W. Linville | 65182b9 | 2009-12-09 15:11:22 -0500 | [diff] [blame] | 192 | if (rd == &us_regdom || rd == &jp_regdom || rd == &world_regdom) |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 193 | return true; |
| 194 | return false; |
| 195 | } |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 196 | #else |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 197 | static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd) |
| 198 | { |
| 199 | return false; |
| 200 | } |
| 201 | #endif |
| 202 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 203 | static void reset_regdomains(void) |
| 204 | { |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 205 | /* avoid freeing static information or freeing something twice */ |
| 206 | if (cfg80211_regdomain == cfg80211_world_regdom) |
| 207 | cfg80211_regdomain = NULL; |
| 208 | if (cfg80211_world_regdom == &world_regdom) |
| 209 | cfg80211_world_regdom = NULL; |
| 210 | if (cfg80211_regdomain == &world_regdom) |
| 211 | cfg80211_regdomain = NULL; |
| 212 | if (is_old_static_regdom(cfg80211_regdomain)) |
| 213 | cfg80211_regdomain = NULL; |
| 214 | |
| 215 | kfree(cfg80211_regdomain); |
| 216 | kfree(cfg80211_world_regdom); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 217 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 218 | cfg80211_world_regdom = &world_regdom; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 219 | cfg80211_regdomain = NULL; |
| 220 | } |
| 221 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 222 | /* |
| 223 | * Dynamic world regulatory domain requested by the wireless |
| 224 | * core upon initialization |
| 225 | */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 226 | static void update_world_regdomain(const struct ieee80211_regdomain *rd) |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 227 | { |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 228 | BUG_ON(!last_request); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 229 | |
| 230 | reset_regdomains(); |
| 231 | |
| 232 | cfg80211_world_regdom = rd; |
| 233 | cfg80211_regdomain = rd; |
| 234 | } |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 235 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 236 | bool is_world_regdom(const char *alpha2) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 237 | { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 238 | if (!alpha2) |
| 239 | return false; |
| 240 | if (alpha2[0] == '0' && alpha2[1] == '0') |
| 241 | return true; |
| 242 | return false; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 245 | static bool is_alpha2_set(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 246 | { |
| 247 | if (!alpha2) |
| 248 | return false; |
| 249 | if (alpha2[0] != 0 && alpha2[1] != 0) |
| 250 | return true; |
| 251 | return false; |
| 252 | } |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 253 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 254 | static bool is_alpha_upper(char letter) |
| 255 | { |
| 256 | /* ASCII A - Z */ |
| 257 | if (letter >= 65 && letter <= 90) |
| 258 | return true; |
| 259 | return false; |
| 260 | } |
| 261 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 262 | static bool is_unknown_alpha2(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 263 | { |
| 264 | if (!alpha2) |
| 265 | return false; |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 266 | /* |
| 267 | * Special case where regulatory domain was built by driver |
| 268 | * but a specific alpha2 cannot be determined |
| 269 | */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 270 | if (alpha2[0] == '9' && alpha2[1] == '9') |
| 271 | return true; |
| 272 | return false; |
| 273 | } |
| 274 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 275 | static bool is_intersected_alpha2(const char *alpha2) |
| 276 | { |
| 277 | if (!alpha2) |
| 278 | return false; |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 279 | /* |
| 280 | * Special case where regulatory domain is the |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 281 | * result of an intersection between two regulatory domain |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 282 | * structures |
| 283 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 284 | if (alpha2[0] == '9' && alpha2[1] == '8') |
| 285 | return true; |
| 286 | return false; |
| 287 | } |
| 288 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 289 | static bool is_an_alpha2(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 290 | { |
| 291 | if (!alpha2) |
| 292 | return false; |
| 293 | if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1])) |
| 294 | return true; |
| 295 | return false; |
| 296 | } |
| 297 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 298 | static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 299 | { |
| 300 | if (!alpha2_x || !alpha2_y) |
| 301 | return false; |
| 302 | if (alpha2_x[0] == alpha2_y[0] && |
| 303 | alpha2_x[1] == alpha2_y[1]) |
| 304 | return true; |
| 305 | return false; |
| 306 | } |
| 307 | |
Luis R. Rodriguez | 69b1572 | 2009-02-21 00:04:33 -0500 | [diff] [blame] | 308 | static bool regdom_changes(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 309 | { |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 310 | assert_cfg80211_lock(); |
| 311 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 312 | if (!cfg80211_regdomain) |
| 313 | return true; |
| 314 | if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2)) |
| 315 | return false; |
| 316 | return true; |
| 317 | } |
| 318 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 319 | /** |
| 320 | * country_ie_integrity_changes - tells us if the country IE has changed |
| 321 | * @checksum: checksum of country IE of fields we are interested in |
| 322 | * |
| 323 | * If the country IE has not changed you can ignore it safely. This is |
| 324 | * useful to determine if two devices are seeing two different country IEs |
| 325 | * even on the same alpha2. Note that this will return false if no IE has |
| 326 | * been set on the wireless core yet. |
| 327 | */ |
| 328 | static bool country_ie_integrity_changes(u32 checksum) |
| 329 | { |
| 330 | /* If no IE has been set then the checksum doesn't change */ |
| 331 | if (unlikely(!last_request->country_ie_checksum)) |
| 332 | return false; |
| 333 | if (unlikely(last_request->country_ie_checksum != checksum)) |
| 334 | return true; |
| 335 | return false; |
| 336 | } |
| 337 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 338 | /* |
| 339 | * This lets us keep regulatory code which is updated on a regulatory |
| 340 | * basis in userspace. |
| 341 | */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 342 | static int call_crda(const char *alpha2) |
| 343 | { |
| 344 | char country_env[9 + 2] = "COUNTRY="; |
| 345 | char *envp[] = { |
| 346 | country_env, |
| 347 | NULL |
| 348 | }; |
| 349 | |
| 350 | if (!is_world_regdom((char *) alpha2)) |
| 351 | printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n", |
| 352 | alpha2[0], alpha2[1]); |
| 353 | else |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 354 | printk(KERN_INFO "cfg80211: Calling CRDA to update world " |
| 355 | "regulatory domain\n"); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 356 | |
| 357 | country_env[8] = alpha2[0]; |
| 358 | country_env[9] = alpha2[1]; |
| 359 | |
| 360 | return kobject_uevent_env(®_pdev->dev.kobj, KOBJ_CHANGE, envp); |
| 361 | } |
| 362 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 363 | /* Used by nl80211 before kmalloc'ing our regulatory domain */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 364 | bool reg_is_valid_request(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 365 | { |
Luis R. Rodriguez | 61405e9 | 2009-05-13 17:04:41 -0400 | [diff] [blame] | 366 | assert_cfg80211_lock(); |
| 367 | |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 368 | if (!last_request) |
| 369 | return false; |
| 370 | |
| 371 | return alpha2_equal(last_request->alpha2, alpha2); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* Sanity check on a regulatory rule */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 375 | static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 376 | { |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 377 | const struct ieee80211_freq_range *freq_range = &rule->freq_range; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 378 | u32 freq_diff; |
| 379 | |
Luis R. Rodriguez | 91e9900 | 2008-11-12 14:21:55 -0800 | [diff] [blame] | 380 | if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 381 | return false; |
| 382 | |
| 383 | if (freq_range->start_freq_khz > freq_range->end_freq_khz) |
| 384 | return false; |
| 385 | |
| 386 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; |
| 387 | |
Roel Kluin | bd05f28 | 2009-03-03 22:55:21 +0100 | [diff] [blame] | 388 | if (freq_range->end_freq_khz <= freq_range->start_freq_khz || |
| 389 | freq_range->max_bandwidth_khz > freq_diff) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 390 | return false; |
| 391 | |
| 392 | return true; |
| 393 | } |
| 394 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 395 | static bool is_valid_rd(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 396 | { |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 397 | const struct ieee80211_reg_rule *reg_rule = NULL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 398 | unsigned int i; |
| 399 | |
| 400 | if (!rd->n_reg_rules) |
| 401 | return false; |
| 402 | |
Luis R. Rodriguez | 88dc1c3 | 2008-11-12 14:22:01 -0800 | [diff] [blame] | 403 | if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES)) |
| 404 | return false; |
| 405 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 406 | for (i = 0; i < rd->n_reg_rules; i++) { |
| 407 | reg_rule = &rd->reg_rules[i]; |
| 408 | if (!is_valid_reg_rule(reg_rule)) |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 415 | static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range, |
| 416 | u32 center_freq_khz, |
| 417 | u32 bw_khz) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 418 | { |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 419 | u32 start_freq_khz, end_freq_khz; |
| 420 | |
| 421 | start_freq_khz = center_freq_khz - (bw_khz/2); |
| 422 | end_freq_khz = center_freq_khz + (bw_khz/2); |
| 423 | |
| 424 | if (start_freq_khz >= freq_range->start_freq_khz && |
| 425 | end_freq_khz <= freq_range->end_freq_khz) |
| 426 | return true; |
| 427 | |
| 428 | return false; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 431 | /** |
| 432 | * freq_in_rule_band - tells us if a frequency is in a frequency band |
| 433 | * @freq_range: frequency rule we want to query |
| 434 | * @freq_khz: frequency we are inquiring about |
| 435 | * |
| 436 | * This lets us know if a specific frequency rule is or is not relevant to |
| 437 | * a specific frequency's band. Bands are device specific and artificial |
| 438 | * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is |
| 439 | * safe for now to assume that a frequency rule should not be part of a |
| 440 | * frequency's band if the start freq or end freq are off by more than 2 GHz. |
| 441 | * This resolution can be lowered and should be considered as we add |
| 442 | * regulatory rule support for other "bands". |
| 443 | **/ |
| 444 | static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range, |
| 445 | u32 freq_khz) |
| 446 | { |
| 447 | #define ONE_GHZ_IN_KHZ 1000000 |
| 448 | if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ)) |
| 449 | return true; |
| 450 | if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ)) |
| 451 | return true; |
| 452 | return false; |
| 453 | #undef ONE_GHZ_IN_KHZ |
| 454 | } |
| 455 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 456 | /* |
| 457 | * Converts a country IE to a regulatory domain. A regulatory domain |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 458 | * structure has a lot of information which the IE doesn't yet have, |
| 459 | * so for the other values we use upper max values as we will intersect |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 460 | * with our userspace regulatory agent to get lower bounds. |
| 461 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 462 | static struct ieee80211_regdomain *country_ie_2_rd( |
| 463 | u8 *country_ie, |
| 464 | u8 country_ie_len, |
| 465 | u32 *checksum) |
| 466 | { |
| 467 | struct ieee80211_regdomain *rd = NULL; |
| 468 | unsigned int i = 0; |
| 469 | char alpha2[2]; |
| 470 | u32 flags = 0; |
| 471 | u32 num_rules = 0, size_of_regd = 0; |
| 472 | u8 *triplets_start = NULL; |
| 473 | u8 len_at_triplet = 0; |
| 474 | /* the last channel we have registered in a subband (triplet) */ |
| 475 | int last_sub_max_channel = 0; |
| 476 | |
| 477 | *checksum = 0xDEADBEEF; |
| 478 | |
| 479 | /* Country IE requirements */ |
| 480 | BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN || |
| 481 | country_ie_len & 0x01); |
| 482 | |
| 483 | alpha2[0] = country_ie[0]; |
| 484 | alpha2[1] = country_ie[1]; |
| 485 | |
| 486 | /* |
| 487 | * Third octet can be: |
| 488 | * 'I' - Indoor |
| 489 | * 'O' - Outdoor |
| 490 | * |
| 491 | * anything else we assume is no restrictions |
| 492 | */ |
| 493 | if (country_ie[2] == 'I') |
| 494 | flags = NL80211_RRF_NO_OUTDOOR; |
| 495 | else if (country_ie[2] == 'O') |
| 496 | flags = NL80211_RRF_NO_INDOOR; |
| 497 | |
| 498 | country_ie += 3; |
| 499 | country_ie_len -= 3; |
| 500 | |
| 501 | triplets_start = country_ie; |
| 502 | len_at_triplet = country_ie_len; |
| 503 | |
| 504 | *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8); |
| 505 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 506 | /* |
| 507 | * We need to build a reg rule for each triplet, but first we must |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 508 | * calculate the number of reg rules we will need. We will need one |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 509 | * for each channel subband |
| 510 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 511 | while (country_ie_len >= 3) { |
Luis R. Rodriguez | 615aab4 | 2009-01-22 15:05:46 -0800 | [diff] [blame] | 512 | int end_channel = 0; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 513 | struct ieee80211_country_ie_triplet *triplet = |
| 514 | (struct ieee80211_country_ie_triplet *) country_ie; |
| 515 | int cur_sub_max_channel = 0, cur_channel = 0; |
| 516 | |
| 517 | if (triplet->ext.reg_extension_id >= |
| 518 | IEEE80211_COUNTRY_EXTENSION_ID) { |
| 519 | country_ie += 3; |
| 520 | country_ie_len -= 3; |
| 521 | continue; |
| 522 | } |
| 523 | |
Luis R. Rodriguez | 615aab4 | 2009-01-22 15:05:46 -0800 | [diff] [blame] | 524 | /* 2 GHz */ |
| 525 | if (triplet->chans.first_channel <= 14) |
| 526 | end_channel = triplet->chans.first_channel + |
| 527 | triplet->chans.num_channels; |
| 528 | else |
| 529 | /* |
| 530 | * 5 GHz -- For example in country IEs if the first |
| 531 | * channel given is 36 and the number of channels is 4 |
| 532 | * then the individual channel numbers defined for the |
| 533 | * 5 GHz PHY by these parameters are: 36, 40, 44, and 48 |
| 534 | * and not 36, 37, 38, 39. |
| 535 | * |
| 536 | * See: http://tinyurl.com/11d-clarification |
| 537 | */ |
| 538 | end_channel = triplet->chans.first_channel + |
| 539 | (4 * (triplet->chans.num_channels - 1)); |
| 540 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 541 | cur_channel = triplet->chans.first_channel; |
Luis R. Rodriguez | 615aab4 | 2009-01-22 15:05:46 -0800 | [diff] [blame] | 542 | cur_sub_max_channel = end_channel; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 543 | |
| 544 | /* Basic sanity check */ |
| 545 | if (cur_sub_max_channel < cur_channel) |
| 546 | return NULL; |
| 547 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 548 | /* |
| 549 | * Do not allow overlapping channels. Also channels |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 550 | * passed in each subband must be monotonically |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 551 | * increasing |
| 552 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 553 | if (last_sub_max_channel) { |
| 554 | if (cur_channel <= last_sub_max_channel) |
| 555 | return NULL; |
| 556 | if (cur_sub_max_channel <= last_sub_max_channel) |
| 557 | return NULL; |
| 558 | } |
| 559 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 560 | /* |
| 561 | * When dot11RegulatoryClassesRequired is supported |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 562 | * we can throw ext triplets as part of this soup, |
| 563 | * for now we don't care when those change as we |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 564 | * don't support them |
| 565 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 566 | *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) | |
| 567 | ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) | |
| 568 | ((triplet->chans.max_power ^ cur_sub_max_channel) << 24); |
| 569 | |
| 570 | last_sub_max_channel = cur_sub_max_channel; |
| 571 | |
| 572 | country_ie += 3; |
| 573 | country_ie_len -= 3; |
| 574 | num_rules++; |
| 575 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 576 | /* |
| 577 | * Note: this is not a IEEE requirement but |
| 578 | * simply a memory requirement |
| 579 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 580 | if (num_rules > NL80211_MAX_SUPP_REG_RULES) |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | country_ie = triplets_start; |
| 585 | country_ie_len = len_at_triplet; |
| 586 | |
| 587 | size_of_regd = sizeof(struct ieee80211_regdomain) + |
| 588 | (num_rules * sizeof(struct ieee80211_reg_rule)); |
| 589 | |
| 590 | rd = kzalloc(size_of_regd, GFP_KERNEL); |
| 591 | if (!rd) |
| 592 | return NULL; |
| 593 | |
| 594 | rd->n_reg_rules = num_rules; |
| 595 | rd->alpha2[0] = alpha2[0]; |
| 596 | rd->alpha2[1] = alpha2[1]; |
| 597 | |
| 598 | /* This time around we fill in the rd */ |
| 599 | while (country_ie_len >= 3) { |
Luis R. Rodriguez | 02e68a3 | 2009-01-07 17:43:37 -0800 | [diff] [blame] | 600 | int end_channel = 0; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 601 | struct ieee80211_country_ie_triplet *triplet = |
| 602 | (struct ieee80211_country_ie_triplet *) country_ie; |
| 603 | struct ieee80211_reg_rule *reg_rule = NULL; |
| 604 | struct ieee80211_freq_range *freq_range = NULL; |
| 605 | struct ieee80211_power_rule *power_rule = NULL; |
| 606 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 607 | /* |
| 608 | * Must parse if dot11RegulatoryClassesRequired is true, |
| 609 | * we don't support this yet |
| 610 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 611 | if (triplet->ext.reg_extension_id >= |
| 612 | IEEE80211_COUNTRY_EXTENSION_ID) { |
| 613 | country_ie += 3; |
| 614 | country_ie_len -= 3; |
| 615 | continue; |
| 616 | } |
| 617 | |
| 618 | reg_rule = &rd->reg_rules[i]; |
| 619 | freq_range = ®_rule->freq_range; |
| 620 | power_rule = ®_rule->power_rule; |
| 621 | |
| 622 | reg_rule->flags = flags; |
| 623 | |
Luis R. Rodriguez | 02e68a3 | 2009-01-07 17:43:37 -0800 | [diff] [blame] | 624 | /* 2 GHz */ |
| 625 | if (triplet->chans.first_channel <= 14) |
| 626 | end_channel = triplet->chans.first_channel + |
| 627 | triplet->chans.num_channels; |
| 628 | else |
Luis R. Rodriguez | 02e68a3 | 2009-01-07 17:43:37 -0800 | [diff] [blame] | 629 | end_channel = triplet->chans.first_channel + |
| 630 | (4 * (triplet->chans.num_channels - 1)); |
| 631 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 632 | /* |
| 633 | * The +10 is since the regulatory domain expects |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 634 | * the actual band edge, not the center of freq for |
| 635 | * its start and end freqs, assuming 20 MHz bandwidth on |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 636 | * the channels passed |
| 637 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 638 | freq_range->start_freq_khz = |
| 639 | MHZ_TO_KHZ(ieee80211_channel_to_frequency( |
| 640 | triplet->chans.first_channel) - 10); |
| 641 | freq_range->end_freq_khz = |
| 642 | MHZ_TO_KHZ(ieee80211_channel_to_frequency( |
Luis R. Rodriguez | 02e68a3 | 2009-01-07 17:43:37 -0800 | [diff] [blame] | 643 | end_channel) + 10); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 644 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 645 | /* |
| 646 | * These are large arbitrary values we use to intersect later. |
| 647 | * Increment this if we ever support >= 40 MHz channels |
| 648 | * in IEEE 802.11 |
| 649 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 650 | freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40); |
| 651 | power_rule->max_antenna_gain = DBI_TO_MBI(100); |
| 652 | power_rule->max_eirp = DBM_TO_MBM(100); |
| 653 | |
| 654 | country_ie += 3; |
| 655 | country_ie_len -= 3; |
| 656 | i++; |
| 657 | |
| 658 | BUG_ON(i > NL80211_MAX_SUPP_REG_RULES); |
| 659 | } |
| 660 | |
| 661 | return rd; |
| 662 | } |
| 663 | |
| 664 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 665 | /* |
| 666 | * Helper for regdom_intersect(), this does the real |
| 667 | * mathematical intersection fun |
| 668 | */ |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 669 | static int reg_rules_intersect( |
| 670 | const struct ieee80211_reg_rule *rule1, |
| 671 | const struct ieee80211_reg_rule *rule2, |
| 672 | struct ieee80211_reg_rule *intersected_rule) |
| 673 | { |
| 674 | const struct ieee80211_freq_range *freq_range1, *freq_range2; |
| 675 | struct ieee80211_freq_range *freq_range; |
| 676 | const struct ieee80211_power_rule *power_rule1, *power_rule2; |
| 677 | struct ieee80211_power_rule *power_rule; |
| 678 | u32 freq_diff; |
| 679 | |
| 680 | freq_range1 = &rule1->freq_range; |
| 681 | freq_range2 = &rule2->freq_range; |
| 682 | freq_range = &intersected_rule->freq_range; |
| 683 | |
| 684 | power_rule1 = &rule1->power_rule; |
| 685 | power_rule2 = &rule2->power_rule; |
| 686 | power_rule = &intersected_rule->power_rule; |
| 687 | |
| 688 | freq_range->start_freq_khz = max(freq_range1->start_freq_khz, |
| 689 | freq_range2->start_freq_khz); |
| 690 | freq_range->end_freq_khz = min(freq_range1->end_freq_khz, |
| 691 | freq_range2->end_freq_khz); |
| 692 | freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz, |
| 693 | freq_range2->max_bandwidth_khz); |
| 694 | |
| 695 | freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz; |
| 696 | if (freq_range->max_bandwidth_khz > freq_diff) |
| 697 | freq_range->max_bandwidth_khz = freq_diff; |
| 698 | |
| 699 | power_rule->max_eirp = min(power_rule1->max_eirp, |
| 700 | power_rule2->max_eirp); |
| 701 | power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain, |
| 702 | power_rule2->max_antenna_gain); |
| 703 | |
| 704 | intersected_rule->flags = (rule1->flags | rule2->flags); |
| 705 | |
| 706 | if (!is_valid_reg_rule(intersected_rule)) |
| 707 | return -EINVAL; |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * regdom_intersect - do the intersection between two regulatory domains |
| 714 | * @rd1: first regulatory domain |
| 715 | * @rd2: second regulatory domain |
| 716 | * |
| 717 | * Use this function to get the intersection between two regulatory domains. |
| 718 | * Once completed we will mark the alpha2 for the rd as intersected, "98", |
| 719 | * as no one single alpha2 can represent this regulatory domain. |
| 720 | * |
| 721 | * Returns a pointer to the regulatory domain structure which will hold the |
| 722 | * resulting intersection of rules between rd1 and rd2. We will |
| 723 | * kzalloc() this structure for you. |
| 724 | */ |
| 725 | static struct ieee80211_regdomain *regdom_intersect( |
| 726 | const struct ieee80211_regdomain *rd1, |
| 727 | const struct ieee80211_regdomain *rd2) |
| 728 | { |
| 729 | int r, size_of_regd; |
| 730 | unsigned int x, y; |
| 731 | unsigned int num_rules = 0, rule_idx = 0; |
| 732 | const struct ieee80211_reg_rule *rule1, *rule2; |
| 733 | struct ieee80211_reg_rule *intersected_rule; |
| 734 | struct ieee80211_regdomain *rd; |
| 735 | /* This is just a dummy holder to help us count */ |
| 736 | struct ieee80211_reg_rule irule; |
| 737 | |
| 738 | /* Uses the stack temporarily for counter arithmetic */ |
| 739 | intersected_rule = &irule; |
| 740 | |
| 741 | memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule)); |
| 742 | |
| 743 | if (!rd1 || !rd2) |
| 744 | return NULL; |
| 745 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 746 | /* |
| 747 | * First we get a count of the rules we'll need, then we actually |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 748 | * build them. This is to so we can malloc() and free() a |
| 749 | * regdomain once. The reason we use reg_rules_intersect() here |
| 750 | * is it will return -EINVAL if the rule computed makes no sense. |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 751 | * All rules that do check out OK are valid. |
| 752 | */ |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 753 | |
| 754 | for (x = 0; x < rd1->n_reg_rules; x++) { |
| 755 | rule1 = &rd1->reg_rules[x]; |
| 756 | for (y = 0; y < rd2->n_reg_rules; y++) { |
| 757 | rule2 = &rd2->reg_rules[y]; |
| 758 | if (!reg_rules_intersect(rule1, rule2, |
| 759 | intersected_rule)) |
| 760 | num_rules++; |
| 761 | memset(intersected_rule, 0, |
| 762 | sizeof(struct ieee80211_reg_rule)); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | if (!num_rules) |
| 767 | return NULL; |
| 768 | |
| 769 | size_of_regd = sizeof(struct ieee80211_regdomain) + |
| 770 | ((num_rules + 1) * sizeof(struct ieee80211_reg_rule)); |
| 771 | |
| 772 | rd = kzalloc(size_of_regd, GFP_KERNEL); |
| 773 | if (!rd) |
| 774 | return NULL; |
| 775 | |
| 776 | for (x = 0; x < rd1->n_reg_rules; x++) { |
| 777 | rule1 = &rd1->reg_rules[x]; |
| 778 | for (y = 0; y < rd2->n_reg_rules; y++) { |
| 779 | rule2 = &rd2->reg_rules[y]; |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 780 | /* |
| 781 | * This time around instead of using the stack lets |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 782 | * write to the target rule directly saving ourselves |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 783 | * a memcpy() |
| 784 | */ |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 785 | intersected_rule = &rd->reg_rules[rule_idx]; |
| 786 | r = reg_rules_intersect(rule1, rule2, |
| 787 | intersected_rule); |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 788 | /* |
| 789 | * No need to memset here the intersected rule here as |
| 790 | * we're not using the stack anymore |
| 791 | */ |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 792 | if (r) |
| 793 | continue; |
| 794 | rule_idx++; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | if (rule_idx != num_rules) { |
| 799 | kfree(rd); |
| 800 | return NULL; |
| 801 | } |
| 802 | |
| 803 | rd->n_reg_rules = num_rules; |
| 804 | rd->alpha2[0] = '9'; |
| 805 | rd->alpha2[1] = '8'; |
| 806 | |
| 807 | return rd; |
| 808 | } |
| 809 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 810 | /* |
| 811 | * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may |
| 812 | * want to just have the channel structure use these |
| 813 | */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 814 | static u32 map_regdom_flags(u32 rd_flags) |
| 815 | { |
| 816 | u32 channel_flags = 0; |
| 817 | if (rd_flags & NL80211_RRF_PASSIVE_SCAN) |
| 818 | channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN; |
| 819 | if (rd_flags & NL80211_RRF_NO_IBSS) |
| 820 | channel_flags |= IEEE80211_CHAN_NO_IBSS; |
| 821 | if (rd_flags & NL80211_RRF_DFS) |
| 822 | channel_flags |= IEEE80211_CHAN_RADAR; |
| 823 | return channel_flags; |
| 824 | } |
| 825 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 826 | static int freq_reg_info_regd(struct wiphy *wiphy, |
| 827 | u32 center_freq, |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 828 | u32 desired_bw_khz, |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 829 | const struct ieee80211_reg_rule **reg_rule, |
| 830 | const struct ieee80211_regdomain *custom_regd) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 831 | { |
| 832 | int i; |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 833 | bool band_rule_found = false; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 834 | const struct ieee80211_regdomain *regd; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 835 | bool bw_fits = false; |
| 836 | |
| 837 | if (!desired_bw_khz) |
| 838 | desired_bw_khz = MHZ_TO_KHZ(20); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 839 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 840 | regd = custom_regd ? custom_regd : cfg80211_regdomain; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 841 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 842 | /* |
| 843 | * Follow the driver's regulatory domain, if present, unless a country |
| 844 | * IE has been processed or a user wants to help complaince further |
| 845 | */ |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 846 | if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && |
| 847 | last_request->initiator != NL80211_REGDOM_SET_BY_USER && |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 848 | wiphy->regd) |
| 849 | regd = wiphy->regd; |
| 850 | |
| 851 | if (!regd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 852 | return -EINVAL; |
| 853 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 854 | for (i = 0; i < regd->n_reg_rules; i++) { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 855 | const struct ieee80211_reg_rule *rr; |
| 856 | const struct ieee80211_freq_range *fr = NULL; |
| 857 | const struct ieee80211_power_rule *pr = NULL; |
| 858 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 859 | rr = ®d->reg_rules[i]; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 860 | fr = &rr->freq_range; |
| 861 | pr = &rr->power_rule; |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 862 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 863 | /* |
| 864 | * We only need to know if one frequency rule was |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 865 | * was in center_freq's band, that's enough, so lets |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 866 | * not overwrite it once found |
| 867 | */ |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 868 | if (!band_rule_found) |
| 869 | band_rule_found = freq_in_rule_band(fr, center_freq); |
| 870 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 871 | bw_fits = reg_does_bw_fit(fr, |
| 872 | center_freq, |
| 873 | desired_bw_khz); |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 874 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 875 | if (band_rule_found && bw_fits) { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 876 | *reg_rule = rr; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 877 | return 0; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 881 | if (!band_rule_found) |
| 882 | return -ERANGE; |
| 883 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 884 | return -EINVAL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 885 | } |
Luis R. Rodriguez | 34f5734 | 2009-01-22 15:05:45 -0800 | [diff] [blame] | 886 | EXPORT_SYMBOL(freq_reg_info); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 887 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 888 | int freq_reg_info(struct wiphy *wiphy, |
| 889 | u32 center_freq, |
| 890 | u32 desired_bw_khz, |
| 891 | const struct ieee80211_reg_rule **reg_rule) |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 892 | { |
Luis R. Rodriguez | ac46d48 | 2009-05-01 18:44:50 -0400 | [diff] [blame] | 893 | assert_cfg80211_lock(); |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 894 | return freq_reg_info_regd(wiphy, |
| 895 | center_freq, |
| 896 | desired_bw_khz, |
| 897 | reg_rule, |
| 898 | NULL); |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 899 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 900 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 901 | /* |
| 902 | * Note that right now we assume the desired channel bandwidth |
| 903 | * is always 20 MHz for each individual channel (HT40 uses 20 MHz |
| 904 | * per channel, the primary and the extension channel). To support |
| 905 | * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a |
| 906 | * new ieee80211_channel.target_bw and re run the regulatory check |
| 907 | * on the wiphy with the target_bw specified. Then we can simply use |
| 908 | * that below for the desired_bw_khz below. |
| 909 | */ |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 910 | static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band, |
| 911 | unsigned int chan_idx) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 912 | { |
| 913 | int r; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 914 | u32 flags, bw_flags = 0; |
| 915 | u32 desired_bw_khz = MHZ_TO_KHZ(20); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 916 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 917 | const struct ieee80211_power_rule *power_rule = NULL; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 918 | const struct ieee80211_freq_range *freq_range = NULL; |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 919 | struct ieee80211_supported_band *sband; |
| 920 | struct ieee80211_channel *chan; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 921 | struct wiphy *request_wiphy = NULL; |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 922 | |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 923 | assert_cfg80211_lock(); |
| 924 | |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 925 | request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
| 926 | |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 927 | sband = wiphy->bands[band]; |
| 928 | BUG_ON(chan_idx >= sband->n_channels); |
| 929 | chan = &sband->channels[chan_idx]; |
| 930 | |
| 931 | flags = chan->orig_flags; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 932 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 933 | r = freq_reg_info(wiphy, |
| 934 | MHZ_TO_KHZ(chan->center_freq), |
| 935 | desired_bw_khz, |
| 936 | ®_rule); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 937 | |
| 938 | if (r) { |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 939 | /* |
| 940 | * This means no regulatory rule was found in the country IE |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 941 | * with a frequency range on the center_freq's band, since |
| 942 | * IEEE-802.11 allows for a country IE to have a subset of the |
| 943 | * regulatory information provided in a country we ignore |
| 944 | * disabling the channel unless at least one reg rule was |
| 945 | * found on the center_freq's band. For details see this |
| 946 | * clarification: |
| 947 | * |
| 948 | * http://tinyurl.com/11d-clarification |
| 949 | */ |
| 950 | if (r == -ERANGE && |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 951 | last_request->initiator == |
| 952 | NL80211_REGDOM_SET_BY_COUNTRY_IE) { |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 953 | #ifdef CONFIG_CFG80211_REG_DEBUG |
| 954 | printk(KERN_DEBUG "cfg80211: Leaving channel %d MHz " |
| 955 | "intact on %s - no rule found in band on " |
| 956 | "Country IE\n", |
| 957 | chan->center_freq, wiphy_name(wiphy)); |
| 958 | #endif |
| 959 | } else { |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 960 | /* |
| 961 | * In this case we know the country IE has at least one reg rule |
| 962 | * for the band so we respect its band definitions |
| 963 | */ |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 964 | #ifdef CONFIG_CFG80211_REG_DEBUG |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 965 | if (last_request->initiator == |
| 966 | NL80211_REGDOM_SET_BY_COUNTRY_IE) |
Luis R. Rodriguez | 0c7dc45 | 2009-01-07 17:43:36 -0800 | [diff] [blame] | 967 | printk(KERN_DEBUG "cfg80211: Disabling " |
| 968 | "channel %d MHz on %s due to " |
| 969 | "Country IE\n", |
| 970 | chan->center_freq, wiphy_name(wiphy)); |
| 971 | #endif |
| 972 | flags |= IEEE80211_CHAN_DISABLED; |
| 973 | chan->flags = flags; |
| 974 | } |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 975 | return; |
| 976 | } |
| 977 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 978 | power_rule = ®_rule->power_rule; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 979 | freq_range = ®_rule->freq_range; |
| 980 | |
| 981 | if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40)) |
| 982 | bw_flags = IEEE80211_CHAN_NO_HT40; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 983 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 984 | if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER && |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 985 | request_wiphy && request_wiphy == wiphy && |
Johannes Berg | 5be83de | 2009-11-19 00:56:28 +0100 | [diff] [blame] | 986 | request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) { |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 987 | /* |
| 988 | * This gaurantees the driver's requested regulatory domain |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 989 | * will always be used as a base for further regulatory |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 990 | * settings |
| 991 | */ |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 992 | chan->flags = chan->orig_flags = |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 993 | map_regdom_flags(reg_rule->flags) | bw_flags; |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 994 | chan->max_antenna_gain = chan->orig_mag = |
| 995 | (int) MBI_TO_DBI(power_rule->max_antenna_gain); |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 996 | chan->max_power = chan->orig_mpwr = |
| 997 | (int) MBM_TO_DBM(power_rule->max_eirp); |
| 998 | return; |
| 999 | } |
| 1000 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1001 | chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1002 | chan->max_antenna_gain = min(chan->orig_mag, |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1003 | (int) MBI_TO_DBI(power_rule->max_antenna_gain)); |
John W. Linville | 253898c | 2008-04-03 15:32:54 -0400 | [diff] [blame] | 1004 | if (chan->orig_mpwr) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1005 | chan->max_power = min(chan->orig_mpwr, |
| 1006 | (int) MBM_TO_DBM(power_rule->max_eirp)); |
John W. Linville | 253898c | 2008-04-03 15:32:54 -0400 | [diff] [blame] | 1007 | else |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1008 | chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 1011 | static void handle_band(struct wiphy *wiphy, enum ieee80211_band band) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1012 | { |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 1013 | unsigned int i; |
| 1014 | struct ieee80211_supported_band *sband; |
| 1015 | |
| 1016 | BUG_ON(!wiphy->bands[band]); |
| 1017 | sband = wiphy->bands[band]; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1018 | |
| 1019 | for (i = 0; i < sband->n_channels; i++) |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 1020 | handle_channel(wiphy, band, i); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1021 | } |
| 1022 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1023 | static bool ignore_reg_update(struct wiphy *wiphy, |
| 1024 | enum nl80211_reg_initiator initiator) |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 1025 | { |
| 1026 | if (!last_request) |
| 1027 | return true; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1028 | if (initiator == NL80211_REGDOM_SET_BY_CORE && |
Johannes Berg | 5be83de | 2009-11-19 00:56:28 +0100 | [diff] [blame] | 1029 | wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 1030 | return true; |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1031 | /* |
| 1032 | * wiphy->regd will be set once the device has its own |
| 1033 | * desired regulatory domain set |
| 1034 | */ |
Johannes Berg | 5be83de | 2009-11-19 00:56:28 +0100 | [diff] [blame] | 1035 | if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd && |
Luis R. Rodriguez | f976376 | 2009-01-22 15:05:52 -0800 | [diff] [blame] | 1036 | !is_world_regdom(last_request->alpha2)) |
Luis R. Rodriguez | 14b9815 | 2008-11-12 14:22:03 -0800 | [diff] [blame] | 1037 | return true; |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1041 | static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1042 | { |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 1043 | struct cfg80211_registered_device *rdev; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1044 | |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 1045 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) |
| 1046 | wiphy_update_regulatory(&rdev->wiphy, initiator); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1049 | static void handle_reg_beacon(struct wiphy *wiphy, |
| 1050 | unsigned int chan_idx, |
| 1051 | struct reg_beacon *reg_beacon) |
| 1052 | { |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1053 | struct ieee80211_supported_band *sband; |
| 1054 | struct ieee80211_channel *chan; |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1055 | bool channel_changed = false; |
| 1056 | struct ieee80211_channel chan_before; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1057 | |
| 1058 | assert_cfg80211_lock(); |
| 1059 | |
| 1060 | sband = wiphy->bands[reg_beacon->chan.band]; |
| 1061 | chan = &sband->channels[chan_idx]; |
| 1062 | |
| 1063 | if (likely(chan->center_freq != reg_beacon->chan.center_freq)) |
| 1064 | return; |
| 1065 | |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1066 | if (chan->beacon_found) |
| 1067 | return; |
| 1068 | |
| 1069 | chan->beacon_found = true; |
| 1070 | |
Johannes Berg | 5be83de | 2009-11-19 00:56:28 +0100 | [diff] [blame] | 1071 | if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS) |
Luis R. Rodriguez | 3718424 | 2009-07-30 17:43:48 -0700 | [diff] [blame] | 1072 | return; |
| 1073 | |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1074 | chan_before.center_freq = chan->center_freq; |
| 1075 | chan_before.flags = chan->flags; |
| 1076 | |
Luis R. Rodriguez | 3718424 | 2009-07-30 17:43:48 -0700 | [diff] [blame] | 1077 | if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) { |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1078 | chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN; |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1079 | channel_changed = true; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1080 | } |
| 1081 | |
Luis R. Rodriguez | 3718424 | 2009-07-30 17:43:48 -0700 | [diff] [blame] | 1082 | if (chan->flags & IEEE80211_CHAN_NO_IBSS) { |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1083 | chan->flags &= ~IEEE80211_CHAN_NO_IBSS; |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1084 | channel_changed = true; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1085 | } |
| 1086 | |
Luis R. Rodriguez | 6bad876 | 2009-04-02 14:08:09 -0400 | [diff] [blame] | 1087 | if (channel_changed) |
| 1088 | nl80211_send_beacon_hint_event(wiphy, &chan_before, chan); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * Called when a scan on a wiphy finds a beacon on |
| 1093 | * new channel |
| 1094 | */ |
| 1095 | static void wiphy_update_new_beacon(struct wiphy *wiphy, |
| 1096 | struct reg_beacon *reg_beacon) |
| 1097 | { |
| 1098 | unsigned int i; |
| 1099 | struct ieee80211_supported_band *sband; |
| 1100 | |
| 1101 | assert_cfg80211_lock(); |
| 1102 | |
| 1103 | if (!wiphy->bands[reg_beacon->chan.band]) |
| 1104 | return; |
| 1105 | |
| 1106 | sband = wiphy->bands[reg_beacon->chan.band]; |
| 1107 | |
| 1108 | for (i = 0; i < sband->n_channels; i++) |
| 1109 | handle_reg_beacon(wiphy, i, reg_beacon); |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * Called upon reg changes or a new wiphy is added |
| 1114 | */ |
| 1115 | static void wiphy_update_beacon_reg(struct wiphy *wiphy) |
| 1116 | { |
| 1117 | unsigned int i; |
| 1118 | struct ieee80211_supported_band *sband; |
| 1119 | struct reg_beacon *reg_beacon; |
| 1120 | |
| 1121 | assert_cfg80211_lock(); |
| 1122 | |
| 1123 | if (list_empty(®_beacon_list)) |
| 1124 | return; |
| 1125 | |
| 1126 | list_for_each_entry(reg_beacon, ®_beacon_list, list) { |
| 1127 | if (!wiphy->bands[reg_beacon->chan.band]) |
| 1128 | continue; |
| 1129 | sband = wiphy->bands[reg_beacon->chan.band]; |
| 1130 | for (i = 0; i < sband->n_channels; i++) |
| 1131 | handle_reg_beacon(wiphy, i, reg_beacon); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | static bool reg_is_world_roaming(struct wiphy *wiphy) |
| 1136 | { |
| 1137 | if (is_world_regdom(cfg80211_regdomain->alpha2) || |
| 1138 | (wiphy->regd && is_world_regdom(wiphy->regd->alpha2))) |
| 1139 | return true; |
Luis R. Rodriguez | b1ed8dd | 2009-05-02 00:34:15 -0400 | [diff] [blame] | 1140 | if (last_request && |
| 1141 | last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && |
Johannes Berg | 5be83de | 2009-11-19 00:56:28 +0100 | [diff] [blame] | 1142 | wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1143 | return true; |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | /* Reap the advantages of previously found beacons */ |
| 1148 | static void reg_process_beacons(struct wiphy *wiphy) |
| 1149 | { |
Luis R. Rodriguez | b1ed8dd | 2009-05-02 00:34:15 -0400 | [diff] [blame] | 1150 | /* |
| 1151 | * Means we are just firing up cfg80211, so no beacons would |
| 1152 | * have been processed yet. |
| 1153 | */ |
| 1154 | if (!last_request) |
| 1155 | return; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1156 | if (!reg_is_world_roaming(wiphy)) |
| 1157 | return; |
| 1158 | wiphy_update_beacon_reg(wiphy); |
| 1159 | } |
| 1160 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1161 | static bool is_ht40_not_allowed(struct ieee80211_channel *chan) |
| 1162 | { |
| 1163 | if (!chan) |
| 1164 | return true; |
| 1165 | if (chan->flags & IEEE80211_CHAN_DISABLED) |
| 1166 | return true; |
| 1167 | /* This would happen when regulatory rules disallow HT40 completely */ |
| 1168 | if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40))) |
| 1169 | return true; |
| 1170 | return false; |
| 1171 | } |
| 1172 | |
| 1173 | static void reg_process_ht_flags_channel(struct wiphy *wiphy, |
| 1174 | enum ieee80211_band band, |
| 1175 | unsigned int chan_idx) |
| 1176 | { |
| 1177 | struct ieee80211_supported_band *sband; |
| 1178 | struct ieee80211_channel *channel; |
| 1179 | struct ieee80211_channel *channel_before = NULL, *channel_after = NULL; |
| 1180 | unsigned int i; |
| 1181 | |
| 1182 | assert_cfg80211_lock(); |
| 1183 | |
| 1184 | sband = wiphy->bands[band]; |
| 1185 | BUG_ON(chan_idx >= sband->n_channels); |
| 1186 | channel = &sband->channels[chan_idx]; |
| 1187 | |
| 1188 | if (is_ht40_not_allowed(channel)) { |
| 1189 | channel->flags |= IEEE80211_CHAN_NO_HT40; |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * We need to ensure the extension channels exist to |
| 1195 | * be able to use HT40- or HT40+, this finds them (or not) |
| 1196 | */ |
| 1197 | for (i = 0; i < sband->n_channels; i++) { |
| 1198 | struct ieee80211_channel *c = &sband->channels[i]; |
| 1199 | if (c->center_freq == (channel->center_freq - 20)) |
| 1200 | channel_before = c; |
| 1201 | if (c->center_freq == (channel->center_freq + 20)) |
| 1202 | channel_after = c; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * Please note that this assumes target bandwidth is 20 MHz, |
| 1207 | * if that ever changes we also need to change the below logic |
| 1208 | * to include that as well. |
| 1209 | */ |
| 1210 | if (is_ht40_not_allowed(channel_before)) |
Luis R. Rodriguez | 689da1b | 2009-05-02 00:37:18 -0400 | [diff] [blame] | 1211 | channel->flags |= IEEE80211_CHAN_NO_HT40MINUS; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1212 | else |
Luis R. Rodriguez | 689da1b | 2009-05-02 00:37:18 -0400 | [diff] [blame] | 1213 | channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1214 | |
| 1215 | if (is_ht40_not_allowed(channel_after)) |
Luis R. Rodriguez | 689da1b | 2009-05-02 00:37:18 -0400 | [diff] [blame] | 1216 | channel->flags |= IEEE80211_CHAN_NO_HT40PLUS; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1217 | else |
Luis R. Rodriguez | 689da1b | 2009-05-02 00:37:18 -0400 | [diff] [blame] | 1218 | channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | static void reg_process_ht_flags_band(struct wiphy *wiphy, |
| 1222 | enum ieee80211_band band) |
| 1223 | { |
| 1224 | unsigned int i; |
| 1225 | struct ieee80211_supported_band *sband; |
| 1226 | |
| 1227 | BUG_ON(!wiphy->bands[band]); |
| 1228 | sband = wiphy->bands[band]; |
| 1229 | |
| 1230 | for (i = 0; i < sband->n_channels; i++) |
| 1231 | reg_process_ht_flags_channel(wiphy, band, i); |
| 1232 | } |
| 1233 | |
| 1234 | static void reg_process_ht_flags(struct wiphy *wiphy) |
| 1235 | { |
| 1236 | enum ieee80211_band band; |
| 1237 | |
| 1238 | if (!wiphy) |
| 1239 | return; |
| 1240 | |
| 1241 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 1242 | if (wiphy->bands[band]) |
| 1243 | reg_process_ht_flags_band(wiphy, band); |
| 1244 | } |
| 1245 | |
| 1246 | } |
| 1247 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1248 | void wiphy_update_regulatory(struct wiphy *wiphy, |
| 1249 | enum nl80211_reg_initiator initiator) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1250 | { |
| 1251 | enum ieee80211_band band; |
Luis R. Rodriguez | d46e5b1 | 2009-01-22 15:05:50 -0800 | [diff] [blame] | 1252 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1253 | if (ignore_reg_update(wiphy, initiator)) |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1254 | goto out; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1255 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1256 | if (wiphy->bands[band]) |
Luis R. Rodriguez | a92a3ce | 2009-01-07 17:43:33 -0800 | [diff] [blame] | 1257 | handle_band(wiphy, band); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1258 | } |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1259 | out: |
| 1260 | reg_process_beacons(wiphy); |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1261 | reg_process_ht_flags(wiphy); |
Luis R. Rodriguez | 560e28e | 2009-01-07 17:43:32 -0800 | [diff] [blame] | 1262 | if (wiphy->reg_notifier) |
Luis R. Rodriguez | 716f939 | 2009-01-22 15:05:51 -0800 | [diff] [blame] | 1263 | wiphy->reg_notifier(wiphy, last_request); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1266 | static void handle_channel_custom(struct wiphy *wiphy, |
| 1267 | enum ieee80211_band band, |
| 1268 | unsigned int chan_idx, |
| 1269 | const struct ieee80211_regdomain *regd) |
| 1270 | { |
| 1271 | int r; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1272 | u32 desired_bw_khz = MHZ_TO_KHZ(20); |
| 1273 | u32 bw_flags = 0; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1274 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 1275 | const struct ieee80211_power_rule *power_rule = NULL; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1276 | const struct ieee80211_freq_range *freq_range = NULL; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1277 | struct ieee80211_supported_band *sband; |
| 1278 | struct ieee80211_channel *chan; |
| 1279 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1280 | assert_reg_lock(); |
Luis R. Rodriguez | ac46d48 | 2009-05-01 18:44:50 -0400 | [diff] [blame] | 1281 | |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1282 | sband = wiphy->bands[band]; |
| 1283 | BUG_ON(chan_idx >= sband->n_channels); |
| 1284 | chan = &sband->channels[chan_idx]; |
| 1285 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1286 | r = freq_reg_info_regd(wiphy, |
| 1287 | MHZ_TO_KHZ(chan->center_freq), |
| 1288 | desired_bw_khz, |
| 1289 | ®_rule, |
| 1290 | regd); |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1291 | |
| 1292 | if (r) { |
| 1293 | chan->flags = IEEE80211_CHAN_DISABLED; |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | power_rule = ®_rule->power_rule; |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1298 | freq_range = ®_rule->freq_range; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1299 | |
Luis R. Rodriguez | 038659e | 2009-05-02 00:37:17 -0400 | [diff] [blame] | 1300 | if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40)) |
| 1301 | bw_flags = IEEE80211_CHAN_NO_HT40; |
| 1302 | |
| 1303 | chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1304 | chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain); |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1305 | chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp); |
| 1306 | } |
| 1307 | |
| 1308 | static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band, |
| 1309 | const struct ieee80211_regdomain *regd) |
| 1310 | { |
| 1311 | unsigned int i; |
| 1312 | struct ieee80211_supported_band *sband; |
| 1313 | |
| 1314 | BUG_ON(!wiphy->bands[band]); |
| 1315 | sband = wiphy->bands[band]; |
| 1316 | |
| 1317 | for (i = 0; i < sband->n_channels; i++) |
| 1318 | handle_channel_custom(wiphy, band, i, regd); |
| 1319 | } |
| 1320 | |
| 1321 | /* Used by drivers prior to wiphy registration */ |
| 1322 | void wiphy_apply_custom_regulatory(struct wiphy *wiphy, |
| 1323 | const struct ieee80211_regdomain *regd) |
| 1324 | { |
| 1325 | enum ieee80211_band band; |
Luis R. Rodriguez | bbcf3f0 | 2009-05-19 17:49:47 -0400 | [diff] [blame] | 1326 | unsigned int bands_set = 0; |
Luis R. Rodriguez | ac46d48 | 2009-05-01 18:44:50 -0400 | [diff] [blame] | 1327 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1328 | mutex_lock(®_mutex); |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1329 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
Luis R. Rodriguez | bbcf3f0 | 2009-05-19 17:49:47 -0400 | [diff] [blame] | 1330 | if (!wiphy->bands[band]) |
| 1331 | continue; |
| 1332 | handle_band_custom(wiphy, band, regd); |
| 1333 | bands_set++; |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1334 | } |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1335 | mutex_unlock(®_mutex); |
Luis R. Rodriguez | bbcf3f0 | 2009-05-19 17:49:47 -0400 | [diff] [blame] | 1336 | |
| 1337 | /* |
| 1338 | * no point in calling this if it won't have any effect |
| 1339 | * on your device's supportd bands. |
| 1340 | */ |
| 1341 | WARN_ON(!bands_set); |
Luis R. Rodriguez | 1fa25e4 | 2009-01-22 15:05:44 -0800 | [diff] [blame] | 1342 | } |
| 1343 | EXPORT_SYMBOL(wiphy_apply_custom_regulatory); |
| 1344 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1345 | static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd, |
| 1346 | const struct ieee80211_regdomain *src_regd) |
| 1347 | { |
| 1348 | struct ieee80211_regdomain *regd; |
| 1349 | int size_of_regd = 0; |
| 1350 | unsigned int i; |
| 1351 | |
| 1352 | size_of_regd = sizeof(struct ieee80211_regdomain) + |
| 1353 | ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule)); |
| 1354 | |
| 1355 | regd = kzalloc(size_of_regd, GFP_KERNEL); |
| 1356 | if (!regd) |
| 1357 | return -ENOMEM; |
| 1358 | |
| 1359 | memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain)); |
| 1360 | |
| 1361 | for (i = 0; i < src_regd->n_reg_rules; i++) |
| 1362 | memcpy(®d->reg_rules[i], &src_regd->reg_rules[i], |
| 1363 | sizeof(struct ieee80211_reg_rule)); |
| 1364 | |
| 1365 | *dst_regd = regd; |
| 1366 | return 0; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1369 | /* |
| 1370 | * Return value which can be used by ignore_request() to indicate |
| 1371 | * it has been determined we should intersect two regulatory domains |
| 1372 | */ |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1373 | #define REG_INTERSECT 1 |
| 1374 | |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1375 | /* This has the logic which determines when a new request |
| 1376 | * should be ignored. */ |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1377 | static int ignore_request(struct wiphy *wiphy, |
| 1378 | struct regulatory_request *pending_request) |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1379 | { |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 1380 | struct wiphy *last_wiphy = NULL; |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 1381 | |
| 1382 | assert_cfg80211_lock(); |
| 1383 | |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1384 | /* All initial requests are respected */ |
| 1385 | if (!last_request) |
| 1386 | return 0; |
| 1387 | |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1388 | switch (pending_request->initiator) { |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1389 | case NL80211_REGDOM_SET_BY_CORE: |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 1390 | return -EINVAL; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1391 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 1392 | |
| 1393 | last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
| 1394 | |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1395 | if (unlikely(!is_an_alpha2(pending_request->alpha2))) |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1396 | return -EINVAL; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1397 | if (last_request->initiator == |
| 1398 | NL80211_REGDOM_SET_BY_COUNTRY_IE) { |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 1399 | if (last_wiphy != wiphy) { |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1400 | /* |
| 1401 | * Two cards with two APs claiming different |
Thadeu Lima de Souza Cascardo | 1fe90b0 | 2009-08-11 11:18:42 -0300 | [diff] [blame] | 1402 | * Country IE alpha2s. We could |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1403 | * intersect them, but that seems unlikely |
| 1404 | * to be correct. Reject second one for now. |
| 1405 | */ |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1406 | if (regdom_changes(pending_request->alpha2)) |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1407 | return -EOPNOTSUPP; |
| 1408 | return -EALREADY; |
| 1409 | } |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1410 | /* |
| 1411 | * Two consecutive Country IE hints on the same wiphy. |
| 1412 | * This should be picked up early by the driver/stack |
| 1413 | */ |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1414 | if (WARN_ON(regdom_changes(pending_request->alpha2))) |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1415 | return 0; |
| 1416 | return -EALREADY; |
| 1417 | } |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1418 | return REG_INTERSECT; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1419 | case NL80211_REGDOM_SET_BY_DRIVER: |
| 1420 | if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) { |
Luis R. Rodriguez | e74b1e7 | 2009-01-22 15:05:48 -0800 | [diff] [blame] | 1421 | if (is_old_static_regdom(cfg80211_regdomain)) |
| 1422 | return 0; |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1423 | if (regdom_changes(pending_request->alpha2)) |
Luis R. Rodriguez | e74b1e7 | 2009-01-22 15:05:48 -0800 | [diff] [blame] | 1424 | return 0; |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1425 | return -EALREADY; |
Luis R. Rodriguez | e74b1e7 | 2009-01-22 15:05:48 -0800 | [diff] [blame] | 1426 | } |
Luis R. Rodriguez | fff32c0 | 2009-02-21 00:04:32 -0500 | [diff] [blame] | 1427 | |
| 1428 | /* |
| 1429 | * This would happen if you unplug and plug your card |
| 1430 | * back in or if you add a new device for which the previously |
| 1431 | * loaded card also agrees on the regulatory domain. |
| 1432 | */ |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1433 | if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER && |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1434 | !regdom_changes(pending_request->alpha2)) |
Luis R. Rodriguez | fff32c0 | 2009-02-21 00:04:32 -0500 | [diff] [blame] | 1435 | return -EALREADY; |
| 1436 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1437 | return REG_INTERSECT; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1438 | case NL80211_REGDOM_SET_BY_USER: |
| 1439 | if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1440 | return REG_INTERSECT; |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1441 | /* |
| 1442 | * If the user knows better the user should set the regdom |
| 1443 | * to their country before the IE is picked up |
| 1444 | */ |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1445 | if (last_request->initiator == NL80211_REGDOM_SET_BY_USER && |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1446 | last_request->intersect) |
| 1447 | return -EOPNOTSUPP; |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1448 | /* |
| 1449 | * Process user requests only after previous user/driver/core |
| 1450 | * requests have been processed |
| 1451 | */ |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1452 | if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE || |
| 1453 | last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER || |
| 1454 | last_request->initiator == NL80211_REGDOM_SET_BY_USER) { |
Luis R. Rodriguez | 69b1572 | 2009-02-21 00:04:33 -0500 | [diff] [blame] | 1455 | if (regdom_changes(last_request->alpha2)) |
Luis R. Rodriguez | 5eebade | 2009-01-22 15:05:47 -0800 | [diff] [blame] | 1456 | return -EAGAIN; |
| 1457 | } |
| 1458 | |
Luis R. Rodriguez | e74b1e7 | 2009-01-22 15:05:48 -0800 | [diff] [blame] | 1459 | if (!is_old_static_regdom(cfg80211_regdomain) && |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1460 | !regdom_changes(pending_request->alpha2)) |
Luis R. Rodriguez | e74b1e7 | 2009-01-22 15:05:48 -0800 | [diff] [blame] | 1461 | return -EALREADY; |
| 1462 | |
Johannes Berg | 84fa4f4 | 2008-10-24 20:32:23 +0200 | [diff] [blame] | 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | return -EINVAL; |
| 1467 | } |
| 1468 | |
Luis R. Rodriguez | d1c96a9 | 2009-02-21 00:24:13 -0500 | [diff] [blame] | 1469 | /** |
| 1470 | * __regulatory_hint - hint to the wireless core a regulatory domain |
| 1471 | * @wiphy: if the hint comes from country information from an AP, this |
| 1472 | * is required to be set to the wiphy that received the information |
Luis R. Rodriguez | 28da32d | 2009-02-21 00:24:14 -0500 | [diff] [blame] | 1473 | * @pending_request: the regulatory request currently being processed |
Luis R. Rodriguez | d1c96a9 | 2009-02-21 00:24:13 -0500 | [diff] [blame] | 1474 | * |
| 1475 | * The Wireless subsystem can use this function to hint to the wireless core |
Luis R. Rodriguez | 28da32d | 2009-02-21 00:24:14 -0500 | [diff] [blame] | 1476 | * what it believes should be the current regulatory domain. |
Luis R. Rodriguez | d1c96a9 | 2009-02-21 00:24:13 -0500 | [diff] [blame] | 1477 | * |
| 1478 | * Returns zero if all went fine, %-EALREADY if a regulatory domain had |
| 1479 | * already been set or other standard error codes. |
| 1480 | * |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1481 | * Caller must hold &cfg80211_mutex and ®_mutex |
Luis R. Rodriguez | d1c96a9 | 2009-02-21 00:24:13 -0500 | [diff] [blame] | 1482 | */ |
Luis R. Rodriguez | 28da32d | 2009-02-21 00:24:14 -0500 | [diff] [blame] | 1483 | static int __regulatory_hint(struct wiphy *wiphy, |
| 1484 | struct regulatory_request *pending_request) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1485 | { |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1486 | bool intersect = false; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1487 | int r = 0; |
| 1488 | |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 1489 | assert_cfg80211_lock(); |
| 1490 | |
Luis R. Rodriguez | 2f92cd2 | 2009-02-21 00:24:16 -0500 | [diff] [blame] | 1491 | r = ignore_request(wiphy, pending_request); |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1492 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1493 | if (r == REG_INTERSECT) { |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1494 | if (pending_request->initiator == |
| 1495 | NL80211_REGDOM_SET_BY_DRIVER) { |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1496 | r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain); |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1497 | if (r) { |
| 1498 | kfree(pending_request); |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1499 | return r; |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1500 | } |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1501 | } |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 1502 | intersect = true; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1503 | } else if (r) { |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1504 | /* |
| 1505 | * If the regulatory domain being requested by the |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1506 | * driver has already been set just copy it to the |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1507 | * wiphy |
| 1508 | */ |
Luis R. Rodriguez | 28da32d | 2009-02-21 00:24:14 -0500 | [diff] [blame] | 1509 | if (r == -EALREADY && |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1510 | pending_request->initiator == |
| 1511 | NL80211_REGDOM_SET_BY_DRIVER) { |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1512 | r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain); |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1513 | if (r) { |
| 1514 | kfree(pending_request); |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1515 | return r; |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1516 | } |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1517 | r = -EALREADY; |
| 1518 | goto new_request; |
| 1519 | } |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1520 | kfree(pending_request); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1521 | return r; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1522 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1523 | |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1524 | new_request: |
Luis R. Rodriguez | 5203cdb | 2008-11-12 14:21:56 -0800 | [diff] [blame] | 1525 | kfree(last_request); |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1526 | |
| 1527 | last_request = pending_request; |
| 1528 | last_request->intersect = intersect; |
| 1529 | |
| 1530 | pending_request = NULL; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1531 | |
| 1532 | /* When r == REG_INTERSECT we do need to call CRDA */ |
Luis R. Rodriguez | 73d54c9 | 2009-03-09 22:07:42 -0400 | [diff] [blame] | 1533 | if (r < 0) { |
| 1534 | /* |
| 1535 | * Since CRDA will not be called in this case as we already |
| 1536 | * have applied the requested regulatory domain before we just |
| 1537 | * inform userspace we have processed the request |
| 1538 | */ |
| 1539 | if (r == -EALREADY) |
| 1540 | nl80211_send_reg_change_event(last_request); |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1541 | return r; |
Luis R. Rodriguez | 73d54c9 | 2009-03-09 22:07:42 -0400 | [diff] [blame] | 1542 | } |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 1543 | |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1544 | return call_crda(last_request->alpha2); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Luis R. Rodriguez | 30a548c | 2009-05-02 01:17:27 -0400 | [diff] [blame] | 1547 | /* This processes *all* regulatory hints */ |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1548 | static void reg_process_hint(struct regulatory_request *reg_request) |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1549 | { |
| 1550 | int r = 0; |
| 1551 | struct wiphy *wiphy = NULL; |
| 1552 | |
| 1553 | BUG_ON(!reg_request->alpha2); |
| 1554 | |
| 1555 | mutex_lock(&cfg80211_mutex); |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1556 | mutex_lock(®_mutex); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1557 | |
| 1558 | if (wiphy_idx_valid(reg_request->wiphy_idx)) |
| 1559 | wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx); |
| 1560 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1561 | if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER && |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1562 | !wiphy) { |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1563 | kfree(reg_request); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1564 | goto out; |
| 1565 | } |
| 1566 | |
Luis R. Rodriguez | 28da32d | 2009-02-21 00:24:14 -0500 | [diff] [blame] | 1567 | r = __regulatory_hint(wiphy, reg_request); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1568 | /* This is required so that the orig_* parameters are saved */ |
Johannes Berg | 5be83de | 2009-11-19 00:56:28 +0100 | [diff] [blame] | 1569 | if (r == -EALREADY && wiphy && |
| 1570 | wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1571 | wiphy_update_regulatory(wiphy, reg_request->initiator); |
| 1572 | out: |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1573 | mutex_unlock(®_mutex); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1574 | mutex_unlock(&cfg80211_mutex); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1575 | } |
| 1576 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1577 | /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */ |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1578 | static void reg_process_pending_hints(void) |
| 1579 | { |
| 1580 | struct regulatory_request *reg_request; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1581 | |
| 1582 | spin_lock(®_requests_lock); |
| 1583 | while (!list_empty(®_requests_list)) { |
| 1584 | reg_request = list_first_entry(®_requests_list, |
| 1585 | struct regulatory_request, |
| 1586 | list); |
| 1587 | list_del_init(®_request->list); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1588 | |
Luis R. Rodriguez | d951c1d | 2009-02-21 00:24:15 -0500 | [diff] [blame] | 1589 | spin_unlock(®_requests_lock); |
| 1590 | reg_process_hint(reg_request); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1591 | spin_lock(®_requests_lock); |
| 1592 | } |
| 1593 | spin_unlock(®_requests_lock); |
| 1594 | } |
| 1595 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1596 | /* Processes beacon hints -- this has nothing to do with country IEs */ |
| 1597 | static void reg_process_pending_beacon_hints(void) |
| 1598 | { |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 1599 | struct cfg80211_registered_device *rdev; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1600 | struct reg_beacon *pending_beacon, *tmp; |
| 1601 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1602 | /* |
| 1603 | * No need to hold the reg_mutex here as we just touch wiphys |
| 1604 | * and do not read or access regulatory variables. |
| 1605 | */ |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1606 | mutex_lock(&cfg80211_mutex); |
| 1607 | |
| 1608 | /* This goes through the _pending_ beacon list */ |
| 1609 | spin_lock_bh(®_pending_beacons_lock); |
| 1610 | |
| 1611 | if (list_empty(®_pending_beacons)) { |
| 1612 | spin_unlock_bh(®_pending_beacons_lock); |
| 1613 | goto out; |
| 1614 | } |
| 1615 | |
| 1616 | list_for_each_entry_safe(pending_beacon, tmp, |
| 1617 | ®_pending_beacons, list) { |
| 1618 | |
| 1619 | list_del_init(&pending_beacon->list); |
| 1620 | |
| 1621 | /* Applies the beacon hint to current wiphys */ |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 1622 | list_for_each_entry(rdev, &cfg80211_rdev_list, list) |
| 1623 | wiphy_update_new_beacon(&rdev->wiphy, pending_beacon); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1624 | |
| 1625 | /* Remembers the beacon hint for new wiphys or reg changes */ |
| 1626 | list_add_tail(&pending_beacon->list, ®_beacon_list); |
| 1627 | } |
| 1628 | |
| 1629 | spin_unlock_bh(®_pending_beacons_lock); |
| 1630 | out: |
| 1631 | mutex_unlock(&cfg80211_mutex); |
| 1632 | } |
| 1633 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1634 | static void reg_todo(struct work_struct *work) |
| 1635 | { |
| 1636 | reg_process_pending_hints(); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1637 | reg_process_pending_beacon_hints(); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | static DECLARE_WORK(reg_work, reg_todo); |
| 1641 | |
| 1642 | static void queue_regulatory_request(struct regulatory_request *request) |
| 1643 | { |
| 1644 | spin_lock(®_requests_lock); |
| 1645 | list_add_tail(&request->list, ®_requests_list); |
| 1646 | spin_unlock(®_requests_lock); |
| 1647 | |
| 1648 | schedule_work(®_work); |
| 1649 | } |
| 1650 | |
| 1651 | /* Core regulatory hint -- happens once during cfg80211_init() */ |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 1652 | static int regulatory_hint_core(const char *alpha2) |
| 1653 | { |
| 1654 | struct regulatory_request *request; |
| 1655 | |
| 1656 | BUG_ON(last_request); |
| 1657 | |
| 1658 | request = kzalloc(sizeof(struct regulatory_request), |
| 1659 | GFP_KERNEL); |
| 1660 | if (!request) |
| 1661 | return -ENOMEM; |
| 1662 | |
| 1663 | request->alpha2[0] = alpha2[0]; |
| 1664 | request->alpha2[1] = alpha2[1]; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1665 | request->initiator = NL80211_REGDOM_SET_BY_CORE; |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 1666 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1667 | queue_regulatory_request(request); |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 1668 | |
Luis R. Rodriguez | 5078b2e | 2009-05-13 17:04:42 -0400 | [diff] [blame] | 1669 | /* |
| 1670 | * This ensures last_request is populated once modules |
| 1671 | * come swinging in and calling regulatory hints and |
| 1672 | * wiphy_apply_custom_regulatory(). |
| 1673 | */ |
| 1674 | flush_scheduled_work(); |
| 1675 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1676 | return 0; |
Luis R. Rodriguez | ba25c14 | 2009-02-21 00:04:23 -0500 | [diff] [blame] | 1677 | } |
| 1678 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1679 | /* User hints */ |
| 1680 | int regulatory_hint_user(const char *alpha2) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1681 | { |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1682 | struct regulatory_request *request; |
| 1683 | |
Johannes Berg | be3d481 | 2008-10-24 20:32:21 +0200 | [diff] [blame] | 1684 | BUG_ON(!alpha2); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1685 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1686 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
| 1687 | if (!request) |
| 1688 | return -ENOMEM; |
| 1689 | |
| 1690 | request->wiphy_idx = WIPHY_IDX_STALE; |
| 1691 | request->alpha2[0] = alpha2[0]; |
| 1692 | request->alpha2[1] = alpha2[1]; |
Luis R. Rodriguez | e12822e | 2010-01-04 11:37:39 -0500 | [diff] [blame^] | 1693 | request->initiator = NL80211_REGDOM_SET_BY_USER; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1694 | |
| 1695 | queue_regulatory_request(request); |
| 1696 | |
| 1697 | return 0; |
| 1698 | } |
| 1699 | |
| 1700 | /* Driver hints */ |
| 1701 | int regulatory_hint(struct wiphy *wiphy, const char *alpha2) |
| 1702 | { |
| 1703 | struct regulatory_request *request; |
| 1704 | |
| 1705 | BUG_ON(!alpha2); |
| 1706 | BUG_ON(!wiphy); |
| 1707 | |
| 1708 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
| 1709 | if (!request) |
| 1710 | return -ENOMEM; |
| 1711 | |
| 1712 | request->wiphy_idx = get_wiphy_idx(wiphy); |
| 1713 | |
| 1714 | /* Must have registered wiphy first */ |
| 1715 | BUG_ON(!wiphy_idx_valid(request->wiphy_idx)); |
| 1716 | |
| 1717 | request->alpha2[0] = alpha2[0]; |
| 1718 | request->alpha2[1] = alpha2[1]; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1719 | request->initiator = NL80211_REGDOM_SET_BY_DRIVER; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1720 | |
| 1721 | queue_regulatory_request(request); |
| 1722 | |
| 1723 | return 0; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1724 | } |
| 1725 | EXPORT_SYMBOL(regulatory_hint); |
| 1726 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1727 | /* Caller must hold reg_mutex */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1728 | static bool reg_same_country_ie_hint(struct wiphy *wiphy, |
| 1729 | u32 country_ie_checksum) |
| 1730 | { |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 1731 | struct wiphy *request_wiphy; |
| 1732 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1733 | assert_reg_lock(); |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 1734 | |
Luis R. Rodriguez | cc0b6fe | 2009-03-20 23:53:05 -0400 | [diff] [blame] | 1735 | if (unlikely(last_request->initiator != |
| 1736 | NL80211_REGDOM_SET_BY_COUNTRY_IE)) |
| 1737 | return false; |
| 1738 | |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 1739 | request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
| 1740 | |
| 1741 | if (!request_wiphy) |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1742 | return false; |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 1743 | |
| 1744 | if (likely(request_wiphy != wiphy)) |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1745 | return !country_ie_integrity_changes(country_ie_checksum); |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1746 | /* |
| 1747 | * We should not have let these through at this point, they |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1748 | * should have been picked up earlier by the first alpha2 check |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1749 | * on the device |
| 1750 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1751 | if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum))) |
| 1752 | return true; |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
Luis R. Rodriguez | 4b44c8b | 2009-07-30 17:38:07 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and |
| 1758 | * therefore cannot iterate over the rdev list here. |
| 1759 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1760 | void regulatory_hint_11d(struct wiphy *wiphy, |
| 1761 | u8 *country_ie, |
| 1762 | u8 country_ie_len) |
| 1763 | { |
| 1764 | struct ieee80211_regdomain *rd = NULL; |
| 1765 | char alpha2[2]; |
| 1766 | u32 checksum = 0; |
| 1767 | enum environment_cap env = ENVIRON_ANY; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1768 | struct regulatory_request *request; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1769 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1770 | mutex_lock(®_mutex); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1771 | |
Luis R. Rodriguez | 9828b01 | 2009-07-30 17:38:06 -0700 | [diff] [blame] | 1772 | if (unlikely(!last_request)) |
| 1773 | goto out; |
Luis R. Rodriguez | d335fe6 | 2009-02-21 00:04:27 -0500 | [diff] [blame] | 1774 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1775 | /* IE len must be evenly divisible by 2 */ |
| 1776 | if (country_ie_len & 0x01) |
| 1777 | goto out; |
| 1778 | |
| 1779 | if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) |
| 1780 | goto out; |
| 1781 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1782 | /* |
| 1783 | * Pending country IE processing, this can happen after we |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1784 | * call CRDA and wait for a response if a beacon was received before |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1785 | * we were able to process the last regulatory_hint_11d() call |
| 1786 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1787 | if (country_ie_regdomain) |
| 1788 | goto out; |
| 1789 | |
| 1790 | alpha2[0] = country_ie[0]; |
| 1791 | alpha2[1] = country_ie[1]; |
| 1792 | |
| 1793 | if (country_ie[2] == 'I') |
| 1794 | env = ENVIRON_INDOOR; |
| 1795 | else if (country_ie[2] == 'O') |
| 1796 | env = ENVIRON_OUTDOOR; |
| 1797 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1798 | /* |
Luis R. Rodriguez | 8b19e6c | 2009-07-30 17:38:09 -0700 | [diff] [blame] | 1799 | * We will run this only upon a successful connection on cfg80211. |
Luis R. Rodriguez | 4b44c8b | 2009-07-30 17:38:07 -0700 | [diff] [blame] | 1800 | * We leave conflict resolution to the workqueue, where can hold |
| 1801 | * cfg80211_mutex. |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1802 | */ |
Luis R. Rodriguez | cc0b6fe | 2009-03-20 23:53:05 -0400 | [diff] [blame] | 1803 | if (likely(last_request->initiator == |
| 1804 | NL80211_REGDOM_SET_BY_COUNTRY_IE && |
Luis R. Rodriguez | 4b44c8b | 2009-07-30 17:38:07 -0700 | [diff] [blame] | 1805 | wiphy_idx_valid(last_request->wiphy_idx))) |
| 1806 | goto out; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1807 | |
| 1808 | rd = country_ie_2_rd(country_ie, country_ie_len, &checksum); |
| 1809 | if (!rd) |
| 1810 | goto out; |
| 1811 | |
Luis R. Rodriguez | 915278e | 2009-02-21 00:04:28 -0500 | [diff] [blame] | 1812 | /* |
| 1813 | * This will not happen right now but we leave it here for the |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1814 | * the future when we want to add suspend/resume support and having |
| 1815 | * the user move to another country after doing so, or having the user |
Luis R. Rodriguez | 915278e | 2009-02-21 00:04:28 -0500 | [diff] [blame] | 1816 | * move to another AP. Right now we just trust the first AP. |
| 1817 | * |
| 1818 | * If we hit this before we add this support we want to be informed of |
| 1819 | * it as it would indicate a mistake in the current design |
| 1820 | */ |
| 1821 | if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum))) |
Luis R. Rodriguez | 0441d6f | 2009-02-21 00:04:29 -0500 | [diff] [blame] | 1822 | goto free_rd_out; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1823 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1824 | request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); |
| 1825 | if (!request) |
| 1826 | goto free_rd_out; |
| 1827 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1828 | /* |
| 1829 | * We keep this around for when CRDA comes back with a response so |
| 1830 | * we can intersect with that |
| 1831 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1832 | country_ie_regdomain = rd; |
| 1833 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1834 | request->wiphy_idx = get_wiphy_idx(wiphy); |
| 1835 | request->alpha2[0] = rd->alpha2[0]; |
| 1836 | request->alpha2[1] = rd->alpha2[1]; |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1837 | request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1838 | request->country_ie_checksum = checksum; |
| 1839 | request->country_ie_env = env; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1840 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1841 | mutex_unlock(®_mutex); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 1842 | |
| 1843 | queue_regulatory_request(request); |
| 1844 | |
| 1845 | return; |
Luis R. Rodriguez | 0441d6f | 2009-02-21 00:04:29 -0500 | [diff] [blame] | 1846 | |
| 1847 | free_rd_out: |
| 1848 | kfree(rd); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1849 | out: |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 1850 | mutex_unlock(®_mutex); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1851 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1852 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 1853 | static bool freq_is_chan_12_13_14(u16 freq) |
| 1854 | { |
| 1855 | if (freq == ieee80211_channel_to_frequency(12) || |
| 1856 | freq == ieee80211_channel_to_frequency(13) || |
| 1857 | freq == ieee80211_channel_to_frequency(14)) |
| 1858 | return true; |
| 1859 | return false; |
| 1860 | } |
| 1861 | |
| 1862 | int regulatory_hint_found_beacon(struct wiphy *wiphy, |
| 1863 | struct ieee80211_channel *beacon_chan, |
| 1864 | gfp_t gfp) |
| 1865 | { |
| 1866 | struct reg_beacon *reg_beacon; |
| 1867 | |
| 1868 | if (likely((beacon_chan->beacon_found || |
| 1869 | (beacon_chan->flags & IEEE80211_CHAN_RADAR) || |
| 1870 | (beacon_chan->band == IEEE80211_BAND_2GHZ && |
| 1871 | !freq_is_chan_12_13_14(beacon_chan->center_freq))))) |
| 1872 | return 0; |
| 1873 | |
| 1874 | reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp); |
| 1875 | if (!reg_beacon) |
| 1876 | return -ENOMEM; |
| 1877 | |
| 1878 | #ifdef CONFIG_CFG80211_REG_DEBUG |
| 1879 | printk(KERN_DEBUG "cfg80211: Found new beacon on " |
| 1880 | "frequency: %d MHz (Ch %d) on %s\n", |
| 1881 | beacon_chan->center_freq, |
| 1882 | ieee80211_frequency_to_channel(beacon_chan->center_freq), |
| 1883 | wiphy_name(wiphy)); |
| 1884 | #endif |
| 1885 | memcpy(®_beacon->chan, beacon_chan, |
| 1886 | sizeof(struct ieee80211_channel)); |
| 1887 | |
| 1888 | |
| 1889 | /* |
| 1890 | * Since we can be called from BH or and non-BH context |
| 1891 | * we must use spin_lock_bh() |
| 1892 | */ |
| 1893 | spin_lock_bh(®_pending_beacons_lock); |
| 1894 | list_add_tail(®_beacon->list, ®_pending_beacons); |
| 1895 | spin_unlock_bh(®_pending_beacons_lock); |
| 1896 | |
| 1897 | schedule_work(®_work); |
| 1898 | |
| 1899 | return 0; |
| 1900 | } |
| 1901 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1902 | static void print_rd_rules(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1903 | { |
| 1904 | unsigned int i; |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1905 | const struct ieee80211_reg_rule *reg_rule = NULL; |
| 1906 | const struct ieee80211_freq_range *freq_range = NULL; |
| 1907 | const struct ieee80211_power_rule *power_rule = NULL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1908 | |
Kalle Valo | 269ac5f | 2009-12-01 10:47:15 +0200 | [diff] [blame] | 1909 | printk(KERN_INFO " (start_freq - end_freq @ bandwidth), " |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1910 | "(max_antenna_gain, max_eirp)\n"); |
| 1911 | |
| 1912 | for (i = 0; i < rd->n_reg_rules; i++) { |
| 1913 | reg_rule = &rd->reg_rules[i]; |
| 1914 | freq_range = ®_rule->freq_range; |
| 1915 | power_rule = ®_rule->power_rule; |
| 1916 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 1917 | /* |
| 1918 | * There may not be documentation for max antenna gain |
| 1919 | * in certain regions |
| 1920 | */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1921 | if (power_rule->max_antenna_gain) |
Kalle Valo | 269ac5f | 2009-12-01 10:47:15 +0200 | [diff] [blame] | 1922 | printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), " |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1923 | "(%d mBi, %d mBm)\n", |
| 1924 | freq_range->start_freq_khz, |
| 1925 | freq_range->end_freq_khz, |
| 1926 | freq_range->max_bandwidth_khz, |
| 1927 | power_rule->max_antenna_gain, |
| 1928 | power_rule->max_eirp); |
| 1929 | else |
Kalle Valo | 269ac5f | 2009-12-01 10:47:15 +0200 | [diff] [blame] | 1930 | printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), " |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1931 | "(N/A, %d mBm)\n", |
| 1932 | freq_range->start_freq_khz, |
| 1933 | freq_range->end_freq_khz, |
| 1934 | freq_range->max_bandwidth_khz, |
| 1935 | power_rule->max_eirp); |
| 1936 | } |
| 1937 | } |
| 1938 | |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 1939 | static void print_regdomain(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1940 | { |
| 1941 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1942 | if (is_intersected_alpha2(rd->alpha2)) { |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1943 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 1944 | if (last_request->initiator == |
| 1945 | NL80211_REGDOM_SET_BY_COUNTRY_IE) { |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 1946 | struct cfg80211_registered_device *rdev; |
| 1947 | rdev = cfg80211_rdev_by_wiphy_idx( |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 1948 | last_request->wiphy_idx); |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 1949 | if (rdev) { |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1950 | printk(KERN_INFO "cfg80211: Current regulatory " |
| 1951 | "domain updated by AP to: %c%c\n", |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 1952 | rdev->country_ie_alpha2[0], |
| 1953 | rdev->country_ie_alpha2[1]); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1954 | } else |
| 1955 | printk(KERN_INFO "cfg80211: Current regulatory " |
| 1956 | "domain intersected: \n"); |
| 1957 | } else |
| 1958 | printk(KERN_INFO "cfg80211: Current regulatory " |
Luis R. Rodriguez | 039498c | 2009-01-07 17:43:35 -0800 | [diff] [blame] | 1959 | "domain intersected: \n"); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1960 | } else if (is_world_regdom(rd->alpha2)) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1961 | printk(KERN_INFO "cfg80211: World regulatory " |
| 1962 | "domain updated:\n"); |
| 1963 | else { |
| 1964 | if (is_unknown_alpha2(rd->alpha2)) |
| 1965 | printk(KERN_INFO "cfg80211: Regulatory domain " |
| 1966 | "changed to driver built-in settings " |
| 1967 | "(unknown country)\n"); |
| 1968 | else |
| 1969 | printk(KERN_INFO "cfg80211: Regulatory domain " |
| 1970 | "changed to country: %c%c\n", |
| 1971 | rd->alpha2[0], rd->alpha2[1]); |
| 1972 | } |
| 1973 | print_rd_rules(rd); |
| 1974 | } |
| 1975 | |
Johannes Berg | 2df7816 | 2008-10-28 16:49:41 +0100 | [diff] [blame] | 1976 | static void print_regdomain_info(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 1977 | { |
| 1978 | printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n", |
| 1979 | rd->alpha2[0], rd->alpha2[1]); |
| 1980 | print_rd_rules(rd); |
| 1981 | } |
| 1982 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1983 | #ifdef CONFIG_CFG80211_REG_DEBUG |
| 1984 | static void reg_country_ie_process_debug( |
| 1985 | const struct ieee80211_regdomain *rd, |
| 1986 | const struct ieee80211_regdomain *country_ie_regdomain, |
| 1987 | const struct ieee80211_regdomain *intersected_rd) |
| 1988 | { |
| 1989 | printk(KERN_DEBUG "cfg80211: Received country IE:\n"); |
| 1990 | print_regdomain_info(country_ie_regdomain); |
| 1991 | printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n"); |
| 1992 | print_regdomain_info(rd); |
| 1993 | if (intersected_rd) { |
| 1994 | printk(KERN_DEBUG "cfg80211: We intersect both of these " |
| 1995 | "and get:\n"); |
Luis R. Rodriguez | 667ecd0 | 2009-01-22 15:05:43 -0800 | [diff] [blame] | 1996 | print_regdomain_info(intersected_rd); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 1997 | return; |
| 1998 | } |
| 1999 | printk(KERN_DEBUG "cfg80211: Intersection between both failed\n"); |
| 2000 | } |
| 2001 | #else |
| 2002 | static inline void reg_country_ie_process_debug( |
| 2003 | const struct ieee80211_regdomain *rd, |
| 2004 | const struct ieee80211_regdomain *country_ie_regdomain, |
| 2005 | const struct ieee80211_regdomain *intersected_rd) |
| 2006 | { |
| 2007 | } |
| 2008 | #endif |
| 2009 | |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 2010 | /* Takes ownership of rd only if it doesn't fail */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 2011 | static int __set_regdom(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2012 | { |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 2013 | const struct ieee80211_regdomain *intersected_rd = NULL; |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 2014 | struct cfg80211_registered_device *rdev = NULL; |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 2015 | struct wiphy *request_wiphy; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2016 | /* Some basic sanity checks first */ |
| 2017 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2018 | if (is_world_regdom(rd->alpha2)) { |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 2019 | if (WARN_ON(!reg_is_valid_request(rd->alpha2))) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2020 | return -EINVAL; |
| 2021 | update_world_regdomain(rd); |
| 2022 | return 0; |
| 2023 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2024 | |
| 2025 | if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) && |
| 2026 | !is_unknown_alpha2(rd->alpha2)) |
| 2027 | return -EINVAL; |
| 2028 | |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 2029 | if (!last_request) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2030 | return -EINVAL; |
| 2031 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2032 | /* |
| 2033 | * Lets only bother proceeding on the same alpha2 if the current |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2034 | * rd is non static (it means CRDA was present and was used last) |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2035 | * and the pending request came in from a country IE |
| 2036 | */ |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 2037 | if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) { |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2038 | /* |
| 2039 | * If someone else asked us to change the rd lets only bother |
| 2040 | * checking if the alpha2 changes if CRDA was already called |
| 2041 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2042 | if (!is_old_static_regdom(cfg80211_regdomain) && |
Luis R. Rodriguez | 69b1572 | 2009-02-21 00:04:33 -0500 | [diff] [blame] | 2043 | !regdom_changes(rd->alpha2)) |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2044 | return -EINVAL; |
| 2045 | } |
| 2046 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2047 | /* |
| 2048 | * Now lets set the regulatory domain, update all driver channels |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2049 | * and finally inform them of what we have done, in case they want |
| 2050 | * to review or adjust their own settings based on their own |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2051 | * internal EEPROM data |
| 2052 | */ |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2053 | |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 2054 | if (WARN_ON(!reg_is_valid_request(rd->alpha2))) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2055 | return -EINVAL; |
| 2056 | |
Luis R. Rodriguez | 8375af3 | 2008-11-12 14:21:57 -0800 | [diff] [blame] | 2057 | if (!is_valid_rd(rd)) { |
| 2058 | printk(KERN_ERR "cfg80211: Invalid " |
| 2059 | "regulatory domain detected:\n"); |
| 2060 | print_regdomain_info(rd); |
| 2061 | return -EINVAL; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 2064 | request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
| 2065 | |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 2066 | if (!last_request->intersect) { |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 2067 | int r; |
| 2068 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 2069 | if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) { |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 2070 | reset_regdomains(); |
| 2071 | cfg80211_regdomain = rd; |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2075 | /* |
| 2076 | * For a driver hint, lets copy the regulatory domain the |
| 2077 | * driver wanted to the wiphy to deal with conflicts |
| 2078 | */ |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 2079 | |
Luis R. Rodriguez | 558f6d3 | 2009-06-08 18:54:37 -0700 | [diff] [blame] | 2080 | /* |
| 2081 | * Userspace could have sent two replies with only |
| 2082 | * one kernel request. |
| 2083 | */ |
| 2084 | if (request_wiphy->regd) |
| 2085 | return -EALREADY; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 2086 | |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 2087 | r = reg_copy_regd(&request_wiphy->regd, rd); |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 2088 | if (r) |
| 2089 | return r; |
| 2090 | |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 2091 | reset_regdomains(); |
| 2092 | cfg80211_regdomain = rd; |
| 2093 | return 0; |
| 2094 | } |
| 2095 | |
| 2096 | /* Intersection requires a bit more work */ |
| 2097 | |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 2098 | if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) { |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 2099 | |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 2100 | intersected_rd = regdom_intersect(rd, cfg80211_regdomain); |
| 2101 | if (!intersected_rd) |
| 2102 | return -EINVAL; |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 2103 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2104 | /* |
| 2105 | * We can trash what CRDA provided now. |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 2106 | * However if a driver requested this specific regulatory |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2107 | * domain we keep it for its private use |
| 2108 | */ |
Luis R. Rodriguez | 7db90f4 | 2009-03-09 22:07:41 -0400 | [diff] [blame] | 2109 | if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER) |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 2110 | request_wiphy->regd = rd; |
Luis R. Rodriguez | 3e0c3ff | 2009-01-07 17:43:34 -0800 | [diff] [blame] | 2111 | else |
| 2112 | kfree(rd); |
| 2113 | |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 2114 | rd = NULL; |
| 2115 | |
| 2116 | reset_regdomains(); |
| 2117 | cfg80211_regdomain = intersected_rd; |
| 2118 | |
| 2119 | return 0; |
Luis R. Rodriguez | 9c96477 | 2008-10-30 13:33:53 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2122 | /* |
| 2123 | * Country IE requests are handled a bit differently, we intersect |
| 2124 | * the country IE rd with what CRDA believes that country should have |
| 2125 | */ |
| 2126 | |
Luis R. Rodriguez | 729e9c7 | 2009-05-31 18:24:34 -0400 | [diff] [blame] | 2127 | /* |
| 2128 | * Userspace could have sent two replies with only |
| 2129 | * one kernel request. By the second reply we would have |
| 2130 | * already processed and consumed the country_ie_regdomain. |
| 2131 | */ |
| 2132 | if (!country_ie_regdomain) |
| 2133 | return -EALREADY; |
Luis R. Rodriguez | 86f0468 | 2009-03-20 23:53:07 -0400 | [diff] [blame] | 2134 | BUG_ON(rd == country_ie_regdomain); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2135 | |
Luis R. Rodriguez | 86f0468 | 2009-03-20 23:53:07 -0400 | [diff] [blame] | 2136 | /* |
| 2137 | * Intersect what CRDA returned and our what we |
| 2138 | * had built from the Country IE received |
| 2139 | */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2140 | |
Luis R. Rodriguez | 86f0468 | 2009-03-20 23:53:07 -0400 | [diff] [blame] | 2141 | intersected_rd = regdom_intersect(rd, country_ie_regdomain); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2142 | |
Luis R. Rodriguez | 86f0468 | 2009-03-20 23:53:07 -0400 | [diff] [blame] | 2143 | reg_country_ie_process_debug(rd, |
| 2144 | country_ie_regdomain, |
| 2145 | intersected_rd); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2146 | |
Luis R. Rodriguez | 86f0468 | 2009-03-20 23:53:07 -0400 | [diff] [blame] | 2147 | kfree(country_ie_regdomain); |
| 2148 | country_ie_regdomain = NULL; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2149 | |
| 2150 | if (!intersected_rd) |
| 2151 | return -EINVAL; |
| 2152 | |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 2153 | rdev = wiphy_to_dev(request_wiphy); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2154 | |
Johannes Berg | 79c97e9 | 2009-07-07 03:56:12 +0200 | [diff] [blame] | 2155 | rdev->country_ie_alpha2[0] = rd->alpha2[0]; |
| 2156 | rdev->country_ie_alpha2[1] = rd->alpha2[1]; |
| 2157 | rdev->env = last_request->country_ie_env; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2158 | |
| 2159 | BUG_ON(intersected_rd == rd); |
| 2160 | |
| 2161 | kfree(rd); |
| 2162 | rd = NULL; |
| 2163 | |
Luis R. Rodriguez | b8295ac | 2008-11-12 14:21:58 -0800 | [diff] [blame] | 2164 | reset_regdomains(); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2165 | cfg80211_regdomain = intersected_rd; |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2166 | |
| 2167 | return 0; |
| 2168 | } |
| 2169 | |
| 2170 | |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2171 | /* |
| 2172 | * Use this call to set the current regulatory domain. Conflicts with |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2173 | * multiple drivers can be ironed out later. Caller must've already |
Luis R. Rodriguez | fb1fc7a | 2009-02-21 00:04:31 -0500 | [diff] [blame] | 2174 | * kmalloc'd the rd structure. Caller must hold cfg80211_mutex |
| 2175 | */ |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 2176 | int set_regdom(const struct ieee80211_regdomain *rd) |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2177 | { |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2178 | int r; |
| 2179 | |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 2180 | assert_cfg80211_lock(); |
| 2181 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 2182 | mutex_lock(®_mutex); |
| 2183 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2184 | /* Note that this doesn't update the wiphys, this is done below */ |
| 2185 | r = __set_regdom(rd); |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 2186 | if (r) { |
| 2187 | kfree(rd); |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 2188 | mutex_unlock(®_mutex); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2189 | return r; |
Johannes Berg | d2372b3 | 2008-10-24 20:32:20 +0200 | [diff] [blame] | 2190 | } |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2191 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2192 | /* This would make this whole thing pointless */ |
Luis R. Rodriguez | a01ddaf | 2008-11-12 14:21:59 -0800 | [diff] [blame] | 2193 | if (!last_request->intersect) |
| 2194 | BUG_ON(rd != cfg80211_regdomain); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2195 | |
| 2196 | /* update all wiphys now with the new established regulatory domain */ |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 2197 | update_all_wiphy_regulatory(last_request->initiator); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2198 | |
Luis R. Rodriguez | a01ddaf | 2008-11-12 14:21:59 -0800 | [diff] [blame] | 2199 | print_regdomain(cfg80211_regdomain); |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2200 | |
Luis R. Rodriguez | 73d54c9 | 2009-03-09 22:07:42 -0400 | [diff] [blame] | 2201 | nl80211_send_reg_change_event(last_request); |
| 2202 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 2203 | mutex_unlock(®_mutex); |
| 2204 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2205 | return r; |
| 2206 | } |
| 2207 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame] | 2208 | /* Caller must hold cfg80211_mutex */ |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2209 | void reg_device_remove(struct wiphy *wiphy) |
| 2210 | { |
Luis R. Rodriguez | 0ad8aca | 2009-03-24 21:21:08 -0400 | [diff] [blame] | 2211 | struct wiphy *request_wiphy = NULL; |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 2212 | |
Luis R. Rodriguez | 761cf7e | 2009-02-21 00:04:25 -0500 | [diff] [blame] | 2213 | assert_cfg80211_lock(); |
| 2214 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 2215 | mutex_lock(®_mutex); |
| 2216 | |
Chris Wright | 0ef9ccd | 2009-04-24 14:09:31 -0700 | [diff] [blame] | 2217 | kfree(wiphy->regd); |
| 2218 | |
Luis R. Rodriguez | 0ad8aca | 2009-03-24 21:21:08 -0400 | [diff] [blame] | 2219 | if (last_request) |
| 2220 | request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx); |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 2221 | |
Chris Wright | 0ef9ccd | 2009-04-24 14:09:31 -0700 | [diff] [blame] | 2222 | if (!request_wiphy || request_wiphy != wiphy) |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 2223 | goto out; |
Chris Wright | 0ef9ccd | 2009-04-24 14:09:31 -0700 | [diff] [blame] | 2224 | |
Luis R. Rodriguez | 806a9e3 | 2009-02-21 00:04:26 -0500 | [diff] [blame] | 2225 | last_request->wiphy_idx = WIPHY_IDX_STALE; |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2226 | last_request->country_ie_env = ENVIRON_ANY; |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 2227 | out: |
| 2228 | mutex_unlock(®_mutex); |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2229 | } |
| 2230 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2231 | int regulatory_init(void) |
| 2232 | { |
Luis R. Rodriguez | bcf4f99 | 2009-02-21 00:04:24 -0500 | [diff] [blame] | 2233 | int err = 0; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2234 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2235 | reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0); |
| 2236 | if (IS_ERR(reg_pdev)) |
| 2237 | return PTR_ERR(reg_pdev); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2238 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2239 | spin_lock_init(®_requests_lock); |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2240 | spin_lock_init(®_pending_beacons_lock); |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2241 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2242 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 2243 | cfg80211_regdomain = static_regdom(ieee80211_regdom); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2244 | |
Johannes Berg | 942b25c | 2008-09-15 11:26:47 +0200 | [diff] [blame] | 2245 | printk(KERN_INFO "cfg80211: Using static regulatory domain info\n"); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2246 | print_regdomain_info(cfg80211_regdomain); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2247 | #else |
Johannes Berg | a3d2eaf | 2008-09-15 11:10:52 +0200 | [diff] [blame] | 2248 | cfg80211_regdomain = cfg80211_world_regdom; |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2249 | |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2250 | #endif |
Luis R. Rodriguez | ae9e4b0 | 2009-07-14 20:23:15 -0400 | [diff] [blame] | 2251 | /* We always try to get an update for the static regdomain */ |
| 2252 | err = regulatory_hint_core(cfg80211_regdomain->alpha2); |
Luis R. Rodriguez | bcf4f99 | 2009-02-21 00:04:24 -0500 | [diff] [blame] | 2253 | if (err) { |
| 2254 | if (err == -ENOMEM) |
| 2255 | return err; |
| 2256 | /* |
| 2257 | * N.B. kobject_uevent_env() can fail mainly for when we're out |
| 2258 | * memory which is handled and propagated appropriately above |
| 2259 | * but it can also fail during a netlink_broadcast() or during |
| 2260 | * early boot for call_usermodehelper(). For now treat these |
| 2261 | * errors as non-fatal. |
| 2262 | */ |
| 2263 | printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable " |
| 2264 | "to call CRDA during init"); |
| 2265 | #ifdef CONFIG_CFG80211_REG_DEBUG |
| 2266 | /* We want to find out exactly why when debugging */ |
| 2267 | WARN_ON(err); |
| 2268 | #endif |
| 2269 | } |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2270 | |
Luis R. Rodriguez | ae9e4b0 | 2009-07-14 20:23:15 -0400 | [diff] [blame] | 2271 | /* |
| 2272 | * Finally, if the user set the module parameter treat it |
| 2273 | * as a user hint. |
| 2274 | */ |
| 2275 | if (!is_world_regdom(ieee80211_regdom)) |
| 2276 | regulatory_hint_user(ieee80211_regdom); |
| 2277 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | void regulatory_exit(void) |
| 2282 | { |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2283 | struct regulatory_request *reg_request, *tmp; |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2284 | struct reg_beacon *reg_beacon, *btmp; |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2285 | |
| 2286 | cancel_work_sync(®_work); |
| 2287 | |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame] | 2288 | mutex_lock(&cfg80211_mutex); |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 2289 | mutex_lock(®_mutex); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2290 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2291 | reset_regdomains(); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2292 | |
Luis R. Rodriguez | 3f2355c | 2008-11-12 14:22:02 -0800 | [diff] [blame] | 2293 | kfree(country_ie_regdomain); |
| 2294 | country_ie_regdomain = NULL; |
| 2295 | |
Johannes Berg | f6037d0 | 2008-10-21 11:01:33 +0200 | [diff] [blame] | 2296 | kfree(last_request); |
| 2297 | |
Luis R. Rodriguez | b2e1b30 | 2008-09-09 23:19:48 -0700 | [diff] [blame] | 2298 | platform_device_unregister(reg_pdev); |
Johannes Berg | 734366d | 2008-09-15 10:56:48 +0200 | [diff] [blame] | 2299 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 2300 | spin_lock_bh(®_pending_beacons_lock); |
| 2301 | if (!list_empty(®_pending_beacons)) { |
| 2302 | list_for_each_entry_safe(reg_beacon, btmp, |
| 2303 | ®_pending_beacons, list) { |
| 2304 | list_del(®_beacon->list); |
| 2305 | kfree(reg_beacon); |
| 2306 | } |
| 2307 | } |
| 2308 | spin_unlock_bh(®_pending_beacons_lock); |
| 2309 | |
| 2310 | if (!list_empty(®_beacon_list)) { |
| 2311 | list_for_each_entry_safe(reg_beacon, btmp, |
| 2312 | ®_beacon_list, list) { |
| 2313 | list_del(®_beacon->list); |
| 2314 | kfree(reg_beacon); |
| 2315 | } |
| 2316 | } |
| 2317 | |
Luis R. Rodriguez | fe33eb3 | 2009-02-21 00:04:30 -0500 | [diff] [blame] | 2318 | spin_lock(®_requests_lock); |
| 2319 | if (!list_empty(®_requests_list)) { |
| 2320 | list_for_each_entry_safe(reg_request, tmp, |
| 2321 | ®_requests_list, list) { |
| 2322 | list_del(®_request->list); |
| 2323 | kfree(reg_request); |
| 2324 | } |
| 2325 | } |
| 2326 | spin_unlock(®_requests_lock); |
| 2327 | |
Luis R. Rodriguez | abc7381 | 2009-07-30 17:38:08 -0700 | [diff] [blame] | 2328 | mutex_unlock(®_mutex); |
Luis R. Rodriguez | a179439 | 2009-02-21 00:04:21 -0500 | [diff] [blame] | 2329 | mutex_unlock(&cfg80211_mutex); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 2330 | } |