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