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